Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file xml.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Generic XML parser routines. |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #ifndef LY_XML_H_ |
| 16 | #define LY_XML_H_ |
| 17 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | #include <stddef.h> |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include "log.h" |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 22 | #include "set.h" |
| 23 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | struct ly_ctx; |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 25 | struct ly_in; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 26 | struct ly_out; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 27 | struct ly_prefix; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 29 | /* 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 Krejci | c8a6390 | 2021-02-04 14:48:19 +0100 | [diff] [blame] | 48 | (c >= 0x2070 && c <= 0x218f) || (c >= 0x203f && c <= 0x2040) || \ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 49 | (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \ |
| 50 | (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \ |
| 51 | (c >= 0x10000 && c <= 0xeffff)) |
| 52 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 53 | struct lyxml_ns { |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 54 | char *prefix; /* prefix of the namespace, NULL for the default namespace */ |
| 55 | char *uri; /* namespace URI */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 56 | uint32_t depth; /* depth level of the element to maintain the list of accessible namespace definitions */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 57 | }; |
| 58 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 59 | /* element tag identifier for matching opening and closing tags */ |
| 60 | struct lyxml_elem { |
| 61 | const char *prefix; |
| 62 | const char *name; |
| 63 | size_t prefix_len; |
| 64 | size_t name_len; |
| 65 | }; |
| 66 | |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 67 | /** |
| 68 | * @brief Status of the parser providing information what is expected next (which function is supposed to be called). |
| 69 | */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 70 | enum LYXML_PARSER_STATUS { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 71 | 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 Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 76 | LYXML_END /* end of input data */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 79 | struct lyxml_ctx { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 80 | const struct ly_ctx *ctx; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 81 | struct ly_in *in; /* input structure */ |
| 82 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 83 | enum LYXML_PARSER_STATUS status; /* status providing information about the last parsed object, following attributes |
| 84 | are filled based on it */ |
| 85 | union { |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 86 | const char *prefix; /* LYXML_ELEMENT, LYXML_ATTRIBUTE - elem/attr prefix */ |
| 87 | const char *value; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT - elem/attr value */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 88 | }; |
| 89 | union { |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 90 | size_t prefix_len; /* LYXML_ELEMENT, LYXML_ATTRIBUTE - elem/attr prefix length */ |
| 91 | size_t value_len; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT - elem/attr value length */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 92 | }; |
| 93 | union { |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 94 | const char *name; /* LYXML_ELEMENT, LYXML_ATTRIBUTE - elem/attr name */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 95 | ly_bool ws_only; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT - whether elem/attr value is empty/white-space only */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 96 | }; |
| 97 | union { |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 98 | size_t name_len; /* LYXML_ELEMENT, LYXML_ATTRIBUTE - elem/attr name length */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 99 | ly_bool dynamic; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT - whether elem/attr value is dynamically allocated */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 100 | }; |
| 101 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 102 | struct ly_set elements; /* list of not-yet-closed elements */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 103 | struct ly_set ns; /* handled with LY_SET_OPT_USEASLIST */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 104 | }; |
| 105 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 106 | /** |
| 107 | * @brief Create a new XML parser context and start parsing. |
| 108 | * |
| 109 | * @param[in] ctx libyang context. |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 110 | * @param[in] in Input structure. |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 111 | * @param[out] xmlctx New XML context with status ::LYXML_ELEMENT. |
| 112 | * @return LY_ERR value. |
| 113 | */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 114 | LY_ERR lyxml_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyxml_ctx **xmlctx); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 115 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 116 | /** |
| 117 | * @brief Move to the next XML artefact and update parser status. |
| 118 | * |
| 119 | * LYXML_ELEMENT (-> LYXML_ATTRIBUTE -> LYXML_ATTR_CONTENT)* -> LYXML_ELEM_CONTENT -> LYXML_ELEM_CLOSE ... |
| 120 | * -> LYXML_ELEMENT ... |
| 121 | * |
| 122 | * @param[in] xmlctx XML context to move. |
| 123 | * @return LY_ERR value. |
| 124 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 125 | LY_ERR lyxml_ctx_next(struct lyxml_ctx *xmlctx); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 126 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 127 | /** |
| 128 | * @brief Peek at the next XML parser status without changing it. |
| 129 | * |
| 130 | * @param[in] xmlctx XML context to use. |
| 131 | * @param[out] next Next XML parser status. |
| 132 | * @return LY_ERR value. |
| 133 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 134 | LY_ERR lyxml_ctx_peek(struct lyxml_ctx *xmlctx, enum LYXML_PARSER_STATUS *next); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 135 | |
| 136 | /** |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 137 | * @brief Get a namespace record for the given prefix in the current context. |
| 138 | * |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 139 | * @param[in] ns_set Set with namespaces from the XML context. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 140 | * @param[in] prefix Pointer to the namespace prefix as taken from ::lyxml_get_attribute() or ::lyxml_get_element(). |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 141 | * Can be NULL for default namespace. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 142 | * @param[in] prefix_len Length of the prefix string (since it is not NULL-terminated when returned from ::lyxml_get_attribute() or |
| 143 | * ::lyxml_get_element()). |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 144 | * @return The namespace record or NULL if the record for the specified prefix not found. |
| 145 | */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 146 | const struct lyxml_ns *lyxml_ns_get(const struct ly_set *ns_set, const char *prefix, size_t prefix_len); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 147 | |
| 148 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 149 | * @brief Print the given @p text as XML string which replaces some of the characters which cannot appear in XML data. |
| 150 | * |
| 151 | * @param[in] out Output structure for printing. |
| 152 | * @param[in] text String to print. |
| 153 | * @param[in] attribute Flag for attribute's value where a double quotes must be replaced. |
| 154 | * @return LY_ERR values. |
| 155 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 156 | LY_ERR lyxml_dump_text(struct ly_out *out, const char *text, ly_bool attribute); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 157 | |
| 158 | /** |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 159 | * @brief Remove the allocated working memory of the context. |
| 160 | * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 161 | * @param[in] xmlctx XML context to clear. |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 162 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 163 | void lyxml_ctx_free(struct lyxml_ctx *xmlctx); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 164 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 165 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 166 | * @brief Compare values and their prefix mappings. |
| 167 | * |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 168 | * @param[in] ctx libyang context. |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 169 | * @param[in] value1 First value. |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 170 | * @param[in] val_prefix_data1 First value prefix data. |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 171 | * @param[in] value2 Second value. |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 172 | * @param[in] val_prefix_data2 Second value prefix data. |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 173 | * @return LY_SUCCESS if values are equal. |
| 174 | * @return LY_ENOT if values are not equal. |
| 175 | * @return LY_ERR on error. |
| 176 | */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 177 | LY_ERR lyxml_value_compare(const struct ly_ctx *ctx, const char *value1, void *val_prefix_data1, const char *value2, |
| 178 | void *val_prefix_data2); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 179 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 180 | #endif /* LY_XML_H_ */ |