Robotics Library  0.7.0
Attribute.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_ATTRIBUTE_H
28 #define RL_XML_ATTRIBUTE_H
29 
30 #include <string>
31 #include <boost/shared_array.hpp>
32 #include <libxml/parser.h>
33 
34 #include "Namespace.h"
35 
36 namespace rl
37 {
38  namespace xml
39  {
40  class Attribute
41  {
42  public:
43  explicit Attribute(::xmlAttrPtr attr) :
44  attr(attr)
45  {
46  }
47 
48  Attribute(::xmlNodePtr node, const ::std::string& name, const ::std::string& value) :
49  attr(
50  ::xmlNewProp(
51  node,
52  reinterpret_cast<const ::xmlChar*>(name.c_str()),
53  reinterpret_cast<const ::xmlChar*>(value.c_str())
54  )
55  )
56  {
57  }
58 
59  Attribute(::xmlNodePtr node, const Namespace& nameSpace, const ::std::string& name, const ::std::string& value) :
60  attr(
61  ::xmlNewNsProp(
62  node,
63  nameSpace.get(),
64  reinterpret_cast<const ::xmlChar*>(name.c_str()),
65  reinterpret_cast<const ::xmlChar*>(value.c_str())
66  )
67  )
68  {
69  }
70 
72  {
73  if (nullptr != this->attr && nullptr == this->attr->doc)
74  {
75  ::xmlFreeProp(this->attr);
76  }
77  }
78 
79  ::xmlAttrPtr get() const
80  {
81  return this->attr;
82  }
83 
84  ::std::string getName() const
85  {
86  return nullptr != this->attr->name ? reinterpret_cast<const char*>(this->attr->name) : ::std::string();
87  }
88 
90  {
91  return Attribute(this->attr->next);
92  }
93 
95  {
96  return Attribute(this->attr->prev);
97  }
98 
99  ::std::string getValue() const
100  {
101  if (nullptr != this->attr->ns)
102  {
103  ::boost::shared_array< ::xmlChar> value(
104  ::xmlGetNsProp(
105  this->attr->parent,
106  this->attr->name,
107  this->attr->ns->href
108  ),
109  ::xmlFree
110  );
111 
112  return nullptr != value.get() ? reinterpret_cast<char*>(value.get()) : ::std::string();
113  }
114  else
115  {
116  ::boost::shared_array< ::xmlChar> value(
117  ::xmlGetProp(
118  this->attr->parent,
119  this->attr->name
120  ),
121  ::xmlFree
122  );
123 
124  return nullptr != value.get() ? reinterpret_cast<char*>(value.get()) : ::std::string();
125  }
126  }
127 
128  ::xmlAttr& operator*() const
129  {
130  return *this->attr;
131  }
132 
133  void remove()
134  {
135  xmlRemoveProp(this->attr);
136  }
137 
138  void setValue(const ::std::string& value)
139  {
140  if (nullptr != this->attr->ns)
141  {
142  ::xmlSetNsProp(
143  this->attr->parent,
144  this->attr->ns,
145  this->attr->name,
146  reinterpret_cast<const ::xmlChar*>(value.c_str())
147  );
148  }
149  else
150  {
151  ::xmlSetProp(
152  this->attr->parent,
153  this->attr->name,
154  reinterpret_cast<const ::xmlChar*>(value.c_str())
155  );
156  }
157  }
158 
159  protected:
160 
161  private:
162  ::xmlAttrPtr attr;
163  };
164  }
165 }
166 
167 #endif // RL_XML_ATTRIBUTE_H
rl::xml::Attribute::getPrevious
Attribute getPrevious() const
Definition: Attribute.h:94
rl::xml::Attribute::getValue
::std::string getValue() const
Definition: Attribute.h:99
rl::xml::Attribute::~Attribute
~Attribute()
Definition: Attribute.h:71
rl::xml::Attribute
Definition: Attribute.h:41
rl::xml::Attribute::get
::xmlAttrPtr get() const
Definition: Attribute.h:79
rl::xml::Attribute::setValue
void setValue(const ::std::string &value)
Definition: Attribute.h:138
rl::xml::Attribute::remove
void remove()
Definition: Attribute.h:133
rl::xml::Attribute::getName
::std::string getName() const
Definition: Attribute.h:84
rl::xml::Attribute::Attribute
Attribute(::xmlNodePtr node, const ::std::string &name, const ::std::string &value)
Definition: Attribute.h:48
Namespace.h
rl::xml::Attribute::Attribute
Attribute(::xmlNodePtr node, const Namespace &nameSpace, const ::std::string &name, const ::std::string &value)
Definition: Attribute.h:59
rl::xml::Attribute::operator*
::xmlAttr & operator*() const
Definition: Attribute.h:128
rl::xml::Attribute::getNext
Attribute getNext() const
Definition: Attribute.h:89
rl::xml::Namespace
Definition: Namespace.h:39
rl::xml::Attribute::Attribute
Attribute(::xmlAttrPtr attr)
Definition: Attribute.h:43
rl::xml::Attribute::attr
::xmlAttrPtr attr
Definition: Attribute.h:162
rl
Robotics Library.
Definition: AnalogInput.cpp:30