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 * |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 62 | xml_print_ns(struct xmlpr_ctx *ctx, 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 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 66 | for (i = ctx->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 */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 69 | if (!ctx->prefix.objs[i - 1]) { |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 70 | if (!strcmp(ctx->ns.objs[i - 1], ns)) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 71 | /* matching default namespace */ |
| 72 | return ctx->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 | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 79 | if (!strcmp(ctx->ns.objs[i - 1], ns)) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 80 | if (!ctx->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 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 85 | if (!strcmp(ctx->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 */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 87 | return ctx->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 */ |
| 94 | ly_print_(ctx->out, " xmlns%s%s=\"%s\"", new_prefix ? ":" : "", new_prefix ? new_prefix : "", ns); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 95 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 96 | /* and added into namespaces */ |
| 97 | if (new_prefix) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 98 | LY_CHECK_RET(lydict_insert(ctx->ctx, new_prefix, 0, &new_prefix), NULL); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 99 | } |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 100 | LY_CHECK_RET(ly_set_add(&ctx->prefix, (void *)new_prefix, 1, NULL), NULL); |
| 101 | LY_CHECK_RET(ly_set_add(&ctx->ns, (void *)ns, 1, &i), NULL); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 102 | |
| 103 | /* return it */ |
| 104 | return ctx->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 | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 108 | xml_print_ns_opaq(struct xmlpr_ctx *ctx, LY_PREFIX_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) { |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 111 | case LY_PREF_XML: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 112 | return xml_print_ns(ctx, 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; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 114 | case LY_PREF_JSON: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 115 | if (name->module_name) { |
| 116 | const struct lys_module *mod = ly_ctx_get_module_latest(ctx->ctx, name->module_name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 117 | if (mod) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 118 | return xml_print_ns(ctx, 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 */ |
| 124 | LOGINT(ctx->ctx); |
| 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. |
| 134 | * @param[in] format Value prefix format. |
| 135 | * @param[in] prefix_data Format-specific data to print: |
| 136 | * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports) |
| 137 | * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module) |
| 138 | * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns) |
| 139 | * LY_PREF_JSON - NULL |
| 140 | * @param[in] prefix_opts Prefix options changing the meaning of parameters. |
| 141 | * @return LY_ERR value. |
| 142 | */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 143 | static void |
| 144 | xml_print_ns_prefix_data(struct xmlpr_ctx *ctx, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t prefix_opts) |
| 145 | { |
| 146 | const struct ly_set *set; |
| 147 | const struct lyxml_ns *ns; |
| 148 | uint32_t i; |
| 149 | |
| 150 | switch (format) { |
| 151 | case LY_PREF_XML: |
| 152 | set = prefix_data; |
| 153 | for (i = 0; i < set->count; ++i) { |
| 154 | ns = set->objs[i]; |
| 155 | xml_print_ns(ctx, ns->uri, (prefix_opts & LYXML_PREFIX_DEFAULT) ? NULL : ns->prefix, prefix_opts); |
| 156 | } |
| 157 | break; |
| 158 | default: |
| 159 | /* cannot be created */ |
| 160 | LOGINT(ctx->ctx); |
| 161 | } |
| 162 | } |
| 163 | |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 164 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 165 | * TODO |
| 166 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 167 | static void |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 168 | xml_print_meta(struct xmlpr_ctx *ctx, const struct lyd_node *node) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 169 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 170 | struct lyd_meta *meta; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 171 | const struct lys_module *mod; |
| 172 | struct ly_set ns_list = {0}; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 173 | |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 174 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 175 | const char **prefs, **nss; |
| 176 | const char *xml_expr = NULL, *mod_name; |
| 177 | uint32_t ns_count, i; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 178 | ly_bool rpc_filter = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 179 | char *p; |
| 180 | size_t len; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 181 | #endif |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 182 | ly_bool dynamic; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 183 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 184 | /* with-defaults */ |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 185 | if (node->schema->nodetype & LYD_NODE_TERM) { |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 186 | if (((node->flags & LYD_DEFAULT) && (ctx->options & (LYD_PRINT_WD_ALL_TAG | LYD_PRINT_WD_IMPL_TAG))) || |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 187 | ((ctx->options & LYD_PRINT_WD_ALL_TAG) && lyd_is_default(node))) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 188 | /* we have implicit OR explicit default node, print attribute only if context include with-defaults schema */ |
| 189 | mod = ly_ctx_get_module_latest(node->schema->module->ctx, "ietf-netconf-with-defaults"); |
| 190 | if (mod) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 191 | ly_print_(ctx->out, " %s:default=\"true\"", xml_print_ns(ctx, mod->ns, mod->prefix, 0)); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 195 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 196 | /* technically, check for the extension get-filter-element-attributes from ietf-netconf */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 197 | if (!strcmp(node->schema->name, "filter") && |
| 198 | (!strcmp(node->schema->module->name, "ietf-netconf") || !strcmp(node->schema->module->name, "notifications"))) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 199 | rpc_filter = 1; |
| 200 | } |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 201 | #endif |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 202 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 203 | const char *value = meta->value.realtype->plugin->print(&meta->value, LY_PREF_XML, &ns_list, &dynamic); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 204 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 205 | /* print namespaces connected with the value's prefixes */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 206 | for (uint32_t u = 0; u < ns_list.count; ++u) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 207 | mod = (const struct lys_module *)ns_list.objs[u]; |
| 208 | xml_print_ns(ctx, mod->ns, mod->prefix, 1); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 209 | } |
| 210 | ly_set_erase(&ns_list, NULL); |
| 211 | |
| 212 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 213 | if (rpc_filter) { |
| 214 | /* exception for NETCONF's filter's attributes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 215 | if (!strcmp(meta->name, "select")) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 216 | /* xpath content, we have to convert the JSON format into XML first */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 217 | xml_expr = transform_json2xml(node->schema->module, meta->value_str, 0, &prefs, &nss, &ns_count); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 218 | if (!xml_expr) { |
| 219 | /* error */ |
| 220 | return EXIT_FAILURE; |
| 221 | } |
| 222 | |
| 223 | for (i = 0; i < ns_count; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 224 | ly_print_(out, " xmlns:%s=\"%s\"", prefs[i], nss[i]); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 225 | } |
| 226 | free(prefs); |
| 227 | free(nss); |
| 228 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 229 | ly_print_(out, " %s=\"", meta->name); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 230 | } else { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 231 | #endif |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 232 | /* print the metadata with its namespace */ |
| 233 | mod = meta->annotation->module; |
| 234 | ly_print_(ctx->out, " %s:%s=\"", xml_print_ns(ctx, mod->ns, mod->prefix, 1), meta->name); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 235 | #if 0 |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 236 | } |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 237 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 238 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 239 | /* print metadata value */ |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 240 | if (value && value[0]) { |
| 241 | lyxml_dump_text(ctx->out, value, 1); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 242 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 243 | ly_print_(ctx->out, "\""); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 244 | if (dynamic) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 245 | free((void *)value); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 246 | } |
| 247 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @brief Print generic XML element despite of the data node type. |
| 252 | * |
| 253 | * Prints the element name, attributes and necessary namespaces. |
| 254 | * |
| 255 | * @param[in] ctx XML printer context. |
| 256 | * @param[in] node Data node to be printed. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 257 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 258 | static void |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 259 | xml_print_node_open(struct xmlpr_ctx *ctx, const struct lyd_node *node) |
| 260 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 261 | /* print node name */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 262 | ly_print_(ctx->out, "%*s<%s", INDENT, node->schema->name); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 263 | |
| 264 | /* print default namespace */ |
| 265 | xml_print_ns(ctx, node->schema->module->ns, NULL, 0); |
| 266 | |
| 267 | /* print metadata */ |
| 268 | xml_print_meta(ctx, node); |
| 269 | } |
| 270 | |
| 271 | static LY_ERR |
| 272 | xml_print_attr(struct xmlpr_ctx *ctx, const struct lyd_node_opaq *node) |
| 273 | { |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 274 | const struct lyd_attr *attr; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 275 | const char *pref; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 276 | |
| 277 | LY_LIST_FOR(node->attr, attr) { |
| 278 | pref = NULL; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 279 | if (attr->name.prefix) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 280 | /* print attribute namespace */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 281 | pref = xml_print_ns_opaq(ctx, attr->format, &attr->name, 0); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* print namespaces connected with the value's prefixes */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 285 | if (attr->val_prefix_data) { |
| 286 | xml_print_ns_prefix_data(ctx, attr->format, attr->val_prefix_data, LYXML_PREFIX_REQUIRED); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* print the attribute with its prefix and value */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 290 | ly_print_(ctx->out, " %s%s%s=\"%s\"", pref ? pref : "", pref ? ":" : "", attr->name.name, attr->value); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 293 | return LY_SUCCESS; |
| 294 | } |
| 295 | |
| 296 | static LY_ERR |
| 297 | xml_print_opaq_open(struct xmlpr_ctx *ctx, const struct lyd_node_opaq *node) |
| 298 | { |
| 299 | /* print node name */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 300 | ly_print_(ctx->out, "%*s<%s", INDENT, node->name.name); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 301 | |
| 302 | /* print default namespace */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 303 | xml_print_ns_opaq(ctx, node->format, &node->name, LYXML_PREFIX_DEFAULT); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 304 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 305 | /* print attributes */ |
| 306 | LY_CHECK_RET(xml_print_attr(ctx, node)); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 307 | |
| 308 | return LY_SUCCESS; |
| 309 | } |
| 310 | |
| 311 | static LY_ERR xml_print_node(struct xmlpr_ctx *ctx, const struct lyd_node *node); |
| 312 | |
| 313 | /** |
| 314 | * @brief Print XML element representing lyd_node_term. |
| 315 | * |
| 316 | * @param[in] ctx XML printer context. |
| 317 | * @param[in] node Data node to be printed. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 318 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 319 | static void |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 320 | xml_print_term(struct xmlpr_ctx *ctx, const struct lyd_node_term *node) |
| 321 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 322 | struct ly_set ns_list = {0}; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 323 | ly_bool dynamic; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 324 | const char *value; |
| 325 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 326 | xml_print_node_open(ctx, &node->node); |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 327 | value = ((struct lysc_node_leaf *)node->schema)->type->plugin->print(&node->value, LY_PREF_XML, &ns_list, &dynamic); |
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 | /* print namespaces connected with the values's prefixes */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 330 | for (uint32_t u = 0; u < ns_list.count; ++u) { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 331 | const struct lys_module *mod = (const struct lys_module *)ns_list.objs[u]; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 332 | ly_print_(ctx->out, " xmlns:%s=\"%s\"", mod->prefix, mod->ns); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 333 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 334 | ly_set_erase(&ns_list, NULL); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 335 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 336 | if (!value || !value[0]) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 337 | ly_print_(ctx->out, "/>%s", DO_FORMAT ? "\n" : ""); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 338 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 339 | ly_print_(ctx->out, ">"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 340 | lyxml_dump_text(ctx->out, value, 0); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 341 | ly_print_(ctx->out, "</%s>%s", node->schema->name, DO_FORMAT ? "\n" : ""); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 342 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 343 | if (dynamic) { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 344 | free((void *)value); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 345 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
| 349 | * @brief Print XML element representing lyd_node_inner. |
| 350 | * |
| 351 | * @param[in] ctx XML printer context. |
| 352 | * @param[in] node Data node to be printed. |
| 353 | * @return LY_ERR value. |
| 354 | */ |
| 355 | static LY_ERR |
| 356 | xml_print_inner(struct xmlpr_ctx *ctx, const struct lyd_node_inner *node) |
| 357 | { |
| 358 | LY_ERR ret; |
| 359 | struct lyd_node *child; |
| 360 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 361 | xml_print_node_open(ctx, &node->node); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 362 | |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 363 | LY_LIST_FOR(node->child, child) { |
| 364 | if (ly_should_print(child, ctx->options)) { |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | if (!child) { |
| 369 | /* there are no children that will be printed */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 370 | ly_print_(ctx->out, "/>%s", DO_FORMAT ? "\n" : ""); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 371 | return LY_SUCCESS; |
| 372 | } |
| 373 | |
| 374 | /* children */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 375 | ly_print_(ctx->out, ">%s", DO_FORMAT ? "\n" : ""); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 376 | |
| 377 | LEVEL_INC; |
| 378 | LY_LIST_FOR(node->child, child) { |
| 379 | ret = xml_print_node(ctx, child); |
| 380 | LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret); |
| 381 | } |
| 382 | LEVEL_DEC; |
| 383 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 384 | ly_print_(ctx->out, "%*s</%s>%s", INDENT, node->schema->name, DO_FORMAT ? "\n" : ""); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 385 | |
| 386 | return LY_SUCCESS; |
| 387 | } |
| 388 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 389 | static LY_ERR |
| 390 | xml_print_anydata(struct xmlpr_ctx *ctx, const struct lyd_node_any *node) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 391 | { |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 392 | struct lyd_node_any *any = (struct lyd_node_any *)node; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 393 | struct lyd_node *iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 394 | uint32_t prev_opts, prev_lo; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 395 | LY_ERR ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 396 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 397 | xml_print_node_open(ctx, &node->node); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 398 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 399 | if (!any->value.tree) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 400 | /* no content */ |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 401 | no_content: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 402 | ly_print_(ctx->out, "/>%s", DO_FORMAT ? "\n" : ""); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 403 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 404 | } else { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 405 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 406 | /* turn logging off */ |
| 407 | prev_lo = ly_log_options(0); |
| 408 | |
| 409 | /* try to parse it into a data tree */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 410 | 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] | 411 | /* successfully parsed */ |
| 412 | free(any->value.mem); |
| 413 | any->value.tree = iter; |
| 414 | any->value_type = LYD_ANYDATA_DATATREE; |
| 415 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 416 | |
| 417 | /* turn loggin on again */ |
| 418 | ly_log_options(prev_lo); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 421 | switch (any->value_type) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 422 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 423 | /* close opening tag and print data */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 424 | prev_opts = ctx->options; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 425 | ctx->options &= ~LYD_PRINT_WITHSIBLINGS; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 426 | LEVEL_INC; |
| 427 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 428 | ly_print_(ctx->out, ">%s", DO_FORMAT ? "\n" : ""); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 429 | LY_LIST_FOR(any->value.tree, iter) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 430 | ret = xml_print_node(ctx, iter); |
| 431 | LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 432 | } |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 433 | |
| 434 | LEVEL_DEC; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 435 | ctx->options = prev_opts; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 436 | break; |
| 437 | case LYD_ANYDATA_STRING: |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 438 | /* escape XML-sensitive characters */ |
| 439 | if (!any->value.str[0]) { |
| 440 | goto no_content; |
| 441 | } |
| 442 | /* close opening tag and print data */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 443 | ly_print_(ctx->out, ">"); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 444 | lyxml_dump_text(ctx->out, any->value.str, 0); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 445 | break; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 446 | case LYD_ANYDATA_XML: |
| 447 | /* print without escaping special characters */ |
| 448 | if (!any->value.str[0]) { |
| 449 | goto no_content; |
| 450 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 451 | ly_print_(ctx->out, ">%s", any->value.str); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 452 | break; |
| 453 | case LYD_ANYDATA_JSON: |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 454 | case LYD_ANYDATA_LYB: |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 455 | /* JSON and LYB format is not supported */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 456 | LOGWRN(LYD_CTX(node), "Unable to print anydata content (type %d) as XML.", any->value_type); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 457 | goto no_content; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* closing tag */ |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 461 | if (any->value_type == LYD_ANYDATA_DATATREE) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 462 | ly_print_(ctx->out, "%*s</%s>%s", INDENT, node->schema->name, DO_FORMAT ? "\n" : ""); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 463 | } else { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 464 | ly_print_(ctx->out, "</%s>%s", node->schema->name, DO_FORMAT ? "\n" : ""); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 465 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 468 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 469 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 470 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 471 | static LY_ERR |
| 472 | xml_print_opaq(struct xmlpr_ctx *ctx, const struct lyd_node_opaq *node) |
| 473 | { |
| 474 | LY_ERR ret; |
| 475 | struct lyd_node *child; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 476 | |
| 477 | LY_CHECK_RET(xml_print_opaq_open(ctx, node)); |
| 478 | |
| 479 | if (node->value[0]) { |
| 480 | /* print namespaces connected with the value's prefixes */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 481 | if (node->val_prefix_data) { |
| 482 | xml_print_ns_prefix_data(ctx, node->format, node->val_prefix_data, LYXML_PREFIX_REQUIRED); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 485 | ly_print_(ctx->out, ">%s", node->value); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | if (node->child) { |
| 489 | /* children */ |
| 490 | if (!node->value[0]) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 491 | ly_print_(ctx->out, ">%s", DO_FORMAT ? "\n" : ""); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | LEVEL_INC; |
| 495 | LY_LIST_FOR(node->child, child) { |
| 496 | ret = xml_print_node(ctx, child); |
| 497 | LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret); |
| 498 | } |
| 499 | LEVEL_DEC; |
| 500 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 501 | ly_print_(ctx->out, "%*s</%s>%s", INDENT, node->name.name, DO_FORMAT ? "\n" : ""); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 502 | } else if (node->value[0]) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 503 | ly_print_(ctx->out, "</%s>%s", node->name.name, DO_FORMAT ? "\n" : ""); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 504 | } else { |
| 505 | /* no value or children */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 506 | ly_print_(ctx->out, "/>%s", DO_FORMAT ? "\n" : ""); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | return LY_SUCCESS; |
| 510 | } |
| 511 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 512 | /** |
| 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 | */ |
| 519 | static LY_ERR |
| 520 | xml_print_node(struct xmlpr_ctx *ctx, const struct lyd_node *node) |
| 521 | { |
| 522 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 523 | uint32_t ns_count; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 524 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 525 | if (!ly_should_print(node, ctx->options)) { |
| 526 | /* do not print at all */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 527 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 528 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 529 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 530 | /* remember namespace definition count on this level */ |
| 531 | ns_count = ctx->ns.count; |
| 532 | |
| 533 | if (!node->schema) { |
| 534 | ret = xml_print_opaq(ctx, (const struct lyd_node_opaq *)node); |
| 535 | } else { |
| 536 | switch (node->schema->nodetype) { |
| 537 | case LYS_CONTAINER: |
| 538 | case LYS_LIST: |
| 539 | case LYS_NOTIF: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 540 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 541 | case LYS_ACTION: |
| 542 | ret = xml_print_inner(ctx, (const struct lyd_node_inner *)node); |
| 543 | break; |
| 544 | case LYS_LEAF: |
| 545 | case LYS_LEAFLIST: |
| 546 | xml_print_term(ctx, (const struct lyd_node_term *)node); |
| 547 | break; |
| 548 | case LYS_ANYXML: |
| 549 | case LYS_ANYDATA: |
| 550 | ret = xml_print_anydata(ctx, (const struct lyd_node_any *)node); |
| 551 | break; |
| 552 | default: |
| 553 | LOGINT(node->schema->module->ctx); |
| 554 | ret = LY_EINT; |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /* remove all added namespaces */ |
| 560 | while (ns_count < ctx->ns.count) { |
| 561 | FREE_STRING(ctx->ctx, ctx->prefix.objs[ctx->prefix.count - 1]); |
| 562 | ly_set_rm_index(&ctx->prefix, ctx->prefix.count - 1, NULL); |
| 563 | ly_set_rm_index(&ctx->ns, ctx->ns.count - 1, NULL); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | return ret; |
| 567 | } |
| 568 | |
| 569 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 570 | 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] | 571 | { |
| 572 | const struct lyd_node *node; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 573 | struct xmlpr_ctx ctx = {0}; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 574 | |
| 575 | if (!root) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 576 | if ((out->type == LY_OUT_MEMORY) || (out->type == LY_OUT_CALLBACK)) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 577 | ly_print_(out, ""); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 578 | } |
| 579 | goto finish; |
| 580 | } |
| 581 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 582 | ctx.out = out; |
Radek Krejci | 17ede7a | 2020-11-13 10:38:30 +0100 | [diff] [blame] | 583 | ctx.level = 0; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 584 | ctx.options = options; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 585 | ctx.ctx = LYD_CTX(root); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 586 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 587 | /* content */ |
| 588 | LY_LIST_FOR(root, node) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 589 | LY_CHECK_RET(xml_print_node(&ctx, node)); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 590 | if (!(options & LYD_PRINT_WITHSIBLINGS)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | finish: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 596 | assert(!ctx.prefix.count && !ctx.ns.count); |
| 597 | ly_set_erase(&ctx.prefix, NULL); |
| 598 | ly_set_erase(&ctx.ns, NULL); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 599 | ly_print_flush(out); |
| 600 | return LY_SUCCESS; |
| 601 | } |