DART  6.7.3
XmlHelpers.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2019, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_UTILS_XMLHELPERS_HPP_
34 #define DART_UTILS_XMLHELPERS_HPP_
35 
36 #include <string>
37 #include <Eigen/Dense>
38 #include <tinyxml2.h>
39 
42 #include "dart/math/MathTypes.hpp"
43 
44 namespace dart {
45 namespace utils {
46 
47 std::string toString(bool v);
48 std::string toString(int v);
49 std::string toString(unsigned int v);
50 std::string toString(float v);
51 std::string toString(double v);
52 std::string toString(char v);
53 std::string toString(const Eigen::Vector2d& v);
54 std::string toString(const Eigen::Vector3d& v);
55 std::string toString(const Eigen::Vector3i& v);
56 std::string toString(const Eigen::Vector6d& v);
57 std::string toString(const Eigen::VectorXd& v);
58 std::string toString(const Eigen::Isometry3d& v);
59 
60 bool toBool (const std::string& str);
61 int toInt (const std::string& str);
62 unsigned int toUInt (const std::string& str);
63 float toFloat (const std::string& str);
64 double toDouble (const std::string& str);
65 char toChar (const std::string& str);
66 Eigen::Vector2d toVector2d (const std::string& str);
67 Eigen::Vector3d toVector3d (const std::string& str);
68 Eigen::Vector3i toVector3i (const std::string& str);
69 Eigen::Vector6d toVector6d (const std::string& str);
70 Eigen::VectorXd toVectorXd (const std::string& str);
71 // TODO: The definition of _str is not clear for transform (see: #250)
72 Eigen::Isometry3d toIsometry3d(const std::string& str);
73 Eigen::Isometry3d toIsometry3dWithExtrinsicRotation(const std::string& str);
74 
75 std::string getValueString (const tinyxml2::XMLElement* parentElement, const std::string& name);
76 bool getValueBool (const tinyxml2::XMLElement* parentElement, const std::string& name);
77 int getValueInt (const tinyxml2::XMLElement* parentElement, const std::string& name);
78 unsigned int getValueUInt (const tinyxml2::XMLElement* parentElement, const std::string& name);
79 float getValueFloat (const tinyxml2::XMLElement* parentElement, const std::string& name);
80 double getValueDouble (const tinyxml2::XMLElement* parentElement, const std::string& name);
81 char getValueChar (const tinyxml2::XMLElement* parentElement, const std::string& name);
82 Eigen::Vector2d getValueVector2d (const tinyxml2::XMLElement* parentElement, const std::string& name);
83 Eigen::Vector3d getValueVector3d (const tinyxml2::XMLElement* parentElement, const std::string& name);
84 Eigen::Vector3i getValueVector3i (const tinyxml2::XMLElement* parentElement, const std::string& name);
85 Eigen::Vector6d getValueVector6d (const tinyxml2::XMLElement* parentElement, const std::string& name);
86 Eigen::VectorXd getValueVectorXd (const tinyxml2::XMLElement* parentElement, const std::string& name);
87 Eigen::Isometry3d getValueIsometry3d(const tinyxml2::XMLElement* parentElement, const std::string& name);
88 Eigen::Isometry3d getValueIsometry3dWithExtrinsicRotation(const tinyxml2::XMLElement* parentElement, const std::string& name);
89 
90 void openXMLFile(tinyxml2::XMLDocument& doc,
91  const common::Uri& uri,
92  const common::ResourceRetrieverPtr& retriever = nullptr);
93 
94 bool hasElement(const tinyxml2::XMLElement* parentElement,
95  const std::string& name);
96 
97 const tinyxml2::XMLElement* getElement(
98  const tinyxml2::XMLElement* parentElement,
99  const std::string& name);
100 
101 tinyxml2::XMLElement* getElement(tinyxml2::XMLElement* parentElement,
102  const std::string& name);
103 
104 bool hasAttribute(const tinyxml2::XMLElement* element, const char* const name);
105 
106 // Please use getAttributeString() instead.
107 DART_DEPRECATED(6.0)
108 std::string getAttribute(tinyxml2::XMLElement* element, const char* const name);
109 
110 // Please use getAttributeDouble() instead.
111 DART_DEPRECATED(6.0)
112 void getAttribute(tinyxml2::XMLElement* element, const char* const name, double* d);
113 
114 std::string getAttributeString (const tinyxml2::XMLElement* element, const std::string& attributeName);
115 bool getAttributeBool (const tinyxml2::XMLElement* element, const std::string& attributeName);
116 int getAttributeInt (const tinyxml2::XMLElement* element, const std::string& attributeName);
117 unsigned int getAttributeUInt (const tinyxml2::XMLElement* element, const std::string& attributeName);
118 float getAttributeFloat (const tinyxml2::XMLElement* element, const std::string& attributeName);
119 double getAttributeDouble (const tinyxml2::XMLElement* element, const std::string& attributeName);
120 char getAttributeChar (const tinyxml2::XMLElement* element, const std::string& attributeName);
121 Eigen::Vector2d getAttributeVector2d(const tinyxml2::XMLElement* element, const std::string& attributeName);
122 Eigen::Vector3d getAttributeVector3d(const tinyxml2::XMLElement* element, const std::string& attributeName);
123 Eigen::Vector6d getAttributeVector6d(const tinyxml2::XMLElement* element, const std::string& attributeName);
124 Eigen::VectorXd getAttributeVectorXd(const tinyxml2::XMLElement* element, const std::string& attributeName);
125 
129 template <typename ElementType>
131 {
132 protected:
133 
134  using ElementPtr = ElementType*;
135  using ElementRef = ElementType&;
136 
137 public:
138 
141  const std::string& childElementName)
142  : mParentElement(parentElement),
143  mChildElementName(childElementName),
144  mCurrentElement(nullptr)
145  {
146  }
147 
150 
153  bool next()
154  {
155  if (!mParentElement)
156  return false;
157 
158  if (mCurrentElement)
159  {
160  mCurrentElement
161  = mCurrentElement->NextSiblingElement(mChildElementName.c_str());
162  }
163  else
164  {
165  mCurrentElement
166  = mParentElement->FirstChildElement(mChildElementName.c_str());
167  }
168 
169  if (!valid())
170  mParentElement = nullptr;
171 
172  return valid();
173  }
174 
176  ElementPtr get() const { return mCurrentElement; }
177 
179  ElementPtr operator->() const { return mCurrentElement; }
180 
182  ElementRef operator*() const { return *mCurrentElement; }
183 
186  {
187  // If they point at the same node, then the names must match
188  return (this->mParentElement == rhs.mParentElement)
189  && (this->mCurrentElement == rhs.mCurrentElement)
190  && (this->mCurrentElement != nullptr
191  || (this->mChildElementName == rhs.mChildElementName));
192  }
193 
197  {
198  this->mParentElement = rhs.mParentElement;
199  this->mChildElementName = rhs.mChildElementName;
200  this->mCurrentElement = rhs.mCurrentElement;
201 
202  return *this;
203  }
204 
205 private:
206 
208  bool valid() const
209  {
210  return mCurrentElement != nullptr;
211  }
212 
213 private:
214 
217 
219  std::string mChildElementName;
220 
223 
224 };
225 
226 // ElementEnumerator is for iterating elements for
231 
232 } // namespace utils
233 } // namespace dart
234 
235 #endif // #ifndef DART_UTILS_XMLHELPERS_HPP_
#define DART_DEPRECATED(version)
Definition: Deprecated.hpp:51
bool valid
Definition: SkelParser.cpp:1618
std::string * name
Definition: SkelParser.cpp:1642
TemplatedElementEnumerator is a convenience class to help visiting all the child elements of given pa...
Definition: XmlHelpers.hpp:131
bool next()
Set the current element to the next sibling element or to the first child element of given parent ele...
Definition: XmlHelpers.hpp:153
ElementType & ElementRef
Definition: XmlHelpers.hpp:135
ElementPtr get() const
Get the current element.
Definition: XmlHelpers.hpp:176
~TemplatedElementEnumerator()
Destructor.
Definition: XmlHelpers.hpp:149
TemplatedElementEnumerator< ElementType > & operator=(const TemplatedElementEnumerator< ElementType > &rhs)
Assignment operator.
Definition: XmlHelpers.hpp:195
ElementPtr operator->() const
Dereference operator.
Definition: XmlHelpers.hpp:179
bool operator==(const TemplatedElementEnumerator< ElementType > &rhs) const
Equality operator.
Definition: XmlHelpers.hpp:185
ElementPtr mCurrentElement
Currently visiting child element.
Definition: XmlHelpers.hpp:222
bool valid() const
Returns true if the current element is valid (not a nullptr)
Definition: XmlHelpers.hpp:208
std::string mChildElementName
Child element name.
Definition: XmlHelpers.hpp:219
ElementRef operator*() const
Dereference operator.
Definition: XmlHelpers.hpp:182
ElementType * ElementPtr
Definition: XmlHelpers.hpp:134
TemplatedElementEnumerator(ElementPtr parentElement, const std::string &childElementName)
Constructor that takes parent element and.
Definition: XmlHelpers.hpp:140
ElementPtr mParentElement
Parent element.
Definition: XmlHelpers.hpp:216
Definition: Random-impl.hpp:92
Matrix< double, 6, 1 > Vector6d
Definition: MathTypes.hpp:49
std::shared_ptr< ResourceRetriever > ResourceRetrieverPtr
Definition: ResourceRetriever.hpp:76
bool hasElement(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:616
double getAttributeDouble(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:780
bool getValueBool(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:435
unsigned int toUInt(const std::string &str)
Definition: XmlHelpers.cpp:154
Eigen::Vector6d getValueVector6d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:556
Eigen::Isometry3d getValueIsometry3dWithExtrinsicRotation(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:604
unsigned int getValueUInt(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:472
Eigen::Isometry3d toIsometry3d(const std::string &str)
Definition: XmlHelpers.cpp:346
Eigen::Vector3d getAttributeVector3d(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:817
char getAttributeChar(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:799
Eigen::VectorXd getValueVectorXd(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:568
const tinyxml2::XMLElement * getElement(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:627
Eigen::Vector3d toVector3d(const std::string &str)
Definition: XmlHelpers.cpp:210
double toDouble(const std::string &str)
Definition: XmlHelpers.cpp:166
float getAttributeFloat(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:761
Eigen::Vector6d getAttributeVector6d(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:826
std::string getAttribute(tinyxml2::XMLElement *element, const char *const name)
Definition: XmlHelpers.cpp:674
Eigen::Isometry3d getValueIsometry3d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:592
int toInt(const std::string &str)
Definition: XmlHelpers.cpp:148
Eigen::Vector3d getValueVector3d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:532
Eigen::VectorXd getAttributeVectorXd(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:835
Eigen::Vector2d toVector2d(const std::string &str)
Definition: XmlHelpers.cpp:177
float getValueFloat(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:484
Eigen::Vector3i toVector3i(const std::string &str)
Definition: XmlHelpers.cpp:244
Eigen::VectorXd toVectorXd(const std::string &str)
Definition: XmlHelpers.cpp:312
Eigen::Isometry3d toIsometry3dWithExtrinsicRotation(const std::string &str)
Definition: XmlHelpers.cpp:382
float toFloat(const std::string &str)
Definition: XmlHelpers.cpp:160
int getValueInt(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:460
std::string getAttributeString(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:688
char toChar(const std::string &str)
Definition: XmlHelpers.cpp:171
Eigen::Vector3i getValueVector3i(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:544
Eigen::Vector2d getValueVector2d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:520
std::string toString(bool v)
Definition: XmlHelpers.cpp:48
char getValueChar(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:508
std::string getValueString(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:423
Eigen::Vector2d getAttributeVector2d(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:808
bool toBool(const std::string &str)
Definition: XmlHelpers.cpp:130
bool getAttributeBool(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:705
double getValueDouble(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:496
Eigen::Vector6d toVector6d(const std::string &str)
Definition: XmlHelpers.cpp:278
int getAttributeInt(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:724
bool hasAttribute(const tinyxml2::XMLElement *element, const char *const name)
Definition: XmlHelpers.cpp:667
void openXMLFile(tinyxml2::XMLDocument &doc, const common::Uri &uri, const common::ResourceRetrieverPtr &retrieverOrNullPtr)
Definition: XmlHelpers.cpp:646
unsigned int getAttributeUInt(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:742
Definition: BulletCollisionDetector.cpp:63
Definition: SharedLibraryManager.hpp:43