XmlParser.h 19 KB

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