2
0

SimXMLDocument.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "tinyxml/tinyxml2.h"
  23. //-----------------------------------------------------------------------------
  24. // Console implementation of STL map.
  25. //-----------------------------------------------------------------------------
  26. #include "core/frameAllocator.h"
  27. #include "console/simBase.h"
  28. #include "console/consoleInternal.h"
  29. #include "console/SimXMLDocument.h"
  30. #include "console/engineAPI.h"
  31. #include "persistence/taml/fsTinyXml.h"
  32. IMPLEMENT_CONOBJECT(SimXMLDocument);
  33. ConsoleDocClass( SimXMLDocument,
  34. "@brief File I/O object used for creating, reading, and writing XML documents.\n\n"
  35. "A SimXMLDocument is a container of various XML nodes. The Document level may contain "
  36. "a header (sometimes called a declaration), comments and child Elements. Elements may "
  37. "contain attributes, data (or text) and child Elements.\n\n"
  38. "You build new Elements using addNewElement(). This makes the new Element the current "
  39. "one you're working with. You then use setAttribute() to add attributes to the Element. "
  40. "You use addData() or addText() to write to the text area of an Element."
  41. "@tsexample\n"
  42. "// Thanks to Rex Hiebert for this example\n"
  43. "// Given the following XML\n"
  44. "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  45. "<DataTables>\n"
  46. " <table tableName=\"2DShapes\">\n"
  47. " <rec id=\"1\">Triangle</rec>\n"
  48. " <rec id=\"2\">Square</rec>\n"
  49. " <rec id=\"3\">Circle</rec>\n"
  50. " </table>\n"
  51. " <table tableName=\"3DShapes\">\n"
  52. " <rec id=\"1\">Pyramid</rec>\n"
  53. " <rec id=\"2\">Cube</rec>\n"
  54. " <rec id=\"3\">Sphere</rec>\n"
  55. " </table>\n"
  56. "</DataTables>\n\n"
  57. "// Using SimXMLDocument by itself\n"
  58. "function readXmlExample(%filename)\n"
  59. "{\n"
  60. " %xml = new SimXMLDocument() {};\n"
  61. " %xml.loadFile(%filename);\n\n"
  62. " %xml.pushChildElement(\"DataTables\");\n"
  63. " %xml.pushFirstChildElement(\"table\");\n"
  64. " while(true)\n"
  65. " {\n"
  66. " echo(\"TABLE:\" SPC %xml.attribute(\"tableName\"));\n"
  67. " %xml.pushFirstChildElement(\"rec\");\n"
  68. " while (true)\n"
  69. " {\n"
  70. " %id = %xml.attribute(\"id\");\n"
  71. " %desc = %xml.getData();\n"
  72. " echo(\" Shape\" SPC %id SPC %desc);\n"
  73. " if (!%xml.nextSiblingElement(\"rec\")) break;\n"
  74. " }\n"
  75. " %xml.popElement();\n"
  76. " if (!%xml.nextSiblingElement(\"table\")) break;\n"
  77. " }\n"
  78. "}\n\n"
  79. "// Thanks to Scott Peal for this example\n"
  80. "// Using FileObject in conjunction with SimXMLDocument\n"
  81. "// This example uses an XML file with a format of:\n"
  82. "// <Models>\n"
  83. "// <Model category=\"\" name=\"\" path=\"\" />\n"
  84. "// </Models>\n"
  85. "function getModelsInCatagory()\n"
  86. "{\n"
  87. " %file = \"./Catalog.xml\";\n"
  88. " %fo = new FileObject();\n"
  89. " %text = \"\";\n\n"
  90. " if(%fo.openForRead(%file))\n"
  91. " {\n"
  92. " while(!%fo.isEOF())\n"
  93. " {\n"
  94. " %text = %text @ %fo.readLine();\n"
  95. " if (!%fo.isEOF()) %text = %text @ \"\\n\";\n"
  96. " }\n"
  97. " }\n"
  98. " else\n"
  99. " {\n"
  100. " echo(\"Unable to locate the file: \" @ %file);\n"
  101. " }\n\n"
  102. " %fo.delete();\n\n"
  103. " %xml = new SimXMLDocument() {};\n"
  104. " %xml.parse(%text);\n"
  105. " // \"Get\" inside of the root element, \"Models\".\n"
  106. " %xml.pushChildElement(0);\n\n"
  107. " // \"Get\" into the first child element\n"
  108. " if (%xml.pushFirstChildElement(\"Model\"))\n"
  109. " {\n"
  110. " while (true)\n"
  111. " {\n"
  112. " // \n"
  113. " // Here, i read the element's attributes.\n"
  114. " // You might want to save these values in an array or call the %xml.getElementValue()\n"
  115. " // if you have a different XML structure.\n\n"
  116. " %catagory = %xml.attribute(\"catagory\");\n"
  117. " %name = %xml.attribute(\"name\");\n"
  118. " %path = %xml.attribute(\"path\");\n\n"
  119. " // now, read the next \"Model\"\n"
  120. " if (!%xml.nextSiblingElement(\"Model\")) break;\n"
  121. " }\n"
  122. " }\n"
  123. "}\n"
  124. "@endtsexample\n\n"
  125. "@note SimXMLDocument is a wrapper around TinyXml, a standard XML library. If you're familiar "
  126. "with its concepts, you'll find they also apply here.\n\n"
  127. "@see FileObject\n\n"
  128. "@ingroup FileSystem\n"
  129. );
  130. // -----------------------------------------------------------------------------
  131. // Constructor.
  132. // -----------------------------------------------------------------------------
  133. SimXMLDocument::SimXMLDocument():
  134. m_qDocument(0),
  135. m_CurrentAttribute(0)
  136. {
  137. }
  138. // -----------------------------------------------------------------------------
  139. // Destructor.
  140. // -----------------------------------------------------------------------------
  141. SimXMLDocument::~SimXMLDocument()
  142. {
  143. }
  144. // -----------------------------------------------------------------------------
  145. // Included for completeness.
  146. // -----------------------------------------------------------------------------
  147. bool SimXMLDocument::processArguments(S32 argc, ConsoleValue *argv)
  148. {
  149. return argc == 0;
  150. }
  151. // -----------------------------------------------------------------------------
  152. // Script constructor.
  153. // -----------------------------------------------------------------------------
  154. bool SimXMLDocument::onAdd()
  155. {
  156. if(!Parent::onAdd())
  157. {
  158. return false;
  159. }
  160. if(!m_qDocument)
  161. {
  162. m_qDocument = new VfsXMLDocument();
  163. }
  164. return true;
  165. }
  166. // -----------------------------------------------------------------------------
  167. // Script destructor.
  168. // -----------------------------------------------------------------------------
  169. void SimXMLDocument::onRemove()
  170. {
  171. Parent::onRemove();
  172. if(m_qDocument)
  173. {
  174. m_qDocument->Clear();
  175. delete(m_qDocument);
  176. }
  177. }
  178. // -----------------------------------------------------------------------------
  179. // Initialize peristent fields (datablocks).
  180. // -----------------------------------------------------------------------------
  181. void SimXMLDocument::initPersistFields()
  182. {
  183. docsURL;
  184. Parent::initPersistFields();
  185. }
  186. // -----------------------------------------------------------------------------
  187. // Set this to default state at construction.
  188. // -----------------------------------------------------------------------------
  189. void SimXMLDocument::reset(void)
  190. {
  191. m_qDocument->Clear();
  192. m_paNode.clear();
  193. m_CurrentAttribute = 0;
  194. }
  195. DefineEngineMethod( SimXMLDocument, reset, void, (),,
  196. "@brief Set this document to its default state.\n\n"
  197. "Clears all Elements from the documents. Equivalent to using clear()\n\n"
  198. "@see clear()")
  199. {
  200. object->reset();
  201. }
  202. // -----------------------------------------------------------------------------
  203. // Get true if file loads successfully.
  204. // -----------------------------------------------------------------------------
  205. bool SimXMLDocument::loadFile(const char* rFileName)
  206. {
  207. reset();
  208. return m_qDocument->LoadFile(rFileName);
  209. }
  210. DefineEngineMethod( SimXMLDocument, loadFile, bool, ( const char* fileName ),,
  211. "@brief Load in given filename and prepare it for use.\n\n"
  212. "@note Clears the current document's contents.\n\n"
  213. "@param fileName Name and path of XML document\n"
  214. "@return True if the file was loaded successfully.")
  215. {
  216. return object->loadFile( fileName );
  217. }
  218. // -----------------------------------------------------------------------------
  219. // Get true if file saves successfully.
  220. // -----------------------------------------------------------------------------
  221. bool SimXMLDocument::saveFile(const char* rFileName)
  222. {
  223. return m_qDocument->SaveFile( rFileName );
  224. }
  225. // -----------------------------------------------------------------------------
  226. // Get true if file saves successfully to string.
  227. // -----------------------------------------------------------------------------
  228. bool SimXMLDocument::saveToString(String& str)
  229. {
  230. tinyxml2::XMLPrinter printer;
  231. bool ret = m_qDocument->Accept( &printer );
  232. if (ret)
  233. str = printer.CStr();
  234. return ret;
  235. }
  236. DefineEngineMethod( SimXMLDocument, saveFile, bool, ( const char* fileName ),,
  237. "@brief Save document to the given file name.\n\n"
  238. "@param fileName Path and name of XML file to save to.\n"
  239. "@return True if the file was successfully saved.")
  240. {
  241. return object->saveFile( fileName );
  242. }
  243. // -----------------------------------------------------------------------------
  244. // Get true if XML text parses correctly.
  245. // -----------------------------------------------------------------------------
  246. S32 SimXMLDocument::parse(const char* rText)
  247. {
  248. reset();
  249. m_qDocument->Parse( rText );
  250. return 1;
  251. }
  252. DefineEngineMethod( SimXMLDocument, parse, void, ( const char* xmlString ),,
  253. "@brief Create a document from a XML string.\n\n"
  254. "@note Clears the current document's contents.\n\n"
  255. "@param xmlString Valid XML to parse and store as a document.")
  256. {
  257. object->parse( xmlString );
  258. }
  259. // -----------------------------------------------------------------------------
  260. // Clear contents of XML document.
  261. // -----------------------------------------------------------------------------
  262. void SimXMLDocument::clear(void)
  263. {
  264. reset();
  265. }
  266. DefineEngineMethod( SimXMLDocument, clear, void, (),,
  267. "@brief Set this document to its default state.\n\n"
  268. "Clears all Elements from the documents. Equivalent to using reset()\n\n"
  269. "@see reset()")
  270. {
  271. object->clear();
  272. }
  273. // -----------------------------------------------------------------------------
  274. // Get error description of XML document.
  275. // -----------------------------------------------------------------------------
  276. const char* SimXMLDocument::getErrorDesc(void) const
  277. {
  278. if(!m_qDocument)
  279. {
  280. return StringTable->insert("No document");
  281. }
  282. return m_qDocument->ErrorStr();
  283. }
  284. DefineEngineMethod( SimXMLDocument, getErrorDesc, const char*, (),,
  285. "@brief Get last error description.\n\n"
  286. "@return A string of the last error message.")
  287. {
  288. return object->getErrorDesc();
  289. }
  290. // -----------------------------------------------------------------------------
  291. // Clear error description of this.
  292. // -----------------------------------------------------------------------------
  293. void SimXMLDocument::clearError(void)
  294. {
  295. m_qDocument->ClearError();
  296. }
  297. DefineEngineMethod( SimXMLDocument, clearError, void, (),,
  298. "@brief Clear the last error description.\n\n")
  299. {
  300. object->clearError();
  301. }
  302. // -----------------------------------------------------------------------------
  303. // Get true if first child element was successfully pushed onto stack.
  304. // -----------------------------------------------------------------------------
  305. bool SimXMLDocument::pushFirstChildElement(const char* rName)
  306. {
  307. // Clear the current attribute pointer
  308. m_CurrentAttribute = 0;
  309. // Push the first element found under the current element of the given name
  310. tinyxml2::XMLElement* pElement;
  311. if(!m_paNode.empty())
  312. {
  313. const S32 iLastElement = m_paNode.size() - 1;
  314. tinyxml2::XMLNode* pNode = m_paNode[iLastElement];
  315. if(!pNode)
  316. {
  317. return false;
  318. }
  319. pElement = pNode->FirstChildElement(rName);
  320. }
  321. else
  322. {
  323. if(!m_qDocument)
  324. {
  325. return false;
  326. }
  327. pElement = m_qDocument->FirstChildElement(rName);
  328. }
  329. if(!pElement)
  330. {
  331. return false;
  332. }
  333. m_paNode.push_back(pElement);
  334. return true;
  335. }
  336. DefineEngineMethod( SimXMLDocument, pushFirstChildElement, bool, ( const char* name ),,
  337. "@brief Push the first child Element with the given name onto the stack, making it the current Element.\n\n"
  338. "@param name String containing name of the child Element.\n"
  339. "@return True if the Element was found and made the current one.\n"
  340. "@tsexample\n"
  341. "// Using the following test.xml file as an example:\n"
  342. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  343. "// <NewElement>Some text</NewElement>\n\n"
  344. "// Load in the file\n"
  345. "%x = new SimXMLDocument();\n"
  346. "%x.loadFile(\"test.xml\");\n\n"
  347. "// Make the first Element the current one\n"
  348. "%x.pushFirstChildElement(\"NewElement\");\n\n"
  349. "// Store the current Element's text ('Some text' in this example)\n"
  350. "// into 'result'\n"
  351. "%result = %x.getText();\n"
  352. "echo( %result );\n"
  353. "@endtsexample\n\n")
  354. {
  355. return object->pushFirstChildElement( name );
  356. }
  357. // -----------------------------------------------------------------------------
  358. // Get true if first child element was successfully pushed onto stack.
  359. // -----------------------------------------------------------------------------
  360. bool SimXMLDocument::pushChildElement(S32 index)
  361. {
  362. // Clear the current attribute pointer
  363. m_CurrentAttribute = 0;
  364. // Push the first element found under the current element of the given name
  365. tinyxml2::XMLElement* pElement;
  366. if(!m_paNode.empty())
  367. {
  368. const S32 iLastElement = m_paNode.size() - 1;
  369. tinyxml2::XMLNode* pNode = m_paNode[iLastElement];
  370. if(!pNode)
  371. {
  372. return false;
  373. }
  374. pElement = pNode->FirstChildElement();
  375. for( S32 i = 0; i < index; i++ )
  376. {
  377. if( !pElement )
  378. return false;
  379. pElement = pElement->NextSiblingElement();
  380. }
  381. }
  382. else
  383. {
  384. if(!m_qDocument)
  385. {
  386. return false;
  387. }
  388. pElement = m_qDocument->FirstChildElement();
  389. for( S32 i = 0; i < index; i++ )
  390. {
  391. if( !pElement )
  392. return false;
  393. pElement = pElement->NextSiblingElement();
  394. }
  395. }
  396. if(!pElement)
  397. {
  398. return false;
  399. }
  400. m_paNode.push_back(pElement);
  401. return true;
  402. }
  403. DefineEngineMethod( SimXMLDocument, pushChildElement, bool, ( S32 index ),,
  404. "@brief Push the child Element at the given index onto the stack, making it the current one.\n\n"
  405. "@param index Numerical index of Element being pushed."
  406. "@return True if the Element was found and made the current one.\n")
  407. {
  408. return object->pushChildElement( index );
  409. }
  410. // -----------------------------------------------------------------------------
  411. // Convert top stack element into its next sibling element.
  412. // -----------------------------------------------------------------------------
  413. bool SimXMLDocument::nextSiblingElement(const char* rName)
  414. {
  415. // Clear the current attribute pointer
  416. m_CurrentAttribute = 0;
  417. // Attempt to find the next sibling element
  418. if(m_paNode.empty())
  419. {
  420. return false;
  421. }
  422. const S32 iLastElement = m_paNode.size() - 1;
  423. tinyxml2::XMLNode*& pElement = m_paNode[iLastElement];
  424. if(!pElement)
  425. {
  426. return false;
  427. }
  428. pElement = pElement->NextSiblingElement(rName);
  429. if(!pElement)
  430. {
  431. return false;
  432. }
  433. return true;
  434. }
  435. DefineEngineMethod( SimXMLDocument, nextSiblingElement, bool, ( const char* name ),,
  436. "@brief Put the next sibling Element with the given name on the stack, making it the current one.\n\n"
  437. "@param name String containing name of the next sibling."
  438. "@return True if the Element was found and made the current one.\n")
  439. {
  440. return object->nextSiblingElement( name );
  441. }
  442. // -----------------------------------------------------------------------------
  443. // Get element value if it exists. Used to extract a text node from the element.
  444. // for example.
  445. // -----------------------------------------------------------------------------
  446. const char* SimXMLDocument::elementValue()
  447. {
  448. if(m_paNode.empty())
  449. {
  450. return StringTable->EmptyString();
  451. }
  452. const S32 iLastElement = m_paNode.size() - 1;
  453. tinyxml2::XMLNode* pNode = m_paNode[iLastElement];
  454. if(!pNode)
  455. {
  456. return StringTable->EmptyString();
  457. }
  458. return pNode->Value();
  459. }
  460. DefineEngineMethod( SimXMLDocument, elementValue, const char*, (),,
  461. "@brief Get the Element's value if it exists.\n\n"
  462. "Usually returns the text from the Element.\n"
  463. "@return The value from the Element, or an empty string if none is found.")
  464. {
  465. return object->elementValue();
  466. }
  467. // -----------------------------------------------------------------------------
  468. // Pop last element off of stack.
  469. // -----------------------------------------------------------------------------
  470. void SimXMLDocument::popElement(void)
  471. {
  472. m_paNode.pop_back();
  473. }
  474. DefineEngineMethod( SimXMLDocument, popElement, void, (),,
  475. "@brief Pop the last Element off the stack.\n\n")
  476. {
  477. object->popElement();
  478. }
  479. // -----------------------------------------------------------------------------
  480. // Get attribute value if it exists.
  481. // -----------------------------------------------------------------------------
  482. const char* SimXMLDocument::attribute(const char* rAttribute)
  483. {
  484. if(m_paNode.empty())
  485. {
  486. return StringTable->EmptyString();
  487. }
  488. const S32 iLastElement = m_paNode.size() - 1;
  489. tinyxml2::XMLElement* pNode = m_paNode[iLastElement]->ToElement();
  490. if(!pNode)
  491. {
  492. return StringTable->EmptyString();
  493. }
  494. if(!pNode->Attribute(rAttribute))
  495. {
  496. return StringTable->EmptyString();
  497. }
  498. return pNode->Attribute(rAttribute);
  499. }
  500. DefineEngineMethod( SimXMLDocument, attribute, const char*, ( const char* attributeName ),,
  501. "@brief Get a string attribute from the current Element on the stack.\n\n"
  502. "@param attributeName Name of attribute to retrieve.\n"
  503. "@return The attribute string if found. Otherwise returns an empty string.\n")
  504. {
  505. return object->attribute( attributeName );
  506. }
  507. // These two methods don't make a lot of sense the way TS works. Leaving them in for backwards-compatibility.
  508. DefineEngineMethod( SimXMLDocument, attributeF32, F32, (const char * attributeName), , "(string attributeName)"
  509. "@brief Get float attribute from the current Element on the stack.\n\n"
  510. "@param attributeName Name of attribute to retrieve.\n"
  511. "@return The value of the given attribute in the form of a float.\n"
  512. "@deprecated Use attribute().")
  513. {
  514. return dAtof( object->attribute( attributeName ) );
  515. }
  516. DefineEngineMethod(SimXMLDocument, attributeS32, S32, (const char * attributeName), , "(string attributeName)"
  517. "@brief Get int attribute from the current Element on the stack.\n\n"
  518. "@param attributeName Name of attribute to retrieve.\n"
  519. "@return The value of the given attribute in the form of an integer.\n"
  520. "@deprecated Use attribute().")
  521. {
  522. return dAtoi( object->attribute( attributeName ) );
  523. }
  524. // -----------------------------------------------------------------------------
  525. // Get true if given attribute exists.
  526. // -----------------------------------------------------------------------------
  527. bool SimXMLDocument::attributeExists(const char* rAttribute)
  528. {
  529. if(m_paNode.empty())
  530. {
  531. return false;
  532. }
  533. const S32 iLastElement = m_paNode.size() - 1;
  534. tinyxml2::XMLElement* pNode = m_paNode[iLastElement]->ToElement();
  535. if(!pNode)
  536. {
  537. return false;
  538. }
  539. if(!pNode->Attribute(rAttribute))
  540. {
  541. return false;
  542. }
  543. return true;
  544. }
  545. DefineEngineMethod( SimXMLDocument, attributeExists, bool, ( const char* attributeName ),,
  546. "@brief Tests if the requested attribute exists.\n\n"
  547. "@param attributeName Name of attribute being queried for.\n\n"
  548. "@return True if the attribute exists.")
  549. {
  550. return object->attributeExists( attributeName );
  551. }
  552. // -----------------------------------------------------------------------------
  553. // Obtain the name of the current element's first attribute
  554. // -----------------------------------------------------------------------------
  555. const char* SimXMLDocument::firstAttribute()
  556. {
  557. // Get the current element
  558. if(m_paNode.empty())
  559. {
  560. return StringTable->EmptyString();
  561. }
  562. const S32 iLastElement = m_paNode.size() - 1;
  563. tinyxml2::XMLElement* pNode = m_paNode[iLastElement]->ToElement();
  564. if(!pNode)
  565. {
  566. return StringTable->EmptyString();
  567. }
  568. // Gets its first attribute, if any
  569. m_CurrentAttribute = pNode->FirstAttribute();
  570. if(!m_CurrentAttribute)
  571. {
  572. return StringTable->EmptyString();
  573. }
  574. return m_CurrentAttribute->Name();
  575. }
  576. DefineEngineMethod( SimXMLDocument, firstAttribute, const char*, (),,
  577. "@brief Obtain the name of the current Element's first attribute.\n\n"
  578. "@return String containing the first attribute's name, or an empty string if none is found.\n\n"
  579. "@see nextAttribute()\n"
  580. "@see lastAttribute()\n"
  581. "@see prevAttribute()")
  582. {
  583. return object->firstAttribute();
  584. }
  585. // -----------------------------------------------------------------------------
  586. // Obtain the name of the current element's last attribute
  587. // -----------------------------------------------------------------------------
  588. const char* SimXMLDocument::lastAttribute()
  589. {
  590. // Get the current element
  591. if(m_paNode.empty())
  592. {
  593. return StringTable->EmptyString();
  594. }
  595. const S32 iLastElement = m_paNode.size() - 1;
  596. tinyxml2::XMLElement* pNode = m_paNode[iLastElement]->ToElement();
  597. if(!pNode)
  598. {
  599. return StringTable->EmptyString();
  600. }
  601. // Gets its last attribute, if any
  602. m_CurrentAttribute = pNode->FirstAttribute();
  603. while (m_CurrentAttribute->Next() != NULL)
  604. {
  605. m_CurrentAttribute = m_CurrentAttribute->Next();
  606. }
  607. if(!m_CurrentAttribute)
  608. {
  609. return StringTable->EmptyString();
  610. }
  611. return m_CurrentAttribute->Name();
  612. }
  613. DefineEngineMethod( SimXMLDocument, lastAttribute, const char*, (),,
  614. "@brief Obtain the name of the current Element's last attribute.\n\n"
  615. "@return String containing the last attribute's name, or an empty string if none is found.\n\n"
  616. "@see prevAttribute()\n"
  617. "@see firstAttribute()\n"
  618. "@see lastAttribute()\n")
  619. {
  620. return object->lastAttribute();
  621. }
  622. // -----------------------------------------------------------------------------
  623. // Get the name of the next attribute for the current element after a call
  624. // to firstAttribute().
  625. // -----------------------------------------------------------------------------
  626. const char* SimXMLDocument::nextAttribute()
  627. {
  628. if(!m_CurrentAttribute)
  629. {
  630. return StringTable->EmptyString();
  631. }
  632. // Gets its next attribute, if any
  633. m_CurrentAttribute = m_CurrentAttribute->Next();
  634. if(!m_CurrentAttribute)
  635. {
  636. return StringTable->EmptyString();
  637. }
  638. return m_CurrentAttribute->Name();
  639. }
  640. DefineEngineMethod( SimXMLDocument, nextAttribute, const char*, (),,
  641. "@brief Get the name of the next attribute for the current Element after a call to firstAttribute().\n\n"
  642. "@return String containing the next attribute's name, or an empty string if none is found."
  643. "@see firstAttribute()\n"
  644. "@see lastAttribute()\n"
  645. "@see prevAttribute()\n")
  646. {
  647. return object->nextAttribute();
  648. }
  649. // -----------------------------------------------------------------------------
  650. // Get the name of the previous attribute for the current element after a call
  651. // to lastAttribute().
  652. // -----------------------------------------------------------------------------
  653. const char* SimXMLDocument::prevAttribute()
  654. {
  655. // Get the current element
  656. if (m_paNode.empty())
  657. {
  658. return StringTable->EmptyString();
  659. }
  660. const S32 iLastElement = m_paNode.size() - 1;
  661. tinyxml2::XMLElement* pNode = m_paNode[iLastElement]->ToElement();
  662. if (!pNode)
  663. {
  664. return StringTable->EmptyString();
  665. }
  666. if(!m_CurrentAttribute)
  667. {
  668. return StringTable->EmptyString();
  669. }
  670. // Gets its next attribute, if any
  671. while (m_CurrentAttribute != NULL && m_CurrentAttribute->Next() != m_CurrentAttribute)
  672. {
  673. m_CurrentAttribute = m_CurrentAttribute->Next();
  674. }
  675. if(!m_CurrentAttribute)
  676. {
  677. return StringTable->EmptyString();
  678. }
  679. return m_CurrentAttribute->Name();
  680. }
  681. DefineEngineMethod( SimXMLDocument, prevAttribute, const char*, (),,
  682. "@brief Get the name of the previous attribute for the current Element after a call to lastAttribute().\n\n"
  683. "@return String containing the previous attribute's name, or an empty string if none is found."
  684. "@see lastAttribute()\n"
  685. "@see firstAttribute()\n"
  686. "@see nextAttribute()\n")
  687. {
  688. return object->prevAttribute();
  689. }
  690. // -----------------------------------------------------------------------------
  691. // Set attribute of top stack element to given value.
  692. // -----------------------------------------------------------------------------
  693. void SimXMLDocument::setAttribute(const char* rAttribute, const char* rVal)
  694. {
  695. if(m_paNode.empty())
  696. {
  697. return;
  698. }
  699. const S32 iLastElement = m_paNode.size() - 1;
  700. tinyxml2::XMLElement* pElement = m_paNode[iLastElement]->ToElement();
  701. if(!pElement)
  702. {
  703. return;
  704. }
  705. pElement->SetAttribute(rAttribute, rVal);
  706. }
  707. DefineEngineMethod( SimXMLDocument, setAttribute, void, ( const char* attributeName, const char* value ),,
  708. "@brief Set the attribute of the current Element on the stack to the given value.\n\n"
  709. "@param attributeName Name of attribute being changed\n"
  710. "@param value New value to assign to the attribute\n")
  711. {
  712. object->setAttribute( attributeName, value );
  713. }
  714. // -----------------------------------------------------------------------------
  715. // Set attribute of top stack element to given value.
  716. // -----------------------------------------------------------------------------
  717. void SimXMLDocument::setObjectAttributes(const char* objectID)
  718. {
  719. if( !objectID || !objectID[0] )
  720. return;
  721. if(m_paNode.empty())
  722. return;
  723. SimObject *pObject = Sim::findObject( objectID );
  724. if( pObject == NULL )
  725. return;
  726. const S32 iLastElement = m_paNode.size() - 1;
  727. tinyxml2::XMLElement* pElement = m_paNode[iLastElement]->ToElement();
  728. if(!pElement)
  729. return;
  730. char textbuf[1024];
  731. tinyxml2::XMLElement* field = m_qDocument->NewElement("Field");
  732. pElement->SetAttribute( "Name", pObject->getName() );
  733. // Iterate over our filed list and add them to the XML document...
  734. AbstractClassRep::FieldList fieldList = pObject->getFieldList();
  735. AbstractClassRep::FieldList::iterator itr;
  736. for(itr = fieldList.begin(); itr != fieldList.end(); itr++)
  737. {
  738. if( itr->type == AbstractClassRep::DeprecatedFieldType ||
  739. itr->type == AbstractClassRep::StartGroupFieldType ||
  740. itr->type == AbstractClassRep::EndGroupFieldType) continue;
  741. // Not an Array
  742. if(itr->elementCount == 1)
  743. {
  744. // get the value of the field as a string.
  745. ConsoleBaseType *cbt = ConsoleBaseType::getType(itr->type);
  746. const char *val = Con::getData(itr->type, (void *) (((const char *)pObject) + itr->offset), 0, itr->table, itr->flag);
  747. // Make a copy for the field check.
  748. if (!val)
  749. continue;
  750. FrameTemp<char> valCopy( dStrlen( val ) + 1 );
  751. dStrcpy( (char *)valCopy, val, valCopy.size() );
  752. if (!pObject->writeField(itr->pFieldname, valCopy))
  753. continue;
  754. val = valCopy;
  755. expandEscape(textbuf, val);
  756. if( !pObject->writeField( itr->pFieldname, textbuf ) )
  757. continue;
  758. field->SetValue( "Property" );
  759. field->SetAttribute( "name", itr->pFieldname );
  760. if( cbt != NULL )
  761. field->SetAttribute( "type", cbt->getTypeName() );
  762. else
  763. field->SetAttribute( "type", "TypeString" );
  764. field->SetAttribute( "data", textbuf );
  765. pElement->InsertEndChild( field );
  766. continue;
  767. }
  768. }
  769. //// IS An Array
  770. //for(U32 j = 0; S32(j) < f->elementCount; j++)
  771. //{
  772. // // If the start of a group create an element for the group and
  773. // // the our chache to it
  774. // const char *val = Con::getData(itr->type, (void *) (((const char *)pObject) + itr->offset), j, itr->table, itr->flag);
  775. // // Make a copy for the field check.
  776. // if (!val)
  777. // continue;
  778. // FrameTemp<char> valCopy( dStrlen( val ) + 1 );
  779. // dStrcpy( (char *)valCopy, val, valCopy.size() );
  780. // if (!pObject->writeField(itr->pFieldname, valCopy))
  781. // continue;
  782. // val = valCopy;
  783. // // get the value of the field as a string.
  784. // ConsoleBaseType *cbt = ConsoleBaseType::getType(itr->type);
  785. // const char * dstr = Con::getData(itr->type, (void *)(((const char *)pObject) + itr->offset), 0, itr->table, itr->flag);
  786. // if(!dstr)
  787. // dstr = "";
  788. // expandEscape(textbuf, dstr);
  789. // if( !pObject->writeField( itr->pFieldname, dstr ) )
  790. // continue;
  791. // field.SetValue( "Property" );
  792. // field.SetAttribute( "name", itr->pFieldname );
  793. // if( cbt != NULL )
  794. // field.SetAttribute( "type", cbt->getTypeName() );
  795. // else
  796. // field.SetAttribute( "type", "TypeString" );
  797. // field.SetAttribute( "data", textbuf );
  798. // pElement->InsertEndChild( field );
  799. //}
  800. }
  801. DefineEngineMethod( SimXMLDocument, setObjectAttributes, void, ( const char* objectID ),,
  802. "@brief Add the given SimObject's fields as attributes of the current Element on the stack.\n\n"
  803. "@param objectID ID of SimObject being copied.")
  804. {
  805. object->setObjectAttributes( objectID );
  806. }
  807. // -----------------------------------------------------------------------------
  808. // Create a new element and set to child of current stack element.
  809. // New element is placed on top of element stack.
  810. // -----------------------------------------------------------------------------
  811. void SimXMLDocument::pushNewElement(const char* rName)
  812. {
  813. tinyxml2::XMLElement* cElement = m_qDocument->NewElement( rName );
  814. tinyxml2::XMLNode* pStackTop = 0;
  815. if(m_paNode.empty())
  816. {
  817. pStackTop = m_qDocument->InsertEndChild(cElement);
  818. }
  819. else
  820. {
  821. const S32 iFinalElement = m_paNode.size() - 1;
  822. tinyxml2::XMLNode *pNode = m_paNode[iFinalElement];
  823. if(!pNode)
  824. {
  825. return;
  826. }
  827. pStackTop = pNode->InsertEndChild( cElement );
  828. }
  829. if(!pStackTop)
  830. {
  831. return;
  832. }
  833. m_paNode.push_back(pStackTop);
  834. }
  835. DefineEngineMethod( SimXMLDocument, pushNewElement, void, ( const char* name ),,
  836. "@brief Create a new element with the given name as child of current Element "
  837. "and push it onto the Element stack making it the current one.\n\n"
  838. "@note This differs from addNewElement() in that it adds the new Element as a "
  839. "child of the current Element (or a child of the document if no Element exists).\n\n"
  840. "@param name XML tag for the new Element.\n"
  841. "@see addNewElement()")
  842. {
  843. object->pushNewElement( name );
  844. }
  845. // -----------------------------------------------------------------------------
  846. // Create a new element and set to child of current stack element.
  847. // New element is placed on top of element stack.
  848. // -----------------------------------------------------------------------------
  849. void SimXMLDocument::addNewElement(const char* rName)
  850. {
  851. tinyxml2::XMLElement* cElement = m_qDocument->NewElement(rName);
  852. tinyxml2::XMLNode* pStackTop = 0;
  853. if(m_paNode.empty())
  854. {
  855. pStackTop = m_qDocument->InsertEndChild( cElement );
  856. if(!pStackTop)
  857. {
  858. return;
  859. }
  860. m_paNode.push_back(pStackTop);
  861. return;
  862. }
  863. const S32 iParentElement = m_paNode.size() - 2;
  864. if(iParentElement < 0)
  865. {
  866. pStackTop = m_qDocument->InsertEndChild( cElement );
  867. if(!pStackTop)
  868. {
  869. return;
  870. }
  871. m_paNode.push_back(pStackTop);
  872. return;
  873. }
  874. else
  875. {
  876. tinyxml2::XMLNode* pNode = m_paNode[iParentElement];
  877. if(!pNode)
  878. {
  879. return;
  880. }
  881. pStackTop = pNode->InsertEndChild( cElement );
  882. if(!pStackTop)
  883. {
  884. return;
  885. }
  886. // Overwrite top stack position.
  887. const S32 iFinalElement = m_paNode.size() - 1;
  888. m_paNode[iFinalElement] = pStackTop;
  889. //pNode = pStackTop;
  890. }
  891. }
  892. DefineEngineMethod( SimXMLDocument, addNewElement, void, ( const char* name ),,
  893. "@brief Create a new element with the given name as child of current Element's "
  894. "parent and push it onto the Element stack making it the current one.\n\n"
  895. "@note This differs from pushNewElement() in that it adds the new Element to the "
  896. "current Element's parent (or document if there is no parent Element). This makes "
  897. "the new Element a sibling of the current one.\n\n"
  898. "@param name XML tag for the new Element.\n"
  899. "@see pushNewElement()")
  900. {
  901. object->addNewElement( name );
  902. }
  903. // -----------------------------------------------------------------------------
  904. // Write XML document declaration.
  905. // -----------------------------------------------------------------------------
  906. void SimXMLDocument::addHeader(void)
  907. {
  908. tinyxml2::XMLDeclaration* cDeclaration = m_qDocument->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\" standalone =\"yes\"");
  909. m_qDocument->InsertEndChild(cDeclaration);
  910. }
  911. DefineEngineMethod( SimXMLDocument, addHeader, void, (),,
  912. "@brief Add a XML header to a document.\n\n"
  913. "Sometimes called a declaration, you typically add a standard header to "
  914. "the document before adding any elements. SimXMLDocument always produces "
  915. "the following header:\n\n"
  916. "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n\n"
  917. "@tsexample\n"
  918. "// Create a new XML document with just a header and single element.\n"
  919. "%x = new SimXMLDocument();\n"
  920. "%x.addHeader();\n"
  921. "%x.addNewElement(\"NewElement\");\n"
  922. "%x.saveFile(\"test.xml\");\n\n"
  923. "// Produces the following file:\n"
  924. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  925. "// <NewElement />\n"
  926. "@endtsexample\n\n")
  927. {
  928. object->addHeader();
  929. }
  930. void SimXMLDocument::addComment(const char* comment)
  931. {
  932. tinyxml2::XMLComment* cComment = m_qDocument->NewComment(comment);
  933. cComment->SetValue(comment);
  934. m_qDocument->InsertEndChild(cComment);
  935. }
  936. DefineEngineMethod(SimXMLDocument, addComment, void, ( const char* comment ),,
  937. "@brief Add the given comment as a child of the document.\n\n"
  938. "@param comment String containing the comment."
  939. "@tsexample\n"
  940. "// Create a new XML document with a header, a comment and single element.\n"
  941. "%x = new SimXMLDocument();\n"
  942. "%x.addHeader();\n"
  943. "%x.addComment(\"This is a test comment\");\n"
  944. "%x.addNewElement(\"NewElement\");\n"
  945. "%x.saveFile(\"test.xml\");\n\n"
  946. "// Produces the following file:\n"
  947. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  948. "// <!--This is a test comment-->\n"
  949. "// <NewElement />\n"
  950. "@endtsexample\n\n"
  951. "@see readComment()")
  952. {
  953. object->addComment( comment );
  954. }
  955. const char* SimXMLDocument::readComment( S32 index )
  956. {
  957. // Clear the current attribute pointer
  958. m_CurrentAttribute = 0;
  959. // Push the first element found under the current element of the given name
  960. if(!m_paNode.empty())
  961. {
  962. const S32 iLastElement = m_paNode.size() - 1;
  963. tinyxml2::XMLNode* pNode = m_paNode[iLastElement];
  964. if(!pNode)
  965. {
  966. return "";
  967. }
  968. tinyxml2::XMLNode* node = pNode->FirstChild();
  969. for( S32 i = 0; i < index; i++ )
  970. {
  971. if( !node )
  972. return "";
  973. node = node->NextSiblingElement();
  974. }
  975. if( node )
  976. {
  977. tinyxml2::XMLComment* comment = node->ToComment();
  978. if( comment )
  979. return comment->Value();
  980. }
  981. }
  982. else
  983. {
  984. if(!m_qDocument)
  985. {
  986. return "";
  987. }
  988. tinyxml2::XMLNode* node = m_qDocument->FirstChild();
  989. for( S32 i = 0; i < index; i++ )
  990. {
  991. if( !node )
  992. return "";
  993. node = node->NextSibling();
  994. }
  995. if( node )
  996. {
  997. tinyxml2::XMLComment* comment = node->ToComment();
  998. if( comment )
  999. return comment->Value();
  1000. }
  1001. }
  1002. return "";
  1003. }
  1004. DefineEngineMethod( SimXMLDocument, readComment, const char*, ( S32 index ),,
  1005. "Gives the comment at the specified index, if any.\n\n"
  1006. "Unlike addComment() that only works at the document level, readComment() may read "
  1007. "comments from the document or any child Element. The current Element (or document "
  1008. "if no Elements have been pushed to the stack) is the parent for any comments, and the "
  1009. "provided index is the number of comments in to read back."
  1010. "@param index Comment index number to query from the current Element stack\n\n"
  1011. "@return String containing the comment, or an empty string if no comment is found.\n\n"
  1012. "@see addComment()")
  1013. {
  1014. return object->readComment( index );
  1015. }
  1016. void SimXMLDocument::addText(const char* text)
  1017. {
  1018. if(m_paNode.empty())
  1019. return;
  1020. const S32 iFinalElement = m_paNode.size() - 1;
  1021. tinyxml2::XMLNode* pNode = m_paNode[iFinalElement];
  1022. if(!pNode)
  1023. return;
  1024. tinyxml2::XMLText* cText = m_qDocument->NewText(text);
  1025. pNode->InsertEndChild( cText );
  1026. }
  1027. DefineEngineMethod( SimXMLDocument, addText, void, ( const char* text ),,
  1028. "@brief Add the given text as a child of current Element.\n\n"
  1029. "Use getText() to retrieve any text from the current Element and removeText() "
  1030. "to clear any text.\n\n"
  1031. "addText() and addData() may be used interchangeably."
  1032. "@param text String containing the text.\n\n"
  1033. "@tsexample\n"
  1034. "// Create a new XML document with a header and single element\n"
  1035. "// with some added text.\n"
  1036. "%x = new SimXMLDocument();\n"
  1037. "%x.addHeader();\n"
  1038. "%x.addNewElement(\"NewElement\");\n"
  1039. "%x.addText(\"Some text\");\n"
  1040. "%x.saveFile(\"test.xml\");\n\n"
  1041. "// Produces the following file:\n"
  1042. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  1043. "// <NewElement>Some text</NewElement>\n"
  1044. "@endtsexample\n\n"
  1045. "@see getText()\n"
  1046. "@see removeText()\n"
  1047. "@see addData()\n"
  1048. "@see getData()")
  1049. {
  1050. object->addText( text );
  1051. }
  1052. const char* SimXMLDocument::getText()
  1053. {
  1054. if(m_paNode.empty())
  1055. return "";
  1056. const S32 iFinalElement = m_paNode.size() - 1;
  1057. tinyxml2::XMLNode* pNode = m_paNode[iFinalElement];
  1058. if(!pNode)
  1059. return "";
  1060. if(!pNode->FirstChild())
  1061. return "";
  1062. tinyxml2::XMLText* text = pNode->FirstChild()->ToText();
  1063. if( !text )
  1064. return "";
  1065. return text->Value();
  1066. }
  1067. DefineEngineMethod( SimXMLDocument, getText, const char*, (),,
  1068. "@brief Gets the text from the current Element.\n\n"
  1069. "Use addText() to add text to the current Element and removeText() "
  1070. "to clear any text.\n\n"
  1071. "getText() and getData() may be used interchangeably."
  1072. "@return String containing the text in the current Element."
  1073. "@tsexample\n"
  1074. "// Using the following test.xml file as an example:\n"
  1075. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  1076. "// <NewElement>Some text</NewElement>\n\n"
  1077. "// Load in the file\n"
  1078. "%x = new SimXMLDocument();\n"
  1079. "%x.loadFile(\"test.xml\");\n\n"
  1080. "// Make the first Element the current one\n"
  1081. "%x.pushFirstChildElement(\"NewElement\");\n\n"
  1082. "// Store the current Element's text ('Some text' in this example)\n"
  1083. "// into 'result'\n"
  1084. "%result = %x.getText();\n"
  1085. "echo( %result );\n"
  1086. "@endtsexample\n\n"
  1087. "@see addText()\n"
  1088. "@see removeText()\n"
  1089. "@see addData()\n"
  1090. "@see getData()\n")
  1091. {
  1092. const char* text = object->getText();
  1093. if( !text )
  1094. return "";
  1095. return text;
  1096. }
  1097. void SimXMLDocument::removeText()
  1098. {
  1099. if(m_paNode.empty())
  1100. return;
  1101. const S32 iFinalElement = m_paNode.size() - 1;
  1102. tinyxml2::XMLNode* pNode = m_paNode[iFinalElement];
  1103. if(!pNode)
  1104. return;
  1105. if( !pNode->FirstChild() )
  1106. return;
  1107. tinyxml2::XMLText* text = pNode->FirstChild()->ToText();
  1108. if( !text )
  1109. return;
  1110. pNode->DeleteChild(text);
  1111. }
  1112. DefineEngineMethod( SimXMLDocument, removeText, void, (),,
  1113. "@brief Remove any text on the current Element.\n\n"
  1114. "Use getText() to retrieve any text from the current Element and addText() "
  1115. "to add text to the current Element. As getData() and addData() are equivalent "
  1116. "to getText() and addText(), removeText() will also remove any data from the "
  1117. "current Element.\n\n"
  1118. "@see addText()\n"
  1119. "@see getText()\n"
  1120. "@see addData()\n"
  1121. "@see getData()\n")
  1122. {
  1123. object->removeText();
  1124. }
  1125. void SimXMLDocument::addData(const char* text)
  1126. {
  1127. if(m_paNode.empty())
  1128. return;
  1129. const S32 iFinalElement = m_paNode.size() - 1;
  1130. tinyxml2::XMLNode* pNode = m_paNode[iFinalElement];
  1131. if(!pNode)
  1132. return;
  1133. tinyxml2::XMLText* cText = m_qDocument->NewText(text);
  1134. pNode->InsertEndChild( cText );
  1135. }
  1136. DefineEngineMethod( SimXMLDocument, addData, void, ( const char* text ),,
  1137. "@brief Add the given text as a child of current Element.\n\n"
  1138. "Use getData() to retrieve any text from the current Element.\n\n"
  1139. "addData() and addText() may be used interchangeably. As there is no "
  1140. "difference between data and text, you may also use removeText() to clear "
  1141. "any data from the current Element.\n\n"
  1142. "@param text String containing the text.\n\n"
  1143. "@tsexample\n"
  1144. "// Create a new XML document with a header and single element\n"
  1145. "// with some added data.\n"
  1146. "%x = new SimXMLDocument();\n"
  1147. "%x.addHeader();\n"
  1148. "%x.addNewElement(\"NewElement\");\n"
  1149. "%x.addData(\"Some text\");\n"
  1150. "%x.saveFile(\"test.xml\");\n\n"
  1151. "// Produces the following file:\n"
  1152. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  1153. "// <NewElement>Some text</NewElement>\n"
  1154. "@endtsexample\n\n"
  1155. "@see getData()"
  1156. "@see addText()\n"
  1157. "@see getText()\n"
  1158. "@see removeText()\n")
  1159. {
  1160. object->addData( text );
  1161. }
  1162. const char* SimXMLDocument::getData()
  1163. {
  1164. if(m_paNode.empty())
  1165. return "";
  1166. const S32 iFinalElement = m_paNode.size() - 1;
  1167. tinyxml2::XMLNode* pNode = m_paNode[iFinalElement];
  1168. if(!pNode)
  1169. return "";
  1170. if( !pNode->FirstChild() )
  1171. return "";
  1172. tinyxml2::XMLText* text = pNode->FirstChild()->ToText();
  1173. if( !text )
  1174. return "";
  1175. return text->Value();
  1176. }
  1177. DefineEngineMethod( SimXMLDocument, getData, const char*, (),,
  1178. "@brief Gets the text from the current Element.\n\n"
  1179. "Use addData() to add text to the current Element.\n\n"
  1180. "getData() and getText() may be used interchangeably. As there is no "
  1181. "difference between data and text, you may also use removeText() to clear "
  1182. "any data from the current Element.\n\n"
  1183. "@return String containing the text in the current Element."
  1184. "@tsexample\n"
  1185. "// Using the following test.xml file as an example:\n"
  1186. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  1187. "// <NewElement>Some data</NewElement>\n\n"
  1188. "// Load in the file\n"
  1189. "%x = new SimXMLDocument();\n"
  1190. "%x.loadFile(\"test.xml\");\n\n"
  1191. "// Make the first Element the current one\n"
  1192. "%x.pushFirstChildElement(\"NewElement\");\n\n"
  1193. "// Store the current Element's data ('Some data' in this example)\n"
  1194. "// into 'result'\n"
  1195. "%result = %x.getData();\n"
  1196. "echo( %result );\n"
  1197. "@endtsexample\n\n"
  1198. "@see addData()\n"
  1199. "@see addText()\n"
  1200. "@see getText()\n"
  1201. "@see removeText()\n")
  1202. {
  1203. const char* text = object->getData();
  1204. if( !text )
  1205. return "";
  1206. return text;
  1207. }
  1208. ////EOF/////////////////////////////////////////////////////////////////////////