blob: 52520384cff1f07b2602daaf97c6641e1c5db354 [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;
85 struct mlist *mlist = NULL, *miter;
86 int r = 0;
87
88#if 0
89 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 }
154
155print:
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 }
164}
165
166/**
167 * TODO
168 */
169static LY_ERR
170xml_print_attrs(struct xmlpr_ctx *ctx, const struct lyd_node *node)
171{
172 (void) ctx;
173 (void) node;
174
175#if 0
176 struct lyd_attr *attr;
177 const char **prefs, **nss;
178 const char *xml_expr = NULL, *mod_name;
179 uint32_t ns_count, i;
180 int rpc_filter = 0;
181 const struct lys_module *wdmod = NULL;
182 char *p;
183 size_t len;
184
185 LY_PRINT_SET;
186
187 /* with-defaults */
188 if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
189 if ((node->dflt && (options & (LYP_WD_ALL_TAG | LYP_WD_IMPL_TAG))) ||
190 (!node->dflt && (options & LYP_WD_ALL_TAG) && lyd_wd_default((struct lyd_node_leaf_list *)node))) {
191 /* we have implicit OR explicit default node */
192 /* get with-defaults module */
193 wdmod = ly_ctx_get_module(node->schema->module->ctx, "ietf-netconf-with-defaults", NULL, 1);
194 if (wdmod) {
195 /* print attribute only if context include with-defaults schema */
196 ly_print(out, " %s:default=\"true\"", wdmod->prefix);
197 }
198 }
199 }
200 /* technically, check for the extension get-filter-element-attributes from ietf-netconf */
201 if (!strcmp(node->schema->name, "filter")
202 && (!strcmp(node->schema->module->name, "ietf-netconf") || !strcmp(node->schema->module->name, "notifications"))) {
203 rpc_filter = 1;
204 }
205
206 for (attr = node->attr; attr; attr = attr->next) {
207 if (rpc_filter) {
208 /* exception for NETCONF's filter's attributes */
209 if (!strcmp(attr->name, "select")) {
210 /* xpath content, we have to convert the JSON format into XML first */
211 xml_expr = transform_json2xml(node->schema->module, attr->value_str, 0, &prefs, &nss, &ns_count);
212 if (!xml_expr) {
213 /* error */
214 return EXIT_FAILURE;
215 }
216
217 for (i = 0; i < ns_count; ++i) {
218 ly_print(out, " xmlns:%s=\"%s\"", prefs[i], nss[i]);
219 }
220 free(prefs);
221 free(nss);
222 }
223 ly_print(out, " %s=\"", attr->name);
224 } else {
225 ly_print(out, " %s:%s=\"", attr->annotation->module->prefix, attr->name);
226 }
227
228 switch (attr->value_type) {
229 case LY_TYPE_BINARY:
230 case LY_TYPE_STRING:
231 case LY_TYPE_BITS:
232 case LY_TYPE_ENUM:
233 case LY_TYPE_BOOL:
234 case LY_TYPE_DEC64:
235 case LY_TYPE_INT8:
236 case LY_TYPE_INT16:
237 case LY_TYPE_INT32:
238 case LY_TYPE_INT64:
239 case LY_TYPE_UINT8:
240 case LY_TYPE_UINT16:
241 case LY_TYPE_UINT32:
242 case LY_TYPE_UINT64:
243 if (attr->value_str) {
244 /* xml_expr can contain transformed xpath */
245 lyxml_dump_text(out, xml_expr ? xml_expr : attr->value_str, LYXML_DATA_ATTR);
246 }
247 break;
248
249 case LY_TYPE_IDENT:
250 if (!attr->value_str) {
251 break;
252 }
253 p = strchr(attr->value_str, ':');
254 assert(p);
255 len = p - attr->value_str;
256 mod_name = attr->annotation->module->name;
257 if (!strncmp(attr->value_str, mod_name, len) && !mod_name[len]) {
258 lyxml_dump_text(out, ++p, LYXML_DATA_ATTR);
259 } else {
260 /* avoid code duplication - use instance-identifier printer which gets necessary namespaces to print */
261 goto printinst;
262 }
263 break;
264 case LY_TYPE_INST:
265printinst:
266 xml_expr = transform_json2xml(node->schema->module, ((struct lyd_node_leaf_list *)node)->value_str, 1,
267 &prefs, &nss, &ns_count);
268 if (!xml_expr) {
269 /* error */
270 return EXIT_FAILURE;
271 }
272
273 for (i = 0; i < ns_count; ++i) {
274 ly_print(out, " xmlns:%s=\"%s\"", prefs[i], nss[i]);
275 }
276 free(prefs);
277 free(nss);
278
279 lyxml_dump_text(out, xml_expr, LYXML_DATA_ATTR);
280 lydict_remove(node->schema->module->ctx, xml_expr);
281 break;
282
283 /* LY_TYPE_LEAFREF not allowed */
284 case LY_TYPE_EMPTY:
285 break;
286
287 default:
288 /* error */
289 LOGINT(node->schema->module->ctx);
290 return EXIT_FAILURE;
291 }
292
293 ly_print(out, "\"");
294
295 if (xml_expr) {
296 lydict_remove(node->schema->module->ctx, xml_expr);
297 }
298 }
299#endif
300
301 return LY_SUCCESS;
302}
303
304/**
305 * @brief Print generic XML element despite of the data node type.
306 *
307 * Prints the element name, attributes and necessary namespaces.
308 *
309 * @param[in] ctx XML printer context.
310 * @param[in] node Data node to be printed.
311 * @return LY_ERR value.
312 */
313static LY_ERR
314xml_print_node_open(struct xmlpr_ctx *ctx, const struct lyd_node *node)
315{
316 if (ctx->toplevel || !node->parent || node->schema->module != node->parent->schema->module) {
317 /* print "namespace" */
318 ly_print(ctx->out, "%*s<%s xmlns=\"%s\"", INDENT, node->schema->name, node->schema->module->ns);
319 } else {
320 ly_print(ctx->out, "%*s<%s", INDENT, node->schema->name);
321 }
322
323 if (ctx->toplevel) {
324 xml_print_ns(ctx, node);
325 ctx->toplevel = 0;
326 }
327
328 LY_CHECK_RET(xml_print_attrs(ctx, node));
329
330 return LY_SUCCESS;
331}
332
333static LY_ERR xml_print_node(struct xmlpr_ctx *ctx, const struct lyd_node *node);
334
335/**
336 * @brief Print XML element representing lyd_node_term.
337 *
338 * @param[in] ctx XML printer context.
339 * @param[in] node Data node to be printed.
340 * @return LY_ERR value.
341 */
342static LY_ERR
343xml_print_term(struct xmlpr_ctx *ctx, const struct lyd_node_term *node)
344{
345 LY_CHECK_RET(xml_print_node_open(ctx, (struct lyd_node *)node));
346
347 if (((struct lysc_node_leaf*)node->schema)->type->plugin->flags & LY_TYPE_FLAG_PREFIXES) {
348 /* TODO get prefixes from the value and print namespaces */
349 }
350
351 if (!node->value.canonized || !node->value.canonized[0]) {
352 ly_print(ctx->out, "/>%s", LEVEL ? "\n" : "");
353 } else {
354 ly_print(ctx->out, ">");
355 lyxml_dump_text(ctx->out, node->value.canonized, 0);
356 ly_print(ctx->out, "</%s>%s", node->schema->name, LEVEL ? "\n" : "");
357 }
358
359 return LY_SUCCESS;
360}
361
362/**
363 * @brief Print XML element representing lyd_node_inner.
364 *
365 * @param[in] ctx XML printer context.
366 * @param[in] node Data node to be printed.
367 * @return LY_ERR value.
368 */
369static LY_ERR
370xml_print_inner(struct xmlpr_ctx *ctx, const struct lyd_node_inner *node)
371{
372 LY_ERR ret;
373 struct lyd_node *child;
374
375 LY_CHECK_RET(xml_print_node_open(ctx, (struct lyd_node *)node));
376
377 if (!node->child) {
378 ly_print(ctx->out, "/>%s", ctx->level ? "\n" : "");
379 return LY_SUCCESS;
380 }
381
382 /* children */
383 ly_print(ctx->out, ">%s", ctx->level ? "\n" : "");
384
385 LEVEL_INC;
386 LY_LIST_FOR(node->child, child) {
387 ret = xml_print_node(ctx, child);
388 LY_CHECK_ERR_RET(ret, LEVEL_DEC, ret);
389 }
390 LEVEL_DEC;
391
392 ly_print(ctx->out, "%*s</%s>%s", INDENT, node->schema->name, LEVEL ? "\n" : "");
393
394 return LY_SUCCESS;
395}
396
397#if 0
398static int
399xml_print_anydata(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
400{
401 char *buf;
402 struct lyd_node_anydata *any = (struct lyd_node_anydata *)node;
403 struct lyd_node *iter;
404 const char *ns;
405
406 LY_PRINT_SET;
407
408 if (toplevel || !node->parent || nscmp(node, node->parent)) {
409 /* print "namespace" */
410 ns = lyd_node_module(node)->ns;
411 ly_print(out, "%*s<%s xmlns=\"%s\"", INDENT, node->schema->name, ns);
412 } else {
413 ly_print(out, "%*s<%s", INDENT, node->schema->name);
414 }
415
416 if (toplevel) {
417 xml_print_ns(out, node, options);
418 }
419 if (xml_print_attrs(out, node, options)) {
420 return EXIT_FAILURE;
421 }
422 if (!(void*)any->value.tree || (any->value_type == LYD_ANYDATA_CONSTSTRING && !any->value.str[0])) {
423 /* no content */
424 ly_print(out, "/>%s", level ? "\n" : "");
425 } else {
426 if (any->value_type == LYD_ANYDATA_DATATREE) {
427 /* print namespaces in the anydata data tree */
428 LY_TREE_FOR(any->value.tree, iter) {
429 xml_print_ns(out, iter, options);
430 }
431 }
432 /* close opening tag ... */
433 ly_print(out, ">");
434 /* ... and print anydata content */
435 switch (any->value_type) {
436 case LYD_ANYDATA_CONSTSTRING:
437 lyxml_dump_text(out, any->value.str, LYXML_DATA_ELEM);
438 break;
439 case LYD_ANYDATA_DATATREE:
440 if (any->value.tree) {
441 if (level) {
442 ly_print(out, "\n");
443 }
444 LY_TREE_FOR(any->value.tree, iter) {
445 if (xml_print_node(out, level ? level + 1 : 0, iter, 0, (options & ~(LYP_WITHSIBLINGS | LYP_NETCONF)))) {
446 return EXIT_FAILURE;
447 }
448 }
449 }
450 break;
451 case LYD_ANYDATA_XML:
452 lyxml_print_mem(&buf, any->value.xml, (level ? LYXML_PRINT_FORMAT | LYXML_PRINT_NO_LAST_NEWLINE : 0)
453 | LYXML_PRINT_SIBLINGS);
454 ly_print(out, "%s%s", level ? "\n" : "", buf);
455 free(buf);
456 break;
457 case LYD_ANYDATA_SXML:
458 /* print without escaping special characters */
459 ly_print(out, "%s", any->value.str);
460 break;
461 case LYD_ANYDATA_JSON:
462 case LYD_ANYDATA_LYB:
463 /* JSON and LYB format is not supported */
464 LOGWRN(node->schema->module->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
465 break;
466 case LYD_ANYDATA_STRING:
467 case LYD_ANYDATA_SXMLD:
468 case LYD_ANYDATA_JSOND:
469 case LYD_ANYDATA_LYBD:
470 /* dynamic strings are used only as input parameters */
471 assert(0);
472 break;
473 }
474
475 /* closing tag */
476 ly_print(out, "</%s>%s", node->schema->name, level ? "\n" : "");
477 }
478
479 LY_PRINT_RET(node->schema->module->ctx);
480}
481#endif
482
483/**
484 * @brief Print XML element representing lyd_node.
485 *
486 * @param[in] ctx XML printer context.
487 * @param[in] node Data node to be printed.
488 * @return LY_ERR value.
489 */
490static LY_ERR
491xml_print_node(struct xmlpr_ctx *ctx, const struct lyd_node *node)
492{
493 LY_ERR ret = LY_SUCCESS;
494
495#if 0
496 if (!lyd_wd_toprint(node, ctx->options)) {
497 /* wd says do not print */
498 return EXIT_SUCCESS;
499 }
500#endif
501
502 switch (node->schema->nodetype) {
503#if 0
504 case LYS_NOTIF:
505 case LYS_ACTION:
506#endif
507 case LYS_CONTAINER:
508 case LYS_LIST:
509 ret = xml_print_inner(ctx, (const struct lyd_node_inner*)node);
510 break;
511 case LYS_LEAF:
512 case LYS_LEAFLIST:
513 ret = xml_print_term(ctx, (const struct lyd_node_term*)node);
514 break;
515#if 0
516 case LYS_ANYXML:
517 case LYS_ANYDATA:
518 ret = xml_print_anydata(ctx, node);
519 break;
520#endif
521 default:
522 LOGINT(node->schema->module->ctx);
523 ret = LY_EINT;
524 break;
525 }
526
527 return ret;
528}
529
530LY_ERR
531xml_print_data(struct lyout *out, const struct lyd_node *root, int options)
532{
533 const struct lyd_node *node;
534 struct xmlpr_ctx ctx_ = {.out = out, .level = (options & LYDP_FORMAT ? 1 : 0), .options = options, .toplevel = 1}, *ctx = &ctx_;
535
536 if (!root) {
537 if (out->type == LYOUT_MEMORY || out->type == LYOUT_CALLBACK) {
538 ly_print(out, "");
539 }
540 goto finish;
541 }
542
543 /* content */
544 LY_LIST_FOR(root, node) {
545 if (xml_print_node(ctx, node)) {
546 return EXIT_FAILURE;
547 }
548 if (!(options & LYDP_WITHSIBLINGS)) {
549 break;
550 }
551 }
552
553finish:
554 ly_print_flush(out);
555 return LY_SUCCESS;
556}
557