SimXMLDocument.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "persistence/SimXMLDocument.h"
  23. #include "memory/frameAllocator.h"
  24. #include "sim/simBase.h"
  25. #include "console/consoleInternal.h"
  26. #include "io/resource/resourceManager.h"
  27. #include "io/fileStream.h"
  28. #include "SimXMLDocument_ScriptBinding.h"
  29. IMPLEMENT_CONOBJECT(SimXMLDocument);
  30. // -----------------------------------------------------------------------------
  31. // Constructor.
  32. // -----------------------------------------------------------------------------
  33. SimXMLDocument::SimXMLDocument():
  34. m_qDocument(0),
  35. m_CurrentAttribute(0)
  36. {
  37. }
  38. // -----------------------------------------------------------------------------
  39. // Destructor.
  40. // -----------------------------------------------------------------------------
  41. SimXMLDocument::~SimXMLDocument()
  42. {
  43. }
  44. // -----------------------------------------------------------------------------
  45. // Included for completeness.
  46. // -----------------------------------------------------------------------------
  47. bool SimXMLDocument::processArguments(S32 argc, const char** argv)
  48. {
  49. if(0 == argc)
  50. {
  51. return true;
  52. }
  53. else
  54. {
  55. return true;
  56. }
  57. return false;
  58. }
  59. // -----------------------------------------------------------------------------
  60. // Script constructor.
  61. // -----------------------------------------------------------------------------
  62. bool SimXMLDocument::onAdd()
  63. {
  64. if(!Parent::onAdd())
  65. {
  66. return false;
  67. }
  68. if(!m_qDocument)
  69. {
  70. m_qDocument = new TiXmlDocument();
  71. }
  72. return true;
  73. }
  74. // -----------------------------------------------------------------------------
  75. // Script destructor.
  76. // -----------------------------------------------------------------------------
  77. void SimXMLDocument::onRemove()
  78. {
  79. Parent::onRemove();
  80. if(m_qDocument)
  81. {
  82. m_qDocument->Clear();
  83. delete(m_qDocument);
  84. }
  85. }
  86. // -----------------------------------------------------------------------------
  87. // Initialize peristent fields (datablocks).
  88. // -----------------------------------------------------------------------------
  89. void SimXMLDocument::initPersistFields()
  90. {
  91. Parent::initPersistFields();
  92. }
  93. // -----------------------------------------------------------------------------
  94. // Set this to default state at construction.
  95. // -----------------------------------------------------------------------------
  96. void SimXMLDocument::reset(void)
  97. {
  98. m_qDocument->Clear();
  99. m_paNode.clear();
  100. m_CurrentAttribute = 0;
  101. }
  102. // -----------------------------------------------------------------------------
  103. // Get true if file loads successfully.
  104. // -----------------------------------------------------------------------------
  105. S32 SimXMLDocument::loadFile(const char* rFileName)
  106. {
  107. reset();
  108. return m_qDocument->LoadFile(rFileName);
  109. }
  110. // -----------------------------------------------------------------------------
  111. // Get true if file saves successfully.
  112. // -----------------------------------------------------------------------------
  113. S32 SimXMLDocument::saveFile(const char* rFileName)
  114. {
  115. char buffer[1024];
  116. Con::expandPath(buffer, sizeof(buffer), rFileName);
  117. if(buffer == NULL || *buffer == 0)
  118. return false;
  119. FileStream strm;
  120. if(!ResourceManager->openFileForWrite(strm, buffer, FileStream::Write))
  121. {
  122. Con::errorf(ConsoleLogEntry::General, "Unable to open file '%s for writing.", buffer);
  123. return false;
  124. }
  125. bool retVal = m_qDocument->SaveFile(strm);//m_qDocument->SaveFile( buffer );
  126. // close the stream
  127. strm.close();
  128. return retVal;
  129. }
  130. // -----------------------------------------------------------------------------
  131. // Get true if XML text parses correctly.
  132. // -----------------------------------------------------------------------------
  133. S32 SimXMLDocument::parse(const char* rText)
  134. {
  135. reset();
  136. m_qDocument->Parse( rText );
  137. return 1;
  138. }
  139. // -----------------------------------------------------------------------------
  140. // Clear contents of XML document.
  141. // -----------------------------------------------------------------------------
  142. void SimXMLDocument::clear(void)
  143. {
  144. reset();
  145. }
  146. // -----------------------------------------------------------------------------
  147. // Get error description of XML document.
  148. // -----------------------------------------------------------------------------
  149. const char* SimXMLDocument::getErrorDesc(void) const
  150. {
  151. if(!m_qDocument)
  152. {
  153. return StringTable->insert("No document");
  154. }
  155. return m_qDocument->ErrorDesc();
  156. }
  157. // -----------------------------------------------------------------------------
  158. // Clear error description of this.
  159. // -----------------------------------------------------------------------------
  160. void SimXMLDocument::clearError(void)
  161. {
  162. m_qDocument->ClearError();
  163. }
  164. // -----------------------------------------------------------------------------
  165. // Get true if first child element was successfully pushed onto stack.
  166. // -----------------------------------------------------------------------------
  167. bool SimXMLDocument::pushFirstChildElement(const char* rName)
  168. {
  169. // Clear the current attribute pointer
  170. m_CurrentAttribute = 0;
  171. // Push the first element found under the current element of the given name
  172. TiXmlElement* pElement;
  173. if(!m_paNode.empty())
  174. {
  175. const int iLastElement = m_paNode.size() - 1;
  176. TiXmlElement* pNode = m_paNode[iLastElement];
  177. if(!pNode)
  178. {
  179. return false;
  180. }
  181. pElement = pNode->FirstChildElement(rName);
  182. }
  183. else
  184. {
  185. if(!m_qDocument)
  186. {
  187. return false;
  188. }
  189. pElement = m_qDocument->FirstChildElement(rName);
  190. }
  191. if(!pElement)
  192. {
  193. return false;
  194. }
  195. m_paNode.push_back(pElement);
  196. return true;
  197. }
  198. // -----------------------------------------------------------------------------
  199. // Get true if first child element was successfully pushed onto stack.
  200. // -----------------------------------------------------------------------------
  201. bool SimXMLDocument::pushChildElement(S32 index)
  202. {
  203. // Clear the current attribute pointer
  204. m_CurrentAttribute = 0;
  205. // Push the first element found under the current element of the given name
  206. TiXmlElement* pElement;
  207. if(!m_paNode.empty())
  208. {
  209. const int iLastElement = m_paNode.size() - 1;
  210. TiXmlElement* pNode = m_paNode[iLastElement];
  211. if(!pNode)
  212. {
  213. return false;
  214. }
  215. pElement = pNode->FirstChildElement();
  216. for( S32 i = 0; i < index; i++ )
  217. {
  218. if( !pElement )
  219. return false;
  220. pElement = pElement->NextSiblingElement();
  221. }
  222. }
  223. else
  224. {
  225. if(!m_qDocument)
  226. {
  227. return false;
  228. }
  229. pElement = m_qDocument->FirstChildElement();
  230. for( S32 i = 0; i < index; i++ )
  231. {
  232. if( !pElement )
  233. return false;
  234. pElement = pElement->NextSiblingElement();
  235. }
  236. }
  237. if(!pElement)
  238. {
  239. return false;
  240. }
  241. m_paNode.push_back(pElement);
  242. return true;
  243. }
  244. // -----------------------------------------------------------------------------
  245. // Convert top stack element into its next sibling element.
  246. // -----------------------------------------------------------------------------
  247. bool SimXMLDocument::nextSiblingElement(const char* rName)
  248. {
  249. // Clear the current attribute pointer
  250. m_CurrentAttribute = 0;
  251. // Attempt to find the next sibling element
  252. if(m_paNode.empty())
  253. {
  254. return false;
  255. }
  256. const int iLastElement = m_paNode.size() - 1;
  257. TiXmlElement*& pElement = m_paNode[iLastElement];
  258. if(!pElement)
  259. {
  260. return false;
  261. }
  262. pElement = pElement->NextSiblingElement(rName);
  263. if(!pElement)
  264. {
  265. return false;
  266. }
  267. return true;
  268. }
  269. // -----------------------------------------------------------------------------
  270. // Get element value if it exists. Used to extract a text node from the element.
  271. // for example.
  272. // -----------------------------------------------------------------------------
  273. const char* SimXMLDocument::elementValue()
  274. {
  275. if(m_paNode.empty())
  276. {
  277. return StringTable->EmptyString;
  278. }
  279. const int iLastElement = m_paNode.size() - 1;
  280. TiXmlElement* pNode = m_paNode[iLastElement];
  281. if(!pNode)
  282. {
  283. return StringTable->EmptyString;
  284. }
  285. return pNode->Value();
  286. }
  287. // -----------------------------------------------------------------------------
  288. // Pop last element off of stack.
  289. // -----------------------------------------------------------------------------
  290. void SimXMLDocument::popElement(void)
  291. {
  292. m_paNode.pop_back();
  293. }
  294. // -----------------------------------------------------------------------------
  295. // Get attribute value if it exists.
  296. // -----------------------------------------------------------------------------
  297. const char* SimXMLDocument::attribute(const char* rAttribute)
  298. {
  299. if(m_paNode.empty())
  300. {
  301. return StringTable->EmptyString;
  302. }
  303. const int iLastElement = m_paNode.size() - 1;
  304. TiXmlElement* pNode = m_paNode[iLastElement];
  305. if(!pNode)
  306. {
  307. return StringTable->EmptyString;
  308. }
  309. if(!pNode->Attribute(rAttribute))
  310. {
  311. return StringTable->EmptyString;
  312. }
  313. return pNode->Attribute(rAttribute);
  314. }
  315. // -----------------------------------------------------------------------------
  316. // Get true if given attribute exists.
  317. // -----------------------------------------------------------------------------
  318. bool SimXMLDocument::attributeExists(const char* rAttribute)
  319. {
  320. if(m_paNode.empty())
  321. {
  322. return false;
  323. }
  324. const int iLastElement = m_paNode.size() - 1;
  325. TiXmlElement* pNode = m_paNode[iLastElement];
  326. if(!pNode)
  327. {
  328. return false;
  329. }
  330. if(!pNode->Attribute(rAttribute))
  331. {
  332. return false;
  333. }
  334. return true;
  335. }
  336. // -----------------------------------------------------------------------------
  337. // Obtain the name of the current element's first attribute
  338. // -----------------------------------------------------------------------------
  339. const char* SimXMLDocument::firstAttribute()
  340. {
  341. // Get the current element
  342. if(m_paNode.empty())
  343. {
  344. return StringTable->EmptyString;
  345. }
  346. const int iLastElement = m_paNode.size() - 1;
  347. TiXmlElement* pNode = m_paNode[iLastElement];
  348. if(!pNode)
  349. {
  350. return StringTable->EmptyString;
  351. }
  352. // Gets its first attribute, if any
  353. m_CurrentAttribute = pNode->FirstAttribute();
  354. if(!m_CurrentAttribute)
  355. {
  356. return StringTable->EmptyString;
  357. }
  358. return m_CurrentAttribute->Name();
  359. }
  360. // -----------------------------------------------------------------------------
  361. // Obtain the name of the current element's last attribute
  362. // -----------------------------------------------------------------------------
  363. const char* SimXMLDocument::lastAttribute()
  364. {
  365. // Get the current element
  366. if(m_paNode.empty())
  367. {
  368. return StringTable->EmptyString;
  369. }
  370. const int iLastElement = m_paNode.size() - 1;
  371. TiXmlElement* pNode = m_paNode[iLastElement];
  372. if(!pNode)
  373. {
  374. return StringTable->EmptyString;
  375. }
  376. // Gets its last attribute, if any
  377. m_CurrentAttribute = pNode->LastAttribute();
  378. if(!m_CurrentAttribute)
  379. {
  380. return StringTable->EmptyString;
  381. }
  382. return m_CurrentAttribute->Name();
  383. }
  384. // -----------------------------------------------------------------------------
  385. // Get the name of the next attribute for the current element after a call
  386. // to firstAttribute().
  387. // -----------------------------------------------------------------------------
  388. const char* SimXMLDocument::nextAttribute()
  389. {
  390. if(!m_CurrentAttribute)
  391. {
  392. return StringTable->EmptyString;
  393. }
  394. // Gets its next attribute, if any
  395. m_CurrentAttribute = m_CurrentAttribute->Next();
  396. if(!m_CurrentAttribute)
  397. {
  398. return StringTable->EmptyString;
  399. }
  400. return m_CurrentAttribute->Name();
  401. }
  402. // -----------------------------------------------------------------------------
  403. // Get the name of the previous attribute for the current element after a call
  404. // to lastAttribute().
  405. // -----------------------------------------------------------------------------
  406. const char* SimXMLDocument::prevAttribute()
  407. {
  408. if(!m_CurrentAttribute)
  409. {
  410. return StringTable->EmptyString;
  411. }
  412. // Gets its next attribute, if any
  413. m_CurrentAttribute = m_CurrentAttribute->Previous();
  414. if(!m_CurrentAttribute)
  415. {
  416. return StringTable->EmptyString;
  417. }
  418. return m_CurrentAttribute->Name();
  419. }
  420. // -----------------------------------------------------------------------------
  421. // Set attribute of top stack element to given value.
  422. // -----------------------------------------------------------------------------
  423. void SimXMLDocument::setAttribute(const char* rAttribute, const char* rVal)
  424. {
  425. if(m_paNode.empty())
  426. {
  427. return;
  428. }
  429. const int iLastElement = m_paNode.size() - 1;
  430. TiXmlElement* pElement = m_paNode[iLastElement];
  431. if(!pElement)
  432. {
  433. return;
  434. }
  435. pElement->SetAttribute(rAttribute, rVal);
  436. }
  437. // -----------------------------------------------------------------------------
  438. // Set attribute of top stack element to given value.
  439. // -----------------------------------------------------------------------------
  440. void SimXMLDocument::setObjectAttributes(const char* objectID)
  441. {
  442. if( !objectID || !objectID[0] )
  443. return;
  444. if(m_paNode.empty())
  445. return;
  446. SimObject *pObject = Sim::findObject( objectID );
  447. if( pObject == NULL )
  448. return;
  449. const int iLastElement = m_paNode.size() - 1;
  450. TiXmlElement* pElement = m_paNode[iLastElement];
  451. if(!pElement)
  452. return;
  453. char textbuf[1024];
  454. TiXmlElement field( "Field" );
  455. TiXmlElement group( "FieldGroup" );
  456. pElement->SetAttribute( "Name", pObject->getName() );
  457. // Iterate over our filed list and add them to the XML document...
  458. AbstractClassRep::FieldList fieldList = pObject->getFieldList();
  459. AbstractClassRep::FieldList::iterator itr;
  460. for(itr = fieldList.begin(); itr != fieldList.end(); itr++)
  461. {
  462. if( itr->type == AbstractClassRep::DepricatedFieldType ||
  463. itr->type == AbstractClassRep::StartGroupFieldType ||
  464. itr->type == AbstractClassRep::EndGroupFieldType) continue;
  465. // Not an Array
  466. if(itr->elementCount == 1)
  467. {
  468. // get the value of the field as a string.
  469. ConsoleBaseType *cbt = ConsoleBaseType::getType(itr->type);
  470. const char *val = Con::getData(itr->type, (void *) (((const char *)pObject) + itr->offset), 0, itr->table, itr->flag);
  471. // Make a copy for the field check.
  472. if (!val)
  473. continue;
  474. FrameTemp<char> valCopy( dStrlen( val ) + 1 );
  475. dStrcpy( (char *)valCopy, val );
  476. if (!pObject->writeField(itr->pFieldname, valCopy))
  477. continue;
  478. val = valCopy;
  479. expandEscape(textbuf, val);
  480. if( !pObject->writeField( itr->pFieldname, textbuf ) )
  481. continue;
  482. field.SetValue( "Property" );
  483. field.SetAttribute( "name", itr->pFieldname );
  484. if( cbt != NULL )
  485. field.SetAttribute( "type", cbt->getTypeName() );
  486. else
  487. field.SetAttribute( "type", "TypeString" );
  488. field.SetAttribute( "data", textbuf );
  489. pElement->InsertEndChild( field );
  490. continue;
  491. }
  492. }
  493. //// IS An Array
  494. //for(U32 j = 0; S32(j) < f->elementCount; j++)
  495. //{
  496. // // If the start of a group create an element for the group and
  497. // // the our chache to it
  498. // const char *val = Con::getData(itr->type, (void *) (((const char *)pObject) + itr->offset), j, itr->table, itr->flag);
  499. // // Make a copy for the field check.
  500. // if (!val)
  501. // continue;
  502. // FrameTemp<char> valCopy( dStrlen( val ) + 1 );
  503. // dStrcpy( (char *)valCopy, val );
  504. // if (!pObject->writeField(itr->pFieldname, valCopy))
  505. // continue;
  506. // val = valCopy;
  507. // // get the value of the field as a string.
  508. // ConsoleBaseType *cbt = ConsoleBaseType::getType(itr->type);
  509. // const char * dstr = Con::getData(itr->type, (void *)(((const char *)pObject) + itr->offset), 0, itr->table, itr->flag);
  510. // if(!dstr)
  511. // dstr = "";
  512. // expandEscape(textbuf, dstr);
  513. // if( !pObject->writeField( itr->pFieldname, dstr ) )
  514. // continue;
  515. // field.SetValue( "Property" );
  516. // field.SetAttribute( "name", itr->pFieldname );
  517. // if( cbt != NULL )
  518. // field.SetAttribute( "type", cbt->getTypeName() );
  519. // else
  520. // field.SetAttribute( "type", "TypeString" );
  521. // field.SetAttribute( "data", textbuf );
  522. // pElement->InsertEndChild( field );
  523. //}
  524. }
  525. // -----------------------------------------------------------------------------
  526. // Create a new element and set to child of current stack element.
  527. // New element is placed on top of element stack.
  528. // -----------------------------------------------------------------------------
  529. void SimXMLDocument::pushNewElement(const char* rName)
  530. {
  531. TiXmlElement cElement( rName );
  532. TiXmlElement* pStackTop = 0;
  533. if(m_paNode.empty())
  534. {
  535. pStackTop = dynamic_cast<TiXmlElement*>
  536. (m_qDocument->InsertEndChild( cElement ) );
  537. }
  538. else
  539. {
  540. const int iFinalElement = m_paNode.size() - 1;
  541. TiXmlElement* pNode = m_paNode[iFinalElement];
  542. if(!pNode)
  543. {
  544. return;
  545. }
  546. pStackTop = dynamic_cast<TiXmlElement*>
  547. (pNode->InsertEndChild( cElement ));
  548. }
  549. if(!pStackTop)
  550. {
  551. return;
  552. }
  553. m_paNode.push_back(pStackTop);
  554. }
  555. // -----------------------------------------------------------------------------
  556. // Create a new element and set to child of current stack element.
  557. // New element is placed on top of element stack.
  558. // -----------------------------------------------------------------------------
  559. void SimXMLDocument::addNewElement(const char* rName)
  560. {
  561. TiXmlElement cElement( rName );
  562. TiXmlElement* pStackTop = 0;
  563. if(m_paNode.empty())
  564. {
  565. pStackTop = dynamic_cast<TiXmlElement*>
  566. (m_qDocument->InsertEndChild( cElement ));
  567. if(!pStackTop)
  568. {
  569. return;
  570. }
  571. m_paNode.push_back(pStackTop);
  572. return;
  573. }
  574. const int iParentElement = m_paNode.size() - 2;
  575. if(iParentElement < 0)
  576. {
  577. pStackTop = dynamic_cast<TiXmlElement*>
  578. (m_qDocument->InsertEndChild( cElement ));
  579. if(!pStackTop)
  580. {
  581. return;
  582. }
  583. m_paNode.push_back(pStackTop);
  584. return;
  585. }
  586. else
  587. {
  588. TiXmlElement* pNode = m_paNode[iParentElement];
  589. if(!pNode)
  590. {
  591. return;
  592. }
  593. pStackTop = dynamic_cast<TiXmlElement*>
  594. (pNode->InsertEndChild( cElement ));
  595. if(!pStackTop)
  596. {
  597. return;
  598. }
  599. // Overwrite top stack position.
  600. const int iFinalElement = m_paNode.size() - 1;
  601. m_paNode[iFinalElement] = pStackTop;
  602. //pNode = pStackTop;
  603. }
  604. }
  605. // -----------------------------------------------------------------------------
  606. // Write XML document declaration.
  607. // -----------------------------------------------------------------------------
  608. void SimXMLDocument::addHeader(void)
  609. {
  610. TiXmlDeclaration cDeclaration("1.0", "utf-8", "yes");
  611. m_qDocument->InsertEndChild(cDeclaration);
  612. }
  613. void SimXMLDocument::addComment(const char* comment)
  614. {
  615. TiXmlComment cComment;
  616. cComment.SetValue(comment);
  617. if(m_paNode.empty())
  618. {
  619. Con::warnf("Cannot add comment without any elements: '%s'", comment);
  620. return;
  621. }
  622. const int iFinalElement = m_paNode.size() - 1;
  623. TiXmlElement* pNode = m_paNode[iFinalElement];
  624. if(!pNode)
  625. {
  626. return;
  627. }
  628. pNode->InsertEndChild( cComment );
  629. }
  630. const char* SimXMLDocument::readComment( S32 index )
  631. {
  632. // Clear the current attribute pointer
  633. m_CurrentAttribute = 0;
  634. // Push the first element found under the current element of the given name
  635. if(!m_paNode.empty())
  636. {
  637. const int iLastElement = m_paNode.size() - 1;
  638. TiXmlElement* pNode = m_paNode[iLastElement];
  639. if(!pNode)
  640. {
  641. return "";
  642. }
  643. TiXmlNode* node = pNode->FirstChild();
  644. for( S32 i = 0; i < index; i++ )
  645. {
  646. if( !node )
  647. return "";
  648. node = node->NextSiblingElement();
  649. }
  650. if( node )
  651. {
  652. TiXmlComment* comment = node->ToComment();
  653. if( comment )
  654. return comment->Value();
  655. }
  656. }
  657. else
  658. {
  659. if(!m_qDocument)
  660. {
  661. return "";
  662. }
  663. TiXmlNode* node = m_qDocument->FirstChild();
  664. for( S32 i = 0; i < index; i++ )
  665. {
  666. if( !node )
  667. return "";
  668. node = node->NextSibling();
  669. }
  670. if( node )
  671. {
  672. TiXmlComment* comment = node->ToComment();
  673. if( comment )
  674. return comment->Value();
  675. }
  676. }
  677. return "";
  678. }
  679. void SimXMLDocument::addText(const char* text)
  680. {
  681. if(m_paNode.empty())
  682. return;
  683. const int iFinalElement = m_paNode.size() - 1;
  684. TiXmlElement* pNode = m_paNode[iFinalElement];
  685. if(!pNode)
  686. return;
  687. TiXmlText cText(text);
  688. pNode->InsertEndChild( cText );
  689. }
  690. const char* SimXMLDocument::getText()
  691. {
  692. if(m_paNode.empty())
  693. return "";
  694. const int iFinalElement = m_paNode.size() - 1;
  695. TiXmlNode* pNode = m_paNode[iFinalElement];
  696. if(!pNode)
  697. return "";
  698. TiXmlText* text = pNode->FirstChild()->ToText();
  699. if( !text )
  700. return "";
  701. return text->Value();
  702. }
  703. void SimXMLDocument::removeText()
  704. {
  705. if(m_paNode.empty())
  706. return;
  707. const int iFinalElement = m_paNode.size() - 1;
  708. TiXmlElement* pNode = m_paNode[iFinalElement];
  709. if(!pNode)
  710. return;
  711. TiXmlText* text = pNode->FirstChild()->ToText();
  712. if( !text )
  713. return;
  714. pNode->RemoveChild(text);
  715. }
  716. void SimXMLDocument::addData(const char* text)
  717. {
  718. if(m_paNode.empty())
  719. return;
  720. const int iFinalElement = m_paNode.size() - 1;
  721. TiXmlElement* pNode = m_paNode[iFinalElement];
  722. if(!pNode)
  723. return;
  724. TiXmlText cText(text);
  725. pNode->InsertEndChild( cText );
  726. }
  727. const char* SimXMLDocument::getData()
  728. {
  729. if(m_paNode.empty())
  730. return "";
  731. const int iFinalElement = m_paNode.size() - 1;
  732. TiXmlNode* pNode = m_paNode[iFinalElement];
  733. if(!pNode)
  734. return "";
  735. TiXmlNode * firstChild = pNode->FirstChild();
  736. if(!firstChild)
  737. return "";
  738. TiXmlText* text = firstChild->ToText();
  739. if( !text )
  740. return "";
  741. return text->Value();
  742. }