DefaultRapidXmlReader.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
30 
31 #include <string>
32 #include <set>
33 
34 namespace armarx
35 {
37  {
38  private:
39  std::vector<RapidXmlReaderNode> nodes;
40 
41  void check() const
42  {
43  if (nodes.empty())
44  {
45  throw exceptions::local::RapidXmlReaderException("DefaultRapidXmlWrapper NullPointerException");
46  }
47  }
48 
49  std::string get_attrib_value(const char* attrName) const
50  {
51  check();
52  for (auto& node : nodes)
53  {
54  if (node.has_attribute(attrName))
55  {
56  return node.attribute_value(attrName);
57  }
58  }
59 
60  //build error
61  std::stringstream ch;
62  ch << "\nchildren " << getChildPaths();
64  std::string("Attribute '") + attrName +
65  "' does not exist in node " + getPathsString() + ch.str());
66  }
67 
68  std::string get_value() const
69  {
70  RapidXmlReaderNode n = nodes.front();
71  return n.value();
72  }
73 
74  public:
75  DefaultRapidXmlReaderNode() = default;
80 
81  DefaultRapidXmlReaderNode(std::vector<RapidXmlReaderNode> rnodes)
82  {
83  for (RapidXmlReaderNode& n : rnodes)
84  {
85  if (!n.is_valid())
86  {
87  throw exceptions::local::RapidXmlReaderException("DefaultRapidXmlWrapper NullPointerException in constructor");
88  }
89  }
90  reverse(rnodes.begin(), rnodes.end());
91  nodes = std::move(rnodes);
92  }
94  DefaultRapidXmlReaderNode(std::vector{n})
95  {}
96 
97  DefaultRapidXmlReaderNode first_node(const std::string& name) const
98  {
99  return first_node(name.c_str());
100  }
101  DefaultRapidXmlReaderNode first_node(const char* name = nullptr) const
102  {
103  check();
104  std::vector<RapidXmlReaderNode> v;
105  for (const RapidXmlReaderNode& n : nodes)
106  {
107  if (n.get_node_ptr()->first_node(name) != nullptr)
108  {
109  v.push_back(n.first_node(name));
110  }
111  }
112  if (v.empty())
113  {
114  std::stringstream ch;
115  ch << "\nchildren " << getChildPaths();
117  std::string("Node with name '") + name +
118  "' does not exist in node " + getPathsString() + ch.str());
119  }
120  reverse(v.begin(), v.end());
121  return DefaultRapidXmlReaderNode(std::move(v));
122  }
123 
124  bool has_attribute(const std::string& attrName) const
125  {
126  return has_attribute(attrName.c_str());
127  }
128  bool has_attribute(const char* attrName) const
129  {
130  check();
131  for (RapidXmlReaderNode n : nodes)
132  {
133  if (n.has_attribute(attrName))
134  {
135  return true;
136  }
137  }
138  return false;
139  }
140 
141  bool attribute_as_bool(const std::string& attrName, const std::string& trueValue, const std::string& falseValue) const
142  {
143  return attribute_as_bool(attrName.c_str(), trueValue, falseValue);
144  }
145  bool attribute_as_bool(const char* attrName, const std::string& trueValue, const std::string& falseValue) const
146  {
147  std::string value = std::string(get_attrib_value(attrName));
148 
149  if (value == trueValue)
150  {
151  return true;
152  }
153  else if (value == falseValue)
154  {
155  return false;
156  }
157  else
158  {
159  throw exceptions::local::RapidXmlReaderException(std::string("Invalid value '") + value + "' for attribute '" + attrName + "'. Expecting '" + trueValue + "' or '" + falseValue + "'.");
160  }
161  }
162 
163  bool attribute_as_optional_bool(const std::string& name, const std::string& trueValue, const std::string& falseValue, bool defaultValue) const
164  {
165  return attribute_as_optional_bool(name.c_str(), trueValue, falseValue, defaultValue);
166  }
167  bool attribute_as_optional_bool(const char* name, const std::string& trueValue, const std::string& falseValue, bool defaultValue) const
168  {
169  check();
170 
171  if (!has_attribute(name))
172  {
173  return defaultValue;
174  }
175 
176  std::string value = std::string(get_attrib_value(name));
177 
178  if (value == trueValue)
179  {
180  return true;
181  }
182  else if (value == falseValue)
183  {
184  return false;
185  }
186  else
187  {
188  throw exceptions::local::RapidXmlReaderException(std::string("Invalid value '") + value + "' for attribute '" + name + "'. Expecting '" + trueValue + "' or '" + falseValue + "'.");
189  }
190  }
191 
192  std::string attribute_as_string(const std::string& attrName) const
193  {
194  return attribute_as_string(attrName.c_str());
195  }
196  std::string attribute_as_string(const char* attrName) const
197  {
198  return std::string(get_attrib_value(attrName));
199  }
200 
201  template<class T> void attribute_as(const std::string& attrName, T& value) const
202  {
203  return attribute_as(attrName.c_str(), value);
204  }
205  template<class T> void attribute_as(const char* attrName, T& value) const
206  {
207  std::stringstream(get_attrib_value(attrName)) >> value;
208  }
209 
210  template<class T> T attribute_as(const std::string& attrName) const
211  {
212  return attribute_as<T>(attrName.c_str());
213  }
214  template<class T> T attribute_as(const char* attrName) const
215  {
216  T val;
217  attribute_as(attrName, val);
218  return val;
219  }
220 
221  float attribute_as_float(const std::string& attrName) const
222  {
223  return attribute_as_float(attrName.c_str());
224  }
225  float attribute_as_float(const char* attrName) const
226  {
227  return attribute_as<float>(attrName);
228  }
229 
230  uint32_t attribute_as_uint(const std::string& attrName) const
231  {
232  return attribute_as_uint(attrName.c_str());
233  }
234  uint32_t attribute_as_uint(const char* attrName) const
235  {
236  return attribute_as<uint32_t>(attrName);
237  }
238 
239  std::string value_as_string() const
240  {
241  check();
242  return std::string(get_value());
243  }
244 
245  float value_as_float() const
246  {
247  check();
248  std::stringstream strValue(get_value());
249  float retValue;
250  strValue >> retValue;
251  return retValue;
252  }
253 
254  uint32_t value_as_uint32() const
255  {
256  check();
257  return static_cast<uint32_t>(std::stoul(get_value(), nullptr, 0));
258  }
259 
260  uint16_t value_as_uint16() const
261  {
262  check();
263  return static_cast<uint16_t>(std::stoul(get_value(), nullptr, 0));
264  }
265 
266  int32_t value_as_int32() const
267  {
268  check();
269  return static_cast<int32_t>(std::stoul(get_value(), nullptr, 0));
270  }
271 
272  int16_t value_as_int16() const
273  {
274  check();
275  return static_cast<int16_t>(std::stoul(get_value(), nullptr, 0));
276  }
277 
278  bool value_as_bool(const std::string& trueValue, const std::string& falseValue) const
279  {
280  check();
281  std::string value = std::string(get_value());
282  if (value == trueValue)
283  {
284  return true;
285  }
286  else if (value == falseValue)
287  {
288  return false;
289  }
290  else
291  {
292  std::string s = "(";
293  for (RapidXmlReaderNode node : nodes)
294  {
295  s += std::string(node.name());
296  s += ", ";
297  }
298  s = s.substr(0, s.length() - 2);
299  s += ")";
300  throw exceptions::local::RapidXmlReaderException(std::string("Invalid value '") + value + "' for represented nodes " + s + ". Expecting '" + trueValue + "' or '" + falseValue + "'.");
301  }
302  }
303 
304  bool value_as_optional_bool(const std::string& trueValue, const std::string& falseValue, bool defaultValue) const
305  {
306  check();
307  std::string value = std::string(get_value());
308  if (value == trueValue)
309  {
310  return true;
311  }
312  else if (value == falseValue)
313  {
314  return false;
315  }
316  else
317  {
318  return defaultValue;
319  }
320  }
321 
322  bool is_valid() const
323  {
324  return !nodes.empty();
325  }
326 
327  std::vector<std::string> getChildPaths() const
328  {
329  check();
330  std::set<std::string> results;
331  for (const RapidXmlReaderNode& n : nodes)
332  {
333  const auto paths = n.getChildPaths();
334  results.insert(paths.begin(), paths.end());
335  }
336  return {results.begin(), results.end()};
337  }
338  std::vector<std::string> getPaths() const
339  {
340  check();
341  std::vector<std::string> results;
342  for (const RapidXmlReaderNode& n : nodes)
343  {
344  results.push_back(n.getPath());
345  }
346  return results;
347  }
348  std::string getPathsString() const
349  {
350  std::vector<std::string> paths = getPaths();
351  std::string all_paths;
352  for (std::string path : paths)
353  {
354  all_paths += path;
355  all_paths += " or ";
356  }
357  all_paths = all_paths.substr(0, all_paths.length() - 4);
358  return all_paths;
359  }
360 
361 
363  {
364  return add_node_at(node, nodes.size());
365  }
367  {
368  return add_node_at(node.nodes, nodes.size());
369  }
370 
371  DefaultRapidXmlReaderNode add_node_at(std::vector<RapidXmlReaderNode> added_nodes, size_t position)
372  {
373  if (position > nodes.size())
374  {
375  throw exceptions::local::RapidXmlReaderException("The index is out of range: given index ") << position << " size of container: " << nodes.size();
376  }
377 
378  if (!added_nodes.empty())
379  {
380  std::vector<RapidXmlReaderNode> v;
381  v.reserve(added_nodes.size() + nodes.size());
382  for (auto& node : added_nodes)
383  {
384  if (node.is_valid())
385  {
386  v.emplace_back(std::move(node));
387  }
388  }
389  if (!v.empty())
390  {
391  const auto n_inserted = v.size();
392  v.insert(v.end(), nodes.rbegin(), nodes.rend());
393  ARMARX_CHECK_EQUAL(n_inserted + nodes.size(), v.size());
394  auto it = v.begin();
395  std::rotate(it, it + n_inserted, it + n_inserted + position);
396  return DefaultRapidXmlReaderNode(std::move(v));
397  }
398  }
399  return *this;
400  }
402  {
403  return add_node_at(std::vector{node}, position);
404  }
405 
407  {
408  if (pos >= nodes.size())
409  {
410  pos = nodes.size() - 1;
411  }
412  if (is_valid())
413  {
414  std::vector<RapidXmlReaderNode> v = nodes;
415  reverse(v.begin(), v.end());
416  v.erase(v.begin() + pos);
417  return DefaultRapidXmlReaderNode(std::move(v));
418  }
419  return *this;
420  }
421 
422  };
423 }
424 
425 
armarx::DefaultRapidXmlReaderNode::DefaultRapidXmlReaderNode
DefaultRapidXmlReaderNode(const RapidXmlReaderNode &n)
Definition: DefaultRapidXmlReader.h:93
armarx::DefaultRapidXmlReaderNode::attribute_as_string
std::string attribute_as_string(const std::string &attrName) const
Definition: DefaultRapidXmlReader.h:192
armarx::DefaultRapidXmlReaderNode::attribute_as_optional_bool
bool attribute_as_optional_bool(const char *name, const std::string &trueValue, const std::string &falseValue, bool defaultValue) const
Definition: DefaultRapidXmlReader.h:167
armarx::DefaultRapidXmlReaderNode::attribute_as_float
float attribute_as_float(const std::string &attrName) const
Definition: DefaultRapidXmlReader.h:221
armarx::DefaultRapidXmlReaderNode::value_as_optional_bool
bool value_as_optional_bool(const std::string &trueValue, const std::string &falseValue, bool defaultValue) const
Definition: DefaultRapidXmlReader.h:304
armarx::DefaultRapidXmlReaderNode::value_as_bool
bool value_as_bool(const std::string &trueValue, const std::string &falseValue) const
Definition: DefaultRapidXmlReader.h:278
armarx::DefaultRapidXmlReaderNode::attribute_as_optional_bool
bool attribute_as_optional_bool(const std::string &name, const std::string &trueValue, const std::string &falseValue, bool defaultValue) const
Definition: DefaultRapidXmlReader.h:163
armarx::DefaultRapidXmlReaderNode::getPathsString
std::string getPathsString() const
Definition: DefaultRapidXmlReader.h:348
armarx::DefaultRapidXmlReaderNode::attribute_as
T attribute_as(const std::string &attrName) const
Definition: DefaultRapidXmlReader.h:210
armarx::exceptions::local::RapidXmlReaderException
Definition: RapidXmlReader.h:45
armarx::DefaultRapidXmlReaderNode
Definition: DefaultRapidXmlReader.h:36
armarx::DefaultRapidXmlReaderNode::value_as_string
std::string value_as_string() const
Definition: DefaultRapidXmlReader.h:239
armarx::DefaultRapidXmlReaderNode::attribute_as_bool
bool attribute_as_bool(const char *attrName, const std::string &trueValue, const std::string &falseValue) const
Definition: DefaultRapidXmlReader.h:145
armarx::DefaultRapidXmlReaderNode::attribute_as_uint
uint32_t attribute_as_uint(const std::string &attrName) const
Definition: DefaultRapidXmlReader.h:230
armarx::DefaultRapidXmlReaderNode::value_as_uint16
uint16_t value_as_uint16() const
Definition: DefaultRapidXmlReader.h:260
armarx::DefaultRapidXmlReaderNode::attribute_as_uint
uint32_t attribute_as_uint(const char *attrName) const
Definition: DefaultRapidXmlReader.h:234
armarx::DefaultRapidXmlReaderNode::is_valid
bool is_valid() const
Definition: DefaultRapidXmlReader.h:322
armarx::DefaultRapidXmlReaderNode::value_as_uint32
uint32_t value_as_uint32() const
Definition: DefaultRapidXmlReader.h:254
armarx::DefaultRapidXmlReaderNode::has_attribute
bool has_attribute(const char *attrName) const
Definition: DefaultRapidXmlReader.h:128
armarx::reverse
T reverse(const T &o)
Definition: container.h:32
armarx::DefaultRapidXmlReaderNode::has_attribute
bool has_attribute(const std::string &attrName) const
Definition: DefaultRapidXmlReader.h:124
armarx::DefaultRapidXmlReaderNode::getPaths
std::vector< std::string > getPaths() const
Definition: DefaultRapidXmlReader.h:338
armarx::DefaultRapidXmlReaderNode::attribute_as
void attribute_as(const char *attrName, T &value) const
Definition: DefaultRapidXmlReader.h:205
armarx::DefaultRapidXmlReaderNode::value_as_int32
int32_t value_as_int32() const
Definition: DefaultRapidXmlReader.h:266
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::DefaultRapidXmlReaderNode::attribute_as_string
std::string attribute_as_string(const char *attrName) const
Definition: DefaultRapidXmlReader.h:196
armarx::DefaultRapidXmlReaderNode::attribute_as
T attribute_as(const char *attrName) const
Definition: DefaultRapidXmlReader.h:214
armarx::DefaultRapidXmlReaderNode::DefaultRapidXmlReaderNode
DefaultRapidXmlReaderNode()=default
armarx::DefaultRapidXmlReaderNode::value_as_int16
int16_t value_as_int16() const
Definition: DefaultRapidXmlReader.h:272
armarx::DefaultRapidXmlReaderNode::first_node
DefaultRapidXmlReaderNode first_node(const std::string &name) const
Definition: DefaultRapidXmlReader.h:97
armarx::RapidXmlReaderNode
Definition: RapidXmlReader.h:68
armarx::DefaultRapidXmlReaderNode::value_as_float
float value_as_float() const
Definition: DefaultRapidXmlReader.h:245
armarx::DefaultRapidXmlReaderNode::first_node
DefaultRapidXmlReaderNode first_node(const char *name=nullptr) const
Definition: DefaultRapidXmlReader.h:101
armarx::DefaultRapidXmlReaderNode::add_node_at
DefaultRapidXmlReaderNode add_node_at(std::vector< RapidXmlReaderNode > added_nodes, size_t position)
Definition: DefaultRapidXmlReader.h:371
armarx::RapidXmlReaderNode::value
std::string value() const
Definition: RapidXmlReader.h:313
armarx::DefaultRapidXmlReaderNode::DefaultRapidXmlReaderNode
DefaultRapidXmlReaderNode(std::vector< RapidXmlReaderNode > rnodes)
Definition: DefaultRapidXmlReader.h:81
ExpressionException.h
armarx::DefaultRapidXmlReaderNode::remove_node_at
DefaultRapidXmlReaderNode remove_node_at(size_t pos)
Definition: DefaultRapidXmlReader.h:406
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::DefaultRapidXmlReaderNode::operator=
DefaultRapidXmlReaderNode & operator=(DefaultRapidXmlReaderNode &&)=default
armarx::DefaultRapidXmlReaderNode::add_node_at_end
DefaultRapidXmlReaderNode add_node_at_end(DefaultRapidXmlReaderNode node)
Definition: DefaultRapidXmlReader.h:366
std
Definition: Application.h:66
RapidXmlReader.h
armarx::DefaultRapidXmlReaderNode::getChildPaths
std::vector< std::string > getChildPaths() const
Definition: DefaultRapidXmlReader.h:327
armarx::DefaultRapidXmlReaderNode::add_node_at_end
DefaultRapidXmlReaderNode add_node_at_end(RapidXmlReaderNode node)
Definition: DefaultRapidXmlReader.h:362
armarx::DefaultRapidXmlReaderNode::attribute_as_float
float attribute_as_float(const char *attrName) const
Definition: DefaultRapidXmlReader.h:225
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
rapidxml.hpp
armarx::DefaultRapidXmlReaderNode::add_node_at
DefaultRapidXmlReaderNode add_node_at(RapidXmlReaderNode node, size_t position)
Definition: DefaultRapidXmlReader.h:401
armarx::DefaultRapidXmlReaderNode::attribute_as
void attribute_as(const std::string &attrName, T &value) const
Definition: DefaultRapidXmlReader.h:201
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::DefaultRapidXmlReaderNode::attribute_as_bool
bool attribute_as_bool(const std::string &attrName, const std::string &trueValue, const std::string &falseValue) const
Definition: DefaultRapidXmlReader.h:141