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