tamlXmlWriter.cc 10 KB

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