blob: ab2d56844b7c6ca6e97057d1ecdf7f27d84c61a2 [file] [log] [blame]
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001#include "tinyxml2.h"
2
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07003#include <cstdlib>
4#include <cstring>
5#include <ctime>
U-Lama\Leee13c3e62011-12-28 14:36:55 -08006
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08007#if defined( _MSC_VER )
Lee Thomason1ff38e02012-02-14 18:18:16 -08008 #include <crtdbg.h>
Lee Thomason6f381b72012-03-02 12:59:39 -08009 #define WIN32_LEAN_AND_MEAN
10 #include <windows.h>
Lee Thomason1ff38e02012-02-14 18:18:16 -080011 _CrtMemState startMemState;
12 _CrtMemState endMemState;
13#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -080014
U-Lama\Leee13c3e62011-12-28 14:36:55 -080015using namespace tinyxml2;
Lee Thomasonec5a7b42012-02-13 18:16:52 -080016int gPass = 0;
17int gFail = 0;
18
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -080019
U-Stream\Lee09a11c52012-02-17 08:31:16 -080020bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080021{
22 bool pass = !strcmp( expected, found );
23 if ( pass )
24 printf ("[pass]");
25 else
26 printf ("[fail]");
27
U-Stream\Lee09a11c52012-02-17 08:31:16 -080028 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080029 printf (" %s\n", testString);
30 else
31 printf (" %s [%s][%s]\n", testString, expected, found);
32
33 if ( pass )
34 ++gPass;
35 else
36 ++gFail;
37 return pass;
38}
39
40
U-Stream\Lee09a11c52012-02-17 08:31:16 -080041bool XMLTest( const char* testString, int expected, int found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080042{
43 bool pass = ( expected == found );
44 if ( pass )
45 printf ("[pass]");
46 else
47 printf ("[fail]");
48
U-Stream\Lee09a11c52012-02-17 08:31:16 -080049 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080050 printf (" %s\n", testString);
51 else
52 printf (" %s [%d][%d]\n", testString, expected, found);
53
54 if ( pass )
55 ++gPass;
56 else
57 ++gFail;
58 return pass;
59}
Lee Thomasonec5a7b42012-02-13 18:16:52 -080060
U-Lama\Leee13c3e62011-12-28 14:36:55 -080061
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -080062void NullLineEndings( char* p )
63{
64 while( p && *p ) {
65 if ( *p == '\n' || *p == '\r' ) {
66 *p = 0;
67 return;
68 }
69 ++p;
70 }
71}
72
73
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -070074int main( int /*argc*/, const char ** /*argv*/ )
U-Lama\Leee13c3e62011-12-28 14:36:55 -080075{
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -080076 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -080077 _CrtMemCheckpoint( &startMemState );
78 #endif
Lee Thomason8a5dfee2012-01-18 17:43:40 -080079
Lee Thomason7f7b1622012-03-24 12:49:03 -070080 #if defined(_MSC_VER)
81 #pragma warning ( push )
82 #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
83 #endif
84
85 FILE* fp = fopen( "dream.xml", "r" );
86 if ( !fp ) {
87 printf( "Error opening test file 'dream.xml'.\n"
88 "Is your working directory the same as where \n"
89 "the xmltest.cpp and dream.xml file are?\n\n"
90 #if defined( _MSC_VER )
91 "In windows Visual Studio you may need to set\n"
92 "Properties->Debugging->Working Directory to '..'\n"
93 #endif
94 );
95 exit( 1 );
96 }
97 fclose( fp );
98
99 #if defined(_MSC_VER)
100 #pragma warning ( pop )
101 #endif
102
Lee Thomason87e475a2012-03-20 11:55:29 -0700103 /* ------ Example 1: Load and parse an XML file. ---- */
104 {
105 XMLDocument doc;
106 doc.LoadFile( "dream.xml" );
107 }
108
109 /* ------ Example 2: Lookup information. ---- */
110 {
111 XMLDocument doc;
112 doc.LoadFile( "dream.xml" );
113
114 // Structure of the XML file:
115 // - Element "PLAY" the root Element
116 // - - Element "TITLE" child of the root PLAY Element
117 // - - - Text child of the TITLE Element
118
119 // Navigate to the title, using the convenience function, with a dangerous lack of error checking.
120 const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText();
121 printf( "Name of play (1): %s\n", title );
122
123 // Text is just another Node to TinyXML-2. The more general way to get to the XMLText:
124 XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText();
125 title = textNode->Value();
126 printf( "Name of play (2): %s\n", title );
127 }
128
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800129 {
Lee Thomason43f59302012-02-06 18:18:11 -0800130 static const char* test[] = { "<element />",
131 "<element></element>",
132 "<element><subelement/></element>",
133 "<element><subelement></subelement></element>",
134 "<element><subelement><subsub/></subelement></element>",
135 "<!--comment beside elements--><element><subelement></subelement></element>",
136 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
137 "<element attrib1='foo' attrib2=\"bar\" ></element>",
138 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
139 "<element>Text inside element.</element>",
140 "<element><b></b></element>",
141 "<element>Text inside and <b>bolded</b> in the element.</element>",
142 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -0800143 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -0800144 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800145 0
146 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800147 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800148 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800149 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800150 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800151 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800152 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800153 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800154#if 1
Lee Thomasond6277762012-02-22 16:00:12 -0800155 {
156 static const char* test = "<!--hello world\n"
157 " line 2\r"
158 " line 3\r\n"
159 " line 4\n\r"
160 " line 5\r-->";
161
162 XMLDocument doc;
163 doc.Parse( test );
164 doc.Print();
165 }
166
Lee Thomason2c85a712012-01-31 08:24:24 -0800167 {
168 static const char* test = "<element>Text before.</element>";
169 XMLDocument doc;
170 doc.Parse( test );
171 XMLElement* root = doc.FirstChildElement();
172 XMLElement* newElement = doc.NewElement( "Subelement" );
173 root->InsertEndChild( newElement );
174 doc.Print();
175 }
Lee Thomasond1983222012-02-06 08:41:24 -0800176 {
177 XMLDocument* doc = new XMLDocument();
178 static const char* test = "<element><sub/></element>";
179 doc->Parse( test );
180 delete doc;
181 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800182 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800183 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800184 // Build:
185 // <element>
186 // <!--comment-->
187 // <sub attrib="1" />
188 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800189 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800190 // <element>
191
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800192 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800193 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
194
195 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
196 for( int i=0; i<3; ++i ) {
197 sub[i]->SetAttribute( "attrib", i );
198 }
199 element->InsertEndChild( sub[2] );
200 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
201 element->InsertAfterChild( comment, sub[0] );
202 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800203 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800204 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800205 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
206 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
207 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
208 XMLTest( "Programmatic DOM", "& Text!",
209 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800210
211 // And now deletion:
212 element->DeleteChild( sub[2] );
213 doc->DeleteNode( comment );
214
215 element->FirstChildElement()->SetAttribute( "attrib", true );
216 element->LastChildElement()->DeleteAttribute( "attrib" );
217
218 XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
219 int value = 10;
220 int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700221 XMLTest( "Programmatic DOM", result, XML_NO_ATTRIBUTE );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800222 XMLTest( "Programmatic DOM", value, 10 );
223
224 doc->Print();
225
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800226 XMLPrinter streamer;
U-Stream\Leeae25a442012-02-17 17:48:16 -0800227 doc->Print( &streamer );
228 printf( "%s", streamer.CStr() );
229
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800230 delete doc;
231 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800232 {
233 // Test: Dream
234 // XML1 : 1,187,569 bytes in 31,209 allocations
235 // XML2 : 469,073 bytes in 323 allocations
236 //int newStart = gNew;
237 XMLDocument doc;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800238 doc.LoadFile( "dream.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800239
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800240 doc.SaveFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800241 doc.PrintError();
242
243 XMLTest( "Dream", "xml version=\"1.0\"",
244 doc.FirstChild()->ToDeclaration()->Value() );
245 XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
246 XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
247 doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
248 XMLTest( "Dream", "And Robin shall restore amends.",
249 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
250 XMLTest( "Dream", "And Robin shall restore amends.",
251 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
252
253 XMLDocument doc2;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800254 doc2.LoadFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800255 XMLTest( "Dream-out", "xml version=\"1.0\"",
256 doc2.FirstChild()->ToDeclaration()->Value() );
257 XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
258 XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
259 doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
260 XMLTest( "Dream-out", "And Robin shall restore amends.",
261 doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
262
263 //gNewTotal = gNew - newStart;
264 }
Lee Thomason6f381b72012-03-02 12:59:39 -0800265
266
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800267 {
268 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
269 "<passages count=\"006\" formatversion=\"20020620\">\n"
270 " <wrong error>\n"
271 "</passages>";
272
273 XMLDocument doc;
274 doc.Parse( error );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700275 XMLTest( "Bad XML", doc.ErrorID(), XML_ERROR_PARSING_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800276 }
277
278 {
279 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
280
281 XMLDocument doc;
282 doc.Parse( str );
283
284 XMLElement* ele = doc.FirstChildElement();
285
286 int iVal, result;
287 double dVal;
288
289 result = ele->QueryDoubleAttribute( "attr0", &dVal );
290 XMLTest( "Query attribute: int as double", result, XML_NO_ERROR );
291 XMLTest( "Query attribute: int as double", (int)dVal, 1 );
292 result = ele->QueryDoubleAttribute( "attr1", &dVal );
293 XMLTest( "Query attribute: double as double", (int)dVal, 2 );
294 result = ele->QueryIntAttribute( "attr1", &iVal );
295 XMLTest( "Query attribute: double as int", result, XML_NO_ERROR );
296 XMLTest( "Query attribute: double as int", iVal, 2 );
297 result = ele->QueryIntAttribute( "attr2", &iVal );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700298 XMLTest( "Query attribute: not a number", result, XML_WRONG_ATTRIBUTE_TYPE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800299 result = ele->QueryIntAttribute( "bar", &iVal );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700300 XMLTest( "Query attribute: does not exist", result, XML_NO_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800301 }
302
303 {
304 const char* str = "<doc/>";
305
306 XMLDocument doc;
307 doc.Parse( str );
308
309 XMLElement* ele = doc.FirstChildElement();
310
311 int iVal;
312 double dVal;
313
314 ele->SetAttribute( "str", "strValue" );
315 ele->SetAttribute( "int", 1 );
316 ele->SetAttribute( "double", -1.0 );
317
318 const char* cStr = ele->Attribute( "str" );
319 ele->QueryIntAttribute( "int", &iVal );
320 ele->QueryDoubleAttribute( "double", &dVal );
321
Lee Thomason8ba7f7d2012-03-24 13:04:04 -0700322 XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800323 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
324 XMLTest( "Attribute round trip. int.", 1, iVal );
325 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
326 }
327
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800328 {
329 XMLDocument doc;
330 doc.LoadFile( "utf8test.xml" );
331
332 // Get the attribute "value" from the "Russian" element and check it.
333 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
334 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
335 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
336
337 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
338
339 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
340 0xd1U, 0x81U, 0xd1U, 0x81U,
341 0xd0U, 0xbaU, 0xd0U, 0xb8U,
342 0xd0U, 0xb9U, 0 };
343 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
344
345 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
346 XMLTest( "UTF-8: Browsing russian element name.",
347 russianText,
348 text->Value() );
349
350 // Now try for a round trip.
351 doc.SaveFile( "utf8testout.xml" );
352
353 // Check the round trip.
354 char savedBuf[256];
355 char verifyBuf[256];
356 int okay = 0;
357
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800358
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800359#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800360#pragma warning ( push )
361#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800362#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800363 FILE* saved = fopen( "utf8testout.xml", "r" );
364 FILE* verify = fopen( "utf8testverify.xml", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800365#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800366#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800367#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800368
369 if ( saved && verify )
370 {
371 okay = 1;
372 while ( fgets( verifyBuf, 256, verify ) )
373 {
374 fgets( savedBuf, 256, saved );
375 NullLineEndings( verifyBuf );
376 NullLineEndings( savedBuf );
377
378 if ( strcmp( verifyBuf, savedBuf ) )
379 {
380 printf( "verify:%s<\n", verifyBuf );
381 printf( "saved :%s<\n", savedBuf );
382 okay = 0;
383 break;
384 }
385 }
386 }
387 if ( saved )
388 fclose( saved );
389 if ( verify )
390 fclose( verify );
391 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
392 }
393
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800394 // --------GetText()-----------
395 {
396 const char* str = "<foo>This is text</foo>";
397 XMLDocument doc;
398 doc.Parse( str );
399 const XMLElement* element = doc.RootElement();
400
401 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
402
403 str = "<foo><b>This is text</b></foo>";
404 doc.Parse( str );
405 element = doc.RootElement();
406
407 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
408 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800409
Lee Thomasond6277762012-02-22 16:00:12 -0800410
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800411 // ---------- CDATA ---------------
412 {
413 const char* str = "<xmlElement>"
414 "<![CDATA["
415 "I am > the rules!\n"
416 "...since I make symbolic puns"
417 "]]>"
418 "</xmlElement>";
419 XMLDocument doc;
420 doc.Parse( str );
421 doc.Print();
Lee Thomasond6277762012-02-22 16:00:12 -0800422
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800423 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
424 "I am > the rules!\n...since I make symbolic puns",
Lee Thomasond6277762012-02-22 16:00:12 -0800425 false );
426 }
427
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800428 // ----------- CDATA -------------
429 {
430 const char* str = "<xmlElement>"
431 "<![CDATA["
432 "<b>I am > the rules!</b>\n"
433 "...since I make symbolic puns"
434 "]]>"
435 "</xmlElement>";
436 XMLDocument doc;
437 doc.Parse( str );
438 doc.Print();
439
440 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
441 "<b>I am > the rules!</b>\n...since I make symbolic puns",
442 false );
443 }
444
445 // InsertAfterChild causes crash.
446 {
447 // InsertBeforeChild and InsertAfterChild causes crash.
448 XMLDocument doc;
449 XMLElement* parent = doc.NewElement( "Parent" );
450 doc.InsertFirstChild( parent );
451
452 XMLElement* childText0 = doc.NewElement( "childText0" );
453 XMLElement* childText1 = doc.NewElement( "childText1" );
454
455 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
456 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
457
458 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
459 }
Lee Thomasond6277762012-02-22 16:00:12 -0800460
461 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800462 // Entities not being written correctly.
463 // From Lynn Allen
Lee Thomasond6277762012-02-22 16:00:12 -0800464
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800465 const char* passages =
466 "<?xml version=\"1.0\" standalone=\"no\" ?>"
467 "<passages count=\"006\" formatversion=\"20020620\">"
468 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
469 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
470 "</passages>";
Lee Thomasond6277762012-02-22 16:00:12 -0800471
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800472 XMLDocument doc;
473 doc.Parse( passages );
474 XMLElement* psg = doc.RootElement()->FirstChildElement();
475 const char* context = psg->Attribute( "context" );
476 const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
Lee Thomasond6277762012-02-22 16:00:12 -0800477
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800478 XMLTest( "Entity transformation: read. ", expected, context, true );
Lee Thomasond6277762012-02-22 16:00:12 -0800479
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800480#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800481#pragma warning ( push )
482#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800483#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800484 FILE* textfile = fopen( "textfile.txt", "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800485#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800486#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800487#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800488 if ( textfile )
489 {
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800490 XMLPrinter streamer( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800491 psg->Accept( &streamer );
492 fclose( textfile );
493 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800494#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800495#pragma warning ( push )
496#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800497#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800498 textfile = fopen( "textfile.txt", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800499#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800500#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800501#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800502 TIXMLASSERT( textfile );
503 if ( textfile )
504 {
505 char buf[ 1024 ];
506 fgets( buf, 1024, textfile );
507 XMLTest( "Entity transformation: write. ",
508 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
509 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
510 buf, false );
511 }
512 fclose( textfile );
513 }
514
515 {
Lee Thomason6f381b72012-03-02 12:59:39 -0800516 // Suppress entities.
517 const char* passages =
518 "<?xml version=\"1.0\" standalone=\"no\" ?>"
519 "<passages count=\"006\" formatversion=\"20020620\">"
520 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
521 "</passages>";
522
523 XMLDocument doc( false );
524 doc.Parse( passages );
525
526 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
527 "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
528 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
529 "Crazy &ttk;" );
530 doc.Print();
531 }
532
533 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800534 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
535
536 XMLDocument doc;
537 doc.Parse( test );
538 XMLTest( "dot in names", doc.Error(), 0);
539 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
540 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
541 }
542
543 {
544 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
545
546 XMLDocument doc;
547 doc.Parse( test );
548
549 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
550 XMLTest( "Entity with one digit.",
551 text->Value(), "1.1 Start easy ignore fin thickness\n",
552 false );
553 }
554
555 {
556 // DOCTYPE not preserved (950171)
557 //
558 const char* doctype =
559 "<?xml version=\"1.0\" ?>"
560 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
561 "<!ELEMENT title (#PCDATA)>"
562 "<!ELEMENT books (title,authors)>"
563 "<element />";
564
565 XMLDocument doc;
566 doc.Parse( doctype );
567 doc.SaveFile( "test7.xml" );
568 doc.DeleteChild( doc.RootElement() );
569 doc.LoadFile( "test7.xml" );
570 doc.Print();
571
572 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
573 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
574
575 }
576
577 {
578 // Comments do not stream out correctly.
579 const char* doctype =
580 "<!-- Somewhat<evil> -->";
581 XMLDocument doc;
582 doc.Parse( doctype );
583
584 XMLComment* comment = doc.FirstChild()->ToComment();
585
586 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
587 }
588 {
589 // Double attributes
590 const char* doctype = "<element attr='red' attr='blue' />";
591
592 XMLDocument doc;
593 doc.Parse( doctype );
594
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700595 XMLTest( "Parsing repeated attributes.", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -0800596 doc.PrintError();
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800597 }
598
599 {
600 // Embedded null in stream.
601 const char* doctype = "<element att\0r='red' attr='blue' />";
602
603 XMLDocument doc;
604 doc.Parse( doctype );
605 XMLTest( "Embedded null throws error.", true, doc.Error() );
606 }
607
608 {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700609 // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800610 const char* str = " ";
611 XMLDocument doc;
612 doc.Parse( str );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700613 XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800614 }
615
616 {
617 // Low entities
618 XMLDocument doc;
619 doc.Parse( "<test>&#x0e;</test>" );
620 const char result[] = { 0x0e, 0 };
621 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
622 doc.Print();
623 }
624
625 {
626 // Attribute values with trailing quotes not handled correctly
627 XMLDocument doc;
628 doc.Parse( "<foo attribute=bar\" />" );
629 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
630 }
631
632 {
633 // [ 1663758 ] Failure to report error on bad XML
634 XMLDocument xml;
635 xml.Parse("<x>");
636 XMLTest("Missing end tag at end of input", xml.Error(), true);
637 xml.Parse("<x> ");
638 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
639 xml.Parse("<x></y>");
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700640 XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800641 }
642
643
644 {
645 // [ 1475201 ] TinyXML parses entities in comments
646 XMLDocument xml;
647 xml.Parse("<!-- declarations for <head> & <body> -->"
648 "<!-- far &amp; away -->" );
649
650 XMLNode* e0 = xml.FirstChild();
651 XMLNode* e1 = e0->NextSibling();
652 XMLComment* c0 = e0->ToComment();
653 XMLComment* c1 = e1->ToComment();
654
655 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
656 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
657 }
658
659 {
660 XMLDocument xml;
661 xml.Parse( "<Parent>"
662 "<child1 att=''/>"
663 "<!-- With this comment, child2 will not be parsed! -->"
664 "<child2 att=''/>"
665 "</Parent>" );
666 xml.Print();
667
668 int count = 0;
669
670 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
671 ele;
672 ele = ele->NextSibling() )
673 {
674 ++count;
675 }
676
677 XMLTest( "Comments iterate correctly.", 3, count );
678 }
679
680 {
681 // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
682 unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
683 buf[60] = 239;
684 buf[61] = 0;
685
686 XMLDocument doc;
687 doc.Parse( (const char*)buf);
688 }
689
690
691 {
692 // bug 1827248 Error while parsing a little bit malformed file
693 // Actually not malformed - should work.
694 XMLDocument xml;
695 xml.Parse( "<attributelist> </attributelist >" );
696 XMLTest( "Handle end tag whitespace", false, xml.Error() );
697 }
698
699 {
700 // This one must not result in an infinite loop
701 XMLDocument xml;
702 xml.Parse( "<infinite>loop" );
703 XMLTest( "Infinite loop test.", true, true );
704 }
705#endif
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800706 {
707 const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
708 XMLDocument doc;
709 doc.Parse( pub );
710
711 XMLDocument clone;
712 for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
713 XMLNode* copy = node->ShallowClone( &clone );
714 clone.InsertEndChild( copy );
715 }
716
717 clone.Print();
718
719 int count=0;
720 const XMLNode* a=clone.FirstChild();
721 const XMLNode* b=doc.FirstChild();
722 for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
723 ++count;
724 XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
725 }
726 XMLTest( "Clone and Equal", 4, count );
727 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800728
Lee Thomason5708f812012-03-28 17:46:41 -0700729 // -------- Handles ------------
730 {
731 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
732 XMLDocument doc;
733 doc.Parse( xml );
734 const XMLDocument& docC = doc;
735
736 XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
737 XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
738
Lee Thomason57612032012-04-03 16:43:22 -0700739 const XMLElement* eleC = XMLConstHandle( docC ).FirstChildElement( "element" ).FirstChild().ToElement();
740 XMLTest( "Handle, success, mutable", eleC->Value(), "sub" );
Lee Thomason5708f812012-03-28 17:46:41 -0700741 }
742
743
Lee Thomason6f381b72012-03-02 12:59:39 -0800744 // ----------- Performance tracking --------------
745 {
746#if defined( _MSC_VER )
747 __int64 start, end, freq;
748 QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
749#endif
750
751#if defined(_MSC_VER)
752#pragma warning ( push )
753#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
754#endif
755 FILE* fp = fopen( "dream.xml", "r" );
756#if defined(_MSC_VER)
757#pragma warning ( pop )
758#endif
759 fseek( fp, 0, SEEK_END );
760 long size = ftell( fp );
761 fseek( fp, 0, SEEK_SET );
762
763 char* mem = new char[size+1];
764 fread( mem, size, 1, fp );
765 fclose( fp );
766 mem[size] = 0;
767
768#if defined( _MSC_VER )
769 QueryPerformanceCounter( (LARGE_INTEGER*) &start );
770#else
771 clock_t cstart = clock();
772#endif
773 static const int COUNT = 10;
774 for( int i=0; i<COUNT; ++i ) {
775 XMLDocument doc;
776 doc.Parse( mem );
777 }
778#if defined( _MSC_VER )
779 QueryPerformanceCounter( (LARGE_INTEGER*) &end );
780#else
781 clock_t cend = clock();
782#endif
783
784 delete [] mem;
785
786 static const char* note =
787#ifdef DEBUG
788 "DEBUG";
789#else
790 "Release";
791#endif
792
793#if defined( _MSC_VER )
794 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
795#else
796 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
797#endif
798 }
799
Lee Thomason (grinliz)7ca55582012-03-07 21:54:57 -0800800 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800801 _CrtMemCheckpoint( &endMemState );
802 //_CrtMemDumpStatistics( &endMemState );
803
804 _CrtMemState diffMemState;
805 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
806 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800807 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800808 #endif
809
810 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800811 return 0;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -0800812}