Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_xml.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief XML data parser for libyang |
| 5 | * |
| 6 | * Copyright (c) 2019 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 | #include "common.h" |
| 16 | |
| 17 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include "context.h" |
| 22 | #include "dict.h" |
| 23 | #include "log.h" |
| 24 | #include "plugins_types.h" |
| 25 | #include "set.h" |
| 26 | #include "tree_data.h" |
| 27 | #include "tree_data_internal.h" |
| 28 | #include "tree_schema.h" |
| 29 | #include "xml.h" |
| 30 | |
| 31 | /** |
| 32 | * @brief internal context for XML YANG data parser. |
| 33 | * |
| 34 | * The leading part is compatible with the struct lyxml_context |
| 35 | */ |
| 36 | struct lyd_xml_ctx { |
| 37 | struct ly_ctx *ctx; /**< libyang context */ |
| 38 | uint64_t line; /**< number of the line being currently processed */ |
| 39 | enum LYXML_PARSER_STATUS status; /**< status providing information about the next expected object in input data */ |
| 40 | struct ly_set elements; /**< list of not-yet-closed elements */ |
| 41 | struct ly_set ns; /**< handled with LY_SET_OPT_USEASLIST */ |
| 42 | |
| 43 | uint16_t options; /**< various @ref dataparseroptions. */ |
| 44 | uint16_t path_len; /**< used bytes in the path buffer */ |
| 45 | #define LYD_PARSER_BUFSIZE 4078 |
| 46 | char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */ |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 47 | struct ly_set incomplete_type_validation; /**< set of nodes validated with LY_EINCOMPLETE result */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | /** |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 51 | * @brief XML-parser's implementation of ly_type_resolve_prefix() callback to provide mapping between prefixes used in the values to the schema |
| 52 | * via XML namespaces. |
| 53 | */ |
| 54 | static const struct lys_module * |
| 55 | lydxml_resolve_prefix(struct ly_ctx *ctx, const char *prefix, size_t prefix_len, void *parser) |
| 56 | { |
| 57 | const struct lyxml_ns *ns; |
| 58 | struct lyxml_context *xmlctx = (struct lyxml_context*)parser; |
| 59 | |
| 60 | ns = lyxml_ns_get(xmlctx, prefix, prefix_len); |
| 61 | if (!ns) { |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | return ly_ctx_get_module_implemented_ns(ctx, ns->uri); |
| 66 | } |
| 67 | |
| 68 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 69 | * @brief Parse XML attributes of the XML element of YANG data. |
| 70 | * |
| 71 | * @param[in] ctx XML YANG data parser context. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 72 | * @param[in,out] data Pointer to the XML string representation of the YANG data to parse. |
| 73 | * @param[out] attributes Resulting list of the parsed attributes. XML namespace definitions are not parsed |
| 74 | * as attributes, they are stored internally in the parser context. |
| 75 | * @reutn LY_ERR value. |
| 76 | */ |
| 77 | static LY_ERR |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 78 | lydxml_attributes(struct lyd_xml_ctx *ctx, const char **data, struct lyd_attr **attributes) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 79 | { |
| 80 | LY_ERR ret = LY_SUCCESS; |
| 81 | unsigned int u; |
| 82 | const char *prefix, *name; |
| 83 | size_t prefix_len, name_len; |
| 84 | struct lyd_attr *attr = NULL, *last = NULL; |
| 85 | const struct lyxml_ns *ns; |
| 86 | struct ly_set attr_prefixes = {0}; |
| 87 | struct attr_prefix_s { |
| 88 | const char *prefix; |
| 89 | size_t prefix_len; |
| 90 | } *attr_prefix; |
| 91 | struct lys_module *mod; |
| 92 | |
| 93 | while(ctx->status == LYXML_ATTRIBUTE && |
| 94 | lyxml_get_attribute((struct lyxml_context*)ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS) { |
| 95 | int dynamic = 0; |
| 96 | char *buffer = NULL, *value; |
| 97 | size_t buffer_size = 0, value_len; |
| 98 | |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 99 | if (!name) { |
| 100 | /* seems like all the attrributes were internally processed as namespace definitions */ |
| 101 | continue; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 102 | } |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 103 | |
| 104 | /* get attribute value */ |
| 105 | ret = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic); |
| 106 | LY_CHECK_GOTO(ret, cleanup); |
| 107 | |
| 108 | attr = calloc(1, sizeof *attr); |
| 109 | LY_CHECK_ERR_GOTO(!attr, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 110 | |
| 111 | attr->name = lydict_insert(ctx->ctx, name, name_len); |
| 112 | /* auxiliary store the prefix information and wait with resolving prefix to the time when all the namespaces, |
| 113 | * defined in this element, are parsed, so we will get the correct namespace for this prefix */ |
| 114 | attr_prefix = malloc(sizeof *attr_prefix); |
| 115 | attr_prefix->prefix = prefix; |
| 116 | attr_prefix->prefix_len = prefix_len; |
| 117 | ly_set_add(&attr_prefixes, attr_prefix, LY_SET_OPT_USEASLIST); |
| 118 | |
| 119 | /* TODO process value */ |
| 120 | |
| 121 | if (last) { |
| 122 | last->next = attr; |
| 123 | } else { |
| 124 | (*attributes) = attr; |
| 125 | } |
| 126 | last = attr; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* resolve annotation pointers in all the attributes */ |
| 130 | for (last = *attributes, u = 0; u < attr_prefixes.count && last; u++, last = last->next) { |
| 131 | attr_prefix = (struct attr_prefix_s*)attr_prefixes.objs[u]; |
| 132 | ns = lyxml_ns_get((struct lyxml_context *)ctx, attr_prefix->prefix, attr_prefix->prefix_len); |
| 133 | mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri); |
| 134 | |
| 135 | /* TODO get annotation */ |
| 136 | } |
| 137 | |
| 138 | cleanup: |
| 139 | |
| 140 | ly_set_erase(&attr_prefixes, free); |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @brief Parse XML elements as children YANG data node of the specified parent node. |
| 146 | * |
| 147 | * @param[in] ctx XML YANG data parser context. |
| 148 | * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements. |
| 149 | * @param[in,out] data Pointer to the XML string representation of the YANG data to parse. |
| 150 | * @param[out] node Resulting list of the parsed nodes. |
| 151 | * @reutn LY_ERR value. |
| 152 | */ |
| 153 | static LY_ERR |
| 154 | lydxml_nodes(struct lyd_xml_ctx *ctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **node) |
| 155 | { |
| 156 | LY_ERR ret = LY_SUCCESS; |
| 157 | const char *prefix, *name; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 158 | size_t prefix_len, name_len; |
| 159 | struct lyd_attr *attributes = NULL; |
| 160 | const struct lyxml_ns *ns; |
| 161 | const struct lysc_node *snode; |
| 162 | struct lys_module *mod; |
| 163 | unsigned int parents_count = ctx->elements.count; |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 164 | struct lyd_node *cur = NULL, *prev = NULL, *last = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 165 | |
| 166 | (*node) = NULL; |
| 167 | |
| 168 | while(ctx->status == LYXML_ELEMENT) { |
| 169 | ret = lyxml_get_element((struct lyxml_context *)ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 170 | LY_CHECK_GOTO(ret, cleanup); |
| 171 | if (!name) { |
| 172 | /* closing previous element */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 173 | if (ctx->elements.count < parents_count) { |
| 174 | /* all siblings parsed */ |
| 175 | break; |
| 176 | } else { |
| 177 | continue; |
| 178 | } |
| 179 | } |
| 180 | attributes = NULL; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 181 | if (ctx->status == LYXML_ATTRIBUTE) { |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 182 | LY_CHECK_GOTO(lydxml_attributes(ctx, data, &attributes), error); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 185 | ns = lyxml_ns_get((struct lyxml_context *)ctx, prefix, prefix_len); |
| 186 | if (!ns) { |
| 187 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%*.s\".", prefix_len, prefix); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 188 | goto error; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 189 | } |
| 190 | mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri); |
| 191 | if (!mod) { |
| 192 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 193 | goto error; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 194 | } |
| 195 | snode = lys_child(parent ? parent->schema : NULL, mod, name, name_len, 0, (ctx->options & LYD_OPT_RPCREPLY) ? LYS_GETNEXT_OUTPUT : 0); |
| 196 | if (!snode) { |
| 197 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.", name_len, name, mod->name); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 198 | goto error; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* allocate new node */ |
| 202 | switch (snode->nodetype) { |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 203 | case LYS_ACTION: |
| 204 | if ((ctx->options & LYD_OPT_TYPEMASK) != LYD_OPT_RPC) { |
| 205 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_RESTRICTION, "Unexpected RPC/action element \"%.*s\" in %s data set.", |
| 206 | name_len, name, lyd_parse_options_type2str(ctx->options & LYD_OPT_TYPEMASK)); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 207 | goto error; |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 208 | } |
| 209 | cur = calloc(1, sizeof(struct lyd_node_inner)); |
| 210 | break; |
| 211 | case LYS_NOTIF: |
| 212 | if ((ctx->options & LYD_OPT_TYPEMASK) != LYD_OPT_RPC) { |
| 213 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_RESTRICTION, "Unexpected Notification element \"%.*s\" in %s data set.", |
| 214 | name_len, name, lyd_parse_options_type2str(ctx->options)); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 215 | goto error; |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 216 | } |
| 217 | cur = calloc(1, sizeof(struct lyd_node_inner)); |
| 218 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 219 | case LYS_CONTAINER: |
| 220 | case LYS_LIST: |
| 221 | cur = calloc(1, sizeof(struct lyd_node_inner)); |
| 222 | break; |
| 223 | case LYS_LEAF: |
| 224 | case LYS_LEAFLIST: |
| 225 | cur = calloc(1, sizeof(struct lyd_node_term)); |
| 226 | break; |
| 227 | case LYS_ANYDATA: |
| 228 | case LYS_ANYXML: |
| 229 | cur = calloc(1, sizeof(struct lyd_node_any)); |
| 230 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 231 | default: |
| 232 | LOGINT(ctx->ctx); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 233 | ret = LY_EINT; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 234 | goto cleanup; |
| 235 | } |
| 236 | if (!(*node)) { |
| 237 | (*node) = cur; |
| 238 | } |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 239 | last = cur; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 240 | cur->schema = snode; |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 241 | cur->prev = cur; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 242 | cur->parent = parent; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 243 | if (parent) { |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 244 | if (prev && cur->schema->nodetype == LYS_LEAF && (cur->schema->flags & LYS_KEY)) { |
| 245 | /* it is key and we need to insert it into a correct place */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 246 | struct lysc_node *key_s; |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 247 | unsigned int cur_index, key_index; |
| 248 | struct lyd_node *key; |
| 249 | |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 250 | for (cur_index = 0, key_s = ((struct lysc_node_list*)parent->schema)->child; |
| 251 | key_s && key_s != cur->schema; |
| 252 | ++cur_index, key_s = key_s->next); |
| 253 | for (key = prev; |
| 254 | !(key->schema->flags & LYS_KEY) && key->prev != prev; |
| 255 | key = key->prev); |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 256 | for (; key->schema->flags & LYS_KEY; key = key->prev) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 257 | for (key_index = 0, key_s = ((struct lysc_node_list*)parent->schema)->child; |
| 258 | key_s && key_s != key->schema; |
| 259 | ++key_index, key_s = key_s->next); |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 260 | if (key_index < cur_index) { |
| 261 | /* cur key is supposed to be placed after the key */ |
| 262 | cur->next = key->next; |
| 263 | cur->prev = key; |
| 264 | key->next = cur; |
| 265 | if (cur->next) { |
| 266 | cur->next->prev = cur; |
| 267 | } else { |
| 268 | parent->child->prev = cur; |
| 269 | } |
| 270 | break; |
| 271 | } |
| 272 | if (key->prev == prev) { |
| 273 | /* current key is supposed to be the first child from the current children */ |
| 274 | key = NULL; |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | if (!key || !(key->schema->flags & LYS_KEY)) { |
| 279 | /* current key is supposed to be the first child from the current children */ |
| 280 | cur->next = parent->child; |
| 281 | cur->prev = parent->child->prev; |
| 282 | parent->child->prev = cur; |
| 283 | parent->child = cur; |
| 284 | } |
| 285 | if (cur->next) { |
| 286 | last = prev; |
| 287 | if (ctx->options & LYD_OPT_STRICT) { |
| 288 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_RESTRICTION, "Invalid position of the key \"%.*s\" in a list.", |
| 289 | name_len, name); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 290 | goto error; |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 291 | } else { |
| 292 | LOGWRN(ctx->ctx, "Invalid position of the key \"%.*s\" in a list.", name_len, name); |
| 293 | } |
| 294 | } |
| 295 | } else { |
| 296 | /* last child of the parent */ |
| 297 | if (prev) { |
| 298 | parent->child->prev = cur; |
| 299 | prev->next = cur; |
| 300 | cur->prev = prev; |
| 301 | } |
| 302 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 303 | } else { |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 304 | /* top level */ |
| 305 | if (prev) { |
| 306 | /* last top level node */ |
| 307 | struct lyd_node *iter; |
| 308 | for (iter = prev; iter->prev->next; iter = iter->prev); |
| 309 | iter->prev = cur; |
| 310 | prev->next = cur; |
| 311 | cur->prev = prev; |
| 312 | } /* first top level node - nothing more to do */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 313 | } |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 314 | prev = last; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 315 | cur->attr = attributes; |
| 316 | attributes = NULL; |
| 317 | |
| 318 | if (snode->nodetype & LYD_NODE_TERM) { |
| 319 | int dynamic = 0; |
| 320 | char *buffer = NULL, *value; |
| 321 | size_t buffer_size = 0, value_len; |
| 322 | |
| 323 | if (ctx->status == LYXML_ELEM_CONTENT) { |
| 324 | /* get the value */ |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 325 | ret = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic); |
| 326 | if (ret == LY_EINVAL) { |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 327 | /* just indentation of a child element found */ |
| 328 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX, "Child element inside terminal node \"%s\" found.", cur->schema->name); |
| 329 | goto cleanup; |
| 330 | } |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 331 | ret = LY_SUCCESS; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 332 | } else { |
| 333 | /* no content - validate empty value */ |
| 334 | value = ""; |
| 335 | value_len = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 336 | } |
Radek Krejci | 3c9758d | 2019-07-11 16:49:10 +0200 | [diff] [blame] | 337 | ret = lyd_value_parse((struct lyd_node_term*)cur, value, value_len, dynamic, 0, lydxml_resolve_prefix, ctx, LYD_XML, NULL); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 338 | if (ret == LY_EINCOMPLETE) { |
| 339 | ly_set_add(&ctx->incomplete_type_validation, cur, LY_SET_OPT_USEASLIST); |
| 340 | } else if (ret) { |
| 341 | if (dynamic){ |
| 342 | free(value); |
| 343 | } |
| 344 | goto cleanup; |
| 345 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 346 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 347 | int dynamic = 0; |
| 348 | char *buffer = NULL, *value; |
| 349 | size_t buffer_size = 0, value_len; |
| 350 | |
| 351 | if (ctx->status == LYXML_ELEM_CONTENT) { |
| 352 | LY_ERR r = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 353 | if (r != LY_EINVAL && (r != LY_SUCCESS || value_len != 0)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 354 | LOGINT(ctx->ctx); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 355 | ret = LY_EINT; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 356 | goto cleanup; |
| 357 | } |
| 358 | } |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 359 | /* process children */ |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 360 | if (ctx->status == LYXML_ELEMENT && parents_count != ctx->elements.count) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 361 | ret = lydxml_nodes(ctx, (struct lyd_node_inner*)cur, data, lyd_node_children_p(cur)); |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 362 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 363 | } |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 364 | } else if (snode->nodetype & LYD_NODE_ANY) { |
| 365 | unsigned int cur_element_index = ctx->elements.count; |
| 366 | const char *start = *data, *stop; |
| 367 | const char *p, *n; |
| 368 | size_t p_len, n_len; |
| 369 | |
| 370 | /* skip children data and store them as a string */ |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 371 | while (ctx->status != LYXML_END && cur_element_index <= ctx->elements.count) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 372 | switch (ctx->status) { |
| 373 | case LYXML_ELEMENT: |
| 374 | ret = lyxml_get_element((struct lyxml_context *)ctx, data, &p, &p_len, &n, &n_len); |
| 375 | break; |
| 376 | case LYXML_ATTRIBUTE: |
| 377 | lyxml_get_attribute((struct lyxml_context*)ctx, data, &p, &p_len, &n, &n_len); |
| 378 | break; |
| 379 | case LYXML_ELEM_CONTENT: |
| 380 | case LYXML_ATTR_CONTENT: |
| 381 | ret = lyxml_get_string((struct lyxml_context *)ctx, data, NULL, NULL, NULL, NULL, NULL); |
| 382 | if (ret == LY_EINVAL) { |
| 383 | /* not an error, just incorrect XML parser status */ |
| 384 | ret = LY_SUCCESS; |
| 385 | } |
| 386 | break; |
| 387 | case LYXML_END: |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 388 | /* end of data */ |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 389 | LOGINT(ctx->ctx); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 390 | ret = LY_EINT; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 391 | goto cleanup; |
| 392 | } |
| 393 | LY_CHECK_GOTO(ret, cleanup); |
| 394 | } |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 395 | ((struct lyd_node_any*)cur)->value_type = LYD_ANYDATA_XML; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 396 | if (start != *data) { |
| 397 | /* data now points after the anydata's closing element tag, we need just end of its content */ |
| 398 | for (stop = *data - 1; *stop != '<'; --stop); |
| 399 | ((struct lyd_node_any*)cur)->value.xml = lydict_insert(ctx->ctx, start, stop - start); |
| 400 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 401 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 402 | |
| 403 | /* calculate the hash and insert it into parent (list with keys is handled when its keys are inserted) */ |
| 404 | lyd_hash(cur); |
| 405 | lyd_insert_hash(cur); |
Radek Krejci | b6f7ae5 | 2019-07-19 10:31:42 +0200 | [diff] [blame] | 406 | |
| 407 | /* if we have empty non-presence container, we keep it, but mark it as default */ |
| 408 | if (cur->schema->nodetype == LYS_CONTAINER && !((struct lyd_node_inner*)cur)->child && |
| 409 | !cur->attr && !(((struct lysc_node_container*)cur->schema)->flags & LYS_PRESENCE)) { |
| 410 | cur->flags |= LYD_DEFAULT; |
| 411 | } |
| 412 | |
| 413 | /* TODO context validation */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 416 | /* TODO add missing siblings default elements */ |
| 417 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 418 | cleanup: |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 419 | lyd_free_attr(ctx->ctx, attributes, 1); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 420 | return ret; |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 421 | |
| 422 | error: |
| 423 | ret = LY_EVALID; |
| 424 | goto cleanup; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | LY_ERR |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 428 | lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node **trees, struct lyd_node **result) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 429 | { |
Radek Krejci | 18a57d9 | 2019-07-25 14:01:42 +0200 | [diff] [blame] | 430 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 431 | struct lyd_node_inner *parent = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 432 | struct lyd_xml_ctx xmlctx = {0}; |
| 433 | |
| 434 | xmlctx.options = options; |
| 435 | xmlctx.ctx = ctx; |
| 436 | xmlctx.line = 1; |
| 437 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 438 | /* init */ |
| 439 | *result = NULL; |
| 440 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 441 | if (options & LYD_OPT_RPCREPLY) { |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 442 | /* prepare container for RPC reply, for which we need RPC |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 443 | * - prepare *result as top-level node |
| 444 | * - prepare parent as the RPC/action node */ |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 445 | const struct lyd_node *action; |
| 446 | for (action = trees[0]; action && action->schema->nodetype != LYS_ACTION; action = lyd_node_children(action)) { |
| 447 | /* skip list's keys */ |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 448 | for ( ;action && action->schema->nodetype == LYS_LEAF; action = action->next); |
| 449 | if (action && action->schema->nodetype == LYS_ACTION) { |
| 450 | break; |
| 451 | } |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 452 | } |
| 453 | if (!action) { |
| 454 | LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.", |
| 455 | lyd_parse_options_type2str(options)); |
| 456 | return LY_EINVAL; |
| 457 | } |
| 458 | parent = (struct lyd_node_inner*)lyd_dup(action, NULL, LYD_DUP_WITH_PARENTS); |
| 459 | LY_CHECK_ERR_RET(!parent, LOGERR(ctx, ly_errcode(ctx), "Unable to duplicate RPC/action container for RPC/action reply."), ly_errcode(ctx)); |
| 460 | for (*result = (struct lyd_node*)parent; (*result)->parent; *result = (struct lyd_node*)(*result)->parent); |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 461 | } |
| 462 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 463 | if (!data || !data[0]) { |
| 464 | goto no_data; |
| 465 | } |
| 466 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 467 | ret = lydxml_nodes(&xmlctx, parent, &data, *result ? &parent->child : result); |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 468 | if (ret) { |
| 469 | lyd_free_all(*result); |
| 470 | *result = NULL; |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 471 | } else { |
| 472 | /* finish incompletely validated terminal values */ |
| 473 | for (unsigned int u = 0; u < xmlctx.incomplete_type_validation.count; u++) { |
| 474 | struct lyd_node_term *node = (struct lyd_node_term*)xmlctx.incomplete_type_validation.objs[u]; |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 475 | const struct lyd_node **result_trees = NULL; |
Radek Krejci | e72c043 | 2019-06-10 10:17:03 +0200 | [diff] [blame] | 476 | |
| 477 | /* prepare sized array for validator */ |
| 478 | if (*result) { |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 479 | result_trees = lyd_trees_new(1, *result); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 480 | } |
Radek Krejci | e72c043 | 2019-06-10 10:17:03 +0200 | [diff] [blame] | 481 | /* validate and store the value of the node */ |
Radek Krejci | 3c9758d | 2019-07-11 16:49:10 +0200 | [diff] [blame] | 482 | ret = lyd_value_parse(node, node->value.canonized, node->value.canonized ? strlen(node->value.canonized) : 0, 0, 1, |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 483 | lydxml_resolve_prefix, ctx, LYD_XML, result_trees); |
| 484 | lyd_trees_free(result_trees, 0); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 485 | if (ret) { |
| 486 | lyd_free_all(*result); |
| 487 | *result = NULL; |
| 488 | break; |
| 489 | } |
| 490 | } |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 491 | |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 492 | if (!(*result) || (parent && !parent->child)) { |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 493 | no_data: |
| 494 | /* no data */ |
| 495 | if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) { |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 496 | /* error, missing top level node identify RPC and Notification */ |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 497 | LOGERR(ctx, LY_EINVAL, "Invalid input data of data parser - expected %s which cannot be empty.", |
| 498 | lyd_parse_options_type2str(options)); |
| 499 | } else { |
| 500 | /* others - no work is needed, just check for missing mandatory nodes */ |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 501 | /* TODO lyd_validate(&result, options, ctx); |
| 502 | * - according to the data tree type */ |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 503 | } |
| 504 | } |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 505 | } |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 506 | |
| 507 | ly_set_erase(&xmlctx.incomplete_type_validation, NULL); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 508 | lyxml_context_clear((struct lyxml_context*)&xmlctx); |
| 509 | return ret; |
| 510 | } |