blob: af60e7a9536811bce0c1d50d0264cb36452fe887 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
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 Krejci535ea9f2020-05-29 16:01:05 +020016#include <assert.h>
17#include <stdint.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <stdlib.h>
19#include <string.h>
20
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "common.h"
22#include "context.h"
23#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010025#include "out.h"
26#include "out_internal.h"
Radek Krejci7931b192020-06-25 17:05:03 +020027#include "parser_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "plugins_types.h"
29#include "printer_data.h"
30#include "printer_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020032#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree_schema.h"
35#include "xml.h"
36
37/**
38 * @brief XML printer context.
39 */
40struct xmlpr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020041 struct ly_out *out; /**< output specification */
42 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
43 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
Michal Vasko52927e22020-03-16 17:26:14 +010044 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci1deb5be2020-08-26 16:43:36 +020045 struct ly_set prefix; /**< printed namespace prefixes */
46 struct ly_set ns; /**< printed namespaces */
Radek Krejcie7b95092019-05-15 11:03:07 +020047};
48
Michal Vasko52927e22020-03-16 17:26:14 +010049#define LYXML_PREFIX_REQUIRED 0x01 /**< The prefix is not just a suggestion but a requirement. */
Radek Krejci1798aae2020-07-14 13:26:06 +020050#define LYXML_PREFIX_DEFAULT 0x02 /**< The namespace is required to be a default (without prefix) */
Radek Krejcie7b95092019-05-15 11:03:07 +020051
52/**
Michal Vasko52927e22020-03-16 17:26:14 +010053 * @brief Print a namespace if not already printed.
54 *
55 * @param[in] ctx XML printer context.
56 * @param[in] ns Namespace to print, expected to be in dictionary.
57 * @param[in] new_prefix Suggested new prefix, NULL for a default namespace without prefix. Stored in the dictionary.
58 * @param[in] prefix_opts Prefix options changing the meaning of parameters.
59 * @return Printed prefix of the namespace to use.
Radek Krejcie7b95092019-05-15 11:03:07 +020060 */
Michal Vasko52927e22020-03-16 17:26:14 +010061static const char *
Radek Krejci1deb5be2020-08-26 16:43:36 +020062xml_print_ns(struct xmlpr_ctx *ctx, const char *ns, const char *new_prefix, uint32_t prefix_opts)
Radek Krejcie7b95092019-05-15 11:03:07 +020063{
Radek Krejciba03a5a2020-08-27 14:40:41 +020064 uint32_t i;
Michal Vasko6f4cbb62020-02-28 11:15:47 +010065
Radek Krejciba03a5a2020-08-27 14:40:41 +020066 for (i = ctx->ns.count; i > 0; --i) {
Michal Vasko52927e22020-03-16 17:26:14 +010067 if (!new_prefix) {
68 /* find default namespace */
Radek Krejciba03a5a2020-08-27 14:40:41 +020069 if (!ctx->prefix.objs[i - 1]) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010070 if (!strcmp(ctx->ns.objs[i - 1], ns)) {
Radek Krejciba03a5a2020-08-27 14:40:41 +020071 /* matching default namespace */
72 return ctx->prefix.objs[i - 1];
Radek Krejcie7b95092019-05-15 11:03:07 +020073 }
Radek Krejciba03a5a2020-08-27 14:40:41 +020074 /* not matching default namespace */
Michal Vasko52927e22020-03-16 17:26:14 +010075 break;
76 }
77 } else {
78 /* find prefixed namespace */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010079 if (!strcmp(ctx->ns.objs[i - 1], ns)) {
Radek Krejciba03a5a2020-08-27 14:40:41 +020080 if (!ctx->prefix.objs[i - 1]) {
Michal Vasko52927e22020-03-16 17:26:14 +010081 /* default namespace is not interesting */
82 continue;
83 }
84
Radek Krejciba03a5a2020-08-27 14:40:41 +020085 if (!strcmp(ctx->prefix.objs[i - 1], new_prefix) || !(prefix_opts & LYXML_PREFIX_REQUIRED)) {
Michal Vasko52927e22020-03-16 17:26:14 +010086 /* the same prefix or can be any */
Radek Krejciba03a5a2020-08-27 14:40:41 +020087 return ctx->prefix.objs[i - 1];
Michal Vasko52927e22020-03-16 17:26:14 +010088 }
89 }
Radek Krejcie7b95092019-05-15 11:03:07 +020090 }
Radek Krejcie7b95092019-05-15 11:03:07 +020091 }
Radek Krejci28681fa2019-09-06 13:08:45 +020092
Radek Krejciba03a5a2020-08-27 14:40:41 +020093 /* suitable namespace not found, must be printed */
94 ly_print_(ctx->out, " xmlns%s%s=\"%s\"", new_prefix ? ":" : "", new_prefix ? new_prefix : "", ns);
Radek Krejcie7b95092019-05-15 11:03:07 +020095
Radek Krejciba03a5a2020-08-27 14:40:41 +020096 /* and added into namespaces */
97 if (new_prefix) {
Radek Krejci011e4aa2020-09-04 15:22:31 +020098 LY_CHECK_RET(lydict_insert(ctx->ctx, new_prefix, 0, &new_prefix), NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +020099 }
Radek Krejci3d92e442020-10-12 12:48:13 +0200100 LY_CHECK_RET(ly_set_add(&ctx->prefix, (void *)new_prefix, 1, NULL), NULL);
101 LY_CHECK_RET(ly_set_add(&ctx->ns, (void *)ns, 1, &i), NULL);
Michal Vasko52927e22020-03-16 17:26:14 +0100102
103 /* return it */
104 return ctx->prefix.objs[i];
Radek Krejci28681fa2019-09-06 13:08:45 +0200105}
106
Michal Vasko22df3f02020-08-24 13:29:22 +0200107static const char *
Radek Krejci8df109d2021-04-23 12:19:08 +0200108xml_print_ns_opaq(struct xmlpr_ctx *ctx, LY_VALUE_FORMAT format, const struct ly_opaq_name *name, uint32_t prefix_opts)
Radek Krejci1798aae2020-07-14 13:26:06 +0200109{
Radek Krejci1798aae2020-07-14 13:26:06 +0200110 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200111 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +0100112 return xml_print_ns(ctx, name->module_ns, (prefix_opts & LYXML_PREFIX_DEFAULT) ? NULL : name->prefix, prefix_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200113 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200114 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100115 if (name->module_name) {
116 const struct lys_module *mod = ly_ctx_get_module_latest(ctx->ctx, name->module_name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200117 if (mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100118 return xml_print_ns(ctx, mod->ns, (prefix_opts & LYXML_PREFIX_DEFAULT) ? NULL : name->prefix, prefix_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200119 }
120 }
121 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100122 default:
Radek Krejci1798aae2020-07-14 13:26:06 +0200123 /* cannot be created */
124 LOGINT(ctx->ctx);
125 }
126
127 return NULL;
128}
129
Radek Krejciec9ad602021-01-04 10:46:30 +0100130/**
131 * @brief Print prefix data.
132 *
133 * @param[in] ctx XML printer context.
Radek Krejci8df109d2021-04-23 12:19:08 +0200134 * @param[in] format Value prefix format, only ::LY_VALUE_XML supported.
135 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Radek Krejciec9ad602021-01-04 10:46:30 +0100136 * @param[in] prefix_opts Prefix options changing the meaning of parameters.
137 * @return LY_ERR value.
138 */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100139static void
Radek Krejci8df109d2021-04-23 12:19:08 +0200140xml_print_ns_prefix_data(struct xmlpr_ctx *ctx, LY_VALUE_FORMAT format, void *prefix_data, uint32_t prefix_opts)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100141{
142 const struct ly_set *set;
143 const struct lyxml_ns *ns;
144 uint32_t i;
145
146 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200147 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100148 set = prefix_data;
149 for (i = 0; i < set->count; ++i) {
150 ns = set->objs[i];
151 xml_print_ns(ctx, ns->uri, (prefix_opts & LYXML_PREFIX_DEFAULT) ? NULL : ns->prefix, prefix_opts);
152 }
153 break;
154 default:
155 /* cannot be created */
156 LOGINT(ctx->ctx);
157 }
158}
159
Radek Krejci28681fa2019-09-06 13:08:45 +0200160/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200161 * TODO
162 */
Michal Vasko52927e22020-03-16 17:26:14 +0100163static void
Michal Vasko9f96a052020-03-10 09:41:45 +0100164xml_print_meta(struct xmlpr_ctx *ctx, const struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200165{
Michal Vasko9f96a052020-03-10 09:41:45 +0100166 struct lyd_meta *meta;
Michal Vasko52927e22020-03-16 17:26:14 +0100167 const struct lys_module *mod;
168 struct ly_set ns_list = {0};
Michal Vasko69730152020-10-09 16:30:07 +0200169
Radek Krejci28681fa2019-09-06 13:08:45 +0200170#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200171 const char **prefs, **nss;
172 const char *xml_expr = NULL, *mod_name;
173 uint32_t ns_count, i;
Radek Krejci857189e2020-09-01 13:26:36 +0200174 ly_bool rpc_filter = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200175 char *p;
176 size_t len;
Radek Krejci28681fa2019-09-06 13:08:45 +0200177#endif
Radek Krejci857189e2020-09-01 13:26:36 +0200178 ly_bool dynamic;
Radek Krejcie7b95092019-05-15 11:03:07 +0200179
Radek Krejcie7b95092019-05-15 11:03:07 +0200180 /* with-defaults */
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100181 if (node->schema->nodetype & LYD_NODE_TERM) {
Radek Krejci7931b192020-06-25 17:05:03 +0200182 if (((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) ||
Radek Krejci19611252020-10-04 13:54:53 +0200183 ((ctx->options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko52927e22020-03-16 17:26:14 +0100184 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
185 mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults");
186 if (mod) {
Michal Vasko5233e962020-08-14 14:26:20 +0200187 ly_print_(ctx->out, " %s:default=\"true\"", xml_print_ns(ctx, mod->ns, mod->prefix, 0));
Radek Krejcie7b95092019-05-15 11:03:07 +0200188 }
189 }
190 }
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100191#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200192 /* technically, check for the extension get-filter-element-attributes from ietf-netconf */
Michal Vasko69730152020-10-09 16:30:07 +0200193 if (!strcmp(node->schema->name, "filter") &&
194 (!strcmp(node->schema->module->name, "ietf-netconf") || !strcmp(node->schema->module->name, "notifications"))) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200195 rpc_filter = 1;
196 }
Radek Krejci28681fa2019-09-06 13:08:45 +0200197#endif
Michal Vasko9f96a052020-03-10 09:41:45 +0100198 for (meta = node->meta; meta; meta = meta->next) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200199 const char *value = meta->value.realtype->plugin->print(&meta->value, LY_VALUE_XML, &ns_list, &dynamic);
Radek Krejci28681fa2019-09-06 13:08:45 +0200200
Michal Vasko52927e22020-03-16 17:26:14 +0100201 /* print namespaces connected with the value's prefixes */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200202 for (uint32_t u = 0; u < ns_list.count; ++u) {
Michal Vasko52927e22020-03-16 17:26:14 +0100203 mod = (const struct lys_module *)ns_list.objs[u];
204 xml_print_ns(ctx, mod->ns, mod->prefix, 1);
Radek Krejci28681fa2019-09-06 13:08:45 +0200205 }
206 ly_set_erase(&ns_list, NULL);
207
208#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200209 if (rpc_filter) {
210 /* exception for NETCONF's filter's attributes */
Michal Vasko9f96a052020-03-10 09:41:45 +0100211 if (!strcmp(meta->name, "select")) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200212 /* xpath content, we have to convert the JSON format into XML first */
Michal Vasko9f96a052020-03-10 09:41:45 +0100213 xml_expr = transform_json2xml(node->schema->module, meta->value_str, 0, &prefs, &nss, &ns_count);
Radek Krejcie7b95092019-05-15 11:03:07 +0200214 if (!xml_expr) {
215 /* error */
216 return EXIT_FAILURE;
217 }
218
219 for (i = 0; i < ns_count; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +0200220 ly_print_(out, " xmlns:%s=\"%s\"", prefs[i], nss[i]);
Radek Krejcie7b95092019-05-15 11:03:07 +0200221 }
222 free(prefs);
223 free(nss);
224 }
Michal Vasko5233e962020-08-14 14:26:20 +0200225 ly_print_(out, " %s=\"", meta->name);
Radek Krejcie7b95092019-05-15 11:03:07 +0200226 } else {
Radek Krejci28681fa2019-09-06 13:08:45 +0200227#endif
Radek Krejci0f969882020-08-21 16:56:47 +0200228 /* print the metadata with its namespace */
229 mod = meta->annotation->module;
230 ly_print_(ctx->out, " %s:%s=\"", xml_print_ns(ctx, mod->ns, mod->prefix, 1), meta->name);
Radek Krejci28681fa2019-09-06 13:08:45 +0200231#if 0
Radek Krejci0f969882020-08-21 16:56:47 +0200232 }
Radek Krejci28681fa2019-09-06 13:08:45 +0200233#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200234
Michal Vasko52927e22020-03-16 17:26:14 +0100235 /* print metadata value */
Radek Krejci28681fa2019-09-06 13:08:45 +0200236 if (value && value[0]) {
237 lyxml_dump_text(ctx->out, value, 1);
Radek Krejcie7b95092019-05-15 11:03:07 +0200238 }
Michal Vasko5233e962020-08-14 14:26:20 +0200239 ly_print_(ctx->out, "\"");
Radek Krejci28681fa2019-09-06 13:08:45 +0200240 if (dynamic) {
Michal Vasko52927e22020-03-16 17:26:14 +0100241 free((void *)value);
Radek Krejcie7b95092019-05-15 11:03:07 +0200242 }
243 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200244}
245
246/**
247 * @brief Print generic XML element despite of the data node type.
248 *
249 * Prints the element name, attributes and necessary namespaces.
250 *
251 * @param[in] ctx XML printer context.
252 * @param[in] node Data node to be printed.
Radek Krejcie7b95092019-05-15 11:03:07 +0200253 */
Michal Vasko52927e22020-03-16 17:26:14 +0100254static void
Radek Krejcie7b95092019-05-15 11:03:07 +0200255xml_print_node_open(struct xmlpr_ctx *ctx, const struct lyd_node *node)
256{
Michal Vasko52927e22020-03-16 17:26:14 +0100257 /* print node name */
Michal Vasko5233e962020-08-14 14:26:20 +0200258 ly_print_(ctx->out, "%*s<%s", INDENT, node->schema->name);
Michal Vasko52927e22020-03-16 17:26:14 +0100259
260 /* print default namespace */
261 xml_print_ns(ctx, node->schema->module->ns, NULL, 0);
262
263 /* print metadata */
264 xml_print_meta(ctx, node);
265}
266
267static LY_ERR
268xml_print_attr(struct xmlpr_ctx *ctx, const struct lyd_node_opaq *node)
269{
Radek Krejci5536d282020-08-04 23:27:44 +0200270 const struct lyd_attr *attr;
Michal Vasko52927e22020-03-16 17:26:14 +0100271 const char *pref;
Michal Vasko52927e22020-03-16 17:26:14 +0100272
273 LY_LIST_FOR(node->attr, attr) {
274 pref = NULL;
Michal Vaskoad92b672020-11-12 13:11:31 +0100275 if (attr->name.prefix) {
Michal Vasko52927e22020-03-16 17:26:14 +0100276 /* print attribute namespace */
Michal Vaskoad92b672020-11-12 13:11:31 +0100277 pref = xml_print_ns_opaq(ctx, attr->format, &attr->name, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100278 }
279
280 /* print namespaces connected with the value's prefixes */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100281 if (attr->val_prefix_data) {
282 xml_print_ns_prefix_data(ctx, attr->format, attr->val_prefix_data, LYXML_PREFIX_REQUIRED);
Michal Vasko52927e22020-03-16 17:26:14 +0100283 }
284
285 /* print the attribute with its prefix and value */
Radek IÅ¡a52c4ac62021-03-08 09:37:32 +0100286 ly_print_(ctx->out, " %s%s%s=\"", pref ? pref : "", pref ? ":" : "", attr->name.name);
287 lyxml_dump_text(ctx->out, attr->value, 1);
288 ly_print_(ctx->out, "\""); /* print attribute value terminator */
289
Radek Krejcie7b95092019-05-15 11:03:07 +0200290 }
291
Michal Vasko52927e22020-03-16 17:26:14 +0100292 return LY_SUCCESS;
293}
294
295static LY_ERR
296xml_print_opaq_open(struct xmlpr_ctx *ctx, const struct lyd_node_opaq *node)
297{
298 /* print node name */
Michal Vaskoad92b672020-11-12 13:11:31 +0100299 ly_print_(ctx->out, "%*s<%s", INDENT, node->name.name);
Michal Vasko52927e22020-03-16 17:26:14 +0100300
301 /* print default namespace */
Michal Vaskoad92b672020-11-12 13:11:31 +0100302 xml_print_ns_opaq(ctx, node->format, &node->name, LYXML_PREFIX_DEFAULT);
Radek Krejcie7b95092019-05-15 11:03:07 +0200303
Michal Vasko52927e22020-03-16 17:26:14 +0100304 /* print attributes */
305 LY_CHECK_RET(xml_print_attr(ctx, node));
Radek Krejcie7b95092019-05-15 11:03:07 +0200306
307 return LY_SUCCESS;
308}
309
310static LY_ERR xml_print_node(struct xmlpr_ctx *ctx, const struct lyd_node *node);
311
312/**
313 * @brief Print XML element representing lyd_node_term.
314 *
315 * @param[in] ctx XML printer context.
316 * @param[in] node Data node to be printed.
Radek Krejcie7b95092019-05-15 11:03:07 +0200317 */
Michal Vasko52927e22020-03-16 17:26:14 +0100318static void
Radek Krejcie7b95092019-05-15 11:03:07 +0200319xml_print_term(struct xmlpr_ctx *ctx, const struct lyd_node_term *node)
320{
Radek Krejcia1911222019-07-22 17:24:50 +0200321 struct ly_set ns_list = {0};
Radek Krejci857189e2020-09-01 13:26:36 +0200322 ly_bool dynamic;
Radek Krejci28681fa2019-09-06 13:08:45 +0200323 const char *value;
324
Michal Vasko9e685082021-01-29 14:49:09 +0100325 xml_print_node_open(ctx, &node->node);
Radek Krejci8df109d2021-04-23 12:19:08 +0200326 value = ((struct lysc_node_leaf *)node->schema)->type->plugin->print(&node->value, LY_VALUE_XML, &ns_list, &dynamic);
Radek Krejcie7b95092019-05-15 11:03:07 +0200327
Radek Krejcia1911222019-07-22 17:24:50 +0200328 /* print namespaces connected with the values's prefixes */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200329 for (uint32_t u = 0; u < ns_list.count; ++u) {
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100330 const struct lys_module *mod = (const struct lys_module *)ns_list.objs[u];
Michal Vasko5233e962020-08-14 14:26:20 +0200331 ly_print_(ctx->out, " xmlns:%s=\"%s\"", mod->prefix, mod->ns);
Radek Krejcie7b95092019-05-15 11:03:07 +0200332 }
Radek Krejcia1911222019-07-22 17:24:50 +0200333 ly_set_erase(&ns_list, NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200334
Radek Krejcia1911222019-07-22 17:24:50 +0200335 if (!value || !value[0]) {
Michal Vasko5233e962020-08-14 14:26:20 +0200336 ly_print_(ctx->out, "/>%s", DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200337 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200338 ly_print_(ctx->out, ">");
Radek Krejcia1911222019-07-22 17:24:50 +0200339 lyxml_dump_text(ctx->out, value, 0);
Michal Vasko5233e962020-08-14 14:26:20 +0200340 ly_print_(ctx->out, "</%s>%s", node->schema->name, DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200341 }
Radek Krejcia1911222019-07-22 17:24:50 +0200342 if (dynamic) {
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100343 free((void *)value);
Radek Krejcia1911222019-07-22 17:24:50 +0200344 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200345}
346
347/**
348 * @brief Print XML element representing lyd_node_inner.
349 *
350 * @param[in] ctx XML printer context.
351 * @param[in] node Data node to be printed.
352 * @return LY_ERR value.
353 */
354static LY_ERR
355xml_print_inner(struct xmlpr_ctx *ctx, const struct lyd_node_inner *node)
356{
357 LY_ERR ret;
358 struct lyd_node *child;
359
Michal Vasko9e685082021-01-29 14:49:09 +0100360 xml_print_node_open(ctx, &node->node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200361
Michal Vasko630d9892020-12-08 17:11:08 +0100362 LY_LIST_FOR(node->child, child) {
363 if (ly_should_print(child, ctx->options)) {
364 break;
365 }
366 }
367 if (!child) {
368 /* there are no children that will be printed */
Michal Vasko5233e962020-08-14 14:26:20 +0200369 ly_print_(ctx->out, "/>%s", DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200370 return LY_SUCCESS;
371 }
372
373 /* children */
Michal Vasko5233e962020-08-14 14:26:20 +0200374 ly_print_(ctx->out, ">%s", DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200375
376 LEVEL_INC;
377 LY_LIST_FOR(node->child, child) {
378 ret = xml_print_node(ctx, child);
379 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
380 }
381 LEVEL_DEC;
382
Michal Vasko5233e962020-08-14 14:26:20 +0200383 ly_print_(ctx->out, "%*s</%s>%s", INDENT, node->schema->name, DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200384
385 return LY_SUCCESS;
386}
387
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200388static LY_ERR
389xml_print_anydata(struct xmlpr_ctx *ctx, const struct lyd_node_any *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200390{
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200391 struct lyd_node_any *any = (struct lyd_node_any *)node;
Radek Krejcie7b95092019-05-15 11:03:07 +0200392 struct lyd_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200393 uint32_t prev_opts, prev_lo;
Michal Vasko52927e22020-03-16 17:26:14 +0100394 LY_ERR ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200395
Michal Vasko9e685082021-01-29 14:49:09 +0100396 xml_print_node_open(ctx, &node->node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200397
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200398 if (!any->value.tree) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200399 /* no content */
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200400no_content:
Michal Vasko5233e962020-08-14 14:26:20 +0200401 ly_print_(ctx->out, "/>%s", DO_FORMAT ? "\n" : "");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200402 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200403 } else {
Michal Vasko60ea6352020-06-29 13:39:39 +0200404 if (any->value_type == LYD_ANYDATA_LYB) {
405 /* turn logging off */
406 prev_lo = ly_log_options(0);
407
408 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200409 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0, &iter) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200410 /* successfully parsed */
411 free(any->value.mem);
412 any->value.tree = iter;
413 any->value_type = LYD_ANYDATA_DATATREE;
414 }
Radek Krejci7931b192020-06-25 17:05:03 +0200415
416 /* turn loggin on again */
417 ly_log_options(prev_lo);
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 }
419
Radek Krejcie7b95092019-05-15 11:03:07 +0200420 switch (any->value_type) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200421 case LYD_ANYDATA_DATATREE:
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200422 /* close opening tag and print data */
Michal Vasko52927e22020-03-16 17:26:14 +0100423 prev_opts = ctx->options;
Radek Krejci7931b192020-06-25 17:05:03 +0200424 ctx->options &= ~LYD_PRINT_WITHSIBLINGS;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200425 LEVEL_INC;
426
Michal Vasko5233e962020-08-14 14:26:20 +0200427 ly_print_(ctx->out, ">%s", DO_FORMAT ? "\n" : "");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200428 LY_LIST_FOR(any->value.tree, iter) {
Michal Vasko52927e22020-03-16 17:26:14 +0100429 ret = xml_print_node(ctx, iter);
430 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
Radek Krejcie7b95092019-05-15 11:03:07 +0200431 }
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200432
433 LEVEL_DEC;
Michal Vasko52927e22020-03-16 17:26:14 +0100434 ctx->options = prev_opts;
Radek Krejcie7b95092019-05-15 11:03:07 +0200435 break;
436 case LYD_ANYDATA_STRING:
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200437 /* escape XML-sensitive characters */
438 if (!any->value.str[0]) {
439 goto no_content;
440 }
441 /* close opening tag and print data */
Michal Vasko5233e962020-08-14 14:26:20 +0200442 ly_print_(ctx->out, ">");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200443 lyxml_dump_text(ctx->out, any->value.str, 0);
Radek Krejcie7b95092019-05-15 11:03:07 +0200444 break;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200445 case LYD_ANYDATA_XML:
446 /* print without escaping special characters */
447 if (!any->value.str[0]) {
448 goto no_content;
449 }
Michal Vasko5233e962020-08-14 14:26:20 +0200450 ly_print_(ctx->out, ">%s", any->value.str);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200451 break;
452 case LYD_ANYDATA_JSON:
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200453 case LYD_ANYDATA_LYB:
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200454 /* JSON and LYB format is not supported */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200455 LOGWRN(LYD_CTX(node), "Unable to print anydata content (type %d) as XML.", any->value_type);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200456 goto no_content;
Radek Krejcie7b95092019-05-15 11:03:07 +0200457 }
458
459 /* closing tag */
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200460 if (any->value_type == LYD_ANYDATA_DATATREE) {
Michal Vasko5233e962020-08-14 14:26:20 +0200461 ly_print_(ctx->out, "%*s</%s>%s", INDENT, node->schema->name, DO_FORMAT ? "\n" : "");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200462 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200463 ly_print_(ctx->out, "</%s>%s", node->schema->name, DO_FORMAT ? "\n" : "");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200464 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200465 }
466
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200467 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200468}
Radek Krejcie7b95092019-05-15 11:03:07 +0200469
Michal Vasko52927e22020-03-16 17:26:14 +0100470static LY_ERR
471xml_print_opaq(struct xmlpr_ctx *ctx, const struct lyd_node_opaq *node)
472{
473 LY_ERR ret;
474 struct lyd_node *child;
Michal Vasko52927e22020-03-16 17:26:14 +0100475
476 LY_CHECK_RET(xml_print_opaq_open(ctx, node));
477
478 if (node->value[0]) {
479 /* print namespaces connected with the value's prefixes */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100480 if (node->val_prefix_data) {
481 xml_print_ns_prefix_data(ctx, node->format, node->val_prefix_data, LYXML_PREFIX_REQUIRED);
Michal Vasko52927e22020-03-16 17:26:14 +0100482 }
483
Radek IÅ¡a52c4ac62021-03-08 09:37:32 +0100484 ly_print_(ctx->out, ">");
485 lyxml_dump_text(ctx->out, node->value, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100486 }
487
488 if (node->child) {
489 /* children */
490 if (!node->value[0]) {
Michal Vasko5233e962020-08-14 14:26:20 +0200491 ly_print_(ctx->out, ">%s", DO_FORMAT ? "\n" : "");
Michal Vasko52927e22020-03-16 17:26:14 +0100492 }
493
494 LEVEL_INC;
495 LY_LIST_FOR(node->child, child) {
496 ret = xml_print_node(ctx, child);
497 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
498 }
499 LEVEL_DEC;
500
Michal Vaskoad92b672020-11-12 13:11:31 +0100501 ly_print_(ctx->out, "%*s</%s>%s", INDENT, node->name.name, DO_FORMAT ? "\n" : "");
Michal Vasko52927e22020-03-16 17:26:14 +0100502 } else if (node->value[0]) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100503 ly_print_(ctx->out, "</%s>%s", node->name.name, DO_FORMAT ? "\n" : "");
Michal Vasko52927e22020-03-16 17:26:14 +0100504 } else {
505 /* no value or children */
Michal Vasko5233e962020-08-14 14:26:20 +0200506 ly_print_(ctx->out, "/>%s", DO_FORMAT ? "\n" : "");
Michal Vasko52927e22020-03-16 17:26:14 +0100507 }
508
509 return LY_SUCCESS;
510}
511
Radek Krejcie7b95092019-05-15 11:03:07 +0200512/**
513 * @brief Print XML element representing lyd_node.
514 *
515 * @param[in] ctx XML printer context.
516 * @param[in] node Data node to be printed.
517 * @return LY_ERR value.
518 */
519static LY_ERR
520xml_print_node(struct xmlpr_ctx *ctx, const struct lyd_node *node)
521{
522 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100523 uint32_t ns_count;
Radek Krejcie7b95092019-05-15 11:03:07 +0200524
Michal Vasko9b368d32020-02-14 13:53:31 +0100525 if (!ly_should_print(node, ctx->options)) {
526 /* do not print at all */
Michal Vasko52927e22020-03-16 17:26:14 +0100527 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200528 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200529
Michal Vasko52927e22020-03-16 17:26:14 +0100530 /* remember namespace definition count on this level */
531 ns_count = ctx->ns.count;
532
533 if (!node->schema) {
534 ret = xml_print_opaq(ctx, (const struct lyd_node_opaq *)node);
535 } else {
536 switch (node->schema->nodetype) {
537 case LYS_CONTAINER:
538 case LYS_LIST:
539 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +0100540 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +0100541 case LYS_ACTION:
542 ret = xml_print_inner(ctx, (const struct lyd_node_inner *)node);
543 break;
544 case LYS_LEAF:
545 case LYS_LEAFLIST:
546 xml_print_term(ctx, (const struct lyd_node_term *)node);
547 break;
548 case LYS_ANYXML:
549 case LYS_ANYDATA:
550 ret = xml_print_anydata(ctx, (const struct lyd_node_any *)node);
551 break;
552 default:
553 LOGINT(node->schema->module->ctx);
554 ret = LY_EINT;
555 break;
556 }
557 }
558
559 /* remove all added namespaces */
560 while (ns_count < ctx->ns.count) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100561 lydict_remove(ctx->ctx, ctx->prefix.objs[ctx->prefix.count - 1]);
Michal Vasko52927e22020-03-16 17:26:14 +0100562 ly_set_rm_index(&ctx->prefix, ctx->prefix.count - 1, NULL);
563 ly_set_rm_index(&ctx->ns, ctx->ns.count - 1, NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200564 }
565
566 return ret;
567}
568
569LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200570xml_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200571{
572 const struct lyd_node *node;
Michal Vasko52927e22020-03-16 17:26:14 +0100573 struct xmlpr_ctx ctx = {0};
Radek Krejcie7b95092019-05-15 11:03:07 +0200574
575 if (!root) {
Michal Vasko69730152020-10-09 16:30:07 +0200576 if ((out->type == LY_OUT_MEMORY) || (out->type == LY_OUT_CALLBACK)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200577 ly_print_(out, "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200578 }
579 goto finish;
580 }
581
Michal Vasko52927e22020-03-16 17:26:14 +0100582 ctx.out = out;
Radek Krejci17ede7a2020-11-13 10:38:30 +0100583 ctx.level = 0;
Michal Vasko52927e22020-03-16 17:26:14 +0100584 ctx.options = options;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200585 ctx.ctx = LYD_CTX(root);
Michal Vasko52927e22020-03-16 17:26:14 +0100586
Radek Krejcie7b95092019-05-15 11:03:07 +0200587 /* content */
588 LY_LIST_FOR(root, node) {
Michal Vasko52927e22020-03-16 17:26:14 +0100589 LY_CHECK_RET(xml_print_node(&ctx, node));
Radek Krejci7931b192020-06-25 17:05:03 +0200590 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200591 break;
592 }
593 }
594
595finish:
Michal Vasko52927e22020-03-16 17:26:14 +0100596 assert(!ctx.prefix.count && !ctx.ns.count);
597 ly_set_erase(&ctx.prefix, NULL);
598 ly_set_erase(&ctx.ns, NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200599 ly_print_flush(out);
600 return LY_SUCCESS;
601}