libyang  1.0.184
YANG data modeling language library
Xml.cpp
Go to the documentation of this file.
1 
15 #include <iostream>
16 #include <memory>
17 #include <stdexcept>
18 #include <vector>
19 
20 #include "Internal.hpp"
21 #include "Xml.hpp"
22 
23 extern "C" {
24 #include "libyang.h"
25 #include "xml.h"
26 }
27 
28 namespace libyang {
29 
30 Xml_Ns::Xml_Ns(const struct lyxml_ns *ns, S_Deleter deleter):
31  ns((struct lyxml_ns *) ns),
32  deleter(deleter)
33 {}
35 S_Xml_Ns Xml_Ns::next() LY_NEW(ns, next, Xml_Ns);
36 
37 Xml_Attr::Xml_Attr(struct lyxml_attr *attr, S_Deleter deleter):
38  attr(attr),
39  deleter(deleter)
40 {}
42 S_Xml_Attr Xml_Attr::next() LY_NEW(attr, next, Xml_Attr);
43 S_Xml_Ns Xml_Attr::ns() LY_NEW(attr, ns, Xml_Ns);
44 
45 Xml_Elem::Xml_Elem(S_Context context, struct lyxml_elem *elem, S_Deleter deleter):
46  context(context),
47  elem(elem),
48  deleter(deleter)
49 {}
51 S_Xml_Elem Xml_Elem::parent() {return elem->parent ? std::make_shared<Xml_Elem>(context, elem->parent, deleter) : nullptr;}
52 S_Xml_Attr Xml_Elem::attr() LY_NEW(elem, attr, Xml_Attr);
53 S_Xml_Elem Xml_Elem::child() {return elem->child ? std::make_shared<Xml_Elem>(context, elem->child, deleter) : nullptr;}
54 S_Xml_Elem Xml_Elem::next() {return elem->next ? std::make_shared<Xml_Elem>(context, elem->next, deleter) : nullptr;}
55 S_Xml_Elem Xml_Elem::prev() {return elem->prev ? std::make_shared<Xml_Elem>(context, elem->prev, deleter) : nullptr;}
56 S_Xml_Ns Xml_Elem::ns() LY_NEW(elem, ns, Xml_Ns);
57 const char *Xml_Elem::get_attr(const char *name, const char *ns) {
58  return lyxml_get_attr(elem, name, ns);
59 }
60 S_Xml_Ns Xml_Elem::get_ns(const char *prefix) {
61  const struct lyxml_ns *ns = lyxml_get_ns(elem, prefix);
62  return elem->ns ? std::make_shared<Xml_Ns>((struct lyxml_ns *)ns, deleter) : nullptr;
63 }
64 std::string Xml_Elem::print_mem(int options) {
65  char *data = nullptr;
66 
67  lyxml_print_mem(&data, (const struct lyxml_elem *) elem, options);
68  if (!data) {
69  return nullptr;
70  }
71 
72  std::string s_data = data;
73  free(data);
74  return s_data;
75 }
76 
77 std::vector<S_Xml_Elem> Xml_Elem::tree_for() {
78  std::vector<S_Xml_Elem> s_vector;
79 
80  struct lyxml_elem *elem = nullptr;
81  LY_TREE_FOR(elem, elem) {
82  s_vector.push_back(std::make_shared<Xml_Elem>(context, elem, deleter));
83  }
84 
85  return s_vector;
86 }
87 std::vector<S_Xml_Elem> Xml_Elem::tree_dfs() {
88  std::vector<S_Xml_Elem> s_vector;
89 
90  struct lyxml_elem *elem = nullptr, *next = nullptr;
91  LY_TREE_DFS_BEGIN(elem, next, elem) {
92  s_vector.push_back(std::make_shared<Xml_Elem>(context, elem, deleter));
93  LY_TREE_DFS_END(elem, next, elem)
94  }
95 
96  return s_vector;
97 }
98 
99 }
libyang::Xml_Attr::~Xml_Attr
~Xml_Attr()
Definition: Xml.cpp:41
lyxml_print_mem
int lyxml_print_mem(char **strp, const struct lyxml_elem *elem, int options)
Dump XML tree to a IO stream.
lyxml_elem::next
struct lyxml_elem * next
Definition: xml.h:100
lyxml_elem::parent
struct lyxml_elem * parent
Definition: xml.h:97
libyang::Xml_Elem::tree_for
std::vector< S_Xml_Elem > tree_for()
Definition: Xml.cpp:77
libyang
Definition: Libyang.hpp:30
lyxml_elem
Structure describing an element in an XML tree.
Definition: xml.h:92
xml.h
Public API of libyang XML parser.
libyang::Xml_Elem
Definition: Xml.hpp:86
lyxml_attr
Element's attribute definition.
Definition: xml.h:75
Xml.hpp
Class implementation for libyang C header xml.h.
libyang::Xml_Elem::~Xml_Elem
~Xml_Elem()
Definition: Xml.cpp:50
libyang.h
The main libyang public header.
lyxml_ns::prefix
const char * prefix
Definition: xml.h:62
libyang::Xml_Elem::child
S_Xml_Elem child()
Definition: Xml.cpp:53
libyang::Xml_Elem::prev
S_Xml_Elem prev()
Definition: Xml.cpp:55
libyang::Xml_Elem::parent
S_Xml_Elem parent()
Definition: Xml.cpp:51
libyang::Xml_Elem::ns
S_Xml_Ns ns()
Definition: Xml.cpp:56
libyang::Xml_Attr::next
S_Xml_Attr next()
Definition: Xml.cpp:42
lyxml_elem::prev
struct lyxml_elem * prev
Definition: xml.h:101
LY_TREE_DFS_BEGIN
#define LY_TREE_DFS_BEGIN(START, NEXT, ELEM)
Macro to iterate via all elements in a tree. This is the opening part to the LY_TREE_DFS_END - they a...
Definition: tree_schema.h:90
libyang::Xml_Elem::print_mem
std::string print_mem(int options)
Definition: Xml.cpp:64
libyang::Xml_Elem::get_ns
S_Xml_Ns get_ns(const char *prefix)
Definition: Xml.cpp:60
libyang::Xml_Ns::~Xml_Ns
~Xml_Ns()
Definition: Xml.cpp:34
LY_TREE_FOR
#define LY_TREE_FOR(START, ELEM)
Macro to iterate via all sibling elements without affecting the list itself.
Definition: tree_schema.h:40
LY_TREE_DFS_END
#define LY_TREE_DFS_END(START, NEXT, ELEM)
Definition: tree_schema.h:122
libyang::Xml_Elem::next
S_Xml_Elem next()
Definition: Xml.cpp:54
libyang::Xml_Elem::tree_dfs
std::vector< S_Xml_Elem > tree_dfs()
Definition: Xml.cpp:87
libyang::Xml_Attr
Definition: Xml.hpp:64
libyang::Xml_Elem::attr
S_Xml_Attr attr()
Definition: Xml.cpp:52
lyxml_get_attr
const char * lyxml_get_attr(const struct lyxml_elem *elem, const char *name, const char *ns)
Get value of the attribute in the specified element.
lyxml_elem::ns
const struct lyxml_ns * ns
Definition: xml.h:104
lyxml_ns
Namespace definition.
Definition: xml.h:58
libyang::Xml_Ns::next
S_Xml_Ns next()
Definition: Xml.cpp:35
lyxml_get_ns
const struct lyxml_ns * lyxml_get_ns(const struct lyxml_elem *elem, const char *prefix)
Get namespace definition of the given prefix in context of the specified element.
libyang::Xml_Ns
class for wrapping lyxml_ns.
Definition: Xml.hpp:43
libyang::Xml_Ns::Xml_Ns
Xml_Ns(const struct lyxml_ns *ns, S_Deleter deleter)
Definition: Xml.cpp:30