SimXMLDocument.cpp 45 KB

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