Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file printer_xml.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 5 | * @brief XML printer for libyang data structure |
| 6 | * |
| 7 | * Copyright (c) 2015 - 2019 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #include "common.h" |
| 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 20 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | |
| 22 | #include "log.h" |
| 23 | #include "plugins_types.h" |
| 24 | #include "printer_data.h" |
| 25 | #include "printer_internal.h" |
| 26 | #include "tree.h" |
| 27 | #include "tree_data.h" |
| 28 | #include "tree_schema.h" |
| 29 | #include "xml.h" |
| 30 | |
| 31 | /** |
| 32 | * @brief XML printer context. |
| 33 | */ |
| 34 | struct xmlpr_ctx { |
| 35 | struct lyout *out; /**< output specification */ |
| 36 | unsigned int level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */ |
| 37 | int options; /**< [Data printer flags](@ref dataprinterflags) */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 38 | const struct ly_ctx *ctx; /**< libyang context */ |
| 39 | struct ly_set prefix; /**< printed namespace prefixes */ |
| 40 | struct ly_set ns; /**< printed namespaces */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | #define LEVEL ctx->level /**< current level */ |
| 44 | #define INDENT ((LEVEL) ? (LEVEL)*2 : 0),"" /**< indentation parameters for printer functions */ |
| 45 | #define LEVEL_INC if (LEVEL) {LEVEL++;} /**< increase indentation level */ |
| 46 | #define LEVEL_DEC if (LEVEL) {LEVEL--;} /**< decrease indentation level */ |
| 47 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 48 | #define LYXML_PREFIX_REQUIRED 0x01 /**< The prefix is not just a suggestion but a requirement. */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 49 | |
| 50 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 51 | * @brief Print a namespace if not already printed. |
| 52 | * |
| 53 | * @param[in] ctx XML printer context. |
| 54 | * @param[in] ns Namespace to print, expected to be in dictionary. |
| 55 | * @param[in] new_prefix Suggested new prefix, NULL for a default namespace without prefix. Stored in the dictionary. |
| 56 | * @param[in] prefix_opts Prefix options changing the meaning of parameters. |
| 57 | * @return Printed prefix of the namespace to use. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 58 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 59 | static const char * |
| 60 | xml_print_ns(struct xmlpr_ctx *ctx, const char *ns, const char *new_prefix, int prefix_opts) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 61 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 62 | int i; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 63 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 64 | for (i = ctx->ns.count - 1; i > -1; --i) { |
| 65 | if (!new_prefix) { |
| 66 | /* find default namespace */ |
| 67 | if (!ctx->prefix.objs[i]) { |
| 68 | if (ctx->ns.objs[i] != ns) { |
| 69 | /* different default namespace */ |
| 70 | i = -1; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 71 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 72 | break; |
| 73 | } |
| 74 | } else { |
| 75 | /* find prefixed namespace */ |
| 76 | if (ctx->ns.objs[i] == ns) { |
| 77 | if (!ctx->prefix.objs[i]) { |
| 78 | /* default namespace is not interesting */ |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | if (!strcmp(ctx->prefix.objs[i], new_prefix) || !(prefix_opts & LYXML_PREFIX_REQUIRED)) { |
| 83 | /* the same prefix or can be any */ |
| 84 | break; |
| 85 | } |
| 86 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 87 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 88 | } |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 89 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 90 | if (i == -1) { |
| 91 | /* suitable namespace not found, must be printed */ |
| 92 | ly_print(ctx->out, " xmlns%s%s=\"%s\"", new_prefix ? ":" : "", new_prefix ? new_prefix : "", ns); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 93 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 94 | /* and added into namespaces */ |
| 95 | if (new_prefix) { |
| 96 | new_prefix = lydict_insert(ctx->ctx, new_prefix, 0); |
| 97 | } |
| 98 | ly_set_add(&ctx->prefix, (void *)new_prefix, LY_SET_OPT_USEASLIST); |
| 99 | i = ly_set_add(&ctx->ns, (void *)ns, LY_SET_OPT_USEASLIST); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 100 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 101 | |
| 102 | /* return it */ |
| 103 | return ctx->prefix.objs[i]; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @brief XML mapping of YANG modules to prefixes in values. |
| 108 | * |
| 109 | * Implementation of ly_clb_get_prefix |
| 110 | */ |
| 111 | static const char * |
| 112 | xml_print_get_prefix(const struct lys_module *mod, void *private) |
| 113 | { |
| 114 | struct ly_set *ns_list = (struct ly_set*)private; |
| 115 | |
| 116 | ly_set_add(ns_list, (void*)mod, 0); |
| 117 | return mod->prefix; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * TODO |
| 122 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 123 | static void |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 124 | xml_print_meta(struct xmlpr_ctx *ctx, const struct lyd_node *node) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 125 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 126 | struct lyd_meta *meta; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 127 | const struct lys_module *mod; |
| 128 | struct ly_set ns_list = {0}; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 129 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 130 | const char **prefs, **nss; |
| 131 | const char *xml_expr = NULL, *mod_name; |
| 132 | uint32_t ns_count, i; |
| 133 | int rpc_filter = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 134 | char *p; |
| 135 | size_t len; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 136 | #endif |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 137 | int dynamic; |
| 138 | unsigned int u; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 139 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 140 | /* with-defaults */ |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 141 | if (node->schema->nodetype & LYD_NODE_TERM) { |
| 142 | if (((node->flags & LYD_DEFAULT) && (ctx->options & (LYDP_WD_ALL_TAG | LYDP_WD_IMPL_TAG))) || |
| 143 | ((ctx->options & LYDP_WD_ALL_TAG) && ly_is_default(node))) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 144 | /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */ |
| 145 | mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults"); |
| 146 | if (mod) { |
| 147 | ly_print(ctx->out, " %s:default=\"true\"", xml_print_ns(ctx, mod->ns, mod->prefix, 0)); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 151 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 152 | /* technically, check for the extension get-filter-element-attributes from ietf-netconf */ |
| 153 | if (!strcmp(node->schema->name, "filter") |
| 154 | && (!strcmp(node->schema->module->name, "ietf-netconf") || !strcmp(node->schema->module->name, "notifications"))) { |
| 155 | rpc_filter = 1; |
| 156 | } |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 157 | #endif |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 158 | for (meta = node->meta; meta; meta = meta->next) { |
| 159 | const char *value = meta->value.realtype->plugin->print(&meta->value, LYD_XML, xml_print_get_prefix, &ns_list, &dynamic); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 160 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 161 | /* print namespaces connected with the value's prefixes */ |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 162 | for (u = 0; u < ns_list.count; ++u) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 163 | mod = (const struct lys_module *)ns_list.objs[u]; |
| 164 | xml_print_ns(ctx, mod->ns, mod->prefix, 1); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 165 | } |
| 166 | ly_set_erase(&ns_list, NULL); |
| 167 | |
| 168 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 169 | if (rpc_filter) { |
| 170 | /* exception for NETCONF's filter's attributes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 171 | if (!strcmp(meta->name, "select")) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 172 | /* xpath content, we have to convert the JSON format into XML first */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 173 | xml_expr = transform_json2xml(node->schema->module, meta->value_str, 0, &prefs, &nss, &ns_count); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 174 | if (!xml_expr) { |
| 175 | /* error */ |
| 176 | return EXIT_FAILURE; |
| 177 | } |
| 178 | |
| 179 | for (i = 0; i < ns_count; ++i) { |
| 180 | ly_print(out, " xmlns:%s=\"%s\"", prefs[i], nss[i]); |
| 181 | } |
| 182 | free(prefs); |
| 183 | free(nss); |
| 184 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 185 | ly_print(out, " %s=\"", meta->name); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 186 | } else { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 187 | #endif |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 188 | /* print the metadata with its namespace */ |
| 189 | mod = meta->annotation->module; |
| 190 | ly_print(ctx->out, " %s:%s=\"", xml_print_ns(ctx, mod->ns, mod->prefix, 1), meta->name); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 191 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 192 | } |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 193 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 194 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 195 | /* print metadata value */ |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 196 | if (value && value[0]) { |
| 197 | lyxml_dump_text(ctx->out, value, 1); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 198 | } |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 199 | ly_print(ctx->out, "\""); |
| 200 | if (dynamic) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 201 | free((void *)value); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 202 | } |
| 203 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @brief Print generic XML element despite of the data node type. |
| 208 | * |
| 209 | * Prints the element name, attributes and necessary namespaces. |
| 210 | * |
| 211 | * @param[in] ctx XML printer context. |
| 212 | * @param[in] node Data node to be printed. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 213 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 214 | static void |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 215 | xml_print_node_open(struct xmlpr_ctx *ctx, const struct lyd_node *node) |
| 216 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 217 | /* print node name */ |
| 218 | ly_print(ctx->out, "%*s<%s", INDENT, node->schema->name); |
| 219 | |
| 220 | /* print default namespace */ |
| 221 | xml_print_ns(ctx, node->schema->module->ns, NULL, 0); |
| 222 | |
| 223 | /* print metadata */ |
| 224 | xml_print_meta(ctx, node); |
| 225 | } |
| 226 | |
| 227 | static LY_ERR |
| 228 | xml_print_attr(struct xmlpr_ctx *ctx, const struct lyd_node_opaq *node) |
| 229 | { |
| 230 | const struct ly_attr *attr; |
| 231 | const char *pref; |
| 232 | uint32_t u; |
| 233 | |
| 234 | LY_LIST_FOR(node->attr, attr) { |
| 235 | pref = NULL; |
| 236 | if (attr->prefix.pref) { |
| 237 | /* print attribute namespace */ |
| 238 | switch (attr->format) { |
| 239 | case LYD_XML: |
| 240 | pref = xml_print_ns(ctx, attr->prefix.ns, attr->prefix.pref, 0); |
| 241 | break; |
| 242 | case LYD_SCHEMA: |
| 243 | /* cannot be created */ |
| 244 | LOGINT(node->ctx); |
| 245 | return LY_EINT; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* print namespaces connected with the value's prefixes */ |
| 250 | if (attr->val_prefs) { |
| 251 | LY_ARRAY_FOR(attr->val_prefs, u) { |
| 252 | xml_print_ns(ctx, attr->val_prefs[u].ns, attr->val_prefs[u].pref, LYXML_PREFIX_REQUIRED); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* print the attribute with its prefix and value */ |
| 257 | ly_print(ctx->out, " %s%s%s=\"%s\"", pref ? pref : "", pref ? ":" : "", attr->name, attr->value); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 260 | return LY_SUCCESS; |
| 261 | } |
| 262 | |
| 263 | static LY_ERR |
| 264 | xml_print_opaq_open(struct xmlpr_ctx *ctx, const struct lyd_node_opaq *node) |
| 265 | { |
| 266 | /* print node name */ |
| 267 | ly_print(ctx->out, "%*s<%s", INDENT, node->name); |
| 268 | |
| 269 | /* print default namespace */ |
| 270 | switch (node->format) { |
| 271 | case LYD_XML: |
| 272 | xml_print_ns(ctx, node->prefix.ns, NULL, 0); |
| 273 | break; |
| 274 | case LYD_SCHEMA: |
| 275 | /* cannot be created */ |
| 276 | LOGINT(node->ctx); |
| 277 | return LY_EINT; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 280 | /* print attributes */ |
| 281 | LY_CHECK_RET(xml_print_attr(ctx, node)); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 282 | |
| 283 | return LY_SUCCESS; |
| 284 | } |
| 285 | |
| 286 | static LY_ERR xml_print_node(struct xmlpr_ctx *ctx, const struct lyd_node *node); |
| 287 | |
| 288 | /** |
| 289 | * @brief Print XML element representing lyd_node_term. |
| 290 | * |
| 291 | * @param[in] ctx XML printer context. |
| 292 | * @param[in] node Data node to be printed. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 293 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 294 | static void |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 295 | xml_print_term(struct xmlpr_ctx *ctx, const struct lyd_node_term *node) |
| 296 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 297 | struct ly_set ns_list = {0}; |
| 298 | unsigned int u; |
| 299 | int dynamic; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 300 | const char *value; |
| 301 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 302 | xml_print_node_open(ctx, (struct lyd_node *)node); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 303 | value = ((struct lysc_node_leaf *)node->schema)->type->plugin->print(&node->value, LYD_XML, xml_print_get_prefix, &ns_list, &dynamic); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 304 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 305 | /* print namespaces connected with the values's prefixes */ |
| 306 | for (u = 0; u < ns_list.count; ++u) { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 307 | const struct lys_module *mod = (const struct lys_module *)ns_list.objs[u]; |
| 308 | ly_print(ctx->out, " xmlns:%s=\"%s\"", mod->prefix, mod->ns); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 309 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 310 | ly_set_erase(&ns_list, NULL); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 311 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 312 | if (!value || !value[0]) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 313 | ly_print(ctx->out, "/>%s", LEVEL ? "\n" : ""); |
| 314 | } else { |
| 315 | ly_print(ctx->out, ">"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 316 | lyxml_dump_text(ctx->out, value, 0); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 317 | ly_print(ctx->out, "</%s>%s", node->schema->name, LEVEL ? "\n" : ""); |
| 318 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 319 | if (dynamic) { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 320 | free((void *)value); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 321 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
| 325 | * @brief Print XML element representing lyd_node_inner. |
| 326 | * |
| 327 | * @param[in] ctx XML printer context. |
| 328 | * @param[in] node Data node to be printed. |
| 329 | * @return LY_ERR value. |
| 330 | */ |
| 331 | static LY_ERR |
| 332 | xml_print_inner(struct xmlpr_ctx *ctx, const struct lyd_node_inner *node) |
| 333 | { |
| 334 | LY_ERR ret; |
| 335 | struct lyd_node *child; |
| 336 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 337 | xml_print_node_open(ctx, (struct lyd_node *)node); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 338 | |
| 339 | if (!node->child) { |
| 340 | ly_print(ctx->out, "/>%s", ctx->level ? "\n" : ""); |
| 341 | return LY_SUCCESS; |
| 342 | } |
| 343 | |
| 344 | /* children */ |
| 345 | ly_print(ctx->out, ">%s", ctx->level ? "\n" : ""); |
| 346 | |
| 347 | LEVEL_INC; |
| 348 | LY_LIST_FOR(node->child, child) { |
| 349 | ret = xml_print_node(ctx, child); |
| 350 | LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret); |
| 351 | } |
| 352 | LEVEL_DEC; |
| 353 | |
| 354 | ly_print(ctx->out, "%*s</%s>%s", INDENT, node->schema->name, LEVEL ? "\n" : ""); |
| 355 | |
| 356 | return LY_SUCCESS; |
| 357 | } |
| 358 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 359 | static LY_ERR |
| 360 | xml_print_anydata(struct xmlpr_ctx *ctx, const struct lyd_node_any *node) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 361 | { |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 362 | struct lyd_node_any *any = (struct lyd_node_any *)node; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 363 | struct lyd_node *iter; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 364 | int prev_opts; |
| 365 | LY_ERR ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 366 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 367 | xml_print_node_open(ctx, (struct lyd_node *)node); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 368 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 369 | if (!any->value.tree) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 370 | /* no content */ |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 371 | no_content: |
| 372 | ly_print(ctx->out, "/>%s", LEVEL ? "\n" : ""); |
| 373 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 374 | } else { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 375 | switch (any->value_type) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 376 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 377 | /* close opening tag and print data */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 378 | prev_opts = ctx->options; |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 379 | ctx->options &= ~LYDP_WITHSIBLINGS; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 380 | LEVEL_INC; |
| 381 | |
| 382 | ly_print(ctx->out, ">%s", LEVEL ? "\n" : ""); |
| 383 | LY_LIST_FOR(any->value.tree, iter) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 384 | ret = xml_print_node(ctx, iter); |
| 385 | LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 386 | } |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 387 | |
| 388 | LEVEL_DEC; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 389 | ctx->options = prev_opts; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 390 | break; |
| 391 | case LYD_ANYDATA_STRING: |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 392 | /* escape XML-sensitive characters */ |
| 393 | if (!any->value.str[0]) { |
| 394 | goto no_content; |
| 395 | } |
| 396 | /* close opening tag and print data */ |
| 397 | ly_print(ctx->out, ">"); |
| 398 | lyxml_dump_text(ctx->out, any->value.str, 0); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 399 | break; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 400 | case LYD_ANYDATA_XML: |
| 401 | /* print without escaping special characters */ |
| 402 | if (!any->value.str[0]) { |
| 403 | goto no_content; |
| 404 | } |
| 405 | ly_print(ctx->out, ">%s", any->value.str); |
| 406 | break; |
| 407 | case LYD_ANYDATA_JSON: |
| 408 | #if 0 /* TODO LYB format */ |
| 409 | case LYD_ANYDATA_LYB: |
| 410 | #endif |
| 411 | /* JSON and LYB format is not supported */ |
| 412 | LOGWRN(node->schema->module->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type); |
| 413 | goto no_content; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* closing tag */ |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 417 | if (any->value_type == LYD_ANYDATA_DATATREE) { |
| 418 | ly_print(ctx->out, "%*s</%s>%s", INDENT, node->schema->name, LEVEL ? "\n" : ""); |
| 419 | } else { |
| 420 | ly_print(ctx->out, "</%s>%s", node->schema->name, LEVEL ? "\n" : ""); |
| 421 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 424 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 425 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 426 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 427 | static LY_ERR |
| 428 | xml_print_opaq(struct xmlpr_ctx *ctx, const struct lyd_node_opaq *node) |
| 429 | { |
| 430 | LY_ERR ret; |
| 431 | struct lyd_node *child; |
| 432 | uint32_t u; |
| 433 | |
| 434 | LY_CHECK_RET(xml_print_opaq_open(ctx, node)); |
| 435 | |
| 436 | if (node->value[0]) { |
| 437 | /* print namespaces connected with the value's prefixes */ |
| 438 | if (node->val_prefs) { |
| 439 | LY_ARRAY_FOR(node->val_prefs, u) { |
| 440 | xml_print_ns(ctx, node->val_prefs[u].ns, node->val_prefs[u].pref, LYXML_PREFIX_REQUIRED); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | ly_print(ctx->out, ">%s", node->value); |
| 445 | } |
| 446 | |
| 447 | if (node->child) { |
| 448 | /* children */ |
| 449 | if (!node->value[0]) { |
| 450 | ly_print(ctx->out, ">%s", ctx->level ? "\n" : ""); |
| 451 | } |
| 452 | |
| 453 | LEVEL_INC; |
| 454 | LY_LIST_FOR(node->child, child) { |
| 455 | ret = xml_print_node(ctx, child); |
| 456 | LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret); |
| 457 | } |
| 458 | LEVEL_DEC; |
| 459 | |
| 460 | ly_print(ctx->out, "%*s</%s>%s", INDENT, node->name, LEVEL ? "\n" : ""); |
| 461 | } else if (node->value[0]) { |
| 462 | ly_print(ctx->out, "</%s>%s", node->name, LEVEL ? "\n" : ""); |
| 463 | } else { |
| 464 | /* no value or children */ |
| 465 | ly_print(ctx->out, "/>%s", ctx->level ? "\n" : ""); |
| 466 | } |
| 467 | |
| 468 | return LY_SUCCESS; |
| 469 | } |
| 470 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 471 | /** |
| 472 | * @brief Print XML element representing lyd_node. |
| 473 | * |
| 474 | * @param[in] ctx XML printer context. |
| 475 | * @param[in] node Data node to be printed. |
| 476 | * @return LY_ERR value. |
| 477 | */ |
| 478 | static LY_ERR |
| 479 | xml_print_node(struct xmlpr_ctx *ctx, const struct lyd_node *node) |
| 480 | { |
| 481 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 482 | uint32_t ns_count; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 483 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 484 | if (!ly_should_print(node, ctx->options)) { |
| 485 | /* do not print at all */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 486 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 487 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 488 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 489 | /* remember namespace definition count on this level */ |
| 490 | ns_count = ctx->ns.count; |
| 491 | |
| 492 | if (!node->schema) { |
| 493 | ret = xml_print_opaq(ctx, (const struct lyd_node_opaq *)node); |
| 494 | } else { |
| 495 | switch (node->schema->nodetype) { |
| 496 | case LYS_CONTAINER: |
| 497 | case LYS_LIST: |
| 498 | case LYS_NOTIF: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 499 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 500 | case LYS_ACTION: |
| 501 | ret = xml_print_inner(ctx, (const struct lyd_node_inner *)node); |
| 502 | break; |
| 503 | case LYS_LEAF: |
| 504 | case LYS_LEAFLIST: |
| 505 | xml_print_term(ctx, (const struct lyd_node_term *)node); |
| 506 | break; |
| 507 | case LYS_ANYXML: |
| 508 | case LYS_ANYDATA: |
| 509 | ret = xml_print_anydata(ctx, (const struct lyd_node_any *)node); |
| 510 | break; |
| 511 | default: |
| 512 | LOGINT(node->schema->module->ctx); |
| 513 | ret = LY_EINT; |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | /* remove all added namespaces */ |
| 519 | while (ns_count < ctx->ns.count) { |
| 520 | FREE_STRING(ctx->ctx, ctx->prefix.objs[ctx->prefix.count - 1]); |
| 521 | ly_set_rm_index(&ctx->prefix, ctx->prefix.count - 1, NULL); |
| 522 | ly_set_rm_index(&ctx->ns, ctx->ns.count - 1, NULL); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | return ret; |
| 526 | } |
| 527 | |
| 528 | LY_ERR |
| 529 | xml_print_data(struct lyout *out, const struct lyd_node *root, int options) |
| 530 | { |
| 531 | const struct lyd_node *node; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 532 | struct xmlpr_ctx ctx = {0}; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 533 | |
| 534 | if (!root) { |
| 535 | if (out->type == LYOUT_MEMORY || out->type == LYOUT_CALLBACK) { |
| 536 | ly_print(out, ""); |
| 537 | } |
| 538 | goto finish; |
| 539 | } |
| 540 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 541 | ctx.out = out; |
| 542 | ctx.level = (options & LYDP_FORMAT ? 1 : 0); |
| 543 | ctx.options = options; |
| 544 | ctx.ctx = LYD_NODE_CTX(root); |
| 545 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 546 | /* content */ |
| 547 | LY_LIST_FOR(root, node) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 548 | LY_CHECK_RET(xml_print_node(&ctx, node)); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 549 | if (!(options & LYDP_WITHSIBLINGS)) { |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | finish: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 555 | assert(!ctx.prefix.count && !ctx.ns.count); |
| 556 | ly_set_erase(&ctx.prefix, NULL); |
| 557 | ly_set_erase(&ctx.ns, NULL); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 558 | ly_print_flush(out); |
| 559 | return LY_SUCCESS; |
| 560 | } |
| 561 | |