2
0

taml.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  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 "taml.h"
  23. #ifndef _TAML_XMLWRITER_H_
  24. #include "persistence/taml/xml/tamlXmlWriter.h"
  25. #endif
  26. #ifndef _TAML_XMLREADER_H_
  27. #include "persistence/taml/xml/tamlXmlReader.h"
  28. #endif
  29. #ifndef _TAML_XMLPARSER_H_
  30. #include "persistence/taml/xml/tamlXmlParser.h"
  31. #endif
  32. #ifndef _TAML_BINARYWRITER_H_
  33. #include "persistence/taml/binary/tamlBinaryWriter.h"
  34. #endif
  35. #ifndef _TAML_BINARYREADER_H_
  36. #include "persistence/taml/binary/tamlBinaryReader.h"
  37. #endif
  38. #ifndef _TAML_BINARYPARSER_H_
  39. #include "persistence/taml/binary/tamlBinaryParser.h"
  40. #endif
  41. #ifndef _TAML_JSONWRITER_H_
  42. #include "persistence/taml/json/tamlJSONWriter.h"
  43. #endif
  44. #ifndef _TAML_JSONREADER_H_
  45. #include "persistence/taml/json/tamlJSONReader.h"
  46. #endif
  47. #ifndef _TAML_JSONPARSER_H_
  48. #include "persistence/taml/json/tamlJSONParser.h"
  49. #endif
  50. #ifndef _FRAMEALLOCATOR_H_
  51. #include "core/frameAllocator.h"
  52. #endif
  53. #ifndef _SIMBASE_H_
  54. #include "console/simBase.h"
  55. #endif
  56. #ifndef _MATHTYPES_H_
  57. #include "math/mathTypes.h"
  58. #endif
  59. #ifndef _MPOINT2_H_
  60. #include "math/mPoint2.h"
  61. #endif
  62. #ifndef _ASSET_BASE_H_
  63. #include "assets/assetBase.h"
  64. #endif
  65. // Script bindings.
  66. #include "taml_ScriptBinding.h"
  67. // Debug Profiling.
  68. #include "platform/profiler.h"
  69. //-----------------------------------------------------------------------------
  70. IMPLEMENT_CONOBJECT(Taml);
  71. //-----------------------------------------------------------------------------
  72. StringTableEntry tamlRefIdName = StringTable->insert("TamlId");
  73. StringTableEntry tamlRefToIdName = StringTable->insert("TamlRefId");
  74. StringTableEntry tamlNamedObjectName = StringTable->insert("Name");
  75. //-----------------------------------------------------------------------------
  76. typedef Taml::TamlFormatMode _TamlFormatMode;
  77. ImplementEnumType(_TamlFormatMode,
  78. "")
  79. { Taml::XmlFormat, "xml" },
  80. { Taml::BinaryFormat, "binary" },
  81. { Taml::JSONFormat, "json" }
  82. EndImplementEnumType;
  83. //-----------------------------------------------------------------------------
  84. Taml::TamlFormatMode Taml::getFormatModeEnum(const char* label)
  85. {
  86. // Search for Mnemonic.
  87. for (U32 i = 0; i < (sizeof(__TamlFormatMode::_sEnums) / sizeof(EnumTable::Value)); i++)
  88. {
  89. if (dStricmp(__TamlFormatMode::_sEnumTable[i].getName(), label) == 0)
  90. return (TamlFormatMode)__TamlFormatMode::_sEnumTable[i].getInt();
  91. }
  92. // Warn.
  93. Con::warnf("Taml::getFormatModeEnum() - Invalid format of '%s'.", label);
  94. return Taml::InvalidFormat;
  95. }
  96. //-----------------------------------------------------------------------------
  97. const char* Taml::getFormatModeDescription(const Taml::TamlFormatMode formatMode)
  98. {
  99. // Search for Mnemonic.
  100. for (U32 i = 0; i < (sizeof(__TamlFormatMode::_sEnums) / sizeof(EnumTable::Value)); i++)
  101. {
  102. if (__TamlFormatMode::_sEnumTable[i].getInt() == (S32)formatMode)
  103. return __TamlFormatMode::_sEnumTable[i].getName();
  104. }
  105. // Warn.
  106. Con::warnf("Taml::getFormatModeDescription() - Invalid format mode.");
  107. return StringTable->EmptyString();
  108. }
  109. //-----------------------------------------------------------------------------
  110. // The string-table-entries are set to string literals below because Taml is used in a static scope and the string-table cannot currently be used like that.
  111. Taml::Taml() :
  112. mMasterNodeId(0),
  113. mFormatMode(XmlFormat),
  114. mJSONStrict(true),
  115. mBinaryCompression(true),
  116. mWriteDefaults(false),
  117. mAutoFormatXmlExtension("taml"),
  118. mAutoFormat(true),
  119. mProgenitorUpdate(true),
  120. mAutoFormatBinaryExtension("baml"),
  121. mAutoFormatJSONExtension("json")
  122. {
  123. // Reset the file-path buffer.
  124. mFilePathBuffer[0] = 0;
  125. }
  126. //-----------------------------------------------------------------------------
  127. void Taml::initPersistFields()
  128. {
  129. docsURL;
  130. // Call parent.
  131. Parent::initPersistFields();
  132. addField("Format", TYPEID<_TamlFormatMode>(), Offset(mFormatMode, Taml), "The read/write format that should be used.");
  133. addField("JSONStrict", TypeBool, Offset(mBinaryCompression, Taml), "Whether to write JSON that is strictly compatible with RFC4627 or not.\n");
  134. addField("BinaryCompression", TypeBool, Offset(mBinaryCompression, Taml), "Whether ZIP compression is used on binary formatting or not.\n");
  135. addField("WriteDefaults", TypeBool, Offset(mWriteDefaults, Taml), "Whether to write static fields that are at their default or not.\n");
  136. addField("ProgenitorUpdate", TypeBool, Offset(mProgenitorUpdate, Taml), "Whether to update each type instances file-progenitor or not.\n");
  137. addField("AutoFormat", TypeBool, Offset(mAutoFormat, Taml), "Whether the format type is automatically determined by the filename extension or not.\n");
  138. addField("AutoFormatXmlExtension", TypeString, Offset(mAutoFormatXmlExtension, Taml), "When using auto-format, this is the extension (end of filename) used to detect the XML format.\n");
  139. addField("AutoFormatBinaryExtension", TypeString, Offset(mAutoFormatBinaryExtension, Taml), "When using auto-format, this is the extension (end of filename) used to detect the BINARY format.\n");
  140. addField("AutoFormatJSONExtension", TypeString, Offset(mAutoFormatJSONExtension, Taml), "When using auto-format, this is the extension (end of filename) used to detect the JSON format.\n");
  141. }
  142. //-----------------------------------------------------------------------------
  143. bool Taml::onAdd()
  144. {
  145. // Call parent.
  146. if (!Parent::onAdd())
  147. return false;
  148. // Set JSON strict mode.
  149. mJSONStrict = Con::getBoolVariable(TAML_JSON_STRICT_VARIBLE, true);
  150. // Reset the compilation.
  151. resetCompilation();
  152. return true;
  153. }
  154. //-----------------------------------------------------------------------------
  155. void Taml::onRemove()
  156. {
  157. // Reset the compilation.
  158. resetCompilation();
  159. // Call parent.
  160. Parent::onRemove();
  161. }
  162. //-----------------------------------------------------------------------------
  163. bool Taml::write(SimObject* pSimObject, const char* pFilename)
  164. {
  165. // Debug Profiling.
  166. PROFILE_SCOPE(Taml_Write);
  167. // Sanity!
  168. AssertFatal(pSimObject != NULL, "Cannot write a NULL object.");
  169. AssertFatal(pFilename != NULL, "Cannot write to a NULL filename.");
  170. // Expand the file-name into the file-path buffer unless we're a secure VFS
  171. #ifndef TORQUE_SECURE_VFS
  172. Con::expandToolScriptFilename(mFilePathBuffer, sizeof(mFilePathBuffer), pFilename);
  173. #else
  174. dMemset(mFilePathBuffer, 0x00, sizeof(mFilePathBuffer));
  175. dMemcpy(mFilePathBuffer, pFilename, dStrlen(pFilename));
  176. #endif
  177. FileStream stream;
  178. // File opened?
  179. if (!stream.open(mFilePathBuffer, Torque::FS::File::Write))
  180. {
  181. // No, so warn.
  182. Con::warnf("Taml::writeFile() - Could not open filename '%s' for write.", mFilePathBuffer);
  183. return false;
  184. }
  185. // Get the file auto-format mode.
  186. const TamlFormatMode formatMode = getFileAutoFormatMode(mFilePathBuffer);
  187. // Reset the compilation.
  188. resetCompilation();
  189. // Write object.
  190. const bool status = write(stream, pSimObject, formatMode);
  191. // Close file.
  192. stream.close();
  193. // Reset the compilation.
  194. resetCompilation();
  195. return status;
  196. }
  197. //-----------------------------------------------------------------------------
  198. SimObject* Taml::read(const char* pFilename)
  199. {
  200. // Debug Profiling.
  201. PROFILE_SCOPE(Taml_Read);
  202. // Sanity!
  203. AssertFatal(pFilename != NULL, "Cannot read from a NULL filename.");
  204. // Expand the file-name into the file-path buffer.
  205. Con::expandScriptFilename(mFilePathBuffer, sizeof(mFilePathBuffer), pFilename);
  206. FileStream stream;
  207. // File opened?
  208. if (!stream.open(mFilePathBuffer, Torque::FS::File::Read))
  209. {
  210. // No, so warn.
  211. Con::warnf("Taml::read() - Could not open filename '%s' for read.", mFilePathBuffer);
  212. return NULL;
  213. }
  214. // Get the file auto-format mode.
  215. const TamlFormatMode formatMode = getFileAutoFormatMode(mFilePathBuffer);
  216. // Reset the compilation.
  217. resetCompilation();
  218. // Write object.
  219. SimObject* pSimObject = read(stream, formatMode);
  220. // Close file.
  221. stream.close();
  222. // Reset the compilation.
  223. resetCompilation();
  224. // Did we generate an object?
  225. if (pSimObject == NULL)
  226. {
  227. // No, so warn.
  228. Con::warnf("Taml::read() - Failed to load an object from the file '%s'.", mFilePathBuffer);
  229. }
  230. else
  231. {
  232. pSimObject->onPostAdd();
  233. }
  234. return pSimObject;
  235. }
  236. //-----------------------------------------------------------------------------
  237. bool Taml::write(FileStream& stream, SimObject* pSimObject, const TamlFormatMode formatMode)
  238. {
  239. // Sanity!
  240. AssertFatal(pSimObject != NULL, "Cannot write a NULL object.");
  241. // Compile nodes.
  242. TamlWriteNode* pRootNode = compileObject(pSimObject);
  243. // Format appropriately.
  244. switch (formatMode)
  245. {
  246. /// Xml.
  247. case XmlFormat:
  248. {
  249. // Create writer.
  250. TamlXmlWriter writer(this);
  251. // Write.
  252. return writer.write(stream, pRootNode);
  253. }
  254. /// Binary.
  255. case BinaryFormat:
  256. {
  257. // Create writer.
  258. TamlBinaryWriter writer(this);
  259. // Write.
  260. return writer.write(stream, pRootNode, mBinaryCompression);
  261. }
  262. /// JSON.
  263. case JSONFormat:
  264. {
  265. // Create writer.
  266. TamlJSONWriter writer( this );
  267. // Write.
  268. return writer.write( stream, pRootNode );
  269. }
  270. /// Invalid.
  271. case InvalidFormat:
  272. {
  273. // Warn.
  274. Con::warnf("Taml::write() - Cannot write, invalid format.");
  275. return false;
  276. }
  277. }
  278. // Warn.
  279. Con::warnf("Taml::write() - Unknown format.");
  280. return false;
  281. }
  282. //-----------------------------------------------------------------------------
  283. SimObject* Taml::read(FileStream& stream, const TamlFormatMode formatMode)
  284. {
  285. // Format appropriately.
  286. switch (formatMode)
  287. {
  288. /// Xml.
  289. case XmlFormat:
  290. {
  291. // Create reader.
  292. TamlXmlReader reader(this);
  293. // Read.
  294. return reader.read(stream);
  295. }
  296. /// Binary.
  297. case BinaryFormat:
  298. {
  299. // Create reader.
  300. TamlBinaryReader reader(this);
  301. // Read.
  302. return reader.read(stream);
  303. }
  304. /// JSON.
  305. case JSONFormat:
  306. {
  307. // Create reader.
  308. TamlJSONReader reader( this );
  309. // Read.
  310. return reader.read( stream );
  311. }
  312. /// Invalid.
  313. case InvalidFormat:
  314. {
  315. // Warn.
  316. Con::warnf("Taml::read() - Cannot read, invalid format.");
  317. return NULL;
  318. }
  319. }
  320. // Warn.
  321. Con::warnf("Taml::read() - Unknown format.");
  322. return NULL;
  323. }
  324. //-----------------------------------------------------------------------------
  325. bool Taml::parse(const char* pFilename, TamlVisitor& visitor)
  326. {
  327. // Debug Profiling.
  328. PROFILE_SCOPE(Taml_Parse);
  329. // Sanity!
  330. AssertFatal(pFilename != NULL, "Taml::parse() - Cannot parse a NULL filename.");
  331. // Fetch format mode.
  332. const TamlFormatMode formatMode = getFileAutoFormatMode(pFilename);
  333. // Handle format mode appropriately.
  334. switch (formatMode)
  335. {
  336. case XmlFormat:
  337. {
  338. // Parse with the visitor.
  339. TamlXmlParser parser;
  340. // Are property changes needed but not supported?
  341. if (visitor.wantsPropertyChanges() && !parser.canChangeProperty())
  342. {
  343. // Yes, so warn.
  344. Con::warnf("Taml::parse() - Cannot parse '%s' file-type for filename '%s' as a specified visitor requires property changes which are not supported by the parser.", getFormatModeDescription(formatMode), pFilename);
  345. return false;
  346. }
  347. return parser.accept(pFilename, visitor);
  348. }
  349. case JSONFormat:
  350. {
  351. // Parse with the visitor.
  352. TamlJSONParser parser;
  353. // Are property changes needed but not supported?
  354. if ( visitor.wantsPropertyChanges() && !parser.canChangeProperty() )
  355. {
  356. // Yes, so warn.
  357. Con::warnf( "Taml::parse() - Cannot parse '%s' file-type for filename '%s' as a specified visitor requires property changes which are not supported by the parser.", getFormatModeDescription(formatMode), pFilename );
  358. return false;
  359. }
  360. return parser.accept( pFilename, visitor );
  361. }
  362. case BinaryFormat:
  363. {
  364. // Parse with the visitor.
  365. TamlBinaryParser parser;
  366. // Are property changes needed but not supported?
  367. if (visitor.wantsPropertyChanges() && !parser.canChangeProperty())
  368. {
  369. // Yes, so warn.
  370. Con::warnf("Taml::parse() - Cannot parse '%s' file-type for filename '%s' as a specified visitor requires property changes which are not supported by the parser.", getFormatModeDescription(formatMode), pFilename);
  371. return false;
  372. }
  373. return parser.accept(pFilename, visitor);
  374. }
  375. default:
  376. break;
  377. }
  378. // Warn.
  379. Con::warnf("Taml::parse() - Cannot parse '%s' file-type for filename '%s' as a required parser is not available.", getFormatModeDescription(formatMode), pFilename);
  380. return false;
  381. }
  382. //-----------------------------------------------------------------------------
  383. void Taml::resetCompilation(void)
  384. {
  385. // Debug Profiling.
  386. PROFILE_SCOPE(Taml_ResetCompilation);
  387. // Clear compiled nodes.
  388. for (typeNodeVector::iterator itr = mCompiledNodes.begin(); itr != mCompiledNodes.end(); ++itr)
  389. {
  390. // Fetch node.
  391. TamlWriteNode* pNode = (*itr);
  392. // Reset node.
  393. pNode->resetNode();
  394. // Delete node.
  395. delete pNode;
  396. }
  397. mCompiledNodes.clear();
  398. // Clear compiled objects.
  399. mCompiledObjects.clear();
  400. // Reset master node Id.
  401. mMasterNodeId = 0;
  402. }
  403. //-----------------------------------------------------------------------------
  404. Taml::TamlFormatMode Taml::getFileAutoFormatMode(const char* pFilename)
  405. {
  406. // Sanity!
  407. AssertFatal(pFilename != NULL, "Taml::getFileAutoFormatMode() - Cannot auto-format using a NULL filename.");
  408. // Is auto-format active?
  409. if (mAutoFormat)
  410. {
  411. // Yes, so fetch the extension lengths.
  412. const U32 xmlExtensionLength = dStrlen(mAutoFormatXmlExtension);
  413. const U32 binaryExtensionLength = dStrlen(mAutoFormatBinaryExtension);
  414. const U32 jsonExtensionLength = dStrlen(mAutoFormatJSONExtension);
  415. // Fetch filename length.
  416. const U32 filenameLength = dStrlen(pFilename);
  417. // Fetch end of filename,
  418. const char* pEndOfFilename = pFilename + filenameLength;
  419. // Check for the XML format.
  420. if (xmlExtensionLength <= filenameLength && dStricmp(pEndOfFilename - xmlExtensionLength, mAutoFormatXmlExtension) == 0)
  421. return Taml::XmlFormat;
  422. // Check for the Binary format.
  423. if (binaryExtensionLength <= filenameLength && dStricmp(pEndOfFilename - xmlExtensionLength, mAutoFormatBinaryExtension) == 0)
  424. return Taml::BinaryFormat;
  425. // Check for the XML format.
  426. if (jsonExtensionLength <= filenameLength && dStricmp(pEndOfFilename - jsonExtensionLength, mAutoFormatJSONExtension) == 0)
  427. return Taml::JSONFormat;
  428. }
  429. // Use the explicitly specified format mode.
  430. return mFormatMode;
  431. }
  432. //-----------------------------------------------------------------------------
  433. TamlWriteNode* Taml::compileObject(SimObject* pSimObject, const bool forceId)
  434. {
  435. // Debug Profiling.
  436. PROFILE_SCOPE(Taml_CompileObject);
  437. // Sanity!
  438. AssertFatal(pSimObject != NULL, "Taml::compileObject() - Cannot compile a NULL object.");
  439. // Fetch object Id.
  440. const SimObjectId objectId = pSimObject->getId();
  441. // Find a previously compiled node.
  442. typeCompiledHash::Iterator compiledItr = mCompiledObjects.find(objectId);
  443. // Have we already compiled this?
  444. if (compiledItr != mCompiledObjects.end())
  445. {
  446. // Yes, so sanity!
  447. AssertFatal(mCompiledNodes.size() != 0, "Taml::compileObject() - Found a compiled node at the root.");
  448. // Yes, so fetch node.
  449. TamlWriteNode* compiledNode = compiledItr->value;
  450. // Is a reference Id already present?
  451. if (compiledNode->mRefId == 0)
  452. {
  453. // No, so allocate one.
  454. compiledNode->mRefId = ++mMasterNodeId;
  455. }
  456. // Create write node.
  457. TamlWriteNode* pNewNode = new TamlWriteNode();
  458. pNewNode->set(pSimObject);
  459. // Set reference node.
  460. pNewNode->mRefToNode = compiledNode;
  461. // Push new node.
  462. mCompiledNodes.push_back(pNewNode);
  463. return pNewNode;
  464. }
  465. // No, so create write node.
  466. TamlWriteNode* pNewNode = new TamlWriteNode();
  467. pNewNode->set(pSimObject);
  468. // Is an Id being forced for this object?
  469. if (forceId)
  470. {
  471. // Yes, so allocate one.
  472. pNewNode->mRefId = ++mMasterNodeId;
  473. }
  474. // Push new node.
  475. mCompiledNodes.push_back(pNewNode);
  476. // Insert compiled object.
  477. mCompiledObjects.insertUnique(objectId, pNewNode);
  478. // Are there any Taml callbacks?
  479. if (pNewNode->mpTamlCallbacks != NULL)
  480. {
  481. // Yes, so call it.
  482. tamlPreWrite(pNewNode->mpTamlCallbacks);
  483. }
  484. // Compile static and dynamic fields.
  485. compileStaticFields(pNewNode);
  486. compileDynamicFields(pNewNode);
  487. // Compile children.
  488. compileChildren(pNewNode);
  489. // Compile custom state.
  490. compileCustomState(pNewNode);
  491. // Are there any Taml callbacks?
  492. if (pNewNode->mpTamlCallbacks != NULL)
  493. {
  494. // Yes, so call it.
  495. tamlPostWrite(pNewNode->mpTamlCallbacks);
  496. }
  497. return pNewNode;
  498. }
  499. //-----------------------------------------------------------------------------
  500. void Taml::compileStaticFields(TamlWriteNode* pTamlWriteNode)
  501. {
  502. // Debug Profiling.
  503. PROFILE_SCOPE(Taml_CompileStaticFields);
  504. // Sanity!
  505. AssertFatal(pTamlWriteNode != NULL, "Cannot compile static fields on a NULL node.");
  506. AssertFatal(pTamlWriteNode->mpSimObject != NULL, "Cannot compile static fields on a node with no object.");
  507. // Fetch object.
  508. SimObject* pSimObject = pTamlWriteNode->mpSimObject;
  509. // Fetch field list.
  510. const AbstractClassRep::FieldList& fieldList = pSimObject->getFieldList();
  511. // Fetch field count.
  512. const U32 fieldCount = fieldList.size();
  513. ConsoleObject* defaultConObject = NULL;
  514. SimObject* defaultObject = NULL;
  515. if (!getWriteDefaults())
  516. {
  517. // Create a default object of the same type
  518. defaultConObject = ConsoleObject::create(pSimObject->getClassName());
  519. if (!defaultConObject)
  520. return;
  521. defaultObject = dynamic_cast<SimObject*>(defaultConObject);
  522. }
  523. // ***Really*** shouldn't happen
  524. if (!defaultConObject || !defaultObject)
  525. return;
  526. // Iterate fields.
  527. U8 arrayDepth = 0;
  528. TamlCustomNode* currentArrayNode = NULL;
  529. for (U32 index = 0; index < fieldCount; ++index)
  530. {
  531. // Fetch field.
  532. const AbstractClassRep::Field* pField = &fieldList[index];
  533. // Ignore if field not appropriate.
  534. if (pField->type == AbstractClassRep::DeprecatedFieldType ||
  535. pField->type == AbstractClassRep::StartGroupFieldType ||
  536. pField->type == AbstractClassRep::EndGroupFieldType)
  537. continue;
  538. if (pField->type == AbstractClassRep::StartArrayFieldType)
  539. {
  540. TamlCustomNodes& pCustomNodes = pTamlWriteNode->mCustomNodes;
  541. currentArrayNode = pCustomNodes.addNode(pField->pGroupname);
  542. for (U16 idx = 0; idx < pField->elementCount; idx++)
  543. currentArrayNode->addNode(pField->pFieldname);
  544. arrayDepth++;
  545. continue;
  546. }
  547. if (pField->type == AbstractClassRep::EndArrayFieldType)
  548. {
  549. arrayDepth--;
  550. continue;
  551. }
  552. if (arrayDepth == 0 && pField->elementCount > 1)
  553. {
  554. TamlCustomNodes& pCustomNodes = pTamlWriteNode->mCustomNodes;
  555. char* niceFieldName = const_cast<char *>(pField->pFieldname);
  556. niceFieldName[0] = dToupper(niceFieldName[0]);
  557. String str_niceFieldName = String(niceFieldName);
  558. currentArrayNode = pCustomNodes.addNode(str_niceFieldName + "s");
  559. for (U16 idx = 0; idx < pField->elementCount; idx++)
  560. currentArrayNode->addNode(str_niceFieldName);
  561. }
  562. // Fetch fieldname.
  563. StringTableEntry fieldName = StringTable->insert(pField->pFieldname);
  564. // Fetch element count.
  565. const U32 elementCount = pField->elementCount;
  566. // Skip if the field should not be written.
  567. // For now, we only deal with non-array fields.
  568. if (elementCount == 1 &&
  569. pField->setDataFn != NULL &&
  570. (!getWriteDefaults() && pField->writeDataFn(pSimObject, fieldName) == false))
  571. continue;
  572. // Iterate elements.
  573. for (U32 elementIndex = 0; elementIndex < elementCount; ++elementIndex)
  574. {
  575. char indexBuffer[8];
  576. dSprintf(indexBuffer, 8, "%d", elementIndex);
  577. // Fetch object field value.
  578. const char* pFieldValue = pSimObject->getPrefixedDataField(fieldName, indexBuffer);
  579. if (!pFieldValue)
  580. pFieldValue = StringTable->EmptyString();
  581. U32 nBufferSize = dStrlen(pFieldValue) + 1;
  582. FrameTemp<char> valueCopy(nBufferSize);
  583. dStrcpy((char *)valueCopy, pFieldValue, nBufferSize);
  584. // Skip if field should not be written.
  585. if (!pSimObject->writeField(fieldName, valueCopy))
  586. continue;
  587. if (!getWriteDefaults())
  588. {
  589. //If the field hasn't been changed from the default value, then don't bother writing it out
  590. const char* fieldData = defaultObject->getDataField(fieldName, indexBuffer);
  591. if (fieldData && fieldData[0] != '\0' && dStricmp(fieldData, pFieldValue) == 0)
  592. continue;
  593. }
  594. // Reassign field value.
  595. pFieldValue = valueCopy;
  596. if (pField->type == TypeBool)
  597. pFieldValue = dAtob(pFieldValue) ? "true" : "false";
  598. // Detect and collapse relative path information
  599. char fnBuf[1024];
  600. if ((S32)pField->type == TypeFilename)
  601. {
  602. Con::collapseScriptFilename(fnBuf, 1024, pFieldValue);
  603. pFieldValue = fnBuf;
  604. }
  605. // Save field/value.
  606. if (currentArrayNode && (arrayDepth > 0 || pField->elementCount > 1))
  607. currentArrayNode->getChildren()[elementIndex]->addField(fieldName, pFieldValue);
  608. else
  609. {
  610. TamlWriteNode::FieldValuePair* pFieldValuePair = new TamlWriteNode::FieldValuePair(fieldName, pFieldValue);
  611. pTamlWriteNode->mFields.push_back(pFieldValuePair);
  612. }
  613. }
  614. }
  615. if (!getWriteDefaults())
  616. {
  617. // Cleanup our created default object
  618. delete defaultConObject;
  619. }
  620. }
  621. //-----------------------------------------------------------------------------
  622. static S32 QSORT_CALLBACK compareFieldEntries(const void* a, const void* b)
  623. {
  624. // Debug Profiling.
  625. PROFILE_SCOPE(Taml_CompareFieldEntries);
  626. SimFieldDictionary::Entry *fa = *((SimFieldDictionary::Entry **)a);
  627. SimFieldDictionary::Entry *fb = *((SimFieldDictionary::Entry **)b);
  628. return dStricmp(fa->slotName, fb->slotName);
  629. }
  630. //-----------------------------------------------------------------------------
  631. void Taml::compileDynamicFields(TamlWriteNode* pTamlWriteNode)
  632. {
  633. // Debug Profiling.
  634. PROFILE_SCOPE(Taml_CompileDynamicFields);
  635. // Sanity!
  636. AssertFatal(pTamlWriteNode != NULL, "Cannot compile dynamic fields on a NULL node.");
  637. AssertFatal(pTamlWriteNode->mpSimObject != NULL, "Cannot compile dynamic fields on a node with no object.");
  638. // Fetch object.
  639. SimObject* pSimObject = pTamlWriteNode->mpSimObject;
  640. // Fetch field dictionary.
  641. SimFieldDictionary* pFieldDictionary = pSimObject->getFieldDictionary();
  642. // Ignore if not writing dynamic fields.
  643. if (!pFieldDictionary || !pSimObject->getCanSaveDynamicFields())
  644. return;
  645. // Fetch field list.
  646. const AbstractClassRep::FieldList& fieldList = pSimObject->getFieldList();
  647. // Fetch field count.
  648. const U32 fieldCount = fieldList.size();
  649. Vector<SimFieldDictionary::Entry*> dynamicFieldList(__FILE__, __LINE__);
  650. // Ensure the dynamic field doesn't conflict with static field.
  651. for (U32 hashIndex = 0; hashIndex < SimFieldDictionary::HashTableSize; ++hashIndex)
  652. {
  653. for (SimFieldDictionary::Entry* pEntry = pFieldDictionary->mHashTable[hashIndex]; pEntry; pEntry = pEntry->next)
  654. {
  655. // Iterate static fields.
  656. U32 fieldIndex;
  657. for (fieldIndex = 0; fieldIndex < fieldCount; ++fieldIndex)
  658. {
  659. if (fieldList[fieldIndex].pFieldname == pEntry->slotName)
  660. break;
  661. }
  662. // Skip if found.
  663. if (fieldIndex != (U32)fieldList.size())
  664. continue;
  665. // Skip if not writing field.
  666. if (!pSimObject->writeField(pEntry->slotName, pEntry->value))
  667. continue;
  668. dynamicFieldList.push_back(pEntry);
  669. }
  670. }
  671. // Sort Entries to prevent version control conflicts
  672. if (dynamicFieldList.size() > 1)
  673. dQsort(dynamicFieldList.address(), dynamicFieldList.size(), sizeof(SimFieldDictionary::Entry*), compareFieldEntries);
  674. // Save the fields.
  675. for (Vector<SimFieldDictionary::Entry*>::iterator entryItr = dynamicFieldList.begin(); entryItr != dynamicFieldList.end(); ++entryItr)
  676. {
  677. // Fetch entry.
  678. SimFieldDictionary::Entry* pEntry = *entryItr;
  679. // Save field/value.
  680. TamlWriteNode::FieldValuePair* pFieldValuePair = new TamlWriteNode::FieldValuePair(pEntry->slotName, pEntry->value);
  681. pTamlWriteNode->mFields.push_back(pFieldValuePair);
  682. }
  683. }
  684. //-----------------------------------------------------------------------------
  685. void Taml::compileChildren(TamlWriteNode* pTamlWriteNode)
  686. {
  687. // Debug Profiling.
  688. PROFILE_SCOPE(Taml_CompileChildren);
  689. // Sanity!
  690. AssertFatal(pTamlWriteNode != NULL, "Cannot compile children on a NULL node.");
  691. AssertFatal(pTamlWriteNode->mpSimObject != NULL, "Cannot compile children on a node with no object.");
  692. // Fetch object.
  693. SimObject* pSimObject = pTamlWriteNode->mpSimObject;
  694. // Fetch the Taml children.
  695. TamlChildren* pChildren = dynamic_cast<TamlChildren*>(pSimObject);
  696. // Finish if object does not contain Taml children.
  697. if (pChildren == NULL || pChildren->getTamlChildCount() == 0)
  698. return;
  699. // Create children vector.
  700. pTamlWriteNode->mChildren = new typeNodeVector();
  701. // Fetch the child count.
  702. const U32 childCount = pChildren->getTamlChildCount();
  703. // Iterate children.
  704. for (U32 childIndex = 0; childIndex < childCount; childIndex++)
  705. {
  706. // Compile object.
  707. TamlWriteNode* pChildTamlWriteNode = compileObject(pChildren->getTamlChild(childIndex));
  708. // Save node.
  709. pTamlWriteNode->mChildren->push_back(pChildTamlWriteNode);
  710. }
  711. }
  712. //-----------------------------------------------------------------------------
  713. void Taml::compileCustomState(TamlWriteNode* pTamlWriteNode)
  714. {
  715. // Debug Profiling.
  716. PROFILE_SCOPE(Taml_CompileCustomProperties);
  717. // Sanity!
  718. AssertFatal(pTamlWriteNode != NULL, "Cannot compile custom state on a NULL node.");
  719. AssertFatal(pTamlWriteNode->mpSimObject != NULL, "Cannot compile custom state on a node with no object.");
  720. // Fetch the custom node on the write node.
  721. TamlCustomNodes& customNodes = pTamlWriteNode->mCustomNodes;
  722. // Are there any Taml callbacks?
  723. if (pTamlWriteNode->mpTamlCallbacks != NULL)
  724. {
  725. // Yes, so call it.
  726. tamlCustomWrite(pTamlWriteNode->mpTamlCallbacks, customNodes);
  727. }
  728. // Fetch custom nodes.
  729. const TamlCustomNodeVector& nodes = customNodes.getNodes();
  730. // Finish if no custom nodes to process.
  731. if (nodes.size() == 0)
  732. return;
  733. // Iterate custom properties.
  734. for (TamlCustomNodeVector::const_iterator customNodesItr = nodes.begin(); customNodesItr != nodes.end(); ++customNodesItr)
  735. {
  736. // Fetch the custom node.
  737. TamlCustomNode* pCustomNode = *customNodesItr;
  738. // Compile custom node state.
  739. compileCustomNodeState(pCustomNode);
  740. }
  741. }
  742. //-----------------------------------------------------------------------------
  743. void Taml::compileCustomNodeState(TamlCustomNode* pCustomNode)
  744. {
  745. // Sanity!
  746. AssertFatal(pCustomNode != NULL, "Taml: Cannot compile NULL custom node state.");
  747. // Fetch children.
  748. const TamlCustomNodeVector& children = pCustomNode->getChildren();
  749. // Fetch proxy object.
  750. SimObject* pProxyObject = pCustomNode->getProxyObject<SimObject>(false);
  751. // Do we have a proxy object?
  752. if (pProxyObject != NULL)
  753. {
  754. // Yes, so sanity!
  755. AssertFatal(children.size() == 0, "Taml: Cannot compile a proxy object on a custom node that has children.");
  756. // Yes, so compile it.
  757. // NOTE: We force an Id for custom compiled objects so we guarantee an Id. The reason for this is fairly
  758. // weak in that the XML parser currently has no way of distinguishing between a compiled object node
  759. // and a custom node. If the node has an Id or an Id-Ref then it's obviously an object and should be parsed as such.
  760. pCustomNode->setWriteNode(compileObject(pProxyObject, true));
  761. return;
  762. }
  763. // Finish if no children.
  764. if (children.size() == 0)
  765. return;
  766. // Iterate children.
  767. for (TamlCustomNodeVector::const_iterator childItr = children.begin(); childItr != children.end(); ++childItr)
  768. {
  769. // Fetch shape node.
  770. TamlCustomNode* pChildNode = *childItr;
  771. // Compile the child.
  772. compileCustomNodeState(pChildNode);
  773. }
  774. }
  775. //-----------------------------------------------------------------------------
  776. SimObject* Taml::createType(StringTableEntry typeName, const Taml* pTaml, const char* pProgenitorSuffix)
  777. {
  778. // Debug Profiling.
  779. PROFILE_SCOPE(Taml_CreateType);
  780. typedef HashTable<StringTableEntry, AbstractClassRep*> typeClassHash;
  781. static typeClassHash mClassMap;
  782. // Sanity!
  783. AssertFatal(typeName != NULL, "Taml: Type cannot be NULL");
  784. // Find type.
  785. typeClassHash::Iterator typeItr = mClassMap.find(typeName);
  786. // Found type?
  787. if (typeItr == mClassMap.end())
  788. {
  789. // No, so find type.
  790. AbstractClassRep* pClassRep = AbstractClassRep::getClassList();
  791. while (pClassRep)
  792. {
  793. // Is this the type?
  794. if (dStricmp(pClassRep->getClassName(), typeName) == 0)
  795. {
  796. // Yes, so insert it.
  797. typeItr = mClassMap.insertUnique(typeName, pClassRep);
  798. break;
  799. }
  800. // Next type.
  801. pClassRep = pClassRep->getNextClass();
  802. }
  803. // Did we find the type?
  804. if (typeItr == mClassMap.end())
  805. {
  806. // No, so warn and fail.
  807. Con::warnf("Taml: Failed to create type '%s' as such a registered type could not be found.", typeName);
  808. return NULL;
  809. }
  810. }
  811. // Create the object.
  812. ConsoleObject* pConsoleObject = typeItr->value->create();
  813. // NOTE: It is important that we don't register the object here as many objects rely on the fact that
  814. // fields are set prior to the object being registered. Registering here will invalid those assumptions.
  815. // Fetch the SimObject.
  816. SimObject* pSimObject = dynamic_cast<SimObject*>(pConsoleObject);
  817. // Was it a SimObject?
  818. if (pSimObject == NULL)
  819. {
  820. // No, so warn.
  821. Con::warnf("Taml: Failed to create type '%s' as it is not a SimObject.", typeName);
  822. // Destroy object and fail.
  823. delete pConsoleObject;
  824. return NULL;
  825. }
  826. // Are we updating the file-progenitor?
  827. if (pTaml->getProgenitorUpdate())
  828. {
  829. // Yes, so do we have a progenitor suffix?
  830. if (pProgenitorSuffix == NULL)
  831. {
  832. // No, so just set it to the progenitor file.
  833. pSimObject->setProgenitorFile(pTaml->getFilePathBuffer());
  834. }
  835. else
  836. {
  837. // Yes, so format the progenitor buffer.
  838. char progenitorBuffer[2048];
  839. dSprintf(progenitorBuffer, sizeof(progenitorBuffer), "%s,%s", pTaml->getFilePathBuffer(), pProgenitorSuffix);
  840. // Set the progenitor file.
  841. pSimObject->setProgenitorFile(progenitorBuffer);
  842. }
  843. }
  844. return pSimObject;
  845. }
  846. //-----------------------------------------------------------------------------
  847. tinyxml2::XMLElement* g__write_schema_attribute_element(const AbstractClassRep::Field& field, AbstractClassRep* pType,
  848. tinyxml2::XMLDocument& schemaDocument)
  849. {
  850. // Skip if not a data field.
  851. if (field.type == AbstractClassRep::DeprecatedFieldType ||
  852. field.type == AbstractClassRep::StartGroupFieldType ||
  853. field.type == AbstractClassRep::EndGroupFieldType)
  854. return NULL;
  855. // Skip if the field root is not this type.
  856. if (pType->findFieldRoot(field.pFieldname) != pType)
  857. return NULL;
  858. // Add attribute element.
  859. tinyxml2::XMLElement* pAttributeElement = schemaDocument.NewElement("xs:attribute");
  860. pAttributeElement->SetAttribute("name", field.pFieldname);
  861. // Handle the console type appropriately.
  862. const S32 fieldType = (S32)field.type;
  863. /*
  864. // Is the field an enumeration?
  865. if ( fieldType == TypeEnum )
  866. {
  867. // Yes, so add attribute type.
  868. tinyxml2::XMLElement* pAttributeSimpleTypeElement = schemaDocument.NewElement( "xs:simpleType" );
  869. pAttributeElement->LinkEndChild( pAttributeSimpleTypeElement );
  870. // Add restriction element.
  871. tinyxml2::XMLElement* pAttributeRestrictionElement = schemaDocument.NewElement( "xs:restriction" );
  872. pAttributeRestrictionElement->SetAttribute( "base", "xs:string" );
  873. pAttributeSimpleTypeElement->LinkEndChild( pAttributeRestrictionElement );
  874. // Yes, so fetch enumeration count.
  875. const S32 enumCount = field.table->size;
  876. // Iterate enumeration.
  877. for( S32 index = 0; index < enumCount; ++index )
  878. {
  879. // Add enumeration element.
  880. tinyxml2::XMLElement* pAttributeEnumerationElement = schemaDocument.NewElement( "xs:enumeration" );
  881. pAttributeEnumerationElement->SetAttribute( "value", field.table->table[index].label );
  882. pAttributeRestrictionElement->LinkEndChild( pAttributeEnumerationElement );
  883. }
  884. }
  885. else
  886. {*/
  887. // No, so assume it's a string type initially.
  888. const char* pFieldTypeDescription = "xs:string";
  889. // Handle known types.
  890. if (fieldType == TypeF32)
  891. {
  892. pFieldTypeDescription = "xs:float";
  893. }
  894. else if (fieldType == TypeS8 || fieldType == TypeS16 || fieldType == TypeS32)
  895. {
  896. pFieldTypeDescription = "xs:int";
  897. }
  898. else if (fieldType == TypeBool || fieldType == TypeFlag)
  899. {
  900. pFieldTypeDescription = "xs:boolean";
  901. }
  902. else if (fieldType == TypePoint2F)
  903. {
  904. pFieldTypeDescription = "Point2F_ConsoleType";
  905. }
  906. else if (fieldType == TypePoint2I)
  907. {
  908. pFieldTypeDescription = "Point2I_ConsoleType";
  909. }
  910. else if (fieldType == TypeRectI)
  911. {
  912. pFieldTypeDescription = "RectI_ConsoleType";
  913. }
  914. else if (fieldType == TypeRectF)
  915. {
  916. pFieldTypeDescription = "RectF_ConsoleType";
  917. }
  918. else if (fieldType == TypeColorF)
  919. {
  920. pFieldTypeDescription = "ColorF_ConsoleType";
  921. }
  922. else if (fieldType == TypeColorI)
  923. {
  924. pFieldTypeDescription = "ColorI_ConsoleType";
  925. }
  926. else if (fieldType == TypeAssetId/* ||
  927. fieldType == TypeImageAssetPtr ||
  928. fieldType == TypeAnimationAssetPtr ||
  929. fieldType == TypeAudioAssetPtr*/)
  930. {
  931. pFieldTypeDescription = "AssetId_ConsoleType";
  932. }
  933. // Set attribute type.
  934. pAttributeElement->SetAttribute("type", pFieldTypeDescription);
  935. //}
  936. pAttributeElement->SetAttribute("use", "optional");
  937. return pAttributeElement;
  938. }
  939. String g_sanitize_schema_element_name(String buffer)
  940. {
  941. return buffer.replace("(", "")
  942. .replace(")", "");
  943. }
  944. bool Taml::generateTamlSchema()
  945. {
  946. // Fetch any TAML Schema file reference.
  947. const char* pTamlSchemaFile = Con::getVariable(TAML_SCHEMA_VARIABLE);
  948. // Do we have a schema file reference?
  949. if (pTamlSchemaFile == NULL || *pTamlSchemaFile == 0)
  950. {
  951. // No, so warn.
  952. Con::warnf("Taml::generateTamlSchema() - Cannot write a TAML schema as no schema variable is set ('%s').", TAML_SCHEMA_VARIABLE);
  953. return false;
  954. }
  955. // Expand the file-name into the file-path buffer.
  956. char filePathBuffer[1024];
  957. Con::expandToolScriptFilename(filePathBuffer, sizeof(filePathBuffer), pTamlSchemaFile);
  958. FileStream stream;
  959. // File opened?
  960. /*if ( !stream.open( filePathBuffer, Torque::FS::File::Write ) )
  961. {
  962. // No, so warn.
  963. Con::warnf("Taml::GenerateTamlSchema() - Could not open filename '%s' for write.", filePathBuffer );
  964. return false;
  965. }*/
  966. // Create document.
  967. tinyxml2::XMLDocument schemaDocument;
  968. // Add declaration.
  969. tinyxml2::XMLDeclaration* schemaDeclaration = schemaDocument.NewDeclaration("xml version=\"1.0\" encoding=\"iso-8859-1\" standalone =\"no\"");
  970. schemaDocument.InsertEndChild(schemaDeclaration);
  971. // Add schema element.
  972. tinyxml2::XMLElement* pSchemaElement = schemaDocument.NewElement("xs:schema");
  973. pSchemaElement->SetAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema");
  974. schemaDocument.LinkEndChild(pSchemaElement);
  975. // Fetch class-rep root.
  976. AbstractClassRep* pRootType = AbstractClassRep::getClassList();
  977. // Fetch SimObject class rep.
  978. AbstractClassRep* pSimObjectType = AbstractClassRep::findClassRep("SimObject");
  979. // Sanity!
  980. AssertFatal(pSimObjectType != NULL, "Taml::GenerateTamlSchema() - Could not find SimObject class rep.");
  981. // Reset scratch state.
  982. char buffer[1024];
  983. HashTable<AbstractClassRep*, StringTableEntry> childGroups;
  984. // *************************************************************
  985. // Generate console type elements.
  986. // *************************************************************
  987. // Vector2.
  988. tinyxml2::XMLComment* pVector2Comment = schemaDocument.NewComment("Vector2 Console Type");
  989. pSchemaElement->LinkEndChild(pVector2Comment);
  990. tinyxml2::XMLElement* pVector2TypeElement = schemaDocument.NewElement("xs:simpleType");
  991. pVector2TypeElement->SetAttribute("name", "Vector2_ConsoleType");
  992. pSchemaElement->LinkEndChild(pVector2TypeElement);
  993. tinyxml2::XMLElement* pVector2ElementA = schemaDocument.NewElement("xs:restriction");
  994. pVector2ElementA->SetAttribute("base", "xs:string");
  995. pVector2TypeElement->LinkEndChild(pVector2ElementA);
  996. tinyxml2::XMLElement* pVector2ElementB = schemaDocument.NewElement("xs:pattern");
  997. pVector2ElementB->SetAttribute("value", "([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b");
  998. pVector2ElementA->LinkEndChild(pVector2ElementB);
  999. // Point2F.
  1000. tinyxml2::XMLComment* pPoint2FComment = schemaDocument.NewComment("Point2F Console Type");
  1001. pSchemaElement->LinkEndChild(pPoint2FComment);
  1002. tinyxml2::XMLElement* pPoint2FTypeElement = schemaDocument.NewElement("xs:simpleType");
  1003. pPoint2FTypeElement->SetAttribute("name", "Point2F_ConsoleType");
  1004. pSchemaElement->LinkEndChild(pPoint2FTypeElement);
  1005. tinyxml2::XMLElement* pPoint2FElementA = schemaDocument.NewElement("xs:restriction");
  1006. pPoint2FElementA->SetAttribute("base", "xs:string");
  1007. pPoint2FTypeElement->LinkEndChild(pPoint2FElementA);
  1008. tinyxml2::XMLElement* pPoint2FElementB = schemaDocument.NewElement("xs:pattern");
  1009. pPoint2FElementB->SetAttribute("value", "([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b");
  1010. pPoint2FElementA->LinkEndChild(pPoint2FElementB);
  1011. // Point2I.
  1012. tinyxml2::XMLComment* pPoint2IComment = schemaDocument.NewComment("Point2I Console Type");
  1013. pSchemaElement->LinkEndChild(pPoint2IComment);
  1014. tinyxml2::XMLElement* pPoint2ITypeElement = schemaDocument.NewElement("xs:simpleType");
  1015. pPoint2ITypeElement->SetAttribute("name", "Point2I_ConsoleType");
  1016. pSchemaElement->LinkEndChild(pPoint2ITypeElement);
  1017. tinyxml2::XMLElement* pPoint2IElementA = schemaDocument.NewElement("xs:restriction");
  1018. pPoint2IElementA->SetAttribute("base", "xs:string");
  1019. pPoint2ITypeElement->LinkEndChild(pPoint2IElementA);
  1020. tinyxml2::XMLElement* pPoint2IElementB = schemaDocument.NewElement("xs:pattern");
  1021. pPoint2IElementB->SetAttribute("value", "[-]?[0-9]* [-]?[0-9]*");
  1022. pPoint2IElementA->LinkEndChild(pPoint2IElementB);
  1023. // b2AABB.
  1024. tinyxml2::XMLComment* pb2AABBComment = schemaDocument.NewComment("b2AABB Console Type");
  1025. pSchemaElement->LinkEndChild(pb2AABBComment);
  1026. tinyxml2::XMLElement* pb2AABBTypeElement = schemaDocument.NewElement("xs:simpleType");
  1027. pb2AABBTypeElement->SetAttribute("name", "b2AABB_ConsoleType");
  1028. pSchemaElement->LinkEndChild(pb2AABBTypeElement);
  1029. tinyxml2::XMLElement* pb2AABBElementA = schemaDocument.NewElement("xs:restriction");
  1030. pb2AABBElementA->SetAttribute("base", "xs:string");
  1031. pb2AABBTypeElement->LinkEndChild(pb2AABBElementA);
  1032. tinyxml2::XMLElement* pb2AABBElementB = schemaDocument.NewElement("xs:pattern");
  1033. pb2AABBElementB->SetAttribute("value", "([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b");
  1034. pb2AABBElementA->LinkEndChild(pb2AABBElementB);
  1035. // RectI.
  1036. tinyxml2::XMLComment* pRectIComment = schemaDocument.NewComment("RectI Console Type");
  1037. pSchemaElement->LinkEndChild(pRectIComment);
  1038. tinyxml2::XMLElement* pRectITypeElement = schemaDocument.NewElement("xs:simpleType");
  1039. pRectITypeElement->SetAttribute("name", "RectI_ConsoleType");
  1040. pSchemaElement->LinkEndChild(pRectITypeElement);
  1041. tinyxml2::XMLElement* pRectIElementA = schemaDocument.NewElement("xs:restriction");
  1042. pRectIElementA->SetAttribute("base", "xs:string");
  1043. pRectITypeElement->LinkEndChild(pRectIElementA);
  1044. tinyxml2::XMLElement* pRectIElementB = schemaDocument.NewElement("xs:pattern");
  1045. pRectIElementB->SetAttribute("value", "[-]?[0-9]* [-]?[0-9]* [-]?[0-9]* [-]?[0-9]*");
  1046. pRectIElementA->LinkEndChild(pRectIElementB);
  1047. // RectF.
  1048. tinyxml2::XMLComment* pRectFComment = schemaDocument.NewComment("RectF Console Type");
  1049. pSchemaElement->LinkEndChild(pRectFComment);
  1050. tinyxml2::XMLElement* pRectFTypeElement = schemaDocument.NewElement("xs:simpleType");
  1051. pRectFTypeElement->SetAttribute("name", "RectF_ConsoleType");
  1052. pSchemaElement->LinkEndChild(pRectFTypeElement);
  1053. tinyxml2::XMLElement* pRectFElementA = schemaDocument.NewElement("xs:restriction");
  1054. pRectFElementA->SetAttribute("base", "xs:string");
  1055. pRectFTypeElement->LinkEndChild(pRectFElementA);
  1056. tinyxml2::XMLElement* pRectFElementB = schemaDocument.NewElement("xs:pattern");
  1057. pRectFElementB->SetAttribute("value", "(\\b[-]?(b[0-9]+)?\\.)?[0-9]+\\b");
  1058. pRectFElementA->LinkEndChild(pRectFElementB);
  1059. // AssetId.
  1060. tinyxml2::XMLComment* pAssetIdComment = schemaDocument.NewComment("AssetId Console Type");
  1061. pSchemaElement->LinkEndChild(pAssetIdComment);
  1062. tinyxml2::XMLElement* pAssetIdTypeElement = schemaDocument.NewElement("xs:simpleType");
  1063. pAssetIdTypeElement->SetAttribute("name", "AssetId_ConsoleType");
  1064. pSchemaElement->LinkEndChild(pAssetIdTypeElement);
  1065. tinyxml2::XMLElement* pAssetIdElementA = schemaDocument.NewElement("xs:restriction");
  1066. pAssetIdElementA->SetAttribute("base", "xs:string");
  1067. pAssetIdTypeElement->LinkEndChild(pAssetIdElementA);
  1068. tinyxml2::XMLElement* pAssetIdElementB = schemaDocument.NewElement("xs:pattern");
  1069. dSprintf(buffer, sizeof(buffer), "(%s)?\\b[a-zA-Z0-9]+\\b%s\\b[a-zA-Z0-9]+\\b", ASSET_ID_FIELD_PREFIX, ASSET_SCOPE_TOKEN);
  1070. pAssetIdElementB->SetAttribute("value", buffer);
  1071. pAssetIdElementA->LinkEndChild(pAssetIdElementB);
  1072. // Color Enums.
  1073. tinyxml2::XMLComment* pColorEnumsComment = schemaDocument.NewComment("Color Enums");
  1074. pSchemaElement->LinkEndChild(pColorEnumsComment);
  1075. tinyxml2::XMLElement* pColorEnumsTypeElement = schemaDocument.NewElement("xs:simpleType");
  1076. pColorEnumsTypeElement->SetAttribute("name", "Color_Enums");
  1077. pSchemaElement->LinkEndChild(pColorEnumsTypeElement);
  1078. tinyxml2::XMLElement* pColorEnumsRestrictionElement = schemaDocument.NewElement("xs:restriction");
  1079. pColorEnumsRestrictionElement->SetAttribute("base", "xs:string");
  1080. pColorEnumsTypeElement->LinkEndChild(pColorEnumsRestrictionElement);
  1081. const S32 ColorEnumsCount = StockColor::getCount();
  1082. for (S32 index = 0; index < ColorEnumsCount; ++index)
  1083. {
  1084. // Add enumeration element.
  1085. tinyxml2::XMLElement* pColorEnumsAttributeEnumerationElement = schemaDocument.NewElement("xs:enumeration");
  1086. pColorEnumsAttributeEnumerationElement->SetAttribute("value", StockColor::getColorItem(index)->getColorName());
  1087. pColorEnumsRestrictionElement->LinkEndChild(pColorEnumsAttributeEnumerationElement);
  1088. }
  1089. // LinearColorF.
  1090. tinyxml2::XMLComment* pColorFValuesComment = schemaDocument.NewComment("LinearColorF Values");
  1091. pSchemaElement->LinkEndChild(pColorFValuesComment);
  1092. tinyxml2::XMLElement* pColorFValuesTypeElement = schemaDocument.NewElement("xs:simpleType");
  1093. pColorFValuesTypeElement->SetAttribute("name", "ColorF_Values");
  1094. pSchemaElement->LinkEndChild(pColorFValuesTypeElement);
  1095. tinyxml2::XMLElement* pColorFValuesElementA = schemaDocument.NewElement("xs:restriction");
  1096. pColorFValuesElementA->SetAttribute("base", "xs:string");
  1097. pColorFValuesTypeElement->LinkEndChild(pColorFValuesElementA);
  1098. tinyxml2::XMLElement* pColorFValuesElementB = schemaDocument.NewElement("xs:pattern");
  1099. pColorFValuesElementB->SetAttribute("value", "([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b");
  1100. pColorFValuesElementA->LinkEndChild(pColorFValuesElementB);
  1101. tinyxml2::XMLComment* pColorFComment = schemaDocument.NewComment("LinearColorF Console Type");
  1102. pSchemaElement->LinkEndChild(pColorFComment);
  1103. tinyxml2::XMLElement* pColorFTypeElement = schemaDocument.NewElement("xs:simpleType");
  1104. pColorFTypeElement->SetAttribute("name", "ColorF_ConsoleType");
  1105. pSchemaElement->LinkEndChild(pColorFTypeElement);
  1106. tinyxml2::XMLElement* pColorFUnionElement = schemaDocument.NewElement("xs:union");
  1107. pColorFUnionElement->SetAttribute("memberTypes", "ColorF_Values Color_Enums");
  1108. pColorFTypeElement->LinkEndChild(pColorFUnionElement);
  1109. // ColorI.
  1110. tinyxml2::XMLComment* pColorIValuesComment = schemaDocument.NewComment("ColorI Values");
  1111. pSchemaElement->LinkEndChild(pColorIValuesComment);
  1112. tinyxml2::XMLElement* pColorIValuesTypeElement = schemaDocument.NewElement("xs:simpleType");
  1113. pColorIValuesTypeElement->SetAttribute("name", "ColorI_Values");
  1114. pSchemaElement->LinkEndChild(pColorIValuesTypeElement);
  1115. tinyxml2::XMLElement* pColorIValuesElementA = schemaDocument.NewElement("xs:restriction");
  1116. pColorIValuesElementA->SetAttribute("base", "xs:string");
  1117. pColorIValuesTypeElement->LinkEndChild(pColorIValuesElementA);
  1118. tinyxml2::XMLElement* pColorIValuesElementB = schemaDocument.NewElement("xs:pattern");
  1119. pColorIValuesElementB->SetAttribute("value", "[-]?[0-9]* [-]?[0-9]* [-]?[0-9]* [-]?[0-9]*");
  1120. pColorIValuesElementA->LinkEndChild(pColorIValuesElementB);
  1121. tinyxml2::XMLComment* pColorIComment = schemaDocument.NewComment("ColorI Console Type");
  1122. pSchemaElement->LinkEndChild(pColorIComment);
  1123. tinyxml2::XMLElement* pColorITypeElement = schemaDocument.NewElement("xs:simpleType");
  1124. pColorITypeElement->SetAttribute("name", "ColorI_ConsoleType");
  1125. pSchemaElement->LinkEndChild(pColorITypeElement);
  1126. tinyxml2::XMLElement* pColorIUnionElement = schemaDocument.NewElement("xs:union");
  1127. pColorIUnionElement->SetAttribute("memberTypes", "ColorI_Values Color_Enums");
  1128. pColorITypeElement->LinkEndChild(pColorIUnionElement);
  1129. // *************************************************************
  1130. // Generate engine type elements.
  1131. // *************************************************************
  1132. // Generate the engine type elements.
  1133. tinyxml2::XMLComment* tComment = schemaDocument.NewComment("Type Elements");
  1134. pSchemaElement->LinkEndChild(tComment);
  1135. for (AbstractClassRep* pType = pRootType; pType != NULL; pType = pType->getNextClass())
  1136. {
  1137. // Add type.
  1138. tinyxml2::XMLElement* pTypeElement = schemaDocument.NewElement("xs:element");
  1139. pTypeElement->SetAttribute("name", pType->getClassName());
  1140. dSprintf(buffer, sizeof(buffer), "%s_Type", pType->getClassName());
  1141. pTypeElement->SetAttribute("type", buffer);
  1142. pSchemaElement->LinkEndChild(pTypeElement);
  1143. }
  1144. // *************************************************************
  1145. // Generate the engine complex types.
  1146. // *************************************************************
  1147. for (AbstractClassRep* pType = pRootType; pType != NULL; pType = pType->getNextClass())
  1148. {
  1149. // Add complex type comment.
  1150. dSprintf(buffer, sizeof(buffer), " %s Type ", pType->getClassName());
  1151. tinyxml2::XMLComment* ctComment = schemaDocument.NewComment(buffer);
  1152. pSchemaElement->LinkEndChild(ctComment);
  1153. // Add complex type.
  1154. tinyxml2::XMLElement* pComplexTypeElement = schemaDocument.NewElement("xs:complexType");
  1155. dSprintf(buffer, sizeof(buffer), "%s_Type", pType->getClassName());
  1156. pComplexTypeElement->SetAttribute("name", buffer);
  1157. pSchemaElement->LinkEndChild(pComplexTypeElement);
  1158. // Add sequence.
  1159. tinyxml2::XMLElement* pAllElement = schemaDocument.NewElement("xs:all");
  1160. pComplexTypeElement->LinkEndChild(pAllElement);
  1161. // Fetch container child class.
  1162. AbstractClassRep* pContainerChildClass = pType->getContainerChildClass(true);
  1163. // Is the type allowed children?
  1164. if (pContainerChildClass != NULL)
  1165. {
  1166. // Yes, so add choice element.
  1167. tinyxml2::XMLElement* pChoiceElement = schemaDocument.NewElement("xs:choice");
  1168. pChoiceElement->SetAttribute("minOccurs", 0);
  1169. pChoiceElement->SetAttribute("maxOccurs", "unbounded");
  1170. pAllElement->LinkEndChild(pChoiceElement);
  1171. // Find child group.
  1172. HashTable<AbstractClassRep*, StringTableEntry>::Iterator childGroupItr = childGroups.find(pContainerChildClass);
  1173. // Does the group exist?
  1174. if (childGroupItr == childGroups.end())
  1175. {
  1176. // No, so format group name.
  1177. dSprintf(buffer, sizeof(buffer), "%s_ChildrenTypes", pContainerChildClass->getClassName());
  1178. // Insert into child group hash.
  1179. childGroupItr = childGroups.insertUnique(pContainerChildClass, StringTable->insert(buffer));
  1180. // Add the group.
  1181. tinyxml2::XMLElement* pChildrenGroupElement = schemaDocument.NewElement("xs:group");
  1182. pChildrenGroupElement->SetAttribute("name", buffer);
  1183. pSchemaElement->LinkEndChild(pChildrenGroupElement);
  1184. // Add choice element.
  1185. tinyxml2::XMLElement* pChildreGroupChoiceElement = schemaDocument.NewElement("xs:choice");
  1186. pChildrenGroupElement->LinkEndChild(pChildreGroupChoiceElement);
  1187. // Add choice members.
  1188. for (AbstractClassRep* pChoiceType = pRootType; pChoiceType != NULL; pChoiceType = pChoiceType->getNextClass())
  1189. {
  1190. // Skip if not derived from the container child class.
  1191. if (!pChoiceType->isClass(pContainerChildClass))
  1192. continue;
  1193. // Add choice member.
  1194. tinyxml2::XMLElement* pChildrenMemberElement = schemaDocument.NewElement("xs:element");
  1195. pChildrenMemberElement->SetAttribute("name", pChoiceType->getClassName());
  1196. dSprintf(buffer, sizeof(buffer), "%s_Type", pChoiceType->getClassName());
  1197. pChildrenMemberElement->SetAttribute("type", buffer);
  1198. pChildreGroupChoiceElement->LinkEndChild(pChildrenMemberElement);
  1199. }
  1200. }
  1201. // Reference the child group.
  1202. tinyxml2::XMLElement* pChoiceGroupReferenceElement = schemaDocument.NewElement("xs:group");
  1203. pChoiceGroupReferenceElement->SetAttribute("ref", childGroupItr->value);
  1204. pChoiceGroupReferenceElement->SetAttribute("minOccurs", 0);
  1205. pChoiceElement->LinkEndChild(pChoiceGroupReferenceElement);
  1206. }
  1207. // Generate the custom Taml schema.
  1208. for (AbstractClassRep* pCustomSchemaType = pType; pCustomSchemaType != NULL; pCustomSchemaType = pCustomSchemaType->getParentClass())
  1209. {
  1210. // Fetch the types custom TAML schema function.
  1211. AbstractClassRep::WriteCustomTamlSchema customSchemaFn = pCustomSchemaType->getCustomTamlSchema();
  1212. // Skip if no function avilable.
  1213. if (customSchemaFn == NULL)
  1214. continue;
  1215. // Call schema generation function.
  1216. customSchemaFn(pType, pAllElement);
  1217. }
  1218. // Fetch field list.
  1219. const AbstractClassRep::FieldList& fields = pType->mFieldList;
  1220. // Fetch field count.
  1221. const S32 fieldCount = fields.size();
  1222. // Generate array attribute groups
  1223. for (S32 index = 0; index < fieldCount; ++index)
  1224. {
  1225. // Fetch field.
  1226. const AbstractClassRep::Field& field = fields[index];
  1227. if (field.type == AbstractClassRep::StartArrayFieldType)
  1228. {
  1229. // Add the top-level array identifier
  1230. tinyxml2::XMLElement* pArrayElement = schemaDocument.NewElement("xs:element");
  1231. dSprintf(buffer, sizeof(buffer), "%s.%s", pType->getClassName(), g_sanitize_schema_element_name(field.pGroupname).c_str());
  1232. pArrayElement->SetAttribute("name", buffer);
  1233. pArrayElement->SetAttribute("minOccurs", 0);
  1234. pAllElement->LinkEndChild(pArrayElement);
  1235. // Inline type specification
  1236. tinyxml2::XMLElement* pArrayComplexTypeElement = schemaDocument.NewElement("xs:complexType");
  1237. pArrayElement->LinkEndChild(pArrayComplexTypeElement);
  1238. // Add the actual (repeating) array elements
  1239. tinyxml2::XMLElement* pInnerArrayElement = schemaDocument.NewElement("xs:element");
  1240. pInnerArrayElement->SetAttribute("name", g_sanitize_schema_element_name(field.pFieldname).c_str());
  1241. pInnerArrayElement->SetAttribute("minOccurs", 0);
  1242. pInnerArrayElement->SetAttribute("maxOccurs", field.elementCount);
  1243. pArrayComplexTypeElement->LinkEndChild(pInnerArrayElement);
  1244. // Inline type specification
  1245. tinyxml2::XMLElement* pInnerComplexTypeElement = schemaDocument.NewElement("xs:complexType");
  1246. pInnerArrayElement->LinkEndChild(pInnerComplexTypeElement);
  1247. // Add a reference to the attribute group for the array
  1248. tinyxml2::XMLElement* pInnerAttributeGroupElement = schemaDocument.NewElement("xs:attributeGroup");
  1249. dSprintf(buffer, sizeof(buffer), "%s_%s_Array_Fields", pType->getClassName(), g_sanitize_schema_element_name(field.pFieldname).c_str());
  1250. pInnerAttributeGroupElement->SetAttribute("ref", buffer);
  1251. pInnerComplexTypeElement->LinkEndChild(pInnerAttributeGroupElement);
  1252. // Add the attribute group itself
  1253. tinyxml2::XMLElement* pFieldAttributeGroupElement = schemaDocument.NewElement("xs:attributeGroup");
  1254. pFieldAttributeGroupElement->SetAttribute("name", buffer);
  1255. pSchemaElement->LinkEndChild(pFieldAttributeGroupElement);
  1256. // Keep adding fields to attribute group until we hit the end of the array
  1257. for (; index < fieldCount; ++index)
  1258. {
  1259. const AbstractClassRep::Field& array_field = fields[index];
  1260. if (array_field.type == AbstractClassRep::EndArrayFieldType)
  1261. {
  1262. break;
  1263. }
  1264. tinyxml2::XMLElement* pAttributeElement = g__write_schema_attribute_element(array_field, pType, schemaDocument);
  1265. if (pAttributeElement == NULL)
  1266. {
  1267. continue;
  1268. }
  1269. pFieldAttributeGroupElement->LinkEndChild(pAttributeElement);
  1270. }
  1271. }
  1272. }
  1273. // Generate field attribute group.
  1274. tinyxml2::XMLElement* pFieldAttributeGroupElement = schemaDocument.NewElement("xs:attributeGroup");
  1275. dSprintf(buffer, sizeof(buffer), "%s_Fields", pType->getClassName());
  1276. pFieldAttributeGroupElement->SetAttribute("name", buffer);
  1277. pSchemaElement->LinkEndChild(pFieldAttributeGroupElement);
  1278. // Iterate static fields (in reverse as most types are organized from the root-fields up).
  1279. for (S32 index = fieldCount - 1; index > 0; --index)
  1280. {
  1281. // Fetch field.
  1282. const AbstractClassRep::Field& field = fields[index];
  1283. // Skip fields inside arrays
  1284. if (field.type == AbstractClassRep::EndArrayFieldType)
  1285. {
  1286. for (; index > 0; --index)
  1287. {
  1288. if (field.type == AbstractClassRep::StartArrayFieldType)
  1289. {
  1290. break;
  1291. }
  1292. }
  1293. continue;
  1294. }
  1295. tinyxml2::XMLElement* pAttributeElement = g__write_schema_attribute_element(field, pType, schemaDocument);
  1296. if (pAttributeElement == NULL)
  1297. {
  1298. continue;
  1299. }
  1300. pFieldAttributeGroupElement->LinkEndChild(pAttributeElement);
  1301. }
  1302. // Is this the SimObject Type?
  1303. if (pType == pSimObjectType)
  1304. {
  1305. // Yes, so add reserved Taml field attributes here...
  1306. // Add Taml "Name" attribute element.
  1307. tinyxml2::XMLElement* pNameAttributeElement = schemaDocument.NewElement("xs:attribute");
  1308. pNameAttributeElement->SetAttribute("name", tamlNamedObjectName);
  1309. pNameAttributeElement->SetAttribute("type", "xs:normalizedString");
  1310. pFieldAttributeGroupElement->LinkEndChild(pNameAttributeElement);
  1311. // Add Taml "TamlId" attribute element.
  1312. tinyxml2::XMLElement* pTamlIdAttributeElement = schemaDocument.NewElement("xs:attribute");
  1313. pTamlIdAttributeElement->SetAttribute("name", tamlRefIdName);
  1314. pTamlIdAttributeElement->SetAttribute("type", "xs:nonNegativeInteger");
  1315. pFieldAttributeGroupElement->LinkEndChild(pTamlIdAttributeElement);
  1316. // Add Taml "TamlRefId" attribute element.
  1317. tinyxml2::XMLElement* pTamlRefIdAttributeElement = schemaDocument.NewElement("xs:attribute");
  1318. pTamlRefIdAttributeElement->SetAttribute("name", tamlRefToIdName);
  1319. pTamlRefIdAttributeElement->SetAttribute("type", "xs:nonNegativeInteger");
  1320. pFieldAttributeGroupElement->LinkEndChild(pTamlRefIdAttributeElement);
  1321. }
  1322. // Add attribute group types.
  1323. for (AbstractClassRep* pAttributeGroupsType = pType; pAttributeGroupsType != NULL; pAttributeGroupsType = pAttributeGroupsType->getParentClass())
  1324. {
  1325. tinyxml2::XMLElement* pFieldAttributeGroupRefElement = schemaDocument.NewElement("xs:attributeGroup");
  1326. dSprintf(buffer, sizeof(buffer), "%s_Fields", pAttributeGroupsType->getClassName());
  1327. pFieldAttributeGroupRefElement->SetAttribute("ref", buffer);
  1328. pComplexTypeElement->LinkEndChild(pFieldAttributeGroupRefElement);
  1329. }
  1330. // Add "any" attribute element (dynamic fields).
  1331. tinyxml2::XMLElement* pAnyAttributeElement = schemaDocument.NewElement("xs:anyAttribute");
  1332. pAnyAttributeElement->SetAttribute("processContents", "skip");
  1333. pComplexTypeElement->LinkEndChild(pAnyAttributeElement);
  1334. }
  1335. // Write the schema document.
  1336. schemaDocument.SaveFile(filePathBuffer);
  1337. // Close file.
  1338. stream.close();
  1339. return true;
  1340. }
  1341. //-----------------------------------------------------------------------------
  1342. void Taml::WriteUnrestrictedCustomTamlSchema(const char* pCustomNodeName, const AbstractClassRep* pClassRep, tinyxml2::XMLElement* pParentElement)
  1343. {
  1344. // Sanity!
  1345. AssertFatal(pCustomNodeName != NULL, "Taml::WriteDefaultCustomTamlSchema() - Node name cannot be NULL.");
  1346. AssertFatal(pClassRep != NULL, "Taml::WriteDefaultCustomTamlSchema() - ClassRep cannot be NULL.");
  1347. AssertFatal(pParentElement != NULL, "Taml::WriteDefaultCustomTamlSchema() - Parent Element cannot be NULL.");
  1348. tinyxml2::XMLDocument* doc = pParentElement->GetDocument();
  1349. char buffer[1024];
  1350. // Add custom type element.
  1351. tinyxml2::XMLElement* pCustomElement = doc->NewElement("xs:element");
  1352. dSprintf(buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), pCustomNodeName);
  1353. pCustomElement->SetAttribute("name", buffer);
  1354. pCustomElement->SetAttribute("minOccurs", 0);
  1355. pCustomElement->SetAttribute("maxOccurs", 1);
  1356. pParentElement->LinkEndChild(pCustomElement);
  1357. // Add complex type element.
  1358. tinyxml2::XMLElement* pComplexTypeElement = doc->NewElement("xs:complexType");
  1359. pCustomElement->LinkEndChild(pComplexTypeElement);
  1360. // Add choice element.
  1361. tinyxml2::XMLElement* pChoiceElement = doc->NewElement("xs:choice");
  1362. pChoiceElement->SetAttribute("minOccurs", 0);
  1363. pChoiceElement->SetAttribute("maxOccurs", "unbounded");
  1364. pComplexTypeElement->LinkEndChild(pChoiceElement);
  1365. // Add sequence element.
  1366. tinyxml2::XMLElement* pSequenceElement = doc->NewElement("xs:sequence");
  1367. pChoiceElement->LinkEndChild(pSequenceElement);
  1368. // Add "any" element.
  1369. tinyxml2::XMLElement* pAnyElement = doc->NewElement("xs:any");
  1370. pAnyElement->SetAttribute("processContents", "skip");
  1371. pSequenceElement->LinkEndChild(pAnyElement);
  1372. }