tinyxml2.cpp 32 KB

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