tamlXmlParser.cpp 6.1 KB

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