tinyxml2.cpp 33 KB

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