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