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" |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 23 | #include "tree_schema.h" |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 24 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 25 | struct ly_out; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 26 | struct ly_prefix; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 28 | /* Macro to test if character is whitespace */ |
| 29 | #define is_xmlws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd) |
| 30 | |
| 31 | /* Macro to test if character is allowed to be a first character of an qualified identifier */ |
| 32 | #define is_xmlqnamestartchar(c) ((c >= 'a' && c <= 'z') || c == '_' || \ |
| 33 | (c >= 'A' && c <= 'Z') || /* c == ':' || */ \ |
| 34 | (c >= 0x370 && c <= 0x1fff && c != 0x37e ) || \ |
| 35 | (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \ |
| 36 | c == 0x200d || (c >= 0x2070 && c <= 0x218f) || \ |
| 37 | (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \ |
| 38 | (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \ |
| 39 | (c >= 0x10000 && c <= 0xeffff)) |
| 40 | |
| 41 | /* Macro to test if character is allowed to be used in an qualified identifier */ |
| 42 | #define is_xmlqnamechar(c) ((c >= 'a' && c <= 'z') || c == '_' || c == '-' || \ |
| 43 | (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || /* c == ':' || */ \ |
| 44 | c == '.' || c == 0xb7 || (c >= 0x370 && c <= 0x1fff && c != 0x37e ) ||\ |
| 45 | (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \ |
| 46 | c == 0x200d || (c >= 0x300 && c <= 0x36f) || \ |
| 47 | (c >= 0x2070 && c <= 0x218f) || (c >= 0x2030f && c <= 0x2040) || \ |
| 48 | (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \ |
| 49 | (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \ |
| 50 | (c >= 0x10000 && c <= 0xeffff)) |
| 51 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 52 | struct lyxml_ns { |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 53 | char *prefix; /* prefix of the namespace, NULL for the default namespace */ |
| 54 | char *uri; /* namespace URI */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 55 | 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] | 56 | }; |
| 57 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 58 | /* element tag identifier for matching opening and closing tags */ |
| 59 | struct lyxml_elem { |
| 60 | const char *prefix; |
| 61 | const char *name; |
| 62 | size_t prefix_len; |
| 63 | size_t name_len; |
| 64 | }; |
| 65 | |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 66 | /** |
| 67 | * @brief Status of the parser providing information what is expected next (which function is supposed to be called). |
| 68 | */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 69 | enum LYXML_PARSER_STATUS { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 70 | LYXML_ELEMENT, /* opening XML element parsed */ |
| 71 | LYXML_ELEM_CLOSE, /* closing XML element parsed */ |
| 72 | LYXML_ELEM_CONTENT, /* XML element context parsed */ |
| 73 | LYXML_ATTRIBUTE, /* XML attribute parsed */ |
| 74 | LYXML_ATTR_CONTENT, /* XML attribute content parsed */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 75 | LYXML_END /* end of input data */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 76 | }; |
| 77 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 78 | struct lyxml_ctx { |
| 79 | enum LYXML_PARSER_STATUS status; /* status providing information about the last parsed object, following attributes |
| 80 | are filled based on it */ |
| 81 | union { |
| 82 | const char *prefix; /* LYXML_ELEMENT, LYXML_ATTRIBUTE */ |
| 83 | const char *value; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT */ |
| 84 | }; |
| 85 | union { |
| 86 | size_t prefix_len; /* LYXML_ELEMENT, LYXML_ATTRIBUTE */ |
| 87 | size_t value_len; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT */ |
| 88 | }; |
| 89 | union { |
| 90 | const char *name; /* LYXML_ELEMENT, LYXML_ATTRIBUTE */ |
| 91 | int ws_only; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT */ |
| 92 | }; |
| 93 | union { |
| 94 | size_t name_len; /* LYXML_ELEMENT, LYXML_ATTRIBUTE */ |
| 95 | int dynamic; /* LYXML_ELEM_CONTENT, LYXML_ATTR_CONTENT */ |
| 96 | }; |
| 97 | |
| 98 | const struct ly_ctx *ctx; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 99 | uint64_t line; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 100 | const char *input; |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 101 | struct ly_set elements; /* list of not-yet-closed elements */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 102 | struct ly_set ns; /* handled with LY_SET_OPT_USEASLIST */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 105 | LY_ERR lyxml_ctx_new(const struct ly_ctx *ctx, const char *input, struct lyxml_ctx **xmlctx); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 106 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 107 | LY_ERR lyxml_ctx_next(struct lyxml_ctx *xmlctx); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 108 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 109 | 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] | 110 | |
| 111 | /** |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 112 | * @brief Get a namespace record for the given prefix in the current context. |
| 113 | * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 114 | * @param[in] xmlctx XML context to work with. |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 115 | * @param[in] prefix Pointer to the namespace prefix as taken from lyxml_get_attribute() or lyxml_get_element(). |
| 116 | * Can be NULL for default namespace. |
| 117 | * @param[in] prefix_len Length of the prefix string (since it is not NULL-terminated when returned from lyxml_get_attribute() or |
| 118 | * lyxml_get_element()). |
| 119 | * @return The namespace record or NULL if the record for the specified prefix not found. |
| 120 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 121 | const struct lyxml_ns *lyxml_ns_get(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 122 | |
| 123 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 124 | * @brief Print the given @p text as XML string which replaces some of the characters which cannot appear in XML data. |
| 125 | * |
| 126 | * @param[in] out Output structure for printing. |
| 127 | * @param[in] text String to print. |
| 128 | * @param[in] attribute Flag for attribute's value where a double quotes must be replaced. |
| 129 | * @return LY_ERR values. |
| 130 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 131 | LY_ERR lyxml_dump_text(struct ly_out *out, const char *text, int attribute); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 132 | |
| 133 | /** |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 134 | * @brief Remove the allocated working memory of the context. |
| 135 | * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 136 | * @param[in] xmlctx XML context to clear. |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 137 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 138 | void lyxml_ctx_free(struct lyxml_ctx *xmlctx); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 139 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 140 | /** |
| 141 | * @brief Find all possible prefixes in a value. |
| 142 | * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 143 | * @param[in] xmlctx XML context to use. |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 144 | * @param[in] value Value to check. |
| 145 | * @param[in] value_len Value length. |
| 146 | * @param[out] val_prefs Array of found prefixes. |
| 147 | * @return LY_ERR value. |
| 148 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 149 | LY_ERR lyxml_get_prefixes(struct lyxml_ctx *xmlctx, const char *value, size_t value_len, struct ly_prefix **val_prefs); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * @brief Compare values and their prefix mappings. |
| 153 | * |
| 154 | * @param[in] value1 First value. |
| 155 | * @param[in] prefs1 First value prefixes. |
| 156 | * @param[in] value2 Second value. |
| 157 | * @param[in] prefs2 Second value prefixes. |
| 158 | * @return LY_SUCCESS if values are equal. |
| 159 | * @return LY_ENOT if values are not equal. |
| 160 | * @return LY_ERR on error. |
| 161 | */ |
| 162 | LY_ERR lyxml_value_compare(const char *value1, const struct ly_prefix *prefs1, const char *value2, |
| 163 | const struct ly_prefix *prefs2); |
| 164 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 165 | void *lyxml_elem_dup(void *item); |
| 166 | |
| 167 | void *lyxml_ns_dup(void *item); |
| 168 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 169 | #endif /* LY_XML_H_ */ |