blob: c7a6c85ba8b76199cd52224b2cf96e271f78afcb [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 "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"
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 Vasko61ad1ff2022-02-10 15:48:39 +0100113 return xml_print_ns(pctx, name->module_ns, (prefix_opts & LYXML_PREFIX_DEFAULT) ? NULL : name->prefix, prefix_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200114 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200115 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +0100116 if (name->module_name) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100117 const struct lys_module *mod = ly_ctx_get_module_latest(pctx->ctx, name->module_name);
Michal Vasko26bbb272022-08-02 14:54:33 +0200118
Radek Krejci1798aae2020-07-14 13:26:06 +0200119 if (mod) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100120 return xml_print_ns(pctx, mod->ns, (prefix_opts & LYXML_PREFIX_DEFAULT) ? NULL : name->prefix, prefix_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200121 }
122 }
123 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100124 default:
Radek Krejci1798aae2020-07-14 13:26:06 +0200125 /* cannot be created */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100126 LOGINT(pctx->ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200127 }
128
129 return NULL;
130}
131
Radek Krejciec9ad602021-01-04 10:46:30 +0100132/**
133 * @brief Print prefix data.
134 *
135 * @param[in] ctx XML printer context.
Radek Krejci8df109d2021-04-23 12:19:08 +0200136 * @param[in] format Value prefix format, only ::LY_VALUE_XML supported.
137 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Radek Krejciec9ad602021-01-04 10:46:30 +0100138 * @param[in] prefix_opts Prefix options changing the meaning of parameters.
139 * @return LY_ERR value.
140 */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100141static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100142xml_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 +0100143{
144 const struct ly_set *set;
145 const struct lyxml_ns *ns;
146 uint32_t i;
147
148 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200149 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100150 set = prefix_data;
151 for (i = 0; i < set->count; ++i) {
152 ns = set->objs[i];
Michal Vasko294e7f02022-02-28 13:59:00 +0100153 if (!ns->prefix) {
154 /* default namespace is not for the element */
155 continue;
156 }
157
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100158 xml_print_ns(pctx, ns->uri, (prefix_opts & LYXML_PREFIX_DEFAULT) ? NULL : ns->prefix, prefix_opts);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100159 }
160 break;
161 default:
162 /* cannot be created */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100163 LOGINT(pctx->ctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100164 }
165}
166
Radek Krejci28681fa2019-09-06 13:08:45 +0200167/**
Michal Vasko4da82072022-05-13 11:13:49 +0200168 * @brief Print metadata of a node.
169 *
170 * @param[in] pctx XML printer context.
171 * @param[in] node Data node with metadata.
Radek Krejcie7b95092019-05-15 11:03:07 +0200172 */
Michal Vasko52927e22020-03-16 17:26:14 +0100173static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100174xml_print_meta(struct xmlpr_ctx *pctx, const struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200175{
Michal Vasko9f96a052020-03-10 09:41:45 +0100176 struct lyd_meta *meta;
Michal Vasko52927e22020-03-16 17:26:14 +0100177 const struct lys_module *mod;
178 struct ly_set ns_list = {0};
Michal Vasko45791ad2021-06-17 08:45:03 +0200179 LY_ARRAY_COUNT_TYPE u;
180 ly_bool dynamic, filter_attrs = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200181
Radek Krejcie7b95092019-05-15 11:03:07 +0200182 /* with-defaults */
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100183 if (node->schema->nodetype & LYD_NODE_TERM) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100184 if (((node->flags & LYD_DEFAULT) && (pctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) ||
185 ((pctx->options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) {
Michal Vasko52927e22020-03-16 17:26:14 +0100186 /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */
Radek Krejcic2b49d82021-04-26 08:05:57 +0200187 mod = ly_ctx_get_module_latest(LYD_CTX(node), "ietf-netconf-with-defaults");
Michal Vasko52927e22020-03-16 17:26:14 +0100188 if (mod) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100189 ly_print_(pctx->out, " %s:default=\"true\"", xml_print_ns(pctx, mod->ns, mod->prefix, 0));
Radek Krejcie7b95092019-05-15 11:03:07 +0200190 }
191 }
192 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200193
194 /* check for NETCONF filter unqualified attributes */
195 LY_ARRAY_FOR(node->schema->exts, u) {
196 if (!strcmp(node->schema->exts[u].def->name, "get-filter-element-attributes") &&
197 !strcmp(node->schema->exts[u].def->module->name, "ietf-netconf")) {
198 filter_attrs = 1;
199 break;
200 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200201 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200202
Michal Vasko9f96a052020-03-10 09:41:45 +0100203 for (meta = node->meta; meta; meta = meta->next) {
Radek Krejci224d4b42021-04-23 13:54:59 +0200204 const char *value = meta->value.realtype->plugin->print(LYD_CTX(node), &meta->value, LY_VALUE_XML, &ns_list,
205 &dynamic, NULL);
Radek Krejci28681fa2019-09-06 13:08:45 +0200206
Michal Vasko52927e22020-03-16 17:26:14 +0100207 /* print namespaces connected with the value's prefixes */
Michal Vasko45791ad2021-06-17 08:45:03 +0200208 for (uint32_t i = 0; i < ns_list.count; ++i) {
209 mod = ns_list.objs[i];
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100210 xml_print_ns(pctx, mod->ns, mod->prefix, 1);
Radek Krejci28681fa2019-09-06 13:08:45 +0200211 }
212 ly_set_erase(&ns_list, NULL);
213
Radek Krejci0f969882020-08-21 16:56:47 +0200214 mod = meta->annotation->module;
Michal Vasko45791ad2021-06-17 08:45:03 +0200215 if (filter_attrs && !strcmp(mod->name, "ietf-netconf") && (!strcmp(meta->name, "type") ||
216 !strcmp(meta->name, "select"))) {
217 /* print special NETCONF filter unqualified attributes */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100218 ly_print_(pctx->out, " %s=\"", meta->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200219 } else {
220 /* print the metadata with its namespace */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100221 ly_print_(pctx->out, " %s:%s=\"", xml_print_ns(pctx, mod->ns, mod->prefix, 1), meta->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200222 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200223
Michal Vasko52927e22020-03-16 17:26:14 +0100224 /* print metadata value */
Radek Krejci28681fa2019-09-06 13:08:45 +0200225 if (value && value[0]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100226 lyxml_dump_text(pctx->out, value, 1);
Radek Krejcie7b95092019-05-15 11:03:07 +0200227 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100228 ly_print_(pctx->out, "\"");
Radek Krejci28681fa2019-09-06 13:08:45 +0200229 if (dynamic) {
Michal Vasko52927e22020-03-16 17:26:14 +0100230 free((void *)value);
Radek Krejcie7b95092019-05-15 11:03:07 +0200231 }
232 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200233}
234
235/**
236 * @brief Print generic XML element despite of the data node type.
237 *
238 * Prints the element name, attributes and necessary namespaces.
239 *
240 * @param[in] ctx XML printer context.
241 * @param[in] node Data node to be printed.
Radek Krejcie7b95092019-05-15 11:03:07 +0200242 */
Michal Vasko52927e22020-03-16 17:26:14 +0100243static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100244xml_print_node_open(struct xmlpr_ctx *pctx, const struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200245{
Michal Vasko52927e22020-03-16 17:26:14 +0100246 /* print node name */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100247 ly_print_(pctx->out, "%*s<%s", INDENT, node->schema->name);
Michal Vasko52927e22020-03-16 17:26:14 +0100248
249 /* print default namespace */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100250 xml_print_ns(pctx, node->schema->module->ns, NULL, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100251
252 /* print metadata */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100253 xml_print_meta(pctx, node);
Michal Vasko52927e22020-03-16 17:26:14 +0100254}
255
256static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100257xml_print_attr(struct xmlpr_ctx *pctx, const struct lyd_node_opaq *node)
Michal Vasko52927e22020-03-16 17:26:14 +0100258{
Radek Krejci5536d282020-08-04 23:27:44 +0200259 const struct lyd_attr *attr;
Michal Vasko52927e22020-03-16 17:26:14 +0100260 const char *pref;
Michal Vasko52927e22020-03-16 17:26:14 +0100261
262 LY_LIST_FOR(node->attr, attr) {
263 pref = NULL;
Michal Vaskoad92b672020-11-12 13:11:31 +0100264 if (attr->name.prefix) {
Michal Vasko52927e22020-03-16 17:26:14 +0100265 /* print attribute namespace */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100266 pref = xml_print_ns_opaq(pctx, attr->format, &attr->name, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100267 }
268
269 /* print namespaces connected with the value's prefixes */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100270 if (attr->val_prefix_data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100271 xml_print_ns_prefix_data(pctx, attr->format, attr->val_prefix_data, LYXML_PREFIX_REQUIRED);
Michal Vasko52927e22020-03-16 17:26:14 +0100272 }
273
274 /* print the attribute with its prefix and value */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100275 ly_print_(pctx->out, " %s%s%s=\"", pref ? pref : "", pref ? ":" : "", attr->name.name);
276 lyxml_dump_text(pctx->out, attr->value, 1);
277 ly_print_(pctx->out, "\""); /* print attribute value terminator */
Radek IÅ¡a52c4ac62021-03-08 09:37:32 +0100278
Radek Krejcie7b95092019-05-15 11:03:07 +0200279 }
280
Michal Vasko52927e22020-03-16 17:26:14 +0100281 return LY_SUCCESS;
282}
283
284static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100285xml_print_opaq_open(struct xmlpr_ctx *pctx, const struct lyd_node_opaq *node)
Michal Vasko52927e22020-03-16 17:26:14 +0100286{
287 /* print node name */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100288 ly_print_(pctx->out, "%*s<%s", INDENT, node->name.name);
Michal Vasko52927e22020-03-16 17:26:14 +0100289
290 /* print default namespace */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100291 xml_print_ns_opaq(pctx, node->format, &node->name, LYXML_PREFIX_DEFAULT);
Radek Krejcie7b95092019-05-15 11:03:07 +0200292
Michal Vasko52927e22020-03-16 17:26:14 +0100293 /* print attributes */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100294 LY_CHECK_RET(xml_print_attr(pctx, node));
Radek Krejcie7b95092019-05-15 11:03:07 +0200295
296 return LY_SUCCESS;
297}
298
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100299static LY_ERR xml_print_node(struct xmlpr_ctx *pctx, const struct lyd_node *node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200300
301/**
302 * @brief Print XML element representing lyd_node_term.
303 *
304 * @param[in] ctx XML printer context.
305 * @param[in] node Data node to be printed.
Michal Vasko569f2f42021-08-04 11:31:08 +0200306 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200307 */
Michal Vasko569f2f42021-08-04 11:31:08 +0200308static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100309xml_print_term(struct xmlpr_ctx *pctx, const struct lyd_node_term *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200310{
Radek Krejcia1911222019-07-22 17:24:50 +0200311 struct ly_set ns_list = {0};
Radek Krejci857189e2020-09-01 13:26:36 +0200312 ly_bool dynamic;
Radek Krejci28681fa2019-09-06 13:08:45 +0200313 const char *value;
314
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100315 xml_print_node_open(pctx, &node->node);
Radek Krejci224d4b42021-04-23 13:54:59 +0200316 value = ((struct lysc_node_leaf *)node->schema)->type->plugin->print(LYD_CTX(node), &node->value, LY_VALUE_XML,
317 &ns_list, &dynamic, NULL);
Michal Vasko569f2f42021-08-04 11:31:08 +0200318 LY_CHECK_RET(!value, LY_EINVAL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200319
Radek Krejcia1911222019-07-22 17:24:50 +0200320 /* print namespaces connected with the values's prefixes */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200321 for (uint32_t u = 0; u < ns_list.count; ++u) {
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100322 const struct lys_module *mod = (const struct lys_module *)ns_list.objs[u];
Michal Vasko26bbb272022-08-02 14:54:33 +0200323
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100324 ly_print_(pctx->out, " xmlns:%s=\"%s\"", mod->prefix, mod->ns);
Radek Krejcie7b95092019-05-15 11:03:07 +0200325 }
Radek Krejcia1911222019-07-22 17:24:50 +0200326 ly_set_erase(&ns_list, NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200327
Michal Vasko569f2f42021-08-04 11:31:08 +0200328 if (!value[0]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100329 ly_print_(pctx->out, "/>%s", DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200330 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100331 ly_print_(pctx->out, ">");
332 lyxml_dump_text(pctx->out, value, 0);
333 ly_print_(pctx->out, "</%s>%s", node->schema->name, DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200334 }
Radek Krejcia1911222019-07-22 17:24:50 +0200335 if (dynamic) {
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100336 free((void *)value);
Radek Krejcia1911222019-07-22 17:24:50 +0200337 }
Michal Vasko569f2f42021-08-04 11:31:08 +0200338
339 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200340}
341
342/**
343 * @brief Print XML element representing lyd_node_inner.
344 *
345 * @param[in] ctx XML printer context.
346 * @param[in] node Data node to be printed.
347 * @return LY_ERR value.
348 */
349static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100350xml_print_inner(struct xmlpr_ctx *pctx, const struct lyd_node_inner *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200351{
352 LY_ERR ret;
353 struct lyd_node *child;
354
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100355 xml_print_node_open(pctx, &node->node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200356
Michal Vasko630d9892020-12-08 17:11:08 +0100357 LY_LIST_FOR(node->child, child) {
Michal Vasko8db584d2022-03-30 13:42:48 +0200358 if (lyd_node_should_print(child, pctx->options)) {
Michal Vasko630d9892020-12-08 17:11:08 +0100359 break;
360 }
361 }
362 if (!child) {
363 /* there are no children that will be printed */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100364 ly_print_(pctx->out, "/>%s", DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200365 return LY_SUCCESS;
366 }
367
368 /* children */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100369 ly_print_(pctx->out, ">%s", DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200370
371 LEVEL_INC;
372 LY_LIST_FOR(node->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100373 ret = xml_print_node(pctx, child);
Radek Krejcie7b95092019-05-15 11:03:07 +0200374 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
375 }
376 LEVEL_DEC;
377
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100378 ly_print_(pctx->out, "%*s</%s>%s", INDENT, node->schema->name, DO_FORMAT ? "\n" : "");
Radek Krejcie7b95092019-05-15 11:03:07 +0200379
380 return LY_SUCCESS;
381}
382
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200383static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100384xml_print_anydata(struct xmlpr_ctx *pctx, const struct lyd_node_any *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200385{
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200386 struct lyd_node_any *any = (struct lyd_node_any *)node;
Radek Krejcie7b95092019-05-15 11:03:07 +0200387 struct lyd_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200388 uint32_t prev_opts, prev_lo;
Michal Vasko52927e22020-03-16 17:26:14 +0100389 LY_ERR ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200390
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100391 if ((node->schema->nodetype == LYS_ANYDATA) && (node->value_type != LYD_ANYDATA_DATATREE)) {
392 LOGINT_RET(pctx->ctx);
393 }
394
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100395 xml_print_node_open(pctx, &node->node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200396
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200397 if (!any->value.tree) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200398 /* no content */
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200399no_content:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100400 ly_print_(pctx->out, "/>%s", DO_FORMAT ? "\n" : "");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200401 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200402 } else {
Michal Vasko60ea6352020-06-29 13:39:39 +0200403 if (any->value_type == LYD_ANYDATA_LYB) {
404 /* turn logging off */
405 prev_lo = ly_log_options(0);
406
407 /* try to parse it into a data tree */
Michal Vasko78041d12022-11-29 14:11:07 +0100408 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB,
409 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
Michal Vasko78041d12022-11-29 14:11:07 +0100416 /* turn logging on again */
Radek Krejci7931b192020-06-25 17:05:03 +0200417 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 Vasko61ad1ff2022-02-10 15:48:39 +0100423 prev_opts = pctx->options;
424 pctx->options &= ~LYD_PRINT_WITHSIBLINGS;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200425 LEVEL_INC;
426
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100427 ly_print_(pctx->out, ">%s", DO_FORMAT ? "\n" : "");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200428 LY_LIST_FOR(any->value.tree, iter) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100429 ret = xml_print_node(pctx, iter);
Michal Vasko52927e22020-03-16 17:26:14 +0100430 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 Vasko61ad1ff2022-02-10 15:48:39 +0100434 pctx->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 Vasko61ad1ff2022-02-10 15:48:39 +0100442 ly_print_(pctx->out, ">");
443 lyxml_dump_text(pctx->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 Vasko61ad1ff2022-02-10 15:48:39 +0100450 ly_print_(pctx->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 Vasko61ad1ff2022-02-10 15:48:39 +0100455 LOGWRN(pctx->ctx, "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 Vasko61ad1ff2022-02-10 15:48:39 +0100461 ly_print_(pctx->out, "%*s</%s>%s", INDENT, node->schema->name, DO_FORMAT ? "\n" : "");
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200462 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100463 ly_print_(pctx->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
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100471xml_print_opaq(struct xmlpr_ctx *pctx, const struct lyd_node_opaq *node)
Michal Vasko52927e22020-03-16 17:26:14 +0100472{
473 LY_ERR ret;
474 struct lyd_node *child;
Michal Vasko52927e22020-03-16 17:26:14 +0100475
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100476 LY_CHECK_RET(xml_print_opaq_open(pctx, node));
Michal Vasko52927e22020-03-16 17:26:14 +0100477
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) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100481 xml_print_ns_prefix_data(pctx, node->format, node->val_prefix_data, LYXML_PREFIX_REQUIRED);
Michal Vasko52927e22020-03-16 17:26:14 +0100482 }
483
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100484 ly_print_(pctx->out, ">");
485 lyxml_dump_text(pctx->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 Vasko61ad1ff2022-02-10 15:48:39 +0100491 ly_print_(pctx->out, ">%s", DO_FORMAT ? "\n" : "");
Michal Vasko52927e22020-03-16 17:26:14 +0100492 }
493
494 LEVEL_INC;
495 LY_LIST_FOR(node->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100496 ret = xml_print_node(pctx, child);
Michal Vasko52927e22020-03-16 17:26:14 +0100497 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
498 }
499 LEVEL_DEC;
500
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100501 ly_print_(pctx->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 Vasko61ad1ff2022-02-10 15:48:39 +0100503 ly_print_(pctx->out, "</%s>%s", node->name.name, DO_FORMAT ? "\n" : "");
Michal Vasko52927e22020-03-16 17:26:14 +0100504 } else {
505 /* no value or children */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100506 ly_print_(pctx->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
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100520xml_print_node(struct xmlpr_ctx *pctx, const struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200521{
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 Vasko8db584d2022-03-30 13:42:48 +0200525 if (!lyd_node_should_print(node, pctx->options)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100526 /* 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 */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100531 ns_count = pctx->ns.count;
Michal Vasko52927e22020-03-16 17:26:14 +0100532
533 if (!node->schema) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100534 ret = xml_print_opaq(pctx, (const struct lyd_node_opaq *)node);
Michal Vasko52927e22020-03-16 17:26:14 +0100535 } 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:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100542 ret = xml_print_inner(pctx, (const struct lyd_node_inner *)node);
Michal Vasko52927e22020-03-16 17:26:14 +0100543 break;
544 case LYS_LEAF:
545 case LYS_LEAFLIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100546 ret = xml_print_term(pctx, (const struct lyd_node_term *)node);
Michal Vasko52927e22020-03-16 17:26:14 +0100547 break;
548 case LYS_ANYXML:
549 case LYS_ANYDATA:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100550 ret = xml_print_anydata(pctx, (const struct lyd_node_any *)node);
Michal Vasko52927e22020-03-16 17:26:14 +0100551 break;
552 default:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100553 LOGINT(pctx->ctx);
Michal Vasko52927e22020-03-16 17:26:14 +0100554 ret = LY_EINT;
555 break;
556 }
557 }
558
559 /* remove all added namespaces */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100560 while (ns_count < pctx->ns.count) {
561 lydict_remove(pctx->ctx, pctx->prefix.objs[pctx->prefix.count - 1]);
562 ly_set_rm_index(&pctx->prefix, pctx->prefix.count - 1, NULL);
563 ly_set_rm_index(&pctx->ns, pctx->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 Vasko61ad1ff2022-02-10 15:48:39 +0100573 struct xmlpr_ctx pctx = {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 Vasko61ad1ff2022-02-10 15:48:39 +0100582 pctx.out = out;
583 pctx.level = 0;
584 pctx.options = options;
585 pctx.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 Vasko61ad1ff2022-02-10 15:48:39 +0100589 LY_CHECK_RET(xml_print_node(&pctx, 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 Vasko61ad1ff2022-02-10 15:48:39 +0100596 assert(!pctx.prefix.count && !pctx.ns.count);
597 ly_set_erase(&pctx.prefix, NULL);
598 ly_set_erase(&pctx.ns, NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200599 ly_print_flush(out);
600 return LY_SUCCESS;
601}