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 | |
Radek Krejci | 20cdf63 | 2015-11-09 14:44:58 +0100 | [diff] [blame] | 98 | if (options & (LYD_OPT_FILTER | LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG)) { |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 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 */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 163 | static int |
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, |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 165 | int options, struct unres_data *unres, struct lyd_node **result) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 166 | { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 167 | struct lyd_node *diter, *dlast; |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 168 | struct lys_node *schema = NULL; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 169 | struct lyd_attr *dattr, *dattr_iter; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 170 | struct lyxml_attr *attr; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 171 | struct lyxml_elem *first_child, *last_child, *child, *next; |
| 172 | int i, havechildren, r; |
| 173 | int ret = 0; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 174 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 175 | assert(xml); |
| 176 | assert(result); |
| 177 | *result = NULL; |
| 178 | |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 179 | if (!xml->ns || !xml->ns->value) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 180 | LOGVAL(LYE_XML_MISS, LOGLINE(xml), "element's", "namespace"); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 181 | return -1; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* find schema node */ |
| 185 | if (!parent) { |
| 186 | /* starting in root */ |
| 187 | for (i = 0; i < ctx->models.used; i++) { |
| 188 | /* match data model based on namespace */ |
| 189 | if (ctx->models.list[i]->ns == xml->ns->value) { |
| 190 | /* get the proper schema node */ |
| 191 | LY_TREE_FOR(ctx->models.list[i]->data, schema) { |
| 192 | if (schema->name == xml->name) { |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | } else { |
| 200 | /* parsing some internal node, we start with parent's schema pointer */ |
| 201 | schema = xml_data_search_schemanode(xml, parent->schema->child); |
| 202 | } |
| 203 | if (!schema) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 204 | if ((options & LYD_OPT_STRICT) || ly_ctx_get_module_by_ns(ctx, xml->ns->value, NULL)) { |
| 205 | LOGVAL(LYE_INELEM, LOGLINE(xml), xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 206 | return -1; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 207 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 208 | return 0; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 209 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 212 | /* check insert attribute and its values */ |
| 213 | if (options & LYD_OPT_EDIT) { |
| 214 | i = 0; |
| 215 | for (attr = xml->attr; attr; attr = attr->next) { |
| 216 | if (attr->type != LYXML_ATTR_STD || !attr->ns || |
| 217 | strcmp(attr->name, "insert") || strcmp(attr->ns->value, LY_NSYANG)) { |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | /* insert attribute present */ |
| 222 | if (!(schema->flags & LYS_USERORDERED)) { |
| 223 | /* ... but it is not expected */ |
| 224 | LOGVAL(LYE_INATTR, LOGLINE(xml), "insert", schema->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 225 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | if (i) { |
| 229 | LOGVAL(LYE_TOOMANY, LOGLINE(xml), "insert attributes", xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 230 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 231 | } |
| 232 | if (!strcmp(attr->value, "first") || !strcmp(attr->value, "last")) { |
| 233 | i = 1; |
| 234 | } else if (!strcmp(attr->value, "before") || !strcmp(attr->value, "after")) { |
| 235 | i = 2; |
| 236 | } else { |
| 237 | LOGVAL(LYE_INARG, LOGLINE(xml), attr->value, attr->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 238 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | for (attr = xml->attr; attr; attr = attr->next) { |
| 243 | if (attr->type != LYXML_ATTR_STD || !attr->ns || |
| 244 | strcmp(attr->name, "value") || strcmp(attr->ns->value, LY_NSYANG)) { |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | /* the value attribute is present */ |
| 249 | if (i < 2) { |
| 250 | /* but it shouldn't */ |
| 251 | LOGVAL(LYE_INATTR, LOGLINE(xml), "value", schema->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 252 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 253 | } |
| 254 | i++; |
| 255 | } |
| 256 | if (i == 2) { |
| 257 | /* missing value attribute for "before" or "after" */ |
| 258 | LOGVAL(LYE_MISSATTR, LOGLINE(xml), "value", xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 259 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 260 | } else if (i > 3) { |
| 261 | /* more than one instance of the value attribute */ |
| 262 | LOGVAL(LYE_TOOMANY, LOGLINE(xml), "value attributes", xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 263 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 264 | } |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 267 | switch (schema->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 268 | case LYS_CONTAINER: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 269 | case LYS_LIST: |
Michal Vasko | 2f30e57 | 2015-10-01 16:00:38 +0200 | [diff] [blame] | 270 | case LYS_NOTIF: |
| 271 | case LYS_RPC: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 272 | *result = calloc(1, sizeof **result); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 273 | havechildren = 1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 274 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 275 | case LYS_LEAF: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 276 | case LYS_LEAFLIST: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 277 | *result = calloc(1, sizeof(struct lyd_node_leaf_list)); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 278 | havechildren = 0; |
| 279 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 280 | case LYS_ANYXML: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 281 | *result = calloc(1, sizeof(struct lyd_node_anyxml)); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 282 | havechildren = 0; |
| 283 | break; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 284 | default: |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 285 | LOGINT; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 286 | return -1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 287 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 288 | (*result)->parent = parent; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 289 | if (parent && !parent->child) { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 290 | parent->child = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 291 | } |
| 292 | if (prev) { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 293 | (*result)->prev = prev; |
| 294 | prev->next = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 295 | |
| 296 | /* fix the "last" pointer */ |
| 297 | for (diter = prev; diter->prev != prev; diter = diter->prev); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 298 | diter->prev = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 299 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 300 | (*result)->prev = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 301 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 302 | (*result)->schema = schema; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 303 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 304 | if (lyv_data_context(*result, options, LOGLINE(xml), unres)) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 305 | goto error; |
| 306 | } |
| 307 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 308 | /* type specific processing */ |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 309 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 310 | /* type detection and assigning the value */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 311 | if (xml_get_value(*result, xml, options, unres)) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 312 | goto error; |
| 313 | } |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 314 | } else if (schema->nodetype == LYS_ANYXML && !(options & LYD_OPT_FILTER)) { |
Michal Vasko | 9f1ef59 | 2015-09-29 14:57:51 +0200 | [diff] [blame] | 315 | /* unlink xml children, they will be the anyxml value */ |
Radek Krejci | e4e4d72 | 2015-10-05 16:53:50 +0200 | [diff] [blame] | 316 | first_child = last_child = NULL; |
Michal Vasko | 9f1ef59 | 2015-09-29 14:57:51 +0200 | [diff] [blame] | 317 | LY_TREE_FOR(xml->child, child) { |
| 318 | lyxml_unlink_elem(ctx, child, 1); |
| 319 | if (!first_child) { |
| 320 | first_child = child; |
| 321 | last_child = child; |
| 322 | } else { |
| 323 | last_child->next = child; |
| 324 | child->prev = last_child; |
| 325 | last_child = child; |
| 326 | } |
Michal Vasko | 60beecb | 2015-09-03 14:24:09 +0200 | [diff] [blame] | 327 | } |
Radek Krejci | e4e4d72 | 2015-10-05 16:53:50 +0200 | [diff] [blame] | 328 | if (first_child) { |
| 329 | first_child->prev = last_child; |
| 330 | } |
Michal Vasko | 60beecb | 2015-09-03 14:24:09 +0200 | [diff] [blame] | 331 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 332 | ((struct lyd_node_anyxml *)(*result))->value = first_child; |
Michal Vasko | 60beecb | 2015-09-03 14:24:09 +0200 | [diff] [blame] | 333 | /* 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] | 334 | } |
| 335 | |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 336 | for (attr = xml->attr; attr; attr = attr->next) { |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 337 | if (attr->type != LYXML_ATTR_STD) { |
| 338 | continue; |
| 339 | } else if (!attr->ns) { |
| 340 | LOGWRN("Ignoring \"%s\" attribute in \"%s\" element.", attr->name, xml->name); |
| 341 | continue; |
| 342 | } |
| 343 | |
| 344 | dattr = malloc(sizeof *dattr); |
| 345 | dattr->next = NULL; |
| 346 | dattr->name = attr->name; |
| 347 | dattr->value = attr->value; |
| 348 | attr->name = NULL; |
| 349 | attr->value = NULL; |
| 350 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 351 | dattr->module = (struct lys_module *)ly_ctx_get_module_by_ns(ctx, attr->ns->value, NULL); |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 352 | if (!dattr->module) { |
| 353 | LOGWRN("Attribute \"%s\" from unknown schema (\"%s\") - skipping.", attr->name, attr->ns->value); |
| 354 | free(dattr); |
| 355 | continue; |
| 356 | } |
| 357 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 358 | if (!(*result)->attr) { |
| 359 | (*result)->attr = dattr; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 360 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 361 | for (dattr_iter = (*result)->attr; dattr_iter->next; dattr_iter = dattr_iter->next); |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 362 | dattr_iter->next = dattr; |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 363 | } |
| 364 | } |
Michal Vasko | 4ff7b07 | 2015-08-21 09:05:03 +0200 | [diff] [blame] | 365 | |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 366 | /* process children */ |
| 367 | if (havechildren && xml->child) { |
| 368 | diter = dlast = NULL; |
| 369 | LY_TREE_FOR_SAFE(xml->child, next, child) { |
| 370 | if (schema->nodetype & (LYS_RPC | LYS_NOTIF)) { |
| 371 | r = xml_parse_data(ctx, child, *result, dlast, 0, unres, &diter); |
| 372 | } else { |
| 373 | r = xml_parse_data(ctx, child, *result, dlast, options, unres, &diter); |
| 374 | } |
| 375 | if (options & LYD_OPT_DESTRUCT) { |
| 376 | lyxml_free_elem(ctx, child); |
| 377 | } |
| 378 | if (r) { |
| 379 | goto error; |
| 380 | } |
| 381 | if (diter) { |
| 382 | dlast = diter; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 387 | /* various validation checks */ |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 388 | ly_errno = 0; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 389 | if (lyv_data_content(*result, options, LOGLINE(xml), unres)) { |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 390 | if (ly_errno) { |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 391 | goto error; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 392 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 393 | goto clear; |
Radek Krejci | da37434 | 2015-08-19 13:33:22 +0200 | [diff] [blame] | 394 | } |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 397 | return ret; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 398 | |
| 399 | error: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 400 | ret--; |
| 401 | |
| 402 | clear: |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 403 | /* cleanup */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 404 | lyd_free(*result); |
| 405 | *result = NULL; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 406 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 407 | return ret; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 408 | } |
| 409 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 410 | API struct lyd_node * |
| 411 | lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem *root, int options) |
| 412 | { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 413 | struct lyd_node *result, *next, *iter, *last; |
| 414 | struct lyxml_elem *xmlelem, *xmlaux; |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 415 | struct unres_data *unres = NULL; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 416 | int r; |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 417 | |
| 418 | if (!ctx || !root) { |
| 419 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | unres = calloc(1, sizeof *unres); |
| 424 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 425 | iter = result = last = NULL; |
| 426 | LY_TREE_FOR_SAFE(root->child, xmlaux, xmlelem) { |
| 427 | r = xml_parse_data(ctx, xmlelem, NULL, last, options, unres, &iter); |
Radek Krejci | 04b97de | 2015-10-31 23:09:15 +0100 | [diff] [blame] | 428 | if (options & LYD_OPT_DESTRUCT) { |
| 429 | lyxml_free_elem(ctx, xmlelem); |
| 430 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 431 | if (r) { |
| 432 | LY_TREE_FOR_SAFE(result, next, iter) { |
| 433 | lyd_free(iter); |
| 434 | } |
| 435 | result = NULL; |
| 436 | goto cleanup; |
| 437 | } |
| 438 | if (iter) { |
| 439 | last = iter; |
| 440 | } |
| 441 | if (!result) { |
| 442 | result = iter; |
| 443 | } |
| 444 | } |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 445 | |
Michal Vasko | c1cf86f | 2015-11-04 09:54:51 +0100 | [diff] [blame] | 446 | if (!result) { |
| 447 | LOGERR(LY_EVALID, "Model for the data to be linked with not found."); |
| 448 | goto cleanup; |
| 449 | } |
| 450 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 451 | /* check leafrefs and/or instids if any */ |
| 452 | if (result && resolve_unres_data(unres)) { |
| 453 | /* leafref & instid checking failed */ |
| 454 | LY_TREE_FOR_SAFE(result, next, iter) { |
| 455 | lyd_free(iter); |
| 456 | } |
| 457 | result = NULL; |
| 458 | } |
| 459 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 460 | cleanup: |
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 | } |