tamlXmlParser.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "persistence/taml/tamlXmlParser.h"
  23. // Debug Profiling.
  24. #include "debug/profiler.h"
  25. //-----------------------------------------------------------------------------
  26. bool TamlXmlParser::parse( const char* pFilename, TamlXmlVisitor& visitor, const bool writeDocument )
  27. {
  28. // Debug Profiling.
  29. PROFILE_SCOPE(TamlXmlParser_Parse);
  30. // Sanity!
  31. AssertFatal( pFilename != NULL, "Cannot parse a NULL filename." );
  32. char filenameBuffer[1024];
  33. Con::expandPath( filenameBuffer, sizeof(filenameBuffer), pFilename );
  34. FileStream stream;
  35. // File open for read?
  36. if ( !stream.open( filenameBuffer, FileStream::Read ) )
  37. {
  38. // No, so warn.
  39. Con::warnf("TamlXmlParser::parse() - Could not open filename '%s' for parse.", filenameBuffer );
  40. return false;
  41. }
  42. TiXmlDocument xmlDocument;
  43. // Load document from stream.
  44. if ( !xmlDocument.LoadFile( stream ) )
  45. {
  46. // Warn!
  47. Con::warnf("TamlXmlParser: Could not load Taml XML file from stream.");
  48. return false;
  49. }
  50. // Close the stream.
  51. stream.close();
  52. // Set parsing filename.
  53. mpParsingFilename = filenameBuffer;
  54. // Parse root element.
  55. parseElement( xmlDocument.RootElement(), visitor );
  56. // Reset parsing filename.
  57. mpParsingFilename = NULL;
  58. // Are we writing the document?
  59. if ( writeDocument )
  60. {
  61. // Yes, so open for write?
  62. if ( !stream.open( filenameBuffer, FileStream::Write ) )
  63. {
  64. // No, so warn.
  65. Con::warnf("TamlXmlParser::parse() - Could not open filename '%s' for write.", filenameBuffer );
  66. return false;
  67. }
  68. // Yes, so save the document.
  69. if ( !xmlDocument.SaveFile( stream ) )
  70. {
  71. // Warn!
  72. Con::warnf("TamlXmlParser: Could not save Taml XML document.");
  73. return false;
  74. }
  75. // Close the stream.
  76. stream.close();
  77. }
  78. return true;
  79. }
  80. //-----------------------------------------------------------------------------
  81. bool TamlXmlParser::parseElement( TiXmlElement* pXmlElement, TamlXmlVisitor& visitor )
  82. {
  83. // Debug Profiling.
  84. PROFILE_SCOPE(TamlXmlParser_ParseElement);
  85. // Visit this element (stop processing if instructed).
  86. if ( !visitor.visit( pXmlElement, *this ) )
  87. return false;
  88. // Parse attributes (stop processing if instructed).
  89. if ( !parseAttributes( pXmlElement, visitor ) )
  90. return false;
  91. // Fetch any children.
  92. TiXmlNode* pChildXmlNode = pXmlElement->FirstChild();
  93. // Do we have any element children?
  94. if ( pChildXmlNode != NULL && pChildXmlNode->Type() == TiXmlNode::TINYXML_ELEMENT )
  95. {
  96. // Iterate children.
  97. for ( TiXmlElement* pChildXmlElement = dynamic_cast<TiXmlElement*>( pChildXmlNode ); pChildXmlElement; pChildXmlElement = pChildXmlElement->NextSiblingElement() )
  98. {
  99. // Parse element (stop processing if instructed).
  100. if ( !parseElement( pChildXmlElement, visitor ) )
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. //-----------------------------------------------------------------------------
  107. bool TamlXmlParser::parseAttributes( TiXmlElement* pXmlElement, TamlXmlVisitor& visitor )
  108. {
  109. // Debug Profiling.
  110. PROFILE_SCOPE(TamlXmlParser_ParseAttribute);
  111. // Iterate attributes.
  112. for ( TiXmlAttribute* pAttribute = pXmlElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->Next() )
  113. {
  114. // Visit this attribute (stop processing if instructed).
  115. if ( !visitor.visit( pAttribute, *this ) )
  116. return false;
  117. }
  118. return true;
  119. }