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