Robotics Library  0.7.0
Node.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef RL_XML_NODE_H
28 #define RL_XML_NODE_H
29 
30 #include <cstring>
31 #include <string>
32 #include <boost/shared_array.hpp>
33 #include <libxml/parser.h>
34 #include <libxml/uri.h>
35 #include <libxml/xinclude.h>
36 
37 #include "Attribute.h"
38 #include "Exception.h"
39 #include "Namespace.h"
40 
41 namespace rl
42 {
43  namespace xml
44  {
45  class Node
46  {
47  public:
48  explicit Node(::xmlNodePtr node) :
49  node(node)
50  {
51  }
52 
53  explicit Node(const ::std::string& name) :
54  node(
55  ::xmlNewNode(
56  nullptr,
57  reinterpret_cast<const ::xmlChar*>(name.c_str())
58  )
59  )
60  {
61  }
62 
64  {
65  if (nullptr != this->node && nullptr == this->node->doc)
66  {
67  ::xmlFree(this->node);
68  }
69  }
70 
71  static Node Text(const ::std::string& content)
72  {
73  return Node(::xmlNewText(reinterpret_cast<const ::xmlChar*>(content.c_str())));
74  }
75 
77  {
78  return Node(::xmlAddChild(this->node, node.get()));
79  }
80 
82  {
83  return Node(::xmlAddNextSibling(this->node, node.get()));
84  }
85 
87  {
88  return Node(::xmlAddPrevSibling(this->node, node.get()));
89  }
90 
92  {
93  return Node(::xmlAddSibling(this->node, node.get()));
94  }
95 
96  ::xmlNodePtr get() const
97  {
98  return this->node;
99  }
100 
101  unsigned long getChildElementCount() const
102  {
103  return ::xmlChildElementCount(this->node);
104  }
105 
106  ::std::string getContent() const
107  {
108  ::boost::shared_array< ::xmlChar> content(
109  ::xmlNodeGetContent(this->node),
110  ::xmlFree
111  );
112 
113  return nullptr != content.get() ? reinterpret_cast<char*>(content.get()) : ::std::string();
114  }
115 
117  {
118  return Node(this->node->children);
119  }
120 
122  {
123  return Attribute(this->node->properties);
124  }
125 
126  unsigned short int getLine() const
127  {
128  return this->node->line;
129  }
130 
131  ::std::string getLocalPath(const ::std::string& uri) const
132  {
133  ::boost::shared_array< ::xmlChar> absolute(
134  ::xmlBuildURI(
135  reinterpret_cast<const ::xmlChar*>(uri.c_str()),
136  ::xmlNodeGetBase(this->node->doc, this->node)
137  ),
138  ::xmlFree
139  );
140 
141  ::boost::shared_array<char> unescaped(
142  ::xmlURIUnescapeString(
143  reinterpret_cast<char*>(absolute.get()),
144  0,
145  nullptr
146  ),
147  ::xmlFree
148  );
149 
150  char* path;
151 
152  if (0 == ::strncmp(unescaped.get(), "file://localhost/", 17))
153  {
154 #ifdef WIN32
155  path = &unescaped.get()[17];
156 #else // WIN32
157  path = &unescaped.get()[16];
158 #endif // WIN32
159  }
160  else if (0 == ::strncmp(unescaped.get(), "file:///", 8))
161  {
162 #ifdef WIN32
163  path = &unescaped.get()[8];
164 #else // WIN32
165  path = &unescaped.get()[7];
166 #endif // WIN32
167  }
168  else if (0 == ::strncmp(unescaped.get(), "file:/", 6))
169  {
170 #ifdef WIN32
171  path = &unescaped.get()[6];
172 #else // WIN32
173  path = &unescaped.get()[5];
174 #endif // WIN32
175  }
176  else
177  {
178  path = unescaped.get();
179  }
180 
181  return nullptr != path ? path : ::std::string();
182  }
183 
184  ::std::string getName() const
185  {
186  return nullptr != this->node->name ? reinterpret_cast<const char*>(this->node->name) : ::std::string();
187  }
188 
190  {
191  return Namespace(this->node->ns);
192  }
193 
194  Node getNext() const
195  {
196  return Node(this->node->next);
197  }
198 
199  Node getParent() const
200  {
201  return Node(this->node->parent);
202  }
203 
205  {
206  return Node(this->node->prev);
207  }
208 
209  ::std::string getProperty(const ::std::string& name) const
210  {
211  ::boost::shared_array< ::xmlChar> prop(
212  ::xmlGetProp(
213  this->node,
214  reinterpret_cast<const ::xmlChar*>(name.c_str())
215  ),
216  ::xmlFree
217  );
218 
219  return nullptr != prop.get() ? reinterpret_cast<char*>(prop.get()) : ::std::string();
220  }
221 
222  ::std::string getProperty(const ::std::string& name, const ::std::string& nameSpace) const
223  {
224  ::boost::shared_array< ::xmlChar> prop(
225  ::xmlGetNsProp(
226  this->node,
227  reinterpret_cast<const ::xmlChar*>(name.c_str()),
228  reinterpret_cast<const ::xmlChar*>(nameSpace.c_str())
229  ),
230  ::xmlFree
231  );
232 
233  return nullptr != prop.get() ? reinterpret_cast<char*>(prop.get()) : ::std::string();
234  }
235 
236  ::std::string getRelativeUri(const ::std::string& uri) const
237  {
238  ::boost::shared_array< ::xmlChar> relative(
239  ::xmlBuildRelativeURI(
240  reinterpret_cast<const ::xmlChar*>(uri.c_str()),
241  ::xmlNodeGetBase(this->node->doc, this->node)
242  ),
243  ::xmlFree
244  );
245 
246  return nullptr != relative.get() ? reinterpret_cast<char*>(relative.get()) : ::std::string();
247  }
248 
249  ::std::string getUri(const ::std::string& uri) const
250  {
251  ::boost::shared_array< ::xmlChar> absolute(
252  ::xmlBuildURI(
253  reinterpret_cast<const ::xmlChar*>(uri.c_str()),
254  ::xmlNodeGetBase(this->node->doc, this->node)
255  ),
256  ::xmlFree
257  );
258 
259  return nullptr != absolute.get() ? reinterpret_cast<char*>(absolute.get()) : ::std::string();
260  }
261 
262  bool hasChildren() const
263  {
264  return nullptr != this->node->children;
265  }
266 
267  bool hasNamespace() const
268  {
269  return nullptr != this->node->ns;
270  }
271 
272  bool hasParent() const
273  {
274  return nullptr != this->node->parent;
275  }
276 
277  bool hasProperties() const
278  {
279  return nullptr != this->node->properties;
280  }
281 
282  Attribute hasProperty(const ::std::string& name) const
283  {
284  return Attribute(
285  ::xmlHasProp(
286  this->node,
287  reinterpret_cast<const ::xmlChar*>(name.c_str())
288  )
289  );
290  }
291 
292  Attribute hasProperty(const ::std::string& name, const ::std::string& nameSpace) const
293  {
294  return Attribute(
295  ::xmlHasNsProp(
296  this->node,
297  reinterpret_cast<const ::xmlChar*>(name.c_str()),
298  reinterpret_cast<const ::xmlChar*>(nameSpace.c_str())
299  )
300  );
301  }
302 
303  bool isBlank() const
304  {
305  return 1 == ::xmlIsBlankNode(this->node) ? true : false;
306  }
307 
308  bool isText() const
309  {
310  return 1 == ::xmlNodeIsText(this->node) ? true : false;
311  }
312 
313  ::xmlNode& operator*() const
314  {
315  return *this->node;
316  }
317 
318  bool removeAttribute(const Attribute& attribute)
319  {
320  return 0 == ::xmlRemoveProp(attribute.get()) ? true : false;
321  }
322 
324  {
325  return Node(::xmlReplaceNode(this->node, node.get()));
326  }
327 
328  void setContent(const ::std::string& content)
329  {
330  ::xmlNodeSetContent(
331  this->node,
332  reinterpret_cast<const ::xmlChar*>(content.c_str())
333  );
334  }
335 
336  void setName(const ::std::string& name)
337  {
338  ::xmlNodeSetName(this->node, reinterpret_cast<const ::xmlChar*>(name.c_str()));
339  }
340 
341  Attribute setProperty(const ::std::string& name, const ::std::string& value)
342  {
343  return Attribute(
344  ::xmlSetProp(
345  this->node,
346  reinterpret_cast<const ::xmlChar*>(name.c_str()),
347  reinterpret_cast<const ::xmlChar*>(value.c_str())
348  )
349  );
350  }
351 
352  Attribute setProperty(const Namespace& nameSpace, const ::std::string& name, const ::std::string& value)
353  {
354  return Attribute(
355  ::xmlSetNsProp(
356  this->node,
357  nameSpace.get(),
358  reinterpret_cast<const ::xmlChar*>(name.c_str()),
359  reinterpret_cast<const ::xmlChar*>(value.c_str())
360  )
361  );
362  }
363 
364  int substitute(const int& flags = 0)
365  {
366  int substitutions = ::xmlXIncludeProcessTreeFlags(this->node, flags);
367 
368  if (-1 == substitutions)
369  {
370  throw Exception(::xmlGetLastError()->message);
371  }
372 
373  return substitutions;
374  }
375 
376  void unlink()
377  {
378  ::xmlUnlinkNode(this->node);
379  }
380 
381  protected:
382 
383  private:
384  ::xmlNodePtr node;
385  };
386  }
387 }
388 
389 #endif // RL_XML_NODE_H
rl::xml::Node::hasChildren
bool hasChildren() const
Definition: Node.h:262
rl::xml::Node::addPrevSibling
Node addPrevSibling(const Node &node)
Definition: Node.h:86
rl::xml::Node::Text
static Node Text(const ::std::string &content)
Definition: Node.h:71
rl::xml::Node::unlink
void unlink()
Definition: Node.h:376
rl::xml::Node::getRelativeUri
::std::string getRelativeUri(const ::std::string &uri) const
Definition: Node.h:236
rl::xml::Node::addNextSibling
Node addNextSibling(const Node &node)
Definition: Node.h:81
rl::xml::Node::getNamespace
Namespace getNamespace() const
Definition: Node.h:189
rl::xml::Node::addSibling
Node addSibling(const Node &node)
Definition: Node.h:91
rl::xml::Node::replace
Node replace(const Node &node)
Definition: Node.h:323
rl::xml::Node::getUri
::std::string getUri(const ::std::string &uri) const
Definition: Node.h:249
rl::xml::Node::setProperty
Attribute setProperty(const Namespace &nameSpace, const ::std::string &name, const ::std::string &value)
Definition: Node.h:352
rl::xml::Node::getNext
Node getNext() const
Definition: Node.h:194
rl::xml::Node::hasProperty
Attribute hasProperty(const ::std::string &name, const ::std::string &nameSpace) const
Definition: Node.h:292
rl::xml::Node::getPrevious
Node getPrevious() const
Definition: Node.h:204
rl::xml::Attribute
Definition: Attribute.h:41
rl::xml::Attribute::get
::xmlAttrPtr get() const
Definition: Attribute.h:79
Attribute.h
rl::xml::Node::Node
Node(::xmlNodePtr node)
Definition: Node.h:48
rl::xml::Node::~Node
~Node()
Definition: Node.h:63
rl::xml::Node::getParent
Node getParent() const
Definition: Node.h:199
rl::xml::Node::removeAttribute
bool removeAttribute(const Attribute &attribute)
Definition: Node.h:318
rl::xml::Node::getFirstProperty
Attribute getFirstProperty() const
Definition: Node.h:121
rl::xml::Node::setContent
void setContent(const ::std::string &content)
Definition: Node.h:328
rl::xml::Node::hasProperty
Attribute hasProperty(const ::std::string &name) const
Definition: Node.h:282
rl::xml::Node::get
::xmlNodePtr get() const
Definition: Node.h:96
rl::xml::Node::hasParent
bool hasParent() const
Definition: Node.h:272
rl::xml::Node::setProperty
Attribute setProperty(const ::std::string &name, const ::std::string &value)
Definition: Node.h:341
rl::xml::Node::hasNamespace
bool hasNamespace() const
Definition: Node.h:267
rl::xml::Node::operator*
::xmlNode & operator*() const
Definition: Node.h:313
rl::xml::Node::getLocalPath
::std::string getLocalPath(const ::std::string &uri) const
Definition: Node.h:131
rl::xml::Node
Definition: Node.h:46
rl::xml::Node::node
::xmlNodePtr node
Definition: Node.h:384
Namespace.h
rl::xml::Node::getFirstChild
Node getFirstChild() const
Definition: Node.h:116
rl::xml::Node::Node
Node(const ::std::string &name)
Definition: Node.h:53
rl::xml::Node::substitute
int substitute(const int &flags=0)
Definition: Node.h:364
rl::xml::Node::getLine
unsigned short int getLine() const
Definition: Node.h:126
rl::xml::Node::addChild
Node addChild(const Node &node)
Definition: Node.h:76
rl::xml::Node::getName
::std::string getName() const
Definition: Node.h:184
rl::xml::Node::getChildElementCount
unsigned long getChildElementCount() const
Definition: Node.h:101
rl::xml::Node::isText
bool isText() const
Definition: Node.h:308
rl::xml::Namespace
Definition: Namespace.h:39
rl::xml::Namespace::get
::xmlNsPtr get() const
Definition: Namespace.h:61
rl::xml::Node::getContent
::std::string getContent() const
Definition: Node.h:106
rl::xml::Node::setName
void setName(const ::std::string &name)
Definition: Node.h:336
rl::xml::Node::hasProperties
bool hasProperties() const
Definition: Node.h:277
Exception.h
rl::xml::Node::getProperty
::std::string getProperty(const ::std::string &name, const ::std::string &nameSpace) const
Definition: Node.h:222
rl::xml::Exception
Definition: Exception.h:37
rl::xml::Node::getProperty
::std::string getProperty(const ::std::string &name) const
Definition: Node.h:209
rl::xml::Node::isBlank
bool isBlank() const
Definition: Node.h:303
rl
Robotics Library.
Definition: AnalogInput.cpp:30