Robotics Library  0.7.0
Path.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_PATH_H
28 #define RL_XML_PATH_H
29 
30 #include <memory>
31 #include <string>
32 #include <boost/shared_array.hpp>
33 #include <libxml/xpath.h>
34 #include <libxml/xpathInternals.h>
35 
36 #include "Document.h"
37 #include "Node.h"
38 #include "Object.h"
39 
40 namespace rl
41 {
42  namespace xml
43  {
44  class Path
45  {
46  public:
47  explicit Path(const Document& document) :
48  context(::xmlXPathNewContext(document.get()), ::xmlXPathFreeContext),
50  {
51  this->context->node = nullptr;
52  }
53 
54  Path(const Document& document, const Node& node) :
55  context(::xmlXPathNewContext(document.get()), ::xmlXPathFreeContext),
57  {
58  this->context->node = node.get();
59  }
60 
62  {
63  ::xmlXPathRegisteredNsCleanup(this->context.get());
64  }
65 
66  Object eval(const ::std::string& expression)
67  {
68  return Object(
69  ::xmlXPathEvalExpression(
70  reinterpret_cast<const ::xmlChar*>(expression.c_str()),
71  this->context.get()
72  )
73  );
74  }
75 
76  ::xmlXPathContextPtr get() const
77  {
78  return this->context.get();
79  }
80 
81  Node getNode() const
82  {
83  return Node(this->context->node);
84  }
85 
86  ::xmlXPathContext& operator*() const
87  {
88  return *this->context;
89  }
90 
91  bool registerNamespace(const ::std::string& prefix, const ::std::string& uri)
92  {
93  return 0 == ::xmlXPathRegisterNs(
94  this->context.get(),
95  reinterpret_cast<const ::xmlChar*>(prefix.c_str()),
96  reinterpret_cast<const ::xmlChar*>(uri.c_str())
97  ) ? true : false;
98  }
99 
100  void setNode(const Node& node)
101  {
102  this->context->node = node.get();
103  }
104 
105  protected:
106 
107  private:
108  ::std::shared_ptr< ::xmlXPathContext> context;
109 
110  ::xmlDocPtr document;
111  };
112  }
113 }
114 
115 #endif // RL_XML_PATH_H
rl::xml::Path::get
::xmlXPathContextPtr get() const
Definition: Path.h:76
rl::xml::Path::eval
Object eval(const ::std::string &expression)
Definition: Path.h:66
rl::xml::Document
Definition: Document.h:47
rl::xml::Path::setNode
void setNode(const Node &node)
Definition: Path.h:100
rl::xml::Path::operator*
::xmlXPathContext & operator*() const
Definition: Path.h:86
rl::xml::Node::get
::xmlNodePtr get() const
Definition: Node.h:96
rl::xml::Object
Definition: Object.h:43
rl::xml::Path::registerNamespace
bool registerNamespace(const ::std::string &prefix, const ::std::string &uri)
Definition: Path.h:91
rl::xml::Node
Definition: Node.h:46
rl::xml::Path::context
::std::shared_ptr< ::xmlXPathContext > context
Definition: Path.h:108
Node.h
rl::xml::Path
Definition: Path.h:45
rl::xml::Path::Path
Path(const Document &document)
Definition: Path.h:47
rl::xml::Path::~Path
~Path()
Definition: Path.h:61
rl::xml::Path::document
::xmlDocPtr document
Definition: Path.h:110
Object.h
rl::xml::Path::getNode
Node getNode() const
Definition: Path.h:81
rl::xml::Path::Path
Path(const Document &document, const Node &node)
Definition: Path.h:54
Document.h
rl
Robotics Library.
Definition: AnalogInput.cpp:30