tamlXmlParser.cpp 6.2 KB

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