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> |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 25 | #include <stdarg.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 28 | #include <limits.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 29 | |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 30 | #include "libyang.h" |
| 31 | #include "common.h" |
| 32 | #include "context.h" |
Michal Vasko | b798232 | 2015-10-16 13:56:12 +0200 | [diff] [blame] | 33 | #include "parser.h" |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 34 | #include "tree_internal.h" |
| 35 | #include "validation.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 36 | #include "xml_internal.h" |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 37 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 38 | /* does not log */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 39 | static struct lys_node * |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 40 | xml_data_search_schemanode(struct lyxml_elem *xml, struct lys_node *start, int options) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 41 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 42 | struct lys_node *result, *aux; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 43 | |
| 44 | LY_TREE_FOR(start, result) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 45 | /* skip groupings ... */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 46 | if (result->nodetype == LYS_GROUPING) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 47 | continue; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 48 | /* ... and output in case of RPC ... */ |
| 49 | } else if (result->nodetype == LYS_OUTPUT && (options & LYD_OPT_RPC)) { |
| 50 | continue; |
| 51 | /* ... and input in case of RPC reply */ |
| 52 | } else if (result->nodetype == LYS_INPUT && (options & LYD_OPT_RPCREPLY)) { |
| 53 | continue; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Radek Krejci | fb54be4 | 2015-10-02 15:21:16 +0200 | [diff] [blame] | 56 | /* go into cases, choices, uses and in RPCs into input and output */ |
| 57 | if (result->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES | LYS_INPUT | LYS_OUTPUT)) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 58 | aux = xml_data_search_schemanode(xml, result->child, options); |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 59 | if (aux) { |
| 60 | /* we have matching result */ |
| 61 | return aux; |
| 62 | } |
| 63 | /* else, continue with next schema node */ |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | /* match data nodes */ |
| 68 | if (result->name == xml->name) { |
| 69 | /* names matches, what about namespaces? */ |
| 70 | if (result->module->ns == xml->ns->value) { |
| 71 | /* we have matching result */ |
| 72 | return result; |
| 73 | } |
| 74 | /* else, continue with next schema node */ |
| 75 | continue; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* no match */ |
| 80 | return NULL; |
| 81 | } |
| 82 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 83 | /* logs directly */ |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 84 | static int |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 85 | 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] | 86 | { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 87 | struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame^] | 88 | int resolve; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 89 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 90 | assert(node && (node->schema->nodetype & (LYS_LEAFLIST | LYS_LEAF)) && xml && unres); |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 91 | |
| 92 | leaf->value_str = xml->content; |
| 93 | xml->content = NULL; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 94 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 95 | /* will be changed in case of union */ |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame^] | 96 | leaf->value_type = ((struct lys_node_leaf *)node->schema)->type.base; |
Radek Krejci | e3c3314 | 2015-08-10 15:04:36 +0200 | [diff] [blame] | 97 | |
Radek Krejci | e2cf7c1 | 2015-08-12 10:24:05 +0200 | [diff] [blame] | 98 | if ((options & LYD_OPT_FILTER) && !leaf->value_str) { |
| 99 | /* no value in filter (selection) node -> nothing more is needed */ |
| 100 | return EXIT_SUCCESS; |
| 101 | } |
| 102 | |
Radek Krejci | 20cdf63 | 2015-11-09 14:44:58 +0100 | [diff] [blame] | 103 | 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] | 104 | resolve = 0; |
| 105 | } else { |
| 106 | resolve = 1; |
| 107 | } |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 108 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame^] | 109 | if ((leaf->value_type == LY_TYPE_IDENT) || (leaf->value_type == LY_TYPE_INST)) { |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 110 | /* convert the path from the XML form using XML namespaces into the JSON format |
| 111 | * using module names as namespaces |
| 112 | */ |
| 113 | xml->content = leaf->value_str; |
Michal Vasko | fba1526 | 2015-10-21 12:10:28 +0200 | [diff] [blame] | 114 | 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] | 115 | lydict_remove(leaf->schema->module->ctx, xml->content); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 116 | xml->content = NULL; |
| 117 | if (!leaf->value_str) { |
| 118 | return EXIT_FAILURE; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 119 | } |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 120 | } |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 121 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame^] | 122 | if (lyp_parse_value(leaf, xml, resolve, unres, LOGLINE(xml))) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 123 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return EXIT_SUCCESS; |
| 127 | } |
| 128 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 129 | /* logs directly */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 130 | static int |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 131 | xml_parse_data(struct ly_ctx *ctx, struct lyxml_elem *xml, const struct lys_node *schema_parent, struct lyd_node *parent, |
| 132 | struct lyd_node *prev, int options, struct unres_data *unres, struct lyd_node **result) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 133 | { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 134 | struct lyd_node *diter, *dlast; |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 135 | struct lys_node *schema = NULL; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 136 | struct lyd_attr *dattr, *dattr_iter; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 137 | struct lyxml_attr *attr; |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 138 | struct lyxml_elem *tmp_xml, *child, *next; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 139 | int i, havechildren, r; |
| 140 | int ret = 0; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 141 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 142 | assert(xml); |
| 143 | assert(result); |
| 144 | *result = NULL; |
| 145 | |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 146 | if (!xml->ns || !xml->ns->value) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 147 | LOGVAL(LYE_XML_MISS, LOGLINE(xml), "element's", "namespace"); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 148 | return -1; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* find schema node */ |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 152 | if (schema_parent) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 153 | schema = xml_data_search_schemanode(xml, schema_parent->child, options); |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 154 | } else if (!parent) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 155 | /* starting in root */ |
| 156 | for (i = 0; i < ctx->models.used; i++) { |
| 157 | /* match data model based on namespace */ |
| 158 | if (ctx->models.list[i]->ns == xml->ns->value) { |
| 159 | /* get the proper schema node */ |
| 160 | LY_TREE_FOR(ctx->models.list[i]->data, schema) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 161 | /* skip nodes in module's data which are not expected here according to options' data type */ |
| 162 | if (options & LYD_OPT_RPC) { |
| 163 | if (schema->nodetype != LYS_RPC) { |
| 164 | continue; |
| 165 | } |
| 166 | } else if (options & LYD_OPT_NOTIF) { |
| 167 | if (schema->nodetype != LYS_NOTIF) { |
| 168 | continue; |
| 169 | } |
| 170 | } else if (!(options & LYD_OPT_RPCREPLY)) { |
| 171 | /* rest of the data types except RPCREPLY which cannot be here */ |
| 172 | if (schema->nodetype & (LYS_RPC | LYS_NOTIF)) { |
| 173 | continue; |
| 174 | } |
| 175 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 176 | if (schema->name == xml->name) { |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | } else { |
| 184 | /* parsing some internal node, we start with parent's schema pointer */ |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 185 | schema = xml_data_search_schemanode(xml, parent->schema->child, options); |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 186 | } |
| 187 | if (!schema) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 188 | if ((options & LYD_OPT_STRICT) || ly_ctx_get_module_by_ns(ctx, xml->ns->value, NULL)) { |
| 189 | LOGVAL(LYE_INELEM, LOGLINE(xml), xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 190 | return -1; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 191 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 192 | return 0; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 193 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 196 | /* check insert attribute and its values */ |
| 197 | if (options & LYD_OPT_EDIT) { |
| 198 | i = 0; |
| 199 | for (attr = xml->attr; attr; attr = attr->next) { |
| 200 | if (attr->type != LYXML_ATTR_STD || !attr->ns || |
| 201 | strcmp(attr->name, "insert") || strcmp(attr->ns->value, LY_NSYANG)) { |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | /* insert attribute present */ |
| 206 | if (!(schema->flags & LYS_USERORDERED)) { |
| 207 | /* ... but it is not expected */ |
| 208 | LOGVAL(LYE_INATTR, LOGLINE(xml), "insert", schema->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 209 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | if (i) { |
| 213 | LOGVAL(LYE_TOOMANY, LOGLINE(xml), "insert attributes", xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 214 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 215 | } |
| 216 | if (!strcmp(attr->value, "first") || !strcmp(attr->value, "last")) { |
| 217 | i = 1; |
| 218 | } else if (!strcmp(attr->value, "before") || !strcmp(attr->value, "after")) { |
| 219 | i = 2; |
| 220 | } else { |
| 221 | LOGVAL(LYE_INARG, LOGLINE(xml), attr->value, attr->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 222 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | for (attr = xml->attr; attr; attr = attr->next) { |
| 227 | if (attr->type != LYXML_ATTR_STD || !attr->ns || |
| 228 | strcmp(attr->name, "value") || strcmp(attr->ns->value, LY_NSYANG)) { |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | /* the value attribute is present */ |
| 233 | if (i < 2) { |
| 234 | /* but it shouldn't */ |
| 235 | LOGVAL(LYE_INATTR, LOGLINE(xml), "value", schema->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 236 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 237 | } |
| 238 | i++; |
| 239 | } |
| 240 | if (i == 2) { |
| 241 | /* missing value attribute for "before" or "after" */ |
| 242 | LOGVAL(LYE_MISSATTR, LOGLINE(xml), "value", xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 243 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 244 | } else if (i > 3) { |
| 245 | /* more than one instance of the value attribute */ |
| 246 | LOGVAL(LYE_TOOMANY, LOGLINE(xml), "value attributes", xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 247 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 248 | } |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 251 | switch (schema->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 252 | case LYS_CONTAINER: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 253 | case LYS_LIST: |
Michal Vasko | 2f30e57 | 2015-10-01 16:00:38 +0200 | [diff] [blame] | 254 | case LYS_NOTIF: |
| 255 | case LYS_RPC: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 256 | *result = calloc(1, sizeof **result); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 257 | havechildren = 1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 258 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 259 | case LYS_LEAF: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 260 | case LYS_LEAFLIST: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 261 | *result = calloc(1, sizeof(struct lyd_node_leaf_list)); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 262 | havechildren = 0; |
| 263 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 264 | case LYS_ANYXML: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 265 | *result = calloc(1, sizeof(struct lyd_node_anyxml)); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 266 | havechildren = 0; |
| 267 | break; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 268 | default: |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 269 | LOGINT; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 270 | return -1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 271 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 272 | if (!(*result)) { |
| 273 | LOGMEM; |
| 274 | return -1; |
| 275 | } |
| 276 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 277 | (*result)->parent = parent; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 278 | if (parent && !parent->child) { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 279 | parent->child = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 280 | } |
| 281 | if (prev) { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 282 | (*result)->prev = prev; |
| 283 | prev->next = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 284 | |
| 285 | /* fix the "last" pointer */ |
| 286 | for (diter = prev; diter->prev != prev; diter = diter->prev); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 287 | diter->prev = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 288 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 289 | (*result)->prev = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 290 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 291 | (*result)->schema = schema; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 292 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 293 | if (lyv_data_context(*result, options, LOGLINE(xml), unres)) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 294 | goto error; |
| 295 | } |
| 296 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 297 | /* type specific processing */ |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 298 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 299 | /* type detection and assigning the value */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 300 | if (xml_get_value(*result, xml, options, unres)) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 301 | goto error; |
| 302 | } |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 303 | } else if (schema->nodetype == LYS_ANYXML && !(options & LYD_OPT_FILTER)) { |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 304 | /* HACK unlink xml children and link them to a separate copy of xml */ |
| 305 | tmp_xml = calloc(1, sizeof *tmp_xml); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 306 | if (!tmp_xml) { |
| 307 | LOGMEM; |
| 308 | goto error; |
| 309 | } |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 310 | memcpy(tmp_xml, xml, sizeof *tmp_xml); |
| 311 | /* keep attributes in the original */ |
| 312 | tmp_xml->attr = NULL; |
| 313 | /* increase reference counters on strings */ |
| 314 | tmp_xml->name = lydict_insert(ctx, tmp_xml->name, 0); |
| 315 | tmp_xml->content = lydict_insert(ctx, tmp_xml->content, 0); |
| 316 | xml->child = NULL; |
| 317 | /* xml is correct now */ |
Michal Vasko | 60beecb | 2015-09-03 14:24:09 +0200 | [diff] [blame] | 318 | |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 319 | LY_TREE_FOR(tmp_xml->child, child) { |
| 320 | child->parent = tmp_xml; |
| 321 | } |
| 322 | /* children are correct now */ |
| 323 | |
| 324 | tmp_xml->parent = NULL; |
| 325 | lyxml_unlink_elem(ctx, tmp_xml, 1); |
| 326 | /* tmp_xml is correct now */ |
| 327 | |
| 328 | ((struct lyd_node_anyxml *)*result)->value = tmp_xml; |
Michal Vasko | 60beecb | 2015-09-03 14:24:09 +0200 | [diff] [blame] | 329 | /* 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] | 330 | } |
| 331 | |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 332 | for (attr = xml->attr; attr; attr = attr->next) { |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 333 | if (attr->type != LYXML_ATTR_STD) { |
| 334 | continue; |
| 335 | } else if (!attr->ns) { |
| 336 | LOGWRN("Ignoring \"%s\" attribute in \"%s\" element.", attr->name, xml->name); |
| 337 | continue; |
| 338 | } |
| 339 | |
| 340 | dattr = malloc(sizeof *dattr); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 341 | if (!dattr) { |
| 342 | goto error; |
| 343 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 344 | dattr->next = NULL; |
| 345 | dattr->name = attr->name; |
| 346 | dattr->value = attr->value; |
| 347 | attr->name = NULL; |
| 348 | attr->value = NULL; |
| 349 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 350 | 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] | 351 | if (!dattr->module) { |
| 352 | LOGWRN("Attribute \"%s\" from unknown schema (\"%s\") - skipping.", attr->name, attr->ns->value); |
| 353 | free(dattr); |
| 354 | continue; |
| 355 | } |
| 356 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 357 | if (!(*result)->attr) { |
| 358 | (*result)->attr = dattr; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 359 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 360 | 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] | 361 | dattr_iter->next = dattr; |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 362 | } |
| 363 | } |
Michal Vasko | 4ff7b07 | 2015-08-21 09:05:03 +0200 | [diff] [blame] | 364 | |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 365 | /* process children */ |
| 366 | if (havechildren && xml->child) { |
| 367 | diter = dlast = NULL; |
| 368 | LY_TREE_FOR_SAFE(xml->child, next, child) { |
| 369 | if (schema->nodetype & (LYS_RPC | LYS_NOTIF)) { |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 370 | r = xml_parse_data(ctx, child, NULL, *result, dlast, 0, unres, &diter); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 371 | } else { |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 372 | r = xml_parse_data(ctx, child, NULL, *result, dlast, options, unres, &diter); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 373 | } |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 374 | if (r) { |
| 375 | goto error; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 376 | } else if (options & LYD_OPT_DESTRUCT) { |
| 377 | lyxml_free(ctx, child); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 378 | } |
| 379 | if (diter) { |
| 380 | dlast = diter; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 385 | /* rest of validation checks */ |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 386 | ly_errno = 0; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 387 | if (lyv_data_content(*result, options, LOGLINE(xml), unres)) { |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 388 | if (ly_errno) { |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 389 | goto error; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 390 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 391 | goto clear; |
Radek Krejci | da37434 | 2015-08-19 13:33:22 +0200 | [diff] [blame] | 392 | } |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 395 | return ret; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 396 | |
| 397 | error: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 398 | ret--; |
| 399 | |
| 400 | clear: |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 401 | /* cleanup */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 402 | lyd_free(*result); |
| 403 | *result = NULL; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 404 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 405 | return ret; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 408 | API struct lyd_node * |
| 409 | lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options, ...) |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 410 | { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 411 | va_list ap; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 412 | int r; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 413 | struct unres_data *unres = NULL; |
| 414 | const struct lys_node *rpc = NULL; |
| 415 | struct lyd_node *result = NULL, *next, *iter, *last; |
| 416 | struct lyxml_elem *xmlstart, *xmlelem, *xmlaux; |
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 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 423 | if (lyp_check_options(options)) { |
| 424 | LOGERR(LY_EINVAL, "%s: Invalid options (multiple data type flags set).", __func__); |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | va_start(ap, options); |
| 429 | if (options & LYD_OPT_RPCREPLY) { |
| 430 | rpc = va_arg(ap, struct lys_node*); |
| 431 | if (!rpc || (rpc->nodetype != LYS_RPC)) { |
| 432 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 433 | goto cleanup; |
| 434 | } |
| 435 | } |
| 436 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 437 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 438 | if (!unres) { |
| 439 | LOGMEM; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 440 | goto cleanup; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 441 | } |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 442 | |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 443 | if (!(options & LYD_OPT_NOSIBLINGS)) { |
| 444 | /* locate the first root to process */ |
| 445 | if ((*root)->parent) { |
| 446 | xmlstart = (*root)->parent->child; |
| 447 | } else { |
| 448 | xmlstart = *root; |
| 449 | while(xmlstart->prev->next) { |
| 450 | xmlstart = xmlstart->prev; |
| 451 | } |
Radek Krejci | 04b97de | 2015-10-31 23:09:15 +0100 | [diff] [blame] | 452 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 453 | } else { |
| 454 | xmlstart = *root; |
| 455 | } |
| 456 | iter = result = last = NULL; |
| 457 | |
| 458 | LY_TREE_FOR_SAFE(xmlstart, xmlaux, xmlelem) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 459 | r = xml_parse_data(ctx, xmlelem, rpc, NULL, last, options, unres, &iter); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 460 | if (r) { |
| 461 | LY_TREE_FOR_SAFE(result, next, iter) { |
| 462 | lyd_free(iter); |
| 463 | } |
| 464 | result = NULL; |
| 465 | goto cleanup; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 466 | } else if (options & LYD_OPT_DESTRUCT) { |
| 467 | lyxml_free(ctx, xmlelem); |
| 468 | *root = xmlaux; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 469 | } |
| 470 | if (iter) { |
| 471 | last = iter; |
| 472 | } |
| 473 | if (!result) { |
| 474 | result = iter; |
| 475 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 476 | |
| 477 | if (options & LYD_OPT_NOSIBLINGS) { |
| 478 | /* stop after the first processed root */ |
| 479 | break; |
| 480 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 481 | } |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 482 | |
Michal Vasko | c1cf86f | 2015-11-04 09:54:51 +0100 | [diff] [blame] | 483 | if (!result) { |
| 484 | LOGERR(LY_EVALID, "Model for the data to be linked with not found."); |
| 485 | goto cleanup; |
| 486 | } |
| 487 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 488 | /* check leafrefs and/or instids if any */ |
| 489 | if (result && resolve_unres_data(unres)) { |
| 490 | /* leafref & instid checking failed */ |
| 491 | LY_TREE_FOR_SAFE(result, next, iter) { |
| 492 | lyd_free(iter); |
| 493 | } |
| 494 | result = NULL; |
| 495 | } |
| 496 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 497 | cleanup: |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 498 | if (unres) { |
| 499 | free(unres->node); |
| 500 | free(unres->type); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 501 | #ifndef NDEBUG |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 502 | free(unres->line); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 503 | #endif |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 504 | free(unres); |
| 505 | } |
| 506 | va_end(ap); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 507 | |
| 508 | return result; |
| 509 | } |