SimXMLDocument.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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. pElement->SetAttribute( "Name", pObject->getName() );
  732. // Iterate over our filed list and add them to the XML document...
  733. AbstractClassRep::FieldList fieldList = pObject->getFieldList();
  734. AbstractClassRep::FieldList::iterator itr;
  735. for(itr = fieldList.begin(); itr != fieldList.end(); itr++)
  736. {
  737. if( itr->type == AbstractClassRep::DeprecatedFieldType ||
  738. itr->type == AbstractClassRep::StartGroupFieldType ||
  739. itr->type == AbstractClassRep::EndGroupFieldType) continue;
  740. // Not an Array
  741. if(itr->elementCount == 1)
  742. {
  743. // get the value of the field as a string.
  744. ConsoleBaseType *cbt = ConsoleBaseType::getType(itr->type);
  745. const char *val = Con::getData(itr->type, (void *) (((const char *)pObject) + itr->offset), 0, itr->table, itr->flag);
  746. // Make a copy for the field check.
  747. if (!val)
  748. continue;
  749. FrameTemp<char> valCopy( dStrlen( val ) + 1 );
  750. dStrcpy( (char *)valCopy, val, valCopy.size() );
  751. if (!pObject->writeField(itr->pFieldname, valCopy))
  752. continue;
  753. val = valCopy;
  754. expandEscape(textbuf, val);
  755. if( !pObject->writeField( itr->pFieldname, textbuf ) )
  756. continue;
  757. field->SetValue( "Property" );
  758. field->SetAttribute( "name", itr->pFieldname );
  759. if( cbt != NULL )
  760. field->SetAttribute( "type", cbt->getTypeName() );
  761. else
  762. field->SetAttribute( "type", "TypeString" );
  763. field->SetAttribute( "data", textbuf );
  764. pElement->InsertEndChild( field );
  765. continue;
  766. }
  767. }
  768. //// IS An Array
  769. //for(U32 j = 0; S32(j) < f->elementCount; j++)
  770. //{
  771. // // If the start of a group create an element for the group and
  772. // // the our chache to it
  773. // const char *val = Con::getData(itr->type, (void *) (((const char *)pObject) + itr->offset), j, itr->table, itr->flag);
  774. // // Make a copy for the field check.
  775. // if (!val)
  776. // continue;
  777. // FrameTemp<char> valCopy( dStrlen( val ) + 1 );
  778. // dStrcpy( (char *)valCopy, val, valCopy.size() );
  779. // if (!pObject->writeField(itr->pFieldname, valCopy))
  780. // continue;
  781. // val = valCopy;
  782. // // get the value of the field as a string.
  783. // ConsoleBaseType *cbt = ConsoleBaseType::getType(itr->type);
  784. // const char * dstr = Con::getData(itr->type, (void *)(((const char *)pObject) + itr->offset), 0, itr->table, itr->flag);
  785. // if(!dstr)
  786. // dstr = "";
  787. // expandEscape(textbuf, dstr);
  788. // if( !pObject->writeField( itr->pFieldname, dstr ) )
  789. // continue;
  790. // field.SetValue( "Property" );
  791. // field.SetAttribute( "name", itr->pFieldname );
  792. // if( cbt != NULL )
  793. // field.SetAttribute( "type", cbt->getTypeName() );
  794. // else
  795. // field.SetAttribute( "type", "TypeString" );
  796. // field.SetAttribute( "data", textbuf );
  797. // pElement->InsertEndChild( field );
  798. //}
  799. }
  800. DefineEngineMethod( SimXMLDocument, setObjectAttributes, void, ( const char* objectID ),,
  801. "@brief Add the given SimObject's fields as attributes of the current Element on the stack.\n\n"
  802. "@param objectID ID of SimObject being copied.")
  803. {
  804. object->setObjectAttributes( objectID );
  805. }
  806. // -----------------------------------------------------------------------------
  807. // Create a new element and set to child of current stack element.
  808. // New element is placed on top of element stack.
  809. // -----------------------------------------------------------------------------
  810. void SimXMLDocument::pushNewElement(const char* rName)
  811. {
  812. tinyxml2::XMLElement* cElement = m_qDocument->NewElement( rName );
  813. tinyxml2::XMLNode* pStackTop = 0;
  814. if(m_paNode.empty())
  815. {
  816. pStackTop = m_qDocument->InsertEndChild(cElement);
  817. }
  818. else
  819. {
  820. const S32 iFinalElement = m_paNode.size() - 1;
  821. tinyxml2::XMLNode *pNode = m_paNode[iFinalElement];
  822. if(!pNode)
  823. {
  824. return;
  825. }
  826. pStackTop = pNode->InsertEndChild( cElement );
  827. }
  828. if(!pStackTop)
  829. {
  830. return;
  831. }
  832. m_paNode.push_back(pStackTop);
  833. }
  834. DefineEngineMethod( SimXMLDocument, pushNewElement, void, ( const char* name ),,
  835. "@brief Create a new element with the given name as child of current Element "
  836. "and push it onto the Element stack making it the current one.\n\n"
  837. "@note This differs from addNewElement() in that it adds the new Element as a "
  838. "child of the current Element (or a child of the document if no Element exists).\n\n"
  839. "@param name XML tag for the new Element.\n"
  840. "@see addNewElement()")
  841. {
  842. object->pushNewElement( name );
  843. }
  844. // -----------------------------------------------------------------------------
  845. // Create a new element and set to child of current stack element.
  846. // New element is placed on top of element stack.
  847. // -----------------------------------------------------------------------------
  848. void SimXMLDocument::addNewElement(const char* rName)
  849. {
  850. tinyxml2::XMLElement* cElement = m_qDocument->NewElement(rName);
  851. tinyxml2::XMLNode* pStackTop = 0;
  852. if(m_paNode.empty())
  853. {
  854. pStackTop = m_qDocument->InsertEndChild( cElement );
  855. if(!pStackTop)
  856. {
  857. return;
  858. }
  859. m_paNode.push_back(pStackTop);
  860. return;
  861. }
  862. const S32 iParentElement = m_paNode.size() - 2;
  863. if(iParentElement < 0)
  864. {
  865. pStackTop = m_qDocument->InsertEndChild( cElement );
  866. if(!pStackTop)
  867. {
  868. return;
  869. }
  870. m_paNode.push_back(pStackTop);
  871. return;
  872. }
  873. else
  874. {
  875. tinyxml2::XMLNode* pNode = m_paNode[iParentElement];
  876. if(!pNode)
  877. {
  878. return;
  879. }
  880. pStackTop = pNode->InsertEndChild( cElement );
  881. if(!pStackTop)
  882. {
  883. return;
  884. }
  885. // Overwrite top stack position.
  886. const S32 iFinalElement = m_paNode.size() - 1;
  887. m_paNode[iFinalElement] = pStackTop;
  888. //pNode = pStackTop;
  889. }
  890. }
  891. DefineEngineMethod( SimXMLDocument, addNewElement, void, ( const char* name ),,
  892. "@brief Create a new element with the given name as child of current Element's "
  893. "parent and push it onto the Element stack making it the current one.\n\n"
  894. "@note This differs from pushNewElement() in that it adds the new Element to the "
  895. "current Element's parent (or document if there is no parent Element). This makes "
  896. "the new Element a sibling of the current one.\n\n"
  897. "@param name XML tag for the new Element.\n"
  898. "@see pushNewElement()")
  899. {
  900. object->addNewElement( name );
  901. }
  902. // -----------------------------------------------------------------------------
  903. // Write XML document declaration.
  904. // -----------------------------------------------------------------------------
  905. void SimXMLDocument::addHeader(void)
  906. {
  907. tinyxml2::XMLDeclaration* cDeclaration = m_qDocument->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\" standalone =\"yes\"");
  908. m_qDocument->InsertEndChild(cDeclaration);
  909. }
  910. DefineEngineMethod( SimXMLDocument, addHeader, void, (),,
  911. "@brief Add a XML header to a document.\n\n"
  912. "Sometimes called a declaration, you typically add a standard header to "
  913. "the document before adding any elements. SimXMLDocument always produces "
  914. "the following header:\n\n"
  915. "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n\n"
  916. "@tsexample\n"
  917. "// Create a new XML document with just a header and single element.\n"
  918. "%x = new SimXMLDocument();\n"
  919. "%x.addHeader();\n"
  920. "%x.addNewElement(\"NewElement\");\n"
  921. "%x.saveFile(\"test.xml\");\n\n"
  922. "// Produces the following file:\n"
  923. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  924. "// <NewElement />\n"
  925. "@endtsexample\n\n")
  926. {
  927. object->addHeader();
  928. }
  929. void SimXMLDocument::addComment(const char* comment)
  930. {
  931. tinyxml2::XMLComment* cComment = m_qDocument->NewComment(comment);
  932. cComment->SetValue(comment);
  933. m_qDocument->InsertEndChild(cComment);
  934. }
  935. DefineEngineMethod(SimXMLDocument, addComment, void, ( const char* comment ),,
  936. "@brief Add the given comment as a child of the document.\n\n"
  937. "@param comment String containing the comment."
  938. "@tsexample\n"
  939. "// Create a new XML document with a header, a comment and single element.\n"
  940. "%x = new SimXMLDocument();\n"
  941. "%x.addHeader();\n"
  942. "%x.addComment(\"This is a test comment\");\n"
  943. "%x.addNewElement(\"NewElement\");\n"
  944. "%x.saveFile(\"test.xml\");\n\n"
  945. "// Produces the following file:\n"
  946. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  947. "// <!--This is a test comment-->\n"
  948. "// <NewElement />\n"
  949. "@endtsexample\n\n"
  950. "@see readComment()")
  951. {
  952. object->addComment( comment );
  953. }
  954. const char* SimXMLDocument::readComment( S32 index )
  955. {
  956. // Clear the current attribute pointer
  957. m_CurrentAttribute = 0;
  958. // Push the first element found under the current element of the given name
  959. if(!m_paNode.empty())
  960. {
  961. const S32 iLastElement = m_paNode.size() - 1;
  962. tinyxml2::XMLNode* pNode = m_paNode[iLastElement];
  963. if(!pNode)
  964. {
  965. return "";
  966. }
  967. tinyxml2::XMLNode* node = pNode->FirstChild();
  968. for( S32 i = 0; i < index; i++ )
  969. {
  970. if( !node )
  971. return "";
  972. node = node->NextSiblingElement();
  973. }
  974. if( node )
  975. {
  976. tinyxml2::XMLComment* comment = node->ToComment();
  977. if( comment )
  978. return comment->Value();
  979. }
  980. }
  981. else
  982. {
  983. if(!m_qDocument)
  984. {
  985. return "";
  986. }
  987. tinyxml2::XMLNode* node = m_qDocument->FirstChild();
  988. for( S32 i = 0; i < index; i++ )
  989. {
  990. if( !node )
  991. return "";
  992. node = node->NextSibling();
  993. }
  994. if( node )
  995. {
  996. tinyxml2::XMLComment* comment = node->ToComment();
  997. if( comment )
  998. return comment->Value();
  999. }
  1000. }
  1001. return "";
  1002. }
  1003. DefineEngineMethod( SimXMLDocument, readComment, const char*, ( S32 index ),,
  1004. "Gives the comment at the specified index, if any.\n\n"
  1005. "Unlike addComment() that only works at the document level, readComment() may read "
  1006. "comments from the document or any child Element. The current Element (or document "
  1007. "if no Elements have been pushed to the stack) is the parent for any comments, and the "
  1008. "provided index is the number of comments in to read back."
  1009. "@param index Comment index number to query from the current Element stack\n\n"
  1010. "@return String containing the comment, or an empty string if no comment is found.\n\n"
  1011. "@see addComment()")
  1012. {
  1013. return object->readComment( index );
  1014. }
  1015. void SimXMLDocument::addText(const char* text)
  1016. {
  1017. if(m_paNode.empty())
  1018. return;
  1019. const S32 iFinalElement = m_paNode.size() - 1;
  1020. tinyxml2::XMLNode* pNode = m_paNode[iFinalElement];
  1021. if(!pNode)
  1022. return;
  1023. tinyxml2::XMLText* cText = m_qDocument->NewText(text);
  1024. pNode->InsertEndChild( cText );
  1025. }
  1026. DefineEngineMethod( SimXMLDocument, addText, void, ( const char* text ),,
  1027. "@brief Add the given text as a child of current Element.\n\n"
  1028. "Use getText() to retrieve any text from the current Element and removeText() "
  1029. "to clear any text.\n\n"
  1030. "addText() and addData() may be used interchangeably."
  1031. "@param text String containing the text.\n\n"
  1032. "@tsexample\n"
  1033. "// Create a new XML document with a header and single element\n"
  1034. "// with some added text.\n"
  1035. "%x = new SimXMLDocument();\n"
  1036. "%x.addHeader();\n"
  1037. "%x.addNewElement(\"NewElement\");\n"
  1038. "%x.addText(\"Some text\");\n"
  1039. "%x.saveFile(\"test.xml\");\n\n"
  1040. "// Produces the following file:\n"
  1041. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  1042. "// <NewElement>Some text</NewElement>\n"
  1043. "@endtsexample\n\n"
  1044. "@see getText()\n"
  1045. "@see removeText()\n"
  1046. "@see addData()\n"
  1047. "@see getData()")
  1048. {
  1049. object->addText( text );
  1050. }
  1051. const char* SimXMLDocument::getText()
  1052. {
  1053. if(m_paNode.empty())
  1054. return "";
  1055. const S32 iFinalElement = m_paNode.size() - 1;
  1056. tinyxml2::XMLNode* pNode = m_paNode[iFinalElement];
  1057. if(!pNode)
  1058. return "";
  1059. if(!pNode->FirstChild())
  1060. return "";
  1061. tinyxml2::XMLText* text = pNode->FirstChild()->ToText();
  1062. if( !text )
  1063. return "";
  1064. return text->Value();
  1065. }
  1066. DefineEngineMethod( SimXMLDocument, getText, const char*, (),,
  1067. "@brief Gets the text from the current Element.\n\n"
  1068. "Use addText() to add text to the current Element and removeText() "
  1069. "to clear any text.\n\n"
  1070. "getText() and getData() may be used interchangeably."
  1071. "@return String containing the text in the current Element."
  1072. "@tsexample\n"
  1073. "// Using the following test.xml file as an example:\n"
  1074. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  1075. "// <NewElement>Some text</NewElement>\n\n"
  1076. "// Load in the file\n"
  1077. "%x = new SimXMLDocument();\n"
  1078. "%x.loadFile(\"test.xml\");\n\n"
  1079. "// Make the first Element the current one\n"
  1080. "%x.pushFirstChildElement(\"NewElement\");\n\n"
  1081. "// Store the current Element's text ('Some text' in this example)\n"
  1082. "// into 'result'\n"
  1083. "%result = %x.getText();\n"
  1084. "echo( %result );\n"
  1085. "@endtsexample\n\n"
  1086. "@see addText()\n"
  1087. "@see removeText()\n"
  1088. "@see addData()\n"
  1089. "@see getData()\n")
  1090. {
  1091. const char* text = object->getText();
  1092. if( !text )
  1093. return "";
  1094. return text;
  1095. }
  1096. void SimXMLDocument::removeText()
  1097. {
  1098. if(m_paNode.empty())
  1099. return;
  1100. const S32 iFinalElement = m_paNode.size() - 1;
  1101. tinyxml2::XMLNode* pNode = m_paNode[iFinalElement];
  1102. if(!pNode)
  1103. return;
  1104. if( !pNode->FirstChild() )
  1105. return;
  1106. tinyxml2::XMLText* text = pNode->FirstChild()->ToText();
  1107. if( !text )
  1108. return;
  1109. pNode->DeleteChild(text);
  1110. }
  1111. DefineEngineMethod( SimXMLDocument, removeText, void, (),,
  1112. "@brief Remove any text on the current Element.\n\n"
  1113. "Use getText() to retrieve any text from the current Element and addText() "
  1114. "to add text to the current Element. As getData() and addData() are equivalent "
  1115. "to getText() and addText(), removeText() will also remove any data from the "
  1116. "current Element.\n\n"
  1117. "@see addText()\n"
  1118. "@see getText()\n"
  1119. "@see addData()\n"
  1120. "@see getData()\n")
  1121. {
  1122. object->removeText();
  1123. }
  1124. void SimXMLDocument::addData(const char* text)
  1125. {
  1126. if(m_paNode.empty())
  1127. return;
  1128. const S32 iFinalElement = m_paNode.size() - 1;
  1129. tinyxml2::XMLNode* pNode = m_paNode[iFinalElement];
  1130. if(!pNode)
  1131. return;
  1132. tinyxml2::XMLText* cText = m_qDocument->NewText(text);
  1133. pNode->InsertEndChild( cText );
  1134. }
  1135. DefineEngineMethod( SimXMLDocument, addData, void, ( const char* text ),,
  1136. "@brief Add the given text as a child of current Element.\n\n"
  1137. "Use getData() to retrieve any text from the current Element.\n\n"
  1138. "addData() and addText() may be used interchangeably. As there is no "
  1139. "difference between data and text, you may also use removeText() to clear "
  1140. "any data from the current Element.\n\n"
  1141. "@param text String containing the text.\n\n"
  1142. "@tsexample\n"
  1143. "// Create a new XML document with a header and single element\n"
  1144. "// with some added data.\n"
  1145. "%x = new SimXMLDocument();\n"
  1146. "%x.addHeader();\n"
  1147. "%x.addNewElement(\"NewElement\");\n"
  1148. "%x.addData(\"Some text\");\n"
  1149. "%x.saveFile(\"test.xml\");\n\n"
  1150. "// Produces the following file:\n"
  1151. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  1152. "// <NewElement>Some text</NewElement>\n"
  1153. "@endtsexample\n\n"
  1154. "@see getData()"
  1155. "@see addText()\n"
  1156. "@see getText()\n"
  1157. "@see removeText()\n")
  1158. {
  1159. object->addData( text );
  1160. }
  1161. const char* SimXMLDocument::getData()
  1162. {
  1163. if(m_paNode.empty())
  1164. return "";
  1165. const S32 iFinalElement = m_paNode.size() - 1;
  1166. tinyxml2::XMLNode* pNode = m_paNode[iFinalElement];
  1167. if(!pNode)
  1168. return "";
  1169. if( !pNode->FirstChild() )
  1170. return "";
  1171. tinyxml2::XMLText* text = pNode->FirstChild()->ToText();
  1172. if( !text )
  1173. return "";
  1174. return text->Value();
  1175. }
  1176. DefineEngineMethod( SimXMLDocument, getData, const char*, (),,
  1177. "@brief Gets the text from the current Element.\n\n"
  1178. "Use addData() to add text to the current Element.\n\n"
  1179. "getData() and getText() may be used interchangeably. As there is no "
  1180. "difference between data and text, you may also use removeText() to clear "
  1181. "any data from the current Element.\n\n"
  1182. "@return String containing the text in the current Element."
  1183. "@tsexample\n"
  1184. "// Using the following test.xml file as an example:\n"
  1185. "// <?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
  1186. "// <NewElement>Some data</NewElement>\n\n"
  1187. "// Load in the file\n"
  1188. "%x = new SimXMLDocument();\n"
  1189. "%x.loadFile(\"test.xml\");\n\n"
  1190. "// Make the first Element the current one\n"
  1191. "%x.pushFirstChildElement(\"NewElement\");\n\n"
  1192. "// Store the current Element's data ('Some data' in this example)\n"
  1193. "// into 'result'\n"
  1194. "%result = %x.getData();\n"
  1195. "echo( %result );\n"
  1196. "@endtsexample\n\n"
  1197. "@see addData()\n"
  1198. "@see addText()\n"
  1199. "@see getText()\n"
  1200. "@see removeText()\n")
  1201. {
  1202. const char* text = object->getData();
  1203. if( !text )
  1204. return "";
  1205. return text;
  1206. }
  1207. ////EOF/////////////////////////////////////////////////////////////////////////