blob: 813d0321d9c298b819c670fc0a2f2eb9d5e8660b [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file printer_xml.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @author Radek Krejci <rkrejci@cesnet.cz>
5 * @brief XML printer for libyang data structure
6 *
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
16#include "common.h"
17
18#include <stdlib.h>
19#include <string.h>
20
21#include "log.h"
22#include "plugins_types.h"
23#include "printer_data.h"
24#include "printer_internal.h"
25#include "tree.h"
26#include "tree_data.h"
27#include "tree_schema.h"
28#include "xml.h"
29
30/**
31 * @brief XML printer context.
32 */
33struct xmlpr_ctx {
34 struct lyout *out; /**< output specification */
35 unsigned int level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
36 int options; /**< [Data printer flags](@ref dataprinterflags) */
37 int toplevel; /**< top-level flag */
38};
39
40#define LEVEL ctx->level /**< current level */
41#define INDENT ((LEVEL) ? (LEVEL)*2 : 0),"" /**< indentation parameters for printer functions */
42#define LEVEL_INC if (LEVEL) {LEVEL++;} /**< increase indentation level */
43#define LEVEL_DEC if (LEVEL) {LEVEL--;} /**< decrease indentation level */
44
45/**
46 * TODO
47 */
48struct mlist {
49 struct mlist *next;
50 struct lys_module *module;
51} *mlist = NULL, *mlist_new;
52
53#if 0
54static LY_ERR
55modlist_add(struct mlist **mlist, const struct lys_module *mod)
56{
57 struct mlist *iter;
58
59 for (iter = *mlist; iter; iter = iter->next) {
60 if (mod == iter->module) {
61 break;
62 }
63 }
64
65 if (!iter) {
66 iter = malloc(sizeof *iter);
67 LY_CHECK_ERR_RET(!iter, LOGMEM(mod->ctx), LY_EMEM);
68 iter->next = *mlist;
69 iter->module = (struct lys_module *)mod;
70 *mlist = iter;
71 }
72
73 return LY_SUCCESS;
74}
75#endif
76
77/**
78 * TODO
79 */
80static void
81xml_print_ns(struct xmlpr_ctx *ctx, const struct lyd_node *node)
82{
83 struct lyd_node *next, *cur, *child;
84 struct lyd_attr *attr;
Radek Krejcie7b95092019-05-15 11:03:07 +020085
86#if 0
Radek Krejci585f1922019-05-17 10:34:15 +020087 struct mlist *mlist = NULL, *miter;
88
Radek Krejcie7b95092019-05-15 11:03:07 +020089 const struct lys_module *wdmod = NULL;
90
91 /* add node attribute modules */
92 for (attr = node->attr; attr; attr = attr->next) {
93 if (!strcmp(node->schema->name, "filter") &&
94 (!strcmp(node->schema->module->name, "ietf-netconf") ||
95 !strcmp(node->schema->module->name, "notifications"))) {
96 /* exception for NETCONF's filter attributes */
97 continue;
98 } else {
99 r = modlist_add(&mlist, lys_main_module(attr->annotation->module));
100 }
101 if (r) {
102 goto print;
103 }
104 }
105#endif
106
107 /* add node children nodes and attribute modules */
108 switch (node->schema->nodetype) {
109 case LYS_LEAFLIST:
110 case LYS_LEAF:
111 /* TODO ietf-netconf-with-defaults namespace */
112#if 0
113 if (node->dflt && (options & (LYP_WD_ALL_TAG | LYP_WD_IMPL_TAG))) {
114 /* get with-defaults module and print its namespace */
115 wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL, 1);
116 if (wdmod && modlist_add(&mlist, wdmod)) {
117 goto print;
118 }
119 }
120#endif
121 break;
122 case LYS_CONTAINER:
123 case LYS_LIST:
124#if 0
125 case LYS_RPC:
126 case LYS_ACTION:
127 case LYS_NOTIF:
128 if (options & (LYP_WD_ALL_TAG | LYP_WD_IMPL_TAG)) {
129 /* get with-defaults module and print its namespace */
130 wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL, 1);
131 if (wdmod && modlist_add(&mlist, wdmod)) {
132 goto print;
133 }
134 }
135#endif
136 LY_LIST_FOR(((struct lyd_node_inner*)node)->child, child) {
137 LYD_TREE_DFS_BEGIN(child, next, cur) {
138 for (attr = cur->attr; attr; attr = attr->next) {
139 if (!strcmp(cur->schema->name, "filter") &&
140 (!strcmp(cur->schema->module->name, "ietf-netconf") ||
141 !strcmp(cur->schema->module->name, "notifications"))) {
142 /* exception for NETCONF's filter attributes */
143 continue;
144 } else {
145 /* TODO annotations r = modlist_add(&mlist, lys_main_module(attr->annotation->module)); */
146 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200147 }
148 LYD_TREE_DFS_END(child, next, cur)}
149 }
150 break;
151 default:
152 break;
153 }
Radek Krejci585f1922019-05-17 10:34:15 +0200154#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200155print:
156 /* print used namespaces */
157 while (mlist) {
158 miter = mlist;
159 mlist = mlist->next;
160
161 ly_print(ctx->out, " xmlns:%s=\"%s\"", miter->module->prefix, miter->module->ns);
162 free(miter);
163 }
Radek Krejci585f1922019-05-17 10:34:15 +0200164#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200165}
166
167/**
168 * TODO
169 */
170static LY_ERR
171xml_print_attrs(struct xmlpr_ctx *ctx, const struct lyd_node *node)
172{
173 (void) ctx;
174 (void) node;
175
176#if 0
177 struct lyd_attr *attr;
178 const char **prefs, **nss;
179 const char *xml_expr = NULL, *mod_name;
180 uint32_t ns_count, i;
181 int rpc_filter = 0;
182 const struct lys_module *wdmod = NULL;
183 char *p;
184 size_t len;
185
186 LY_PRINT_SET;
187
188 /* with-defaults */
189 if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
190 if ((node->dflt && (options & (LYP_WD_ALL_TAG | LYP_WD_IMPL_TAG))) ||
191 (!node->dflt && (options & LYP_WD_ALL_TAG) && lyd_wd_default((struct lyd_node_leaf_list *)node))) {
192 /* we have implicit OR explicit default node */
193 /* get with-defaults module */
194 wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL, 1);
195 if (wdmod) {
196 /* print attribute only if context include with-defaults schema */
197 ly_print(out, " %s:default=\"true\"", wdmod->prefix);
198 }
199 }
200 }
201 /* technically, check for the extension get-filter-element-attributes from ietf-netconf */
202 if (!strcmp(node->schema->name, "filter")
203 && (!strcmp(node->schema->module->name, "ietf-netconf") || !strcmp(node->schema->module->name, "notifications"))) {
204 rpc_filter = 1;
205 }
206
207 for (attr = node->attr; attr; attr = attr->next) {
208 if (rpc_filter) {
209 /* exception for NETCONF's filter's attributes */
210 if (!strcmp(attr->name, "select")) {
211 /* xpath content, we have to convert the JSON format into XML first */
212 xml_expr = transform_json2xml(node->schema->module, attr->value_str, 0, &prefs, &nss, &ns_count);
213 if (!xml_expr) {
214 /* error */
215 return EXIT_FAILURE;
216 }
217
218 for (i = 0; i < ns_count; ++i) {
219 ly_print(out, " xmlns:%s=\"%s\"", prefs[i], nss[i]);
220 }
221 free(prefs);
222 free(nss);
223 }
224 ly_print(out, " %s=\"", attr->name);
225 } else {
226 ly_print(out, " %s:%s=\"", attr->annotation->module->prefix, attr->name);
227 }
228
229 switch (attr->value_type) {
230 case LY_TYPE_BINARY:
231 case LY_TYPE_STRING:
232 case LY_TYPE_BITS:
233 case LY_TYPE_ENUM:
234 case LY_TYPE_BOOL:
235 case LY_TYPE_DEC64:
236 case LY_TYPE_INT8:
237 case LY_TYPE_INT16:
238 case LY_TYPE_INT32:
239 case LY_TYPE_INT64:
240 case LY_TYPE_UINT8:
241 case LY_TYPE_UINT16:
242 case LY_TYPE_UINT32:
243 case LY_TYPE_UINT64:
244 if (attr->value_str) {
245 /* xml_expr can contain transformed xpath */
246 lyxml_dump_text(out, xml_expr ? xml_expr : attr->value_str, LYXML_DATA_ATTR);
247 }
248 break;
249
250 case LY_TYPE_IDENT:
251 if (!attr->value_str) {
252 break;
253 }
254 p = strchr(attr->value_str, ':');
255 assert(p);
256 len = p - attr->value_str;
257 mod_name = attr->annotation->module->name;
258 if (!strncmp(attr->value_str, mod_name, len) && !mod_name[len]) {
259 lyxml_dump_text(out, ++p, LYXML_DATA_ATTR);
260 } else {
261 /* avoid code duplication - use instance-identifier printer which gets necessary namespaces to print */
262 goto printinst;
263 }
264 break;
265 case LY_TYPE_INST:
266printinst:
267 xml_expr = transform_json2xml(node->schema->module, ((struct lyd_node_leaf_list *)node)->value_str, 1,
268 &prefs, &nss, &ns_count);
269 if (!xml_expr) {
270 /* error */
271 return EXIT_FAILURE;
272 }
273
274 for (i = 0; i < ns_count; ++i) {
275 ly_print(out, " xmlns:%s=\"%s\"", prefs[i], nss[i]);
276 }
277 free(prefs);
278 free(nss);
279
280 lyxml_dump_text(out, xml_expr, LYXML_DATA_ATTR);
281 lydict_remove(node->schema->module->ctx, xml_expr);
282 break;
283
284 /* LY_TYPE_LEAFREF not allowed */
285 case LY_TYPE_EMPTY:
286 break;
287
288 default:
289 /* error */
290 LOGINT(node->schema->module->ctx);
291 return EXIT_FAILURE;
292 }
293
294 ly_print(out, "\"");
295
296 if (xml_expr) {
297 lydict_remove(node->schema->module->ctx, xml_expr);
298 }
299 }
300#endif
301
302 return LY_SUCCESS;
303}
304
305/**
306 * @brief Print generic XML element despite of the data node type.
307 *
308 * Prints the element name, attributes and necessary namespaces.
309 *
310 * @param[in] ctx XML printer context.
311 * @param[in] node Data node to be printed.
312 * @return LY_ERR value.
313 */
314static LY_ERR
315xml_print_node_open(struct xmlpr_ctx *ctx, const struct lyd_node *node)
316{
317 if (ctx->toplevel || !node->parent || node->schema->module != node->parent->schema->module) {
318 /* print "namespace" */
319 ly_print(ctx->out, "%*s<%s xmlns=\"%s\"", INDENT, node->schema->name, node->schema->module->ns);
320 } else {
321 ly_print(ctx->out, "%*s<%s", INDENT, node->schema->name);
322 }
323
324 if (ctx->toplevel) {
325 xml_print_ns(ctx, node);
326 ctx->toplevel = 0;
327 }
328
329 LY_CHECK_RET(xml_print_attrs(ctx, node));
330
331 return LY_SUCCESS;
332}
333
334static LY_ERR xml_print_node(struct xmlpr_ctx *ctx, const struct lyd_node *node);
335
336/**
337 * @brief Print XML element representing lyd_node_term.
338 *
339 * @param[in] ctx XML printer context.
340 * @param[in] node Data node to be printed.
341 * @return LY_ERR value.
342 */
343static LY_ERR
344xml_print_term(struct xmlpr_ctx *ctx, const struct lyd_node_term *node)
345{
346 LY_CHECK_RET(xml_print_node_open(ctx, (struct lyd_node *)node));
347
348 if (((struct lysc_node_leaf*)node->schema)->type->plugin->flags & LY_TYPE_FLAG_PREFIXES) {
349 /* TODO get prefixes from the value and print namespaces */
350 }
351
352 if (!node->value.canonized || !node->value.canonized[0]) {
353 ly_print(ctx->out, "/>%s", LEVEL ? "\n" : "");
354 } else {
355 ly_print(ctx->out, ">");
356 lyxml_dump_text(ctx->out, node->value.canonized, 0);
357 ly_print(ctx->out, "</%s>%s", node->schema->name, LEVEL ? "\n" : "");
358 }
359
360 return LY_SUCCESS;
361}
362
363/**
364 * @brief Print XML element representing lyd_node_inner.
365 *
366 * @param[in] ctx XML printer context.
367 * @param[in] node Data node to be printed.
368 * @return LY_ERR value.
369 */
370static LY_ERR
371xml_print_inner(struct xmlpr_ctx *ctx, const struct lyd_node_inner *node)
372{
373 LY_ERR ret;
374 struct lyd_node *child;
375
376 LY_CHECK_RET(xml_print_node_open(ctx, (struct lyd_node *)node));
377
378 if (!node->child) {
379 ly_print(ctx->out, "/>%s", ctx->level ? "\n" : "");
380 return LY_SUCCESS;
381 }
382
383 /* children */
384 ly_print(ctx->out, ">%s", ctx->level ? "\n" : "");
385
386 LEVEL_INC;
387 LY_LIST_FOR(node->child, child) {
388 ret = xml_print_node(ctx, child);
389 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
390 }
391 LEVEL_DEC;
392
393 ly_print(ctx->out, "%*s</%s>%s", INDENT, node->schema->name, LEVEL ? "\n" : "");
394
395 return LY_SUCCESS;
396}
397
398#if 0
399static int
400xml_print_anydata(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
401{
402 char *buf;
403 struct lyd_node_anydata *any = (struct lyd_node_anydata *)node;
404 struct lyd_node *iter;
405 const char *ns;
406
407 LY_PRINT_SET;
408
409 if (toplevel || !node->parent || nscmp(node, node->parent)) {
410 /* print "namespace" */
411 ns = lyd_node_module(node)->ns;
412 ly_print(out, "%*s<%s xmlns=\"%s\"", INDENT, node->schema->name, ns);
413 } else {
414 ly_print(out, "%*s<%s", INDENT, node->schema->name);
415 }
416
417 if (toplevel) {
418 xml_print_ns(out, node, options);
419 }
420 if (xml_print_attrs(out, node, options)) {
421 return EXIT_FAILURE;
422 }
423 if (!(void*)any->value.tree || (any->value_type == LYD_ANYDATA_CONSTSTRING && !any->value.str[0])) {
424 /* no content */
425 ly_print(out, "/>%s", level ? "\n" : "");
426 } else {
427 if (any->value_type == LYD_ANYDATA_DATATREE) {
428 /* print namespaces in the anydata data tree */
429 LY_TREE_FOR(any->value.tree, iter) {
430 xml_print_ns(out, iter, options);
431 }
432 }
433 /* close opening tag ... */
434 ly_print(out, ">");
435 /* ... and print anydata content */
436 switch (any->value_type) {
437 case LYD_ANYDATA_CONSTSTRING:
438 lyxml_dump_text(out, any->value.str, LYXML_DATA_ELEM);
439 break;
440 case LYD_ANYDATA_DATATREE:
441 if (any->value.tree) {
442 if (level) {
443 ly_print(out, "\n");
444 }
445 LY_TREE_FOR(any->value.tree, iter) {
446 if (xml_print_node(out, level ? level + 1 : 0, iter, 0, (options & ~(LYP_WITHSIBLINGS | LYP_NETCONF)))) {
447 return EXIT_FAILURE;
448 }
449 }
450 }
451 break;
452 case LYD_ANYDATA_XML:
453 lyxml_print_mem(&buf, any->value.xml, (level ? LYXML_PRINT_FORMAT | LYXML_PRINT_NO_LAST_NEWLINE : 0)
454 | LYXML_PRINT_SIBLINGS);
455 ly_print(out, "%s%s", level ? "\n" : "", buf);
456 free(buf);
457 break;
458 case LYD_ANYDATA_SXML:
459 /* print without escaping special characters */
460 ly_print(out, "%s", any->value.str);
461 break;
462 case LYD_ANYDATA_JSON:
463 case LYD_ANYDATA_LYB:
464 /* JSON and LYB format is not supported */
465 LOGWRN(node->schema->module->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
466 break;
467 case LYD_ANYDATA_STRING:
468 case LYD_ANYDATA_SXMLD:
469 case LYD_ANYDATA_JSOND:
470 case LYD_ANYDATA_LYBD:
471 /* dynamic strings are used only as input parameters */
472 assert(0);
473 break;
474 }
475
476 /* closing tag */
477 ly_print(out, "</%s>%s", node->schema->name, level ? "\n" : "");
478 }
479
480 LY_PRINT_RET(node->schema->module->ctx);
481}
482#endif
483
484/**
485 * @brief Print XML element representing lyd_node.
486 *
487 * @param[in] ctx XML printer context.
488 * @param[in] node Data node to be printed.
489 * @return LY_ERR value.
490 */
491static LY_ERR
492xml_print_node(struct xmlpr_ctx *ctx, const struct lyd_node *node)
493{
494 LY_ERR ret = LY_SUCCESS;
495
496#if 0
497 if (!lyd_wd_toprint(node, ctx->options)) {
498 /* wd says do not print */
499 return EXIT_SUCCESS;
500 }
501#endif
502
503 switch (node->schema->nodetype) {
504#if 0
505 case LYS_NOTIF:
506 case LYS_ACTION:
507#endif
508 case LYS_CONTAINER:
509 case LYS_LIST:
510 ret = xml_print_inner(ctx, (const struct lyd_node_inner*)node);
511 break;
512 case LYS_LEAF:
513 case LYS_LEAFLIST:
514 ret = xml_print_term(ctx, (const struct lyd_node_term*)node);
515 break;
516#if 0
517 case LYS_ANYXML:
518 case LYS_ANYDATA:
519 ret = xml_print_anydata(ctx, node);
520 break;
521#endif
522 default:
523 LOGINT(node->schema->module->ctx);
524 ret = LY_EINT;
525 break;
526 }
527
528 return ret;
529}
530
531LY_ERR
532xml_print_data(struct lyout *out, const struct lyd_node *root, int options)
533{
534 const struct lyd_node *node;
535 struct xmlpr_ctx ctx_ = {.out = out, .level = (options & LYDP_FORMAT ? 1 : 0), .options = options, .toplevel = 1}, *ctx = &ctx_;
536
537 if (!root) {
538 if (out->type == LYOUT_MEMORY || out->type == LYOUT_CALLBACK) {
539 ly_print(out, "");
540 }
541 goto finish;
542 }
543
544 /* content */
545 LY_LIST_FOR(root, node) {
546 if (xml_print_node(ctx, node)) {
547 return EXIT_FAILURE;
548 }
549 if (!(options & LYDP_WITHSIBLINGS)) {
550 break;
551 }
552 }
553
554finish:
555 ly_print_flush(out);
556 return LY_SUCCESS;
557}
558