tinyxml2.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  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 <new> // yes, this one new style header, is in the Android SDK.
  20. # ifdef ANDROID_NDK
  21. # include <stddef.h>
  22. #else
  23. # include <cstddef>
  24. #endif
  25. static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
  26. static const char LF = LINE_FEED;
  27. static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
  28. static const char CR = CARRIAGE_RETURN;
  29. static const char SINGLE_QUOTE = '\'';
  30. static const char DOUBLE_QUOTE = '\"';
  31. // Bunch of unicode info at:
  32. // http://www.unicode.org/faq/utf_bom.html
  33. // ef bb bf (Microsoft "lead bytes") - designates UTF-8
  34. static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
  35. static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
  36. static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
  37. namespace tinyxml2
  38. {
  39. struct Entity {
  40. const char* pattern;
  41. int length;
  42. char value;
  43. };
  44. static const int NUM_ENTITIES = 5;
  45. static const Entity entities[NUM_ENTITIES] = {
  46. { "quot", 4, DOUBLE_QUOTE },
  47. { "amp", 3, '&' },
  48. { "apos", 4, SINGLE_QUOTE },
  49. { "lt", 2, '<' },
  50. { "gt", 2, '>' }
  51. };
  52. StrPair::~StrPair()
  53. {
  54. Reset();
  55. }
  56. void StrPair::Reset()
  57. {
  58. if ( _flags & NEEDS_DELETE ) {
  59. delete [] _start;
  60. }
  61. _flags = 0;
  62. _start = 0;
  63. _end = 0;
  64. }
  65. void StrPair::SetStr( const char* str, int flags )
  66. {
  67. Reset();
  68. size_t len = strlen( str );
  69. _start = new char[ len+1 ];
  70. memcpy( _start, str, len+1 );
  71. _end = _start + len;
  72. _flags = flags | NEEDS_DELETE;
  73. }
  74. char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
  75. {
  76. TIXMLASSERT( endTag && *endTag );
  77. char* start = p;
  78. char endChar = *endTag;
  79. size_t length = strlen( endTag );
  80. // Inner loop of text parsing.
  81. while ( *p ) {
  82. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  83. Set( start, p, strFlags );
  84. return p + length;
  85. }
  86. ++p;
  87. }
  88. return 0;
  89. }
  90. char* StrPair::ParseName( char* p )
  91. {
  92. char* start = p;
  93. if ( !start || !(*start) ) {
  94. return 0;
  95. }
  96. while( *p && ( p == start ? XMLUtil::IsNameStartChar( *p ) : XMLUtil::IsNameChar( *p ) )) {
  97. ++p;
  98. }
  99. if ( p > start ) {
  100. Set( start, p, 0 );
  101. return p;
  102. }
  103. return 0;
  104. }
  105. void StrPair::CollapseWhitespace()
  106. {
  107. // Adjusting _start would cause undefined behavior on delete[]
  108. TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 );
  109. // Trim leading space.
  110. _start = XMLUtil::SkipWhiteSpace( _start );
  111. if ( _start && *_start ) {
  112. char* p = _start; // the read pointer
  113. char* q = _start; // the write pointer
  114. while( *p ) {
  115. if ( XMLUtil::IsWhiteSpace( *p )) {
  116. p = XMLUtil::SkipWhiteSpace( p );
  117. if ( *p == 0 ) {
  118. break; // don't write to q; this trims the trailing space.
  119. }
  120. *q = ' ';
  121. ++q;
  122. }
  123. *q = *p;
  124. ++q;
  125. ++p;
  126. }
  127. *q = 0;
  128. }
  129. }
  130. const char* StrPair::GetStr()
  131. {
  132. if ( _flags & NEEDS_FLUSH ) {
  133. *_end = 0;
  134. _flags ^= NEEDS_FLUSH;
  135. if ( _flags ) {
  136. char* p = _start; // the read pointer
  137. char* q = _start; // the write pointer
  138. while( p < _end ) {
  139. if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
  140. // CR-LF pair becomes LF
  141. // CR alone becomes LF
  142. // LF-CR becomes LF
  143. if ( *(p+1) == LF ) {
  144. p += 2;
  145. }
  146. else {
  147. ++p;
  148. }
  149. *q++ = LF;
  150. }
  151. else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
  152. if ( *(p+1) == CR ) {
  153. p += 2;
  154. }
  155. else {
  156. ++p;
  157. }
  158. *q++ = LF;
  159. }
  160. else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
  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. const int buflen = 10;
  167. char buf[buflen] = { 0 };
  168. int len = 0;
  169. p = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
  170. TIXMLASSERT( 0 <= len && len <= buflen );
  171. TIXMLASSERT( q + len <= p );
  172. memcpy( q, buf, len );
  173. q += len;
  174. }
  175. else {
  176. int i=0;
  177. for(; i<NUM_ENTITIES; ++i ) {
  178. if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
  179. && *(p+entities[i].length+1) == ';' ) {
  180. // Found an entity convert;
  181. *q = entities[i].value;
  182. ++q;
  183. p += entities[i].length + 2;
  184. break;
  185. }
  186. }
  187. if ( i == NUM_ENTITIES ) {
  188. // fixme: treat as error?
  189. ++p;
  190. ++q;
  191. }
  192. }
  193. }
  194. else {
  195. *q = *p;
  196. ++p;
  197. ++q;
  198. }
  199. }
  200. *q = 0;
  201. }
  202. // The loop below has plenty going on, and this
  203. // is a less useful mode. Break it out.
  204. if ( _flags & COLLAPSE_WHITESPACE ) {
  205. CollapseWhitespace();
  206. }
  207. _flags = (_flags & NEEDS_DELETE);
  208. }
  209. return _start;
  210. }
  211. // --------- XMLUtil ----------- //
  212. const char* XMLUtil::ReadBOM( const char* p, bool* bom )
  213. {
  214. *bom = false;
  215. const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
  216. // Check for BOM:
  217. if ( *(pu+0) == TIXML_UTF_LEAD_0
  218. && *(pu+1) == TIXML_UTF_LEAD_1
  219. && *(pu+2) == TIXML_UTF_LEAD_2 ) {
  220. *bom = true;
  221. p += 3;
  222. }
  223. return p;
  224. }
  225. void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
  226. {
  227. const unsigned long BYTE_MASK = 0xBF;
  228. const unsigned long BYTE_MARK = 0x80;
  229. const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  230. if (input < 0x80) {
  231. *length = 1;
  232. }
  233. else if ( input < 0x800 ) {
  234. *length = 2;
  235. }
  236. else if ( input < 0x10000 ) {
  237. *length = 3;
  238. }
  239. else if ( input < 0x200000 ) {
  240. *length = 4;
  241. }
  242. else {
  243. *length = 0; // This code won't covert this correctly anyway.
  244. return;
  245. }
  246. output += *length;
  247. // Scary scary fall throughs.
  248. switch (*length) {
  249. case 4:
  250. --output;
  251. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  252. input >>= 6;
  253. case 3:
  254. --output;
  255. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  256. input >>= 6;
  257. case 2:
  258. --output;
  259. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  260. input >>= 6;
  261. case 1:
  262. --output;
  263. *output = (char)(input | FIRST_BYTE_MARK[*length]);
  264. default:
  265. break;
  266. }
  267. }
  268. const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
  269. {
  270. // Presume an entity, and pull it out.
  271. *length = 0;
  272. if ( *(p+1) == '#' && *(p+2) ) {
  273. unsigned long ucs = 0;
  274. ptrdiff_t delta = 0;
  275. unsigned mult = 1;
  276. if ( *(p+2) == 'x' ) {
  277. // Hexadecimal.
  278. if ( !*(p+3) ) {
  279. return 0;
  280. }
  281. const char* q = p+3;
  282. q = strchr( q, ';' );
  283. if ( !q || !*q ) {
  284. return 0;
  285. }
  286. delta = q-p;
  287. --q;
  288. while ( *q != 'x' ) {
  289. if ( *q >= '0' && *q <= '9' ) {
  290. ucs += mult * (*q - '0');
  291. }
  292. else if ( *q >= 'a' && *q <= 'f' ) {
  293. ucs += mult * (*q - 'a' + 10);
  294. }
  295. else if ( *q >= 'A' && *q <= 'F' ) {
  296. ucs += mult * (*q - 'A' + 10 );
  297. }
  298. else {
  299. return 0;
  300. }
  301. mult *= 16;
  302. --q;
  303. }
  304. }
  305. else {
  306. // Decimal.
  307. if ( !*(p+2) ) {
  308. return 0;
  309. }
  310. const char* q = p+2;
  311. q = strchr( q, ';' );
  312. if ( !q || !*q ) {
  313. return 0;
  314. }
  315. delta = q-p;
  316. --q;
  317. while ( *q != '#' ) {
  318. if ( *q >= '0' && *q <= '9' ) {
  319. ucs += mult * (*q - '0');
  320. }
  321. else {
  322. return 0;
  323. }
  324. mult *= 10;
  325. --q;
  326. }
  327. }
  328. // convert the UCS to UTF-8
  329. ConvertUTF32ToUTF8( ucs, value, length );
  330. return p + delta + 1;
  331. }
  332. return p+1;
  333. }
  334. void XMLUtil::ToStr( int v, char* buffer, int bufferSize )
  335. {
  336. TIXML_SNPRINTF( buffer, bufferSize, "%d", v );
  337. }
  338. void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize )
  339. {
  340. TIXML_SNPRINTF( buffer, bufferSize, "%u", v );
  341. }
  342. void XMLUtil::ToStr( bool v, char* buffer, int bufferSize )
  343. {
  344. TIXML_SNPRINTF( buffer, bufferSize, "%d", v ? 1 : 0 );
  345. }
  346. /*
  347. ToStr() of a number is a very tricky topic.
  348. https://github.com/leethomason/tinyxml2/issues/106
  349. */
  350. void XMLUtil::ToStr( float v, char* buffer, int bufferSize )
  351. {
  352. TIXML_SNPRINTF( buffer, bufferSize, "%.8g", v );
  353. }
  354. void XMLUtil::ToStr( double v, char* buffer, int bufferSize )
  355. {
  356. TIXML_SNPRINTF( buffer, bufferSize, "%.17g", v );
  357. }
  358. bool XMLUtil::ToInt( const char* str, int* value )
  359. {
  360. if ( TIXML_SSCANF( str, "%d", value ) == 1 ) {
  361. return true;
  362. }
  363. return false;
  364. }
  365. bool XMLUtil::ToUnsigned( const char* str, unsigned *value )
  366. {
  367. if ( TIXML_SSCANF( str, "%u", value ) == 1 ) {
  368. return true;
  369. }
  370. return false;
  371. }
  372. bool XMLUtil::ToBool( const char* str, bool* value )
  373. {
  374. int ival = 0;
  375. if ( ToInt( str, &ival )) {
  376. *value = (ival==0) ? false : true;
  377. return true;
  378. }
  379. if ( StringEqual( str, "true" ) ) {
  380. *value = true;
  381. return true;
  382. }
  383. else if ( StringEqual( str, "false" ) ) {
  384. *value = false;
  385. return true;
  386. }
  387. return false;
  388. }
  389. bool XMLUtil::ToFloat( const char* str, float* value )
  390. {
  391. if ( TIXML_SSCANF( str, "%f", value ) == 1 ) {
  392. return true;
  393. }
  394. return false;
  395. }
  396. bool XMLUtil::ToDouble( const char* str, double* value )
  397. {
  398. if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) {
  399. return true;
  400. }
  401. return false;
  402. }
  403. char* XMLDocument::Identify( char* p, XMLNode** node )
  404. {
  405. XMLNode* returnNode = 0;
  406. char* start = p;
  407. p = XMLUtil::SkipWhiteSpace( p );
  408. if( !p || !*p ) {
  409. return p;
  410. }
  411. // What is this thing?
  412. // These strings define the matching patters:
  413. static const char* xmlHeader = { "<?" };
  414. static const char* commentHeader = { "<!--" };
  415. static const char* dtdHeader = { "<!" };
  416. static const char* cdataHeader = { "<![CDATA[" };
  417. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  418. static const int xmlHeaderLen = 2;
  419. static const int commentHeaderLen = 4;
  420. static const int dtdHeaderLen = 2;
  421. static const int cdataHeaderLen = 9;
  422. static const int elementHeaderLen = 1;
  423. #if defined(_MSC_VER)
  424. #pragma warning ( push )
  425. #pragma warning ( disable : 4127 )
  426. #endif
  427. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
  428. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
  429. #if defined(_MSC_VER)
  430. #pragma warning (pop)
  431. #endif
  432. if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
  433. returnNode = new (_commentPool.Alloc()) XMLDeclaration( this );
  434. returnNode->_memPool = &_commentPool;
  435. p += xmlHeaderLen;
  436. }
  437. else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  438. returnNode = new (_commentPool.Alloc()) XMLComment( this );
  439. returnNode->_memPool = &_commentPool;
  440. p += commentHeaderLen;
  441. }
  442. else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
  443. XMLText* text = new (_textPool.Alloc()) XMLText( this );
  444. returnNode = text;
  445. returnNode->_memPool = &_textPool;
  446. p += cdataHeaderLen;
  447. text->SetCData( true );
  448. }
  449. else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
  450. returnNode = new (_commentPool.Alloc()) XMLUnknown( this );
  451. returnNode->_memPool = &_commentPool;
  452. p += dtdHeaderLen;
  453. }
  454. else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
  455. returnNode = new (_elementPool.Alloc()) XMLElement( this );
  456. returnNode->_memPool = &_elementPool;
  457. p += elementHeaderLen;
  458. }
  459. else {
  460. returnNode = new (_textPool.Alloc()) XMLText( this );
  461. returnNode->_memPool = &_textPool;
  462. p = start; // Back it up, all the text counts.
  463. }
  464. *node = returnNode;
  465. return p;
  466. }
  467. bool XMLDocument::Accept( XMLVisitor* visitor ) const
  468. {
  469. if ( visitor->VisitEnter( *this ) ) {
  470. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
  471. if ( !node->Accept( visitor ) ) {
  472. break;
  473. }
  474. }
  475. }
  476. return visitor->VisitExit( *this );
  477. }
  478. // --------- XMLNode ----------- //
  479. XMLNode::XMLNode( XMLDocument* doc ) :
  480. _document( doc ),
  481. _parent( 0 ),
  482. _firstChild( 0 ), _lastChild( 0 ),
  483. _prev( 0 ), _next( 0 ),
  484. _memPool( 0 )
  485. {
  486. }
  487. XMLNode::~XMLNode()
  488. {
  489. DeleteChildren();
  490. if ( _parent ) {
  491. _parent->Unlink( this );
  492. }
  493. }
  494. const char* XMLNode::Value() const
  495. {
  496. return _value.GetStr();
  497. }
  498. void XMLNode::SetValue( const char* str, bool staticMem )
  499. {
  500. if ( staticMem ) {
  501. _value.SetInternedStr( str );
  502. }
  503. else {
  504. _value.SetStr( str );
  505. }
  506. }
  507. void XMLNode::DeleteChildren()
  508. {
  509. while( _firstChild ) {
  510. XMLNode* node = _firstChild;
  511. Unlink( node );
  512. DeleteNode( node );
  513. }
  514. _firstChild = _lastChild = 0;
  515. }
  516. void XMLNode::Unlink( XMLNode* child )
  517. {
  518. if ( child == _firstChild ) {
  519. _firstChild = _firstChild->_next;
  520. }
  521. if ( child == _lastChild ) {
  522. _lastChild = _lastChild->_prev;
  523. }
  524. if ( child->_prev ) {
  525. child->_prev->_next = child->_next;
  526. }
  527. if ( child->_next ) {
  528. child->_next->_prev = child->_prev;
  529. }
  530. child->_parent = 0;
  531. }
  532. void XMLNode::DeleteChild( XMLNode* node )
  533. {
  534. TIXMLASSERT( node->_parent == this );
  535. DeleteNode( node );
  536. }
  537. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  538. {
  539. if (addThis->_document != _document)
  540. return 0;
  541. if (addThis->_parent)
  542. addThis->_parent->Unlink( addThis );
  543. else
  544. addThis->_memPool->SetTracked();
  545. if ( _lastChild ) {
  546. TIXMLASSERT( _firstChild );
  547. TIXMLASSERT( _lastChild->_next == 0 );
  548. _lastChild->_next = addThis;
  549. addThis->_prev = _lastChild;
  550. _lastChild = addThis;
  551. addThis->_next = 0;
  552. }
  553. else {
  554. TIXMLASSERT( _firstChild == 0 );
  555. _firstChild = _lastChild = addThis;
  556. addThis->_prev = 0;
  557. addThis->_next = 0;
  558. }
  559. addThis->_parent = this;
  560. return addThis;
  561. }
  562. XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
  563. {
  564. if (addThis->_document != _document)
  565. return 0;
  566. if (addThis->_parent)
  567. addThis->_parent->Unlink( addThis );
  568. else
  569. addThis->_memPool->SetTracked();
  570. if ( _firstChild ) {
  571. TIXMLASSERT( _lastChild );
  572. TIXMLASSERT( _firstChild->_prev == 0 );
  573. _firstChild->_prev = addThis;
  574. addThis->_next = _firstChild;
  575. _firstChild = addThis;
  576. addThis->_prev = 0;
  577. }
  578. else {
  579. TIXMLASSERT( _lastChild == 0 );
  580. _firstChild = _lastChild = addThis;
  581. addThis->_prev = 0;
  582. addThis->_next = 0;
  583. }
  584. addThis->_parent = this;
  585. return addThis;
  586. }
  587. XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
  588. {
  589. if (addThis->_document != _document)
  590. return 0;
  591. TIXMLASSERT( afterThis->_parent == this );
  592. if ( afterThis->_parent != this ) {
  593. return 0;
  594. }
  595. if ( afterThis->_next == 0 ) {
  596. // The last node or the only node.
  597. return InsertEndChild( addThis );
  598. }
  599. if (addThis->_parent)
  600. addThis->_parent->Unlink( addThis );
  601. else
  602. addThis->_memPool->SetTracked();
  603. addThis->_prev = afterThis;
  604. addThis->_next = afterThis->_next;
  605. afterThis->_next->_prev = addThis;
  606. afterThis->_next = addThis;
  607. addThis->_parent = this;
  608. return addThis;
  609. }
  610. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  611. {
  612. for( XMLNode* node=_firstChild; node; node=node->_next ) {
  613. XMLElement* element = node->ToElement();
  614. if ( element ) {
  615. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  616. return element;
  617. }
  618. }
  619. }
  620. return 0;
  621. }
  622. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  623. {
  624. for( XMLNode* node=_lastChild; node; node=node->_prev ) {
  625. XMLElement* element = node->ToElement();
  626. if ( element ) {
  627. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  628. return element;
  629. }
  630. }
  631. }
  632. return 0;
  633. }
  634. const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
  635. {
  636. for( XMLNode* node=this->_next; node; node = node->_next ) {
  637. const XMLElement* element = node->ToElement();
  638. if ( element
  639. && (!value || XMLUtil::StringEqual( value, node->Value() ))) {
  640. return element;
  641. }
  642. }
  643. return 0;
  644. }
  645. const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
  646. {
  647. for( XMLNode* node=_prev; node; node = node->_prev ) {
  648. const XMLElement* element = node->ToElement();
  649. if ( element
  650. && (!value || XMLUtil::StringEqual( value, node->Value() ))) {
  651. return element;
  652. }
  653. }
  654. return 0;
  655. }
  656. char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
  657. {
  658. // This is a recursive method, but thinking about it "at the current level"
  659. // it is a pretty simple flat list:
  660. // <foo/>
  661. // <!-- comment -->
  662. //
  663. // With a special case:
  664. // <foo>
  665. // </foo>
  666. // <!-- comment -->
  667. //
  668. // Where the closing element (/foo) *must* be the next thing after the opening
  669. // element, and the names must match. BUT the tricky bit is that the closing
  670. // element will be read by the child.
  671. //
  672. // 'endTag' is the end tag for this node, it is returned by a call to a child.
  673. // 'parentEnd' is the end tag for the parent, which is filled in and returned.
  674. while( p && *p ) {
  675. XMLNode* node = 0;
  676. p = _document->Identify( p, &node );
  677. if ( p == 0 || node == 0 ) {
  678. break;
  679. }
  680. StrPair endTag;
  681. p = node->ParseDeep( p, &endTag );
  682. if ( !p ) {
  683. DeleteNode( node );
  684. node = 0;
  685. if ( !_document->Error() ) {
  686. _document->SetError( XML_ERROR_PARSING, 0, 0 );
  687. }
  688. break;
  689. }
  690. XMLElement* ele = node->ToElement();
  691. // We read the end tag. Return it to the parent.
  692. if ( ele && ele->ClosingType() == XMLElement::CLOSING ) {
  693. if ( parentEnd ) {
  694. *parentEnd = static_cast<XMLElement*>(node)->_value;
  695. }
  696. node->_memPool->SetTracked(); // created and then immediately deleted.
  697. DeleteNode( node );
  698. return p;
  699. }
  700. // Handle an end tag returned to this level.
  701. // And handle a bunch of annoying errors.
  702. if ( ele ) {
  703. if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
  704. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  705. p = 0;
  706. }
  707. else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) {
  708. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  709. p = 0;
  710. }
  711. else if ( !endTag.Empty() ) {
  712. if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
  713. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  714. p = 0;
  715. }
  716. }
  717. }
  718. if ( p == 0 ) {
  719. DeleteNode( node );
  720. node = 0;
  721. }
  722. if ( node ) {
  723. this->InsertEndChild( node );
  724. }
  725. }
  726. return 0;
  727. }
  728. void XMLNode::DeleteNode( XMLNode* node )
  729. {
  730. if ( node == 0 ) {
  731. return;
  732. }
  733. MemPool* pool = node->_memPool;
  734. node->~XMLNode();
  735. pool->Free( node );
  736. }
  737. // --------- XMLText ---------- //
  738. char* XMLText::ParseDeep( char* p, StrPair* )
  739. {
  740. const char* start = p;
  741. if ( this->CData() ) {
  742. p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  743. if ( !p ) {
  744. _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
  745. }
  746. return p;
  747. }
  748. else {
  749. int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES;
  750. if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) {
  751. flags |= StrPair::COLLAPSE_WHITESPACE;
  752. }
  753. p = _value.ParseText( p, "<", flags );
  754. if ( !p ) {
  755. _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
  756. }
  757. if ( p && *p ) {
  758. return p-1;
  759. }
  760. }
  761. return 0;
  762. }
  763. XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
  764. {
  765. if ( !doc ) {
  766. doc = _document;
  767. }
  768. XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
  769. text->SetCData( this->CData() );
  770. return text;
  771. }
  772. bool XMLText::ShallowEqual( const XMLNode* compare ) const
  773. {
  774. return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() ));
  775. }
  776. bool XMLText::Accept( XMLVisitor* visitor ) const
  777. {
  778. return visitor->Visit( *this );
  779. }
  780. // --------- XMLComment ---------- //
  781. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  782. {
  783. }
  784. XMLComment::~XMLComment()
  785. {
  786. }
  787. char* XMLComment::ParseDeep( char* p, StrPair* )
  788. {
  789. // Comment parses as text.
  790. const char* start = p;
  791. p = _value.ParseText( p, "-->", StrPair::COMMENT );
  792. if ( p == 0 ) {
  793. _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
  794. }
  795. return p;
  796. }
  797. XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
  798. {
  799. if ( !doc ) {
  800. doc = _document;
  801. }
  802. XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
  803. return comment;
  804. }
  805. bool XMLComment::ShallowEqual( const XMLNode* compare ) const
  806. {
  807. const XMLComment* comment = compare->ToComment();
  808. return ( comment && XMLUtil::StringEqual( comment->Value(), Value() ));
  809. }
  810. bool XMLComment::Accept( XMLVisitor* visitor ) const
  811. {
  812. return visitor->Visit( *this );
  813. }
  814. // --------- XMLDeclaration ---------- //
  815. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  816. {
  817. }
  818. XMLDeclaration::~XMLDeclaration()
  819. {
  820. //printf( "~XMLDeclaration\n" );
  821. }
  822. char* XMLDeclaration::ParseDeep( char* p, StrPair* )
  823. {
  824. // Declaration parses as text.
  825. const char* start = p;
  826. p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  827. if ( p == 0 ) {
  828. _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
  829. }
  830. return p;
  831. }
  832. XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
  833. {
  834. if ( !doc ) {
  835. doc = _document;
  836. }
  837. XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
  838. return dec;
  839. }
  840. bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
  841. {
  842. const XMLDeclaration* declaration = compare->ToDeclaration();
  843. return ( declaration && XMLUtil::StringEqual( declaration->Value(), Value() ));
  844. }
  845. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  846. {
  847. return visitor->Visit( *this );
  848. }
  849. // --------- XMLUnknown ---------- //
  850. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  851. {
  852. }
  853. XMLUnknown::~XMLUnknown()
  854. {
  855. }
  856. char* XMLUnknown::ParseDeep( char* p, StrPair* )
  857. {
  858. // Unknown parses as text.
  859. const char* start = p;
  860. p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  861. if ( !p ) {
  862. _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
  863. }
  864. return p;
  865. }
  866. XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
  867. {
  868. if ( !doc ) {
  869. doc = _document;
  870. }
  871. XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
  872. return text;
  873. }
  874. bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
  875. {
  876. const XMLUnknown* unknown = compare->ToUnknown();
  877. return ( unknown && XMLUtil::StringEqual( unknown->Value(), Value() ));
  878. }
  879. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  880. {
  881. return visitor->Visit( *this );
  882. }
  883. // --------- XMLAttribute ---------- //
  884. const char* XMLAttribute::Name() const
  885. {
  886. return _name.GetStr();
  887. }
  888. const char* XMLAttribute::Value() const
  889. {
  890. return _value.GetStr();
  891. }
  892. char* XMLAttribute::ParseDeep( char* p, bool processEntities )
  893. {
  894. // Parse using the name rules: bug fix, was using ParseText before
  895. p = _name.ParseName( p );
  896. if ( !p || !*p ) {
  897. return 0;
  898. }
  899. // Skip white space before =
  900. p = XMLUtil::SkipWhiteSpace( p );
  901. if ( !p || *p != '=' ) {
  902. return 0;
  903. }
  904. ++p; // move up to opening quote
  905. p = XMLUtil::SkipWhiteSpace( p );
  906. if ( *p != '\"' && *p != '\'' ) {
  907. return 0;
  908. }
  909. char endTag[2] = { *p, 0 };
  910. ++p; // move past opening quote
  911. p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
  912. return p;
  913. }
  914. void XMLAttribute::SetName( const char* n )
  915. {
  916. _name.SetStr( n );
  917. }
  918. XMLError XMLAttribute::QueryIntValue( int* value ) const
  919. {
  920. if ( XMLUtil::ToInt( Value(), value )) {
  921. return XML_NO_ERROR;
  922. }
  923. return XML_WRONG_ATTRIBUTE_TYPE;
  924. }
  925. XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
  926. {
  927. if ( XMLUtil::ToUnsigned( Value(), value )) {
  928. return XML_NO_ERROR;
  929. }
  930. return XML_WRONG_ATTRIBUTE_TYPE;
  931. }
  932. XMLError XMLAttribute::QueryBoolValue( bool* value ) const
  933. {
  934. if ( XMLUtil::ToBool( Value(), value )) {
  935. return XML_NO_ERROR;
  936. }
  937. return XML_WRONG_ATTRIBUTE_TYPE;
  938. }
  939. XMLError XMLAttribute::QueryFloatValue( float* value ) const
  940. {
  941. if ( XMLUtil::ToFloat( Value(), value )) {
  942. return XML_NO_ERROR;
  943. }
  944. return XML_WRONG_ATTRIBUTE_TYPE;
  945. }
  946. XMLError XMLAttribute::QueryDoubleValue( double* value ) const
  947. {
  948. if ( XMLUtil::ToDouble( Value(), value )) {
  949. return XML_NO_ERROR;
  950. }
  951. return XML_WRONG_ATTRIBUTE_TYPE;
  952. }
  953. void XMLAttribute::SetAttribute( const char* v )
  954. {
  955. _value.SetStr( v );
  956. }
  957. void XMLAttribute::SetAttribute( int v )
  958. {
  959. char buf[BUF_SIZE];
  960. XMLUtil::ToStr( v, buf, BUF_SIZE );
  961. _value.SetStr( buf );
  962. }
  963. void XMLAttribute::SetAttribute( unsigned v )
  964. {
  965. char buf[BUF_SIZE];
  966. XMLUtil::ToStr( v, buf, BUF_SIZE );
  967. _value.SetStr( buf );
  968. }
  969. void XMLAttribute::SetAttribute( bool v )
  970. {
  971. char buf[BUF_SIZE];
  972. XMLUtil::ToStr( v, buf, BUF_SIZE );
  973. _value.SetStr( buf );
  974. }
  975. void XMLAttribute::SetAttribute( double v )
  976. {
  977. char buf[BUF_SIZE];
  978. XMLUtil::ToStr( v, buf, BUF_SIZE );
  979. _value.SetStr( buf );
  980. }
  981. void XMLAttribute::SetAttribute( float v )
  982. {
  983. char buf[BUF_SIZE];
  984. XMLUtil::ToStr( v, buf, BUF_SIZE );
  985. _value.SetStr( buf );
  986. }
  987. // --------- XMLElement ---------- //
  988. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  989. _closingType( 0 ),
  990. _rootAttribute( 0 )
  991. {
  992. }
  993. XMLElement::~XMLElement()
  994. {
  995. while( _rootAttribute ) {
  996. XMLAttribute* next = _rootAttribute->_next;
  997. DeleteAttribute( _rootAttribute );
  998. _rootAttribute = next;
  999. }
  1000. }
  1001. XMLAttribute* XMLElement::FindAttribute( const char* name )
  1002. {
  1003. for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) {
  1004. if ( XMLUtil::StringEqual( a->Name(), name ) ) {
  1005. return a;
  1006. }
  1007. }
  1008. return 0;
  1009. }
  1010. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  1011. {
  1012. for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) {
  1013. if ( XMLUtil::StringEqual( a->Name(), name ) ) {
  1014. return a;
  1015. }
  1016. }
  1017. return 0;
  1018. }
  1019. const char* XMLElement::Attribute( const char* name, const char* value ) const
  1020. {
  1021. const XMLAttribute* a = FindAttribute( name );
  1022. if ( !a ) {
  1023. return 0;
  1024. }
  1025. if ( !value || XMLUtil::StringEqual( a->Value(), value )) {
  1026. return a->Value();
  1027. }
  1028. return 0;
  1029. }
  1030. const char* XMLElement::GetText() const
  1031. {
  1032. if ( FirstChild() && FirstChild()->ToText() ) {
  1033. return FirstChild()->ToText()->Value();
  1034. }
  1035. return 0;
  1036. }
  1037. void XMLElement::SetText( const char* inText )
  1038. {
  1039. if ( FirstChild() && FirstChild()->ToText() )
  1040. FirstChild()->SetValue( inText );
  1041. else {
  1042. XMLText* theText = GetDocument()->NewText( inText );
  1043. InsertFirstChild( theText );
  1044. }
  1045. }
  1046. void XMLElement::SetText( int v )
  1047. {
  1048. char buf[BUF_SIZE];
  1049. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1050. SetText( buf );
  1051. }
  1052. void XMLElement::SetText( unsigned v )
  1053. {
  1054. char buf[BUF_SIZE];
  1055. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1056. SetText( buf );
  1057. }
  1058. void XMLElement::SetText( bool v )
  1059. {
  1060. char buf[BUF_SIZE];
  1061. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1062. SetText( buf );
  1063. }
  1064. void XMLElement::SetText( float v )
  1065. {
  1066. char buf[BUF_SIZE];
  1067. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1068. SetText( buf );
  1069. }
  1070. void XMLElement::SetText( double v )
  1071. {
  1072. char buf[BUF_SIZE];
  1073. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1074. SetText( buf );
  1075. }
  1076. XMLError XMLElement::QueryIntText( int* ival ) const
  1077. {
  1078. if ( FirstChild() && FirstChild()->ToText() ) {
  1079. const char* t = FirstChild()->ToText()->Value();
  1080. if ( XMLUtil::ToInt( t, ival ) ) {
  1081. return XML_SUCCESS;
  1082. }
  1083. return XML_CAN_NOT_CONVERT_TEXT;
  1084. }
  1085. return XML_NO_TEXT_NODE;
  1086. }
  1087. XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const
  1088. {
  1089. if ( FirstChild() && FirstChild()->ToText() ) {
  1090. const char* t = FirstChild()->ToText()->Value();
  1091. if ( XMLUtil::ToUnsigned( t, uval ) ) {
  1092. return XML_SUCCESS;
  1093. }
  1094. return XML_CAN_NOT_CONVERT_TEXT;
  1095. }
  1096. return XML_NO_TEXT_NODE;
  1097. }
  1098. XMLError XMLElement::QueryBoolText( bool* bval ) const
  1099. {
  1100. if ( FirstChild() && FirstChild()->ToText() ) {
  1101. const char* t = FirstChild()->ToText()->Value();
  1102. if ( XMLUtil::ToBool( t, bval ) ) {
  1103. return XML_SUCCESS;
  1104. }
  1105. return XML_CAN_NOT_CONVERT_TEXT;
  1106. }
  1107. return XML_NO_TEXT_NODE;
  1108. }
  1109. XMLError XMLElement::QueryDoubleText( double* dval ) const
  1110. {
  1111. if ( FirstChild() && FirstChild()->ToText() ) {
  1112. const char* t = FirstChild()->ToText()->Value();
  1113. if ( XMLUtil::ToDouble( t, dval ) ) {
  1114. return XML_SUCCESS;
  1115. }
  1116. return XML_CAN_NOT_CONVERT_TEXT;
  1117. }
  1118. return XML_NO_TEXT_NODE;
  1119. }
  1120. XMLError XMLElement::QueryFloatText( float* fval ) const
  1121. {
  1122. if ( FirstChild() && FirstChild()->ToText() ) {
  1123. const char* t = FirstChild()->ToText()->Value();
  1124. if ( XMLUtil::ToFloat( t, fval ) ) {
  1125. return XML_SUCCESS;
  1126. }
  1127. return XML_CAN_NOT_CONVERT_TEXT;
  1128. }
  1129. return XML_NO_TEXT_NODE;
  1130. }
  1131. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  1132. {
  1133. XMLAttribute* last = 0;
  1134. XMLAttribute* attrib = 0;
  1135. for( attrib = _rootAttribute;
  1136. attrib;
  1137. last = attrib, attrib = attrib->_next ) {
  1138. if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {
  1139. break;
  1140. }
  1141. }
  1142. if ( !attrib ) {
  1143. attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1144. attrib->_memPool = &_document->_attributePool;
  1145. if ( last ) {
  1146. last->_next = attrib;
  1147. }
  1148. else {
  1149. _rootAttribute = attrib;
  1150. }
  1151. attrib->SetName( name );
  1152. attrib->_memPool->SetTracked(); // always created and linked.
  1153. }
  1154. return attrib;
  1155. }
  1156. void XMLElement::DeleteAttribute( const char* name )
  1157. {
  1158. XMLAttribute* prev = 0;
  1159. for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) {
  1160. if ( XMLUtil::StringEqual( name, a->Name() ) ) {
  1161. if ( prev ) {
  1162. prev->_next = a->_next;
  1163. }
  1164. else {
  1165. _rootAttribute = a->_next;
  1166. }
  1167. DeleteAttribute( a );
  1168. break;
  1169. }
  1170. prev = a;
  1171. }
  1172. }
  1173. char* XMLElement::ParseAttributes( char* p )
  1174. {
  1175. const char* start = p;
  1176. XMLAttribute* prevAttribute = 0;
  1177. // Read the attributes.
  1178. while( p ) {
  1179. p = XMLUtil::SkipWhiteSpace( p );
  1180. if ( !p || !(*p) ) {
  1181. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );
  1182. return 0;
  1183. }
  1184. // attribute.
  1185. if (XMLUtil::IsNameStartChar( *p ) ) {
  1186. XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1187. attrib->_memPool = &_document->_attributePool;
  1188. attrib->_memPool->SetTracked();
  1189. p = attrib->ParseDeep( p, _document->ProcessEntities() );
  1190. if ( !p || Attribute( attrib->Name() ) ) {
  1191. DeleteAttribute( attrib );
  1192. _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );
  1193. return 0;
  1194. }
  1195. // There is a minor bug here: if the attribute in the source xml
  1196. // document is duplicated, it will not be detected and the
  1197. // attribute will be doubly added. However, tracking the 'prevAttribute'
  1198. // avoids re-scanning the attribute list. Preferring performance for
  1199. // now, may reconsider in the future.
  1200. if ( prevAttribute ) {
  1201. prevAttribute->_next = attrib;
  1202. }
  1203. else {
  1204. _rootAttribute = attrib;
  1205. }
  1206. prevAttribute = attrib;
  1207. }
  1208. // end of the tag
  1209. else if ( *p == '/' && *(p+1) == '>' ) {
  1210. _closingType = CLOSED;
  1211. return p+2; // done; sealed element.
  1212. }
  1213. // end of the tag
  1214. else if ( *p == '>' ) {
  1215. ++p;
  1216. break;
  1217. }
  1218. else {
  1219. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );
  1220. return 0;
  1221. }
  1222. }
  1223. return p;
  1224. }
  1225. void XMLElement::DeleteAttribute( XMLAttribute* attribute )
  1226. {
  1227. if ( attribute == 0 ) {
  1228. return;
  1229. }
  1230. MemPool* pool = attribute->_memPool;
  1231. attribute->~XMLAttribute();
  1232. pool->Free( attribute );
  1233. }
  1234. //
  1235. // <ele></ele>
  1236. // <ele>foo<b>bar</b></ele>
  1237. //
  1238. char* XMLElement::ParseDeep( char* p, StrPair* strPair )
  1239. {
  1240. // Read the element name.
  1241. p = XMLUtil::SkipWhiteSpace( p );
  1242. if ( !p ) {
  1243. return 0;
  1244. }
  1245. // The closing element is the </element> form. It is
  1246. // parsed just like a regular element then deleted from
  1247. // the DOM.
  1248. if ( *p == '/' ) {
  1249. _closingType = CLOSING;
  1250. ++p;
  1251. }
  1252. p = _value.ParseName( p );
  1253. if ( _value.Empty() ) {
  1254. return 0;
  1255. }
  1256. p = ParseAttributes( p );
  1257. if ( !p || !*p || _closingType ) {
  1258. return p;
  1259. }
  1260. p = XMLNode::ParseDeep( p, strPair );
  1261. return p;
  1262. }
  1263. XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
  1264. {
  1265. if ( !doc ) {
  1266. doc = _document;
  1267. }
  1268. XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
  1269. for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
  1270. element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
  1271. }
  1272. return element;
  1273. }
  1274. bool XMLElement::ShallowEqual( const XMLNode* compare ) const
  1275. {
  1276. const XMLElement* other = compare->ToElement();
  1277. if ( other && XMLUtil::StringEqual( other->Value(), Value() )) {
  1278. const XMLAttribute* a=FirstAttribute();
  1279. const XMLAttribute* b=other->FirstAttribute();
  1280. while ( a && b ) {
  1281. if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
  1282. return false;
  1283. }
  1284. a = a->Next();
  1285. b = b->Next();
  1286. }
  1287. if ( a || b ) {
  1288. // different count
  1289. return false;
  1290. }
  1291. return true;
  1292. }
  1293. return false;
  1294. }
  1295. bool XMLElement::Accept( XMLVisitor* visitor ) const
  1296. {
  1297. if ( visitor->VisitEnter( *this, _rootAttribute ) ) {
  1298. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
  1299. if ( !node->Accept( visitor ) ) {
  1300. break;
  1301. }
  1302. }
  1303. }
  1304. return visitor->VisitExit( *this );
  1305. }
  1306. // --------- XMLDocument ----------- //
  1307. XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) :
  1308. XMLNode( 0 ),
  1309. _writeBOM( false ),
  1310. _processEntities( processEntities ),
  1311. _errorID( XML_NO_ERROR ),
  1312. _whitespace( whitespace ),
  1313. _errorStr1( 0 ),
  1314. _errorStr2( 0 ),
  1315. _charBuffer( 0 )
  1316. {
  1317. _document = this; // avoid warning about 'this' in initializer list
  1318. }
  1319. XMLDocument::~XMLDocument()
  1320. {
  1321. DeleteChildren();
  1322. delete [] _charBuffer;
  1323. #if 0
  1324. _textPool.Trace( "text" );
  1325. _elementPool.Trace( "element" );
  1326. _commentPool.Trace( "comment" );
  1327. _attributePool.Trace( "attribute" );
  1328. #endif
  1329. #ifdef DEBUG
  1330. if ( Error() == false ) {
  1331. TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() );
  1332. TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() );
  1333. TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() );
  1334. TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() );
  1335. }
  1336. #endif
  1337. }
  1338. void XMLDocument::Clear()
  1339. {
  1340. DeleteChildren();
  1341. _errorID = XML_NO_ERROR;
  1342. _errorStr1 = 0;
  1343. _errorStr2 = 0;
  1344. delete [] _charBuffer;
  1345. _charBuffer = 0;
  1346. }
  1347. XMLElement* XMLDocument::NewElement( const char* name )
  1348. {
  1349. XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this );
  1350. ele->_memPool = &_elementPool;
  1351. ele->SetName( name );
  1352. return ele;
  1353. }
  1354. XMLComment* XMLDocument::NewComment( const char* str )
  1355. {
  1356. XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this );
  1357. comment->_memPool = &_commentPool;
  1358. comment->SetValue( str );
  1359. return comment;
  1360. }
  1361. XMLText* XMLDocument::NewText( const char* str )
  1362. {
  1363. XMLText* text = new (_textPool.Alloc()) XMLText( this );
  1364. text->_memPool = &_textPool;
  1365. text->SetValue( str );
  1366. return text;
  1367. }
  1368. XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
  1369. {
  1370. XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this );
  1371. dec->_memPool = &_commentPool;
  1372. dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
  1373. return dec;
  1374. }
  1375. XMLUnknown* XMLDocument::NewUnknown( const char* str )
  1376. {
  1377. XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this );
  1378. unk->_memPool = &_commentPool;
  1379. unk->SetValue( str );
  1380. return unk;
  1381. }
  1382. static FILE* callfopen( const char* filepath, const char* mode )
  1383. {
  1384. #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
  1385. FILE* fp = 0;
  1386. errno_t err = fopen_s( &fp, filepath, mode );
  1387. if ( err ) {
  1388. return 0;
  1389. }
  1390. #else
  1391. FILE* fp = fopen( filepath, mode );
  1392. #endif
  1393. return fp;
  1394. }
  1395. XMLError XMLDocument::LoadFile( const char* filename )
  1396. {
  1397. Clear();
  1398. FILE* fp = callfopen( filename, "rb" );
  1399. if ( !fp ) {
  1400. SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
  1401. return _errorID;
  1402. }
  1403. LoadFile( fp );
  1404. fclose( fp );
  1405. return _errorID;
  1406. }
  1407. XMLError XMLDocument::LoadFile( FILE* fp )
  1408. {
  1409. Clear();
  1410. fseek( fp, 0, SEEK_SET );
  1411. if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
  1412. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1413. return _errorID;
  1414. }
  1415. fseek( fp, 0, SEEK_END );
  1416. const long filelength = ftell( fp );
  1417. fseek( fp, 0, SEEK_SET );
  1418. if ( filelength == -1L ) {
  1419. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1420. return _errorID;
  1421. }
  1422. const size_t size = filelength;
  1423. if ( size == 0 ) {
  1424. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1425. return _errorID;
  1426. }
  1427. _charBuffer = new char[size+1];
  1428. size_t read = fread( _charBuffer, 1, size, fp );
  1429. if ( read != size ) {
  1430. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1431. return _errorID;
  1432. }
  1433. _charBuffer[size] = 0;
  1434. const char* p = _charBuffer;
  1435. p = XMLUtil::SkipWhiteSpace( p );
  1436. p = XMLUtil::ReadBOM( p, &_writeBOM );
  1437. if ( !p || !*p ) {
  1438. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1439. return _errorID;
  1440. }
  1441. ParseDeep( _charBuffer + (p-_charBuffer), 0 );
  1442. return _errorID;
  1443. }
  1444. XMLError XMLDocument::SaveFile( const char* filename, bool compact )
  1445. {
  1446. FILE* fp = callfopen( filename, "w" );
  1447. if ( !fp ) {
  1448. SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
  1449. return _errorID;
  1450. }
  1451. SaveFile(fp, compact);
  1452. fclose( fp );
  1453. return _errorID;
  1454. }
  1455. XMLError XMLDocument::SaveFile( FILE* fp, bool compact )
  1456. {
  1457. XMLPrinter stream( fp, compact );
  1458. Print( &stream );
  1459. return _errorID;
  1460. }
  1461. XMLError XMLDocument::Parse( const char* p, size_t len )
  1462. {
  1463. const char* start = p;
  1464. Clear();
  1465. if ( len == 0 || !p || !*p ) {
  1466. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1467. return _errorID;
  1468. }
  1469. if ( len == (size_t)(-1) ) {
  1470. len = strlen( p );
  1471. }
  1472. _charBuffer = new char[ len+1 ];
  1473. memcpy( _charBuffer, p, len );
  1474. _charBuffer[len] = 0;
  1475. p = XMLUtil::SkipWhiteSpace( p );
  1476. p = XMLUtil::ReadBOM( p, &_writeBOM );
  1477. if ( !p || !*p ) {
  1478. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1479. return _errorID;
  1480. }
  1481. ptrdiff_t delta = p - start; // skip initial whitespace, BOM, etc.
  1482. ParseDeep( _charBuffer+delta, 0 );
  1483. return _errorID;
  1484. }
  1485. void XMLDocument::Print( XMLPrinter* streamer ) const
  1486. {
  1487. XMLPrinter stdStreamer( stdout );
  1488. if ( !streamer ) {
  1489. streamer = &stdStreamer;
  1490. }
  1491. Accept( streamer );
  1492. }
  1493. void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 )
  1494. {
  1495. _errorID = error;
  1496. _errorStr1 = str1;
  1497. _errorStr2 = str2;
  1498. }
  1499. void XMLDocument::PrintError() const
  1500. {
  1501. if ( _errorID ) {
  1502. static const int LEN = 20;
  1503. char buf1[LEN] = { 0 };
  1504. char buf2[LEN] = { 0 };
  1505. if ( _errorStr1 ) {
  1506. TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 );
  1507. }
  1508. if ( _errorStr2 ) {
  1509. TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 );
  1510. }
  1511. printf( "XMLDocument error id=%d str1=%s str2=%s\n",
  1512. _errorID, buf1, buf2 );
  1513. }
  1514. }
  1515. XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
  1516. _elementJustOpened( false ),
  1517. _firstElement( true ),
  1518. _fp( file ),
  1519. _depth( depth ),
  1520. _textDepth( -1 ),
  1521. _processEntities( true ),
  1522. _compactMode( compact )
  1523. {
  1524. for( int i=0; i<ENTITY_RANGE; ++i ) {
  1525. _entityFlag[i] = false;
  1526. _restrictedEntityFlag[i] = false;
  1527. }
  1528. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1529. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  1530. if ( entities[i].value < ENTITY_RANGE ) {
  1531. _entityFlag[ (int)entities[i].value ] = true;
  1532. }
  1533. }
  1534. _restrictedEntityFlag[(int)'&'] = true;
  1535. _restrictedEntityFlag[(int)'<'] = true;
  1536. _restrictedEntityFlag[(int)'>'] = true; // not required, but consistency is nice
  1537. _buffer.Push( 0 );
  1538. }
  1539. void XMLPrinter::Print( const char* format, ... )
  1540. {
  1541. va_list va;
  1542. va_start( va, format );
  1543. if ( _fp ) {
  1544. vfprintf( _fp, format, va );
  1545. }
  1546. else {
  1547. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1548. #if defined(WINCE)
  1549. int len = 512;
  1550. do {
  1551. len = len*2;
  1552. char* str = new char[len]();
  1553. len = _vsnprintf(str, len, format, va);
  1554. delete[] str;
  1555. }while (len < 0);
  1556. #else
  1557. int len = _vscprintf( format, va );
  1558. #endif
  1559. #else
  1560. int len = vsnprintf( 0, 0, format, va );
  1561. #endif
  1562. // Close out and re-start the va-args
  1563. va_end( va );
  1564. va_start( va, format );
  1565. char* p = _buffer.PushArr( len ) - 1; // back up over the null terminator.
  1566. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1567. #if defined(WINCE)
  1568. _vsnprintf( p, len+1, format, va );
  1569. #else
  1570. vsnprintf_s( p, len+1, _TRUNCATE, format, va );
  1571. #endif
  1572. #else
  1573. vsnprintf( p, len+1, format, va );
  1574. #endif
  1575. }
  1576. va_end( va );
  1577. }
  1578. void XMLPrinter::PrintSpace( int depth )
  1579. {
  1580. for( int i=0; i<depth; ++i ) {
  1581. Print( " " );
  1582. }
  1583. }
  1584. void XMLPrinter::PrintString( const char* p, bool restricted )
  1585. {
  1586. // Look for runs of bytes between entities to print.
  1587. const char* q = p;
  1588. const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag;
  1589. if ( _processEntities ) {
  1590. while ( *q ) {
  1591. // Remember, char is sometimes signed. (How many times has that bitten me?)
  1592. if ( *q > 0 && *q < ENTITY_RANGE ) {
  1593. // Check for entities. If one is found, flush
  1594. // the stream up until the entity, write the
  1595. // entity, and keep looking.
  1596. if ( flag[(unsigned)(*q)] ) {
  1597. while ( p < q ) {
  1598. Print( "%c", *p );
  1599. ++p;
  1600. }
  1601. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1602. if ( entities[i].value == *q ) {
  1603. Print( "&%s;", entities[i].pattern );
  1604. break;
  1605. }
  1606. }
  1607. ++p;
  1608. }
  1609. }
  1610. ++q;
  1611. }
  1612. }
  1613. // Flush the remaining string. This will be the entire
  1614. // string if an entity wasn't found.
  1615. if ( !_processEntities || (q-p > 0) ) {
  1616. Print( "%s", p );
  1617. }
  1618. }
  1619. void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
  1620. {
  1621. if ( writeBOM ) {
  1622. static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
  1623. Print( "%s", bom );
  1624. }
  1625. if ( writeDec ) {
  1626. PushDeclaration( "xml version=\"1.0\"" );
  1627. }
  1628. }
  1629. void XMLPrinter::OpenElement( const char* name, bool compactMode )
  1630. {
  1631. if ( _elementJustOpened ) {
  1632. SealElement();
  1633. }
  1634. _stack.Push( name );
  1635. if ( _textDepth < 0 && !_firstElement && !compactMode ) {
  1636. Print( "\n" );
  1637. }
  1638. if ( !compactMode ) {
  1639. PrintSpace( _depth );
  1640. }
  1641. Print( "<%s", name );
  1642. _elementJustOpened = true;
  1643. _firstElement = false;
  1644. ++_depth;
  1645. }
  1646. void XMLPrinter::PushAttribute( const char* name, const char* value )
  1647. {
  1648. TIXMLASSERT( _elementJustOpened );
  1649. Print( " %s=\"", name );
  1650. PrintString( value, false );
  1651. Print( "\"" );
  1652. }
  1653. void XMLPrinter::PushAttribute( const char* name, int v )
  1654. {
  1655. char buf[BUF_SIZE];
  1656. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1657. PushAttribute( name, buf );
  1658. }
  1659. void XMLPrinter::PushAttribute( const char* name, unsigned v )
  1660. {
  1661. char buf[BUF_SIZE];
  1662. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1663. PushAttribute( name, buf );
  1664. }
  1665. void XMLPrinter::PushAttribute( const char* name, bool v )
  1666. {
  1667. char buf[BUF_SIZE];
  1668. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1669. PushAttribute( name, buf );
  1670. }
  1671. void XMLPrinter::PushAttribute( const char* name, double v )
  1672. {
  1673. char buf[BUF_SIZE];
  1674. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1675. PushAttribute( name, buf );
  1676. }
  1677. void XMLPrinter::CloseElement( bool compactMode )
  1678. {
  1679. --_depth;
  1680. const char* name = _stack.Pop();
  1681. if ( _elementJustOpened ) {
  1682. Print( "/>" );
  1683. }
  1684. else {
  1685. if ( _textDepth < 0 && !compactMode) {
  1686. Print( "\n" );
  1687. PrintSpace( _depth );
  1688. }
  1689. Print( "</%s>", name );
  1690. }
  1691. if ( _textDepth == _depth ) {
  1692. _textDepth = -1;
  1693. }
  1694. if ( _depth == 0 && !compactMode) {
  1695. Print( "\n" );
  1696. }
  1697. _elementJustOpened = false;
  1698. }
  1699. void XMLPrinter::SealElement()
  1700. {
  1701. _elementJustOpened = false;
  1702. Print( ">" );
  1703. }
  1704. void XMLPrinter::PushText( const char* text, bool cdata )
  1705. {
  1706. _textDepth = _depth-1;
  1707. if ( _elementJustOpened ) {
  1708. SealElement();
  1709. }
  1710. if ( cdata ) {
  1711. Print( "<![CDATA[" );
  1712. Print( "%s", text );
  1713. Print( "]]>" );
  1714. }
  1715. else {
  1716. PrintString( text, true );
  1717. }
  1718. }
  1719. void XMLPrinter::PushText( int value )
  1720. {
  1721. char buf[BUF_SIZE];
  1722. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1723. PushText( buf, false );
  1724. }
  1725. void XMLPrinter::PushText( unsigned value )
  1726. {
  1727. char buf[BUF_SIZE];
  1728. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1729. PushText( buf, false );
  1730. }
  1731. void XMLPrinter::PushText( bool value )
  1732. {
  1733. char buf[BUF_SIZE];
  1734. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1735. PushText( buf, false );
  1736. }
  1737. void XMLPrinter::PushText( float value )
  1738. {
  1739. char buf[BUF_SIZE];
  1740. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1741. PushText( buf, false );
  1742. }
  1743. void XMLPrinter::PushText( double value )
  1744. {
  1745. char buf[BUF_SIZE];
  1746. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1747. PushText( buf, false );
  1748. }
  1749. void XMLPrinter::PushComment( const char* comment )
  1750. {
  1751. if ( _elementJustOpened ) {
  1752. SealElement();
  1753. }
  1754. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1755. Print( "\n" );
  1756. PrintSpace( _depth );
  1757. }
  1758. _firstElement = false;
  1759. Print( "<!--%s-->", comment );
  1760. }
  1761. void XMLPrinter::PushDeclaration( const char* value )
  1762. {
  1763. if ( _elementJustOpened ) {
  1764. SealElement();
  1765. }
  1766. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1767. Print( "\n" );
  1768. PrintSpace( _depth );
  1769. }
  1770. _firstElement = false;
  1771. Print( "<?%s?>", value );
  1772. }
  1773. void XMLPrinter::PushUnknown( const char* value )
  1774. {
  1775. if ( _elementJustOpened ) {
  1776. SealElement();
  1777. }
  1778. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1779. Print( "\n" );
  1780. PrintSpace( _depth );
  1781. }
  1782. _firstElement = false;
  1783. Print( "<!%s>", value );
  1784. }
  1785. bool XMLPrinter::VisitEnter( const XMLDocument& doc )
  1786. {
  1787. _processEntities = doc.ProcessEntities();
  1788. if ( doc.HasBOM() ) {
  1789. PushHeader( true, false );
  1790. }
  1791. return true;
  1792. }
  1793. bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  1794. {
  1795. const XMLElement* parentElem = element.Parent()->ToElement();
  1796. bool compactMode = parentElem ? CompactMode(*parentElem) : _compactMode;
  1797. OpenElement( element.Name(), compactMode );
  1798. while ( attribute ) {
  1799. PushAttribute( attribute->Name(), attribute->Value() );
  1800. attribute = attribute->Next();
  1801. }
  1802. return true;
  1803. }
  1804. bool XMLPrinter::VisitExit( const XMLElement& element )
  1805. {
  1806. CloseElement( CompactMode(element) );
  1807. return true;
  1808. }
  1809. bool XMLPrinter::Visit( const XMLText& text )
  1810. {
  1811. PushText( text.Value(), text.CData() );
  1812. return true;
  1813. }
  1814. bool XMLPrinter::Visit( const XMLComment& comment )
  1815. {
  1816. PushComment( comment.Value() );
  1817. return true;
  1818. }
  1819. bool XMLPrinter::Visit( const XMLDeclaration& declaration )
  1820. {
  1821. PushDeclaration( declaration.Value() );
  1822. return true;
  1823. }
  1824. bool XMLPrinter::Visit( const XMLUnknown& unknown )
  1825. {
  1826. PushUnknown( unknown.Value() );
  1827. return true;
  1828. }
  1829. } // namespace tinyxml2