tinyxml2.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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 p;
  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. }
  351. else {
  352. returnNode = new (textPool.Alloc()) XMLText( this );
  353. returnNode->memPool = &textPool;
  354. p = start; // Back it up, all the text counts.
  355. }
  356. *node = returnNode;
  357. return p;
  358. }
  359. bool XMLDocument::Accept( XMLVisitor* visitor ) const
  360. {
  361. if ( visitor->VisitEnter( *this ) )
  362. {
  363. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  364. {
  365. if ( !node->Accept( visitor ) )
  366. break;
  367. }
  368. }
  369. return visitor->VisitExit( *this );
  370. }
  371. // --------- XMLNode ----------- //
  372. XMLNode::XMLNode( XMLDocument* doc ) :
  373. document( doc ),
  374. parent( 0 ),
  375. firstChild( 0 ), lastChild( 0 ),
  376. prev( 0 ), next( 0 )
  377. {
  378. }
  379. XMLNode::~XMLNode()
  380. {
  381. ClearChildren();
  382. if ( parent ) {
  383. parent->Unlink( this );
  384. }
  385. }
  386. void XMLNode::SetValue( const char* str, bool staticMem )
  387. {
  388. if ( staticMem )
  389. value.SetInternedStr( str );
  390. else
  391. value.SetStr( str );
  392. }
  393. void XMLNode::ClearChildren()
  394. {
  395. while( firstChild ) {
  396. XMLNode* node = firstChild;
  397. Unlink( node );
  398. DELETE_NODE( node );
  399. }
  400. firstChild = lastChild = 0;
  401. }
  402. void XMLNode::Unlink( XMLNode* child )
  403. {
  404. TIXMLASSERT( child->parent == this );
  405. if ( child == firstChild )
  406. firstChild = firstChild->next;
  407. if ( child == lastChild )
  408. lastChild = lastChild->prev;
  409. if ( child->prev ) {
  410. child->prev->next = child->next;
  411. }
  412. if ( child->next ) {
  413. child->next->prev = child->prev;
  414. }
  415. child->parent = 0;
  416. }
  417. void XMLNode::DeleteChild( XMLNode* node )
  418. {
  419. TIXMLASSERT( node->parent == this );
  420. DELETE_NODE( node );
  421. }
  422. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  423. {
  424. if ( lastChild ) {
  425. TIXMLASSERT( firstChild );
  426. TIXMLASSERT( lastChild->next == 0 );
  427. lastChild->next = addThis;
  428. addThis->prev = lastChild;
  429. lastChild = addThis;
  430. addThis->next = 0;
  431. }
  432. else {
  433. TIXMLASSERT( firstChild == 0 );
  434. firstChild = lastChild = addThis;
  435. addThis->prev = 0;
  436. addThis->next = 0;
  437. }
  438. addThis->parent = this;
  439. return addThis;
  440. }
  441. XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
  442. {
  443. if ( firstChild ) {
  444. TIXMLASSERT( lastChild );
  445. TIXMLASSERT( firstChild->prev == 0 );
  446. firstChild->prev = addThis;
  447. addThis->next = firstChild;
  448. firstChild = addThis;
  449. addThis->prev = 0;
  450. }
  451. else {
  452. TIXMLASSERT( lastChild == 0 );
  453. firstChild = lastChild = addThis;
  454. addThis->prev = 0;
  455. addThis->next = 0;
  456. }
  457. addThis->parent = this;
  458. return addThis;
  459. }
  460. XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
  461. {
  462. TIXMLASSERT( afterThis->parent == this );
  463. if ( afterThis->parent != this )
  464. return 0;
  465. if ( afterThis->next == 0 ) {
  466. // The last node or the only node.
  467. return InsertEndChild( addThis );
  468. }
  469. addThis->prev = afterThis;
  470. addThis->next = afterThis->next;
  471. afterThis->next->prev = addThis;
  472. afterThis->next = addThis;
  473. addThis->parent = this;
  474. return addThis;
  475. }
  476. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  477. {
  478. for( XMLNode* node=firstChild; node; node=node->next ) {
  479. XMLElement* element = node->ToElement();
  480. if ( element ) {
  481. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  482. return element;
  483. }
  484. }
  485. }
  486. return 0;
  487. }
  488. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  489. {
  490. for( XMLNode* node=lastChild; node; node=node->prev ) {
  491. XMLElement* element = node->ToElement();
  492. if ( element ) {
  493. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  494. return element;
  495. }
  496. }
  497. }
  498. return 0;
  499. }
  500. char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
  501. {
  502. // This is a recursive method, but thinking about it "at the current level"
  503. // it is a pretty simple flat list:
  504. // <foo/>
  505. // <!-- comment -->
  506. //
  507. // With a special case:
  508. // <foo>
  509. // </foo>
  510. // <!-- comment -->
  511. //
  512. // Where the closing element (/foo) *must* be the next thing after the opening
  513. // element, and the names must match. BUT the tricky bit is that the closing
  514. // element will be read by the child.
  515. //
  516. // 'endTag' is the end tag for this node, it is returned by a call to a child.
  517. // 'parentEnd' is the end tag for the parent, which is filled in and returned.
  518. while( p && *p ) {
  519. XMLNode* node = 0;
  520. p = document->Identify( p, &node );
  521. if ( p == 0 || node == 0 ) {
  522. break;
  523. }
  524. StrPair endTag;
  525. p = node->ParseDeep( p, &endTag );
  526. if ( !p ) {
  527. DELETE_NODE( node );
  528. node = 0;
  529. break;
  530. }
  531. // We read the end tag. Return it to the parent.
  532. if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
  533. if ( parentEnd ) {
  534. *parentEnd = ((XMLElement*)node)->value;
  535. }
  536. DELETE_NODE( node );
  537. return p;
  538. }
  539. // Handle an end tag returned to this level.
  540. // And handle a bunch of annoying errors.
  541. XMLElement* ele = node->ToElement();
  542. if ( ele ) {
  543. if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
  544. document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  545. p = 0;
  546. }
  547. else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) {
  548. document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  549. p = 0;
  550. }
  551. else if ( !endTag.Empty() ) {
  552. if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
  553. document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  554. p = 0;
  555. }
  556. }
  557. }
  558. if ( p == 0 ) {
  559. DELETE_NODE( node );
  560. node = 0;
  561. }
  562. if ( node ) {
  563. this->InsertEndChild( node );
  564. }
  565. }
  566. return 0;
  567. }
  568. // --------- XMLText ---------- //
  569. char* XMLText::ParseDeep( char* p, StrPair* )
  570. {
  571. const char* start = p;
  572. if ( this->CData() ) {
  573. p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  574. if ( !p ) {
  575. document->SetError( ERROR_PARSING_CDATA, start, 0 );
  576. }
  577. return p;
  578. }
  579. else {
  580. p = value.ParseText( p, "<", StrPair::TEXT_ELEMENT );
  581. if ( !p ) {
  582. document->SetError( ERROR_PARSING_TEXT, start, 0 );
  583. }
  584. if ( p && *p ) {
  585. return p-1;
  586. }
  587. }
  588. return 0;
  589. }
  590. bool XMLText::Accept( XMLVisitor* visitor ) const
  591. {
  592. return visitor->Visit( *this );
  593. }
  594. // --------- XMLComment ---------- //
  595. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  596. {
  597. }
  598. XMLComment::~XMLComment()
  599. {
  600. //printf( "~XMLComment\n" );
  601. }
  602. char* XMLComment::ParseDeep( char* p, StrPair* )
  603. {
  604. // Comment parses as text.
  605. const char* start = p;
  606. p = value.ParseText( p, "-->", StrPair::COMMENT );
  607. if ( p == 0 ) {
  608. document->SetError( ERROR_PARSING_COMMENT, start, 0 );
  609. }
  610. return p;
  611. }
  612. bool XMLComment::Accept( XMLVisitor* visitor ) const
  613. {
  614. return visitor->Visit( *this );
  615. }
  616. // --------- XMLDeclaration ---------- //
  617. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  618. {
  619. }
  620. XMLDeclaration::~XMLDeclaration()
  621. {
  622. //printf( "~XMLDeclaration\n" );
  623. }
  624. char* XMLDeclaration::ParseDeep( char* p, StrPair* )
  625. {
  626. // Declaration parses as text.
  627. const char* start = p;
  628. p = value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  629. if ( p == 0 ) {
  630. document->SetError( ERROR_PARSING_DECLARATION, start, 0 );
  631. }
  632. return p;
  633. }
  634. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  635. {
  636. return visitor->Visit( *this );
  637. }
  638. // --------- XMLUnknown ---------- //
  639. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  640. {
  641. }
  642. XMLUnknown::~XMLUnknown()
  643. {
  644. }
  645. char* XMLUnknown::ParseDeep( char* p, StrPair* )
  646. {
  647. // Unknown parses as text.
  648. const char* start = p;
  649. p = value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  650. if ( !p ) {
  651. document->SetError( ERROR_PARSING_UNKNOWN, start, 0 );
  652. }
  653. return p;
  654. }
  655. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  656. {
  657. return visitor->Visit( *this );
  658. }
  659. // --------- XMLAttribute ---------- //
  660. char* XMLAttribute::ParseDeep( char* p )
  661. {
  662. p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
  663. if ( !p || !*p ) return 0;
  664. char endTag[2] = { *p, 0 };
  665. ++p;
  666. p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
  667. //if ( value.Empty() ) return 0;
  668. return p;
  669. }
  670. void XMLAttribute::SetName( const char* n )
  671. {
  672. name.SetStr( n );
  673. }
  674. int XMLAttribute::QueryIntAttribute( int* value ) const
  675. {
  676. if ( TIXML_SSCANF( Value(), "%d", value ) == 1 )
  677. return XML_NO_ERROR;
  678. return WRONG_ATTRIBUTE_TYPE;
  679. }
  680. int XMLAttribute::QueryUnsignedAttribute( unsigned int* value ) const
  681. {
  682. if ( TIXML_SSCANF( Value(), "%u", value ) == 1 )
  683. return XML_NO_ERROR;
  684. return WRONG_ATTRIBUTE_TYPE;
  685. }
  686. int XMLAttribute::QueryBoolAttribute( bool* value ) const
  687. {
  688. int ival = -1;
  689. QueryIntAttribute( &ival );
  690. if ( ival > 0 || XMLUtil::StringEqual( Value(), "true" ) ) {
  691. *value = true;
  692. return XML_NO_ERROR;
  693. }
  694. else if ( ival == 0 || XMLUtil::StringEqual( Value(), "false" ) ) {
  695. *value = false;
  696. return XML_NO_ERROR;
  697. }
  698. return WRONG_ATTRIBUTE_TYPE;
  699. }
  700. int XMLAttribute::QueryDoubleAttribute( double* value ) const
  701. {
  702. if ( TIXML_SSCANF( Value(), "%lf", value ) == 1 )
  703. return XML_NO_ERROR;
  704. return WRONG_ATTRIBUTE_TYPE;
  705. }
  706. int XMLAttribute::QueryFloatAttribute( float* value ) const
  707. {
  708. if ( TIXML_SSCANF( Value(), "%f", value ) == 1 )
  709. return XML_NO_ERROR;
  710. return WRONG_ATTRIBUTE_TYPE;
  711. }
  712. void XMLAttribute::SetAttribute( const char* v )
  713. {
  714. value.SetStr( v );
  715. }
  716. void XMLAttribute::SetAttribute( int v )
  717. {
  718. char buf[BUF_SIZE];
  719. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v );
  720. value.SetStr( buf );
  721. }
  722. void XMLAttribute::SetAttribute( unsigned v )
  723. {
  724. char buf[BUF_SIZE];
  725. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%u", v );
  726. value.SetStr( buf );
  727. }
  728. void XMLAttribute::SetAttribute( bool v )
  729. {
  730. char buf[BUF_SIZE];
  731. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v ? 1 : 0 );
  732. value.SetStr( buf );
  733. }
  734. void XMLAttribute::SetAttribute( double v )
  735. {
  736. char buf[BUF_SIZE];
  737. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  738. value.SetStr( buf );
  739. }
  740. void XMLAttribute::SetAttribute( float v )
  741. {
  742. char buf[BUF_SIZE];
  743. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  744. value.SetStr( buf );
  745. }
  746. // --------- XMLElement ---------- //
  747. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  748. closingType( 0 ),
  749. rootAttribute( 0 )
  750. {
  751. }
  752. XMLElement::~XMLElement()
  753. {
  754. while( rootAttribute ) {
  755. XMLAttribute* next = rootAttribute->next;
  756. DELETE_ATTRIBUTE( rootAttribute );
  757. rootAttribute = next;
  758. }
  759. }
  760. XMLAttribute* XMLElement::FindAttribute( const char* name )
  761. {
  762. XMLAttribute* a = 0;
  763. for( a=rootAttribute; a; a = a->next ) {
  764. if ( XMLUtil::StringEqual( a->Name(), name ) )
  765. return a;
  766. }
  767. return 0;
  768. }
  769. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  770. {
  771. XMLAttribute* a = 0;
  772. for( a=rootAttribute; a; a = a->next ) {
  773. if ( XMLUtil::StringEqual( a->Name(), name ) )
  774. return a;
  775. }
  776. return 0;
  777. }
  778. const char* XMLElement::GetText() const
  779. {
  780. if ( FirstChild() && FirstChild()->ToText() ) {
  781. return FirstChild()->ToText()->Value();
  782. }
  783. return 0;
  784. }
  785. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  786. {
  787. XMLAttribute* attrib = FindAttribute( name );
  788. if ( !attrib ) {
  789. attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  790. attrib->memPool = &document->attributePool;
  791. LinkAttribute( attrib );
  792. attrib->SetName( name );
  793. }
  794. return attrib;
  795. }
  796. void XMLElement::LinkAttribute( XMLAttribute* attrib )
  797. {
  798. if ( rootAttribute ) {
  799. XMLAttribute* end = rootAttribute;
  800. while ( end->next )
  801. end = end->next;
  802. end->next = attrib;
  803. }
  804. else {
  805. rootAttribute = attrib;
  806. }
  807. }
  808. void XMLElement::DeleteAttribute( const char* name )
  809. {
  810. XMLAttribute* prev = 0;
  811. for( XMLAttribute* a=rootAttribute; a; a=a->next ) {
  812. if ( XMLUtil::StringEqual( name, a->Name() ) ) {
  813. if ( prev ) {
  814. prev->next = a->next;
  815. }
  816. else {
  817. rootAttribute = a->next;
  818. }
  819. DELETE_ATTRIBUTE( a );
  820. break;
  821. }
  822. prev = a;
  823. }
  824. }
  825. char* XMLElement::ParseAttributes( char* p )
  826. {
  827. const char* start = p;
  828. // Read the attributes.
  829. while( p ) {
  830. p = XMLUtil::SkipWhiteSpace( p );
  831. if ( !p || !(*p) ) {
  832. document->SetError( ERROR_PARSING_ELEMENT, start, Name() );
  833. return 0;
  834. }
  835. // attribute.
  836. if ( XMLUtil::IsAlpha( *p ) ) {
  837. XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  838. attrib->memPool = &document->attributePool;
  839. p = attrib->ParseDeep( p );
  840. if ( !p || Attribute( attrib->Name() ) ) {
  841. DELETE_ATTRIBUTE( attrib );
  842. document->SetError( ERROR_PARSING_ATTRIBUTE, start, p );
  843. return 0;
  844. }
  845. LinkAttribute( attrib );
  846. }
  847. // end of the tag
  848. else if ( *p == '/' && *(p+1) == '>' ) {
  849. closingType = CLOSED;
  850. return p+2; // done; sealed element.
  851. }
  852. // end of the tag
  853. else if ( *p == '>' ) {
  854. ++p;
  855. break;
  856. }
  857. else {
  858. document->SetError( ERROR_PARSING_ELEMENT, start, p );
  859. return 0;
  860. }
  861. }
  862. return p;
  863. }
  864. //
  865. // <ele></ele>
  866. // <ele>foo<b>bar</b></ele>
  867. //
  868. char* XMLElement::ParseDeep( char* p, StrPair* strPair )
  869. {
  870. // Read the element name.
  871. p = XMLUtil::SkipWhiteSpace( p );
  872. if ( !p ) return 0;
  873. const char* start = p;
  874. // The closing element is the </element> form. It is
  875. // parsed just like a regular element then deleted from
  876. // the DOM.
  877. if ( *p == '/' ) {
  878. closingType = CLOSING;
  879. ++p;
  880. }
  881. p = value.ParseName( p );
  882. if ( value.Empty() ) return 0;
  883. bool elementClosed=false;
  884. p = ParseAttributes( p );
  885. if ( !p || !*p || closingType )
  886. return p;
  887. p = XMLNode::ParseDeep( p, strPair );
  888. // FIXME: proces end tage here??
  889. return p;
  890. }
  891. bool XMLElement::Accept( XMLVisitor* visitor ) const
  892. {
  893. if ( visitor->VisitEnter( *this, rootAttribute ) )
  894. {
  895. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  896. {
  897. if ( !node->Accept( visitor ) )
  898. break;
  899. }
  900. }
  901. return visitor->VisitExit( *this );
  902. }
  903. // --------- XMLDocument ----------- //
  904. XMLDocument::XMLDocument() :
  905. XMLNode( 0 ),
  906. writeBOM( false ),
  907. charBuffer( 0 )
  908. {
  909. document = this; // avoid warning about 'this' in initializer list
  910. }
  911. XMLDocument::~XMLDocument()
  912. {
  913. ClearChildren();
  914. delete [] charBuffer;
  915. #if 0
  916. textPool.Trace( "text" );
  917. elementPool.Trace( "element" );
  918. commentPool.Trace( "comment" );
  919. attributePool.Trace( "attribute" );
  920. #endif
  921. TIXMLASSERT( textPool.CurrentAllocs() == 0 );
  922. TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
  923. TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
  924. TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
  925. }
  926. void XMLDocument::InitDocument()
  927. {
  928. errorID = XML_NO_ERROR;
  929. errorStr1 = 0;
  930. errorStr2 = 0;
  931. delete [] charBuffer;
  932. charBuffer = 0;
  933. }
  934. XMLElement* XMLDocument::NewElement( const char* name )
  935. {
  936. XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
  937. ele->memPool = &elementPool;
  938. ele->SetName( name );
  939. return ele;
  940. }
  941. XMLComment* XMLDocument::NewComment( const char* str )
  942. {
  943. XMLComment* comment = new (commentPool.Alloc()) XMLComment( this );
  944. comment->memPool = &commentPool;
  945. comment->SetValue( str );
  946. return comment;
  947. }
  948. XMLText* XMLDocument::NewText( const char* str )
  949. {
  950. XMLText* text = new (textPool.Alloc()) XMLText( this );
  951. text->memPool = &textPool;
  952. text->SetValue( str );
  953. return text;
  954. }
  955. int XMLDocument::LoadFile( const char* filename )
  956. {
  957. ClearChildren();
  958. InitDocument();
  959. FILE* fp = fopen( filename, "rb" );
  960. if ( !fp ) {
  961. SetError( ERROR_FILE_NOT_FOUND, filename, 0 );
  962. return errorID;
  963. }
  964. LoadFile( fp );
  965. fclose( fp );
  966. return errorID;
  967. }
  968. int XMLDocument::LoadFile( FILE* fp )
  969. {
  970. ClearChildren();
  971. InitDocument();
  972. fseek( fp, 0, SEEK_END );
  973. unsigned size = ftell( fp );
  974. fseek( fp, 0, SEEK_SET );
  975. if ( size == 0 ) {
  976. return errorID;
  977. }
  978. charBuffer = new char[size+1];
  979. fread( charBuffer, size, 1, fp );
  980. charBuffer[size] = 0;
  981. const char* p = charBuffer;
  982. p = XMLUtil::SkipWhiteSpace( p );
  983. p = XMLUtil::ReadBOM( p, &writeBOM );
  984. if ( !p || !*p ) {
  985. SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
  986. return errorID;
  987. }
  988. ParseDeep( charBuffer + (p-charBuffer), 0 );
  989. return errorID;
  990. }
  991. void XMLDocument::SaveFile( const char* filename )
  992. {
  993. FILE* fp = fopen( filename, "w" );
  994. XMLStreamer stream( fp );
  995. Print( &stream );
  996. fclose( fp );
  997. }
  998. int XMLDocument::Parse( const char* p )
  999. {
  1000. ClearChildren();
  1001. InitDocument();
  1002. if ( !p || !*p ) {
  1003. SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
  1004. return errorID;
  1005. }
  1006. p = XMLUtil::SkipWhiteSpace( p );
  1007. p = XMLUtil::ReadBOM( p, &writeBOM );
  1008. if ( !p || !*p ) {
  1009. SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
  1010. return errorID;
  1011. }
  1012. size_t len = strlen( p );
  1013. charBuffer = new char[ len+1 ];
  1014. memcpy( charBuffer, p, len+1 );
  1015. ParseDeep( charBuffer, 0 );
  1016. return errorID;
  1017. }
  1018. void XMLDocument::Print( XMLStreamer* streamer )
  1019. {
  1020. XMLStreamer stdStreamer( stdout );
  1021. if ( !streamer )
  1022. streamer = &stdStreamer;
  1023. Accept( streamer );
  1024. }
  1025. void XMLDocument::SetError( int error, const char* str1, const char* str2 )
  1026. {
  1027. errorID = error;
  1028. errorStr1 = str1;
  1029. errorStr2 = str2;
  1030. }
  1031. void XMLDocument::PrintError() const
  1032. {
  1033. if ( errorID ) {
  1034. char buf1[20] = { 0 };
  1035. char buf2[20] = { 0 };
  1036. if ( errorStr1 ) {
  1037. strncpy( buf1, errorStr1, 20 );
  1038. buf1[19] = 0;
  1039. }
  1040. if ( errorStr2 ) {
  1041. strncpy( buf2, errorStr2, 20 );
  1042. buf2[19] = 0;
  1043. }
  1044. printf( "XMLDocument error id=%d str1=%s str2=%s\n",
  1045. errorID, buf1, buf2 );
  1046. }
  1047. }
  1048. XMLStreamer::XMLStreamer( FILE* file ) :
  1049. elementJustOpened( false ),
  1050. firstElement( true ),
  1051. fp( file ),
  1052. depth( 0 ),
  1053. textDepth( -1 )
  1054. {
  1055. for( int i=0; i<ENTITY_RANGE; ++i ) {
  1056. entityFlag[i] = false;
  1057. restrictedEntityFlag[i] = false;
  1058. }
  1059. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1060. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  1061. if ( entities[i].value < ENTITY_RANGE ) {
  1062. entityFlag[ entities[i].value ] = true;
  1063. }
  1064. }
  1065. restrictedEntityFlag['&'] = true;
  1066. restrictedEntityFlag['<'] = true;
  1067. restrictedEntityFlag['>'] = true; // not required, but consistency is nice
  1068. buffer.Push( 0 );
  1069. }
  1070. void XMLStreamer::Print( const char* format, ... )
  1071. {
  1072. va_list va;
  1073. va_start( va, format );
  1074. if ( fp ) {
  1075. vfprintf( fp, format, va );
  1076. }
  1077. else {
  1078. // This seems brutally complex. Haven't figured out a better
  1079. // way on windows.
  1080. #ifdef _MSC_VER
  1081. int len = -1;
  1082. int expand = 1000;
  1083. while ( len < 0 ) {
  1084. len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), accumulator.Capacity()-1, format, va );
  1085. if ( len < 0 ) {
  1086. accumulator.PushArr( expand );
  1087. expand *= 3/2;
  1088. }
  1089. }
  1090. char* p = buffer.PushArr( len ) - 1;
  1091. memcpy( p, accumulator.Mem(), len+1 );
  1092. #else
  1093. int len = vsnprintf( 0, 0, format, va );
  1094. char* p = buffer.PushArr( len ) - 1;
  1095. vsprintf_s( p, len+1, format, va );
  1096. #endif
  1097. }
  1098. va_end( va );
  1099. }
  1100. void XMLStreamer::PrintSpace( int depth )
  1101. {
  1102. for( int i=0; i<depth; ++i ) {
  1103. Print( " " );
  1104. }
  1105. }
  1106. void XMLStreamer::PrintString( const char* p, bool restricted )
  1107. {
  1108. // Look for runs of bytes between entities to print.
  1109. const char* q = p;
  1110. const bool* flag = restricted ? restrictedEntityFlag : entityFlag;
  1111. while ( *q ) {
  1112. // Remember, char is sometimes signed. (How many times has that bitten me?)
  1113. if ( *q > 0 && *q < ENTITY_RANGE ) {
  1114. // Check for entities. If one is found, flush
  1115. // the stream up until the entity, write the
  1116. // entity, and keep looking.
  1117. if ( flag[*q] ) {
  1118. while ( p < q ) {
  1119. Print( "%c", *p );
  1120. ++p;
  1121. }
  1122. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1123. if ( entities[i].value == *q ) {
  1124. Print( "&%s;", entities[i].pattern );
  1125. break;
  1126. }
  1127. }
  1128. ++p;
  1129. }
  1130. }
  1131. ++q;
  1132. }
  1133. // Flush the remaining string. This will be the entire
  1134. // string if an entity wasn't found.
  1135. if ( q-p > 0 ) {
  1136. Print( "%s", p );
  1137. }
  1138. }
  1139. void XMLStreamer::PushHeader( bool writeBOM, bool writeDec )
  1140. {
  1141. static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
  1142. if ( writeBOM ) {
  1143. Print( "%s", bom );
  1144. }
  1145. if ( writeDec ) {
  1146. PushDeclaration( "xml version=\"1.0\"" );
  1147. }
  1148. }
  1149. void XMLStreamer::OpenElement( const char* name )
  1150. {
  1151. if ( elementJustOpened ) {
  1152. SealElement();
  1153. }
  1154. stack.Push( name );
  1155. if ( textDepth < 0 && !firstElement ) {
  1156. Print( "\n" );
  1157. PrintSpace( depth );
  1158. }
  1159. Print( "<%s", name );
  1160. elementJustOpened = true;
  1161. firstElement = false;
  1162. ++depth;
  1163. }
  1164. void XMLStreamer::PushAttribute( const char* name, const char* value )
  1165. {
  1166. TIXMLASSERT( elementJustOpened );
  1167. Print( " %s=\"", name );
  1168. PrintString( value, false );
  1169. Print( "\"" );
  1170. }
  1171. void XMLStreamer::CloseElement()
  1172. {
  1173. --depth;
  1174. const char* name = stack.Pop();
  1175. if ( elementJustOpened ) {
  1176. Print( "/>" );
  1177. }
  1178. else {
  1179. if ( textDepth < 0 ) {
  1180. Print( "\n" );
  1181. PrintSpace( depth );
  1182. }
  1183. Print( "</%s>", name );
  1184. }
  1185. if ( textDepth == depth )
  1186. textDepth = -1;
  1187. if ( depth == 0 )
  1188. Print( "\n" );
  1189. elementJustOpened = false;
  1190. }
  1191. void XMLStreamer::SealElement()
  1192. {
  1193. elementJustOpened = false;
  1194. Print( ">" );
  1195. }
  1196. void XMLStreamer::PushText( const char* text, bool cdata )
  1197. {
  1198. textDepth = depth-1;
  1199. if ( elementJustOpened ) {
  1200. SealElement();
  1201. }
  1202. if ( cdata ) {
  1203. Print( "<![CDATA[" );
  1204. Print( "%s", text );
  1205. Print( "]]>" );
  1206. }
  1207. else {
  1208. PrintString( text, true );
  1209. }
  1210. }
  1211. void XMLStreamer::PushComment( const char* comment )
  1212. {
  1213. if ( elementJustOpened ) {
  1214. SealElement();
  1215. }
  1216. if ( textDepth < 0 && !firstElement ) {
  1217. Print( "\n" );
  1218. PrintSpace( depth );
  1219. }
  1220. firstElement = false;
  1221. Print( "<!--%s-->", comment );
  1222. }
  1223. void XMLStreamer::PushDeclaration( const char* value )
  1224. {
  1225. if ( elementJustOpened ) {
  1226. SealElement();
  1227. }
  1228. if ( textDepth < 0 && !firstElement) {
  1229. Print( "\n" );
  1230. PrintSpace( depth );
  1231. }
  1232. firstElement = false;
  1233. Print( "<?%s?>", value );
  1234. }
  1235. void XMLStreamer::PushUnknown( const char* value )
  1236. {
  1237. if ( elementJustOpened ) {
  1238. SealElement();
  1239. }
  1240. if ( textDepth < 0 && !firstElement ) {
  1241. Print( "\n" );
  1242. PrintSpace( depth );
  1243. }
  1244. firstElement = false;
  1245. Print( "<!%s>", value );
  1246. }
  1247. bool XMLStreamer::VisitEnter( const XMLDocument& doc )
  1248. {
  1249. if ( doc.HasBOM() ) {
  1250. PushHeader( true, false );
  1251. }
  1252. return true;
  1253. }
  1254. bool XMLStreamer::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  1255. {
  1256. OpenElement( element.Name() );
  1257. while ( attribute ) {
  1258. PushAttribute( attribute->Name(), attribute->Value() );
  1259. attribute = attribute->Next();
  1260. }
  1261. return true;
  1262. }
  1263. bool XMLStreamer::VisitExit( const XMLElement& element )
  1264. {
  1265. CloseElement();
  1266. return true;
  1267. }
  1268. bool XMLStreamer::Visit( const XMLText& text )
  1269. {
  1270. PushText( text.Value(), text.CData() );
  1271. return true;
  1272. }
  1273. bool XMLStreamer::Visit( const XMLComment& comment )
  1274. {
  1275. PushComment( comment.Value() );
  1276. return true;
  1277. }
  1278. bool XMLStreamer::Visit( const XMLDeclaration& declaration )
  1279. {
  1280. PushDeclaration( declaration.Value() );
  1281. return true;
  1282. }
  1283. bool XMLStreamer::Visit( const XMLUnknown& unknown )
  1284. {
  1285. PushUnknown( unknown.Value() );
  1286. return true;
  1287. }