tinyxml2.cpp 54 KB

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