blob: d33a72589e8619cf572a212293fcdde698878f05 [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 *
Michal Vasko78041d12022-11-29 14:11:07 +01007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
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 "context.h"
22#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010024#include "ly_common.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"
Michal Vaskob4750962022-10-06 15:33:35 +020028#include "plugins_exts/metadata.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "plugins_types.h"
30#include "printer_data.h"
31#include "printer_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020035#include "tree_schema.h"
36#include "xml.h"
37
38/**
39 * @brief XML printer context.
40 */
41struct xmlpr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020042 struct ly_out *out; /**< output specification */
43 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
44 uint32_t options; /**< [Data printer flags](@ref dataprinterflags) */
Michal Vasko52927e22020-03-16 17:26:14 +010045 const struct ly_ctx *ctx; /**< libyang context */
Radek Krejci1deb5be2020-08-26 16:43:36 +020046 struct ly_set prefix; /**< printed namespace prefixes */
47 struct ly_set ns; /**< printed namespaces */
Radek Krejcie7b95092019-05-15 11:03:07 +020048};
49
Michal Vasko52927e22020-03-16 17:26:14 +010050#define LYXML_PREFIX_REQUIRED 0x01 /**< The prefix is not just a suggestion but a requirement. */
Radek Krejci1798aae2020-07-14 13:26:06 +020051#define LYXML_PREFIX_DEFAULT 0x02 /**< The namespace is required to be a default (without prefix) */
Radek Krejcie7b95092019-05-15 11:03:07 +020052
53/**
Michal Vasko52927e22020-03-16 17:26:14 +010054 * @brief Print a namespace if not already printed.
55 *
56 * @param[in] ctx XML printer context.
57 * @param[in] ns Namespace to print, expected to be in dictionary.
58 * @param[in] new_prefix Suggested new prefix, NULL for a default namespace without prefix. Stored in the dictionary.
59 * @param[in] prefix_opts Prefix options changing the meaning of parameters.
60 * @return Printed prefix of the namespace to use.
Radek Krejcie7b95092019-05-15 11:03:07 +020061 */
Michal Vasko52927e22020-03-16 17:26:14 +010062static const char *
Michal Vasko61ad1ff2022-02-10 15:48:39 +010063xml_print_ns(struct xmlpr_ctx *pctx, const char *ns, const char *new_prefix, uint32_t prefix_opts)
Radek Krejcie7b95092019-05-15 11:03:07 +020064{
Radek Krejciba03a5a2020-08-27 14:40:41 +020065 uint32_t i;
Michal Vasko6f4cbb62020-02-28 11:15:47 +010066
Michal Vasko61ad1ff2022-02-10 15:48:39 +010067 for (i = pctx->ns.count; i > 0; --i) {
Michal Vasko52927e22020-03-16 17:26:14 +010068 if (!new_prefix) {
69 /* find default namespace */
Michal Vasko61ad1ff2022-02-10 15:48:39 +010070 if (!pctx->prefix.objs[i - 1]) {
71 if (!strcmp(pctx->ns.objs[i - 1], ns)) {
Radek Krejciba03a5a2020-08-27 14:40:41 +020072 /* matching default namespace */
Michal Vasko61ad1ff2022-02-10 15:48:39 +010073 return pctx->prefix.objs[i - 1];
Radek Krejcie7b95092019-05-15 11:03:07 +020074 }
Radek Krejciba03a5a2020-08-27 14:40:41 +020075 /* not matching default namespace */
Michal Vasko52927e22020-03-16 17:26:14 +010076 break;
77 }
78 } else {
79 /* find prefixed namespace */
Michal Vasko61ad1ff2022-02-10 15:48:39 +010080 if (!strcmp(pctx->ns.objs[i - 1], ns)) {
81 if (!pctx->prefix.objs[i - 1]) {
Michal Vasko52927e22020-03-16 17:26:14 +010082 /* default namespace is not interesting */
83 continue;
84 }
85
Michal Vasko61ad1ff2022-02-10 15:48:39 +010086 if (!strcmp(pctx->prefix.objs[i - 1], new_prefix) || !(prefix_opts & LYXML_PREFIX_REQUIRED)) {
Michal Vasko52927e22020-03-16 17:26:14 +010087 /* the same prefix or can be any */
Michal Vasko61ad1ff2022-02-10 15:48:39 +010088 return pctx->prefix.objs[i - 1];
Michal Vasko52927e22020-03-16 17:26:14 +010089 }
90 }
Radek Krejcie7b95092019-05-15 11:03:07 +020091 }
Radek Krejcie7b95092019-05-15 11:03:07 +020092 }
Radek Krejci28681fa2019-09-06 13:08:45 +020093
Radek Krejciba03a5a2020-08-27 14:40:41 +020094 /* suitable namespace not found, must be printed */
Michal Vasko61ad1ff2022-02-10 15:48:39 +010095 ly_print_(pctx->out, " xmlns%s%s=\"%s\"", new_prefix ? ":" : "", new_prefix ? new_prefix : "", ns);
Radek Krejcie7b95092019-05-15 11:03:07 +020096
Radek Krejciba03a5a2020-08-27 14:40:41 +020097 /* and added into namespaces */
98 if (new_prefix) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010099 LY_CHECK_RET(lydict_insert(pctx->ctx, new_prefix, 0, &new_prefix), NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200100 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100101 LY_CHECK_RET(ly_set_add(&pctx->prefix, (void *)new_prefix, 1, NULL), NULL);
102 LY_CHECK_RET(ly_set_add(&pctx->ns, (void *)ns, 1, &i), NULL);
Michal Vasko52927e22020-03-16 17:26:14 +0100103
104 /* return it */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100105 return pctx->prefix.objs[i];
Radek Krejci28681fa2019-09-06 13:08:45 +0200106}
107
Michal Vasko22df3f02020-08-24 13:29:22 +0200108static const char *
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100109xml_print_ns_opaq(struct xmlpr_ctx *pctx, LY_VALUE_FORMAT format, const struct ly_opaq_name *name, uint32_t prefix_opts)
Radek Krejci1798aae2020-07-14 13:26:06 +0200110{
Radek Krejci1798aae2020-07-14 13:26:06 +0200111 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200112 case LY_VALUE_XML:
Michal Vasko9c8125e2023-09-08 08:08:25 +0200113 if (name->module_ns) {
114 return xml_print_ns(pctx, name->module_ns, (prefix_opts & LYXML_PREFIX_DEFAULT) ? NULL : name->prefix, prefix_opts);
115 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200116 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200117 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100118 if (name->module_name) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100119 const struct lys_module *mod = ly_ctx_get_module_latest(pctx->ctx, name->module_name);
Michal Vasko26bbb272022-08-02 14:54:33 +0200120
Radek Krejci1798aae2020-07-14 13:26:06 +0200121 if (mod) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100122 return xml_print_ns(pctx, mod->ns, (prefix_opts & LYXML_PREFIX_DEFAULT) ? NULL : name->prefix, prefix_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200123 }
124 }
125 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100126 default:
Radek Krejci1798aae2020-07-14 13:26:06 +0200127 /* cannot be created */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100128 LOGINT(pctx->ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200129 }
130
131 return NULL;
132}
133
Radek Krejciec9ad602021-01-04 10:46:30 +0100134/**
135 * @brief Print prefix data.
136 *
137 * @param[in] ctx XML printer context.
Radek Krejci8df109d2021-04-23 12:19:08 +0200138 * @param[in] format Value prefix format, only ::LY_VALUE_XML supported.
139 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Radek Krejciec9ad602021-01-04 10:46:30 +0100140 * @param[in] prefix_opts Prefix options changing the meaning of parameters.
141 * @return LY_ERR value.
142 */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100143static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100144xml_print_ns_prefix_data(struct xmlpr_ctx *pctx, LY_VALUE_FORMAT format, void *prefix_data, uint32_t prefix_opts)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100145{
146 const struct ly_set *set;
147 const struct lyxml_ns *ns;
148 uint32_t i;
149
150 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200151 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100152 set = prefix_data;
153 for (i = 0; i < set->count; ++i) {
154 ns = set->objs[i];
Michal Vasko294e7f02022-02-28 13:59:00 +0100155 if (!ns->prefix) {
156 /* default namespace is not for the element */
157 continue;
158 }
159
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100160 xml_print_ns(pctx, ns->uri, (prefix_opts & LYXML_PREFIX_DEFAULT) ? NULL : ns->prefix, prefix_opts);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100161 }
162 break;
163 default:
164 /* cannot be created */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100165 LOGINT(pctx->ctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100166 }
167}
168
Radek Krejci28681fa2019-09-06 13:08:45 +0200169/**
Michal Vasko4da82072022-05-13 11:13:49 +0200170 * @brief Print metadata of a node.
171 *
172 * @param[in] pctx XML printer context.
173 * @param[in] node Data node with metadata.
Radek Krejcie7b95092019-05-15 11:03:07 +0200174 */
Michal Vasko52927e22020-03-16 17:26:14 +0100175static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100176xml_print_meta(struct xmlpr_ctx *pctx, const struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200177{
Michal Vasko9f96a052020-03-10 09:41:45 +0100178 struct lyd_meta *meta;
Michal Vasko52927e22020-03-16 17:26:14 +0100179 const struct lys_module *mod;
180 struct ly_set ns_list = {0};
Michal Vasko45791ad2021-06-17 08:45:03 +0200181 LY_ARRAY_COUNT_TYPE u;
182 ly_bool dynamic, filter_attrs = 0;
Michal Vasko32f067f2023-06-01 12:31:14 +0200183 const char *value;
184 uint32_t i;
Radek Krejcie7b95092019-05-15 11:03:07 +0200185
Radek Krejcie7b95092019-05-15 11:03:07 +0200186 /* with-defaults */
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100187 if (node->schema->nodetype & LYD_NODE_TERM) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100188 if (((node->flags & LYD_DEFAULT) && (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) ||
189 ((pctx->options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko52927e22020-03-16 17:26:14 +0100190 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
Radek Krejcic2b49d82021-04-26 08:05:57 +0200191 mod = ly_ctx_get_module_latest(LYD_CTX(node), "ietf-netconf-with-defaults");
Michal Vasko52927e22020-03-16 17:26:14 +0100192 if (mod) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100193 ly_print_(pctx->out, " %s:default=\"true\"", xml_print_ns(pctx, mod->ns, mod->prefix, 0));
Radek Krejcie7b95092019-05-15 11:03:07 +0200194 }
195 }
196 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200197
198 /* check for NETCONF filter unqualified attributes */
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100199 if (!strcmp(node->schema->module->name, "notifications")) {
200 filter_attrs = 1;
201 } else {
202 LY_ARRAY_FOR(node->schema->exts, u) {
203 if (!strcmp(node->schema->exts[u].def->name, "get-filter-element-attributes") &&
204 !strcmp(node->schema->exts[u].def->module->name, "ietf-netconf")) {
205 filter_attrs = 1;
206 break;
207 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200208 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200209 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200210
Michal Vasko9f96a052020-03-10 09:41:45 +0100211 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko32f067f2023-06-01 12:31:14 +0200212 /* store the module of the default namespace, NULL because there is none */
213 ly_set_add(&ns_list, NULL, 0, NULL);
214
215 /* print the value */
216 value = meta->value.realtype->plugin->print(LYD_CTX(node), &meta->value, LY_VALUE_XML, &ns_list, &dynamic, NULL);
Radek Krejci28681fa2019-09-06 13:08:45 +0200217
Michal Vasko52927e22020-03-16 17:26:14 +0100218 /* print namespaces connected with the value's prefixes */
Michal Vasko32f067f2023-06-01 12:31:14 +0200219 for (i = 1; i < ns_list.count; ++i) {
Michal Vasko45791ad2021-06-17 08:45:03 +0200220 mod = ns_list.objs[i];
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100221 xml_print_ns(pctx, mod->ns, mod->prefix, 1);
Radek Krejci28681fa2019-09-06 13:08:45 +0200222 }
223 ly_set_erase(&ns_list, NULL);
224
Radek Krejci0f969882020-08-21 16:56:47 +0200225 mod = meta->annotation->module;
Michal Vasko45791ad2021-06-17 08:45:03 +0200226 if (filter_attrs && !strcmp(mod->name, "ietf-netconf") && (!strcmp(meta->name, "type") ||
227 !strcmp(meta->name, "select"))) {
228 /* print special NETCONF filter unqualified attributes */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100229 ly_print_(pctx->out, " %s=\"", meta->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200230 } else {
231 /* print the metadata with its namespace */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100232 ly_print_(pctx->out, " %s:%s=\"", xml_print_ns(pctx, mod->ns, mod->prefix, 1), meta->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200233 }
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]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100237 lyxml_dump_text(pctx->out, value, 1);
Radek Krejcie7b95092019-05-15 11:03:07 +0200238 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100239 ly_print_(pctx->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
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100255xml_print_node_open(struct xmlpr_ctx *pctx, const struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200256{
Michal Vasko52927e22020-03-16 17:26:14 +0100257 /* print node name */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100258 ly_print_(pctx->out, "%*s<%s", INDENT, node->schema->name);
Michal Vasko52927e22020-03-16 17:26:14 +0100259
260 /* print default namespace */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100261 xml_print_ns(pctx, node->schema->module->ns, NULL, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100262
263 /* print metadata */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100264 xml_print_meta(pctx, node);
Michal Vasko52927e22020-03-16 17:26:14 +0100265}
266
267static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100268xml_print_attr(struct xmlpr_ctx *pctx, const struct lyd_node_opaq *node)
Michal Vasko52927e22020-03-16 17:26:14 +0100269{
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 Vasko61ad1ff2022-02-10 15:48:39 +0100277 pref = xml_print_ns_opaq(pctx, 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) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100282 xml_print_ns_prefix_data(pctx, 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 */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100286 ly_print_(pctx->out, " %s%s%s=\"", pref ? pref : "", pref ? ":" : "", attr->name.name);
287 lyxml_dump_text(pctx->out, attr->value, 1);
288 ly_print_(pctx->out, "\""); /* print attribute value terminator */
Radek IÅ¡a52c4ac62021-03-08 09:37:32 +0100289
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
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100296xml_print_opaq_open(struct xmlpr_ctx *pctx, const struct lyd_node_opaq *node)
Michal Vasko52927e22020-03-16 17:26:14 +0100297{
298 /* print node name */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100299 ly_print_(pctx->out, "%*s<%s", INDENT, node->name.name);
Michal Vasko52927e22020-03-16 17:26:14 +0100300
Michal Vaskoea0f96c2022-12-20 08:34:39 +0100301 if (node->name.prefix || node->name.module_ns) {
302 /* print default namespace */
303 xml_print_ns_opaq(pctx, node->format, &node->name, LYXML_PREFIX_DEFAULT);
304 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200305
Michal Vasko52927e22020-03-16 17:26:14 +0100306 /* print attributes */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100307 LY_CHECK_RET(xml_print_attr(pctx, node));
Radek Krejcie7b95092019-05-15 11:03:07 +0200308
309 return LY_SUCCESS;
310}
311
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100312static LY_ERR xml_print_node(struct xmlpr_ctx *pctx, const struct lyd_node *node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200313
314/**
315 * @brief Print XML element representing lyd_node_term.
316 *
317 * @param[in] ctx XML printer context.
318 * @param[in] node Data node to be printed.
Michal Vasko569f2f42021-08-04 11:31:08 +0200319 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200320 */
Michal Vasko569f2f42021-08-04 11:31:08 +0200321static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100322xml_print_term(struct xmlpr_ctx *pctx, const struct lyd_node_term *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200323{
Michal Vasko32f067f2023-06-01 12:31:14 +0200324 LY_ERR rc = LY_SUCCESS;
Radek Krejcia1911222019-07-22 17:24:50 +0200325 struct ly_set ns_list = {0};
Michal Vasko32f067f2023-06-01 12:31:14 +0200326 ly_bool dynamic = 0;
327 const char *value = NULL;
328 const struct lys_module *mod;
329 uint32_t i;
Radek Krejci28681fa2019-09-06 13:08:45 +0200330
Michal Vasko32f067f2023-06-01 12:31:14 +0200331 /* store the module of the default namespace */
332 if ((rc = ly_set_add(&ns_list, node->schema->module, 0, NULL))) {
333 LOGMEM(pctx->ctx);
334 goto cleanup;
335 }
336
337 /* print the value */
Radek Krejci224d4b42021-04-23 13:54:59 +0200338 value = ((struct lysc_node_leaf *)node->schema)->type->plugin->print(LYD_CTX(node), &node->value, LY_VALUE_XML,
339 &ns_list, &dynamic, NULL);
Michal Vasko32f067f2023-06-01 12:31:14 +0200340 LY_CHECK_ERR_GOTO(!value, rc = LY_EINVAL, cleanup);
341
342 /* print node opening */
343 xml_print_node_open(pctx, &node->node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200344
Radek Krejcia1911222019-07-22 17:24:50 +0200345 /* print namespaces connected with the values's prefixes */
Michal Vasko32f067f2023-06-01 12:31:14 +0200346 for (i = 1; i < ns_list.count; ++i) {
347 mod = ns_list.objs[i];
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100348 ly_print_(pctx->out, " xmlns:%s=\"%s\"", mod->prefix, mod->ns);
Radek Krejcie7b95092019-05-15 11:03:07 +0200349 }
350
Michal Vasko569f2f42021-08-04 11:31:08 +0200351 if (!value[0]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100352 ly_print_(pctx->out, "/>%s", DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200353 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100354 ly_print_(pctx->out, ">");
355 lyxml_dump_text(pctx->out, value, 0);
356 ly_print_(pctx->out, "</%s>%s", node->schema->name, DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200357 }
Michal Vasko32f067f2023-06-01 12:31:14 +0200358
359cleanup:
360 ly_set_erase(&ns_list, NULL);
Radek Krejcia1911222019-07-22 17:24:50 +0200361 if (dynamic) {
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100362 free((void *)value);
Radek Krejcia1911222019-07-22 17:24:50 +0200363 }
Michal Vasko32f067f2023-06-01 12:31:14 +0200364 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200365}
366
367/**
368 * @brief Print XML element representing lyd_node_inner.
369 *
370 * @param[in] ctx XML printer context.
371 * @param[in] node Data node to be printed.
372 * @return LY_ERR value.
373 */
374static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100375xml_print_inner(struct xmlpr_ctx *pctx, const struct lyd_node_inner *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200376{
377 LY_ERR ret;
378 struct lyd_node *child;
379
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100380 xml_print_node_open(pctx, &node->node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200381
Michal Vasko630d9892020-12-08 17:11:08 +0100382 LY_LIST_FOR(node->child, child) {
Michal Vasko8db584d2022-03-30 13:42:48 +0200383 if (lyd_node_should_print(child, pctx->options)) {
Michal Vasko630d9892020-12-08 17:11:08 +0100384 break;
385 }
386 }
387 if (!child) {
388 /* there are no children that will be printed */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100389 ly_print_(pctx->out, "/>%s", DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200390 return LY_SUCCESS;
391 }
392
393 /* children */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100394 ly_print_(pctx->out, ">%s", DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200395
396 LEVEL_INC;
397 LY_LIST_FOR(node->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100398 ret = xml_print_node(pctx, child);
Radek Krejcie7b95092019-05-15 11:03:07 +0200399 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
400 }
401 LEVEL_DEC;
402
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100403 ly_print_(pctx->out, "%*s</%s>%s", INDENT, node->schema->name, DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200404
405 return LY_SUCCESS;
406}
407
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200408static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100409xml_print_anydata(struct xmlpr_ctx *pctx, const struct lyd_node_any *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200410{
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200411 struct lyd_node_any *any = (struct lyd_node_any *)node;
Radek Krejcie7b95092019-05-15 11:03:07 +0200412 struct lyd_node *iter;
Michal Vasko59004e32024-01-30 16:09:54 +0100413 uint32_t prev_opts, *prev_lo, temp_lo = 0;
Michal Vasko52927e22020-03-16 17:26:14 +0100414 LY_ERR ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200415
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100416 if ((node->schema->nodetype == LYS_ANYDATA) && (node->value_type != LYD_ANYDATA_DATATREE)) {
417 LOGINT_RET(pctx->ctx);
418 }
419
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100420 xml_print_node_open(pctx, &node->node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200421
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200422 if (!any->value.tree) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200423 /* no content */
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200424no_content:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100425 ly_print_(pctx->out, "/>%s", DO_FORMAT ? "\n" : "");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200426 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200427 } else {
Michal Vasko60ea6352020-06-29 13:39:39 +0200428 if (any->value_type == LYD_ANYDATA_LYB) {
429 /* turn logging off */
Michal Vasko59004e32024-01-30 16:09:54 +0100430 prev_lo = ly_temp_log_options(&temp_lo);
Michal Vasko60ea6352020-06-29 13:39:39 +0200431
432 /* try to parse it into a data tree */
Michal Vasko78041d12022-11-29 14:11:07 +0100433 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB,
434 LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0, &iter) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200435 /* successfully parsed */
436 free(any->value.mem);
437 any->value.tree = iter;
438 any->value_type = LYD_ANYDATA_DATATREE;
439 }
Radek Krejci7931b192020-06-25 17:05:03 +0200440
Michal Vasko78041d12022-11-29 14:11:07 +0100441 /* turn logging on again */
Michal Vasko59004e32024-01-30 16:09:54 +0100442 ly_temp_log_options(prev_lo);
Michal Vasko60ea6352020-06-29 13:39:39 +0200443 }
444
Radek Krejcie7b95092019-05-15 11:03:07 +0200445 switch (any->value_type) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200446 case LYD_ANYDATA_DATATREE:
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200447 /* close opening tag and print data */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100448 prev_opts = pctx->options;
449 pctx->options &= ~LYD_PRINT_WITHSIBLINGS;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200450 LEVEL_INC;
451
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100452 ly_print_(pctx->out, ">%s", DO_FORMAT ? "\n" : "");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200453 LY_LIST_FOR(any->value.tree, iter) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100454 ret = xml_print_node(pctx, iter);
Michal Vasko52927e22020-03-16 17:26:14 +0100455 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
Radek Krejcie7b95092019-05-15 11:03:07 +0200456 }
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200457
458 LEVEL_DEC;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100459 pctx->options = prev_opts;
Radek Krejcie7b95092019-05-15 11:03:07 +0200460 break;
461 case LYD_ANYDATA_STRING:
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200462 /* escape XML-sensitive characters */
463 if (!any->value.str[0]) {
464 goto no_content;
465 }
466 /* close opening tag and print data */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100467 ly_print_(pctx->out, ">");
468 lyxml_dump_text(pctx->out, any->value.str, 0);
Radek Krejcie7b95092019-05-15 11:03:07 +0200469 break;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200470 case LYD_ANYDATA_XML:
471 /* print without escaping special characters */
472 if (!any->value.str[0]) {
473 goto no_content;
474 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100475 ly_print_(pctx->out, ">%s", any->value.str);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200476 break;
477 case LYD_ANYDATA_JSON:
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200478 case LYD_ANYDATA_LYB:
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200479 /* JSON and LYB format is not supported */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100480 LOGWRN(pctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200481 goto no_content;
Radek Krejcie7b95092019-05-15 11:03:07 +0200482 }
483
484 /* closing tag */
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200485 if (any->value_type == LYD_ANYDATA_DATATREE) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100486 ly_print_(pctx->out, "%*s</%s>%s", INDENT, node->schema->name, DO_FORMAT ? "\n" : "");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200487 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100488 ly_print_(pctx->out, "</%s>%s", node->schema->name, DO_FORMAT ? "\n" : "");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200489 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200490 }
491
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200492 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200493}
Radek Krejcie7b95092019-05-15 11:03:07 +0200494
Michal Vasko52927e22020-03-16 17:26:14 +0100495static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100496xml_print_opaq(struct xmlpr_ctx *pctx, const struct lyd_node_opaq *node)
Michal Vasko52927e22020-03-16 17:26:14 +0100497{
498 LY_ERR ret;
499 struct lyd_node *child;
Michal Vasko52927e22020-03-16 17:26:14 +0100500
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100501 LY_CHECK_RET(xml_print_opaq_open(pctx, node));
Michal Vasko52927e22020-03-16 17:26:14 +0100502
503 if (node->value[0]) {
504 /* print namespaces connected with the value's prefixes */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100505 if (node->val_prefix_data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100506 xml_print_ns_prefix_data(pctx, node->format, node->val_prefix_data, LYXML_PREFIX_REQUIRED);
Michal Vasko52927e22020-03-16 17:26:14 +0100507 }
508
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100509 ly_print_(pctx->out, ">");
510 lyxml_dump_text(pctx->out, node->value, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100511 }
512
513 if (node->child) {
514 /* children */
515 if (!node->value[0]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100516 ly_print_(pctx->out, ">%s", DO_FORMAT ? "\n" : "");
Michal Vasko52927e22020-03-16 17:26:14 +0100517 }
518
519 LEVEL_INC;
520 LY_LIST_FOR(node->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100521 ret = xml_print_node(pctx, child);
Michal Vasko52927e22020-03-16 17:26:14 +0100522 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
523 }
524 LEVEL_DEC;
525
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100526 ly_print_(pctx->out, "%*s</%s>%s", INDENT, node->name.name, DO_FORMAT ? "\n" : "");
Michal Vasko52927e22020-03-16 17:26:14 +0100527 } else if (node->value[0]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100528 ly_print_(pctx->out, "</%s>%s", node->name.name, DO_FORMAT ? "\n" : "");
Michal Vasko52927e22020-03-16 17:26:14 +0100529 } else {
530 /* no value or children */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100531 ly_print_(pctx->out, "/>%s", DO_FORMAT ? "\n" : "");
Michal Vasko52927e22020-03-16 17:26:14 +0100532 }
533
534 return LY_SUCCESS;
535}
536
Radek Krejcie7b95092019-05-15 11:03:07 +0200537/**
538 * @brief Print XML element representing lyd_node.
539 *
540 * @param[in] ctx XML printer context.
541 * @param[in] node Data node to be printed.
542 * @return LY_ERR value.
543 */
544static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100545xml_print_node(struct xmlpr_ctx *pctx, const struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200546{
547 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100548 uint32_t ns_count;
Radek Krejcie7b95092019-05-15 11:03:07 +0200549
Michal Vasko8db584d2022-03-30 13:42:48 +0200550 if (!lyd_node_should_print(node, pctx->options)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100551 /* do not print at all */
Michal Vasko52927e22020-03-16 17:26:14 +0100552 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200553 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200554
Michal Vasko52927e22020-03-16 17:26:14 +0100555 /* remember namespace definition count on this level */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100556 ns_count = pctx->ns.count;
Michal Vasko52927e22020-03-16 17:26:14 +0100557
558 if (!node->schema) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100559 ret = xml_print_opaq(pctx, (const struct lyd_node_opaq *)node);
Michal Vasko52927e22020-03-16 17:26:14 +0100560 } else {
561 switch (node->schema->nodetype) {
562 case LYS_CONTAINER:
563 case LYS_LIST:
564 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +0100565 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +0100566 case LYS_ACTION:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100567 ret = xml_print_inner(pctx, (const struct lyd_node_inner *)node);
Michal Vasko52927e22020-03-16 17:26:14 +0100568 break;
569 case LYS_LEAF:
570 case LYS_LEAFLIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100571 ret = xml_print_term(pctx, (const struct lyd_node_term *)node);
Michal Vasko52927e22020-03-16 17:26:14 +0100572 break;
573 case LYS_ANYXML:
574 case LYS_ANYDATA:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100575 ret = xml_print_anydata(pctx, (const struct lyd_node_any *)node);
Michal Vasko52927e22020-03-16 17:26:14 +0100576 break;
577 default:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100578 LOGINT(pctx->ctx);
Michal Vasko52927e22020-03-16 17:26:14 +0100579 ret = LY_EINT;
580 break;
581 }
582 }
583
584 /* remove all added namespaces */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100585 while (ns_count < pctx->ns.count) {
586 lydict_remove(pctx->ctx, pctx->prefix.objs[pctx->prefix.count - 1]);
587 ly_set_rm_index(&pctx->prefix, pctx->prefix.count - 1, NULL);
588 ly_set_rm_index(&pctx->ns, pctx->ns.count - 1, NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200589 }
590
591 return ret;
592}
593
594LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200595xml_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200596{
597 const struct lyd_node *node;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100598 struct xmlpr_ctx pctx = {0};
Radek Krejcie7b95092019-05-15 11:03:07 +0200599
600 if (!root) {
Michal Vasko69730152020-10-09 16:30:07 +0200601 if ((out->type == LY_OUT_MEMORY) || (out->type == LY_OUT_CALLBACK)) {
Michal Vasko5233e962020-08-14 14:26:20 +0200602 ly_print_(out, "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200603 }
604 goto finish;
605 }
606
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100607 pctx.out = out;
608 pctx.level = 0;
609 pctx.options = options;
610 pctx.ctx = LYD_CTX(root);
Michal Vasko52927e22020-03-16 17:26:14 +0100611
Radek Krejcie7b95092019-05-15 11:03:07 +0200612 /* content */
613 LY_LIST_FOR(root, node) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100614 LY_CHECK_RET(xml_print_node(&pctx, node));
Radek Krejci7931b192020-06-25 17:05:03 +0200615 if (!(options & LYD_PRINT_WITHSIBLINGS)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200616 break;
617 }
618 }
619
620finish:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100621 assert(!pctx.prefix.count && !pctx.ns.count);
622 ly_set_erase(&pctx.prefix, NULL);
623 ly_set_erase(&pctx.ns, NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200624 ly_print_flush(out);
625 return LY_SUCCESS;
626}