tamlXmlWriter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/tamlXmlWriter.h"
  23. // Debug Profiling.
  24. #include "platform/profiler.h"
  25. #include "persistence/taml/fsTinyXml.h"
  26. //-----------------------------------------------------------------------------
  27. bool TamlXmlWriter::write( FileStream& stream, const TamlWriteNode* pTamlWriteNode )
  28. {
  29. // Debug Profiling.
  30. PROFILE_SCOPE(TamlXmlWriter_Write);
  31. // Create document.
  32. fsTiXmlDocument xmlDocument;
  33. // Compile the root element.
  34. TiXmlElement* pRootElement = compileElement( pTamlWriteNode );
  35. // Fetch any TAML Schema file reference.
  36. const char* pTamlSchemaFile = Con::getVariable( TAML_SCHEMA_VARIABLE );
  37. // Do we have a schema file reference?
  38. if ( pTamlSchemaFile != NULL && *pTamlSchemaFile != 0 )
  39. {
  40. // Yes, so add namespace attribute to root.
  41. pRootElement->SetAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
  42. // Expand the file-path reference.
  43. char schemaFilePathBuffer[1024];
  44. Con::expandToolScriptFilename( schemaFilePathBuffer, sizeof(schemaFilePathBuffer), pTamlSchemaFile );
  45. // Fetch the output path for the Taml file.
  46. char outputFileBuffer[1024];
  47. dSprintf( outputFileBuffer, sizeof(outputFileBuffer), "%s", mpTaml->getFilePathBuffer() );
  48. char* pFileStart = dStrrchr( outputFileBuffer, '/' );
  49. if ( pFileStart == NULL )
  50. *outputFileBuffer = 0;
  51. else
  52. *pFileStart = 0;
  53. // Fetch the schema file-path relative to the output file.
  54. StringTableEntry relativeSchemaFilePath = Platform::makeRelativePathName( schemaFilePathBuffer, outputFileBuffer );
  55. // Add schema location attribute to root.
  56. pRootElement->SetAttribute( "xsi:noNamespaceSchemaLocation", relativeSchemaFilePath );
  57. }
  58. // Link the root element.
  59. xmlDocument.LinkEndChild( pRootElement );
  60. // Save document to stream.
  61. return xmlDocument.SaveFile( stream );
  62. }
  63. //-----------------------------------------------------------------------------
  64. TiXmlElement* TamlXmlWriter::compileElement( const TamlWriteNode* pTamlWriteNode )
  65. {
  66. // Debug Profiling.
  67. PROFILE_SCOPE(TamlXmlWriter_CompileElement);
  68. // Fetch object.
  69. SimObject* pSimObject = pTamlWriteNode->mpSimObject;
  70. // Fetch element name.
  71. const char* pElementName = pSimObject->getClassName();
  72. // Create element.
  73. TiXmlElement* pElement = new fsTiXmlElement( pElementName );
  74. // Fetch reference Id.
  75. const U32 referenceId = pTamlWriteNode->mRefId;
  76. // Do we have a reference Id?
  77. if ( referenceId != 0 )
  78. {
  79. // Yes, so set reference Id attribute.
  80. pElement->SetAttribute( tamlRefIdName, referenceId );
  81. }
  82. // Do we have a reference to node?
  83. else if ( pTamlWriteNode->mRefToNode != NULL )
  84. {
  85. // Yes, so fetch reference to Id.
  86. const U32 referenceToId = pTamlWriteNode->mRefToNode->mRefId;
  87. // Sanity!
  88. AssertFatal( referenceToId != 0, "Taml: Invalid reference to Id." );
  89. // Set reference to Id attribute.
  90. pElement->SetAttribute( tamlRefToIdName, referenceToId );
  91. // Finish because we're a reference to another object.
  92. return pElement;
  93. }
  94. // Fetch object name.
  95. const char* pObjectName = pTamlWriteNode->mpObjectName;
  96. // Do we have a name?
  97. if ( pObjectName != NULL )
  98. {
  99. // Yes, so set name attribute.
  100. pElement->SetAttribute( tamlNamedObjectName, pObjectName );
  101. }
  102. // Compile attributes.
  103. compileAttributes( pElement, pTamlWriteNode );
  104. // Fetch children.
  105. Vector<TamlWriteNode*>* pChildren = pTamlWriteNode->mChildren;
  106. // Do we have any children?
  107. if ( pChildren )
  108. {
  109. // Yes, so iterate children.
  110. for( Vector<TamlWriteNode*>::iterator itr = pChildren->begin(); itr != pChildren->end(); ++itr )
  111. {
  112. // Write child element.
  113. pElement->LinkEndChild( compileElement( (*itr) ) );
  114. }
  115. }
  116. // Compile custom elements.
  117. compileCustomElements( pElement, pTamlWriteNode );
  118. return pElement;
  119. }
  120. //-----------------------------------------------------------------------------
  121. void TamlXmlWriter::compileAttributes( TiXmlElement* pXmlElement, const TamlWriteNode* pTamlWriteNode )
  122. {
  123. // Debug Profiling.
  124. PROFILE_SCOPE(TamlXmlWriter_CompileAttributes);
  125. // Fetch fields.
  126. const Vector<TamlWriteNode::FieldValuePair*>& fields = pTamlWriteNode->mFields;
  127. // Ignore if no fields.
  128. if ( fields.size() == 0 )
  129. return;
  130. // Iterate fields.
  131. for( Vector<TamlWriteNode::FieldValuePair*>::const_iterator itr = fields.begin(); itr != fields.end(); ++itr )
  132. {
  133. // Fetch field/value pair.
  134. TamlWriteNode::FieldValuePair* pFieldValue = (*itr);
  135. // Set field attribute.
  136. pXmlElement->SetAttribute( pFieldValue->mName, pFieldValue->mpValue );
  137. }
  138. }
  139. //-----------------------------------------------------------------------------
  140. void TamlXmlWriter::compileCustomElements( TiXmlElement* pXmlElement, const TamlWriteNode* pTamlWriteNode )
  141. {
  142. // Debug Profiling.
  143. PROFILE_SCOPE(TamlXmlWriter_CompileCustomElements);
  144. // Fetch custom nodes.
  145. const TamlCustomNodes& customNodes = pTamlWriteNode->mCustomNodes;
  146. // Fetch custom nodes.
  147. const TamlCustomNodeVector& nodes = customNodes.getNodes();
  148. // Finish if no custom nodes to process.
  149. if ( nodes.size() == 0 )
  150. return;
  151. // Iterate custom nodes.
  152. for( TamlCustomNodeVector::const_iterator customNodesItr = nodes.begin(); customNodesItr != nodes.end(); ++customNodesItr )
  153. {
  154. // Fetch the custom node.
  155. TamlCustomNode* pCustomNode = *customNodesItr;
  156. // Format extended element name.
  157. char extendedElementNameBuffer[256];
  158. dSprintf( extendedElementNameBuffer, sizeof(extendedElementNameBuffer), "%s.%s", pXmlElement->Value(), pCustomNode->getNodeName() );
  159. StringTableEntry extendedElementName = StringTable->insert( extendedElementNameBuffer );
  160. // Create element.
  161. TiXmlElement* pExtendedPropertyElement = new fsTiXmlElement( extendedElementName );
  162. // Fetch node children.
  163. const TamlCustomNodeVector& nodeChildren = pCustomNode->getChildren();
  164. // Iterate children nodes.
  165. for( TamlCustomNodeVector::const_iterator childNodeItr = nodeChildren.begin(); childNodeItr != nodeChildren.end(); ++childNodeItr )
  166. {
  167. // Fetch child node.
  168. const TamlCustomNode* pChildNode = *childNodeItr;
  169. // Compile the custom node.
  170. compileCustomNode( pExtendedPropertyElement, pChildNode );
  171. }
  172. // Finish if the node is set to ignore if empty and it is empty.
  173. if ( pCustomNode->getIgnoreEmpty() && pExtendedPropertyElement->NoChildren() )
  174. {
  175. // Yes, so delete the extended element.
  176. delete pExtendedPropertyElement;
  177. pExtendedPropertyElement = NULL;
  178. }
  179. else
  180. {
  181. // No, so add elementt as child.
  182. pXmlElement->LinkEndChild( pExtendedPropertyElement );
  183. }
  184. }
  185. }
  186. //-----------------------------------------------------------------------------
  187. void TamlXmlWriter::compileCustomNode( TiXmlElement* pXmlElement, const TamlCustomNode* pCustomNode )
  188. {
  189. // Finish if the node is set to ignore if empty and it is empty.
  190. if ( pCustomNode->getIgnoreEmpty() && pCustomNode->isEmpty() )
  191. return;
  192. // Is the node a proxy object?
  193. if ( pCustomNode->isProxyObject() )
  194. {
  195. // Yes, so write the proxy object.
  196. pXmlElement->LinkEndChild( compileElement( pCustomNode->getProxyWriteNode() ) );
  197. return;
  198. }
  199. // Create element.
  200. TiXmlElement* pNodeElement = new fsTiXmlElement( pCustomNode->getNodeName() );
  201. // Is there any node text?
  202. if ( !pCustomNode->getNodeTextField().isValueEmpty() )
  203. {
  204. // Yes, so add a text node.
  205. pNodeElement->LinkEndChild( new TiXmlText( pCustomNode->getNodeTextField().getFieldValue() ) );
  206. }
  207. // Fetch fields.
  208. const TamlCustomFieldVector& fields = pCustomNode->getFields();
  209. // Iterate fields.
  210. for ( TamlCustomFieldVector::const_iterator fieldItr = fields.begin(); fieldItr != fields.end(); ++fieldItr )
  211. {
  212. // Fetch field.
  213. const TamlCustomField* pField = *fieldItr;
  214. // Set field.
  215. pNodeElement->SetAttribute( pField->getFieldName(), pField->getFieldValue() );
  216. }
  217. // Fetch node children.
  218. const TamlCustomNodeVector& nodeChildren = pCustomNode->getChildren();
  219. // Iterate children nodes.
  220. for( TamlCustomNodeVector::const_iterator childNodeItr = nodeChildren.begin(); childNodeItr != nodeChildren.end(); ++childNodeItr )
  221. {
  222. // Fetch child node.
  223. const TamlCustomNode* pChildNode = *childNodeItr;
  224. // Compile the child node.
  225. compileCustomNode( pNodeElement, pChildNode );
  226. }
  227. // Finish if the node is set to ignore if empty and it is empty (including fields).
  228. if ( pCustomNode->getIgnoreEmpty() && fields.size() == 0 && pNodeElement->NoChildren() )
  229. {
  230. // Yes, so delete the extended element.
  231. delete pNodeElement;
  232. pNodeElement = NULL;
  233. }
  234. else
  235. {
  236. // Add node element as child.
  237. pXmlElement->LinkEndChild( pNodeElement );
  238. }
  239. }