tamlXmlParser.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/xml/tamlXmlParser.h"
  23. #include "persistence/taml/tamlVisitor.h"
  24. #include "console/console.h"
  25. // Debug Profiling.
  26. #include "debug/profiler.h"
  27. //-----------------------------------------------------------------------------
  28. bool TamlXmlParser::accept( const char* pFilename, TamlVisitor& visitor )
  29. {
  30. // Debug Profiling.
  31. PROFILE_SCOPE(TamlXmlParser_Accept);
  32. // Sanity!
  33. AssertFatal( pFilename != NULL, "Cannot parse a NULL filename." );
  34. // Expand the file-path.
  35. char filenameBuffer[1024];
  36. Con::expandPath( filenameBuffer, sizeof(filenameBuffer), pFilename );
  37. FileStream stream;
  38. #ifdef TORQUE_OS_ANDROID
  39. if (strlen(pFilename) > strlen(filenameBuffer)) {
  40. strcpy(filenameBuffer, pFilename);
  41. }
  42. #endif
  43. // File open for read?
  44. if ( !stream.open( filenameBuffer, FileStream::Read ) )
  45. {
  46. // No, so warn.
  47. Con::warnf("TamlXmlParser::parse() - Could not open filename '%s' for parse.", filenameBuffer );
  48. return false;
  49. }
  50. TiXmlDocument xmlDocument;
  51. // Load document from stream.
  52. if ( !xmlDocument.LoadFile( stream ) )
  53. {
  54. // Warn!
  55. Con::warnf("TamlXmlParser: Could not load Taml XML file from stream.");
  56. return false;
  57. }
  58. // Close the stream.
  59. stream.close();
  60. // Set parsing filename.
  61. setParsingFilename( filenameBuffer );
  62. // Flag document as not dirty.
  63. mDocumentDirty = false;
  64. // Parse root element.
  65. parseElement( xmlDocument.RootElement(), visitor );
  66. // Reset parsing filename.
  67. setParsingFilename( StringTable->EmptyString );
  68. // Finish if the document is not dirty.
  69. if ( !mDocumentDirty )
  70. return true;
  71. // Open for write?
  72. if ( !stream.open( filenameBuffer, FileStream::Write ) )
  73. {
  74. // No, so warn.
  75. Con::warnf("TamlXmlParser::parse() - Could not open filename '%s' for write.", filenameBuffer );
  76. return false;
  77. }
  78. // Yes, so save the document.
  79. if ( !xmlDocument.SaveFile( stream ) )
  80. {
  81. // Warn!
  82. Con::warnf("TamlXmlParser: Could not save Taml XML document.");
  83. return false;
  84. }
  85. // Close the stream.
  86. stream.close();
  87. return true;
  88. }
  89. //-----------------------------------------------------------------------------
  90. inline bool TamlXmlParser::parseElement( TiXmlElement* pXmlElement, TamlVisitor& visitor )
  91. {
  92. // Debug Profiling.
  93. PROFILE_SCOPE(TamlXmlParser_ParseElement);
  94. // Parse attributes (stop processing if instructed).
  95. if ( !parseAttributes( pXmlElement, visitor ) )
  96. return false;
  97. // Finish if only the root is needed.
  98. if ( visitor.wantsRootOnly() )
  99. return false;
  100. // Fetch any children.
  101. TiXmlNode* pChildXmlNode = pXmlElement->FirstChild();
  102. // Do we have any element children?
  103. if ( pChildXmlNode != NULL && pChildXmlNode->Type() == TiXmlNode::TINYXML_ELEMENT )
  104. {
  105. // Iterate children.
  106. for ( TiXmlElement* pChildXmlElement = dynamic_cast<TiXmlElement*>( pChildXmlNode ); pChildXmlElement; pChildXmlElement = pChildXmlElement->NextSiblingElement() )
  107. {
  108. // Parse element (stop processing if instructed).
  109. if ( !parseElement( pChildXmlElement, visitor ) )
  110. return false;
  111. }
  112. }
  113. return true;
  114. }
  115. //-----------------------------------------------------------------------------
  116. inline bool TamlXmlParser::parseAttributes( TiXmlElement* pXmlElement, TamlVisitor& visitor )
  117. {
  118. // Debug Profiling.
  119. PROFILE_SCOPE(TamlXmlParser_ParseAttribute);
  120. // Calculate if element is at the root or not.
  121. const bool isRoot = pXmlElement->GetDocument()->RootElement() == pXmlElement;
  122. // Create a visitor property state.
  123. TamlVisitor::PropertyState propertyState;
  124. propertyState.setObjectName( pXmlElement->Value(), isRoot );
  125. // Iterate attributes.
  126. for ( TiXmlAttribute* pAttribute = pXmlElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->Next() )
  127. {
  128. // Configure property state.
  129. propertyState.setProperty( pAttribute->Name(), pAttribute->Value() );
  130. // Visit this attribute.
  131. const bool visitStatus = visitor.visit( *this, propertyState );
  132. // Was the property value changed?
  133. if ( propertyState.getPropertyValueDirty() )
  134. {
  135. // Yes, so update the attribute.
  136. pAttribute->SetValue( propertyState.getPropertyValue() );
  137. // Flag the document as dirty.
  138. mDocumentDirty = true;
  139. }
  140. // Finish if requested.
  141. if ( !visitStatus )
  142. return false;
  143. }
  144. return true;
  145. }