tamlXmlParser.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "persistence/taml/fsTinyXml.h"
  27. #include "platform/profiler.h"
  28. #ifndef _FILESTREAM_H_
  29. #include "core/stream/fileStream.h"
  30. #endif
  31. #ifndef TINYXML2_INCLUDED
  32. #include <tinyxml2.h>
  33. #endif
  34. //-----------------------------------------------------------------------------
  35. bool TamlXmlParser::accept( const char* pFilename, TamlVisitor& visitor )
  36. {
  37. // Debug Profiling.
  38. PROFILE_SCOPE(TamlXmlParser_Accept);
  39. // Sanity!
  40. AssertFatal( pFilename != NULL, "Cannot parse a NULL filename." );
  41. // Expand the file-path.
  42. char filenameBuffer[1024];
  43. // TODO: Make sure this is a proper substitute for
  44. // Con::expandPath (T2D)
  45. Con::expandScriptFilename( filenameBuffer, sizeof(filenameBuffer), pFilename );
  46. /** T2D uses a custom version of TinyXML that supports FileStream.
  47. * We don't so we can't do this
  48. *
  49. FileStream stream;
  50. #ifdef TORQUE_OS_ANDROID
  51. if (strlen(pFilename) > strlen(filenameBuffer)) {
  52. dStrcpy(filenameBuffer, pFilename, 1024);
  53. }
  54. #endif
  55. // File open for read?
  56. if ( !stream.open( filenameBuffer, Torque::FS::File::AccessMode::Read ) )
  57. {
  58. // No, so warn.
  59. Con::warnf("TamlXmlParser::parse() - Could not open filename '%s' for parse.", filenameBuffer );
  60. return false;
  61. }
  62. */
  63. VfsXMLDocument xmlDocument;
  64. // Load document from stream.
  65. if ( !xmlDocument.LoadFile( filenameBuffer ) )
  66. {
  67. // Warn!
  68. Con::warnf("TamlXmlParser: Could not load Taml XML file from stream.");
  69. return false;
  70. }
  71. // Close the stream.
  72. // stream.close();
  73. // Set parsing filename.
  74. setParsingFilename( filenameBuffer );
  75. // Flag document as not dirty.
  76. mDocumentDirty = false;
  77. // Parse root element.
  78. parseElement( xmlDocument.RootElement(), visitor );
  79. // Reset parsing filename.
  80. setParsingFilename( StringTable->EmptyString() );
  81. // Finish if the document is not dirty.
  82. if ( !mDocumentDirty )
  83. return true;
  84. // Open for write?
  85. /*if ( !stream.open( filenameBuffer, FileStream::StreamWrite ) )
  86. {
  87. // No, so warn.
  88. Con::warnf("TamlXmlParser::parse() - Could not open filename '%s' for write.", filenameBuffer );
  89. return false;
  90. }*/
  91. // Yes, so save the document.
  92. if ( !xmlDocument.SaveFile( filenameBuffer ) )
  93. {
  94. // Warn!
  95. Con::warnf("TamlXmlParser: Could not save Taml XML document.");
  96. return false;
  97. }
  98. // Close the stream.
  99. //stream.close();
  100. return true;
  101. }
  102. //-----------------------------------------------------------------------------
  103. inline bool TamlXmlParser::parseElement( tinyxml2::XMLElement* pXmlElement, TamlVisitor& visitor )
  104. {
  105. // Debug Profiling.
  106. PROFILE_SCOPE(TamlXmlParser_ParseElement);
  107. // Parse attributes (stop processing if instructed).
  108. if ( !parseAttributes( pXmlElement, visitor ) )
  109. return false;
  110. // Finish if only the root is needed.
  111. if ( visitor.wantsRootOnly() )
  112. return false;
  113. // Fetch any children.
  114. tinyxml2::XMLNode* pChildXmlNode = pXmlElement->FirstChild();
  115. // Do we have any element children?
  116. if ( pChildXmlNode != NULL && pChildXmlNode->ToElement() != NULL )
  117. {
  118. // Iterate children.
  119. for ( tinyxml2::XMLElement* pChildXmlElement = pChildXmlNode->ToElement(); pChildXmlElement; pChildXmlElement = pChildXmlElement->NextSiblingElement() )
  120. {
  121. // Parse element (stop processing if instructed).
  122. if ( !parseElement( pChildXmlElement, visitor ) )
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. //-----------------------------------------------------------------------------
  129. inline bool TamlXmlParser::parseAttributes( tinyxml2::XMLElement* pXmlElement, TamlVisitor& visitor )
  130. {
  131. // Debug Profiling.
  132. PROFILE_SCOPE(TamlXmlParser_ParseAttribute);
  133. // Calculate if element is at the root or not.
  134. const bool isRoot = pXmlElement->GetDocument()->RootElement() == pXmlElement;
  135. // Create a visitor property state.
  136. TamlVisitor::PropertyState propertyState;
  137. propertyState.setObjectName( pXmlElement->Value(), isRoot );
  138. // Iterate attributes.
  139. for ( const tinyxml2::XMLAttribute* pAttribute = pXmlElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->Next() )
  140. {
  141. // Configure property state.
  142. propertyState.setProperty( pAttribute->Name(), pAttribute->Value() );
  143. // Visit this attribute.
  144. const bool visitStatus = visitor.visit( *this, propertyState );
  145. // Was the property value changed?
  146. if ( propertyState.getPropertyValueDirty() )
  147. {
  148. // Yes, so update the attribute.
  149. const_cast<tinyxml2::XMLAttribute*>(pAttribute)->SetAttribute( propertyState.getPropertyValue() );
  150. // Flag the document as dirty.
  151. mDocumentDirty = true;
  152. }
  153. // Finish if requested.
  154. if ( !visitStatus )
  155. return false;
  156. }
  157. return true;
  158. }