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