1 /* 2 Open Asset Import Library (assimp) 3 ---------------------------------------------------------------------- 4 5 Copyright (c) 2006-2021, assimp team 6 7 All rights reserved. 8 9 Redistribution and use of this software in source and binary forms, 10 with or without modification, are permitted provided that the 11 following conditions are met: 12 13 * Redistributions of source code must retain the above 14 copyright notice, this list of conditions and the 15 following disclaimer. 16 17 * Redistributions in binary form must reproduce the above 18 copyright notice, this list of conditions and the 19 following disclaimer in the documentation and/or other 20 materials provided with the distribution. 21 22 * Neither the name of the assimp team, nor the names of its 23 contributors may be used to endorse or promote products 24 derived from this software without specific prior 25 written permission of the assimp team. 26 27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 31 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 39 ---------------------------------------------------------------------- 40 */ 41 42 #ifndef INCLUDED_AI_IRRXML_WRAPPER 43 #define INCLUDED_AI_IRRXML_WRAPPER 44 45 #include <assimp/ai_assert.h> 46 #include <assimp/DefaultLogger.hpp> 47 48 #include "BaseImporter.h" 49 #include "IOStream.hpp" 50 51 #include <pugixml.hpp> 52 #include <vector> 53 54 namespace Assimp { 55 56 /// @brief Will find a node by its name. 57 struct find_node_by_name_predicate { 58 std::string mName; find_node_by_name_predicatefind_node_by_name_predicate59 find_node_by_name_predicate(const std::string &name) : 60 mName(name) { 61 // empty 62 } 63 operatorfind_node_by_name_predicate64 bool operator()(pugi::xml_node node) const { 65 return node.name() == mName; 66 } 67 }; 68 69 /// @brief Will convert an attribute to its int value. 70 /// @tparam TNodeType The node type. 71 template <class TNodeType> 72 struct NodeConverter { 73 public: to_intNodeConverter74 static int to_int(TNodeType &node, const char *attribName) { 75 ai_assert(nullptr != attribName); 76 return node.attribute(attribName).to_int(); 77 } 78 }; 79 80 using XmlNode = pugi::xml_node; 81 using XmlAttribute = pugi::xml_attribute; 82 83 /// @brief The Xml-Parser class. 84 /// 85 /// Use this parser if you have to import any kind of xml-format. 86 /// 87 /// An example: 88 /// @code 89 /// TXmlParser<XmlNode> theParser; 90 /// if (theParser.parse(fileStream)) { 91 /// auto node = theParser.getRootNode(); 92 /// for ( auto currentNode : node.children()) { 93 /// // Will loop over all children 94 /// } 95 /// } 96 /// @endcode 97 /// @tparam TNodeType 98 template <class TNodeType> 99 class TXmlParser { 100 public: 101 /// @brief The default class constructor. TXmlParser()102 TXmlParser() : 103 mDoc(nullptr), 104 mData() { 105 // empty 106 } 107 108 /// @brief The class destructor. ~TXmlParser()109 ~TXmlParser() { 110 clear(); 111 } 112 113 /// @brief Will clear the parsed xml-file. clear()114 void clear() { 115 if (mData.empty()) { 116 mDoc = nullptr; 117 return; 118 } 119 mData.clear(); 120 delete mDoc; 121 mDoc = nullptr; 122 } 123 124 /// @brief Will search for a child-node by its name 125 /// @param name [in] The name of the child-node. 126 /// @return The node instance or nullptr, if nothing was found. findNode(const std::string & name)127 TNodeType *findNode(const std::string &name) { 128 if (name.empty()) { 129 return nullptr; 130 } 131 132 if (nullptr == mDoc) { 133 return nullptr; 134 } 135 136 find_node_by_name_predicate predicate(name); 137 mCurrent = mDoc->find_node(predicate); 138 if (mCurrent.empty()) { 139 return nullptr; 140 } 141 142 return &mCurrent; 143 } 144 145 /// @brief Will return true, if the node is a child-node. 146 /// @param name [in] The name of the child node to look for. 147 /// @return true, if the node is a child-node or false if not. hasNode(const std::string & name)148 bool hasNode(const std::string &name) { 149 return nullptr != findNode(name); 150 } 151 152 /// @brief Will parse an xml-file from a given stream. 153 /// @param stream The input stream. 154 /// @return true, if the parsing was successful, false if not. parse(IOStream * stream)155 bool parse(IOStream *stream) { 156 if (nullptr == stream) { 157 ASSIMP_LOG_DEBUG("Stream is nullptr."); 158 return false; 159 } 160 161 const size_t len = stream->FileSize(); 162 mData.resize(len + 1); 163 memset(&mData[0], '\0', len + 1); 164 stream->Read(&mData[0], 1, len); 165 166 mDoc = new pugi::xml_document(); 167 pugi::xml_parse_result parse_result = mDoc->load_string(&mData[0], pugi::parse_full); 168 if (parse_result.status == pugi::status_ok) { 169 return true; 170 } 171 172 ASSIMP_LOG_DEBUG("Error while parse xml.", std::string(parse_result.description()), " @ ", parse_result.offset); 173 174 return false; 175 } 176 177 /// @brief Will return truem if a root node is there. 178 /// @return true in case of an existing root. hasRoot()179 bool hasRoot() const { 180 return nullptr != mDoc; 181 } 182 /// @brief Will return the document pointer, is nullptr if no xml-file was parsed. 183 /// @return The pointer showing to the document. getDocument()184 pugi::xml_document *getDocument() const { 185 return mDoc; 186 } 187 188 /// @brief Will return the root node, const version. 189 /// @return The root node. getRootNode()190 const TNodeType getRootNode() const { 191 static pugi::xml_node none; 192 if (nullptr == mDoc) { 193 return none; 194 } 195 return mDoc->root(); 196 } 197 198 /// @brief Will return the root node, non-const version. 199 /// @return The root node. getRootNode()200 TNodeType getRootNode() { 201 static pugi::xml_node none; 202 if (nullptr == mDoc) { 203 return none; 204 } 205 return mDoc->root(); 206 } 207 208 /// @brief Will check if a node with the given name is in. 209 /// @param node [in] The node to look in. 210 /// @param name [in] The name of the child-node. 211 /// @return true, if node was found, false if not. hasNode(XmlNode & node,const char * name)212 static inline bool hasNode(XmlNode &node, const char *name) { 213 pugi::xml_node child = node.find_child(find_node_by_name_predicate(name)); 214 return !child.empty(); 215 } 216 217 /// @brief Will check if an attribute is part of the XmlNode. 218 /// @param xmlNode [in] The node to search in. 219 /// @param name [in} The attribute name to look for. 220 /// @return true, if the was found, false if not. hasAttribute(XmlNode & xmlNode,const char * name)221 static inline bool hasAttribute(XmlNode &xmlNode, const char *name) { 222 pugi::xml_attribute attr = xmlNode.attribute(name); 223 return !attr.empty(); 224 } 225 226 /// @brief Will try to get an unsigned int attribute value. 227 /// @param xmlNode [in] The node to search in. 228 /// @param name [in] The attribute name to look for. 229 /// @param val [out] The unsigned int value from the attribute. 230 /// @return true, if the node contains an attribute with the given name and if the value is an unsigned int. getUIntAttribute(XmlNode & xmlNode,const char * name,unsigned int & val)231 static inline bool getUIntAttribute(XmlNode &xmlNode, const char *name, unsigned int &val) { 232 pugi::xml_attribute attr = xmlNode.attribute(name); 233 if (attr.empty()) { 234 return false; 235 } 236 237 val = attr.as_uint(); 238 return true; 239 } 240 241 /// @brief Will try to get an int attribute value. 242 /// @param xmlNode [in] The node to search in. 243 /// @param name [in] The attribute name to look for. 244 /// @param val [out] The int value from the attribute. 245 /// @return true, if the node contains an attribute with the given name and if the value is an int. getIntAttribute(XmlNode & xmlNode,const char * name,int & val)246 static inline bool getIntAttribute(XmlNode &xmlNode, const char *name, int &val) { 247 pugi::xml_attribute attr = xmlNode.attribute(name); 248 if (attr.empty()) { 249 return false; 250 } 251 252 val = attr.as_int(); 253 return true; 254 } 255 256 /// @brief Will try to get a real attribute value. 257 /// @param xmlNode [in] The node to search in. 258 /// @param name [in] The attribute name to look for. 259 /// @param val [out] The real value from the attribute. 260 /// @return true, if the node contains an attribute with the given name and if the value is a real. getRealAttribute(XmlNode & xmlNode,const char * name,ai_real & val)261 static inline bool getRealAttribute(XmlNode &xmlNode, const char *name, ai_real &val) { 262 pugi::xml_attribute attr = xmlNode.attribute(name); 263 if (attr.empty()) { 264 return false; 265 } 266 #ifdef ASSIMP_DOUBLE_PRECISION 267 val = attr.as_double(); 268 #else 269 val = attr.as_float(); 270 #endif 271 return true; 272 } 273 274 /// @brief Will try to get a float attribute value. 275 /// @param xmlNode [in] The node to search in. 276 /// @param name [in] The attribute name to look for. 277 /// @param val [out] The float value from the attribute. 278 /// @return true, if the node contains an attribute with the given name and if the value is a float. getFloatAttribute(XmlNode & xmlNode,const char * name,float & val)279 static inline bool getFloatAttribute(XmlNode &xmlNode, const char *name, float &val) { 280 pugi::xml_attribute attr = xmlNode.attribute(name); 281 if (attr.empty()) { 282 return false; 283 } 284 285 val = attr.as_float(); 286 return true; 287 } 288 289 /// @brief Will try to get a double attribute value. 290 /// @param xmlNode [in] The node to search in. 291 /// @param name [in] The attribute name to look for. 292 /// @param val [out] The double value from the attribute. 293 /// @return true, if the node contains an attribute with the given name and if the value is a double. getDoubleAttribute(XmlNode & xmlNode,const char * name,double & val)294 static inline bool getDoubleAttribute(XmlNode &xmlNode, const char *name, double &val) { 295 pugi::xml_attribute attr = xmlNode.attribute(name); 296 if (attr.empty()) { 297 return false; 298 } 299 300 val = attr.as_double(); 301 return true; 302 } 303 304 /// @brief Will try to get a std::string attribute value. 305 /// @param xmlNode [in] The node to search in. 306 /// @param name [in] The attribute name to look for. 307 /// @param val [out] The std::string value from the attribute. 308 /// @return true, if the node contains an attribute with the given name and if the value is a std::string. getStdStrAttribute(XmlNode & xmlNode,const char * name,std::string & val)309 static inline bool getStdStrAttribute(XmlNode &xmlNode, const char *name, std::string &val) { 310 pugi::xml_attribute attr = xmlNode.attribute(name); 311 if (attr.empty()) { 312 return false; 313 } 314 315 val = attr.as_string(); 316 return true; 317 } 318 319 /// @brief Will try to get a bool attribute value. 320 /// @param xmlNode [in] The node to search in. 321 /// @param name [in] The attribute name to look for. 322 /// @param val [out] The bool value from the attribute. 323 /// @return true, if the node contains an attribute with the given name and if the value is a bool. getBoolAttribute(XmlNode & xmlNode,const char * name,bool & val)324 static inline bool getBoolAttribute(XmlNode &xmlNode, const char *name, bool &val) { 325 pugi::xml_attribute attr = xmlNode.attribute(name); 326 if (attr.empty()) { 327 return false; 328 } 329 330 val = attr.as_bool(); 331 return true; 332 } 333 334 /// @brief Will try to get the value of the node as a string. 335 /// @param node [in] The node to search in. 336 /// @param text [out] The value as a text. 337 /// @return true, if the value can be read out. getValueAsString(XmlNode & node,std::string & text)338 static inline bool getValueAsString(XmlNode &node, std::string &text) { 339 text = std::string(); 340 if (node.empty()) { 341 return false; 342 } 343 344 text = node.text().as_string(); 345 346 return true; 347 } 348 349 /// @brief Will try to get the value of the node as a float. 350 /// @param node [in] The node to search in. 351 /// @param text [out] The value as a float. 352 /// @return true, if the value can be read out. getValueAsFloat(XmlNode & node,ai_real & v)353 static inline bool getValueAsFloat(XmlNode &node, ai_real &v) { 354 if (node.empty()) { 355 return false; 356 } 357 358 v = node.text().as_float(); 359 360 return true; 361 } 362 363 /// @brief Will try to get the value of the node as an integer. 364 /// @param node [in] The node to search in. 365 /// @param text [out] The value as a int. 366 /// @return true, if the value can be read out. getValueAsInt(XmlNode & node,int & v)367 static inline bool getValueAsInt(XmlNode &node, int &v) { 368 if (node.empty()) { 369 return false; 370 } 371 372 v = node.text().as_int(); 373 374 return true; 375 } 376 377 /// @brief Will try to get the value of the node as an bool. 378 /// @param node [in] The node to search in. 379 /// @param text [out] The value as a bool. 380 /// @return true, if the value can be read out. getValueAsBool(XmlNode & node,bool & v)381 static inline bool getValueAsBool(XmlNode& node, bool& v) 382 { 383 if (node.empty()) { 384 return false; 385 } 386 387 v = node.text().as_bool(); 388 389 return true; 390 } 391 392 private: 393 pugi::xml_document *mDoc; 394 TNodeType mCurrent; 395 std::vector<char> mData; 396 }; 397 398 using XmlParser = TXmlParser<pugi::xml_node>; 399 400 /// @brief This class declares an iterator to loop through all children of the root node. 401 class XmlNodeIterator { 402 public: 403 /// @brief The iteration mode. 404 enum IterationMode { 405 PreOrderMode, ///< Pre-ordering, get the values, continue the iteration. 406 PostOrderMode ///< Post-ordering, continue the iteration, get the values. 407 }; 408 /// @brief The class constructor 409 /// @param parent [in] The xml parent to to iterate through. 410 /// @param mode [in] The iteration mode. XmlNodeIterator(XmlNode & parent,IterationMode mode)411 explicit XmlNodeIterator(XmlNode &parent, IterationMode mode) : 412 mParent(parent), 413 mNodes(), 414 mIndex(0) { 415 if (mode == PreOrderMode) { 416 collectChildrenPreOrder(parent); 417 } else { 418 collectChildrenPostOrder(parent); 419 } 420 } 421 422 /// @brief The class destructor. ~XmlNodeIterator()423 ~XmlNodeIterator() { 424 // empty 425 } 426 427 /// @brief Will iterate through all children in pre-order iteration. 428 /// @param node [in] The nod to iterate through. collectChildrenPreOrder(XmlNode & node)429 void collectChildrenPreOrder(XmlNode &node) { 430 if (node != mParent && node.type() == pugi::node_element) { 431 mNodes.push_back(node); 432 } 433 for (XmlNode currentNode : node.children()) { 434 collectChildrenPreOrder(currentNode); 435 } 436 } 437 438 /// @brief Will iterate through all children in post-order iteration. 439 /// @param node [in] The nod to iterate through. collectChildrenPostOrder(XmlNode & node)440 void collectChildrenPostOrder(XmlNode &node) { 441 for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) { 442 collectChildrenPostOrder(currentNode); 443 } 444 if (node != mParent) { 445 mNodes.push_back(node); 446 } 447 } 448 449 /// @brief Will iterate through all collected nodes. 450 /// @param next The next node, if there is any. 451 /// @return true, if there is a node left. getNext(XmlNode & next)452 bool getNext(XmlNode &next) { 453 if (mIndex == mNodes.size()) { 454 return false; 455 } 456 457 next = mNodes[mIndex]; 458 ++mIndex; 459 460 return true; 461 } 462 463 /// @brief Will return the number of collected nodes. 464 /// @return The number of collected nodes. size()465 size_t size() const { 466 return mNodes.size(); 467 } 468 469 /// @brief Returns true, if the node is empty. 470 /// @return true, if the node is empty, false if not. isEmpty()471 bool isEmpty() const { 472 return mNodes.empty(); 473 } 474 475 /// @brief Will clear all collected nodes. clear()476 void clear() { 477 if (mNodes.empty()) { 478 return; 479 } 480 481 mNodes.clear(); 482 mIndex = 0; 483 } 484 485 private: 486 XmlNode &mParent; 487 std::vector<XmlNode> mNodes; 488 size_t mIndex; 489 }; 490 491 } // namespace Assimp 492 493 #endif // !! INCLUDED_AI_IRRXML_WRAPPER 494