1 /**
2  * @file Xml.hpp
3  * @author Mislav Novakovic <mislav.novakovic@sartura.hr>
4  * @brief Class implementation for libyang C header xml.h.
5  *
6  * Copyright (c) 2017 Deutsche Telekom AG.
7  *
8  * This source code is licensed under BSD 3-Clause License (the "License").
9  * You may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     https://opensource.org/licenses/BSD-3-Clause
13  */
14 
15 #ifndef XML_H
16 #define XML_H
17 
18 #include <iostream>
19 #include <memory>
20 #include <exception>
21 #include <vector>
22 
23 #include "Internal.hpp"
24 
25 extern "C" {
26 #include "libyang.h"
27 #include "xml.h"
28 }
29 
30 namespace libyang {
31 
32 /**
33  * @defgroup classes C++/Python
34  * @{
35  *
36  * Class wrappers for data structures and functions to manipulate and access instance data tree.
37  */
38 
39 /**
40  * @brief class for wrapping [lyxml_ns](@ref lyxml_ns).
41  * @class Xml_Ns
42  */
43 class Xml_Ns
44 {
45 public:
46     /** wrapper for struct [lyxml_ns](@ref lyxml_ns), for internal use only */
47     Xml_Ns(const struct lyxml_ns *ns, S_Deleter deleter);
48     ~Xml_Ns();
49     /** get type variable from [lyxml_ns](@ref lyxml_ns)*/
type()50     LYXML_ATTR_TYPE type() {return ns->type;};
51     /** get next variable from [lyxml_ns](@ref lyxml_ns)*/
52     S_Xml_Ns next();
53     //struct lyxml_elem *parent;       /**< parent node of the attribute */
54     /** get prefix variable from [lyxml_ns](@ref lyxml_ns)*/
prefix()55     const char *prefix() {return ns->prefix;};
56     /** get value variable from [lyxml_ns](@ref lyxml_ns)*/
value()57     const char *value() {return ns->value;};
58 
59 private:
60     struct lyxml_ns *ns;
61     S_Deleter deleter;
62 };
63 
64 class Xml_Attr
65 {
66 public:
67     /** wrapper for struct [lyxml_attr](@ref lyxml_attr), for internal use only */
68     Xml_Attr(struct lyxml_attr *attr, S_Deleter deleter);
69     ~Xml_Attr();
70     /** get type variable from [lyxml_attr](@ref lyxml_attr)*/
type()71     LYXML_ATTR_TYPE type() {return attr->type;};
72     /** get next variable from [lyxml_attr](@ref lyxml_attr)*/
73     S_Xml_Attr next();
74     /** get ns variable from [lyxml_attr](@ref lyxml_attr)*/
75     S_Xml_Ns ns();
76     /** get name variable from [lyxml_attr](@ref lyxml_attr)*/
name()77     const char *name() {return attr->name;};
78     /** get value variable from [lyxml_attr](@ref lyxml_attr)*/
value()79     const char *value() {return attr->value;};
80 
81 private:
82     struct lyxml_attr *attr;
83     S_Deleter deleter;
84 };
85 
86 class Xml_Elem
87 {
88 public:
89     /** wrapper for struct [lyxml_elem](@ref lyxml_elem), for internal use only */
90     Xml_Elem(S_Context context, struct lyxml_elem *elem, S_Deleter deleter);
91     ~Xml_Elem();
92     /** get flags variable from [lyxml_elem](@ref lyxml_elem)*/
flags()93     char flags() {return elem->flags;};
94     /** get parent variable from [lyxml_elem](@ref lyxml_elem)*/
95     S_Xml_Elem parent();
96     /** get attr variable from [lyxml_elem](@ref lyxml_elem)*/
97     S_Xml_Attr attr();
98     /** get child variable from [lyxml_elem](@ref lyxml_elem)*/
99     S_Xml_Elem child();
100     /** get next variable from [lyxml_elem](@ref lyxml_elem)*/
101     S_Xml_Elem next();
102     /** get prev variable from [lyxml_elem](@ref lyxml_elem)*/
103     S_Xml_Elem prev();
104     /** get name variable from [lyxml_elem](@ref lyxml_elem)*/
name()105     const char *name() {return elem->name;};
106     /** get ns variable from [lyxml_elem](@ref lyxml_elem)*/
107     S_Xml_Ns ns();
108     /** get content variable from [lyxml_elem](@ref lyxml_elem)*/
content()109     const char *content() {return elem->content;};
110 
111     /* methods */
112     /** wrapper for [lyxml_get_attr](@ref lyxml_get_attr) */
113     const char *get_attr(const char *name, const char *ns = nullptr);
114     /** wrapper for [lyxml_get_ns](@ref lyxml_get_ns) */
115     S_Xml_Ns get_ns(const char *prefix);
116     /** wrapper for [lyxml_print_mem](@ref lyxml_print_mem) */
117     std::string print_mem(int options);
118     //int lyxml_print_fd(int fd, const struct lyxml_elem *elem, int options);
119     //int lyxml_print_file(FILE * stream, const struct lyxml_elem *elem, int options);
120 
121     /* emulate TREE macro's */
122     /** wrapper for macro [LY_TREE_FOR](@ref LY_TREE_FOR) */
123     std::vector<S_Xml_Elem> tree_for();
124     /** wrapper for macro [LY_TREE_DFS_BEGIN](@ref LY_TREE_DFS_BEGIN) and [LY_TREE_DFS_END](@ref LY_TREE_DFS_END) */
125     std::vector<S_Xml_Elem> tree_dfs();
126 
127     /* TODO
128     struct lyxml_elem *lyxml_dup(struct ly_ctx *ctx, struct lyxml_elem *root);
129     struct lyxml_elem *lyxml_parse_mem(struct ly_ctx *ctx, const char *data, int options);
130     struct lyxml_elem *lyxml_parse_path(struct ly_ctx *ctx, const char *filename, int options);
131     */
132 
133     friend Data_Node;
134     friend Context;
135 
136 private:
137     S_Context context;
138     struct lyxml_elem *elem;
139     S_Deleter deleter;
140 };
141 
142 /**@} */
143 
144 }
145 
146 #endif
147