1
0

tinyxml2.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221
  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. std::string XMLUtil::ToErrorName( const XMLError errorID )
  404. {
  405. #if __cplusplus > 199711LL
  406. return ErrorNames[errorID];
  407. #else
  408. return std::string("Use C++11 or higher to use this function");
  409. #endif
  410. }
  411. char* XMLDocument::Identify( char* p, XMLNode** node )
  412. {
  413. XMLNode* returnNode = 0;
  414. char* start = p;
  415. p = XMLUtil::SkipWhiteSpace( p );
  416. if( !p || !*p ) {
  417. return p;
  418. }
  419. // What is this thing?
  420. // These strings define the matching patters:
  421. static const char* xmlHeader = { "<?" };
  422. static const char* commentHeader = { "<!--" };
  423. static const char* dtdHeader = { "<!" };
  424. static const char* cdataHeader = { "<![CDATA[" };
  425. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  426. static const int xmlHeaderLen = 2;
  427. static const int commentHeaderLen = 4;
  428. static const int dtdHeaderLen = 2;
  429. static const int cdataHeaderLen = 9;
  430. static const int elementHeaderLen = 1;
  431. #if defined(_MSC_VER)
  432. #pragma warning ( push )
  433. #pragma warning ( disable : 4127 )
  434. #endif
  435. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
  436. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
  437. #if defined(_MSC_VER)
  438. #pragma warning (pop)
  439. #endif
  440. if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
  441. returnNode = new (_commentPool.Alloc()) XMLDeclaration( this );
  442. returnNode->_memPool = &_commentPool;
  443. p += xmlHeaderLen;
  444. }
  445. else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  446. returnNode = new (_commentPool.Alloc()) XMLComment( this );
  447. returnNode->_memPool = &_commentPool;
  448. p += commentHeaderLen;
  449. }
  450. else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
  451. XMLText* text = new (_textPool.Alloc()) XMLText( this );
  452. returnNode = text;
  453. returnNode->_memPool = &_textPool;
  454. p += cdataHeaderLen;
  455. text->SetCData( true );
  456. }
  457. else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
  458. returnNode = new (_commentPool.Alloc()) XMLUnknown( this );
  459. returnNode->_memPool = &_commentPool;
  460. p += dtdHeaderLen;
  461. }
  462. else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
  463. returnNode = new (_elementPool.Alloc()) XMLElement( this );
  464. returnNode->_memPool = &_elementPool;
  465. p += elementHeaderLen;
  466. }
  467. else {
  468. returnNode = new (_textPool.Alloc()) XMLText( this );
  469. returnNode->_memPool = &_textPool;
  470. p = start; // Back it up, all the text counts.
  471. }
  472. *node = returnNode;
  473. return p;
  474. }
  475. bool XMLDocument::Accept( XMLVisitor* visitor ) const
  476. {
  477. if ( visitor->VisitEnter( *this ) ) {
  478. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
  479. if ( !node->Accept( visitor ) ) {
  480. break;
  481. }
  482. }
  483. }
  484. return visitor->VisitExit( *this );
  485. }
  486. // --------- XMLNode ----------- //
  487. XMLNode::XMLNode( XMLDocument* doc ) :
  488. _document( doc ),
  489. _parent( 0 ),
  490. _firstChild( 0 ), _lastChild( 0 ),
  491. _prev( 0 ), _next( 0 ),
  492. _memPool( 0 )
  493. {
  494. }
  495. XMLNode::~XMLNode()
  496. {
  497. DeleteChildren();
  498. if ( _parent ) {
  499. _parent->Unlink( this );
  500. }
  501. }
  502. const char* XMLNode::Value() const
  503. {
  504. return _value.GetStr();
  505. }
  506. void XMLNode::SetValue( const char* str, bool staticMem )
  507. {
  508. if ( staticMem ) {
  509. _value.SetInternedStr( str );
  510. }
  511. else {
  512. _value.SetStr( str );
  513. }
  514. }
  515. void XMLNode::DeleteChildren()
  516. {
  517. while( _firstChild ) {
  518. XMLNode* node = _firstChild;
  519. Unlink( node );
  520. DeleteNode( node );
  521. }
  522. _firstChild = _lastChild = 0;
  523. }
  524. void XMLNode::Unlink( XMLNode* child )
  525. {
  526. if ( child == _firstChild ) {
  527. _firstChild = _firstChild->_next;
  528. }
  529. if ( child == _lastChild ) {
  530. _lastChild = _lastChild->_prev;
  531. }
  532. if ( child->_prev ) {
  533. child->_prev->_next = child->_next;
  534. }
  535. if ( child->_next ) {
  536. child->_next->_prev = child->_prev;
  537. }
  538. child->_parent = 0;
  539. }
  540. void XMLNode::DeleteChild( XMLNode* node )
  541. {
  542. TIXMLASSERT( node->_parent == this );
  543. DeleteNode( node );
  544. }
  545. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  546. {
  547. if (addThis->_document != _document)
  548. return 0;
  549. if (addThis->_parent)
  550. addThis->_parent->Unlink( addThis );
  551. else
  552. addThis->_memPool->SetTracked();
  553. if ( _lastChild ) {
  554. TIXMLASSERT( _firstChild );
  555. TIXMLASSERT( _lastChild->_next == 0 );
  556. _lastChild->_next = addThis;
  557. addThis->_prev = _lastChild;
  558. _lastChild = addThis;
  559. addThis->_next = 0;
  560. }
  561. else {
  562. TIXMLASSERT( _firstChild == 0 );
  563. _firstChild = _lastChild = addThis;
  564. addThis->_prev = 0;
  565. addThis->_next = 0;
  566. }
  567. addThis->_parent = this;
  568. return addThis;
  569. }
  570. XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
  571. {
  572. if (addThis->_document != _document)
  573. return 0;
  574. if (addThis->_parent)
  575. addThis->_parent->Unlink( addThis );
  576. else
  577. addThis->_memPool->SetTracked();
  578. if ( _firstChild ) {
  579. TIXMLASSERT( _lastChild );
  580. TIXMLASSERT( _firstChild->_prev == 0 );
  581. _firstChild->_prev = addThis;
  582. addThis->_next = _firstChild;
  583. _firstChild = addThis;
  584. addThis->_prev = 0;
  585. }
  586. else {
  587. TIXMLASSERT( _lastChild == 0 );
  588. _firstChild = _lastChild = addThis;
  589. addThis->_prev = 0;
  590. addThis->_next = 0;
  591. }
  592. addThis->_parent = this;
  593. return addThis;
  594. }
  595. XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
  596. {
  597. if (addThis->_document != _document)
  598. return 0;
  599. TIXMLASSERT( afterThis->_parent == this );
  600. if ( afterThis->_parent != this ) {
  601. return 0;
  602. }
  603. if ( afterThis->_next == 0 ) {
  604. // The last node or the only node.
  605. return InsertEndChild( addThis );
  606. }
  607. if (addThis->_parent)
  608. addThis->_parent->Unlink( addThis );
  609. else
  610. addThis->_memPool->SetTracked();
  611. addThis->_prev = afterThis;
  612. addThis->_next = afterThis->_next;
  613. afterThis->_next->_prev = addThis;
  614. afterThis->_next = addThis;
  615. addThis->_parent = this;
  616. return addThis;
  617. }
  618. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  619. {
  620. for( XMLNode* node=_firstChild; node; node=node->_next ) {
  621. XMLElement* element = node->ToElement();
  622. if ( element ) {
  623. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  624. return element;
  625. }
  626. }
  627. }
  628. return 0;
  629. }
  630. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  631. {
  632. for( XMLNode* node=_lastChild; node; node=node->_prev ) {
  633. XMLElement* element = node->ToElement();
  634. if ( element ) {
  635. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  636. return element;
  637. }
  638. }
  639. }
  640. return 0;
  641. }
  642. const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
  643. {
  644. for( XMLNode* node=this->_next; node; node = node->_next ) {
  645. const XMLElement* element = node->ToElement();
  646. if ( element
  647. && (!value || XMLUtil::StringEqual( value, node->Value() ))) {
  648. return element;
  649. }
  650. }
  651. return 0;
  652. }
  653. const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
  654. {
  655. for( XMLNode* node=_prev; node; node = node->_prev ) {
  656. const XMLElement* element = node->ToElement();
  657. if ( element
  658. && (!value || XMLUtil::StringEqual( value, node->Value() ))) {
  659. return element;
  660. }
  661. }
  662. return 0;
  663. }
  664. char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
  665. {
  666. // This is a recursive method, but thinking about it "at the current level"
  667. // it is a pretty simple flat list:
  668. // <foo/>
  669. // <!-- comment -->
  670. //
  671. // With a special case:
  672. // <foo>
  673. // </foo>
  674. // <!-- comment -->
  675. //
  676. // Where the closing element (/foo) *must* be the next thing after the opening
  677. // element, and the names must match. BUT the tricky bit is that the closing
  678. // element will be read by the child.
  679. //
  680. // 'endTag' is the end tag for this node, it is returned by a call to a child.
  681. // 'parentEnd' is the end tag for the parent, which is filled in and returned.
  682. while( p && *p ) {
  683. XMLNode* node = 0;
  684. p = _document->Identify( p, &node );
  685. if ( p == 0 || node == 0 ) {
  686. break;
  687. }
  688. StrPair endTag;
  689. p = node->ParseDeep( p, &endTag );
  690. if ( !p ) {
  691. DeleteNode( node );
  692. node = 0;
  693. if ( !_document->Error() ) {
  694. _document->SetError( XML_ERROR_PARSING, 0, 0 );
  695. }
  696. break;
  697. }
  698. XMLElement* ele = node->ToElement();
  699. // We read the end tag. Return it to the parent.
  700. if ( ele && ele->ClosingType() == XMLElement::CLOSING ) {
  701. if ( parentEnd ) {
  702. *parentEnd = static_cast<XMLElement*>(node)->_value;
  703. }
  704. node->_memPool->SetTracked(); // created and then immediately deleted.
  705. DeleteNode( node );
  706. return p;
  707. }
  708. // Handle an end tag returned to this level.
  709. // And handle a bunch of annoying errors.
  710. if ( ele ) {
  711. if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
  712. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  713. p = 0;
  714. }
  715. else 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() ) {
  720. if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
  721. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  722. p = 0;
  723. }
  724. }
  725. }
  726. if ( p == 0 ) {
  727. DeleteNode( node );
  728. node = 0;
  729. }
  730. if ( node ) {
  731. this->InsertEndChild( node );
  732. }
  733. }
  734. return 0;
  735. }
  736. void XMLNode::DeleteNode( XMLNode* node )
  737. {
  738. if ( node == 0 ) {
  739. return;
  740. }
  741. MemPool* pool = node->_memPool;
  742. node->~XMLNode();
  743. pool->Free( node );
  744. }
  745. // --------- XMLText ---------- //
  746. char* XMLText::ParseDeep( char* p, StrPair* )
  747. {
  748. const char* start = p;
  749. if ( this->CData() ) {
  750. p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  751. if ( !p ) {
  752. _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
  753. }
  754. return p;
  755. }
  756. else {
  757. int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES;
  758. if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) {
  759. flags |= StrPair::COLLAPSE_WHITESPACE;
  760. }
  761. p = _value.ParseText( p, "<", flags );
  762. if ( !p ) {
  763. _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
  764. }
  765. if ( p && *p ) {
  766. return p-1;
  767. }
  768. }
  769. return 0;
  770. }
  771. XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
  772. {
  773. if ( !doc ) {
  774. doc = _document;
  775. }
  776. XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
  777. text->SetCData( this->CData() );
  778. return text;
  779. }
  780. bool XMLText::ShallowEqual( const XMLNode* compare ) const
  781. {
  782. return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() ));
  783. }
  784. bool XMLText::Accept( XMLVisitor* visitor ) const
  785. {
  786. return visitor->Visit( *this );
  787. }
  788. // --------- XMLComment ---------- //
  789. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  790. {
  791. }
  792. XMLComment::~XMLComment()
  793. {
  794. }
  795. char* XMLComment::ParseDeep( char* p, StrPair* )
  796. {
  797. // Comment parses as text.
  798. const char* start = p;
  799. p = _value.ParseText( p, "-->", StrPair::COMMENT );
  800. if ( p == 0 ) {
  801. _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
  802. }
  803. return p;
  804. }
  805. XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
  806. {
  807. if ( !doc ) {
  808. doc = _document;
  809. }
  810. XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
  811. return comment;
  812. }
  813. bool XMLComment::ShallowEqual( const XMLNode* compare ) const
  814. {
  815. const XMLComment* comment = compare->ToComment();
  816. return ( comment && XMLUtil::StringEqual( comment->Value(), Value() ));
  817. }
  818. bool XMLComment::Accept( XMLVisitor* visitor ) const
  819. {
  820. return visitor->Visit( *this );
  821. }
  822. // --------- XMLDeclaration ---------- //
  823. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  824. {
  825. }
  826. XMLDeclaration::~XMLDeclaration()
  827. {
  828. //printf( "~XMLDeclaration\n" );
  829. }
  830. char* XMLDeclaration::ParseDeep( char* p, StrPair* )
  831. {
  832. // Declaration parses as text.
  833. const char* start = p;
  834. p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  835. if ( p == 0 ) {
  836. _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
  837. }
  838. return p;
  839. }
  840. XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
  841. {
  842. if ( !doc ) {
  843. doc = _document;
  844. }
  845. XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
  846. return dec;
  847. }
  848. bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
  849. {
  850. const XMLDeclaration* declaration = compare->ToDeclaration();
  851. return ( declaration && XMLUtil::StringEqual( declaration->Value(), Value() ));
  852. }
  853. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  854. {
  855. return visitor->Visit( *this );
  856. }
  857. // --------- XMLUnknown ---------- //
  858. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  859. {
  860. }
  861. XMLUnknown::~XMLUnknown()
  862. {
  863. }
  864. char* XMLUnknown::ParseDeep( char* p, StrPair* )
  865. {
  866. // Unknown parses as text.
  867. const char* start = p;
  868. p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  869. if ( !p ) {
  870. _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
  871. }
  872. return p;
  873. }
  874. XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
  875. {
  876. if ( !doc ) {
  877. doc = _document;
  878. }
  879. XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
  880. return text;
  881. }
  882. bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
  883. {
  884. const XMLUnknown* unknown = compare->ToUnknown();
  885. return ( unknown && XMLUtil::StringEqual( unknown->Value(), Value() ));
  886. }
  887. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  888. {
  889. return visitor->Visit( *this );
  890. }
  891. // --------- XMLAttribute ---------- //
  892. const char* XMLAttribute::Name() const
  893. {
  894. return _name.GetStr();
  895. }
  896. const char* XMLAttribute::Value() const
  897. {
  898. return _value.GetStr();
  899. }
  900. char* XMLAttribute::ParseDeep( char* p, bool processEntities )
  901. {
  902. // Parse using the name rules: bug fix, was using ParseText before
  903. p = _name.ParseName( p );
  904. if ( !p || !*p ) {
  905. return 0;
  906. }
  907. // Skip white space before =
  908. p = XMLUtil::SkipWhiteSpace( p );
  909. if ( !p || *p != '=' ) {
  910. return 0;
  911. }
  912. ++p; // move up to opening quote
  913. p = XMLUtil::SkipWhiteSpace( p );
  914. if ( *p != '\"' && *p != '\'' ) {
  915. return 0;
  916. }
  917. char endTag[2] = { *p, 0 };
  918. ++p; // move past opening quote
  919. p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
  920. return p;
  921. }
  922. void XMLAttribute::SetName( const char* n )
  923. {
  924. _name.SetStr( n );
  925. }
  926. XMLError XMLAttribute::QueryIntValue( int* value ) const
  927. {
  928. if ( XMLUtil::ToInt( Value(), value )) {
  929. return XML_NO_ERROR;
  930. }
  931. return XML_WRONG_ATTRIBUTE_TYPE;
  932. }
  933. XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
  934. {
  935. if ( XMLUtil::ToUnsigned( Value(), value )) {
  936. return XML_NO_ERROR;
  937. }
  938. return XML_WRONG_ATTRIBUTE_TYPE;
  939. }
  940. XMLError XMLAttribute::QueryBoolValue( bool* value ) const
  941. {
  942. if ( XMLUtil::ToBool( Value(), value )) {
  943. return XML_NO_ERROR;
  944. }
  945. return XML_WRONG_ATTRIBUTE_TYPE;
  946. }
  947. XMLError XMLAttribute::QueryFloatValue( float* value ) const
  948. {
  949. if ( XMLUtil::ToFloat( Value(), value )) {
  950. return XML_NO_ERROR;
  951. }
  952. return XML_WRONG_ATTRIBUTE_TYPE;
  953. }
  954. XMLError XMLAttribute::QueryDoubleValue( double* value ) const
  955. {
  956. if ( XMLUtil::ToDouble( Value(), value )) {
  957. return XML_NO_ERROR;
  958. }
  959. return XML_WRONG_ATTRIBUTE_TYPE;
  960. }
  961. void XMLAttribute::SetAttribute( const char* v )
  962. {
  963. _value.SetStr( v );
  964. }
  965. void XMLAttribute::SetAttribute( int v )
  966. {
  967. char buf[BUF_SIZE];
  968. XMLUtil::ToStr( v, buf, BUF_SIZE );
  969. _value.SetStr( buf );
  970. }
  971. void XMLAttribute::SetAttribute( unsigned v )
  972. {
  973. char buf[BUF_SIZE];
  974. XMLUtil::ToStr( v, buf, BUF_SIZE );
  975. _value.SetStr( buf );
  976. }
  977. void XMLAttribute::SetAttribute( bool v )
  978. {
  979. char buf[BUF_SIZE];
  980. XMLUtil::ToStr( v, buf, BUF_SIZE );
  981. _value.SetStr( buf );
  982. }
  983. void XMLAttribute::SetAttribute( double v )
  984. {
  985. char buf[BUF_SIZE];
  986. XMLUtil::ToStr( v, buf, BUF_SIZE );
  987. _value.SetStr( buf );
  988. }
  989. void XMLAttribute::SetAttribute( float v )
  990. {
  991. char buf[BUF_SIZE];
  992. XMLUtil::ToStr( v, buf, BUF_SIZE );
  993. _value.SetStr( buf );
  994. }
  995. // --------- XMLElement ---------- //
  996. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  997. _closingType( 0 ),
  998. _rootAttribute( 0 )
  999. {
  1000. }
  1001. XMLElement::~XMLElement()
  1002. {
  1003. while( _rootAttribute ) {
  1004. XMLAttribute* next = _rootAttribute->_next;
  1005. DeleteAttribute( _rootAttribute );
  1006. _rootAttribute = next;
  1007. }
  1008. }
  1009. XMLAttribute* XMLElement::FindAttribute( const char* name )
  1010. {
  1011. for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) {
  1012. if ( XMLUtil::StringEqual( a->Name(), name ) ) {
  1013. return a;
  1014. }
  1015. }
  1016. return 0;
  1017. }
  1018. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  1019. {
  1020. for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) {
  1021. if ( XMLUtil::StringEqual( a->Name(), name ) ) {
  1022. return a;
  1023. }
  1024. }
  1025. return 0;
  1026. }
  1027. const char* XMLElement::Attribute( const char* name, const char* value ) const
  1028. {
  1029. const XMLAttribute* a = FindAttribute( name );
  1030. if ( !a ) {
  1031. return 0;
  1032. }
  1033. if ( !value || XMLUtil::StringEqual( a->Value(), value )) {
  1034. return a->Value();
  1035. }
  1036. return 0;
  1037. }
  1038. const char* XMLElement::GetText() const
  1039. {
  1040. if ( FirstChild() && FirstChild()->ToText() ) {
  1041. return FirstChild()->ToText()->Value();
  1042. }
  1043. return 0;
  1044. }
  1045. void XMLElement::SetText( const char* inText )
  1046. {
  1047. if ( FirstChild() && FirstChild()->ToText() )
  1048. FirstChild()->SetValue( inText );
  1049. else {
  1050. XMLText* theText = GetDocument()->NewText( inText );
  1051. InsertFirstChild( theText );
  1052. }
  1053. }
  1054. void XMLElement::SetText( int v )
  1055. {
  1056. char buf[BUF_SIZE];
  1057. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1058. SetText( buf );
  1059. }
  1060. void XMLElement::SetText( unsigned v )
  1061. {
  1062. char buf[BUF_SIZE];
  1063. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1064. SetText( buf );
  1065. }
  1066. void XMLElement::SetText( bool v )
  1067. {
  1068. char buf[BUF_SIZE];
  1069. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1070. SetText( buf );
  1071. }
  1072. void XMLElement::SetText( float v )
  1073. {
  1074. char buf[BUF_SIZE];
  1075. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1076. SetText( buf );
  1077. }
  1078. void XMLElement::SetText( double v )
  1079. {
  1080. char buf[BUF_SIZE];
  1081. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1082. SetText( buf );
  1083. }
  1084. XMLError XMLElement::QueryIntText( int* ival ) const
  1085. {
  1086. if ( FirstChild() && FirstChild()->ToText() ) {
  1087. const char* t = FirstChild()->ToText()->Value();
  1088. if ( XMLUtil::ToInt( t, ival ) ) {
  1089. return XML_SUCCESS;
  1090. }
  1091. return XML_CAN_NOT_CONVERT_TEXT;
  1092. }
  1093. return XML_NO_TEXT_NODE;
  1094. }
  1095. XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const
  1096. {
  1097. if ( FirstChild() && FirstChild()->ToText() ) {
  1098. const char* t = FirstChild()->ToText()->Value();
  1099. if ( XMLUtil::ToUnsigned( t, uval ) ) {
  1100. return XML_SUCCESS;
  1101. }
  1102. return XML_CAN_NOT_CONVERT_TEXT;
  1103. }
  1104. return XML_NO_TEXT_NODE;
  1105. }
  1106. XMLError XMLElement::QueryBoolText( bool* bval ) const
  1107. {
  1108. if ( FirstChild() && FirstChild()->ToText() ) {
  1109. const char* t = FirstChild()->ToText()->Value();
  1110. if ( XMLUtil::ToBool( t, bval ) ) {
  1111. return XML_SUCCESS;
  1112. }
  1113. return XML_CAN_NOT_CONVERT_TEXT;
  1114. }
  1115. return XML_NO_TEXT_NODE;
  1116. }
  1117. XMLError XMLElement::QueryDoubleText( double* dval ) const
  1118. {
  1119. if ( FirstChild() && FirstChild()->ToText() ) {
  1120. const char* t = FirstChild()->ToText()->Value();
  1121. if ( XMLUtil::ToDouble( t, dval ) ) {
  1122. return XML_SUCCESS;
  1123. }
  1124. return XML_CAN_NOT_CONVERT_TEXT;
  1125. }
  1126. return XML_NO_TEXT_NODE;
  1127. }
  1128. XMLError XMLElement::QueryFloatText( float* fval ) const
  1129. {
  1130. if ( FirstChild() && FirstChild()->ToText() ) {
  1131. const char* t = FirstChild()->ToText()->Value();
  1132. if ( XMLUtil::ToFloat( t, fval ) ) {
  1133. return XML_SUCCESS;
  1134. }
  1135. return XML_CAN_NOT_CONVERT_TEXT;
  1136. }
  1137. return XML_NO_TEXT_NODE;
  1138. }
  1139. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  1140. {
  1141. XMLAttribute* last = 0;
  1142. XMLAttribute* attrib = 0;
  1143. for( attrib = _rootAttribute;
  1144. attrib;
  1145. last = attrib, attrib = attrib->_next ) {
  1146. if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {
  1147. break;
  1148. }
  1149. }
  1150. if ( !attrib ) {
  1151. attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1152. attrib->_memPool = &_document->_attributePool;
  1153. if ( last ) {
  1154. last->_next = attrib;
  1155. }
  1156. else {
  1157. _rootAttribute = attrib;
  1158. }
  1159. attrib->SetName( name );
  1160. attrib->_memPool->SetTracked(); // always created and linked.
  1161. }
  1162. return attrib;
  1163. }
  1164. void XMLElement::DeleteAttribute( const char* name )
  1165. {
  1166. XMLAttribute* prev = 0;
  1167. for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) {
  1168. if ( XMLUtil::StringEqual( name, a->Name() ) ) {
  1169. if ( prev ) {
  1170. prev->_next = a->_next;
  1171. }
  1172. else {
  1173. _rootAttribute = a->_next;
  1174. }
  1175. DeleteAttribute( a );
  1176. break;
  1177. }
  1178. prev = a;
  1179. }
  1180. }
  1181. char* XMLElement::ParseAttributes( char* p )
  1182. {
  1183. const char* start = p;
  1184. XMLAttribute* prevAttribute = 0;
  1185. // Read the attributes.
  1186. while( p ) {
  1187. p = XMLUtil::SkipWhiteSpace( p );
  1188. if ( !p || !(*p) ) {
  1189. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );
  1190. return 0;
  1191. }
  1192. // attribute.
  1193. if (XMLUtil::IsNameStartChar( *p ) ) {
  1194. XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1195. attrib->_memPool = &_document->_attributePool;
  1196. attrib->_memPool->SetTracked();
  1197. p = attrib->ParseDeep( p, _document->ProcessEntities() );
  1198. if ( !p || Attribute( attrib->Name() ) ) {
  1199. DeleteAttribute( attrib );
  1200. _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );
  1201. return 0;
  1202. }
  1203. // There is a minor bug here: if the attribute in the source xml
  1204. // document is duplicated, it will not be detected and the
  1205. // attribute will be doubly added. However, tracking the 'prevAttribute'
  1206. // avoids re-scanning the attribute list. Preferring performance for
  1207. // now, may reconsider in the future.
  1208. if ( prevAttribute ) {
  1209. prevAttribute->_next = attrib;
  1210. }
  1211. else {
  1212. _rootAttribute = attrib;
  1213. }
  1214. prevAttribute = attrib;
  1215. }
  1216. // end of the tag
  1217. else if ( *p == '/' && *(p+1) == '>' ) {
  1218. _closingType = CLOSED;
  1219. return p+2; // done; sealed element.
  1220. }
  1221. // end of the tag
  1222. else if ( *p == '>' ) {
  1223. ++p;
  1224. break;
  1225. }
  1226. else {
  1227. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );
  1228. return 0;
  1229. }
  1230. }
  1231. return p;
  1232. }
  1233. void XMLElement::DeleteAttribute( XMLAttribute* attribute )
  1234. {
  1235. if ( attribute == 0 ) {
  1236. return;
  1237. }
  1238. MemPool* pool = attribute->_memPool;
  1239. attribute->~XMLAttribute();
  1240. pool->Free( attribute );
  1241. }
  1242. //
  1243. // <ele></ele>
  1244. // <ele>foo<b>bar</b></ele>
  1245. //
  1246. char* XMLElement::ParseDeep( char* p, StrPair* strPair )
  1247. {
  1248. // Read the element name.
  1249. p = XMLUtil::SkipWhiteSpace( p );
  1250. if ( !p ) {
  1251. return 0;
  1252. }
  1253. // The closing element is the </element> form. It is
  1254. // parsed just like a regular element then deleted from
  1255. // the DOM.
  1256. if ( *p == '/' ) {
  1257. _closingType = CLOSING;
  1258. ++p;
  1259. }
  1260. p = _value.ParseName( p );
  1261. if ( _value.Empty() ) {
  1262. return 0;
  1263. }
  1264. p = ParseAttributes( p );
  1265. if ( !p || !*p || _closingType ) {
  1266. return p;
  1267. }
  1268. p = XMLNode::ParseDeep( p, strPair );
  1269. return p;
  1270. }
  1271. XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
  1272. {
  1273. if ( !doc ) {
  1274. doc = _document;
  1275. }
  1276. XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
  1277. for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
  1278. element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
  1279. }
  1280. return element;
  1281. }
  1282. bool XMLElement::ShallowEqual( const XMLNode* compare ) const
  1283. {
  1284. const XMLElement* other = compare->ToElement();
  1285. if ( other && XMLUtil::StringEqual( other->Value(), Value() )) {
  1286. const XMLAttribute* a=FirstAttribute();
  1287. const XMLAttribute* b=other->FirstAttribute();
  1288. while ( a && b ) {
  1289. if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
  1290. return false;
  1291. }
  1292. a = a->Next();
  1293. b = b->Next();
  1294. }
  1295. if ( a || b ) {
  1296. // different count
  1297. return false;
  1298. }
  1299. return true;
  1300. }
  1301. return false;
  1302. }
  1303. bool XMLElement::Accept( XMLVisitor* visitor ) const
  1304. {
  1305. if ( visitor->VisitEnter( *this, _rootAttribute ) ) {
  1306. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
  1307. if ( !node->Accept( visitor ) ) {
  1308. break;
  1309. }
  1310. }
  1311. }
  1312. return visitor->VisitExit( *this );
  1313. }
  1314. // --------- XMLDocument ----------- //
  1315. XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) :
  1316. XMLNode( 0 ),
  1317. _writeBOM( false ),
  1318. _processEntities( processEntities ),
  1319. _errorID( XML_NO_ERROR ),
  1320. _whitespace( whitespace ),
  1321. _errorStr1( 0 ),
  1322. _errorStr2( 0 ),
  1323. _charBuffer( 0 )
  1324. {
  1325. _document = this; // avoid warning about 'this' in initializer list
  1326. }
  1327. XMLDocument::~XMLDocument()
  1328. {
  1329. DeleteChildren();
  1330. delete [] _charBuffer;
  1331. #if 0
  1332. _textPool.Trace( "text" );
  1333. _elementPool.Trace( "element" );
  1334. _commentPool.Trace( "comment" );
  1335. _attributePool.Trace( "attribute" );
  1336. #endif
  1337. #ifdef DEBUG
  1338. if ( Error() == false ) {
  1339. TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() );
  1340. TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() );
  1341. TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() );
  1342. TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() );
  1343. }
  1344. #endif
  1345. }
  1346. void XMLDocument::Clear()
  1347. {
  1348. DeleteChildren();
  1349. _errorID = XML_NO_ERROR;
  1350. _errorStr1 = 0;
  1351. _errorStr2 = 0;
  1352. delete [] _charBuffer;
  1353. _charBuffer = 0;
  1354. }
  1355. XMLElement* XMLDocument::NewElement( const char* name )
  1356. {
  1357. XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this );
  1358. ele->_memPool = &_elementPool;
  1359. ele->SetName( name );
  1360. return ele;
  1361. }
  1362. XMLComment* XMLDocument::NewComment( const char* str )
  1363. {
  1364. XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this );
  1365. comment->_memPool = &_commentPool;
  1366. comment->SetValue( str );
  1367. return comment;
  1368. }
  1369. XMLText* XMLDocument::NewText( const char* str )
  1370. {
  1371. XMLText* text = new (_textPool.Alloc()) XMLText( this );
  1372. text->_memPool = &_textPool;
  1373. text->SetValue( str );
  1374. return text;
  1375. }
  1376. XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
  1377. {
  1378. XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this );
  1379. dec->_memPool = &_commentPool;
  1380. dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
  1381. return dec;
  1382. }
  1383. XMLUnknown* XMLDocument::NewUnknown( const char* str )
  1384. {
  1385. XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this );
  1386. unk->_memPool = &_commentPool;
  1387. unk->SetValue( str );
  1388. return unk;
  1389. }
  1390. static FILE* callfopen( const char* filepath, const char* mode )
  1391. {
  1392. #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
  1393. FILE* fp = 0;
  1394. errno_t err = fopen_s( &fp, filepath, mode );
  1395. if ( err ) {
  1396. return 0;
  1397. }
  1398. #else
  1399. FILE* fp = fopen( filepath, mode );
  1400. #endif
  1401. return fp;
  1402. }
  1403. XMLError XMLDocument::LoadFile( const char* filename )
  1404. {
  1405. Clear();
  1406. FILE* fp = callfopen( filename, "rb" );
  1407. if ( !fp ) {
  1408. SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
  1409. return _errorID;
  1410. }
  1411. LoadFile( fp );
  1412. fclose( fp );
  1413. return _errorID;
  1414. }
  1415. XMLError XMLDocument::LoadFile( FILE* fp )
  1416. {
  1417. Clear();
  1418. fseek( fp, 0, SEEK_SET );
  1419. if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
  1420. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1421. return _errorID;
  1422. }
  1423. fseek( fp, 0, SEEK_END );
  1424. const long filelength = ftell( fp );
  1425. fseek( fp, 0, SEEK_SET );
  1426. if ( filelength == -1L ) {
  1427. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1428. return _errorID;
  1429. }
  1430. const size_t size = filelength;
  1431. if ( size == 0 ) {
  1432. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1433. return _errorID;
  1434. }
  1435. _charBuffer = new char[size+1];
  1436. size_t read = fread( _charBuffer, 1, size, fp );
  1437. if ( read != size ) {
  1438. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1439. return _errorID;
  1440. }
  1441. _charBuffer[size] = 0;
  1442. const char* p = _charBuffer;
  1443. p = XMLUtil::SkipWhiteSpace( p );
  1444. p = XMLUtil::ReadBOM( p, &_writeBOM );
  1445. if ( !p || !*p ) {
  1446. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1447. return _errorID;
  1448. }
  1449. ParseDeep( _charBuffer + (p-_charBuffer), 0 );
  1450. return _errorID;
  1451. }
  1452. XMLError XMLDocument::SaveFile( const char* filename, bool compact )
  1453. {
  1454. FILE* fp = callfopen( filename, "w" );
  1455. if ( !fp ) {
  1456. SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
  1457. return _errorID;
  1458. }
  1459. SaveFile(fp, compact);
  1460. fclose( fp );
  1461. return _errorID;
  1462. }
  1463. XMLError XMLDocument::SaveFile( FILE* fp, bool compact )
  1464. {
  1465. XMLPrinter stream( fp, compact );
  1466. Print( &stream );
  1467. return _errorID;
  1468. }
  1469. XMLError XMLDocument::Parse( const char* p, size_t len )
  1470. {
  1471. const char* start = p;
  1472. Clear();
  1473. if ( len == 0 || !p || !*p ) {
  1474. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1475. return _errorID;
  1476. }
  1477. if ( len == (size_t)(-1) ) {
  1478. len = strlen( p );
  1479. }
  1480. _charBuffer = new char[ len+1 ];
  1481. memcpy( _charBuffer, p, len );
  1482. _charBuffer[len] = 0;
  1483. p = XMLUtil::SkipWhiteSpace( p );
  1484. p = XMLUtil::ReadBOM( p, &_writeBOM );
  1485. if ( !p || !*p ) {
  1486. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1487. return _errorID;
  1488. }
  1489. ptrdiff_t delta = p - start; // skip initial whitespace, BOM, etc.
  1490. ParseDeep( _charBuffer+delta, 0 );
  1491. return _errorID;
  1492. }
  1493. void XMLDocument::Print( XMLPrinter* streamer ) const
  1494. {
  1495. XMLPrinter stdStreamer( stdout );
  1496. if ( !streamer ) {
  1497. streamer = &stdStreamer;
  1498. }
  1499. Accept( streamer );
  1500. }
  1501. void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 )
  1502. {
  1503. _errorID = error;
  1504. _errorStr1 = str1;
  1505. _errorStr2 = str2;
  1506. }
  1507. void XMLDocument::PrintError() const
  1508. {
  1509. if ( _errorID ) {
  1510. static const int LEN = 20;
  1511. char buf1[LEN] = { 0 };
  1512. char buf2[LEN] = { 0 };
  1513. if ( _errorStr1 ) {
  1514. TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 );
  1515. }
  1516. if ( _errorStr2 ) {
  1517. TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 );
  1518. }
  1519. printf( "XMLDocument error id=%d str1=%s str2=%s\n",
  1520. _errorID, buf1, buf2 );
  1521. }
  1522. }
  1523. XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
  1524. _elementJustOpened( false ),
  1525. _firstElement( true ),
  1526. _fp( file ),
  1527. _depth( depth ),
  1528. _textDepth( -1 ),
  1529. _processEntities( true ),
  1530. _compactMode( compact )
  1531. {
  1532. for( int i=0; i<ENTITY_RANGE; ++i ) {
  1533. _entityFlag[i] = false;
  1534. _restrictedEntityFlag[i] = false;
  1535. }
  1536. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1537. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  1538. if ( entities[i].value < ENTITY_RANGE ) {
  1539. _entityFlag[ (int)entities[i].value ] = true;
  1540. }
  1541. }
  1542. _restrictedEntityFlag[(int)'&'] = true;
  1543. _restrictedEntityFlag[(int)'<'] = true;
  1544. _restrictedEntityFlag[(int)'>'] = true; // not required, but consistency is nice
  1545. _buffer.Push( 0 );
  1546. }
  1547. void XMLPrinter::Print( const char* format, ... )
  1548. {
  1549. va_list va;
  1550. va_start( va, format );
  1551. if ( _fp ) {
  1552. vfprintf( _fp, format, va );
  1553. }
  1554. else {
  1555. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1556. #if defined(WINCE)
  1557. int len = 512;
  1558. do {
  1559. len = len*2;
  1560. char* str = new char[len]();
  1561. len = _vsnprintf(str, len, format, va);
  1562. delete[] str;
  1563. }while (len < 0);
  1564. #else
  1565. int len = _vscprintf( format, va );
  1566. #endif
  1567. #else
  1568. int len = vsnprintf( 0, 0, format, va );
  1569. #endif
  1570. // Close out and re-start the va-args
  1571. va_end( va );
  1572. va_start( va, format );
  1573. char* p = _buffer.PushArr( len ) - 1; // back up over the null terminator.
  1574. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1575. #if defined(WINCE)
  1576. _vsnprintf( p, len+1, format, va );
  1577. #else
  1578. vsnprintf_s( p, len+1, _TRUNCATE, format, va );
  1579. #endif
  1580. #else
  1581. vsnprintf( p, len+1, format, va );
  1582. #endif
  1583. }
  1584. va_end( va );
  1585. }
  1586. void XMLPrinter::PrintSpace( int depth )
  1587. {
  1588. for( int i=0; i<depth; ++i ) {
  1589. Print( " " );
  1590. }
  1591. }
  1592. void XMLPrinter::PrintString( const char* p, bool restricted )
  1593. {
  1594. // Look for runs of bytes between entities to print.
  1595. const char* q = p;
  1596. const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag;
  1597. if ( _processEntities ) {
  1598. while ( *q ) {
  1599. // Remember, char is sometimes signed. (How many times has that bitten me?)
  1600. if ( *q > 0 && *q < ENTITY_RANGE ) {
  1601. // Check for entities. If one is found, flush
  1602. // the stream up until the entity, write the
  1603. // entity, and keep looking.
  1604. if ( flag[(unsigned)(*q)] ) {
  1605. while ( p < q ) {
  1606. Print( "%c", *p );
  1607. ++p;
  1608. }
  1609. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1610. if ( entities[i].value == *q ) {
  1611. Print( "&%s;", entities[i].pattern );
  1612. break;
  1613. }
  1614. }
  1615. ++p;
  1616. }
  1617. }
  1618. ++q;
  1619. }
  1620. }
  1621. // Flush the remaining string. This will be the entire
  1622. // string if an entity wasn't found.
  1623. if ( !_processEntities || (q-p > 0) ) {
  1624. Print( "%s", p );
  1625. }
  1626. }
  1627. void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
  1628. {
  1629. if ( writeBOM ) {
  1630. static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
  1631. Print( "%s", bom );
  1632. }
  1633. if ( writeDec ) {
  1634. PushDeclaration( "xml version=\"1.0\"" );
  1635. }
  1636. }
  1637. void XMLPrinter::OpenElement( const char* name, bool compactMode )
  1638. {
  1639. if ( _elementJustOpened ) {
  1640. SealElement();
  1641. }
  1642. _stack.Push( name );
  1643. if ( _textDepth < 0 && !_firstElement && !compactMode ) {
  1644. Print( "\n" );
  1645. }
  1646. if ( !compactMode ) {
  1647. PrintSpace( _depth );
  1648. }
  1649. Print( "<%s", name );
  1650. _elementJustOpened = true;
  1651. _firstElement = false;
  1652. ++_depth;
  1653. }
  1654. void XMLPrinter::PushAttribute( const char* name, const char* value )
  1655. {
  1656. TIXMLASSERT( _elementJustOpened );
  1657. Print( " %s=\"", name );
  1658. PrintString( value, false );
  1659. Print( "\"" );
  1660. }
  1661. void XMLPrinter::PushAttribute( const char* name, int v )
  1662. {
  1663. char buf[BUF_SIZE];
  1664. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1665. PushAttribute( name, buf );
  1666. }
  1667. void XMLPrinter::PushAttribute( const char* name, unsigned v )
  1668. {
  1669. char buf[BUF_SIZE];
  1670. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1671. PushAttribute( name, buf );
  1672. }
  1673. void XMLPrinter::PushAttribute( const char* name, bool v )
  1674. {
  1675. char buf[BUF_SIZE];
  1676. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1677. PushAttribute( name, buf );
  1678. }
  1679. void XMLPrinter::PushAttribute( const char* name, double v )
  1680. {
  1681. char buf[BUF_SIZE];
  1682. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1683. PushAttribute( name, buf );
  1684. }
  1685. void XMLPrinter::CloseElement( bool compactMode )
  1686. {
  1687. --_depth;
  1688. const char* name = _stack.Pop();
  1689. if ( _elementJustOpened ) {
  1690. Print( "/>" );
  1691. }
  1692. else {
  1693. if ( _textDepth < 0 && !compactMode) {
  1694. Print( "\n" );
  1695. PrintSpace( _depth );
  1696. }
  1697. Print( "</%s>", name );
  1698. }
  1699. if ( _textDepth == _depth ) {
  1700. _textDepth = -1;
  1701. }
  1702. if ( _depth == 0 && !compactMode) {
  1703. Print( "\n" );
  1704. }
  1705. _elementJustOpened = false;
  1706. }
  1707. void XMLPrinter::SealElement()
  1708. {
  1709. _elementJustOpened = false;
  1710. Print( ">" );
  1711. }
  1712. void XMLPrinter::PushText( const char* text, bool cdata )
  1713. {
  1714. _textDepth = _depth-1;
  1715. if ( _elementJustOpened ) {
  1716. SealElement();
  1717. }
  1718. if ( cdata ) {
  1719. Print( "<![CDATA[" );
  1720. Print( "%s", text );
  1721. Print( "]]>" );
  1722. }
  1723. else {
  1724. PrintString( text, true );
  1725. }
  1726. }
  1727. void XMLPrinter::PushText( int value )
  1728. {
  1729. char buf[BUF_SIZE];
  1730. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1731. PushText( buf, false );
  1732. }
  1733. void XMLPrinter::PushText( unsigned value )
  1734. {
  1735. char buf[BUF_SIZE];
  1736. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1737. PushText( buf, false );
  1738. }
  1739. void XMLPrinter::PushText( bool value )
  1740. {
  1741. char buf[BUF_SIZE];
  1742. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1743. PushText( buf, false );
  1744. }
  1745. void XMLPrinter::PushText( float value )
  1746. {
  1747. char buf[BUF_SIZE];
  1748. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1749. PushText( buf, false );
  1750. }
  1751. void XMLPrinter::PushText( double value )
  1752. {
  1753. char buf[BUF_SIZE];
  1754. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1755. PushText( buf, false );
  1756. }
  1757. void XMLPrinter::PushComment( const char* comment )
  1758. {
  1759. if ( _elementJustOpened ) {
  1760. SealElement();
  1761. }
  1762. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1763. Print( "\n" );
  1764. PrintSpace( _depth );
  1765. }
  1766. _firstElement = false;
  1767. Print( "<!--%s-->", comment );
  1768. }
  1769. void XMLPrinter::PushDeclaration( const char* value )
  1770. {
  1771. if ( _elementJustOpened ) {
  1772. SealElement();
  1773. }
  1774. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1775. Print( "\n" );
  1776. PrintSpace( _depth );
  1777. }
  1778. _firstElement = false;
  1779. Print( "<?%s?>", value );
  1780. }
  1781. void XMLPrinter::PushUnknown( const char* value )
  1782. {
  1783. if ( _elementJustOpened ) {
  1784. SealElement();
  1785. }
  1786. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1787. Print( "\n" );
  1788. PrintSpace( _depth );
  1789. }
  1790. _firstElement = false;
  1791. Print( "<!%s>", value );
  1792. }
  1793. bool XMLPrinter::VisitEnter( const XMLDocument& doc )
  1794. {
  1795. _processEntities = doc.ProcessEntities();
  1796. if ( doc.HasBOM() ) {
  1797. PushHeader( true, false );
  1798. }
  1799. return true;
  1800. }
  1801. bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  1802. {
  1803. const XMLElement* parentElem = element.Parent()->ToElement();
  1804. bool compactMode = parentElem ? CompactMode(*parentElem) : _compactMode;
  1805. OpenElement( element.Name(), compactMode );
  1806. while ( attribute ) {
  1807. PushAttribute( attribute->Name(), attribute->Value() );
  1808. attribute = attribute->Next();
  1809. }
  1810. return true;
  1811. }
  1812. bool XMLPrinter::VisitExit( const XMLElement& element )
  1813. {
  1814. CloseElement( CompactMode(element) );
  1815. return true;
  1816. }
  1817. bool XMLPrinter::Visit( const XMLText& text )
  1818. {
  1819. PushText( text.Value(), text.CData() );
  1820. return true;
  1821. }
  1822. bool XMLPrinter::Visit( const XMLComment& comment )
  1823. {
  1824. PushComment( comment.Value() );
  1825. return true;
  1826. }
  1827. bool XMLPrinter::Visit( const XMLDeclaration& declaration )
  1828. {
  1829. PushDeclaration( declaration.Value() );
  1830. return true;
  1831. }
  1832. bool XMLPrinter::Visit( const XMLUnknown& unknown )
  1833. {
  1834. PushUnknown( unknown.Value() );
  1835. return true;
  1836. }
  1837. } // namespace tinyxml2