blob: fc86a81e36cc46978aeaf31d0ba4810d73f028b7 [file] [log] [blame]
Radek Krejcid91dbaf2018-09-21 15:51:39 +02001/**
2 * @file xml.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko2b979d62022-05-10 09:28:56 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejcid91dbaf2018-09-21 15:51:39 +02005 * @brief Generic XML parser routines.
6 *
Michal Vasko2b979d62022-05-10 09:28:56 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcid91dbaf2018-09-21 15:51:39 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#ifndef LY_XML_H_
17#define LY_XML_H_
18
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020020#include <stdint.h>
21
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include "log.h"
Radek Krejcid91dbaf2018-09-21 15:51:39 +020023#include "set.h"
24
Radek Krejci535ea9f2020-05-29 16:01:05 +020025struct ly_ctx;
Radek Krejci47fab892020-11-05 17:02:41 +010026struct ly_in;
Radek Krejci241f6b52020-05-21 18:13:49 +020027struct ly_out;
Radek Krejcie7b95092019-05-15 11:03:07 +020028
Radek Krejcib1646a92018-11-02 16:08:26 +010029/* Macro to test if character is whitespace */
30#define is_xmlws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd)
31
32/* Macro to test if character is allowed to be a first character of an qualified identifier */
33#define is_xmlqnamestartchar(c) ((c >= 'a' && c <= 'z') || c == '_' || \
34 (c >= 'A' && c <= 'Z') || /* c == ':' || */ \
35 (c >= 0x370 && c <= 0x1fff && c != 0x37e ) || \
36 (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
37 c == 0x200d || (c >= 0x2070 && c <= 0x218f) || \
38 (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
39 (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
40 (c >= 0x10000 && c <= 0xeffff))
41
42/* Macro to test if character is allowed to be used in an qualified identifier */
43#define is_xmlqnamechar(c) ((c >= 'a' && c <= 'z') || c == '_' || c == '-' || \
44 (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || /* c == ':' || */ \
45 c == '.' || c == 0xb7 || (c >= 0x370 && c <= 0x1fff && c != 0x37e ) ||\
46 (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
47 c == 0x200d || (c >= 0x300 && c <= 0x36f) || \
Radek Krejcic8a63902021-02-04 14:48:19 +010048 (c >= 0x2070 && c <= 0x218f) || (c >= 0x203f && c <= 0x2040) || \
Radek Krejcib1646a92018-11-02 16:08:26 +010049 (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
50 (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
51 (c >= 0x10000 && c <= 0xeffff))
52
Radek Krejcid91dbaf2018-09-21 15:51:39 +020053struct lyxml_ns {
Radek Krejci4b74d5e2018-09-26 14:30:55 +020054 char *prefix; /* prefix of the namespace, NULL for the default namespace */
55 char *uri; /* namespace URI */
Michal Vaskob36053d2020-03-26 15:49:30 +010056 uint32_t depth; /* depth level of the element to maintain the list of accessible namespace definitions */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020057};
58
Radek Krejcib1890642018-10-03 14:05:40 +020059/* element tag identifier for matching opening and closing tags */
60struct lyxml_elem {
Michal Vaskoda8fbbf2021-06-16 11:44:44 +020061 const char *prefix; /**< only pointer, not in dictionary */
62 const char *name; /**< only pointer, not in dictionary */
Radek Krejcib1890642018-10-03 14:05:40 +020063 size_t prefix_len;
64 size_t name_len;
65};
66
Radek Krejci28e8cb52019-03-08 11:31:31 +010067/**
68 * @brief Status of the parser providing information what is expected next (which function is supposed to be called).
69 */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020070enum LYXML_PARSER_STATUS {
Michal Vaskob36053d2020-03-26 15:49:30 +010071 LYXML_ELEMENT, /* opening XML element parsed */
72 LYXML_ELEM_CLOSE, /* closing XML element parsed */
73 LYXML_ELEM_CONTENT, /* XML element context parsed */
74 LYXML_ATTRIBUTE, /* XML attribute parsed */
75 LYXML_ATTR_CONTENT, /* XML attribute content parsed */
Radek Krejcib1890642018-10-03 14:05:40 +020076 LYXML_END /* end of input data */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020077};
78
Michal Vaskob36053d2020-03-26 15:49:30 +010079struct lyxml_ctx {
Radek Krejci1798aae2020-07-14 13:26:06 +020080 const struct ly_ctx *ctx;
Radek Krejci1798aae2020-07-14 13:26:06 +020081 struct ly_in *in; /* input structure */
82
Michal Vaskob36053d2020-03-26 15:49:30 +010083 enum LYXML_PARSER_STATUS status; /* status providing information about the last parsed object, following attributes
84 are filled based on it */
Michal Vasko26bbb272022-08-02 14:54:33 +020085
Michal Vaskob36053d2020-03-26 15:49:30 +010086 union {
Michal Vasko8cef5232020-06-15 17:59:47 +020087 const char *prefix; /* LYXML_ELEMENT, LYXML_ATTRIBUTE - elem/attr prefix */
88 const char *value; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT - elem/attr value */
Michal Vaskob36053d2020-03-26 15:49:30 +010089 };
90 union {
Michal Vasko8cef5232020-06-15 17:59:47 +020091 size_t prefix_len; /* LYXML_ELEMENT, LYXML_ATTRIBUTE - elem/attr prefix length */
92 size_t value_len; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT - elem/attr value length */
Michal Vaskob36053d2020-03-26 15:49:30 +010093 };
94 union {
Michal Vasko8cef5232020-06-15 17:59:47 +020095 const char *name; /* LYXML_ELEMENT, LYXML_ATTRIBUTE - elem/attr name */
Radek Krejci857189e2020-09-01 13:26:36 +020096 ly_bool ws_only; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT - whether elem/attr value is empty/white-space only */
Michal Vaskob36053d2020-03-26 15:49:30 +010097 };
98 union {
Michal Vasko8cef5232020-06-15 17:59:47 +020099 size_t name_len; /* LYXML_ELEMENT, LYXML_ATTRIBUTE - elem/attr name length */
Radek Krejci857189e2020-09-01 13:26:36 +0200100 ly_bool dynamic; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT - whether elem/attr value is dynamically allocated */
Michal Vaskob36053d2020-03-26 15:49:30 +0100101 };
102
Radek Krejcib1890642018-10-03 14:05:40 +0200103 struct ly_set elements; /* list of not-yet-closed elements */
Michal Vaskob36053d2020-03-26 15:49:30 +0100104 struct ly_set ns; /* handled with LY_SET_OPT_USEASLIST */
Michal Vasko2b979d62022-05-10 09:28:56 +0200105
106 /* backup in members */
107 const char *b_current;
108 uint64_t b_line;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200109};
110
Michal Vasko8cef5232020-06-15 17:59:47 +0200111/**
112 * @brief Create a new XML parser context and start parsing.
113 *
114 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 * @param[in] in Input structure.
Michal Vasko8cef5232020-06-15 17:59:47 +0200116 * @param[out] xmlctx New XML context with status ::LYXML_ELEMENT.
117 * @return LY_ERR value.
118 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200119LY_ERR lyxml_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyxml_ctx **xmlctx);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200120
Michal Vasko8cef5232020-06-15 17:59:47 +0200121/**
122 * @brief Move to the next XML artefact and update parser status.
123 *
124 * LYXML_ELEMENT (-> LYXML_ATTRIBUTE -> LYXML_ATTR_CONTENT)* -> LYXML_ELEM_CONTENT -> LYXML_ELEM_CLOSE ...
125 * -> LYXML_ELEMENT ...
126 *
127 * @param[in] xmlctx XML context to move.
128 * @return LY_ERR value.
129 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100130LY_ERR lyxml_ctx_next(struct lyxml_ctx *xmlctx);
Michal Vasko52927e22020-03-16 17:26:14 +0100131
Michal Vasko8cef5232020-06-15 17:59:47 +0200132/**
133 * @brief Peek at the next XML parser status without changing it.
134 *
135 * @param[in] xmlctx XML context to use.
136 * @param[out] next Next XML parser status.
137 * @return LY_ERR value.
138 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100139LY_ERR lyxml_ctx_peek(struct lyxml_ctx *xmlctx, enum LYXML_PARSER_STATUS *next);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200140
141/**
Michal Vaskoddd76592022-01-17 13:34:48 +0100142 * @brief Remove all the namespaces defined in the element recently closed (removed from the xmlctx->elements).
143 *
144 * @param[in] xmlctx XML context to work with.
145 */
146void lyxml_ns_rm(struct lyxml_ctx *xmlctx);
147
148/**
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200149 * @brief Get a namespace record for the given prefix in the current context.
150 *
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200151 * @param[in] ns_set Set with namespaces from the XML context.
Radek Krejci84d7fd72021-07-14 18:32:21 +0200152 * @param[in] prefix Pointer to the namespace prefix. Can be NULL for default namespace.
153 * @param[in] prefix_len Length of the prefix string (since it might not be NULL-terminated).
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200154 * @return The namespace record or NULL if the record for the specified prefix not found.
155 */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200156const struct lyxml_ns *lyxml_ns_get(const struct ly_set *ns_set, const char *prefix, size_t prefix_len);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200157
158/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200159 * @brief Print the given @p text as XML string which replaces some of the characters which cannot appear in XML data.
160 *
161 * @param[in] out Output structure for printing.
162 * @param[in] text String to print.
163 * @param[in] attribute Flag for attribute's value where a double quotes must be replaced.
164 * @return LY_ERR values.
165 */
Radek Krejci857189e2020-09-01 13:26:36 +0200166LY_ERR lyxml_dump_text(struct ly_out *out, const char *text, ly_bool attribute);
Radek Krejcie7b95092019-05-15 11:03:07 +0200167
168/**
Radek Krejcib1890642018-10-03 14:05:40 +0200169 * @brief Remove the allocated working memory of the context.
170 *
Michal Vaskob36053d2020-03-26 15:49:30 +0100171 * @param[in] xmlctx XML context to clear.
Radek Krejcib1890642018-10-03 14:05:40 +0200172 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100173void lyxml_ctx_free(struct lyxml_ctx *xmlctx);
Radek Krejcib1890642018-10-03 14:05:40 +0200174
Michal Vasko52927e22020-03-16 17:26:14 +0100175/**
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200176 * @brief Create a backup of XML context.
177 *
178 * @param[in] xmlctx XML context to back up.
179 * @param[out] backup Backup XML context.
180 * @return LY_ERR value.
181 */
182LY_ERR lyxml_ctx_backup(struct lyxml_ctx *xmlctx, struct lyxml_ctx *backup);
183
184/**
185 * @brief Restore previous backup of XML context.
186 *
187 * @param[in,out] xmlctx XML context to restore.
aPiecekb0445f22021-06-24 11:34:07 +0200188 * @param[in] backup Backup XML context to restore, is unusable afterwards.
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200189 */
190void lyxml_ctx_restore(struct lyxml_ctx *xmlctx, struct lyxml_ctx *backup);
191
192/**
Michal Vasko52927e22020-03-16 17:26:14 +0100193 * @brief Compare values and their prefix mappings.
194 *
aPiecek2f63f952021-03-30 12:22:18 +0200195 * @param[in] ctx1 Libyang context for resolving prefixes in @p value1.
Michal Vasko52927e22020-03-16 17:26:14 +0100196 * @param[in] value1 First value.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100197 * @param[in] val_prefix_data1 First value prefix data.
aPiecek2f63f952021-03-30 12:22:18 +0200198 * @param[in] ctx2 Libyang context for resolving prefixes in @p value2.
199 * Can be set to NULL if @p ctx1 is equal to @p ctx2.
Michal Vasko52927e22020-03-16 17:26:14 +0100200 * @param[in] value2 Second value.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100201 * @param[in] val_prefix_data2 Second value prefix data.
Michal Vasko52927e22020-03-16 17:26:14 +0100202 * @return LY_SUCCESS if values are equal.
203 * @return LY_ENOT if values are not equal.
204 * @return LY_ERR on error.
205 */
aPiecek2f63f952021-03-30 12:22:18 +0200206LY_ERR lyxml_value_compare(const struct ly_ctx *ctx1, const char *value1, void *val_prefix_data1,
207 const struct ly_ctx *ctx2, const char *value2, void *val_prefix_data2);
Michal Vasko52927e22020-03-16 17:26:14 +0100208
Radek Krejcib416be62018-10-01 14:51:45 +0200209#endif /* LY_XML_H_ */