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
27#include <set>
28#include <string>
29
33
34namespace armarx
35{
37 {
38 private:
39 std::vector<RapidXmlReaderNode> nodes;
40
41 void
42 check() const
43 {
44 if (nodes.empty())
45 {
47 "DefaultRapidXmlWrapper NullPointerException");
48 }
49 }
50
51 std::string
52 get_attrib_value(const char* attrName) const
53 {
54 check();
55 for (auto& node : nodes)
56 {
57 if (node.has_attribute(attrName))
58 {
59 return node.attribute_value(attrName);
60 }
61 }
62
63 //build error
64 std::stringstream ch;
65 ch << "\nchildren " << getChildPaths();
66 throw exceptions::local::RapidXmlReaderException(std::string("Attribute '") + attrName +
67 "' does not exist in node " +
68 getPathsString() + ch.str());
69 }
70
71 std::string
72 get_value() const
73 {
74 RapidXmlReaderNode n = nodes.front();
75 return n.value();
76 }
77
78 public:
84
85 DefaultRapidXmlReaderNode(std::vector<RapidXmlReaderNode> rnodes)
86 {
87 for (RapidXmlReaderNode& n : rnodes)
88 {
89 if (!n.is_valid())
90 {
92 "DefaultRapidXmlWrapper NullPointerException in constructor");
93 }
94 }
95 reverse(rnodes.begin(), rnodes.end());
96 nodes = std::move(rnodes);
97 }
98
103
105 first_node(const std::string& name) const
106 {
107 return first_node(name.c_str());
108 }
109
111 first_node(const char* name = nullptr) const
112 {
113 check();
114 std::vector<RapidXmlReaderNode> v;
115 for (const RapidXmlReaderNode& n : nodes)
116 {
117 if (n.get_node_ptr()->first_node(name) != nullptr)
118 {
119 v.push_back(n.first_node(name));
120 }
121 }
122 if (v.empty())
123 {
124 std::stringstream ch;
125 ch << "\nchildren " << getChildPaths();
127 std::string("Node with name '") + name + "' does not exist in node " +
128 getPathsString() + ch.str());
129 }
130 reverse(v.begin(), v.end());
131 return DefaultRapidXmlReaderNode(std::move(v));
132 }
133
134 bool
135 has_attribute(const std::string& attrName) const
136 {
137 return has_attribute(attrName.c_str());
138 }
139
140 bool
141 has_attribute(const char* attrName) const
142 {
143 check();
144 for (RapidXmlReaderNode n : nodes)
145 {
146 if (n.has_attribute(attrName))
147 {
148 return true;
149 }
150 }
151 return false;
152 }
153
154 bool
155 attribute_as_bool(const std::string& attrName,
156 const std::string& trueValue,
157 const std::string& falseValue) const
158 {
159 return attribute_as_bool(attrName.c_str(), trueValue, falseValue);
160 }
161
162 bool
163 attribute_as_bool(const char* attrName,
164 const std::string& trueValue,
165 const std::string& falseValue) const
166 {
167 std::string value = std::string(get_attrib_value(attrName));
168
169 if (value == trueValue)
170 {
171 return true;
172 }
173 else if (value == falseValue)
174 {
175 return false;
176 }
177 else
178 {
180 std::string("Invalid value '") + value + "' for attribute '" + attrName +
181 "'. Expecting '" + trueValue + "' or '" + falseValue + "'.");
182 }
183 }
184
185 bool
186 attribute_as_optional_bool(const std::string& name,
187 const std::string& trueValue,
188 const std::string& falseValue,
189 bool defaultValue) const
190 {
191 return attribute_as_optional_bool(name.c_str(), trueValue, falseValue, defaultValue);
192 }
193
194 bool
196 const std::string& trueValue,
197 const std::string& falseValue,
198 bool defaultValue) const
199 {
200 check();
201
202 if (!has_attribute(name))
203 {
204 return defaultValue;
205 }
206
207 std::string value = std::string(get_attrib_value(name));
208
209 if (value == trueValue)
210 {
211 return true;
212 }
213 else if (value == falseValue)
214 {
215 return false;
216 }
217 else
218 {
220 std::string("Invalid value '") + value + "' for attribute '" + name +
221 "'. Expecting '" + trueValue + "' or '" + falseValue + "'.");
222 }
223 }
224
225 std::string
226 attribute_as_string(const std::string& attrName) const
227 {
228 return attribute_as_string(attrName.c_str());
229 }
230
231 std::string
232 attribute_as_string(const char* attrName) const
233 {
234 return std::string(get_attrib_value(attrName));
235 }
236
237 template <class T>
238 void
239 attribute_as(const std::string& attrName, T& value) const
240 {
241 return attribute_as(attrName.c_str(), value);
242 }
243
244 template <class T>
245 void
246 attribute_as(const char* attrName, T& value) const
247 {
248 std::stringstream(get_attrib_value(attrName)) >> value;
249 }
250
251 template <class T>
252 T
253 attribute_as(const std::string& attrName) const
254 {
255 return attribute_as<T>(attrName.c_str());
256 }
257
258 template <class T>
259 T
260 attribute_as(const char* attrName) const
261 {
262 T val;
263 attribute_as(attrName, val);
264 return val;
265 }
266
267 float
268 attribute_as_float(const std::string& attrName) const
269 {
270 return attribute_as_float(attrName.c_str());
271 }
272
273 float
274 attribute_as_float(const char* attrName) const
275 {
276 return attribute_as<float>(attrName);
277 }
278
279 uint32_t
280 attribute_as_uint(const std::string& attrName) const
281 {
282 return attribute_as_uint(attrName.c_str());
283 }
284
285 uint32_t
286 attribute_as_uint(const char* attrName) const
287 {
288 return attribute_as<uint32_t>(attrName);
289 }
290
291 std::string
293 {
294 check();
295 return std::string(get_value());
296 }
297
298 float
300 {
301 check();
302 std::stringstream strValue(get_value());
303 float retValue;
304 strValue >> retValue;
305 return retValue;
306 }
307
308 uint32_t
310 {
311 check();
312 return static_cast<uint32_t>(std::stoul(get_value(), nullptr, 0));
313 }
314
315 uint16_t
317 {
318 check();
319 return static_cast<uint16_t>(std::stoul(get_value(), nullptr, 0));
320 }
321
322 int32_t
324 {
325 check();
326 return static_cast<int32_t>(std::stoul(get_value(), nullptr, 0));
327 }
328
329 int16_t
331 {
332 check();
333 return static_cast<int16_t>(std::stoul(get_value(), nullptr, 0));
334 }
335
336 bool
337 value_as_bool(const std::string& trueValue, const std::string& falseValue) const
338 {
339 check();
340 std::string value = std::string(get_value());
341 if (value == trueValue)
342 {
343 return true;
344 }
345 else if (value == falseValue)
346 {
347 return false;
348 }
349 else
350 {
351 std::string s = "(";
352 for (RapidXmlReaderNode node : nodes)
353 {
354 s += std::string(node.name());
355 s += ", ";
356 }
357 s = s.substr(0, s.length() - 2);
358 s += ")";
360 std::string("Invalid value '") + value + "' for represented nodes " + s +
361 ". Expecting '" + trueValue + "' or '" + falseValue + "'.");
362 }
363 }
364
365 bool
366 value_as_optional_bool(const std::string& trueValue,
367 const std::string& falseValue,
368 bool defaultValue) const
369 {
370 check();
371 std::string value = std::string(get_value());
372 if (value == trueValue)
373 {
374 return true;
375 }
376 else if (value == falseValue)
377 {
378 return false;
379 }
380 else
381 {
382 return defaultValue;
383 }
384 }
385
386 bool
387 is_valid() const
388 {
389 return !nodes.empty();
390 }
391
392 std::vector<std::string>
394 {
395 check();
396 std::set<std::string> results;
397 for (const RapidXmlReaderNode& n : nodes)
398 {
399 const auto paths = n.getChildPaths();
400 results.insert(paths.begin(), paths.end());
401 }
402 return {results.begin(), results.end()};
403 }
404
405 std::vector<std::string>
406 getPaths() const
407 {
408 check();
409 std::vector<std::string> results;
410 for (const RapidXmlReaderNode& n : nodes)
411 {
412 results.push_back(n.getPath());
413 }
414 return results;
415 }
416
417 std::string
419 {
420 std::vector<std::string> paths = getPaths();
421 std::string all_paths;
422 for (std::string path : paths)
423 {
424 all_paths += path;
425 all_paths += " or ";
426 }
427 all_paths = all_paths.substr(0, all_paths.length() - 4);
428 return all_paths;
429 }
430
433 {
434 return add_node_at(node, nodes.size());
435 }
436
439 {
440 return add_node_at(node.nodes, nodes.size());
441 }
442
444 add_node_at(std::vector<RapidXmlReaderNode> added_nodes, size_t position)
445 {
446 if (position > nodes.size())
447 {
449 "The index is out of range: given index ")
450 << position << " size of container: " << nodes.size();
451 }
452
453 if (!added_nodes.empty())
454 {
455 std::vector<RapidXmlReaderNode> v;
456 v.reserve(added_nodes.size() + nodes.size());
457 for (auto& node : added_nodes)
458 {
459 if (node.is_valid())
460 {
461 v.emplace_back(std::move(node));
462 }
463 }
464 if (!v.empty())
465 {
466 const auto n_inserted = v.size();
467 v.insert(v.end(), nodes.rbegin(), nodes.rend());
468 ARMARX_CHECK_EQUAL(n_inserted + nodes.size(), v.size());
469 auto it = v.begin();
470 std::rotate(it, it + n_inserted, it + n_inserted + position);
471 return DefaultRapidXmlReaderNode(std::move(v));
472 }
473 }
474 return *this;
475 }
476
478 add_node_at(RapidXmlReaderNode node, size_t position)
479 {
480 return add_node_at(std::vector{node}, position);
481 }
482
484 remove_node_at(size_t pos)
485 {
486 if (pos >= nodes.size())
487 {
488 pos = nodes.size() - 1;
489 }
490 if (is_valid())
491 {
492 std::vector<RapidXmlReaderNode> v = nodes;
493 reverse(v.begin(), v.end());
494 v.erase(v.begin() + pos);
495 return DefaultRapidXmlReaderNode(std::move(v));
496 }
497 return *this;
498 }
499 };
500} // namespace armarx
DefaultRapidXmlReaderNode first_node(const char *name=nullptr) const
void attribute_as(const char *attrName, T &value) const
bool attribute_as_bool(const char *attrName, const std::string &trueValue, const std::string &falseValue) const
DefaultRapidXmlReaderNode & operator=(DefaultRapidXmlReaderNode const &)=default
DefaultRapidXmlReaderNode(DefaultRapidXmlReaderNode &&)=default
std::string attribute_as_string(const std::string &attrName) const
bool has_attribute(const char *attrName) const
bool value_as_bool(const std::string &trueValue, const std::string &falseValue) const
void attribute_as(const std::string &attrName, T &value) const
DefaultRapidXmlReaderNode add_node_at(std::vector< RapidXmlReaderNode > added_nodes, size_t position)
DefaultRapidXmlReaderNode first_node(const std::string &name) const
uint32_t attribute_as_uint(const std::string &attrName) const
std::string attribute_as_string(const char *attrName) const
bool value_as_optional_bool(const std::string &trueValue, const std::string &falseValue, bool defaultValue) const
T attribute_as(const char *attrName) const
bool attribute_as_bool(const std::string &attrName, const std::string &trueValue, const std::string &falseValue) const
DefaultRapidXmlReaderNode remove_node_at(size_t pos)
uint32_t attribute_as_uint(const char *attrName) const
float attribute_as_float(const char *attrName) const
bool has_attribute(const std::string &attrName) const
T attribute_as(const std::string &attrName) const
DefaultRapidXmlReaderNode add_node_at(RapidXmlReaderNode node, size_t position)
std::vector< std::string > getChildPaths() const
bool attribute_as_optional_bool(const std::string &name, const std::string &trueValue, const std::string &falseValue, bool defaultValue) const
float attribute_as_float(const std::string &attrName) const
DefaultRapidXmlReaderNode & operator=(DefaultRapidXmlReaderNode &&)=default
DefaultRapidXmlReaderNode(std::vector< RapidXmlReaderNode > rnodes)
DefaultRapidXmlReaderNode add_node_at_end(DefaultRapidXmlReaderNode node)
DefaultRapidXmlReaderNode add_node_at_end(RapidXmlReaderNode node)
std::vector< std::string > getPaths() const
DefaultRapidXmlReaderNode(const RapidXmlReaderNode &n)
bool attribute_as_optional_bool(const char *name, const std::string &trueValue, const std::string &falseValue, bool defaultValue) const
DefaultRapidXmlReaderNode(DefaultRapidXmlReaderNode const &)=default
#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...
This file offers overloads of toIce() and fromIce() functions for STL container types.
T reverse(const T &o)
Definition container.h:33
This file contains rapidxml parser and DOM implementation.