Robotics Library  0.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 // * Neither the name of the Technische Universitaet Muenchen nor the names of
14 // its contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef _RL_XML_NODE_H_
31 #define _RL_XML_NODE_H_
32 
33 #include <cstring>
34 #include <string>
35 #include <boost/shared_array.hpp>
36 #include <libxml/parser.h>
37 #include <libxml/uri.h>
38 
39 #include "Attribute.h"
40 
41 namespace rl
42 {
43  namespace xml
44  {
45  class Node
46  {
47  public:
48  Node(xmlNodePtr node) :
49  node(node)
50  {
51  }
52 
53  Node(const ::std::string& name) :
54  node(
55  xmlNewNode(
56  NULL,
57  reinterpret_cast< const xmlChar* >(name.c_str())
58  )
59  )
60  {
61  }
62 
63  virtual ~Node()
64  {
65  if (NULL == this->node->doc)
66  {
67  xmlFree(this->node);
68  }
69  }
70 
71  static Node Text(const ::std::string& content)
72  {
73  return xmlNewText(
74  reinterpret_cast< const xmlChar* >(content.c_str())
75  );
76  }
77 
79  {
80  return xmlAddChild(this->node, node());
81  }
82 
84  {
85  return xmlAddNextSibling(this->node, node());
86  }
87 
89  {
90  return xmlAddPrevSibling(this->node, node());
91  }
92 
94  {
95  return xmlAddSibling(this->node, node());
96  }
97 
98  Attribute getAttribute(const ::std::string& name) const
99  {
100  return Attribute(
101  xmlHasProp(
102  this->node,
103  reinterpret_cast< const xmlChar* >(name.c_str())
104  )
105  );
106  }
107 
108  unsigned long getChildElementCount() const
109  {
110  return xmlChildElementCount(this->node);
111  }
112 
113  ::std::string getContent() const
114  {
115  ::boost::shared_array< xmlChar > content(
116  xmlNodeGetContent(this->node),
117  xmlFree
118  );
119 
120  return reinterpret_cast< char* >(content.get());
121  }
122 
123  ::std::string getLocalPath(const ::std::string& uri) const
124  {
125  ::boost::shared_array< xmlChar > absolute(
126  xmlBuildURI(
127  reinterpret_cast< const xmlChar* >(uri.c_str()),
128  xmlNodeGetBase(this->node->doc, this->node)
129  ),
130  xmlFree
131  );
132 
133  ::boost::shared_array< char > unescaped(
134  xmlURIUnescapeString(
135  reinterpret_cast< char* >(absolute.get()),
136  0,
137  NULL
138  ),
139  xmlFree
140  );
141 
142  char* path;
143 
144  if (0 == strncmp(unescaped.get(), "file://localhost/", 17))
145  {
146 #ifdef WIN32
147  path = &unescaped.get()[17];
148 #else // WIN32
149  path = &unescaped.get()[16];
150 #endif // WIN32
151  }
152  else if (0 == strncmp(unescaped.get(), "file:///", 8))
153  {
154 #ifdef WIN32
155  path = &unescaped.get()[8];
156 #else // WIN32
157  path = &unescaped.get()[7];
158 #endif // WIN32
159  }
160  else
161  {
162  path = unescaped.get();
163  }
164 
165  return path;
166  }
167 
168  ::std::string getName() const
169  {
170  return reinterpret_cast< const char* >(this->node->name);
171  }
172 
173  ::std::string getRelativeUri(const ::std::string& uri) const
174  {
175  ::boost::shared_array<xmlChar> relative(
176  xmlBuildRelativeURI(
177  reinterpret_cast< const xmlChar* >(uri.c_str()),
178  xmlNodeGetBase(this->node->doc, this->node)
179  ),
180  xmlFree
181  );
182 
183  return reinterpret_cast< char* >(relative.get());
184  }
185 
186  ::std::string getUri(const ::std::string& uri) const
187  {
188  ::boost::shared_array< xmlChar > absolute(
189  xmlBuildURI(
190  reinterpret_cast< const xmlChar* >(uri.c_str()),
191  xmlNodeGetBase(this->node->doc, this->node)
192  ),
193  xmlFree
194  );
195 
196  return reinterpret_cast< char* >(absolute.get());
197  }
198 
199  bool hasAttribute(const ::std::string& name) const
200  {
201  return NULL != xmlHasProp(
202  this->node,
203  reinterpret_cast< const xmlChar* >(name.c_str())
204  ) ? true : false;
205  }
206 
207  bool isText() const
208  {
209  return 1 == xmlNodeIsText(this->node) ? true : false;
210  }
211 
212  xmlNodePtr operator()() const
213  {
214  return this->node;
215  }
216 
218  {
219  return xmlReplaceNode(this->node, node());
220  }
221 
222  void setContent(const ::std::string& content)
223  {
224  xmlNodeSetContent(
225  this->node,
226  reinterpret_cast< const xmlChar* >(content.c_str())
227  );
228  }
229 
230  void setName(const ::std::string& name)
231  {
232  xmlNodeSetName(this->node, reinterpret_cast< const xmlChar* >(name.c_str()));
233  }
234 
235  int substitute(const int& flags = 0)
236  {
237  int substitutions = xmlXIncludeProcessTreeFlags(this->node, flags);
238 
239  if (-1 == substitutions)
240  {
241  throw Exception(xmlGetLastError()->message);
242  }
243 
244  return substitutions;
245  }
246 
247  void unlink()
248  {
249  xmlUnlinkNode(this->node);
250  }
251 
252  protected:
253 
254  private:
255  xmlNodePtr node;
256  };
257  }
258 }
259 
260 #endif // _RL_XML_NODE_H_