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. Parent::initPersistFields();
  184. }
  185. // -----------------------------------------------------------------------------
  186. // Set this to default state at construction.
  187. // -----------------------------------------------------------------------------
  188. void SimXMLDocument::reset(void)
  189. {
  190. m_qDocument->Clear();
  191. m_paNode.clear();
  192. m_CurrentAttribute = 0;
  193. }
  194. DefineEngineMethod( SimXMLDocument, reset, void, (),,
  195. "@brief Set this document to its default state.\n\n"
  196. "Clears all Elements from the documents. Equivalent to using clear()\n\n"
  197. "@see clear()")
  198. {
  199. object->reset();
  200. }
  201. // -----------------------------------------------------------------------------
  202. // Get true if file loads successfully.
  203. // -----------------------------------------------------------------------------
  204. bool SimXMLDocument::loadFile(const char* rFileName)
  205. {
  206. reset();
  207. return m_qDocument->LoadFile(rFileName);
  208. }
  209. DefineEngineMethod( SimXMLDocument, loadFile, bool, ( const char* fileName ),,
  210. "@brief Load in given filename and prepare it for use.\n\n"
  211. "@note Clears the current document's contents.\n\n"
  212. "@param fileName Name and path of XML document\n"
  213. "@return True if the file was loaded successfully.")
  214. {
  215. return object->loadFile( fileName );
  216. }
  217. // -----------------------------------------------------------------------------
  218. // Get true if file saves successfully.
  219. // -----------------------------------------------------------------------------
  220. bool SimXMLDocument::saveFile(const char* rFileName)
  221. {
  222. return m_qDocument->SaveFile( rFileName );
  223. }
  224. // -----------------------------------------------------------------------------
  225. // Get true if file saves successfully to string.
  226. // -----------------------------------------------------------------------------
  227. bool SimXMLDocument::saveToString(String& str)
  228. {
  229. tinyxml2::XMLPrinter printer;
  230. bool ret = m_qDocument->Accept( &printer );
  231. if (ret)
  232. str = printer.CStr();
  233. return ret;
  234. }
  235. DefineEngineMethod( SimXMLDocument, saveFile, bool, ( const char* fileName ),,
  236. "@brief Save document to the given file name.\n\n"
  237. "@param fileName Path and name of XML file to save to.\n"
  238. "@return True if the file was successfully saved.")
  239. {
  240. return object->saveFile( fileName );
  241. }
  242. // -----------------------------------------------------------------------------
  243. // Get true if XML text parses correctly.
  244. // -----------------------------------------------------------------------------
  245. S32 SimXMLDocument::parse(const char* rText)
  246. {
  247. reset();
  248. m_qDocument->Parse( rText );
  249. return 1;
  250. }
  251. DefineEngineMethod( SimXMLDocument, parse, void, ( const char* xmlString ),,
  252. "@brief Create a document from a XML string.\n\n"
  253. "@note Clears the current document's contents.\n\n"
  254. "@param xmlString Valid XML to parse and store as a document.")
  255. {
  256. object->parse( xmlString );
  257. }
  258. // -----------------------------------------------------------------------------
  259. // Clear contents of XML document.
  260. // -----------------------------------------------------------------------------
  261. void SimXMLDocument::clear(void)
  262. {
  263. reset();
  264. }
  265. DefineEngineMethod( SimXMLDocument, clear, void, (),,
  266. "@brief Set this document to its default state.\n\n"
  267. "Clears all Elements from the documents. Equivalent to using reset()\n\n"
  268. "@see reset()")
  269. {
  270. object->clear();
  271. }
  272. // -----------------------------------------------------------------------------
  273. // Get error description of XML document.
  274. // -----------------------------------------------------------------------------
  275. const char* SimXMLDocument::getErrorDesc(void) const
  276. {
  277. if(!m_qDocument)
  278. {
  279. return StringTable->insert("No document");
  280. }
  281. return m_qDocument->ErrorStr();
  282. }
  283. DefineEngineMethod( SimXMLDocument, getErrorDesc, const char*, (),,
  284. "@brief Get last error description.\n\n"
  285. "@return A string of the last error message.")
  286. {
  287. return object->getErrorDesc();
  288. }
  289. // -----------------------------------------------------------------------------
  290. // Clear error description of this.
  291. // -----------------------------------------------------------------------------
  292. void SimXMLDocument::clearError(void)
  293. {
  294. m_qDocument->ClearError();
  295. }
  296. DefineEngineMethod( SimXMLDocument, clearError, void, (),,
  297. "@brief Clear the last error description.\n\n")
  298. {
  299. object->clearError();
  300. }
  301. // -----------------------------------------------------------------------------
  302. // Get true if first child element was successfully pushed onto stack.
  303. // -----------------------------------------------------------------------------
  304. bool SimXMLDocument::pushFirstChildElement(const char* rName)
  305. {
  306. // Clear the current attribute pointer
  307. m_CurrentAttribute = 0;
  308. // Push the first element found under the current element of the given name
  309. tinyxml2::XMLElement* pElement;
  310. if(!m_paNode.empty())
  311. {
  312. const S32 iLastElement = m_paNode.size() - 1;
  313. tinyxml2::XMLNode* pNode = m_paNode[iLastElement];
  314. if(!pNode)
  315. {
  316. return false;
  317. }
  318. pElement = pNode->FirstChildElement(rName);
  319. }
  320. else
  321. {
  322. if(!m_qDocument)
  323. {
  324. return false;
  325. }
  326. pElement = m_qDocument->FirstChildElement(rName);
  327. }
  328. if(!pElement)
  329. {
  330. return false;
  331. }
  332. m_paNode.push_back(pElement);
  333. return true;
  334. }
  335. DefineEngineMethod( SimXMLDocument, pushFirstChildElement, bool, ( const char* name ),,
  336. "@brief Push the first child Element with the given name onto the stack, making it the current Element.\n\n"
  337. "@param name String containing name of the child Element.\n"
  338. "@return True if the Element was found and made the current one.\n"
  339. "@tsexample\n"
  340. "// Using the following test.xml file as an example:\n"
  341. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  342. "// <NewElement>Some text</NewElement>\n\n"
  343. "// Load in the file\n"
  344. "%x = new SimXMLDocument();\n"
  345. "%x.loadFile(\"test.xml\");\n\n"
  346. "// Make the first Element the current one\n"
  347. "%x.pushFirstChildElement(\"NewElement\");\n\n"
  348. "// Store the current Element's text ('Some text' in this example)\n"
  349. "// into 'result'\n"
  350. "%result = %x.getText();\n"
  351. "echo( %result );\n"
  352. "@endtsexample\n\n")
  353. {
  354. return object->pushFirstChildElement( name );
  355. }
  356. // -----------------------------------------------------------------------------
  357. // Get true if first child element was successfully pushed onto stack.
  358. // -----------------------------------------------------------------------------
  359. bool SimXMLDocument::pushChildElement(S32 index)
  360. {
  361. // Clear the current attribute pointer
  362. m_CurrentAttribute = 0;
  363. // Push the first element found under the current element of the given name
  364. tinyxml2::XMLElement* pElement;
  365. if(!m_paNode.empty())
  366. {
  367. const S32 iLastElement = m_paNode.size() - 1;
  368. tinyxml2::XMLNode* pNode = m_paNode[iLastElement];
  369. if(!pNode)
  370. {
  371. return false;
  372. }
  373. pElement = pNode->FirstChildElement();
  374. for( S32 i = 0; i < index; i++ )
  375. {
  376. if( !pElement )
  377. return false;
  378. pElement = pElement->NextSiblingElement();
  379. }
  380. }
  381. else
  382. {
  383. if(!m_qDocument)
  384. {
  385. return false;
  386. }
  387. pElement = m_qDocument->FirstChildElement();
  388. for( S32 i = 0; i < index; i++ )
  389. {
  390. if( !pElement )
  391. return false;
  392. pElement = pElement->NextSiblingElement();
  393. }
  394. }
  395. if(!pElement)
  396. {
  397. return false;
  398. }
  399. m_paNode.push_back(pElement);
  400. return true;
  401. }
  402. DefineEngineMethod( SimXMLDocument, pushChildElement, bool, ( S32 index ),,
  403. "@brief Push the child Element at the given index onto the stack, making it the current one.\n\n"
  404. "@param index Numerical index of Element being pushed."
  405. "@return True if the Element was found and made the current one.\n")
  406. {
  407. return object->pushChildElement( index );
  408. }
  409. // -----------------------------------------------------------------------------
  410. // Convert top stack element into its next sibling element.
  411. // -----------------------------------------------------------------------------
  412. bool SimXMLDocument::nextSiblingElement(const char* rName)
  413. {
  414. // Clear the current attribute pointer
  415. m_CurrentAttribute = 0;
  416. // Attempt to find the next sibling element
  417. if(m_paNode.empty())
  418. {
  419. return false;
  420. }
  421. const S32 iLastElement = m_paNode.size() - 1;
  422. tinyxml2::XMLNode*& pElement = m_paNode[iLastElement];
  423. if(!pElement)
  424. {
  425. return false;
  426. }
  427. pElement = pElement->NextSiblingElement(rName);
  428. if(!pElement)
  429. {
  430. return false;
  431. }
  432. return true;
  433. }
  434. DefineEngineMethod( SimXMLDocument, nextSiblingElement, bool, ( const char* name ),,
  435. "@brief Put the next sibling Element with the given name on the stack, making it the current one.\n\n"
  436. "@param name String containing name of the next sibling."
  437. "@return True if the Element was found and made the current one.\n")
  438. {
  439. return object->nextSiblingElement( name );
  440. }
  441. // -----------------------------------------------------------------------------
  442. // Get element value if it exists. Used to extract a text node from the element.
  443. // for example.
  444. // -----------------------------------------------------------------------------
  445. const char* SimXMLDocument::elementValue()
  446. {
  447. if(m_paNode.empty())
  448. {
  449. return StringTable->EmptyString();
  450. }
  451. const S32 iLastElement = m_paNode.size() - 1;
  452. tinyxml2::XMLNode* pNode = m_paNode[iLastElement];
  453. if(!pNode)
  454. {
  455. return StringTable->EmptyString();
  456. }
  457. return pNode->Value();
  458. }
  459. DefineEngineMethod( SimXMLDocument, elementValue, const char*, (),,
  460. "@brief Get the Element's value if it exists.\n\n"
  461. "Usually returns the text from the Element.\n"
  462. "@return The value from the Element, or an empty string if none is found.")
  463. {
  464. return object->elementValue();
  465. }
  466. // -----------------------------------------------------------------------------
  467. // Pop last element off of stack.
  468. // -----------------------------------------------------------------------------
  469. void SimXMLDocument::popElement(void)
  470. {
  471. m_paNode.pop_back();
  472. }
  473. DefineEngineMethod( SimXMLDocument, popElement, void, (),,
  474. "@brief Pop the last Element off the stack.\n\n")
  475. {
  476. object->popElement();
  477. }
  478. // -----------------------------------------------------------------------------
  479. // Get attribute value if it exists.
  480. // -----------------------------------------------------------------------------
  481. const char* SimXMLDocument::attribute(const char* rAttribute)
  482. {
  483. if(m_paNode.empty())
  484. {
  485. return StringTable->EmptyString();
  486. }
  487. const S32 iLastElement = m_paNode.size() - 1;
  488. tinyxml2::XMLElement* pNode = m_paNode[iLastElement]->ToElement();
  489. if(!pNode)
  490. {
  491. return StringTable->EmptyString();
  492. }
  493. if(!pNode->Attribute(rAttribute))
  494. {
  495. return StringTable->EmptyString();
  496. }
  497. return pNode->Attribute(rAttribute);
  498. }
  499. DefineEngineMethod( SimXMLDocument, attribute, const char*, ( const char* attributeName ),,
  500. "@brief Get a string attribute from the current Element on the stack.\n\n"
  501. "@param attributeName Name of attribute to retrieve.\n"
  502. "@return The attribute string if found. Otherwise returns an empty string.\n")
  503. {
  504. return object->attribute( attributeName );
  505. }
  506. // These two methods don't make a lot of sense the way TS works. Leaving them in for backwards-compatibility.
  507. DefineEngineMethod( SimXMLDocument, attributeF32, F32, (const char * attributeName), , "(string attributeName)"
  508. "@brief Get float attribute from the current Element on the stack.\n\n"
  509. "@param attributeName Name of attribute to retrieve.\n"
  510. "@return The value of the given attribute in the form of a float.\n"
  511. "@deprecated Use attribute().")
  512. {
  513. return dAtof( object->attribute( attributeName ) );
  514. }
  515. DefineEngineMethod(SimXMLDocument, attributeS32, S32, (const char * attributeName), , "(string attributeName)"
  516. "@brief Get int attribute from the current Element on the stack.\n\n"
  517. "@param attributeName Name of attribute to retrieve.\n"
  518. "@return The value of the given attribute in the form of an integer.\n"
  519. "@deprecated Use attribute().")
  520. {
  521. return dAtoi( object->attribute( attributeName ) );
  522. }
  523. // -----------------------------------------------------------------------------
  524. // Get true if given attribute exists.
  525. // -----------------------------------------------------------------------------
  526. bool SimXMLDocument::attributeExists(const char* rAttribute)
  527. {
  528. if(m_paNode.empty())
  529. {
  530. return false;
  531. }
  532. const S32 iLastElement = m_paNode.size() - 1;
  533. tinyxml2::XMLElement* pNode = m_paNode[iLastElement]->ToElement();
  534. if(!pNode)
  535. {
  536. return false;
  537. }
  538. if(!pNode->Attribute(rAttribute))
  539. {
  540. return false;
  541. }
  542. return true;
  543. }
  544. DefineEngineMethod( SimXMLDocument, attributeExists, bool, ( const char* attributeName ),,
  545. "@brief Tests if the requested attribute exists.\n\n"
  546. "@param attributeName Name of attribute being queried for.\n\n"
  547. "@return True if the attribute exists.")
  548. {
  549. return object->attributeExists( attributeName );
  550. }
  551. // -----------------------------------------------------------------------------
  552. // Obtain the name of the current element's first attribute
  553. // -----------------------------------------------------------------------------
  554. const char* SimXMLDocument::firstAttribute()
  555. {
  556. // Get the current element
  557. if(m_paNode.empty())
  558. {
  559. return StringTable->EmptyString();
  560. }
  561. const S32 iLastElement = m_paNode.size() - 1;
  562. tinyxml2::XMLElement* pNode = m_paNode[iLastElement]->ToElement();
  563. if(!pNode)
  564. {
  565. return StringTable->EmptyString();
  566. }
  567. // Gets its first attribute, if any
  568. m_CurrentAttribute = pNode->FirstAttribute();
  569. if(!m_CurrentAttribute)
  570. {
  571. return StringTable->EmptyString();
  572. }
  573. return m_CurrentAttribute->Name();
  574. }
  575. DefineEngineMethod( SimXMLDocument, firstAttribute, const char*, (),,
  576. "@brief Obtain the name of the current Element's first attribute.\n\n"
  577. "@return String containing the first attribute's name, or an empty string if none is found.\n\n"
  578. "@see nextAttribute()\n"
  579. "@see lastAttribute()\n"
  580. "@see prevAttribute()")
  581. {
  582. return object->firstAttribute();
  583. }
  584. // -----------------------------------------------------------------------------
  585. // Obtain the name of the current element's last attribute
  586. // -----------------------------------------------------------------------------
  587. const char* SimXMLDocument::lastAttribute()
  588. {
  589. // Get the current element
  590. if(m_paNode.empty())
  591. {
  592. return StringTable->EmptyString();
  593. }
  594. const S32 iLastElement = m_paNode.size() - 1;
  595. tinyxml2::XMLElement* pNode = m_paNode[iLastElement]->ToElement();
  596. if(!pNode)
  597. {
  598. return StringTable->EmptyString();
  599. }
  600. // Gets its last attribute, if any
  601. m_CurrentAttribute = pNode->FirstAttribute();
  602. while (m_CurrentAttribute->Next() != NULL)
  603. {
  604. m_CurrentAttribute = m_CurrentAttribute->Next();
  605. }
  606. if(!m_CurrentAttribute)
  607. {
  608. return StringTable->EmptyString();
  609. }
  610. return m_CurrentAttribute->Name();
  611. }
  612. DefineEngineMethod( SimXMLDocument, lastAttribute, const char*, (),,
  613. "@brief Obtain the name of the current Element's last attribute.\n\n"
  614. "@return String containing the last attribute's name, or an empty string if none is found.\n\n"
  615. "@see prevAttribute()\n"
  616. "@see firstAttribute()\n"
  617. "@see lastAttribute()\n")
  618. {
  619. return object->lastAttribute();
  620. }
  621. // -----------------------------------------------------------------------------
  622. // Get the name of the next attribute for the current element after a call
  623. // to firstAttribute().
  624. // -----------------------------------------------------------------------------
  625. const char* SimXMLDocument::nextAttribute()
  626. {
  627. if(!m_CurrentAttribute)
  628. {
  629. return StringTable->EmptyString();
  630. }
  631. // Gets its next attribute, if any
  632. m_CurrentAttribute = m_CurrentAttribute->Next();
  633. if(!m_CurrentAttribute)
  634. {
  635. return StringTable->EmptyString();
  636. }
  637. return m_CurrentAttribute->Name();
  638. }
  639. DefineEngineMethod( SimXMLDocument, nextAttribute, const char*, (),,
  640. "@brief Get the name of the next attribute for the current Element after a call to firstAttribute().\n\n"
  641. "@return String containing the next attribute's name, or an empty string if none is found."
  642. "@see firstAttribute()\n"
  643. "@see lastAttribute()\n"
  644. "@see prevAttribute()\n")
  645. {
  646. return object->nextAttribute();
  647. }
  648. // -----------------------------------------------------------------------------
  649. // Get the name of the previous attribute for the current element after a call
  650. // to lastAttribute().
  651. // -----------------------------------------------------------------------------
  652. const char* SimXMLDocument::prevAttribute()
  653. {
  654. // Get the current element
  655. if (m_paNode.empty())
  656. {
  657. return StringTable->EmptyString();
  658. }
  659. const S32 iLastElement = m_paNode.size() - 1;
  660. tinyxml2::XMLElement* pNode = m_paNode[iLastElement]->ToElement();
  661. if (!pNode)
  662. {
  663. return StringTable->EmptyString();
  664. }
  665. if(!m_CurrentAttribute)
  666. {
  667. return StringTable->EmptyString();
  668. }
  669. // Gets its next attribute, if any
  670. while (m_CurrentAttribute != NULL && m_CurrentAttribute->Next() != m_CurrentAttribute)
  671. {
  672. m_CurrentAttribute = m_CurrentAttribute->Next();
  673. }
  674. if(!m_CurrentAttribute)
  675. {
  676. return StringTable->EmptyString();
  677. }
  678. return m_CurrentAttribute->Name();
  679. }
  680. DefineEngineMethod( SimXMLDocument, prevAttribute, const char*, (),,
  681. "@brief Get the name of the previous attribute for the current Element after a call to lastAttribute().\n\n"
  682. "@return String containing the previous attribute's name, or an empty string if none is found."
  683. "@see lastAttribute()\n"
  684. "@see firstAttribute()\n"
  685. "@see nextAttribute()\n")
  686. {
  687. return object->prevAttribute();
  688. }
  689. // -----------------------------------------------------------------------------
  690. // Set attribute of top stack element to given value.
  691. // -----------------------------------------------------------------------------
  692. void SimXMLDocument::setAttribute(const char* rAttribute, const char* rVal)
  693. {
  694. if(m_paNode.empty())
  695. {
  696. return;
  697. }
  698. const S32 iLastElement = m_paNode.size() - 1;
  699. tinyxml2::XMLElement* pElement = m_paNode[iLastElement]->ToElement();
  700. if(!pElement)
  701. {
  702. return;
  703. }
  704. pElement->SetAttribute(rAttribute, rVal);
  705. }
  706. DefineEngineMethod( SimXMLDocument, setAttribute, void, ( const char* attributeName, const char* value ),,
  707. "@brief Set the attribute of the current Element on the stack to the given value.\n\n"
  708. "@param attributeName Name of attribute being changed\n"
  709. "@param value New value to assign to the attribute\n")
  710. {
  711. object->setAttribute( attributeName, value );
  712. }
  713. // -----------------------------------------------------------------------------
  714. // Set attribute of top stack element to given value.
  715. // -----------------------------------------------------------------------------
  716. void SimXMLDocument::setObjectAttributes(const char* objectID)
  717. {
  718. if( !objectID || !objectID[0] )
  719. return;
  720. if(m_paNode.empty())
  721. return;
  722. SimObject *pObject = Sim::findObject( objectID );
  723. if( pObject == NULL )
  724. return;
  725. const S32 iLastElement = m_paNode.size() - 1;
  726. tinyxml2::XMLElement* pElement = m_paNode[iLastElement]->ToElement();
  727. if(!pElement)
  728. return;
  729. char textbuf[1024];
  730. tinyxml2::XMLElement* field = m_qDocument->NewElement("Field");
  731. tinyxml2::XMLElement* group = m_qDocument->NewElement("FieldGroup");
  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/////////////////////////////////////////////////////////////////////////