Robotics Library  0.6.2
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 
40 namespace rl
41 {
42  namespace xml
43  {
44  class Node
45  {
46  public:
47  Node(xmlNodePtr node) :
48  node(node)
49  {
50  }
51 
52  Node(const ::std::string& name) :
53  node(
54  xmlNewNode(
55  NULL,
56  reinterpret_cast< const xmlChar* >(name.c_str())
57  )
58  )
59  {
60  }
61 
62  virtual ~Node()
63  {
64  if (NULL == this->node->doc)
65  {
66  xmlFree(this->node);
67  }
68  }
69 
70  static Node Text(const ::std::string& content)
71  {
72  return xmlNewText(
73  reinterpret_cast< const xmlChar* >(content.c_str())
74  );
75  }
76 
78  {
79  return xmlAddChild(this->node, node());
80  }
81 
83  {
84  return xmlAddNextSibling(this->node, node());
85  }
86 
88  {
89  return xmlAddPrevSibling(this->node, node());
90  }
91 
93  {
94  return xmlAddSibling(this->node, node());
95  }
96 
97  Attribute getAttribute(const ::std::string& name) const
98  {
99  return Attribute(
100  xmlHasProp(
101  this->node,
102  reinterpret_cast< const xmlChar* >(name.c_str())
103  )
104  );
105  }
106 
107  unsigned long getChildElementCount() const
108  {
109  return xmlChildElementCount(this->node);
110  }
111 
112  ::std::string getContent() const
113  {
114  ::boost::shared_array< xmlChar > content(
115  xmlNodeGetContent(this->node),
116  xmlFree
117  );
118 
119  return reinterpret_cast< char* >(content.get());
120  }
121 
122  ::std::string getLocalPath(const ::std::string& uri) const
123  {
124  ::boost::shared_array< xmlChar > absolute(
125  xmlBuildURI(
126  reinterpret_cast< const xmlChar* >(uri.c_str()),
127  xmlNodeGetBase(this->node->doc, this->node)
128  ),
129  xmlFree
130  );
131 
132  ::boost::shared_array< char > unescaped(
133  xmlURIUnescapeString(
134  reinterpret_cast< char* >(absolute.get()),
135  0,
136  NULL
137  ),
138  xmlFree
139  );
140 
141  char* path;
142 
143  if (0 == strncmp(unescaped.get(), "file://localhost/", 17))
144  {
145 #ifdef WIN32
146  path = &unescaped.get()[17];
147 #else // WIN32
148  path = &unescaped.get()[16];
149 #endif // WIN32
150  }
151  else if (0 == strncmp(unescaped.get(), "file:///", 8))
152  {
153 #ifdef WIN32
154  path = &unescaped.get()[8];
155 #else // WIN32
156  path = &unescaped.get()[7];
157 #endif // WIN32
158  }
159  else
160  {
161  path = unescaped.get();
162  }
163 
164  return path;
165  }
166 
167  ::std::string getName() const
168  {
169  return reinterpret_cast< const char* >(this->node->name);
170  }
171 
172  ::std::string getRelativeUri(const ::std::string& uri) const
173  {
174  ::boost::shared_array<xmlChar> relative(
175  xmlBuildRelativeURI(
176  reinterpret_cast< const xmlChar* >(uri.c_str()),
177  xmlNodeGetBase(this->node->doc, this->node)
178  ),
179  xmlFree
180  );
181 
182  return reinterpret_cast< char* >(relative.get());
183  }
184 
185  ::std::string getUri(const ::std::string& uri) const
186  {
187  ::boost::shared_array< xmlChar > absolute(
188  xmlBuildURI(
189  reinterpret_cast< const xmlChar* >(uri.c_str()),
190  xmlNodeGetBase(this->node->doc, this->node)
191  ),
192  xmlFree
193  );
194 
195  return reinterpret_cast< char* >(absolute.get());
196  }
197 
198  bool hasAttribute(const ::std::string& name) const
199  {
200  return NULL != xmlHasProp(
201  this->node,
202  reinterpret_cast< const xmlChar* >(name.c_str())
203  ) ? true : false;
204  }
205 
206  bool isText() const
207  {
208  return 1 == xmlNodeIsText(this->node) ? true : false;
209  }
210 
211  xmlNodePtr operator()() const
212  {
213  return this->node;
214  }
215 
217  {
218  return xmlReplaceNode(this->node, node());
219  }
220 
221  void setContent(const ::std::string& content)
222  {
223  xmlNodeSetContent(
224  this->node,
225  reinterpret_cast< const xmlChar* >(content.c_str())
226  );
227  }
228 
229  void setName(const ::std::string& name)
230  {
231  xmlNodeSetName(this->node, reinterpret_cast< const xmlChar* >(name.c_str()));
232  }
233 
234  int substitute(const int& flags = 0)
235  {
236  int substitutions = xmlXIncludeProcessTreeFlags(this->node, flags);
237 
238  if (-1 == substitutions)
239  {
240  throw Exception(xmlGetLastError()->message);
241  }
242 
243  return substitutions;
244  }
245 
246  void unlink()
247  {
248  xmlUnlinkNode(this->node);
249  }
250 
251  protected:
252 
253  private:
254  xmlNodePtr node;
255  };
256  }
257 }
258 
259 #endif // _RL_XML_NODE_H_
rl::xml::Node::addPrevSibling
Node addPrevSibling(const Node &node)
Definition: Node.h:87
rl::xml::Node::hasAttribute
bool hasAttribute(const ::std::string &name) const
Definition: Node.h:198
rl::xml::Node::Text
static Node Text(const ::std::string &content)
Definition: Node.h:70
rl::xml::Node::unlink
void unlink()
Definition: Node.h:246
rl::xml::Node::node
xmlNodePtr node
Definition: Node.h:254
rl::xml::Node::getRelativeUri
::std::string getRelativeUri(const ::std::string &uri) const
Definition: Node.h:172
rl::xml::Node::addNextSibling
Node addNextSibling(const Node &node)
Definition: Node.h:82
rl::xml::Node::addSibling
Node addSibling(const Node &node)
Definition: Node.h:92
rl::xml::Node::replace
Node replace(const Node &node)
Definition: Node.h:216
rl::xml::Node::getUri
::std::string getUri(const ::std::string &uri) const
Definition: Node.h:185
rl::xml::Node::~Node
virtual ~Node()
Definition: Node.h:62
rl::xml::Node::Node
Node(xmlNodePtr node)
Definition: Node.h:47
rl::xml::Attribute
Definition: Attribute.h:39
Attribute.h
rl::xml::Node::getAttribute
Attribute getAttribute(const ::std::string &name) const
Definition: Node.h:97
rl::xml::Node::setContent
void setContent(const ::std::string &content)
Definition: Node.h:221
rl::xml::Node::getLocalPath
::std::string getLocalPath(const ::std::string &uri) const
Definition: Node.h:122
rl::xml::Node
Definition: Node.h:45
rl::xml::Node::Node
Node(const ::std::string &name)
Definition: Node.h:52
rl::xml::Node::substitute
int substitute(const int &flags=0)
Definition: Node.h:234
rl::xml::Node::addChild
Node addChild(const Node &node)
Definition: Node.h:77
rl::xml::Node::getName
::std::string getName() const
Definition: Node.h:167
rl::xml::Node::getChildElementCount
unsigned long getChildElementCount() const
Definition: Node.h:107
rl::xml::Node::isText
bool isText() const
Definition: Node.h:206
rl::xml::Node::getContent
::std::string getContent() const
Definition: Node.h:112
rl::xml::Node::setName
void setName(const ::std::string &name)
Definition: Node.h:229
rl::xml::Node::operator()
xmlNodePtr operator()() const
Definition: Node.h:211
Exception.h
rl::xml::Exception
Definition: Exception.h:37
rl
Definition: Ati.cpp:35