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