tinyxml2.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. #include "tinyxml2.h"
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <new.h>
  7. #include <stdarg.h>
  8. //#pragma warning ( disable : 4291 )
  9. using namespace tinyxml2;
  10. static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
  11. static const char LF = LINE_FEED;
  12. static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
  13. static const char CR = CARRIAGE_RETURN;
  14. static const char SINGLE_QUOTE = '\'';
  15. static const char DOUBLE_QUOTE = '\"';
  16. // Bunch of unicode info at:
  17. // http://www.unicode.org/faq/utf_bom.html
  18. // ef bb bf (Microsoft "lead bytes") - designates UTF-8
  19. static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
  20. static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
  21. static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
  22. #define DELETE_NODE( node ) { \
  23. if ( node ) { \
  24. MemPool* pool = node->memPool; \
  25. node->~XMLNode(); \
  26. pool->Free( node ); \
  27. } \
  28. }
  29. #define DELETE_ATTRIBUTE( attrib ) { \
  30. if ( attrib ) { \
  31. MemPool* pool = attrib->memPool; \
  32. attrib->~XMLAttribute(); \
  33. pool->Free( attrib ); \
  34. } \
  35. }
  36. struct Entity {
  37. const char* pattern;
  38. int length;
  39. char value;
  40. };
  41. static const int NUM_ENTITIES = 5;
  42. static const Entity entities[NUM_ENTITIES] =
  43. {
  44. { "quot", 4, DOUBLE_QUOTE },
  45. { "amp", 3, '&' },
  46. { "apos", 4, SINGLE_QUOTE },
  47. { "lt", 2, '<' },
  48. { "gt", 2, '>' }
  49. };
  50. StrPair::~StrPair()
  51. {
  52. Reset();
  53. }
  54. void StrPair::Reset()
  55. {
  56. if ( flags & NEEDS_DELETE ) {
  57. delete [] start;
  58. }
  59. flags = 0;
  60. start = 0;
  61. end = 0;
  62. }
  63. void StrPair::SetStr( const char* str, int flags )
  64. {
  65. Reset();
  66. size_t len = strlen( str );
  67. start = new char[ len+1 ];
  68. memcpy( start, str, len+1 );
  69. end = start + len;
  70. this->flags = flags | NEEDS_DELETE;
  71. }
  72. char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
  73. {
  74. TIXMLASSERT( endTag && *endTag );
  75. char* start = p; // fixme: hides a member
  76. char endChar = *endTag;
  77. int length = strlen( endTag );
  78. // Inner loop of text parsing.
  79. while ( *p ) {
  80. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  81. Set( start, p, strFlags );
  82. return p + length;
  83. }
  84. ++p;
  85. }
  86. return 0;
  87. }
  88. char* StrPair::ParseName( char* p )
  89. {
  90. char* start = p;
  91. start = p;
  92. if ( !start || !(*start) ) {
  93. return 0;
  94. }
  95. if ( !XMLUtil::IsAlpha( *p ) ) {
  96. return 0;
  97. }
  98. while( *p && (
  99. XMLUtil::IsAlphaNum( (unsigned char) *p )
  100. || *p == '_'
  101. || *p == '-'
  102. || *p == '.'
  103. || *p == ':' ))
  104. {
  105. ++p;
  106. }
  107. if ( p > start ) {
  108. Set( start, p, 0 );
  109. return p;
  110. }
  111. return 0;
  112. }
  113. const char* StrPair::GetStr()
  114. {
  115. if ( flags & NEEDS_FLUSH ) {
  116. *end = 0;
  117. flags ^= NEEDS_FLUSH;
  118. if ( flags ) {
  119. char* p = start; // the read pointer
  120. char* q = start; // the write pointer
  121. while( p < end ) {
  122. if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
  123. // CR-LF pair becomes LF
  124. // CR alone becomes LF
  125. // LF-CR becomes LF
  126. if ( *(p+1) == LF ) {
  127. p += 2;
  128. }
  129. else {
  130. ++p;
  131. }
  132. *q++ = LF;
  133. }
  134. else if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
  135. if ( *(p+1) == CR ) {
  136. p += 2;
  137. }
  138. else {
  139. ++p;
  140. }
  141. *q++ = LF;
  142. }
  143. else if ( (flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
  144. int i=0;
  145. // Entities handled by tinyXML2:
  146. // - special entities in the entity table [in/out]
  147. // - numeric character reference [in]
  148. // &#20013; or &#x4e2d;
  149. if ( *(p+1) == '#' ) {
  150. char buf[10] = { 0 };
  151. int len;
  152. p = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
  153. for( int i=0; i<len; ++i ) {
  154. *q++ = buf[i];
  155. }
  156. TIXMLASSERT( q <= p );
  157. }
  158. else {
  159. for( i=0; i<NUM_ENTITIES; ++i ) {
  160. if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
  161. && *(p+entities[i].length+1) == ';' )
  162. {
  163. // Found an entity convert;
  164. *q = entities[i].value;
  165. ++q;
  166. p += entities[i].length + 2;
  167. break;
  168. }
  169. }
  170. if ( i == NUM_ENTITIES ) {
  171. // fixme: treat as error?
  172. ++p;
  173. ++q;
  174. }
  175. }
  176. }
  177. else {
  178. *q = *p;
  179. ++p;
  180. ++q;
  181. }
  182. }
  183. *q = 0;
  184. }
  185. flags = (flags & NEEDS_DELETE);
  186. }
  187. return start;
  188. }
  189. // --------- XMLUtil ----------- //
  190. const char* XMLUtil::ReadBOM( const char* p, bool* bom )
  191. {
  192. *bom = false;
  193. const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
  194. // Check for BOM:
  195. if ( *(pu+0) == TIXML_UTF_LEAD_0
  196. && *(pu+1) == TIXML_UTF_LEAD_1
  197. && *(pu+2) == TIXML_UTF_LEAD_2 )
  198. {
  199. *bom = true;
  200. p += 3;
  201. }
  202. return p;
  203. }
  204. void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
  205. {
  206. const unsigned long BYTE_MASK = 0xBF;
  207. const unsigned long BYTE_MARK = 0x80;
  208. const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  209. if (input < 0x80)
  210. *length = 1;
  211. else if ( input < 0x800 )
  212. *length = 2;
  213. else if ( input < 0x10000 )
  214. *length = 3;
  215. else if ( input < 0x200000 )
  216. *length = 4;
  217. else
  218. { *length = 0; return; } // This code won't covert this correctly anyway.
  219. output += *length;
  220. // Scary scary fall throughs.
  221. switch (*length)
  222. {
  223. case 4:
  224. --output;
  225. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  226. input >>= 6;
  227. case 3:
  228. --output;
  229. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  230. input >>= 6;
  231. case 2:
  232. --output;
  233. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  234. input >>= 6;
  235. case 1:
  236. --output;
  237. *output = (char)(input | FIRST_BYTE_MARK[*length]);
  238. }
  239. }
  240. const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
  241. {
  242. // Presume an entity, and pull it out.
  243. *length = 0;
  244. if ( *(p+1) == '#' && *(p+2) )
  245. {
  246. unsigned long ucs = 0;
  247. ptrdiff_t delta = 0;
  248. unsigned mult = 1;
  249. if ( *(p+2) == 'x' )
  250. {
  251. // Hexadecimal.
  252. if ( !*(p+3) ) return 0;
  253. const char* q = p+3;
  254. q = strchr( q, ';' );
  255. if ( !q || !*q ) return 0;
  256. delta = q-p;
  257. --q;
  258. while ( *q != 'x' )
  259. {
  260. if ( *q >= '0' && *q <= '9' )
  261. ucs += mult * (*q - '0');
  262. else if ( *q >= 'a' && *q <= 'f' )
  263. ucs += mult * (*q - 'a' + 10);
  264. else if ( *q >= 'A' && *q <= 'F' )
  265. ucs += mult * (*q - 'A' + 10 );
  266. else
  267. return 0;
  268. mult *= 16;
  269. --q;
  270. }
  271. }
  272. else
  273. {
  274. // Decimal.
  275. if ( !*(p+2) ) return 0;
  276. const char* q = p+2;
  277. q = strchr( q, ';' );
  278. if ( !q || !*q ) return 0;
  279. delta = q-p;
  280. --q;
  281. while ( *q != '#' )
  282. {
  283. if ( *q >= '0' && *q <= '9' )
  284. ucs += mult * (*q - '0');
  285. else
  286. return 0;
  287. mult *= 10;
  288. --q;
  289. }
  290. }
  291. // convert the UCS to UTF-8
  292. ConvertUTF32ToUTF8( ucs, value, length );
  293. return p + delta + 1;
  294. }
  295. return p+1;
  296. }
  297. char* XMLDocument::Identify( char* p, XMLNode** node )
  298. {
  299. XMLNode* returnNode = 0;
  300. char* start = p;
  301. p = XMLUtil::SkipWhiteSpace( p );
  302. if( !p || !*p )
  303. {
  304. return 0;
  305. }
  306. // What is this thing?
  307. // - Elements start with a letter or underscore, but xml is reserved.
  308. // - Comments: <!--
  309. // - Decleration: <?
  310. // - Everthing else is unknown to tinyxml.
  311. //
  312. static const char* xmlHeader = { "<?" };
  313. static const char* commentHeader = { "<!--" };
  314. static const char* dtdHeader = { "<!" };
  315. static const char* cdataHeader = { "<![CDATA[" };
  316. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  317. static const int xmlHeaderLen = 2;
  318. static const int commentHeaderLen = 4;
  319. static const int dtdHeaderLen = 2;
  320. static const int cdataHeaderLen = 9;
  321. static const int elementHeaderLen = 1;
  322. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
  323. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
  324. if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
  325. returnNode = new (commentPool.Alloc()) XMLDeclaration( this );
  326. returnNode->memPool = &commentPool;
  327. p += xmlHeaderLen;
  328. }
  329. else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  330. returnNode = new (commentPool.Alloc()) XMLComment( this );
  331. returnNode->memPool = &commentPool;
  332. p += commentHeaderLen;
  333. }
  334. else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
  335. XMLText* text = new (textPool.Alloc()) XMLText( this );
  336. returnNode = text;
  337. returnNode->memPool = &textPool;
  338. p += cdataHeaderLen;
  339. text->SetCData( true );
  340. }
  341. else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
  342. returnNode = new (commentPool.Alloc()) XMLUnknown( this );
  343. returnNode->memPool = &commentPool;
  344. p += dtdHeaderLen;
  345. }
  346. else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
  347. returnNode = new (elementPool.Alloc()) XMLElement( this );
  348. returnNode->memPool = &elementPool;
  349. p += elementHeaderLen;
  350. p = XMLUtil::SkipWhiteSpace( p );
  351. if ( p && *p == '/' ) {
  352. ((XMLElement*)returnNode)->closingType = XMLElement::CLOSING;
  353. }
  354. }
  355. else {
  356. returnNode = new (textPool.Alloc()) XMLText( this );
  357. returnNode->memPool = &textPool;
  358. p = start; // Back it up, all the text counts.
  359. }
  360. *node = returnNode;
  361. return p;
  362. }
  363. bool XMLDocument::Accept( XMLVisitor* visitor ) const
  364. {
  365. if ( visitor->VisitEnter( *this ) )
  366. {
  367. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  368. {
  369. if ( !node->Accept( visitor ) )
  370. break;
  371. }
  372. }
  373. return visitor->VisitExit( *this );
  374. }
  375. // --------- XMLNode ----------- //
  376. XMLNode::XMLNode( XMLDocument* doc ) :
  377. document( doc ),
  378. parent( 0 ),
  379. firstChild( 0 ), lastChild( 0 ),
  380. prev( 0 ), next( 0 )
  381. {
  382. }
  383. XMLNode::~XMLNode()
  384. {
  385. ClearChildren();
  386. if ( parent ) {
  387. parent->Unlink( this );
  388. }
  389. }
  390. void XMLNode::SetValue( const char* str, bool staticMem )
  391. {
  392. if ( staticMem )
  393. value.SetInternedStr( str );
  394. else
  395. value.SetStr( str );
  396. }
  397. void XMLNode::ClearChildren()
  398. {
  399. while( firstChild ) {
  400. XMLNode* node = firstChild;
  401. Unlink( node );
  402. DELETE_NODE( node );
  403. }
  404. firstChild = lastChild = 0;
  405. }
  406. void XMLNode::Unlink( XMLNode* child )
  407. {
  408. TIXMLASSERT( child->parent == this );
  409. if ( child == firstChild )
  410. firstChild = firstChild->next;
  411. if ( child == lastChild )
  412. lastChild = lastChild->prev;
  413. if ( child->prev ) {
  414. child->prev->next = child->next;
  415. }
  416. if ( child->next ) {
  417. child->next->prev = child->prev;
  418. }
  419. child->parent = 0;
  420. }
  421. void XMLNode::DeleteChild( XMLNode* node )
  422. {
  423. TIXMLASSERT( node->parent == this );
  424. DELETE_NODE( node );
  425. }
  426. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  427. {
  428. if ( lastChild ) {
  429. TIXMLASSERT( firstChild );
  430. TIXMLASSERT( lastChild->next == 0 );
  431. lastChild->next = addThis;
  432. addThis->prev = lastChild;
  433. lastChild = addThis;
  434. addThis->next = 0;
  435. }
  436. else {
  437. TIXMLASSERT( firstChild == 0 );
  438. firstChild = lastChild = addThis;
  439. addThis->prev = 0;
  440. addThis->next = 0;
  441. }
  442. addThis->parent = this;
  443. return addThis;
  444. }
  445. XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
  446. {
  447. if ( firstChild ) {
  448. TIXMLASSERT( lastChild );
  449. TIXMLASSERT( firstChild->prev == 0 );
  450. firstChild->prev = addThis;
  451. addThis->next = firstChild;
  452. firstChild = addThis;
  453. addThis->prev = 0;
  454. }
  455. else {
  456. TIXMLASSERT( lastChild == 0 );
  457. firstChild = lastChild = addThis;
  458. addThis->prev = 0;
  459. addThis->next = 0;
  460. }
  461. addThis->parent = this;
  462. return addThis;
  463. }
  464. XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
  465. {
  466. TIXMLASSERT( afterThis->parent == this );
  467. if ( afterThis->parent != this )
  468. return 0;
  469. if ( afterThis->next == 0 ) {
  470. // The last node or the only node.
  471. return InsertEndChild( addThis );
  472. }
  473. addThis->prev = afterThis;
  474. addThis->next = afterThis->next;
  475. afterThis->next->prev = addThis;
  476. afterThis->next = addThis;
  477. addThis->parent = this;
  478. return addThis;
  479. }
  480. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  481. {
  482. for( XMLNode* node=firstChild; node; node=node->next ) {
  483. XMLElement* element = node->ToElement();
  484. if ( element ) {
  485. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  486. return element;
  487. }
  488. }
  489. }
  490. return 0;
  491. }
  492. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  493. {
  494. for( XMLNode* node=lastChild; node; node=node->prev ) {
  495. XMLElement* element = node->ToElement();
  496. if ( element ) {
  497. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  498. return element;
  499. }
  500. }
  501. }
  502. return 0;
  503. }
  504. char* XMLNode::ParseDeep( char* p )
  505. {
  506. // This is a recursive method, but thinking about it "at the current level"
  507. // it is a pretty simple flat list:
  508. // <foo/>
  509. // <!-- comment -->
  510. //
  511. // With a special case:
  512. // <foo>
  513. // </foo>
  514. // <!-- comment -->
  515. //
  516. // Where the closing element (/foo) *must* be the next thing after the opening
  517. // element, and the names must match. BUT the tricky bit is that the closing
  518. // element will be read by the child.
  519. while( p && *p ) {
  520. XMLNode* node = 0;
  521. char* mark = p;
  522. p = document->Identify( p, &node );
  523. if ( p == 0 ) {
  524. break;
  525. }
  526. // We read the end tag. Back up and return.
  527. if ( node && node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
  528. DELETE_NODE( node );
  529. return mark;
  530. }
  531. if ( node ) {
  532. p = node->ParseDeep( p );
  533. if ( !p ) {
  534. DELETE_NODE( node );
  535. node = 0;
  536. break;
  537. }
  538. XMLElement* ele = node->ToElement();
  539. if ( ele && ele->ClosingType() == XMLElement::OPEN ) {
  540. XMLNode* closingNode = 0;
  541. p = document->Identify( p, &closingNode );
  542. XMLElement* closingEle = closingNode ? closingNode->ToElement() : 0;
  543. if ( closingEle == 0 ) {
  544. document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  545. p = 0;
  546. }
  547. else if ( closingEle->ClosingType() != XMLElement::CLOSING ) {
  548. document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  549. p = 0;
  550. }
  551. else
  552. {
  553. p = closingEle->ParseDeep( p );
  554. if ( !XMLUtil::StringEqual( closingEle->Value(), node->Value() )) {
  555. document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  556. p = 0;
  557. }
  558. }
  559. // Else everything is fine, but we need to throw away the node.
  560. DELETE_NODE( closingNode );
  561. if ( p == 0 ) {
  562. DELETE_NODE( node );
  563. node = 0;
  564. }
  565. }
  566. if ( node ) {
  567. this->InsertEndChild( node );
  568. }
  569. }
  570. }
  571. return 0;
  572. }
  573. // --------- XMLText ---------- //
  574. char* XMLText::ParseDeep( char* p )
  575. {
  576. const char* start = p;
  577. if ( this->CData() ) {
  578. p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  579. if ( !p ) {
  580. document->SetError( ERROR_PARSING_CDATA, start, 0 );
  581. }
  582. return p;
  583. }
  584. else {
  585. p = value.ParseText( p, "<", StrPair::TEXT_ELEMENT );
  586. if ( !p ) {
  587. document->SetError( ERROR_PARSING_TEXT, start, 0 );
  588. }
  589. if ( p && *p ) {
  590. return p-1;
  591. }
  592. }
  593. return 0;
  594. }
  595. bool XMLText::Accept( XMLVisitor* visitor ) const
  596. {
  597. return visitor->Visit( *this );
  598. }
  599. // --------- XMLComment ---------- //
  600. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  601. {
  602. }
  603. XMLComment::~XMLComment()
  604. {
  605. //printf( "~XMLComment\n" );
  606. }
  607. char* XMLComment::ParseDeep( char* p )
  608. {
  609. // Comment parses as text.
  610. const char* start = p;
  611. p = value.ParseText( p, "-->", StrPair::COMMENT );
  612. if ( p == 0 ) {
  613. document->SetError( ERROR_PARSING_COMMENT, start, 0 );
  614. }
  615. return p;
  616. }
  617. bool XMLComment::Accept( XMLVisitor* visitor ) const
  618. {
  619. return visitor->Visit( *this );
  620. }
  621. // --------- XMLDeclaration ---------- //
  622. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  623. {
  624. }
  625. XMLDeclaration::~XMLDeclaration()
  626. {
  627. //printf( "~XMLDeclaration\n" );
  628. }
  629. char* XMLDeclaration::ParseDeep( char* p )
  630. {
  631. // Declaration parses as text.
  632. const char* start = p;
  633. p = value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  634. if ( p == 0 ) {
  635. document->SetError( ERROR_PARSING_DECLARATION, start, 0 );
  636. }
  637. return p;
  638. }
  639. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  640. {
  641. return visitor->Visit( *this );
  642. }
  643. // --------- XMLUnknown ---------- //
  644. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  645. {
  646. }
  647. XMLUnknown::~XMLUnknown()
  648. {
  649. }
  650. char* XMLUnknown::ParseDeep( char* p )
  651. {
  652. // Unknown parses as text.
  653. const char* start = p;
  654. p = value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  655. if ( !p ) {
  656. document->SetError( ERROR_PARSING_UNKNOWN, start, 0 );
  657. }
  658. return p;
  659. }
  660. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  661. {
  662. return visitor->Visit( *this );
  663. }
  664. // --------- XMLAttribute ---------- //
  665. char* XMLAttribute::ParseDeep( char* p )
  666. {
  667. p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
  668. if ( !p || !*p ) return 0;
  669. char endTag[2] = { *p, 0 };
  670. ++p;
  671. p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
  672. //if ( value.Empty() ) return 0;
  673. return p;
  674. }
  675. void XMLAttribute::SetName( const char* n )
  676. {
  677. name.SetStr( n );
  678. }
  679. int XMLAttribute::QueryIntAttribute( int* value ) const
  680. {
  681. if ( TIXML_SSCANF( Value(), "%d", value ) == 1 )
  682. return XML_NO_ERROR;
  683. return WRONG_ATTRIBUTE_TYPE;
  684. }
  685. int XMLAttribute::QueryUnsignedAttribute( unsigned int* value ) const
  686. {
  687. if ( TIXML_SSCANF( Value(), "%u", value ) == 1 )
  688. return XML_NO_ERROR;
  689. return WRONG_ATTRIBUTE_TYPE;
  690. }
  691. int XMLAttribute::QueryBoolAttribute( bool* value ) const
  692. {
  693. int ival = -1;
  694. QueryIntAttribute( &ival );
  695. if ( ival > 0 || XMLUtil::StringEqual( Value(), "true" ) ) {
  696. *value = true;
  697. return XML_NO_ERROR;
  698. }
  699. else if ( ival == 0 || XMLUtil::StringEqual( Value(), "false" ) ) {
  700. *value = false;
  701. return XML_NO_ERROR;
  702. }
  703. return WRONG_ATTRIBUTE_TYPE;
  704. }
  705. int XMLAttribute::QueryDoubleAttribute( double* value ) const
  706. {
  707. if ( TIXML_SSCANF( Value(), "%lf", value ) == 1 )
  708. return XML_NO_ERROR;
  709. return WRONG_ATTRIBUTE_TYPE;
  710. }
  711. int XMLAttribute::QueryFloatAttribute( float* value ) const
  712. {
  713. if ( TIXML_SSCANF( Value(), "%f", value ) == 1 )
  714. return XML_NO_ERROR;
  715. return WRONG_ATTRIBUTE_TYPE;
  716. }
  717. void XMLAttribute::SetAttribute( const char* v )
  718. {
  719. value.SetStr( v );
  720. }
  721. void XMLAttribute::SetAttribute( int v )
  722. {
  723. char buf[BUF_SIZE];
  724. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v );
  725. value.SetStr( buf );
  726. }
  727. void XMLAttribute::SetAttribute( unsigned v )
  728. {
  729. char buf[BUF_SIZE];
  730. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%u", v );
  731. value.SetStr( buf );
  732. }
  733. void XMLAttribute::SetAttribute( bool v )
  734. {
  735. char buf[BUF_SIZE];
  736. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v ? 1 : 0 );
  737. value.SetStr( buf );
  738. }
  739. void XMLAttribute::SetAttribute( double v )
  740. {
  741. char buf[BUF_SIZE];
  742. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  743. value.SetStr( buf );
  744. }
  745. void XMLAttribute::SetAttribute( float v )
  746. {
  747. char buf[BUF_SIZE];
  748. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  749. value.SetStr( buf );
  750. }
  751. // --------- XMLElement ---------- //
  752. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  753. closingType( 0 ),
  754. rootAttribute( 0 )
  755. {
  756. }
  757. XMLElement::~XMLElement()
  758. {
  759. while( rootAttribute ) {
  760. XMLAttribute* next = rootAttribute->next;
  761. DELETE_ATTRIBUTE( rootAttribute );
  762. rootAttribute = next;
  763. }
  764. }
  765. XMLAttribute* XMLElement::FindAttribute( const char* name )
  766. {
  767. XMLAttribute* a = 0;
  768. for( a=rootAttribute; a; a = a->next ) {
  769. if ( XMLUtil::StringEqual( a->Name(), name ) )
  770. return a;
  771. }
  772. return 0;
  773. }
  774. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  775. {
  776. XMLAttribute* a = 0;
  777. for( a=rootAttribute; a; a = a->next ) {
  778. if ( XMLUtil::StringEqual( a->Name(), name ) )
  779. return a;
  780. }
  781. return 0;
  782. }
  783. const char* XMLElement::GetText() const
  784. {
  785. if ( FirstChild() && FirstChild()->ToText() ) {
  786. return FirstChild()->ToText()->Value();
  787. }
  788. return 0;
  789. }
  790. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  791. {
  792. XMLAttribute* attrib = FindAttribute( name );
  793. if ( !attrib ) {
  794. attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  795. attrib->memPool = &document->attributePool;
  796. LinkAttribute( attrib );
  797. attrib->SetName( name );
  798. }
  799. return attrib;
  800. }
  801. void XMLElement::LinkAttribute( XMLAttribute* attrib )
  802. {
  803. if ( rootAttribute ) {
  804. XMLAttribute* end = rootAttribute;
  805. while ( end->next )
  806. end = end->next;
  807. end->next = attrib;
  808. }
  809. else {
  810. rootAttribute = attrib;
  811. }
  812. }
  813. void XMLElement::DeleteAttribute( const char* name )
  814. {
  815. XMLAttribute* prev = 0;
  816. for( XMLAttribute* a=rootAttribute; a; a=a->next ) {
  817. if ( XMLUtil::StringEqual( name, a->Name() ) ) {
  818. if ( prev ) {
  819. prev->next = a->next;
  820. }
  821. else {
  822. rootAttribute = a->next;
  823. }
  824. DELETE_ATTRIBUTE( a );
  825. break;
  826. }
  827. prev = a;
  828. }
  829. }
  830. char* XMLElement::ParseAttributes( char* p )
  831. {
  832. const char* start = p;
  833. // Read the attributes.
  834. while( p ) {
  835. p = XMLUtil::SkipWhiteSpace( p );
  836. if ( !p || !(*p) ) {
  837. document->SetError( ERROR_PARSING_ELEMENT, start, Name() );
  838. return 0;
  839. }
  840. // attribute.
  841. if ( XMLUtil::IsAlpha( *p ) ) {
  842. XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  843. attrib->memPool = &document->attributePool;
  844. p = attrib->ParseDeep( p );
  845. if ( !p || Attribute( attrib->Name() ) ) {
  846. DELETE_ATTRIBUTE( attrib );
  847. document->SetError( ERROR_PARSING_ATTRIBUTE, start, p );
  848. return 0;
  849. }
  850. LinkAttribute( attrib );
  851. }
  852. // end of the tag
  853. else if ( *p == '/' && *(p+1) == '>' ) {
  854. closingType = CLOSED;
  855. return p+2; // done; sealed element.
  856. }
  857. // end of the tag
  858. else if ( *p == '>' ) {
  859. ++p;
  860. break;
  861. }
  862. else {
  863. document->SetError( ERROR_PARSING_ELEMENT, start, p );
  864. return 0;
  865. }
  866. }
  867. return p;
  868. }
  869. //
  870. // <ele></ele>
  871. // <ele>foo<b>bar</b></ele>
  872. //
  873. char* XMLElement::ParseDeep( char* p )
  874. {
  875. // Read the element name.
  876. p = XMLUtil::SkipWhiteSpace( p );
  877. if ( !p ) return 0;
  878. const char* start = p;
  879. // The closing element is the </element> form. It is
  880. // parsed just like a regular element then deleted from
  881. // the DOM.
  882. if ( *p == '/' ) {
  883. closingType = CLOSING;
  884. ++p;
  885. }
  886. p = value.ParseName( p );
  887. if ( value.Empty() ) return 0;
  888. bool elementClosed=false;
  889. p = ParseAttributes( p );
  890. if ( !p || !*p || closingType )
  891. return p;
  892. p = XMLNode::ParseDeep( p );
  893. return p;
  894. }
  895. bool XMLElement::Accept( XMLVisitor* visitor ) const
  896. {
  897. if ( visitor->VisitEnter( *this, rootAttribute ) )
  898. {
  899. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  900. {
  901. if ( !node->Accept( visitor ) )
  902. break;
  903. }
  904. }
  905. return visitor->VisitExit( *this );
  906. }
  907. // --------- XMLDocument ----------- //
  908. XMLDocument::XMLDocument() :
  909. XMLNode( 0 ),
  910. writeBOM( false ),
  911. charBuffer( 0 )
  912. {
  913. document = this; // avoid warning about 'this' in initializer list
  914. }
  915. XMLDocument::~XMLDocument()
  916. {
  917. ClearChildren();
  918. delete [] charBuffer;
  919. #if 0
  920. textPool.Trace( "text" );
  921. elementPool.Trace( "element" );
  922. commentPool.Trace( "comment" );
  923. attributePool.Trace( "attribute" );
  924. #endif
  925. TIXMLASSERT( textPool.CurrentAllocs() == 0 );
  926. TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
  927. TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
  928. TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
  929. }
  930. void XMLDocument::InitDocument()
  931. {
  932. errorID = XML_NO_ERROR;
  933. errorStr1 = 0;
  934. errorStr2 = 0;
  935. delete [] charBuffer;
  936. charBuffer = 0;
  937. }
  938. XMLElement* XMLDocument::NewElement( const char* name )
  939. {
  940. XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
  941. ele->memPool = &elementPool;
  942. ele->SetName( name );
  943. return ele;
  944. }
  945. XMLComment* XMLDocument::NewComment( const char* str )
  946. {
  947. XMLComment* comment = new (commentPool.Alloc()) XMLComment( this );
  948. comment->memPool = &commentPool;
  949. comment->SetValue( str );
  950. return comment;
  951. }
  952. XMLText* XMLDocument::NewText( const char* str )
  953. {
  954. XMLText* text = new (textPool.Alloc()) XMLText( this );
  955. text->memPool = &textPool;
  956. text->SetValue( str );
  957. return text;
  958. }
  959. int XMLDocument::LoadFile( const char* filename )
  960. {
  961. ClearChildren();
  962. InitDocument();
  963. FILE* fp = fopen( filename, "rb" );
  964. if ( !fp ) {
  965. SetError( ERROR_FILE_NOT_FOUND, filename, 0 );
  966. return errorID;
  967. }
  968. LoadFile( fp );
  969. fclose( fp );
  970. return errorID;
  971. }
  972. int XMLDocument::LoadFile( FILE* fp )
  973. {
  974. ClearChildren();
  975. InitDocument();
  976. fseek( fp, 0, SEEK_END );
  977. unsigned size = ftell( fp );
  978. fseek( fp, 0, SEEK_SET );
  979. if ( size == 0 ) {
  980. return errorID;
  981. }
  982. charBuffer = new char[size+1];
  983. fread( charBuffer, size, 1, fp );
  984. charBuffer[size] = 0;
  985. const char* p = charBuffer;
  986. p = XMLUtil::SkipWhiteSpace( p );
  987. p = XMLUtil::ReadBOM( p, &writeBOM );
  988. if ( !p || !*p ) {
  989. SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
  990. return errorID;
  991. }
  992. ParseDeep( charBuffer + (p-charBuffer) );
  993. return errorID;
  994. }
  995. void XMLDocument::SaveFile( const char* filename )
  996. {
  997. FILE* fp = fopen( filename, "w" );
  998. XMLStreamer stream( fp );
  999. Print( &stream );
  1000. fclose( fp );
  1001. }
  1002. int XMLDocument::Parse( const char* p )
  1003. {
  1004. ClearChildren();
  1005. InitDocument();
  1006. if ( !p || !*p ) {
  1007. SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
  1008. return errorID;
  1009. }
  1010. p = XMLUtil::SkipWhiteSpace( p );
  1011. p = XMLUtil::ReadBOM( p, &writeBOM );
  1012. if ( !p || !*p ) {
  1013. SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
  1014. return errorID;
  1015. }
  1016. size_t len = strlen( p );
  1017. charBuffer = new char[ len+1 ];
  1018. memcpy( charBuffer, p, len+1 );
  1019. ParseDeep( charBuffer );
  1020. return errorID;
  1021. }
  1022. void XMLDocument::Print( XMLStreamer* streamer )
  1023. {
  1024. XMLStreamer stdStreamer( stdout );
  1025. if ( !streamer )
  1026. streamer = &stdStreamer;
  1027. Accept( streamer );
  1028. }
  1029. void XMLDocument::SetError( int error, const char* str1, const char* str2 )
  1030. {
  1031. errorID = error;
  1032. errorStr1 = str1;
  1033. errorStr2 = str2;
  1034. }
  1035. void XMLDocument::PrintError() const
  1036. {
  1037. if ( errorID ) {
  1038. char buf1[20] = { 0 };
  1039. char buf2[20] = { 0 };
  1040. if ( errorStr1 ) {
  1041. strncpy( buf1, errorStr1, 20 );
  1042. buf1[19] = 0;
  1043. }
  1044. if ( errorStr2 ) {
  1045. strncpy( buf2, errorStr2, 20 );
  1046. buf2[19] = 0;
  1047. }
  1048. printf( "XMLDocument error id=%d str1=%s str2=%s\n",
  1049. errorID, buf1, buf2 );
  1050. }
  1051. }
  1052. XMLStreamer::XMLStreamer( FILE* file ) :
  1053. elementJustOpened( false ),
  1054. firstElement( true ),
  1055. fp( file ),
  1056. depth( 0 ),
  1057. textDepth( -1 )
  1058. {
  1059. for( int i=0; i<ENTITY_RANGE; ++i ) {
  1060. entityFlag[i] = false;
  1061. restrictedEntityFlag[i] = false;
  1062. }
  1063. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1064. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  1065. if ( entities[i].value < ENTITY_RANGE ) {
  1066. entityFlag[ entities[i].value ] = true;
  1067. }
  1068. }
  1069. restrictedEntityFlag['&'] = true;
  1070. restrictedEntityFlag['<'] = true;
  1071. restrictedEntityFlag['>'] = true; // not required, but consistency is nice
  1072. buffer.Push( 0 );
  1073. }
  1074. void XMLStreamer::Print( const char* format, ... )
  1075. {
  1076. va_list va;
  1077. va_start( va, format );
  1078. if ( fp ) {
  1079. vfprintf( fp, format, va );
  1080. }
  1081. else {
  1082. // This seems brutally complex. Haven't figured out a better
  1083. // way on windows.
  1084. #ifdef _MSC_VER
  1085. int len = -1;
  1086. int expand = 1000;
  1087. while ( len < 0 ) {
  1088. len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), accumulator.Capacity()-1, format, va );
  1089. if ( len < 0 ) {
  1090. accumulator.PushArr( expand );
  1091. expand *= 3/2;
  1092. }
  1093. }
  1094. char* p = buffer.PushArr( len ) - 1;
  1095. memcpy( p, accumulator.Mem(), len+1 );
  1096. #else
  1097. int len = vsnprintf( 0, 0, format, va );
  1098. char* p = buffer.PushArr( len ) - 1;
  1099. vsprintf_s( p, len+1, format, va );
  1100. #endif
  1101. }
  1102. va_end( va );
  1103. }
  1104. void XMLStreamer::PrintSpace( int depth )
  1105. {
  1106. for( int i=0; i<depth; ++i ) {
  1107. Print( " " );
  1108. }
  1109. }
  1110. void XMLStreamer::PrintString( const char* p, bool restricted )
  1111. {
  1112. // Look for runs of bytes between entities to print.
  1113. const char* q = p;
  1114. const bool* flag = restricted ? restrictedEntityFlag : entityFlag;
  1115. while ( *q ) {
  1116. // Remember, char is sometimes signed. (How many times has that bitten me?)
  1117. if ( *q > 0 && *q < ENTITY_RANGE ) {
  1118. // Check for entities. If one is found, flush
  1119. // the stream up until the entity, write the
  1120. // entity, and keep looking.
  1121. if ( flag[*q] ) {
  1122. while ( p < q ) {
  1123. Print( "%c", *p );
  1124. ++p;
  1125. }
  1126. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1127. if ( entities[i].value == *q ) {
  1128. Print( "&%s;", entities[i].pattern );
  1129. break;
  1130. }
  1131. }
  1132. ++p;
  1133. }
  1134. }
  1135. ++q;
  1136. }
  1137. // Flush the remaining string. This will be the entire
  1138. // string if an entity wasn't found.
  1139. if ( q-p > 0 ) {
  1140. Print( "%s", p );
  1141. }
  1142. }
  1143. void XMLStreamer::PushHeader( bool writeBOM, bool writeDec )
  1144. {
  1145. static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
  1146. if ( writeBOM ) {
  1147. Print( "%s", bom );
  1148. }
  1149. if ( writeDec ) {
  1150. PushDeclaration( "xml version=\"1.0\"" );
  1151. }
  1152. }
  1153. void XMLStreamer::OpenElement( const char* name )
  1154. {
  1155. if ( elementJustOpened ) {
  1156. SealElement();
  1157. }
  1158. stack.Push( name );
  1159. if ( textDepth < 0 && !firstElement ) {
  1160. Print( "\n" );
  1161. PrintSpace( depth );
  1162. }
  1163. Print( "<%s", name );
  1164. elementJustOpened = true;
  1165. firstElement = false;
  1166. ++depth;
  1167. }
  1168. void XMLStreamer::PushAttribute( const char* name, const char* value )
  1169. {
  1170. TIXMLASSERT( elementJustOpened );
  1171. Print( " %s=\"", name );
  1172. PrintString( value, false );
  1173. Print( "\"" );
  1174. }
  1175. void XMLStreamer::CloseElement()
  1176. {
  1177. --depth;
  1178. const char* name = stack.Pop();
  1179. if ( elementJustOpened ) {
  1180. Print( "/>" );
  1181. }
  1182. else {
  1183. if ( textDepth < 0 ) {
  1184. Print( "\n" );
  1185. PrintSpace( depth );
  1186. }
  1187. Print( "</%s>", name );
  1188. }
  1189. if ( textDepth == depth )
  1190. textDepth = -1;
  1191. if ( depth == 0 )
  1192. Print( "\n" );
  1193. elementJustOpened = false;
  1194. }
  1195. void XMLStreamer::SealElement()
  1196. {
  1197. elementJustOpened = false;
  1198. Print( ">" );
  1199. }
  1200. void XMLStreamer::PushText( const char* text, bool cdata )
  1201. {
  1202. textDepth = depth-1;
  1203. if ( elementJustOpened ) {
  1204. SealElement();
  1205. }
  1206. if ( cdata ) {
  1207. Print( "<![CDATA[" );
  1208. Print( "%s", text );
  1209. Print( "]]>" );
  1210. }
  1211. else {
  1212. PrintString( text, true );
  1213. }
  1214. }
  1215. void XMLStreamer::PushComment( const char* comment )
  1216. {
  1217. if ( elementJustOpened ) {
  1218. SealElement();
  1219. }
  1220. if ( textDepth < 0 && !firstElement ) {
  1221. Print( "\n" );
  1222. PrintSpace( depth );
  1223. }
  1224. firstElement = false;
  1225. Print( "<!--%s-->", comment );
  1226. }
  1227. void XMLStreamer::PushDeclaration( const char* value )
  1228. {
  1229. if ( elementJustOpened ) {
  1230. SealElement();
  1231. }
  1232. if ( textDepth < 0 && !firstElement) {
  1233. Print( "\n" );
  1234. PrintSpace( depth );
  1235. }
  1236. firstElement = false;
  1237. Print( "<?%s?>", value );
  1238. }
  1239. void XMLStreamer::PushUnknown( const char* value )
  1240. {
  1241. if ( elementJustOpened ) {
  1242. SealElement();
  1243. }
  1244. if ( textDepth < 0 && !firstElement ) {
  1245. Print( "\n" );
  1246. PrintSpace( depth );
  1247. }
  1248. firstElement = false;
  1249. Print( "<!%s>", value );
  1250. }
  1251. bool XMLStreamer::VisitEnter( const XMLDocument& doc )
  1252. {
  1253. if ( doc.HasBOM() ) {
  1254. PushHeader( true, false );
  1255. }
  1256. return true;
  1257. }
  1258. bool XMLStreamer::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  1259. {
  1260. OpenElement( element.Name() );
  1261. while ( attribute ) {
  1262. PushAttribute( attribute->Name(), attribute->Value() );
  1263. attribute = attribute->Next();
  1264. }
  1265. return true;
  1266. }
  1267. bool XMLStreamer::VisitExit( const XMLElement& element )
  1268. {
  1269. CloseElement();
  1270. return true;
  1271. }
  1272. bool XMLStreamer::Visit( const XMLText& text )
  1273. {
  1274. PushText( text.Value(), text.CData() );
  1275. return true;
  1276. }
  1277. bool XMLStreamer::Visit( const XMLComment& comment )
  1278. {
  1279. PushComment( comment.Value() );
  1280. return true;
  1281. }
  1282. bool XMLStreamer::Visit( const XMLDeclaration& declaration )
  1283. {
  1284. PushDeclaration( declaration.Value() );
  1285. return true;
  1286. }
  1287. bool XMLStreamer::Visit( const XMLUnknown& unknown )
  1288. {
  1289. PushUnknown( unknown.Value() );
  1290. return true;
  1291. }