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 | |
| 18 | #include <stdint.h> |
| 19 | |
| 20 | #include "context.h" |
| 21 | #include "set.h" |
| 22 | |
| 23 | struct lyxml_ns { |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 24 | const char *element; /* element where the namespace is defined */ |
| 25 | char *prefix; /* prefix of the namespace, NULL for the default namespace */ |
| 26 | char *uri; /* namespace URI */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 29 | /* element tag identifier for matching opening and closing tags */ |
| 30 | struct lyxml_elem { |
| 31 | const char *prefix; |
| 32 | const char *name; |
| 33 | size_t prefix_len; |
| 34 | size_t name_len; |
| 35 | }; |
| 36 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 37 | enum LYXML_PARSER_STATUS { |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 38 | LYXML_ELEMENT, /* expecting XML element, call lyxml_get_element() */ |
| 39 | LYXML_ELEM_CONTENT, /* expecting content of an element, call lyxml_get_string */ |
| 40 | LYXML_ATTRIBUTE, /* expecting XML attribute, call lyxml_get_attribute() */ |
| 41 | LYXML_ATTR_CONTENT, /* expecting value of an attribute, call lyxml_get_string */ |
| 42 | LYXML_END /* end of input data */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct lyxml_context { |
| 46 | struct ly_ctx *ctx; |
| 47 | uint64_t line; |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 48 | enum LYXML_PARSER_STATUS status; /* status providing information about the next expected object in input data */ |
| 49 | struct ly_set elements; /* list of not-yet-closed elements */ |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 50 | struct ly_set ns; /* handled with LY_SET_OPT_USEASLIST */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 53 | /** |
| 54 | * @brief Parse input expecting an XML element. |
| 55 | * |
| 56 | * Able to silently skip comments, PIs and CData. DOCTYPE is not parsable, so it is reported as LY_EVALID error. |
| 57 | * If '<' is not found in input, LY_EINVAL is returned (but no error is logged), so it is possible to continue |
| 58 | * with parsing input as text content. |
| 59 | * |
| 60 | * Input string is not being modified, so the returned values are not NULL-terminated, instead their length |
| 61 | * is returned. |
| 62 | * |
| 63 | * @param[in] context XML context to track lines or store errors into libyang context. |
| 64 | * @param[in,out] input Input string to process, updated according to the processed/read data. |
| 65 | * @param[in] options Currently unused options to modify input processing. |
| 66 | * @param[out] prefix Pointer to prefix if present in the element name, NULL otherwise. |
| 67 | * @param[out] prefix_len Length of the prefix if any. |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 68 | * @param[out] name Element name. When LY_SUCCESS is returned but name is NULL, check context's status field: |
| 69 | * - LYXML_END - end of input was reached |
| 70 | * - LYXML_ELEMENT - closing element found, expecting sibling element so call lyxml_get_element() again |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 71 | * @param[out] name_len Length of the element name. |
| 72 | * @return LY_ERR values. |
| 73 | */ |
| 74 | LY_ERR lyxml_get_element(struct lyxml_context *context, const char **input, |
| 75 | const char **prefix, size_t *prefix_len, const char **name, size_t *name_len); |
| 76 | |
| 77 | /** |
| 78 | * @brief Parse input expecting an XML attribute (including XML namespace). |
| 79 | * |
| 80 | * Input string is not being modified, so the returned values are not NULL-terminated, instead their length |
| 81 | * is returned. |
| 82 | * |
| 83 | * In case of a namespace definition, prefix just contains xmlns string. In case of the default namespace, |
| 84 | * prefix is NULL and the attribute name is xmlns. |
| 85 | * |
| 86 | * @param[in] context XML context to track lines or store errors into libyang context. |
| 87 | * @param[in,out] input Input string to process, updated according to the processed/read data so, |
| 88 | * when succeeded, it points to the opening quote of the attribute's value. |
| 89 | * @param[out] prefix Pointer to prefix if present in the attribute name, NULL otherwise. |
| 90 | * @param[out] prefix_len Length of the prefix if any. |
| 91 | * @param[out] name Attribute name. LY_SUCCESS can be returned with NULL name only in case the |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 92 | * end of the element tag was reached. According to the context's status field, the opening tag was read |
| 93 | * (LYXML_CONTENT) or empty element was closed (LYXML_ELEMENT). |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 94 | * @param[out] name_len Length of the element name. |
| 95 | * @return LY_ERR values. |
| 96 | */ |
| 97 | LY_ERR lyxml_get_attribute(struct lyxml_context *context, const char **input, |
| 98 | const char **prefix, size_t *prefix_len, const char **name, size_t *name_len); |
| 99 | |
| 100 | /** |
| 101 | * @brief Parse input as XML text (attribute's values and element's content). |
| 102 | * |
| 103 | * Mixed content of XML elements is not allowed. Formating whitespaces before child element are ignored, |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 104 | * LY_EINVAL is returned in such a case (output is not set, no error is printed) and input is moved |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 105 | * to the beginning of a child definition. |
| 106 | * |
| 107 | * In the case of attribute's values, the input string is expected to start on a quotation mark to |
| 108 | * select which delimiter (single or double quote) is used. Otherwise, the element content is being |
| 109 | * parsed expected to be terminated by '<' character. |
| 110 | * |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 111 | * If function succeeds, the string in a dynamically allocated output buffer is always NULL-terminated. |
| 112 | * |
| 113 | * The dynamically allocated buffer is used only when necessary because of a character or the supported entity |
| 114 | * reference which modify the input data. These constructs are replaced by their real value, so in case the output |
| 115 | * string will be again printed as an XML data, it may be necessary to correctly encode such characters. |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 116 | * |
| 117 | * @param[in] context XML context to track lines or store errors into libyang context. |
| 118 | * @param[in,out] input Input string to process, updated according to the processed/read data. |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 119 | * @param[in, out] buffer Storage for the output string. If the parameter points to NULL, the buffer is allocated if needed. |
| 120 | * Otherwise, when needed, the buffer is used and enlarged when necessary. Whenever the buffer is used, the string is NULL-terminated. |
| 121 | * @param[in, out] buffer_size Allocated size of the returned buffer. If a buffer is provided by a caller, it |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 122 | * is not being reduced even if the string is shorter. On the other hand, it can be enlarged if needed. |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 123 | * @param[out] output Returns pointer to the resulting string - to the provided/allocated buffer if it was necessary to modify |
| 124 | * the input string or directly into the input string (see the \p dynamic parameter). |
| 125 | * @param[out] length Length of the \p output string. |
| 126 | * @param[out] dynamic Flag if a dynamically allocated memory (\p buffer) was used and caller is supposed to free it at the end. |
| 127 | * In case the value is zero, the \p output points directly into the \p input string. |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 128 | * @return LY_ERR value. |
| 129 | */ |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 130 | LY_ERR lyxml_get_string(struct lyxml_context *context, const char **input, char **buffer, size_t *buffer_size, char **output, size_t *length, int *dynamic); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * @brief Add namespace definition into XML context. |
| 134 | * |
| 135 | * Namespaces from a single element are supposed to be added sequentially together (not interleaved by a namespace from other |
| 136 | * element). This mimic namespace visibility, since the namespace defined in element E is not visible from its parents or |
| 137 | * siblings. On the other hand, namespace from a parent element can be redefined in a child element. This is also reflected |
| 138 | * by lyxml_ns_get() which returns the most recent namespace definition for the given prefix. |
| 139 | * |
| 140 | * When leaving processing of a subtree of some element, caller is supposed to call lyxml_ns_rm() to remove all the namespaces |
| 141 | * defined in such an element from the context. |
| 142 | * |
| 143 | * @param[in] context XML context to work with. |
| 144 | * @param[in] element_name Pointer to the element name where the namespace is defined. Serve as an identifier to select |
| 145 | * which namespaces are supposed to be removed via lyxml_ns_rm() when leaving the element's subtree. |
| 146 | * @param[in] prefix Pointer to the namespace prefix as taken from lyxml_get_attribute(). Can be NULL for default namespace. |
| 147 | * @param[in] prefix_len Length of the prefix string (since it is not NULL-terminated when returned from lyxml_get_attribute()). |
| 148 | * @param[in] uri Namespace URI (value) to store. Value can be obtained via lyxml_get_string() and caller is not supposed to |
| 149 | * work with the pointer when the function succeeds. |
| 150 | * @return LY_ERR values. |
| 151 | */ |
| 152 | LY_ERR lyxml_ns_add(struct lyxml_context *context, const char *element_name, const char *prefix, size_t prefix_len, char *uri); |
| 153 | |
| 154 | /** |
| 155 | * @brief Get a namespace record for the given prefix in the current context. |
| 156 | * |
| 157 | * @param[in] context XML context to work with. |
| 158 | * @param[in] prefix Pointer to the namespace prefix as taken from lyxml_get_attribute() or lyxml_get_element(). |
| 159 | * Can be NULL for default namespace. |
| 160 | * @param[in] prefix_len Length of the prefix string (since it is not NULL-terminated when returned from lyxml_get_attribute() or |
| 161 | * lyxml_get_element()). |
| 162 | * @return The namespace record or NULL if the record for the specified prefix not found. |
| 163 | */ |
| 164 | const struct lyxml_ns *lyxml_ns_get(struct lyxml_context *context, const char *prefix, size_t prefix_len); |
| 165 | |
| 166 | /** |
| 167 | * @brief Remove all the namespaces defined in the given element. |
| 168 | * |
| 169 | * @param[in] context XML context to work with. |
| 170 | * @param[in] element_name Pointer to the element name where the namespaces are defined. Serve as an identifier previously provided |
| 171 | * by lyxml_get_element() |
| 172 | * @return LY_ERR values. |
| 173 | */ |
| 174 | LY_ERR lyxml_ns_rm(struct lyxml_context *context, const char *element_name); |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 175 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 176 | /** |
| 177 | * @brief Remove the allocated working memory of the context. |
| 178 | * |
| 179 | * @param[in] context XML context to clear. |
| 180 | */ |
| 181 | void lyxml_context_clear(struct lyxml_context *context); |
| 182 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 183 | #endif /* LY_XML_H_ */ |