OpenSDN source code
xml_pugi.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <boost/algorithm/string/replace.hpp>
7 #include "xml/xml_base.h"
8 #include "xml/xml_pugi.h"
9 #include "base/logging.h"
10 
11 // This is internal implementation detail specific to this lib to make
12 // exposed methods efficient.
13 
14 //XmlPugi::tmp_("");
15 pugi::xml_attribute XmlPugi::GAttr;
16 pugi::xml_node XmlPugi::GNode;
17 
18 XmlPugi::XmlPugi() : writer_(this), node_(GNode), attrib_(GAttr) {
19 }
20 
22  doc_.reset();
23 }
24 
25 pugi::xml_node XmlPugi::RootNode() {
26  return(doc_.root());
27 }
28 
29 const char *XmlPugi::ReadNode(const std::string &name) {
30  PugiPredicate p1(name);
31 
32  pugi::xml_node node = doc_.find_node(p1);
33  if (IsNull(node)) {
34  return NULL;
35  }
36 
37  SetContext(node);
38  if (node.type() != pugi::node_pi) {
39  for (node = node.first_child(); node; node = node.next_sibling()) {
40  // traverse to first plain character data or char data,
41  // skipping XML comments
42  if (node.type() == pugi::node_pcdata ||
43  node.type() == pugi::node_cdata)
44  break;
45  }
46  }
47 
48  return node.value();
49 
50 }
51 
52 pugi::xml_node XmlPugi::FindNode(const std::string &name) {
53  PugiPredicate p1(name);
54 
55  pugi::xml_node node = doc_.find_node(p1);
56  return node;
57 }
58 
59 const char *XmlPugi::ReadNodeName(const std::string &name) {
60  PugiPredicate p1(name);
61 
62  pugi::xml_node node = doc_.find_node(p1);
63  SetContext(node);
64 
65  return node.name();
66 }
67 
68 const char *XmlPugi::ReadNodeValue() {
69  pugi::xml_node node = node_;
70 
71  if (node.type() != pugi::node_pi) {
72  for (node = node.first_child(); node; node = node.next_sibling()) {
73  // traverse to first plain character data or char data,
74  // skipping XML comments
75  if (node.type() == pugi::node_pcdata ||
76  node.type() == pugi::node_cdata)
77  break;
78  }
79  }
80  return node.value();
81 }
82 
83 const char *XmlPugi::ReadChildNode() {
84  pugi::xml_node tmp = (node_ == GNode) ? doc_.first_child() :
85  node_.first_child();
86  SetContext(tmp);
87  return tmp.value();
88 }
89 
91  pugi::xml_node tmp = (node_ == GNode) ? doc_.first_child() :
92  node_.first_child();
93  SetContext(tmp);
94  return tmp.name();
95 }
96 
97 const char *XmlPugi::ReadNextNode() {
98  pugi::xml_node tmp = node_.next_sibling();
99 
100  SetContext(tmp);
101  return tmp.value();
102 }
103 
105  pugi::xml_node tmp = node_.next_sibling();
106 
107  SetContext(tmp);
108  return tmp.name();
109 }
110 
112  SetContext(node_);
113 }
114 
115 const char *XmlPugi::ReadAttrib(const std::string &str) {
116  pugi::xml_attribute tmp = node_.attribute(str.c_str());
117  SetContext(node_, tmp);
118  return tmp.value();
119 }
120 
122  pugi::xml_attribute tmp = node_.first_attribute();
123  SetContext(node_, tmp);
124  return tmp.value();
125 }
126 
127 const char *XmlPugi::ReadNextAttrib() {
128  pugi::xml_attribute tmp = attrib_.next_attribute();
129  SetContext(node_, tmp);
130  return tmp.value();
131 }
132 
135 }
136 
137 const char *XmlPugi::ReadParentName() {
138  pugi::xml_node tmp = node_.parent();
139  SetContext(tmp);
140  return tmp.name();
141 }
142 
143 int XmlPugi::WriteDoc(uint8_t *buf) {
144  buf_tmp_ = buf; // this will be used by writer_->write
145  ts_ = 0;
146  doc_.save(writer_, "", pugi::format_default, pugi::encoding_utf8);
147 
148  if (ts_ == 0) return -1;
149  return static_cast<int>(ts_);
150 }
151 
152 int XmlPugi::WriteRawDoc(uint8_t *buf) {
153  buf_tmp_ = buf; // this will be used by writer_->write
154  ts_ = 0;
155  doc_.save(writer_, "", pugi::format_raw | pugi::format_no_declaration);
156 
157  if (ts_ == 0) return -1;
158  return static_cast<int>(ts_);
159 }
160 
161 void XmlPugi::PrintDoc(std::ostream& os) const {
162  doc_.print(os, " ", pugi::format_raw | pugi::format_no_declaration);
163 }
164 
165 void XmlPugi::PrintDocFormatted(std::ostream& os) const {
166  doc_.print(os, " ", pugi::format_indent | pugi::format_no_declaration);
167 }
168 
169 void XmlPugi::SetContext(pugi::xml_node node, pugi::xml_attribute attrib) {
170  node_ = node;
171  attrib_ = attrib;
172 }
173 
174 int XmlPugi::LoadDoc(const std::string &document) {
175  RewindDoc();
176  doc_.reset();
177 
178  pugi::xml_parse_result ret = doc_.load_buffer(document.c_str(), document.size(),
179  pugi::parse_default,
180  pugi::encoding_utf8);
181  if (ret == false) {
182  LOG(DEBUG, "XML doc load failed, code: " << ret << " " << ret.description());
183  LOG(DEBUG, "Error offset: " << ret.offset << " (error at [..." << (document.c_str() + ret.offset) << "]");
184  LOG(DEBUG, "Document: " << document);
185  return -1;
186  }
187  return 0;
188 }
189 
191  SetContext();
192 }
193 
194 void XmlPugi::AppendDoc(const std::string &node_name, XmlBase *a_doc) {
195  std::string str;
196  pugi::xml_node node_s;
197  pugi::xml_node node1;
198  pugi::xml_node node2;
199 
200  PugiPredicate p1(node_name);
201  node1 = doc_.find_node(p1);
202  SetContext(node1);
203 
204  XmlPugi *xp = static_cast<XmlPugi *>(a_doc);
205  node_s = xp->doc_.first_child();
206  if (!IsNull(node1)) {
207  node2 = node1.parent().append_copy(node_s);
208  SetContext(node2);
209  }
210 
211 }
212 
213 int XmlPugi::AddNode(const std::string &key, const std::string &value) {
214  pugi::xml_node node;
215 
216  if (IsNull(node_)) {
217  node = doc_.append_child(key.c_str());
218  } else {
219  node = node_.parent().append_child(key.c_str());
220  }
221  if (value != "") {
222  node.text().set(value.c_str());
223  }
224 
225  SetContext(node);
226  return 0;
227 }
228 
229 int XmlPugi::DeleteNode(const std::string &key) {
230  PugiPredicate p1(key);
231  pugi::xml_node node = doc_.find_node(p1);
232  if (IsNull(node))
233  return -1;
234 
235  node.parent().remove_child(key.c_str());
236  return 0;
237 }
238 
239 int XmlPugi::ModifyNode(const std::string &key, const std::string &value) {
240  PugiPredicate p1(key);
241  pugi::xml_node node = doc_.find_node(p1);
242  if (IsNull(node))
243  return -1;
244 
245  node.text().set(value.c_str());
246 
247  SetContext(node);
248  return 0;
249 }
250 
251 int XmlPugi::AddChildNode(const std::string &key, const std::string &value) {
252  pugi::xml_node node;
253 
254  if (IsNull(node_)) {
255  node = doc_.append_child(key.c_str());
256  } else {
257  node = node_.append_child(key.c_str());
258  }
259  if (value != "") {
260  node.text().set(value.c_str());
261  }
262 
263  SetContext(node);
264  return 0;
265 }
266 
267 int XmlPugi::AddChildNodeAfter(const std::string &node_name,
268  const std::string &key,
269  const std::string &value) {
270 
271  PugiPredicate p1(node_name);
272  pugi::xml_node node1 = doc_.find_node(p1);
273  SetContext(node1);
274 
275  if (!IsNull(node1)) {
276  AddChildNode(key, value);
277  }
278 
279  return 0;
280 }
281 
282 
283 int XmlPugi::AddAttribute(const std::string &key, const std::string &value) {
284  if (IsNull(node_))
285  return -1;
286 
287  pugi::xml_attribute attrib;
288 
289  if (IsNull(attrib_) ) {
290  attrib = node_.append_attribute(key.c_str()) = value.c_str();
291  } else {
292  attrib =
293  node_.insert_attribute_after(key.c_str(), attrib_) = value.c_str();
294  }
295 
296  SetContext(node_, attrib);
297  return 0;
298 }
299 
300 int XmlPugi::DeleteAttribute(const std::string &key) {
301  if (IsNull(node_))
302  return -1;
303  pugi::xml_attribute tmp = node_.attribute(key.c_str());
304  bool res;
305  if (IsNull(tmp)) {
306  return -1;
307  } else {
308  pugi::xml_attribute attrib = tmp.next_attribute();
309  res = node_.remove_attribute(tmp);
310  SetContext(node_, attrib);
311  return (res ? -1 : 0);
312  }
313  return 0;
314 }
315 
316 int XmlPugi::ModifyAttribute(const std::string &key, const std::string &value) {
317  if (node_.type() == pugi::node_null)
318  return -1;
319 
320  //ReadAttrib(key);
321  pugi::xml_attribute tmp = node_.attribute(key.c_str());
322  if (IsNull(tmp)) {
323  return -1;
324  } else {
325  tmp.set_value(value.c_str());
326  SetContext(node_, tmp);
327  }
328 
329  return 0;
330 }
331 
332 void XmlPugi::xmpp_buf_write::write(const void *data, size_t sz) {
333  ref->SetBuf(data, sz);
334 }
335 
336 void XmlPugi::SetBuf(const void *buf, size_t sz) {
337  memcpy(buf_tmp_ + ts_, buf, sz);
338  ts_ += sz;
339  *(buf_tmp_+ts_) = '\0';
340 }
pugi::xml_attribute attrib_
Definition: xml_pugi.h:80
uint8_t * buf_tmp_
Definition: xml_pugi.h:72
virtual int WriteRawDoc(uint8_t *buf)
Definition: xml_pugi.cc:152
virtual const char * ReadNextNode()
Definition: xml_pugi.cc:97
static pugi::xml_node GNode
Definition: xml_pugi.h:94
XmlPugi()
Definition: xml_pugi.cc:18
virtual ~XmlPugi()
Definition: xml_pugi.cc:21
virtual const char * ReadNodeValue()
Definition: xml_pugi.cc:68
size_t ts_
Definition: xml_pugi.h:73
virtual void RewindAttrib()
Definition: xml_pugi.cc:133
virtual void PrintDoc(std::ostream &os) const
Definition: xml_pugi.cc:161
void SetContext(pugi::xml_node node=GNode, pugi::xml_attribute atrib=GAttr)
Definition: xml_pugi.cc:169
virtual int AddChildNode(const std::string &key, const std::string &value)
Definition: xml_pugi.cc:251
virtual const char * ReadChildNodeName()
Definition: xml_pugi.cc:90
pugi::xml_node node_
Definition: xml_pugi.h:79
virtual int LoadDoc(const std::string &doc)
Definition: xml_pugi.cc:174
virtual int ModifyNode(const std::string &key, const std::string &value)
Definition: xml_pugi.cc:239
bool IsNull(pugi::xml_node &node)
Definition: xml_pugi.h:64
virtual const char * ReadChildNode()
Definition: xml_pugi.cc:83
virtual int DeleteNode(const std::string &key)
Definition: xml_pugi.cc:229
virtual const char * ReadNextAttrib()
Definition: xml_pugi.cc:127
virtual void RewindDoc()
Definition: xml_pugi.cc:190
static pugi::xml_attribute GAttr
Definition: xml_pugi.h:93
pugi::xml_node FindNode(const std::string &name)
Definition: xml_pugi.cc:52
virtual void AppendDoc(const std::string &str, XmlBase *a_doc)
Definition: xml_pugi.cc:194
virtual const char * ReadFirstAttrib()
Definition: xml_pugi.cc:121
virtual const char * ReadNode(const std::string &name)
Definition: xml_pugi.cc:29
virtual const char * ReadNextNodeName()
Definition: xml_pugi.cc:104
virtual void RewindNode()
Definition: xml_pugi.cc:111
void SetBuf(const void *buf, size_t sz)
Definition: xml_pugi.cc:336
pugi::xml_document doc_
Definition: xml_pugi.h:76
struct xmpp_buf_write writer_
Definition: xml_pugi.h:74
virtual const char * ReadAttrib(const std::string &str)
Definition: xml_pugi.cc:115
virtual int AddNode(const std::string &key, const std::string &value)
Definition: xml_pugi.cc:213
virtual int DeleteAttribute(const std::string &key)
Definition: xml_pugi.cc:300
virtual int WriteDoc(uint8_t *buf)
Definition: xml_pugi.cc:143
virtual int ModifyAttribute(const std::string &key, const std::string &value)
Definition: xml_pugi.cc:316
pugi::xml_node RootNode()
Definition: xml_pugi.cc:25
virtual int AddAttribute(const std::string &key, const std::string &value)
Definition: xml_pugi.cc:283
virtual const char * ReadParentName()
Definition: xml_pugi.cc:137
virtual int AddChildNodeAfter(const std::string &node_name, const std::string &key, const std::string &value)
Definition: xml_pugi.cc:267
virtual const char * ReadNodeName(const std::string &name)
Definition: xml_pugi.cc:59
virtual void PrintDocFormatted(std::ostream &os) const
Definition: xml_pugi.cc:165
#define LOG(_Level, _Msg)
Definition: logging.h:35
virtual void write(const void *data, size_t sz)
Definition: xml_pugi.cc:332