Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file xml.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief XML data parser for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | */ |
| 21 | |
| 22 | #include <assert.h> |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 23 | #include <ctype.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 24 | #include <errno.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 27 | #include <limits.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 28 | |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 29 | #include "libyang.h" |
| 30 | #include "common.h" |
| 31 | #include "context.h" |
Michal Vasko | b798232 | 2015-10-16 13:56:12 +0200 | [diff] [blame] | 32 | #include "parser.h" |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 33 | #include "tree_internal.h" |
| 34 | #include "validation.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 35 | #include "xml_internal.h" |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 36 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 37 | /* does not log */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 38 | static struct lys_node * |
| 39 | xml_data_search_schemanode(struct lyxml_elem *xml, struct lys_node *start) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 40 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 41 | struct lys_node *result, *aux; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 42 | |
| 43 | LY_TREE_FOR(start, result) { |
| 44 | /* skip groupings */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 45 | if (result->nodetype == LYS_GROUPING) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 46 | continue; |
| 47 | } |
| 48 | |
Radek Krejci | fb54be4 | 2015-10-02 15:21:16 +0200 | [diff] [blame] | 49 | /* go into cases, choices, uses and in RPCs into input and output */ |
| 50 | if (result->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES | LYS_INPUT | LYS_OUTPUT)) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 51 | aux = xml_data_search_schemanode(xml, result->child); |
| 52 | if (aux) { |
| 53 | /* we have matching result */ |
| 54 | return aux; |
| 55 | } |
| 56 | /* else, continue with next schema node */ |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | /* match data nodes */ |
| 61 | if (result->name == xml->name) { |
| 62 | /* names matches, what about namespaces? */ |
| 63 | if (result->module->ns == xml->ns->value) { |
| 64 | /* we have matching result */ |
| 65 | return result; |
| 66 | } |
| 67 | /* else, continue with next schema node */ |
| 68 | continue; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /* no match */ |
| 73 | return NULL; |
| 74 | } |
| 75 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 76 | /* logs directly */ |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 77 | static int |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 78 | xml_get_value(struct lyd_node *node, struct lyxml_elem *xml, int options, struct unres_data *unres) |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 79 | { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 80 | struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 81 | struct lys_type *type, *stype; |
| 82 | int resolve, found; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 83 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 84 | assert(node && (node->schema->nodetype & (LYS_LEAFLIST | LYS_LEAF)) && xml && unres); |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 85 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 86 | stype = &((struct lys_node_leaf *)node->schema)->type; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 87 | leaf->value_str = xml->content; |
| 88 | xml->content = NULL; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 89 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 90 | /* will be changed in case of union */ |
| 91 | leaf->value_type = stype->base; |
Radek Krejci | e3c3314 | 2015-08-10 15:04:36 +0200 | [diff] [blame] | 92 | |
Radek Krejci | e2cf7c1 | 2015-08-12 10:24:05 +0200 | [diff] [blame] | 93 | if ((options & LYD_OPT_FILTER) && !leaf->value_str) { |
| 94 | /* no value in filter (selection) node -> nothing more is needed */ |
| 95 | return EXIT_SUCCESS; |
| 96 | } |
| 97 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 98 | if (options & (LYD_OPT_FILTER | LYD_OPT_EDIT)) { |
| 99 | resolve = 0; |
| 100 | } else { |
| 101 | resolve = 1; |
| 102 | } |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 103 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 104 | if ((stype->base == LY_TYPE_IDENT) || (stype->base == LY_TYPE_INST)) { |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 105 | /* convert the path from the XML form using XML namespaces into the JSON format |
| 106 | * using module names as namespaces |
| 107 | */ |
| 108 | xml->content = leaf->value_str; |
Michal Vasko | fba1526 | 2015-10-21 12:10:28 +0200 | [diff] [blame] | 109 | leaf->value_str = transform_xml2json(leaf->schema->module->ctx, xml->content, xml, 1); |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 110 | lydict_remove(leaf->schema->module->ctx, xml->content); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 111 | xml->content = NULL; |
| 112 | if (!leaf->value_str) { |
| 113 | return EXIT_FAILURE; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 114 | } |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 115 | } |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 116 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 117 | if (stype->base == LY_TYPE_UNION) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 118 | found = 0; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 119 | type = lyp_get_next_union_type(stype, NULL, &found); |
| 120 | while (type) { |
| 121 | leaf->value_type = type->base; |
Michal Vasko | 9f9ea08 | 2015-10-16 13:41:25 +0200 | [diff] [blame] | 122 | |
| 123 | /* in these cases we use JSON format */ |
| 124 | if ((type->base == LY_TYPE_IDENT) || (type->base == LY_TYPE_INST)) { |
| 125 | xml->content = leaf->value_str; |
Michal Vasko | fba1526 | 2015-10-21 12:10:28 +0200 | [diff] [blame] | 126 | leaf->value_str = transform_xml2json(leaf->schema->module->ctx, xml->content, xml, 0); |
Michal Vasko | 9f9ea08 | 2015-10-16 13:41:25 +0200 | [diff] [blame] | 127 | if (!leaf->value_str) { |
| 128 | leaf->value_str = xml->content; |
| 129 | xml->content = NULL; |
| 130 | |
| 131 | found = 0; |
| 132 | type = lyp_get_next_union_type(stype, type, &found); |
| 133 | continue; |
| 134 | } |
| 135 | } |
| 136 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 137 | if (!lyp_parse_value(leaf, type, resolve, unres, UINT_MAX)) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 138 | break; |
| 139 | } |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 140 | |
Michal Vasko | 9f9ea08 | 2015-10-16 13:41:25 +0200 | [diff] [blame] | 141 | if ((type->base == LY_TYPE_IDENT) || (type->base == LY_TYPE_INST)) { |
| 142 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 143 | leaf->value_str = xml->content; |
| 144 | xml->content = NULL; |
| 145 | } |
| 146 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 147 | found = 0; |
| 148 | type = lyp_get_next_union_type(stype, type, &found); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (!type) { |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 152 | LOGVAL(LYE_INVAL, LOGLINE(xml), (leaf->value_str ? leaf->value_str : ""), xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 153 | return EXIT_FAILURE; |
| 154 | } |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 155 | } else if (lyp_parse_value(leaf, stype, resolve, unres, LOGLINE(xml))) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 156 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return EXIT_SUCCESS; |
| 160 | } |
| 161 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 162 | /* logs directly */ |
Michal Vasko | 2443755 | 2015-10-01 16:00:00 +0200 | [diff] [blame] | 163 | static struct lyd_node * |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 164 | xml_parse_data(struct ly_ctx *ctx, struct lyxml_elem *xml, struct lyd_node *parent, struct lyd_node *prev, |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 165 | int options, struct unres_data *unres) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 166 | { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 167 | struct lyd_node *result = NULL, *diter; |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 168 | struct lys_node *schema = NULL; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 169 | struct lyxml_attr *attr; |
Michal Vasko | 9f1ef59 | 2015-09-29 14:57:51 +0200 | [diff] [blame] | 170 | struct lyxml_elem *first_child, *last_child, *child; |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 171 | int i, havechildren; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 172 | |
| 173 | if (!xml) { |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 174 | LOGINT; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 175 | return NULL; |
| 176 | } |
| 177 | if (!xml->ns || !xml->ns->value) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 178 | LOGVAL(LYE_XML_MISS, LOGLINE(xml), "element's", "namespace"); |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | /* find schema node */ |
| 183 | if (!parent) { |
| 184 | /* starting in root */ |
| 185 | for (i = 0; i < ctx->models.used; i++) { |
| 186 | /* match data model based on namespace */ |
| 187 | if (ctx->models.list[i]->ns == xml->ns->value) { |
| 188 | /* get the proper schema node */ |
| 189 | LY_TREE_FOR(ctx->models.list[i]->data, schema) { |
| 190 | if (schema->name == xml->name) { |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | } else { |
| 198 | /* parsing some internal node, we start with parent's schema pointer */ |
| 199 | schema = xml_data_search_schemanode(xml, parent->schema->child); |
| 200 | } |
| 201 | if (!schema) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 202 | if ((options & LYD_OPT_STRICT) || ly_ctx_get_module_by_ns(ctx, xml->ns->value, NULL)) { |
| 203 | LOGVAL(LYE_INELEM, LOGLINE(xml), xml->name); |
| 204 | return NULL; |
| 205 | } else { |
| 206 | goto siblings; |
| 207 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 210 | /* check insert attribute and its values */ |
| 211 | if (options & LYD_OPT_EDIT) { |
| 212 | i = 0; |
| 213 | for (attr = xml->attr; attr; attr = attr->next) { |
| 214 | if (attr->type != LYXML_ATTR_STD || !attr->ns || |
| 215 | strcmp(attr->name, "insert") || strcmp(attr->ns->value, LY_NSYANG)) { |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | /* insert attribute present */ |
| 220 | if (!(schema->flags & LYS_USERORDERED)) { |
| 221 | /* ... but it is not expected */ |
| 222 | LOGVAL(LYE_INATTR, LOGLINE(xml), "insert", schema->name); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | if (i) { |
| 227 | LOGVAL(LYE_TOOMANY, LOGLINE(xml), "insert attributes", xml->name); |
| 228 | return NULL; |
| 229 | } |
| 230 | if (!strcmp(attr->value, "first") || !strcmp(attr->value, "last")) { |
| 231 | i = 1; |
| 232 | } else if (!strcmp(attr->value, "before") || !strcmp(attr->value, "after")) { |
| 233 | i = 2; |
| 234 | } else { |
| 235 | LOGVAL(LYE_INARG, LOGLINE(xml), attr->value, attr->name); |
| 236 | return NULL; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | for (attr = xml->attr; attr; attr = attr->next) { |
| 241 | if (attr->type != LYXML_ATTR_STD || !attr->ns || |
| 242 | strcmp(attr->name, "value") || strcmp(attr->ns->value, LY_NSYANG)) { |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | /* the value attribute is present */ |
| 247 | if (i < 2) { |
| 248 | /* but it shouldn't */ |
| 249 | LOGVAL(LYE_INATTR, LOGLINE(xml), "value", schema->name); |
| 250 | return NULL; |
| 251 | } |
| 252 | i++; |
| 253 | } |
| 254 | if (i == 2) { |
| 255 | /* missing value attribute for "before" or "after" */ |
| 256 | LOGVAL(LYE_MISSATTR, LOGLINE(xml), "value", xml->name); |
| 257 | return NULL; |
| 258 | } else if (i > 3) { |
| 259 | /* more than one instance of the value attribute */ |
| 260 | LOGVAL(LYE_TOOMANY, LOGLINE(xml), "value attributes", xml->name); |
| 261 | return NULL; |
| 262 | } |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 265 | switch (schema->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 266 | case LYS_CONTAINER: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 267 | case LYS_LIST: |
Michal Vasko | 2f30e57 | 2015-10-01 16:00:38 +0200 | [diff] [blame] | 268 | case LYS_NOTIF: |
| 269 | case LYS_RPC: |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 270 | result = calloc(1, sizeof *result); |
| 271 | havechildren = 1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 272 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 273 | case LYS_LEAF: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 274 | case LYS_LEAFLIST: |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 275 | result = calloc(1, sizeof(struct lyd_node_leaf_list)); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 276 | havechildren = 0; |
| 277 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 278 | case LYS_ANYXML: |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 279 | result = calloc(1, sizeof(struct lyd_node_anyxml)); |
| 280 | havechildren = 0; |
| 281 | break; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 282 | default: |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 283 | LOGINT; |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 284 | return NULL; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 285 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 286 | result->parent = parent; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 287 | if (parent && !parent->child) { |
| 288 | parent->child = result; |
| 289 | } |
| 290 | if (prev) { |
| 291 | result->prev = prev; |
| 292 | prev->next = result; |
| 293 | |
| 294 | /* fix the "last" pointer */ |
| 295 | for (diter = prev; diter->prev != prev; diter = diter->prev); |
| 296 | diter->prev = result; |
| 297 | } else { |
| 298 | result->prev = result; |
| 299 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 300 | result->schema = schema; |
| 301 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 302 | if (lyv_data_context(result, options, LOGLINE(xml), unres)) { |
| 303 | goto error; |
| 304 | } |
| 305 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 306 | /* type specific processing */ |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 307 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 308 | /* type detection and assigning the value */ |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 309 | if (xml_get_value(result, xml, options, unres)) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 310 | goto error; |
| 311 | } |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 312 | } else if (schema->nodetype == LYS_ANYXML && !(options & LYD_OPT_FILTER)) { |
Michal Vasko | 9f1ef59 | 2015-09-29 14:57:51 +0200 | [diff] [blame] | 313 | /* unlink xml children, they will be the anyxml value */ |
Radek Krejci | e4e4d72 | 2015-10-05 16:53:50 +0200 | [diff] [blame] | 314 | first_child = last_child = NULL; |
Michal Vasko | 9f1ef59 | 2015-09-29 14:57:51 +0200 | [diff] [blame] | 315 | LY_TREE_FOR(xml->child, child) { |
| 316 | lyxml_unlink_elem(ctx, child, 1); |
| 317 | if (!first_child) { |
| 318 | first_child = child; |
| 319 | last_child = child; |
| 320 | } else { |
| 321 | last_child->next = child; |
| 322 | child->prev = last_child; |
| 323 | last_child = child; |
| 324 | } |
Michal Vasko | 60beecb | 2015-09-03 14:24:09 +0200 | [diff] [blame] | 325 | } |
Radek Krejci | e4e4d72 | 2015-10-05 16:53:50 +0200 | [diff] [blame] | 326 | if (first_child) { |
| 327 | first_child->prev = last_child; |
| 328 | } |
Michal Vasko | 60beecb | 2015-09-03 14:24:09 +0200 | [diff] [blame] | 329 | |
Michal Vasko | 9f1ef59 | 2015-09-29 14:57:51 +0200 | [diff] [blame] | 330 | ((struct lyd_node_anyxml *)result)->value = first_child; |
Michal Vasko | 60beecb | 2015-09-03 14:24:09 +0200 | [diff] [blame] | 331 | /* we can safely continue with xml, it's like it was, only without children */ |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 334 | /* process children */ |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 335 | if (havechildren && xml->child) { |
Michal Vasko | 2f30e57 | 2015-10-01 16:00:38 +0200 | [diff] [blame] | 336 | if (schema->nodetype & (LYS_RPC | LYS_NOTIF)) { |
| 337 | xml_parse_data(ctx, xml->child, result, NULL, 0, unres); |
| 338 | } else { |
| 339 | xml_parse_data(ctx, xml->child, result, NULL, options, unres); |
| 340 | } |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 341 | if (ly_errno) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 342 | goto error; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 343 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Michal Vasko | 4ff7b07 | 2015-08-21 09:05:03 +0200 | [diff] [blame] | 346 | result->attr = (struct lyd_attr *)xml->attr; |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame^] | 347 | for (attr = xml->attr; attr; attr = attr->next) { |
| 348 | if (attr->type == LYXML_ATTR_NS) { |
| 349 | ((struct lyd_ns *)attr)->parent = result; |
| 350 | } |
| 351 | } |
Michal Vasko | 4ff7b07 | 2015-08-21 09:05:03 +0200 | [diff] [blame] | 352 | xml->attr = NULL; |
| 353 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 354 | /* various validation checks */ |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 355 | ly_errno = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 356 | if (lyv_data_content(result, options, LOGLINE(xml), unres)) { |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 357 | if (ly_errno) { |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 358 | goto error; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 359 | } else { |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 360 | goto cleargotosiblings; |
Radek Krejci | da37434 | 2015-08-19 13:33:22 +0200 | [diff] [blame] | 361 | } |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 364 | siblings: |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 365 | /* process siblings */ |
| 366 | if (xml->next) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 367 | if (result) { |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 368 | xml_parse_data(ctx, xml->next, parent, result, options, unres); |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 369 | } else { |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 370 | xml_parse_data(ctx, xml->next, parent, prev, options, unres); |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 371 | } |
| 372 | if (ly_errno) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 373 | goto error; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 374 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | return result; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 378 | |
| 379 | error: |
| 380 | |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 381 | /* cleanup */ |
| 382 | lyd_free(result); |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 383 | |
| 384 | return NULL; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 385 | |
| 386 | cleargotosiblings: |
| 387 | |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 388 | /* remove the result ... */ |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 389 | lyd_free(result); |
| 390 | result = NULL; |
| 391 | |
| 392 | /* ... and then go to siblings label */ |
| 393 | goto siblings; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 396 | API struct lyd_node * |
| 397 | lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem *root, int options) |
| 398 | { |
| 399 | struct lyd_node *result, *next, *iter; |
| 400 | struct unres_data *unres = NULL; |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame^] | 401 | struct lyd_attr *attr; |
| 402 | struct nsc { |
| 403 | struct lyd_ns* ns; |
| 404 | struct nsc *next; |
| 405 | } *nsc = NULL, *nsc_aux, *nsc_prev; |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 406 | |
| 407 | if (!ctx || !root) { |
| 408 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | unres = calloc(1, sizeof *unres); |
| 413 | |
| 414 | ly_errno = 0; |
| 415 | result = xml_parse_data(ctx, root->child, NULL, NULL, options, unres); |
| 416 | |
| 417 | /* check leafrefs and/or instids if any */ |
| 418 | if (result && resolve_unres_data(unres)) { |
| 419 | /* leafref & instid checking failed */ |
| 420 | LY_TREE_FOR_SAFE(result, next, iter) { |
| 421 | lyd_free(iter); |
| 422 | } |
| 423 | result = NULL; |
| 424 | } |
| 425 | |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame^] | 426 | /* namespace cleanup */ |
| 427 | LY_TREE_DFS_BEGIN(result, next, iter) { |
| 428 | for (attr = iter->attr; attr; attr = attr->next) { |
| 429 | if (attr->type == LYD_ATTR_NS) { |
| 430 | /* new namespace found */ |
| 431 | nsc_aux = malloc(sizeof *nsc_aux); |
| 432 | nsc_aux->next = nsc; |
| 433 | nsc_aux->ns = (struct lyd_ns *)attr; |
| 434 | nsc = nsc_aux; |
| 435 | } else if (attr->ns) { |
| 436 | /* search for the used namespace and remove the namespace from nsc list */ |
| 437 | for (nsc_prev = NULL, nsc_aux = nsc; nsc_aux; nsc_prev = nsc_aux, nsc_aux = nsc_aux->next) { |
| 438 | if (nsc_aux->ns == attr->ns) { |
| 439 | if (nsc_prev) { |
| 440 | nsc_prev->next = nsc_aux->next; |
| 441 | } else { |
| 442 | nsc = nsc_aux->next; |
| 443 | } |
| 444 | free(nsc_aux); |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | LY_TREE_DFS_END(result, next, iter)} |
| 451 | while(nsc) { |
| 452 | lyxml_free_attr(nsc->ns->parent->schema->module->ctx, |
| 453 | (struct lyxml_elem *)nsc->ns->parent, |
| 454 | (struct lyxml_attr *)nsc->ns); |
| 455 | |
| 456 | nsc_aux = nsc; |
| 457 | nsc = nsc->next; |
| 458 | free(nsc_aux); |
| 459 | } |
| 460 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 461 | free(unres->node); |
| 462 | free(unres->type); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 463 | #ifndef NDEBUG |
| 464 | free(unres->line); |
| 465 | #endif |
| 466 | free(unres); |
| 467 | |
| 468 | return result; |
| 469 | } |