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 */ |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * @brief Parse XML attributes of the XML element of YANG data. |
| 51 | * |
| 52 | * @param[in] ctx XML YANG data parser context. |
| 53 | * @param[in] element_name Element identifier to distinguish namespaces defined in different elements. |
| 54 | * @param[in,out] data Pointer to the XML string representation of the YANG data to parse. |
| 55 | * @param[out] attributes Resulting list of the parsed attributes. XML namespace definitions are not parsed |
| 56 | * as attributes, they are stored internally in the parser context. |
| 57 | * @reutn LY_ERR value. |
| 58 | */ |
| 59 | static LY_ERR |
| 60 | lydxml_attributes(struct lyd_xml_ctx *ctx, const char *element_name, const char **data, struct lyd_attr **attributes) |
| 61 | { |
| 62 | LY_ERR ret = LY_SUCCESS; |
| 63 | unsigned int u; |
| 64 | const char *prefix, *name; |
| 65 | size_t prefix_len, name_len; |
| 66 | struct lyd_attr *attr = NULL, *last = NULL; |
| 67 | const struct lyxml_ns *ns; |
| 68 | struct ly_set attr_prefixes = {0}; |
| 69 | struct attr_prefix_s { |
| 70 | const char *prefix; |
| 71 | size_t prefix_len; |
| 72 | } *attr_prefix; |
| 73 | struct lys_module *mod; |
| 74 | |
| 75 | while(ctx->status == LYXML_ATTRIBUTE && |
| 76 | lyxml_get_attribute((struct lyxml_context*)ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS) { |
| 77 | int dynamic = 0; |
| 78 | char *buffer = NULL, *value; |
| 79 | size_t buffer_size = 0, value_len; |
| 80 | |
| 81 | lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic); |
| 82 | |
| 83 | if (prefix_len == 5 && !strncmp(prefix, "xmlns", 5)) { |
| 84 | /* named namespace */ |
| 85 | lyxml_ns_add((struct lyxml_context *)ctx, element_name, name, name_len, |
| 86 | dynamic ? value : strndup(value, value_len)); |
| 87 | } else if (!prefix && name_len == 5 && !strncmp(name, "xmlns", 5)) { |
| 88 | /* default namespace */ |
| 89 | lyxml_ns_add((struct lyxml_context *)ctx, element_name, NULL, 0, |
| 90 | dynamic ? value : strndup(value, value_len)); |
| 91 | } else { |
| 92 | /* attribute */ |
| 93 | attr = calloc(1, sizeof *attr); |
| 94 | LY_CHECK_ERR_GOTO(!attr, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 95 | |
| 96 | attr->name = lydict_insert(ctx->ctx, name, name_len); |
| 97 | /* auxiliary store the prefix information and wait with resolving prefix to the time when all the namespaces, |
| 98 | * defined in this element, are parsed, so we will get the correct namespace for this prefix */ |
| 99 | attr_prefix = malloc(sizeof *attr_prefix); |
| 100 | attr_prefix->prefix = prefix; |
| 101 | attr_prefix->prefix_len = prefix_len; |
| 102 | ly_set_add(&attr_prefixes, attr_prefix, LY_SET_OPT_USEASLIST); |
| 103 | |
| 104 | /* TODO process value */ |
| 105 | |
| 106 | if (last) { |
| 107 | last->next = attr; |
| 108 | } else { |
| 109 | (*attributes) = attr; |
| 110 | } |
| 111 | last = attr; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* resolve annotation pointers in all the attributes */ |
| 116 | for (last = *attributes, u = 0; u < attr_prefixes.count && last; u++, last = last->next) { |
| 117 | attr_prefix = (struct attr_prefix_s*)attr_prefixes.objs[u]; |
| 118 | ns = lyxml_ns_get((struct lyxml_context *)ctx, attr_prefix->prefix, attr_prefix->prefix_len); |
| 119 | mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri); |
| 120 | |
| 121 | /* TODO get annotation */ |
| 122 | } |
| 123 | |
| 124 | cleanup: |
| 125 | |
| 126 | ly_set_erase(&attr_prefixes, free); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @brief Parse XML elements as children YANG data node of the specified parent node. |
| 132 | * |
| 133 | * @param[in] ctx XML YANG data parser context. |
| 134 | * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements. |
| 135 | * @param[in,out] data Pointer to the XML string representation of the YANG data to parse. |
| 136 | * @param[out] node Resulting list of the parsed nodes. |
| 137 | * @reutn LY_ERR value. |
| 138 | */ |
| 139 | static LY_ERR |
| 140 | lydxml_nodes(struct lyd_xml_ctx *ctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **node) |
| 141 | { |
| 142 | LY_ERR ret = LY_SUCCESS; |
| 143 | const char *prefix, *name; |
| 144 | char *element_name = NULL; |
| 145 | size_t prefix_len, name_len; |
| 146 | struct lyd_attr *attributes = NULL; |
| 147 | const struct lyxml_ns *ns; |
| 148 | const struct lysc_node *snode; |
| 149 | struct lys_module *mod; |
| 150 | unsigned int parents_count = ctx->elements.count; |
| 151 | struct lyd_node *cur = NULL, *prev = NULL; |
| 152 | |
| 153 | (*node) = NULL; |
| 154 | |
| 155 | while(ctx->status == LYXML_ELEMENT) { |
| 156 | ret = lyxml_get_element((struct lyxml_context *)ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 157 | LY_CHECK_GOTO(ret, cleanup); |
| 158 | if (!name) { |
| 159 | /* closing previous element */ |
| 160 | lyxml_ns_rm((struct lyxml_context *)ctx, element_name); |
| 161 | free(element_name); |
| 162 | element_name = NULL; |
| 163 | if (ctx->elements.count < parents_count) { |
| 164 | /* all siblings parsed */ |
| 165 | break; |
| 166 | } else { |
| 167 | continue; |
| 168 | } |
| 169 | } |
| 170 | attributes = NULL; |
| 171 | element_name = strndup(name, name_len); |
| 172 | LY_CHECK_GOTO(lydxml_attributes(ctx, element_name, data, &attributes), cleanup); |
| 173 | ns = lyxml_ns_get((struct lyxml_context *)ctx, prefix, prefix_len); |
| 174 | if (!ns) { |
| 175 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%*.s\".", prefix_len, prefix); |
| 176 | goto cleanup; |
| 177 | } |
| 178 | mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri); |
| 179 | if (!mod) { |
| 180 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri); |
| 181 | goto cleanup; |
| 182 | } |
| 183 | snode = lys_child(parent ? parent->schema : NULL, mod, name, name_len, 0, (ctx->options & LYD_OPT_RPCREPLY) ? LYS_GETNEXT_OUTPUT : 0); |
| 184 | if (!snode) { |
| 185 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.", name_len, name, mod->name); |
| 186 | goto cleanup; |
| 187 | } |
| 188 | |
| 189 | /* allocate new node */ |
| 190 | switch (snode->nodetype) { |
| 191 | case LYS_CONTAINER: |
| 192 | case LYS_LIST: |
| 193 | cur = calloc(1, sizeof(struct lyd_node_inner)); |
| 194 | break; |
| 195 | case LYS_LEAF: |
| 196 | case LYS_LEAFLIST: |
| 197 | cur = calloc(1, sizeof(struct lyd_node_term)); |
| 198 | break; |
| 199 | case LYS_ANYDATA: |
| 200 | case LYS_ANYXML: |
| 201 | cur = calloc(1, sizeof(struct lyd_node_any)); |
| 202 | break; |
| 203 | /* TODO LYS_ACTION, LYS_NOTIF */ |
| 204 | default: |
| 205 | LOGINT(ctx->ctx); |
| 206 | goto cleanup; |
| 207 | } |
| 208 | if (!(*node)) { |
| 209 | (*node) = cur; |
| 210 | } |
| 211 | cur->schema = snode; |
| 212 | cur->parent = parent; |
| 213 | if (prev) { |
| 214 | cur->prev = prev; |
| 215 | prev->next = cur; |
| 216 | } else { |
| 217 | cur->prev = cur; |
| 218 | } |
| 219 | prev = cur; |
| 220 | cur->attr = attributes; |
| 221 | attributes = NULL; |
| 222 | |
| 223 | if (snode->nodetype & LYD_NODE_TERM) { |
| 224 | int dynamic = 0; |
| 225 | char *buffer = NULL, *value; |
| 226 | size_t buffer_size = 0, value_len; |
| 227 | |
| 228 | if (ctx->status == LYXML_ELEM_CONTENT) { |
| 229 | /* get the value */ |
| 230 | lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic); |
| 231 | lyd_value_validate((struct lyd_node_term*)cur, value, value_len, |
| 232 | LY_TYPE_VALIDATE_CANONIZE | (dynamic ? LY_TYPE_VALIDATE_DYNAMIC : 0)); |
| 233 | } |
| 234 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 235 | int dynamic = 0; |
| 236 | char *buffer = NULL, *value; |
| 237 | size_t buffer_size = 0, value_len; |
| 238 | |
| 239 | if (ctx->status == LYXML_ELEM_CONTENT) { |
| 240 | LY_ERR r = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic); |
| 241 | if (r != LY_EINVAL) { |
| 242 | LOGINT(ctx->ctx); |
| 243 | goto cleanup; |
| 244 | } |
| 245 | } |
| 246 | if (ctx->status == LYXML_ELEMENT) { |
| 247 | ret = lydxml_nodes(ctx, (struct lyd_node_inner*)cur, data, lyd_node_children_p(cur)); |
| 248 | } |
| 249 | } |
| 250 | /* TODO anyxml/anydata */ |
| 251 | } |
| 252 | |
| 253 | cleanup: |
| 254 | lyxml_ns_rm((struct lyxml_context *)ctx, element_name); |
| 255 | lyd_free_attr(ctx->ctx, attributes, 1); |
| 256 | free(element_name); |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | LY_ERR |
| 261 | lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **result) |
| 262 | { |
| 263 | LY_ERR ret; |
| 264 | struct lyd_xml_ctx xmlctx = {0}; |
| 265 | |
| 266 | xmlctx.options = options; |
| 267 | xmlctx.ctx = ctx; |
| 268 | xmlctx.line = 1; |
| 269 | |
| 270 | ret = lydxml_nodes(&xmlctx, NULL, &data, result); |
| 271 | lyxml_context_clear((struct lyxml_context*)&xmlctx); |
| 272 | return ret; |
| 273 | } |