xmltest.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. #include "tinyxml2.h"
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <ctime>
  5. #if defined( _MSC_VER )
  6. #include <direct.h> // _mkdir
  7. #include <crtdbg.h>
  8. #define WIN32_LEAN_AND_MEAN
  9. #include <windows.h>
  10. _CrtMemState startMemState;
  11. _CrtMemState endMemState;
  12. #else
  13. #include <sys/stat.h> // mkdir
  14. #endif
  15. using namespace tinyxml2;
  16. int gPass = 0;
  17. int gFail = 0;
  18. bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
  19. {
  20. bool pass = !strcmp( expected, found );
  21. if ( pass )
  22. printf ("[pass]");
  23. else
  24. printf ("[fail]");
  25. if ( !echo )
  26. printf (" %s\n", testString);
  27. else
  28. printf (" %s [%s][%s]\n", testString, expected, found);
  29. if ( pass )
  30. ++gPass;
  31. else
  32. ++gFail;
  33. return pass;
  34. }
  35. template< class T > bool XMLTest( const char* testString, T expected, T found, bool echo=true )
  36. {
  37. bool pass = ( expected == found );
  38. if ( pass )
  39. printf ("[pass]");
  40. else
  41. printf ("[fail]");
  42. if ( !echo )
  43. printf (" %s\n", testString);
  44. else
  45. printf (" %s [%d][%d]\n", testString, static_cast<int>(expected), static_cast<int>(found) );
  46. if ( pass )
  47. ++gPass;
  48. else
  49. ++gFail;
  50. return pass;
  51. }
  52. void NullLineEndings( char* p )
  53. {
  54. while( p && *p ) {
  55. if ( *p == '\n' || *p == '\r' ) {
  56. *p = 0;
  57. return;
  58. }
  59. ++p;
  60. }
  61. }
  62. // Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
  63. int example_1()
  64. {
  65. XMLDocument doc;
  66. doc.LoadFile( "resources/dream.xml" );
  67. return doc.ErrorID();
  68. }
  69. // Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
  70. int example_2()
  71. {
  72. static const char* xml = "<element/>";
  73. XMLDocument doc;
  74. doc.Parse( xml );
  75. return doc.ErrorID();
  76. }
  77. int example_3()
  78. {
  79. static const char* xml =
  80. "<?xml version=\"1.0\"?>"
  81. "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
  82. "<PLAY>"
  83. "<TITLE>A Midsummer Night's Dream</TITLE>"
  84. "</PLAY>";
  85. XMLDocument doc;
  86. doc.Parse( xml );
  87. XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
  88. const char* title = titleElement->GetText();
  89. printf( "Name of play (1): %s\n", title );
  90. XMLText* textNode = titleElement->FirstChild()->ToText();
  91. title = textNode->Value();
  92. printf( "Name of play (2): %s\n", title );
  93. return doc.ErrorID();
  94. }
  95. bool example_4()
  96. {
  97. static const char* xml =
  98. "<information>"
  99. " <attributeApproach v='2' />"
  100. " <textApproach>"
  101. " <v>2</v>"
  102. " </textApproach>"
  103. "</information>";
  104. XMLDocument doc;
  105. doc.Parse( xml );
  106. int v0 = 0;
  107. int v1 = 0;
  108. XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement( "attributeApproach" );
  109. attributeApproachElement->QueryIntAttribute( "v", &v0 );
  110. XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement( "textApproach" );
  111. textApproachElement->FirstChildElement( "v" )->QueryIntText( &v1 );
  112. printf( "Both values are the same: %d and %d\n", v0, v1 );
  113. return !doc.Error() && ( v0 == v1 );
  114. }
  115. int main( int /*argc*/, const char ** /*argv*/ )
  116. {
  117. #if defined( _MSC_VER ) && defined( DEBUG )
  118. _CrtMemCheckpoint( &startMemState );
  119. #endif
  120. #if defined(_MSC_VER)
  121. #pragma warning ( push )
  122. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  123. #endif
  124. #if defined(_MSC_VER)
  125. _mkdir( "resources/out/" );
  126. #else
  127. mkdir( "resources/out/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  128. #endif
  129. FILE* fp = fopen( "resources/dream.xml", "r" );
  130. if ( !fp ) {
  131. printf( "Error opening test file 'dream.xml'.\n"
  132. "Is your working directory the same as where \n"
  133. "the xmltest.cpp and dream.xml file are?\n\n"
  134. #if defined( _MSC_VER )
  135. "In windows Visual Studio you may need to set\n"
  136. "Properties->Debugging->Working Directory to '..'\n"
  137. #endif
  138. );
  139. exit( 1 );
  140. }
  141. fclose( fp );
  142. #if defined(_MSC_VER)
  143. #pragma warning ( pop )
  144. #endif
  145. XMLTest( "Example-1", 0, example_1() );
  146. XMLTest( "Example-2", 0, example_2() );
  147. XMLTest( "Example-3", 0, example_3() );
  148. XMLTest( "Example-4", true, example_4() );
  149. /* ------ Example 2: Lookup information. ---- */
  150. {
  151. static const char* test[] = { "<element />",
  152. "<element></element>",
  153. "<element><subelement/></element>",
  154. "<element><subelement></subelement></element>",
  155. "<element><subelement><subsub/></subelement></element>",
  156. "<!--comment beside elements--><element><subelement></subelement></element>",
  157. "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
  158. "<element attrib1='foo' attrib2=\"bar\" ></element>",
  159. "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
  160. "<element>Text inside element.</element>",
  161. "<element><b></b></element>",
  162. "<element>Text inside and <b>bolded</b> in the element.</element>",
  163. "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
  164. "<element>This &amp; That.</element>",
  165. "<element attrib='This&lt;That' />",
  166. 0
  167. };
  168. for( int i=0; test[i]; ++i ) {
  169. XMLDocument doc;
  170. doc.Parse( test[i] );
  171. doc.Print();
  172. printf( "----------------------------------------------\n" );
  173. }
  174. }
  175. #if 1
  176. {
  177. static const char* test = "<!--hello world\n"
  178. " line 2\r"
  179. " line 3\r\n"
  180. " line 4\n\r"
  181. " line 5\r-->";
  182. XMLDocument doc;
  183. doc.Parse( test );
  184. doc.Print();
  185. }
  186. {
  187. static const char* test = "<element>Text before.</element>";
  188. XMLDocument doc;
  189. doc.Parse( test );
  190. XMLElement* root = doc.FirstChildElement();
  191. XMLElement* newElement = doc.NewElement( "Subelement" );
  192. root->InsertEndChild( newElement );
  193. doc.Print();
  194. }
  195. {
  196. XMLDocument* doc = new XMLDocument();
  197. static const char* test = "<element><sub/></element>";
  198. doc->Parse( test );
  199. delete doc;
  200. }
  201. {
  202. // Test: Programmatic DOM
  203. // Build:
  204. // <element>
  205. // <!--comment-->
  206. // <sub attrib="1" />
  207. // <sub attrib="2" />
  208. // <sub attrib="3" >& Text!</sub>
  209. // <element>
  210. XMLDocument* doc = new XMLDocument();
  211. XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
  212. XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
  213. for( int i=0; i<3; ++i ) {
  214. sub[i]->SetAttribute( "attrib", i );
  215. }
  216. element->InsertEndChild( sub[2] );
  217. XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
  218. element->InsertAfterChild( comment, sub[0] );
  219. element->InsertAfterChild( sub[0], sub[1] );
  220. sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
  221. doc->Print();
  222. XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
  223. XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
  224. XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
  225. XMLTest( "Programmatic DOM", "& Text!",
  226. doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
  227. // And now deletion:
  228. element->DeleteChild( sub[2] );
  229. doc->DeleteNode( comment );
  230. element->FirstChildElement()->SetAttribute( "attrib", true );
  231. element->LastChildElement()->DeleteAttribute( "attrib" );
  232. XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
  233. int value = 10;
  234. int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
  235. XMLTest( "Programmatic DOM", result, (int)XML_NO_ATTRIBUTE );
  236. XMLTest( "Programmatic DOM", value, 10 );
  237. doc->Print();
  238. {
  239. XMLPrinter streamer;
  240. doc->Print( &streamer );
  241. printf( "%s", streamer.CStr() );
  242. }
  243. {
  244. XMLPrinter streamer( 0, true );
  245. doc->Print( &streamer );
  246. XMLTest( "Compact mode", "<element><sub attrib=\"1\"/><sub/></element>", streamer.CStr(), false );
  247. }
  248. delete doc;
  249. }
  250. {
  251. // Test: Dream
  252. // XML1 : 1,187,569 bytes in 31,209 allocations
  253. // XML2 : 469,073 bytes in 323 allocations
  254. //int newStart = gNew;
  255. XMLDocument doc;
  256. doc.LoadFile( "resources/dream.xml" );
  257. doc.SaveFile( "resources/out/dreamout.xml" );
  258. doc.PrintError();
  259. XMLTest( "Dream", "xml version=\"1.0\"",
  260. doc.FirstChild()->ToDeclaration()->Value() );
  261. XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
  262. XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
  263. doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
  264. XMLTest( "Dream", "And Robin shall restore amends.",
  265. doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  266. XMLTest( "Dream", "And Robin shall restore amends.",
  267. doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  268. XMLDocument doc2;
  269. doc2.LoadFile( "resources/out/dreamout.xml" );
  270. XMLTest( "Dream-out", "xml version=\"1.0\"",
  271. doc2.FirstChild()->ToDeclaration()->Value() );
  272. XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
  273. XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
  274. doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
  275. XMLTest( "Dream-out", "And Robin shall restore amends.",
  276. doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  277. //gNewTotal = gNew - newStart;
  278. }
  279. {
  280. const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
  281. "<passages count=\"006\" formatversion=\"20020620\">\n"
  282. " <wrong error>\n"
  283. "</passages>";
  284. XMLDocument doc;
  285. doc.Parse( error );
  286. XMLTest( "Bad XML", doc.ErrorID(), (int)XML_ERROR_PARSING_ATTRIBUTE );
  287. }
  288. {
  289. const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
  290. XMLDocument doc;
  291. doc.Parse( str );
  292. XMLElement* ele = doc.FirstChildElement();
  293. int iVal, result;
  294. double dVal;
  295. result = ele->QueryDoubleAttribute( "attr0", &dVal );
  296. XMLTest( "Query attribute: int as double", result, (int)XML_NO_ERROR );
  297. XMLTest( "Query attribute: int as double", (int)dVal, 1 );
  298. result = ele->QueryDoubleAttribute( "attr1", &dVal );
  299. XMLTest( "Query attribute: double as double", (int)dVal, 2 );
  300. result = ele->QueryIntAttribute( "attr1", &iVal );
  301. XMLTest( "Query attribute: double as int", result, (int)XML_NO_ERROR );
  302. XMLTest( "Query attribute: double as int", iVal, 2 );
  303. result = ele->QueryIntAttribute( "attr2", &iVal );
  304. XMLTest( "Query attribute: not a number", result, (int)XML_WRONG_ATTRIBUTE_TYPE );
  305. result = ele->QueryIntAttribute( "bar", &iVal );
  306. XMLTest( "Query attribute: does not exist", result, (int)XML_NO_ATTRIBUTE );
  307. }
  308. {
  309. const char* str = "<doc/>";
  310. XMLDocument doc;
  311. doc.Parse( str );
  312. XMLElement* ele = doc.FirstChildElement();
  313. int iVal;
  314. double dVal;
  315. ele->SetAttribute( "str", "strValue" );
  316. ele->SetAttribute( "int", 1 );
  317. ele->SetAttribute( "double", -1.0 );
  318. const char* cStr = ele->Attribute( "str" );
  319. ele->QueryIntAttribute( "int", &iVal );
  320. ele->QueryDoubleAttribute( "double", &dVal );
  321. XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
  322. XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
  323. XMLTest( "Attribute round trip. int.", 1, iVal );
  324. XMLTest( "Attribute round trip. double.", -1, (int)dVal );
  325. }
  326. {
  327. XMLDocument doc;
  328. doc.LoadFile( "resources/utf8test.xml" );
  329. // Get the attribute "value" from the "Russian" element and check it.
  330. XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
  331. const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
  332. 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
  333. XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
  334. const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
  335. 0xd1U, 0x81U, 0xd1U, 0x81U,
  336. 0xd0U, 0xbaU, 0xd0U, 0xb8U,
  337. 0xd0U, 0xb9U, 0 };
  338. const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
  339. XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
  340. XMLTest( "UTF-8: Browsing russian element name.",
  341. russianText,
  342. text->Value() );
  343. // Now try for a round trip.
  344. doc.SaveFile( "resources/out/utf8testout.xml" );
  345. // Check the round trip.
  346. int okay = 0;
  347. #if defined(_MSC_VER)
  348. #pragma warning ( push )
  349. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  350. #endif
  351. FILE* saved = fopen( "resources/out/utf8testout.xml", "r" );
  352. FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
  353. #if defined(_MSC_VER)
  354. #pragma warning ( pop )
  355. #endif
  356. if ( saved && verify )
  357. {
  358. okay = 1;
  359. char verifyBuf[256];
  360. while ( fgets( verifyBuf, 256, verify ) )
  361. {
  362. char savedBuf[256];
  363. fgets( savedBuf, 256, saved );
  364. NullLineEndings( verifyBuf );
  365. NullLineEndings( savedBuf );
  366. if ( strcmp( verifyBuf, savedBuf ) )
  367. {
  368. printf( "verify:%s<\n", verifyBuf );
  369. printf( "saved :%s<\n", savedBuf );
  370. okay = 0;
  371. break;
  372. }
  373. }
  374. }
  375. if ( saved )
  376. fclose( saved );
  377. if ( verify )
  378. fclose( verify );
  379. XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
  380. }
  381. // --------GetText()-----------
  382. {
  383. const char* str = "<foo>This is text</foo>";
  384. XMLDocument doc;
  385. doc.Parse( str );
  386. const XMLElement* element = doc.RootElement();
  387. XMLTest( "GetText() normal use.", "This is text", element->GetText() );
  388. str = "<foo><b>This is text</b></foo>";
  389. doc.Parse( str );
  390. element = doc.RootElement();
  391. XMLTest( "GetText() contained element.", element->GetText() == 0, true );
  392. }
  393. // ---------- CDATA ---------------
  394. {
  395. const char* str = "<xmlElement>"
  396. "<![CDATA["
  397. "I am > the rules!\n"
  398. "...since I make symbolic puns"
  399. "]]>"
  400. "</xmlElement>";
  401. XMLDocument doc;
  402. doc.Parse( str );
  403. doc.Print();
  404. XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
  405. "I am > the rules!\n...since I make symbolic puns",
  406. false );
  407. }
  408. // ----------- CDATA -------------
  409. {
  410. const char* str = "<xmlElement>"
  411. "<![CDATA["
  412. "<b>I am > the rules!</b>\n"
  413. "...since I make symbolic puns"
  414. "]]>"
  415. "</xmlElement>";
  416. XMLDocument doc;
  417. doc.Parse( str );
  418. doc.Print();
  419. XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
  420. "<b>I am > the rules!</b>\n...since I make symbolic puns",
  421. false );
  422. }
  423. // InsertAfterChild causes crash.
  424. {
  425. // InsertBeforeChild and InsertAfterChild causes crash.
  426. XMLDocument doc;
  427. XMLElement* parent = doc.NewElement( "Parent" );
  428. doc.InsertFirstChild( parent );
  429. XMLElement* childText0 = doc.NewElement( "childText0" );
  430. XMLElement* childText1 = doc.NewElement( "childText1" );
  431. XMLNode* childNode0 = parent->InsertEndChild( childText0 );
  432. XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
  433. XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
  434. }
  435. {
  436. // Entities not being written correctly.
  437. // From Lynn Allen
  438. const char* passages =
  439. "<?xml version=\"1.0\" standalone=\"no\" ?>"
  440. "<passages count=\"006\" formatversion=\"20020620\">"
  441. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  442. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
  443. "</passages>";
  444. XMLDocument doc;
  445. doc.Parse( passages );
  446. XMLElement* psg = doc.RootElement()->FirstChildElement();
  447. const char* context = psg->Attribute( "context" );
  448. const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
  449. XMLTest( "Entity transformation: read. ", expected, context, true );
  450. #if defined(_MSC_VER)
  451. #pragma warning ( push )
  452. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  453. #endif
  454. FILE* textfile = fopen( "resources/out/textfile.txt", "w" );
  455. #if defined(_MSC_VER)
  456. #pragma warning ( pop )
  457. #endif
  458. if ( textfile )
  459. {
  460. XMLPrinter streamer( textfile );
  461. psg->Accept( &streamer );
  462. fclose( textfile );
  463. }
  464. #if defined(_MSC_VER)
  465. #pragma warning ( push )
  466. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  467. #endif
  468. textfile = fopen( "resources/out/textfile.txt", "r" );
  469. #if defined(_MSC_VER)
  470. #pragma warning ( pop )
  471. #endif
  472. TIXMLASSERT( textfile );
  473. if ( textfile )
  474. {
  475. char buf[ 1024 ];
  476. fgets( buf, 1024, textfile );
  477. XMLTest( "Entity transformation: write. ",
  478. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  479. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
  480. buf, false );
  481. fclose( textfile );
  482. }
  483. }
  484. {
  485. // Suppress entities.
  486. const char* passages =
  487. "<?xml version=\"1.0\" standalone=\"no\" ?>"
  488. "<passages count=\"006\" formatversion=\"20020620\">"
  489. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
  490. "</passages>";
  491. XMLDocument doc( false );
  492. doc.Parse( passages );
  493. XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
  494. "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
  495. XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
  496. "Crazy &ttk;" );
  497. doc.Print();
  498. }
  499. {
  500. const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
  501. XMLDocument doc;
  502. doc.Parse( test );
  503. XMLTest( "dot in names", doc.Error(), false );
  504. XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
  505. XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
  506. }
  507. {
  508. const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
  509. XMLDocument doc;
  510. doc.Parse( test );
  511. XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
  512. XMLTest( "Entity with one digit.",
  513. text->Value(), "1.1 Start easy ignore fin thickness\n",
  514. false );
  515. }
  516. {
  517. // DOCTYPE not preserved (950171)
  518. //
  519. const char* doctype =
  520. "<?xml version=\"1.0\" ?>"
  521. "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
  522. "<!ELEMENT title (#PCDATA)>"
  523. "<!ELEMENT books (title,authors)>"
  524. "<element />";
  525. XMLDocument doc;
  526. doc.Parse( doctype );
  527. doc.SaveFile( "resources/out/test7.xml" );
  528. doc.DeleteChild( doc.RootElement() );
  529. doc.LoadFile( "resources/out/test7.xml" );
  530. doc.Print();
  531. const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
  532. XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
  533. }
  534. {
  535. // Comments do not stream out correctly.
  536. const char* doctype =
  537. "<!-- Somewhat<evil> -->";
  538. XMLDocument doc;
  539. doc.Parse( doctype );
  540. XMLComment* comment = doc.FirstChild()->ToComment();
  541. XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
  542. }
  543. {
  544. // Double attributes
  545. const char* doctype = "<element attr='red' attr='blue' />";
  546. XMLDocument doc;
  547. doc.Parse( doctype );
  548. XMLTest( "Parsing repeated attributes.", (int)XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
  549. doc.PrintError();
  550. }
  551. {
  552. // Embedded null in stream.
  553. const char* doctype = "<element att\0r='red' attr='blue' />";
  554. XMLDocument doc;
  555. doc.Parse( doctype );
  556. XMLTest( "Embedded null throws error.", true, doc.Error() );
  557. }
  558. {
  559. // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
  560. const char* str = " ";
  561. XMLDocument doc;
  562. doc.Parse( str );
  563. XMLTest( "Empty document error", (int)XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
  564. }
  565. {
  566. // Low entities
  567. XMLDocument doc;
  568. doc.Parse( "<test>&#x0e;</test>" );
  569. const char result[] = { 0x0e, 0 };
  570. XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
  571. doc.Print();
  572. }
  573. {
  574. // Attribute values with trailing quotes not handled correctly
  575. XMLDocument doc;
  576. doc.Parse( "<foo attribute=bar\" />" );
  577. XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
  578. }
  579. {
  580. // [ 1663758 ] Failure to report error on bad XML
  581. XMLDocument xml;
  582. xml.Parse("<x>");
  583. XMLTest("Missing end tag at end of input", xml.Error(), true);
  584. xml.Parse("<x> ");
  585. XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
  586. xml.Parse("<x></y>");
  587. XMLTest("Mismatched tags", xml.ErrorID(), (int)XML_ERROR_MISMATCHED_ELEMENT);
  588. }
  589. {
  590. // [ 1475201 ] TinyXML parses entities in comments
  591. XMLDocument xml;
  592. xml.Parse("<!-- declarations for <head> & <body> -->"
  593. "<!-- far &amp; away -->" );
  594. XMLNode* e0 = xml.FirstChild();
  595. XMLNode* e1 = e0->NextSibling();
  596. XMLComment* c0 = e0->ToComment();
  597. XMLComment* c1 = e1->ToComment();
  598. XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
  599. XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
  600. }
  601. {
  602. XMLDocument xml;
  603. xml.Parse( "<Parent>"
  604. "<child1 att=''/>"
  605. "<!-- With this comment, child2 will not be parsed! -->"
  606. "<child2 att=''/>"
  607. "</Parent>" );
  608. xml.Print();
  609. int count = 0;
  610. for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
  611. ele;
  612. ele = ele->NextSibling() )
  613. {
  614. ++count;
  615. }
  616. XMLTest( "Comments iterate correctly.", 3, count );
  617. }
  618. {
  619. // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
  620. unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
  621. buf[60] = 239;
  622. buf[61] = 0;
  623. XMLDocument doc;
  624. doc.Parse( (const char*)buf);
  625. }
  626. {
  627. // bug 1827248 Error while parsing a little bit malformed file
  628. // Actually not malformed - should work.
  629. XMLDocument xml;
  630. xml.Parse( "<attributelist> </attributelist >" );
  631. XMLTest( "Handle end tag whitespace", false, xml.Error() );
  632. }
  633. {
  634. // This one must not result in an infinite loop
  635. XMLDocument xml;
  636. xml.Parse( "<infinite>loop" );
  637. XMLTest( "Infinite loop test.", true, true );
  638. }
  639. #endif
  640. {
  641. const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
  642. XMLDocument doc;
  643. doc.Parse( pub );
  644. XMLDocument clone;
  645. for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
  646. XMLNode* copy = node->ShallowClone( &clone );
  647. clone.InsertEndChild( copy );
  648. }
  649. clone.Print();
  650. int count=0;
  651. const XMLNode* a=clone.FirstChild();
  652. const XMLNode* b=doc.FirstChild();
  653. for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
  654. ++count;
  655. XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
  656. }
  657. XMLTest( "Clone and Equal", 4, count );
  658. }
  659. {
  660. // This shouldn't crash.
  661. XMLDocument doc;
  662. if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
  663. {
  664. doc.PrintError();
  665. }
  666. XMLTest( "Error in snprinf handling.", true, doc.Error() );
  667. }
  668. {
  669. // Attribute ordering.
  670. static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
  671. XMLDocument doc;
  672. doc.Parse( xml );
  673. XMLElement* ele = doc.FirstChildElement();
  674. const XMLAttribute* a = ele->FirstAttribute();
  675. XMLTest( "Attribute order", "1", a->Value() );
  676. a = a->Next();
  677. XMLTest( "Attribute order", "2", a->Value() );
  678. a = a->Next();
  679. XMLTest( "Attribute order", "3", a->Value() );
  680. XMLTest( "Attribute order", "attrib3", a->Name() );
  681. ele->DeleteAttribute( "attrib2" );
  682. a = ele->FirstAttribute();
  683. XMLTest( "Attribute order", "1", a->Value() );
  684. a = a->Next();
  685. XMLTest( "Attribute order", "3", a->Value() );
  686. ele->DeleteAttribute( "attrib1" );
  687. ele->DeleteAttribute( "attrib3" );
  688. XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
  689. }
  690. {
  691. // Make sure an attribute with a space in it succeeds.
  692. static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
  693. static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
  694. static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
  695. XMLDocument doc0;
  696. doc0.Parse( xml0 );
  697. XMLDocument doc1;
  698. doc1.Parse( xml1 );
  699. XMLDocument doc2;
  700. doc2.Parse( xml2 );
  701. XMLElement* ele = 0;
  702. ele = doc0.FirstChildElement();
  703. XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
  704. ele = doc1.FirstChildElement();
  705. XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
  706. ele = doc2.FirstChildElement();
  707. XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
  708. }
  709. {
  710. // Make sure we don't go into an infinite loop.
  711. static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
  712. XMLDocument doc;
  713. doc.Parse( xml );
  714. XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
  715. XMLElement* ele1 = ele0->NextSiblingElement();
  716. bool equal = ele0->ShallowEqual( ele1 );
  717. XMLTest( "Infinite loop in shallow equal.", true, equal );
  718. }
  719. // -------- Handles ------------
  720. {
  721. static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
  722. XMLDocument doc;
  723. doc.Parse( xml );
  724. XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
  725. XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
  726. XMLHandle docH( doc );
  727. ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
  728. XMLTest( "Handle, dne, mutable", false, ele != 0 );
  729. }
  730. {
  731. static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
  732. XMLDocument doc;
  733. doc.Parse( xml );
  734. XMLConstHandle docH( doc );
  735. const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
  736. XMLTest( "Handle, success, const", ele->Value(), "sub" );
  737. ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
  738. XMLTest( "Handle, dne, const", false, ele != 0 );
  739. }
  740. {
  741. // Default Declaration & BOM
  742. XMLDocument doc;
  743. doc.InsertEndChild( doc.NewDeclaration() );
  744. doc.SetBOM( true );
  745. XMLPrinter printer;
  746. doc.Print( &printer );
  747. static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  748. XMLTest( "BOM and default declaration", printer.CStr(), result, false );
  749. XMLTest( "CStrSize", printer.CStrSize(), 42, false );
  750. }
  751. {
  752. const char* xml = "<ipxml ws='1'><info bla=' /></ipxml>";
  753. XMLDocument doc;
  754. doc.Parse( xml );
  755. XMLTest( "Ill formed XML", true, doc.Error() );
  756. }
  757. // QueryXYZText
  758. {
  759. const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
  760. XMLDocument doc;
  761. doc.Parse( xml );
  762. const XMLElement* pointElement = doc.RootElement();
  763. int intValue = 0;
  764. unsigned unsignedValue = 0;
  765. float floatValue = 0;
  766. double doubleValue = 0;
  767. bool boolValue = false;
  768. pointElement->FirstChildElement( "y" )->QueryIntText( &intValue );
  769. pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
  770. pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
  771. pointElement->FirstChildElement( "x" )->QueryDoubleText( &doubleValue );
  772. pointElement->FirstChildElement( "valid" )->QueryBoolText( &boolValue );
  773. XMLTest( "QueryIntText", intValue, 1, false );
  774. XMLTest( "QueryUnsignedText", unsignedValue, (unsigned)1, false );
  775. XMLTest( "QueryFloatText", floatValue, 1.2f, false );
  776. XMLTest( "QueryDoubleText", doubleValue, 1.2, false );
  777. XMLTest( "QueryBoolText", boolValue, true, false );
  778. }
  779. // ----------- Whitespace ------------
  780. {
  781. const char* xml = "<element>"
  782. "<a> This \nis &apos; text &apos; </a>"
  783. "<b> This is &apos; text &apos; \n</b>"
  784. "<c>This is &apos; \n\n text &apos;</c>"
  785. "</element>";
  786. XMLDocument doc( true, COLLAPSE_WHITESPACE );
  787. doc.Parse( xml );
  788. const XMLElement* element = doc.FirstChildElement();
  789. for( const XMLElement* parent = element->FirstChildElement();
  790. parent;
  791. parent = parent->NextSiblingElement() )
  792. {
  793. XMLTest( "Whitespace collapse", "This is ' text '", parent->GetText() );
  794. }
  795. }
  796. // ----------- Performance tracking --------------
  797. {
  798. #if defined( _MSC_VER )
  799. __int64 start, end, freq;
  800. QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
  801. #endif
  802. #if defined(_MSC_VER)
  803. #pragma warning ( push )
  804. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  805. #endif
  806. FILE* fp = fopen( "resources/dream.xml", "r" );
  807. #if defined(_MSC_VER)
  808. #pragma warning ( pop )
  809. #endif
  810. fseek( fp, 0, SEEK_END );
  811. long size = ftell( fp );
  812. fseek( fp, 0, SEEK_SET );
  813. char* mem = new char[size+1];
  814. fread( mem, size, 1, fp );
  815. fclose( fp );
  816. mem[size] = 0;
  817. #if defined( _MSC_VER )
  818. QueryPerformanceCounter( (LARGE_INTEGER*) &start );
  819. #else
  820. clock_t cstart = clock();
  821. #endif
  822. static const int COUNT = 10;
  823. for( int i=0; i<COUNT; ++i ) {
  824. XMLDocument doc;
  825. doc.Parse( mem );
  826. }
  827. #if defined( _MSC_VER )
  828. QueryPerformanceCounter( (LARGE_INTEGER*) &end );
  829. #else
  830. clock_t cend = clock();
  831. #endif
  832. delete [] mem;
  833. static const char* note =
  834. #ifdef DEBUG
  835. "DEBUG";
  836. #else
  837. "Release";
  838. #endif
  839. #if defined( _MSC_VER )
  840. printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
  841. #else
  842. printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
  843. #endif
  844. }
  845. #if defined( _MSC_VER ) && defined( DEBUG )
  846. _CrtMemCheckpoint( &endMemState );
  847. //_CrtMemDumpStatistics( &endMemState );
  848. _CrtMemState diffMemState;
  849. _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
  850. _CrtMemDumpStatistics( &diffMemState );
  851. //printf( "new total=%d\n", gNewTotal );
  852. #endif
  853. printf ("\nPass %d, Fail %d\n", gPass, gFail);
  854. return 0;
  855. }