DART  6.10.1
XmlHelpers.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2021, 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 
38 #include <Eigen/Dense>
39 #include <boost/algorithm/string.hpp>
40 #include <boost/lexical_cast.hpp>
41 #include <tinyxml2.h>
42 
43 #include "dart/common/Console.hpp"
46 #include "dart/math/MathTypes.hpp"
47 
48 namespace dart {
49 namespace utils {
50 
51 std::string toString(bool v);
52 std::string toString(int v);
53 std::string toString(unsigned int v);
54 std::string toString(float v);
55 std::string toString(double v);
56 std::string toString(char v);
57 std::string toString(const Eigen::Vector2d& v);
58 std::string toString(const Eigen::Vector3d& v);
59 std::string toString(const Eigen::Vector3i& v);
60 std::string toString(const Eigen::Vector6d& v);
61 std::string toString(const Eigen::VectorXd& v);
62 std::string toString(const Eigen::Isometry3d& v);
63 
64 bool toBool(const std::string& str);
65 int toInt(const std::string& str);
66 unsigned int toUInt(const std::string& str);
67 float toFloat(const std::string& str);
68 double toDouble(const std::string& str);
69 char toChar(const std::string& str);
70 Eigen::Vector2d toVector2d(const std::string& str);
71 Eigen::Vector2i toVector2i(const std::string& str);
72 Eigen::Vector3d toVector3d(const std::string& str);
73 Eigen::Vector3i toVector3i(const std::string& str);
74 Eigen::Vector4d toVector4d(const std::string& str);
75 Eigen::Vector6d toVector6d(const std::string& str);
76 Eigen::VectorXd toVectorXd(const std::string& str);
77 template <std::size_t N>
78 Eigen::Matrix<double, N, 1> toVectorNd(const std::string& str)
79 {
80  Eigen::Matrix<double, N, 1> ret = Eigen::Matrix<double, N, 1>::Zero();
81 
82  std::vector<std::string> pieces;
83  std::string trimedStr = boost::trim_copy(str);
84  boost::split(
85  pieces, trimedStr, boost::is_any_of(" "), boost::token_compress_on);
86  std::size_t sizeToRead = std::min(N, pieces.size());
87  if (pieces.size() < N)
88  {
89  dterr << "Failed to read a vector because the dimension '" << pieces.size()
90  << "' is less than the expectation '" << N << "'.\n";
91  }
92  else if (pieces.size() > N)
93  {
94  dterr << "Failed to read a vector because the dimension '" << pieces.size()
95  << "' is greater than the expectation '" << N << "'.\n";
96  }
97 
98  for (std::size_t i = 0; i < sizeToRead; ++i)
99  {
100  if (pieces[i] != "")
101  {
102  try
103  {
104  ret(i) = boost::lexical_cast<double>(pieces[i].c_str());
105  }
106  catch (boost::bad_lexical_cast& e)
107  {
108  dterr << "value [" << pieces[i]
109  << "] is not a valid double for Eigen::Vector" << N << "d[" << i
110  << "]: " << e.what() << "\n";
111  }
112  }
113  }
114 
115  return ret;
116 }
117 // TODO: The definition of _str is not clear for transform (see: #250)
118 Eigen::Isometry3d toIsometry3d(const std::string& str);
119 Eigen::Isometry3d toIsometry3dWithExtrinsicRotation(const std::string& str);
120 
121 std::string getValueString(
122  const tinyxml2::XMLElement* parentElement, const std::string& name);
123 bool getValueBool(
124  const tinyxml2::XMLElement* parentElement, const std::string& name);
125 int getValueInt(
126  const tinyxml2::XMLElement* parentElement, const std::string& name);
127 unsigned int getValueUInt(
128  const tinyxml2::XMLElement* parentElement, const std::string& name);
129 float getValueFloat(
130  const tinyxml2::XMLElement* parentElement, const std::string& name);
131 double getValueDouble(
132  const tinyxml2::XMLElement* parentElement, const std::string& name);
133 char getValueChar(
134  const tinyxml2::XMLElement* parentElement, const std::string& name);
135 Eigen::Vector2d getValueVector2d(
136  const tinyxml2::XMLElement* parentElement, const std::string& name);
137 Eigen::Vector3d getValueVector3d(
138  const tinyxml2::XMLElement* parentElement, const std::string& name);
139 Eigen::Vector3i getValueVector3i(
140  const tinyxml2::XMLElement* parentElement, const std::string& name);
142  const tinyxml2::XMLElement* parentElement, const std::string& name);
143 Eigen::VectorXd getValueVectorXd(
144  const tinyxml2::XMLElement* parentElement, const std::string& name);
145 Eigen::Isometry3d getValueIsometry3d(
146  const tinyxml2::XMLElement* parentElement, const std::string& name);
148  const tinyxml2::XMLElement* parentElement, const std::string& name);
149 
150 // TODO(JS): Deprecate
151 void openXMLFile(
152  tinyxml2::XMLDocument& doc,
153  const common::Uri& uri,
154  const common::ResourceRetrieverPtr& retriever = nullptr);
155 
156 bool readXmlFile(
157  tinyxml2::XMLDocument& doc,
158  const common::Uri& uri,
159  const common::ResourceRetrieverPtr& retrieverOrNullPtr = nullptr);
160 
161 bool hasElement(
162  const tinyxml2::XMLElement* parentElement, const std::string& name);
163 
164 const tinyxml2::XMLElement* getElement(
165  const tinyxml2::XMLElement* parentElement, const std::string& name);
166 
167 tinyxml2::XMLElement* getElement(
168  tinyxml2::XMLElement* parentElement, const std::string& name);
169 
170 bool hasAttribute(const tinyxml2::XMLElement* element, const char* const name);
171 
172 // Please use getAttributeString() instead.
173 DART_DEPRECATED(6.0)
174 std::string getAttribute(tinyxml2::XMLElement* element, const char* const name);
175 
176 // Please use getAttributeDouble() instead.
177 DART_DEPRECATED(6.0)
178 void getAttribute(
179  tinyxml2::XMLElement* element, const char* const name, double* d);
180 
181 std::string getAttributeString(
182  const tinyxml2::XMLElement* element, const std::string& attributeName);
183 bool getAttributeBool(
184  const tinyxml2::XMLElement* element, const std::string& attributeName);
185 int getAttributeInt(
186  const tinyxml2::XMLElement* element, const std::string& attributeName);
187 unsigned int getAttributeUInt(
188  const tinyxml2::XMLElement* element, const std::string& attributeName);
189 float getAttributeFloat(
190  const tinyxml2::XMLElement* element, const std::string& attributeName);
191 double getAttributeDouble(
192  const tinyxml2::XMLElement* element, const std::string& attributeName);
193 char getAttributeChar(
194  const tinyxml2::XMLElement* element, const std::string& attributeName);
195 Eigen::Vector2i getAttributeVector2i(
196  const tinyxml2::XMLElement* element, const std::string& attributeName);
197 Eigen::Vector2d getAttributeVector2d(
198  const tinyxml2::XMLElement* element, const std::string& attributeName);
199 Eigen::Vector3d getAttributeVector3d(
200  const tinyxml2::XMLElement* element, const std::string& attributeName);
201 Eigen::Vector4d getAttributeVector4d(
202  const tinyxml2::XMLElement* element, const std::string& attributeName);
204  const tinyxml2::XMLElement* element, const std::string& attributeName);
205 Eigen::VectorXd getAttributeVectorXd(
206  const tinyxml2::XMLElement* element, const std::string& attributeName);
207 template <std::size_t N>
208 Eigen::Matrix<double, N, 1> getAttributeVectorNd(
209  const tinyxml2::XMLElement* element, const std::string& attributeName)
210 {
211  const std::string val = getAttributeString(element, attributeName);
212  return toVectorNd<N>(val);
213 }
214 
218 template <typename ElementType>
220 {
221 protected:
222  using ElementPtr = ElementType*;
223  using ElementRef = ElementType&;
224 
225 public:
228  ElementPtr parentElement, const std::string& childElementName)
229  : mParentElement(parentElement),
230  mChildElementName(childElementName),
231  mCurrentElement(nullptr)
232  {
233  }
234 
237  {
238  }
239 
242  bool next()
243  {
244  if (!mParentElement)
245  return false;
246 
247  if (mCurrentElement)
248  {
250  = mCurrentElement->NextSiblingElement(mChildElementName.c_str());
251  }
252  else
253  {
255  = mParentElement->FirstChildElement(mChildElementName.c_str());
256  }
257 
258  if (!valid())
259  mParentElement = nullptr;
260 
261  return valid();
262  }
263 
265  ElementPtr get() const
266  {
267  return mCurrentElement;
268  }
269 
272  {
273  return mCurrentElement;
274  }
275 
278  {
279  return *mCurrentElement;
280  }
281 
284  {
285  // If they point at the same node, then the names must match
286  return (this->mParentElement == rhs.mParentElement)
287  && (this->mCurrentElement == rhs.mCurrentElement)
288  && (this->mCurrentElement != nullptr
289  || (this->mChildElementName == rhs.mChildElementName));
290  }
291 
295  {
296  this->mParentElement = rhs.mParentElement;
298  this->mCurrentElement = rhs.mCurrentElement;
299 
300  return *this;
301  }
302 
303 private:
305  bool valid() const
306  {
307  return mCurrentElement != nullptr;
308  }
309 
310 private:
313 
315  std::string mChildElementName;
316 
319 };
320 
321 // ElementEnumerator is for iterating elements for
325 
326 bool copyNode(tinyxml2::XMLNode* destParent, const tinyxml2::XMLNode& src);
327 
328 bool copyChildNodes(
329  tinyxml2::XMLNode* destParent, const tinyxml2::XMLNode& src);
330 
331 } // namespace utils
332 } // namespace dart
333 
334 #endif // #ifndef DART_UTILS_XMLHELPERS_HPP_
#define dterr
Output an error message.
Definition: Console.hpp:49
#define DART_DEPRECATED(version)
Definition: Deprecated.hpp:51
std::string * name
Definition: SkelParser.cpp:1697
TemplatedElementEnumerator is a convenience class to help visiting all the child elements of given pa...
Definition: XmlHelpers.hpp:220
bool next()
Set the current element to the next sibling element or to the first child element of given parent ele...
Definition: XmlHelpers.hpp:242
ElementType & ElementRef
Definition: XmlHelpers.hpp:223
ElementPtr get() const
Get the current element.
Definition: XmlHelpers.hpp:265
~TemplatedElementEnumerator()
Destructor.
Definition: XmlHelpers.hpp:236
TemplatedElementEnumerator< ElementType > & operator=(const TemplatedElementEnumerator< ElementType > &rhs)
Assignment operator.
Definition: XmlHelpers.hpp:293
ElementPtr operator->() const
Dereference operator.
Definition: XmlHelpers.hpp:271
bool operator==(const TemplatedElementEnumerator< ElementType > &rhs) const
Equality operator.
Definition: XmlHelpers.hpp:283
ElementPtr mCurrentElement
Currently visiting child element.
Definition: XmlHelpers.hpp:318
bool valid() const
Returns true if the current element is valid (not a nullptr)
Definition: XmlHelpers.hpp:305
std::string mChildElementName
Child element name.
Definition: XmlHelpers.hpp:315
ElementRef operator*() const
Dereference operator.
Definition: XmlHelpers.hpp:277
ElementType * ElementPtr
Definition: XmlHelpers.hpp:222
TemplatedElementEnumerator(ElementPtr parentElement, const std::string &childElementName)
Constructor that takes parent element and.
Definition: XmlHelpers.hpp:227
ElementPtr mParentElement
Parent element.
Definition: XmlHelpers.hpp:312
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:650
double getAttributeDouble(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:883
bool getValueBool(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:472
unsigned int toUInt(const std::string &str)
Definition: XmlHelpers.cpp:151
Eigen::Vector6d getValueVector6d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:590
Eigen::Vector4d toVector4d(const std::string &str)
Definition: XmlHelpers.cpp:298
Eigen::Isometry3d getValueIsometry3dWithExtrinsicRotation(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:638
bool copyChildNodes(tinyxml2::XMLNode *destParent, const tinyxml2::XMLNode &src)
Definition: XmlHelpers.cpp:999
unsigned int getValueUInt(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:506
Eigen::Isometry3d toIsometry3d(const std::string &str)
Definition: XmlHelpers.cpp:391
Eigen::Vector3d getAttributeVector3d(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:928
Eigen::Vector2i toVector2i(const std::string &str)
Definition: XmlHelpers.cpp:205
char getAttributeChar(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:901
Eigen::VectorXd getValueVectorXd(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:602
const tinyxml2::XMLElement * getElement(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:661
Eigen::Vector3d toVector3d(const std::string &str)
Definition: XmlHelpers.cpp:236
double toDouble(const std::string &str)
Definition: XmlHelpers.cpp:163
float getAttributeFloat(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:865
Eigen::Vector6d getAttributeVector6d(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:946
bool copyNode(tinyxml2::XMLNode *destParent, const tinyxml2::XMLNode &src)
Definition: XmlHelpers.cpp:964
std::string getAttribute(tinyxml2::XMLElement *element, const char *const name)
Definition: XmlHelpers.cpp:780
Eigen::Isometry3d getValueIsometry3d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:626
Eigen::Vector4d getAttributeVector4d(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:937
int toInt(const std::string &str)
Definition: XmlHelpers.cpp:145
Eigen::Vector3d getValueVector3d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:566
Eigen::VectorXd getAttributeVectorXd(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:955
Eigen::Vector2d toVector2d(const std::string &str)
Definition: XmlHelpers.cpp:174
Eigen::Matrix< double, N, 1 > toVectorNd(const std::string &str)
Definition: XmlHelpers.hpp:78
float getValueFloat(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:518
Eigen::Vector3i toVector3i(const std::string &str)
Definition: XmlHelpers.cpp:267
Eigen::VectorXd toVectorXd(const std::string &str)
Definition: XmlHelpers.cpp:360
Eigen::Isometry3d toIsometry3dWithExtrinsicRotation(const std::string &str)
Definition: XmlHelpers.cpp:424
float toFloat(const std::string &str)
Definition: XmlHelpers.cpp:157
int getValueInt(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:494
std::string getAttributeString(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:793
char toChar(const std::string &str)
Definition: XmlHelpers.cpp:168
Eigen::Vector3i getValueVector3i(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:578
Eigen::Vector2d getValueVector2d(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:554
Eigen::Matrix< double, N, 1 > getAttributeVectorNd(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.hpp:208
std::string toString(bool v)
Definition: XmlHelpers.cpp:49
char getValueChar(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:542
std::string getValueString(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:460
Eigen::Vector2d getAttributeVector2d(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:919
bool toBool(const std::string &str)
Definition: XmlHelpers.cpp:130
bool getAttributeBool(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:810
double getValueDouble(const tinyxml2::XMLElement *parentElement, const std::string &name)
Definition: XmlHelpers.cpp:530
Eigen::Vector6d toVector6d(const std::string &str)
Definition: XmlHelpers.cpp:329
int getAttributeInt(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:828
bool readXmlFile(tinyxml2::XMLDocument &doc, const common::Uri &uri, const common::ResourceRetrieverPtr &retrieverOrNullPtr)
Definition: XmlHelpers.cpp:749
bool hasAttribute(const tinyxml2::XMLElement *element, const char *const name)
Definition: XmlHelpers.cpp:773
void openXMLFile(tinyxml2::XMLDocument &doc, const common::Uri &uri, const common::ResourceRetrieverPtr &retrieverOrNullPtr)
Definition: XmlHelpers.cpp:727
Eigen::Vector2i getAttributeVector2i(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:910
unsigned int getAttributeUInt(const tinyxml2::XMLElement *element, const std::string &attributeName)
Definition: XmlHelpers.cpp:846
Definition: BulletCollisionDetector.cpp:65
Definition: SharedLibraryManager.hpp:46
The Uri struct provides URI parsing and merging functionality based on RFC 3986.
Definition: Uri.hpp:87