Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 1 | #if defined( _MSC_VER )
|
| 2 | #define _CRT_SECURE_NO_WARNINGS // This test file is not intended to be secure.
|
| 3 | #endif
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 4 |
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 5 | #include "tinyxml2.h"
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 6 | #include <cstdlib>
|
| 7 | #include <cstring>
|
| 8 | #include <ctime>
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 9 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 10 | #if defined( _MSC_VER )
|
Lee Thomason | e9699e6 | 2012-07-25 12:24:23 -0700 | [diff] [blame] | 11 | #include <direct.h> // _mkdir
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 12 | #include <crtdbg.h>
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 13 | #define WIN32_LEAN_AND_MEAN
|
| 14 | #include <windows.h>
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 15 | _CrtMemState startMemState;
|
| 16 | _CrtMemState endMemState;
|
Martinsh Shaiters | 39ddc26 | 2013-01-15 21:53:08 +0200 | [diff] [blame] | 17 | #elif defined(MINGW32) || defined(__MINGW32__)
|
| 18 | #include <io.h> // mkdir
|
Lee Thomason | e9699e6 | 2012-07-25 12:24:23 -0700 | [diff] [blame] | 19 | #else
|
| 20 | #include <sys/stat.h> // mkdir
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 21 | #endif
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 22 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 23 | using namespace tinyxml2;
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 24 | int gPass = 0;
|
| 25 | int gFail = 0;
|
| 26 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 27 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 28 | 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] | 29 | {
|
| 30 | bool pass = !strcmp( expected, found );
|
| 31 | if ( pass )
|
| 32 | printf ("[pass]");
|
| 33 | else
|
| 34 | printf ("[fail]");
|
| 35 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 36 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 37 | printf (" %s\n", testString);
|
| 38 | else
|
| 39 | printf (" %s [%s][%s]\n", testString, expected, found);
|
| 40 |
|
| 41 | if ( pass )
|
| 42 | ++gPass;
|
| 43 | else
|
| 44 | ++gFail;
|
| 45 | return pass;
|
| 46 | }
|
| 47 |
|
| 48 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 49 | 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] | 50 | {
|
| 51 | bool pass = ( expected == found );
|
| 52 | if ( pass )
|
| 53 | printf ("[pass]");
|
| 54 | else
|
| 55 | printf ("[fail]");
|
| 56 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 57 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 58 | printf (" %s\n", testString);
|
| 59 | else
|
Lee Thomason | c831279 | 2012-07-16 12:44:41 -0700 | [diff] [blame] | 60 | 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] | 61 |
|
| 62 | if ( pass )
|
| 63 | ++gPass;
|
| 64 | else
|
| 65 | ++gFail;
|
| 66 | return pass;
|
| 67 | }
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 68 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 69 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 70 | void NullLineEndings( char* p )
|
| 71 | {
|
| 72 | while( p && *p ) {
|
| 73 | if ( *p == '\n' || *p == '\r' ) {
|
| 74 | *p = 0;
|
| 75 | return;
|
| 76 | }
|
| 77 | ++p;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 82 | int example_1()
|
| 83 | {
|
| 84 | XMLDocument doc;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 85 | doc.LoadFile( "resources/dream.xml" );
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 86 |
|
| 87 | return doc.ErrorID();
|
| 88 | }
|
Martinsh Shaiters | c9c8b77 | 2013-01-16 02:08:19 +0200 | [diff] [blame] | 89 | /** @page Example-1 Load an XML File
|
| 90 | * @dontinclude ./xmltest.cpp
|
| 91 | * Basic XML file loading.
|
| 92 | * The basic syntax to load an XML file from
|
| 93 | * disk and check for an error. (ErrorID()
|
| 94 | * will return 0 for no error.)
|
| 95 | * @skip example_1()
|
| 96 | * @until }
|
| 97 | */
|
| 98 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 99 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 100 | int example_2()
|
| 101 | {
|
| 102 | static const char* xml = "<element/>";
|
| 103 | XMLDocument doc;
|
| 104 | doc.Parse( xml );
|
| 105 |
|
| 106 | return doc.ErrorID();
|
| 107 | }
|
Martinsh Shaiters | c9c8b77 | 2013-01-16 02:08:19 +0200 | [diff] [blame] | 108 | /** @page Example-2 Parse an XML from char buffer
|
| 109 | * @dontinclude ./xmltest.cpp
|
| 110 | * Basic XML string parsing.
|
| 111 | * The basic syntax to parse an XML for
|
| 112 | * a char* and check for an error. (ErrorID()
|
| 113 | * will return 0 for no error.)
|
| 114 | * @skip example_2()
|
| 115 | * @until }
|
| 116 | */
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 117 |
|
| 118 |
|
| 119 | int example_3()
|
| 120 | {
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 121 | static const char* xml =
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 122 | "<?xml version=\"1.0\"?>"
|
| 123 | "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
|
| 124 | "<PLAY>"
|
| 125 | "<TITLE>A Midsummer Night's Dream</TITLE>"
|
| 126 | "</PLAY>";
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 127 |
|
| 128 | XMLDocument doc;
|
| 129 | doc.Parse( xml );
|
| 130 |
|
| 131 | XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
|
| 132 | const char* title = titleElement->GetText();
|
| 133 | printf( "Name of play (1): %s\n", title );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 134 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 135 | XMLText* textNode = titleElement->FirstChild()->ToText();
|
| 136 | title = textNode->Value();
|
| 137 | printf( "Name of play (2): %s\n", title );
|
| 138 |
|
| 139 | return doc.ErrorID();
|
| 140 | }
|
Martinsh Shaiters | c9c8b77 | 2013-01-16 02:08:19 +0200 | [diff] [blame] | 141 | /** @page Example-3 Get information out of XML
|
| 142 | @dontinclude ./xmltest.cpp
|
| 143 | In this example, we navigate a simple XML
|
| 144 | file, and read some interesting text. Note
|
| 145 | that this is examlpe doesn't use error
|
| 146 | checking; working code should check for null
|
| 147 | pointers when walking an XML tree, or use
|
| 148 | XMLHandle.
|
| 149 |
|
| 150 | (The XML is an excerpt from "dream.xml").
|
| 151 |
|
| 152 | @skip example_3()
|
| 153 | @until </PLAY>";
|
| 154 |
|
| 155 | The structure of the XML file is:
|
| 156 |
|
| 157 | <ul>
|
| 158 | <li>(declaration)</li>
|
| 159 | <li>(dtd stuff)</li>
|
| 160 | <li>Element "PLAY"</li>
|
| 161 | <ul>
|
| 162 | <li>Element "TITLE"</li>
|
| 163 | <ul>
|
| 164 | <li>Text "A Midsummer Night's Dream"</li>
|
| 165 | </ul>
|
| 166 | </ul>
|
| 167 | </ul>
|
| 168 |
|
| 169 | For this example, we want to print out the
|
| 170 | title of the play. The text of the title (what
|
| 171 | we want) is child of the "TITLE" element which
|
| 172 | is a child of the "PLAY" element.
|
| 173 |
|
| 174 | We want to skip the declaration and dtd, so the
|
| 175 | method FirstChildElement() is a good choice. The
|
| 176 | FirstChildElement() of the Document is the "PLAY"
|
| 177 | Element, the FirstChildElement() of the "PLAY" Element
|
| 178 | is the "TITLE" Element.
|
| 179 |
|
| 180 | @until ( "TITLE" );
|
| 181 |
|
| 182 | We can then use the convenience function GetText()
|
| 183 | to get the title of the play.
|
| 184 |
|
| 185 | @until title );
|
| 186 |
|
| 187 | Text is just another Node in the XML DOM. And in
|
| 188 | fact you should be a little cautious with it, as
|
| 189 | text nodes can contain elements.
|
| 190 |
|
| 191 | @verbatim
|
| 192 | Consider: A Midsummer Night's <b>Dream</b>
|
| 193 | @endverbatim
|
| 194 |
|
| 195 | It is more correct to actually query the Text Node
|
| 196 | if in doubt:
|
| 197 |
|
| 198 | @until title );
|
| 199 |
|
| 200 | Noting that here we use FirstChild() since we are
|
| 201 | looking for XMLText, not an element, and ToText()
|
| 202 | is a cast from a Node to a XMLText.
|
| 203 | */
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 204 |
|
| 205 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 206 | bool example_4()
|
| 207 | {
|
| 208 | static const char* xml =
|
| 209 | "<information>"
|
| 210 | " <attributeApproach v='2' />"
|
| 211 | " <textApproach>"
|
| 212 | " <v>2</v>"
|
| 213 | " </textApproach>"
|
| 214 | "</information>";
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 215 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 216 | XMLDocument doc;
|
| 217 | doc.Parse( xml );
|
| 218 |
|
| 219 | int v0 = 0;
|
| 220 | int v1 = 0;
|
| 221 |
|
| 222 | XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement( "attributeApproach" );
|
| 223 | attributeApproachElement->QueryIntAttribute( "v", &v0 );
|
| 224 |
|
| 225 | XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement( "textApproach" );
|
| 226 | textApproachElement->FirstChildElement( "v" )->QueryIntText( &v1 );
|
| 227 |
|
| 228 | printf( "Both values are the same: %d and %d\n", v0, v1 );
|
| 229 |
|
| 230 | return !doc.Error() && ( v0 == v1 );
|
| 231 | }
|
Martinsh Shaiters | c9c8b77 | 2013-01-16 02:08:19 +0200 | [diff] [blame] | 232 | /** @page Example-4 Read attributes and text information.
|
| 233 | @dontinclude ./xmltest.cpp
|
| 234 |
|
| 235 | There are fundamentally 2 ways of writing a key-value
|
| 236 | pair into an XML file. (Something that's always annoyed
|
| 237 | me about XML.) Either by using attributes, or by writing
|
| 238 | the key name into an element and the value into
|
| 239 | the text node wrapped by the element. Both approaches
|
| 240 | are illustrated in this example, which shows two ways
|
| 241 | to encode the value "2" into the key "v":
|
| 242 |
|
| 243 | @skip example_4()
|
| 244 | @until "</information>";
|
| 245 |
|
| 246 | TinyXML-2 has accessors for both approaches.
|
| 247 |
|
| 248 | When using an attribute, you navigate to the XMLElement
|
| 249 | with that attribute and use the QueryIntAttribute()
|
| 250 | group of methods. (Also QueryFloatAttribute(), etc.)
|
| 251 |
|
| 252 | @skip XMLElement* attributeApproachElement
|
| 253 | @until &v0 );
|
| 254 |
|
| 255 | When using the text approach, you need to navigate
|
| 256 | down one more step to the XMLElement that contains
|
| 257 | the text. Note the extra FirstChildElement( "v" )
|
| 258 | in the code below. The value of the text can then
|
| 259 | be safely queried with the QueryIntText() group
|
| 260 | of methods. (Also QueryFloatText(), etc.)
|
| 261 |
|
| 262 | @skip XMLElement* textApproachElement
|
| 263 | @until &v1 );
|
| 264 | */
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 265 |
|
| 266 |
|
Lee Thomason | 178e4cc | 2013-01-25 16:19:05 -0800 | [diff] [blame] | 267 | int main( int argc, const char ** argv )
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 268 | {
|
Lee Thomason (grinliz) | 0a4df40 | 2012-02-27 20:50:52 -0800 | [diff] [blame] | 269 | #if defined( _MSC_VER ) && defined( DEBUG )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 270 | _CrtMemCheckpoint( &startMemState );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 271 | #endif
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 272 |
|
Martinsh Shaiters | 39ddc26 | 2013-01-15 21:53:08 +0200 | [diff] [blame] | 273 | #if defined(_MSC_VER) || defined(MINGW32) || defined(__MINGW32__)
|
Lee Thomason | e9699e6 | 2012-07-25 12:24:23 -0700 | [diff] [blame] | 274 | _mkdir( "resources/out/" );
|
| 275 | #else
|
| 276 | mkdir( "resources/out/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
| 277 | #endif
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 278 |
|
Lee Thomason | 178e4cc | 2013-01-25 16:19:05 -0800 | [diff] [blame] | 279 | if ( argc > 1 ) {
|
| 280 | XMLDocument* doc = new XMLDocument();
|
| 281 | clock_t startTime = clock();
|
| 282 | doc->LoadFile( argv[1] );
|
| 283 | clock_t loadTime = clock();
|
| 284 | int errorID = doc->ErrorID();
|
| 285 | delete doc; doc = 0;
|
| 286 | clock_t deleteTime = clock();
|
| 287 |
|
| 288 | printf( "Test file '%s' loaded. ErrorID=%d\n", argv[1], errorID );
|
| 289 | if ( !errorID ) {
|
| 290 | printf( "Load time=%d\n", loadTime - startTime );
|
| 291 | printf( "Delete time=%d\n", deleteTime - loadTime );
|
Lee Thomason (grinliz) | ac83b4e | 2013-02-01 09:02:34 -0800 | [diff] [blame] | 292 | printf( "Total time=%d\n", deleteTime - startTime );
|
Lee Thomason | 178e4cc | 2013-01-25 16:19:05 -0800 | [diff] [blame] | 293 | }
|
| 294 | exit(0);
|
| 295 | }
|
| 296 |
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 297 | FILE* fp = fopen( "resources/dream.xml", "r" );
|
Lee Thomason | 7f7b162 | 2012-03-24 12:49:03 -0700 | [diff] [blame] | 298 | if ( !fp ) {
|
| 299 | printf( "Error opening test file 'dream.xml'.\n"
|
| 300 | "Is your working directory the same as where \n"
|
| 301 | "the xmltest.cpp and dream.xml file are?\n\n"
|
| 302 | #if defined( _MSC_VER )
|
| 303 | "In windows Visual Studio you may need to set\n"
|
| 304 | "Properties->Debugging->Working Directory to '..'\n"
|
| 305 | #endif
|
| 306 | );
|
| 307 | exit( 1 );
|
| 308 | }
|
| 309 | fclose( fp );
|
| 310 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 311 | XMLTest( "Example-1", 0, example_1() );
|
| 312 | XMLTest( "Example-2", 0, example_2() );
|
| 313 | XMLTest( "Example-3", 0, example_3() );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 314 | XMLTest( "Example-4", true, example_4() );
|
Lee Thomason | 87e475a | 2012-03-20 11:55:29 -0700 | [diff] [blame] | 315 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 316 | /* ------ Example 2: Lookup information. ---- */
|
Lee Thomason | 87e475a | 2012-03-20 11:55:29 -0700 | [diff] [blame] | 317 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 318 | {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 319 | static const char* test[] = { "<element />",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 320 | "<element></element>",
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 321 | "<element><subelement/></element>",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 322 | "<element><subelement></subelement></element>",
|
| 323 | "<element><subelement><subsub/></subelement></element>",
|
| 324 | "<!--comment beside elements--><element><subelement></subelement></element>",
|
| 325 | "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
|
| 326 | "<element attrib1='foo' attrib2=\"bar\" ></element>",
|
| 327 | "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 328 | "<element>Text inside element.</element>",
|
| 329 | "<element><b></b></element>",
|
| 330 | "<element>Text inside and <b>bolded</b> in the element.</element>",
|
| 331 | "<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] | 332 | "<element>This & That.</element>",
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 333 | "<element attrib='This<That' />",
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 334 | 0
|
| 335 | };
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 336 | for( int i=0; test[i]; ++i ) {
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 337 | XMLDocument doc;
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 338 | doc.Parse( test[i] );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 339 | doc.Print();
|
Lee Thomason | ec975ce | 2012-01-23 11:42:06 -0800 | [diff] [blame] | 340 | printf( "----------------------------------------------\n" );
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 341 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 342 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 343 | #if 1
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 344 | {
|
| 345 | static const char* test = "<!--hello world\n"
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 346 | " line 2\r"
|
| 347 | " line 3\r\n"
|
| 348 | " line 4\n\r"
|
| 349 | " line 5\r-->";
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 350 |
|
| 351 | XMLDocument doc;
|
| 352 | doc.Parse( test );
|
| 353 | doc.Print();
|
| 354 | }
|
| 355 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 356 | {
|
| 357 | static const char* test = "<element>Text before.</element>";
|
| 358 | XMLDocument doc;
|
| 359 | doc.Parse( test );
|
| 360 | XMLElement* root = doc.FirstChildElement();
|
| 361 | XMLElement* newElement = doc.NewElement( "Subelement" );
|
| 362 | root->InsertEndChild( newElement );
|
| 363 | doc.Print();
|
| 364 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 365 | {
|
| 366 | XMLDocument* doc = new XMLDocument();
|
| 367 | static const char* test = "<element><sub/></element>";
|
| 368 | doc->Parse( test );
|
| 369 | delete doc;
|
| 370 | }
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 371 | {
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 372 | // Test: Programmatic DOM
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 373 | // Build:
|
| 374 | // <element>
|
| 375 | // <!--comment-->
|
| 376 | // <sub attrib="1" />
|
| 377 | // <sub attrib="2" />
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 378 | // <sub attrib="3" >& Text!</sub>
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 379 | // <element>
|
| 380 |
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 381 | XMLDocument* doc = new XMLDocument();
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 382 | XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
|
| 383 |
|
| 384 | XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
|
| 385 | for( int i=0; i<3; ++i ) {
|
| 386 | sub[i]->SetAttribute( "attrib", i );
|
| 387 | }
|
| 388 | element->InsertEndChild( sub[2] );
|
| 389 | XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
|
| 390 | element->InsertAfterChild( comment, sub[0] );
|
| 391 | element->InsertAfterChild( sub[0], sub[1] );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 392 | sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 393 | doc->Print();
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 394 | XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
|
| 395 | XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
|
| 396 | XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 397 | XMLTest( "Programmatic DOM", "& Text!",
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 398 | doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 399 |
|
| 400 | // And now deletion:
|
| 401 | element->DeleteChild( sub[2] );
|
| 402 | doc->DeleteNode( comment );
|
| 403 |
|
| 404 | element->FirstChildElement()->SetAttribute( "attrib", true );
|
| 405 | element->LastChildElement()->DeleteAttribute( "attrib" );
|
| 406 |
|
| 407 | XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
|
| 408 | int value = 10;
|
| 409 | int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 410 | XMLTest( "Programmatic DOM", result, (int)XML_NO_ATTRIBUTE );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 411 | XMLTest( "Programmatic DOM", value, 10 );
|
| 412 |
|
| 413 | doc->Print();
|
| 414 |
|
Lee Thomason | 7b1b86a | 2012-06-04 17:01:38 -0700 | [diff] [blame] | 415 | {
|
| 416 | XMLPrinter streamer;
|
| 417 | doc->Print( &streamer );
|
| 418 | printf( "%s", streamer.CStr() );
|
| 419 | }
|
| 420 | {
|
| 421 | XMLPrinter streamer( 0, true );
|
| 422 | doc->Print( &streamer );
|
| 423 | XMLTest( "Compact mode", "<element><sub attrib=\"1\"/><sub/></element>", streamer.CStr(), false );
|
| 424 | }
|
Lee Thomason (grinliz) | 6b8b012 | 2012-09-08 21:21:00 -0700 | [diff] [blame] | 425 | doc->SaveFile( "./resources/out/pretty.xml" );
|
| 426 | doc->SaveFile( "./resources/out/compact.xml", true );
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 427 | delete doc;
|
| 428 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 429 | {
|
| 430 | // Test: Dream
|
| 431 | // XML1 : 1,187,569 bytes in 31,209 allocations
|
| 432 | // XML2 : 469,073 bytes in 323 allocations
|
| 433 | //int newStart = gNew;
|
| 434 | XMLDocument doc;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 435 | doc.LoadFile( "resources/dream.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 436 |
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 437 | doc.SaveFile( "resources/out/dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 438 | doc.PrintError();
|
| 439 |
|
| 440 | XMLTest( "Dream", "xml version=\"1.0\"",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 441 | doc.FirstChild()->ToDeclaration()->Value() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 442 | XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 443 | XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 444 | doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 445 | XMLTest( "Dream", "And Robin shall restore amends.",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 446 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 447 | XMLTest( "Dream", "And Robin shall restore amends.",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 448 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 449 |
|
| 450 | XMLDocument doc2;
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 451 | doc2.LoadFile( "resources/out/dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 452 | XMLTest( "Dream-out", "xml version=\"1.0\"",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 453 | doc2.FirstChild()->ToDeclaration()->Value() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 454 | XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 455 | XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 456 | doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 457 | XMLTest( "Dream-out", "And Robin shall restore amends.",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 458 | doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 459 |
|
| 460 | //gNewTotal = gNew - newStart;
|
| 461 | }
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 462 |
|
| 463 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 464 | {
|
| 465 | const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
|
| 466 | "<passages count=\"006\" formatversion=\"20020620\">\n"
|
| 467 | " <wrong error>\n"
|
| 468 | "</passages>";
|
| 469 |
|
| 470 | XMLDocument doc;
|
| 471 | doc.Parse( error );
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 472 | XMLTest( "Bad XML", doc.ErrorID(), XML_ERROR_PARSING_ATTRIBUTE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 473 | }
|
| 474 |
|
| 475 | {
|
| 476 | const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
|
| 477 |
|
| 478 | XMLDocument doc;
|
| 479 | doc.Parse( str );
|
| 480 |
|
| 481 | XMLElement* ele = doc.FirstChildElement();
|
| 482 |
|
| 483 | int iVal, result;
|
| 484 | double dVal;
|
| 485 |
|
| 486 | result = ele->QueryDoubleAttribute( "attr0", &dVal );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 487 | XMLTest( "Query attribute: int as double", result, (int)XML_NO_ERROR );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 488 | XMLTest( "Query attribute: int as double", (int)dVal, 1 );
|
| 489 | result = ele->QueryDoubleAttribute( "attr1", &dVal );
|
| 490 | XMLTest( "Query attribute: double as double", (int)dVal, 2 );
|
| 491 | result = ele->QueryIntAttribute( "attr1", &iVal );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 492 | XMLTest( "Query attribute: double as int", result, (int)XML_NO_ERROR );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 493 | XMLTest( "Query attribute: double as int", iVal, 2 );
|
| 494 | result = ele->QueryIntAttribute( "attr2", &iVal );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 495 | 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] | 496 | result = ele->QueryIntAttribute( "bar", &iVal );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 497 | XMLTest( "Query attribute: does not exist", result, (int)XML_NO_ATTRIBUTE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 498 | }
|
| 499 |
|
| 500 | {
|
| 501 | const char* str = "<doc/>";
|
| 502 |
|
| 503 | XMLDocument doc;
|
| 504 | doc.Parse( str );
|
| 505 |
|
| 506 | XMLElement* ele = doc.FirstChildElement();
|
| 507 |
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame^] | 508 | int iVal, iVal2;
|
| 509 | double dVal, dVal2;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 510 |
|
| 511 | ele->SetAttribute( "str", "strValue" );
|
| 512 | ele->SetAttribute( "int", 1 );
|
| 513 | ele->SetAttribute( "double", -1.0 );
|
| 514 |
|
| 515 | const char* cStr = ele->Attribute( "str" );
|
| 516 | ele->QueryIntAttribute( "int", &iVal );
|
| 517 | ele->QueryDoubleAttribute( "double", &dVal );
|
| 518 |
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame^] | 519 | ele->QueryAttribute( "int", &iVal2 );
|
| 520 | ele->QueryAttribute( "double", &dVal2 );
|
| 521 |
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 522 | XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 523 | XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
|
| 524 | XMLTest( "Attribute round trip. int.", 1, iVal );
|
| 525 | XMLTest( "Attribute round trip. double.", -1, (int)dVal );
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame^] | 526 | XMLTest( "Alternate query", true, iVal == iVal2 );
|
| 527 | XMLTest( "Alternate query", true, dVal == dVal2 );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 528 | }
|
| 529 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 530 | {
|
| 531 | XMLDocument doc;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 532 | doc.LoadFile( "resources/utf8test.xml" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 533 |
|
| 534 | // Get the attribute "value" from the "Russian" element and check it.
|
| 535 | XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 536 | const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 537 | 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
|
| 538 |
|
| 539 | XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
|
| 540 |
|
| 541 | const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
|
| 542 | 0xd1U, 0x81U, 0xd1U, 0x81U,
|
| 543 | 0xd0U, 0xbaU, 0xd0U, 0xb8U,
|
| 544 | 0xd0U, 0xb9U, 0 };
|
| 545 | const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
|
| 546 |
|
| 547 | XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
|
| 548 | XMLTest( "UTF-8: Browsing russian element name.",
|
| 549 | russianText,
|
| 550 | text->Value() );
|
| 551 |
|
| 552 | // Now try for a round trip.
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 553 | doc.SaveFile( "resources/out/utf8testout.xml" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 554 |
|
| 555 | // Check the round trip.
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 556 | int okay = 0;
|
| 557 |
|
Thomas Roß | a6dd8c6 | 2012-07-26 20:42:18 +0200 | [diff] [blame] | 558 | FILE* saved = fopen( "resources/out/utf8testout.xml", "r" );
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 559 | FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 560 |
|
| 561 | if ( saved && verify )
|
| 562 | {
|
| 563 | okay = 1;
|
PKEuS | c28ba3a | 2012-07-16 03:08:47 -0700 | [diff] [blame] | 564 | char verifyBuf[256];
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 565 | while ( fgets( verifyBuf, 256, verify ) )
|
| 566 | {
|
PKEuS | c28ba3a | 2012-07-16 03:08:47 -0700 | [diff] [blame] | 567 | char savedBuf[256];
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 568 | fgets( savedBuf, 256, saved );
|
| 569 | NullLineEndings( verifyBuf );
|
| 570 | NullLineEndings( savedBuf );
|
| 571 |
|
| 572 | if ( strcmp( verifyBuf, savedBuf ) )
|
| 573 | {
|
| 574 | printf( "verify:%s<\n", verifyBuf );
|
| 575 | printf( "saved :%s<\n", savedBuf );
|
| 576 | okay = 0;
|
| 577 | break;
|
| 578 | }
|
| 579 | }
|
| 580 | }
|
| 581 | if ( saved )
|
| 582 | fclose( saved );
|
| 583 | if ( verify )
|
| 584 | fclose( verify );
|
| 585 | XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
|
| 586 | }
|
| 587 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 588 | // --------GetText()-----------
|
| 589 | {
|
| 590 | const char* str = "<foo>This is text</foo>";
|
| 591 | XMLDocument doc;
|
| 592 | doc.Parse( str );
|
| 593 | const XMLElement* element = doc.RootElement();
|
| 594 |
|
| 595 | XMLTest( "GetText() normal use.", "This is text", element->GetText() );
|
| 596 |
|
| 597 | str = "<foo><b>This is text</b></foo>";
|
| 598 | doc.Parse( str );
|
| 599 | element = doc.RootElement();
|
| 600 |
|
| 601 | XMLTest( "GetText() contained element.", element->GetText() == 0, true );
|
| 602 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 603 |
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 604 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 605 | // ---------- CDATA ---------------
|
| 606 | {
|
| 607 | const char* str = "<xmlElement>"
|
| 608 | "<![CDATA["
|
| 609 | "I am > the rules!\n"
|
| 610 | "...since I make symbolic puns"
|
| 611 | "]]>"
|
| 612 | "</xmlElement>";
|
| 613 | XMLDocument doc;
|
| 614 | doc.Parse( str );
|
| 615 | doc.Print();
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 616 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 617 | XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 618 | "I am > the rules!\n...since I make symbolic puns",
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 619 | false );
|
| 620 | }
|
| 621 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 622 | // ----------- CDATA -------------
|
| 623 | {
|
| 624 | const char* str = "<xmlElement>"
|
| 625 | "<![CDATA["
|
| 626 | "<b>I am > the rules!</b>\n"
|
| 627 | "...since I make symbolic puns"
|
| 628 | "]]>"
|
| 629 | "</xmlElement>";
|
| 630 | XMLDocument doc;
|
| 631 | doc.Parse( str );
|
| 632 | doc.Print();
|
| 633 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 634 | XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 635 | "<b>I am > the rules!</b>\n...since I make symbolic puns",
|
| 636 | false );
|
| 637 | }
|
| 638 |
|
| 639 | // InsertAfterChild causes crash.
|
| 640 | {
|
| 641 | // InsertBeforeChild and InsertAfterChild causes crash.
|
| 642 | XMLDocument doc;
|
| 643 | XMLElement* parent = doc.NewElement( "Parent" );
|
| 644 | doc.InsertFirstChild( parent );
|
| 645 |
|
| 646 | XMLElement* childText0 = doc.NewElement( "childText0" );
|
| 647 | XMLElement* childText1 = doc.NewElement( "childText1" );
|
| 648 |
|
| 649 | XMLNode* childNode0 = parent->InsertEndChild( childText0 );
|
| 650 | XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
|
| 651 |
|
| 652 | XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
|
| 653 | }
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 654 |
|
| 655 | {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 656 | // Entities not being written correctly.
|
| 657 | // From Lynn Allen
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 658 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 659 | const char* passages =
|
| 660 | "<?xml version=\"1.0\" standalone=\"no\" ?>"
|
| 661 | "<passages count=\"006\" formatversion=\"20020620\">"
|
| 662 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 663 | " It also has <, >, and &, as well as a fake copyright ©.\"> </psg>"
|
| 664 | "</passages>";
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 665 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 666 | XMLDocument doc;
|
| 667 | doc.Parse( passages );
|
| 668 | XMLElement* psg = doc.RootElement()->FirstChildElement();
|
| 669 | const char* context = psg->Attribute( "context" );
|
| 670 | 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] | 671 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 672 | XMLTest( "Entity transformation: read. ", expected, context, true );
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 673 |
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 674 | FILE* textfile = fopen( "resources/out/textfile.txt", "w" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 675 | if ( textfile )
|
| 676 | {
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 677 | XMLPrinter streamer( textfile );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 678 | psg->Accept( &streamer );
|
| 679 | fclose( textfile );
|
| 680 | }
|
Thomas Roß | 0922b73 | 2012-09-23 16:31:22 +0200 | [diff] [blame] | 681 |
|
| 682 | textfile = fopen( "resources/out/textfile.txt", "r" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 683 | TIXMLASSERT( textfile );
|
| 684 | if ( textfile )
|
| 685 | {
|
| 686 | char buf[ 1024 ];
|
| 687 | fgets( buf, 1024, textfile );
|
| 688 | XMLTest( "Entity transformation: write. ",
|
| 689 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 690 | " It also has <, >, and &, as well as a fake copyright \xC2\xA9.\"/>\n",
|
| 691 | buf, false );
|
PKEuS | c28ba3a | 2012-07-16 03:08:47 -0700 | [diff] [blame] | 692 | fclose( textfile );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 693 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 694 | }
|
| 695 |
|
| 696 | {
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 697 | // Suppress entities.
|
| 698 | const char* passages =
|
| 699 | "<?xml version=\"1.0\" standalone=\"no\" ?>"
|
| 700 | "<passages count=\"006\" formatversion=\"20020620\">"
|
| 701 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'.\">Crazy &ttk;</psg>"
|
| 702 | "</passages>";
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 703 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 704 | XMLDocument doc( false );
|
| 705 | doc.Parse( passages );
|
| 706 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 707 | XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 708 | "Line 5 has "quotation marks" and 'apostrophe marks'." );
|
| 709 | XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
|
| 710 | "Crazy &ttk;" );
|
| 711 | doc.Print();
|
| 712 | }
|
| 713 |
|
| 714 | {
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 715 | const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 716 |
|
| 717 | XMLDocument doc;
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 718 | doc.Parse( test );
|
| 719 | XMLTest( "dot in names", doc.Error(), false );
|
| 720 | XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
|
| 721 | XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 722 | }
|
| 723 |
|
| 724 | {
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 725 | const char* test = "<element><Name>1.1 Start easy ignore fin thickness
</Name></element>";
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 726 |
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 727 | XMLDocument doc;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 728 | doc.Parse( test );
|
| 729 |
|
| 730 | XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
|
| 731 | XMLTest( "Entity with one digit.",
|
| 732 | text->Value(), "1.1 Start easy ignore fin thickness\n",
|
| 733 | false );
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 734 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 735 |
|
| 736 | {
|
| 737 | // DOCTYPE not preserved (950171)
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 738 | //
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 739 | const char* doctype =
|
| 740 | "<?xml version=\"1.0\" ?>"
|
| 741 | "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
|
| 742 | "<!ELEMENT title (#PCDATA)>"
|
| 743 | "<!ELEMENT books (title,authors)>"
|
| 744 | "<element />";
|
| 745 |
|
| 746 | XMLDocument doc;
|
| 747 | doc.Parse( doctype );
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 748 | doc.SaveFile( "resources/out/test7.xml" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 749 | doc.DeleteChild( doc.RootElement() );
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 750 | doc.LoadFile( "resources/out/test7.xml" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 751 | doc.Print();
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 752 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 753 | const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
|
| 754 | XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
|
| 755 |
|
| 756 | }
|
| 757 |
|
| 758 | {
|
| 759 | // Comments do not stream out correctly.
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 760 | const char* doctype =
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 761 | "<!-- Somewhat<evil> -->";
|
| 762 | XMLDocument doc;
|
| 763 | doc.Parse( doctype );
|
| 764 |
|
| 765 | XMLComment* comment = doc.FirstChild()->ToComment();
|
| 766 |
|
| 767 | XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
|
| 768 | }
|
| 769 | {
|
| 770 | // Double attributes
|
| 771 | const char* doctype = "<element attr='red' attr='blue' />";
|
| 772 |
|
| 773 | XMLDocument doc;
|
| 774 | doc.Parse( doctype );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 775 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 776 | 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) | 0a4df40 | 2012-02-27 20:50:52 -0800 | [diff] [blame] | 777 | doc.PrintError();
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 778 | }
|
| 779 |
|
| 780 | {
|
| 781 | // Embedded null in stream.
|
| 782 | const char* doctype = "<element att\0r='red' attr='blue' />";
|
| 783 |
|
| 784 | XMLDocument doc;
|
| 785 | doc.Parse( doctype );
|
| 786 | XMLTest( "Embedded null throws error.", true, doc.Error() );
|
| 787 | }
|
| 788 |
|
| 789 | {
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 790 | // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 791 | const char* str = " ";
|
| 792 | XMLDocument doc;
|
| 793 | doc.Parse( str );
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 794 | XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 795 | }
|
| 796 |
|
| 797 | {
|
| 798 | // Low entities
|
| 799 | XMLDocument doc;
|
| 800 | doc.Parse( "<test></test>" );
|
| 801 | const char result[] = { 0x0e, 0 };
|
| 802 | XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
|
| 803 | doc.Print();
|
| 804 | }
|
| 805 |
|
| 806 | {
|
| 807 | // Attribute values with trailing quotes not handled correctly
|
| 808 | XMLDocument doc;
|
| 809 | doc.Parse( "<foo attribute=bar\" />" );
|
| 810 | XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
|
| 811 | }
|
| 812 |
|
| 813 | {
|
| 814 | // [ 1663758 ] Failure to report error on bad XML
|
| 815 | XMLDocument xml;
|
| 816 | xml.Parse("<x>");
|
| 817 | XMLTest("Missing end tag at end of input", xml.Error(), true);
|
| 818 | xml.Parse("<x> ");
|
| 819 | XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
|
| 820 | xml.Parse("<x></y>");
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 821 | XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 822 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 823 |
|
| 824 |
|
| 825 | {
|
| 826 | // [ 1475201 ] TinyXML parses entities in comments
|
| 827 | XMLDocument xml;
|
| 828 | xml.Parse("<!-- declarations for <head> & <body> -->"
|
| 829 | "<!-- far & away -->" );
|
| 830 |
|
| 831 | XMLNode* e0 = xml.FirstChild();
|
| 832 | XMLNode* e1 = e0->NextSibling();
|
| 833 | XMLComment* c0 = e0->ToComment();
|
| 834 | XMLComment* c1 = e1->ToComment();
|
| 835 |
|
| 836 | XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
|
| 837 | XMLTest( "Comments ignore entities.", " far & away ", c1->Value(), true );
|
| 838 | }
|
| 839 |
|
| 840 | {
|
| 841 | XMLDocument xml;
|
| 842 | xml.Parse( "<Parent>"
|
| 843 | "<child1 att=''/>"
|
| 844 | "<!-- With this comment, child2 will not be parsed! -->"
|
| 845 | "<child2 att=''/>"
|
| 846 | "</Parent>" );
|
| 847 | xml.Print();
|
| 848 |
|
| 849 | int count = 0;
|
| 850 |
|
| 851 | for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
|
| 852 | ele;
|
| 853 | ele = ele->NextSibling() )
|
| 854 | {
|
| 855 | ++count;
|
| 856 | }
|
| 857 |
|
| 858 | XMLTest( "Comments iterate correctly.", 3, count );
|
| 859 | }
|
| 860 |
|
| 861 | {
|
| 862 | // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
|
| 863 | unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
|
| 864 | buf[60] = 239;
|
| 865 | buf[61] = 0;
|
| 866 |
|
| 867 | XMLDocument doc;
|
| 868 | doc.Parse( (const char*)buf);
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 869 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 870 |
|
| 871 |
|
| 872 | {
|
| 873 | // bug 1827248 Error while parsing a little bit malformed file
|
| 874 | // Actually not malformed - should work.
|
| 875 | XMLDocument xml;
|
| 876 | xml.Parse( "<attributelist> </attributelist >" );
|
| 877 | XMLTest( "Handle end tag whitespace", false, xml.Error() );
|
| 878 | }
|
| 879 |
|
| 880 | {
|
| 881 | // This one must not result in an infinite loop
|
| 882 | XMLDocument xml;
|
| 883 | xml.Parse( "<infinite>loop" );
|
| 884 | XMLTest( "Infinite loop test.", true, true );
|
| 885 | }
|
| 886 | #endif
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 887 | {
|
| 888 | const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
|
| 889 | XMLDocument doc;
|
| 890 | doc.Parse( pub );
|
| 891 |
|
| 892 | XMLDocument clone;
|
| 893 | for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
|
| 894 | XMLNode* copy = node->ShallowClone( &clone );
|
| 895 | clone.InsertEndChild( copy );
|
| 896 | }
|
| 897 |
|
| 898 | clone.Print();
|
| 899 |
|
| 900 | int count=0;
|
| 901 | const XMLNode* a=clone.FirstChild();
|
| 902 | const XMLNode* b=doc.FirstChild();
|
| 903 | for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
|
| 904 | ++count;
|
| 905 | XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
|
| 906 | }
|
| 907 | XMLTest( "Clone and Equal", 4, count );
|
| 908 | }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 909 |
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 910 | {
|
| 911 | // This shouldn't crash.
|
| 912 | XMLDocument doc;
|
| 913 | if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
|
| 914 | {
|
| 915 | doc.PrintError();
|
| 916 | }
|
| 917 | XMLTest( "Error in snprinf handling.", true, doc.Error() );
|
| 918 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 919 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 920 | {
|
| 921 | // Attribute ordering.
|
| 922 | static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
|
| 923 | XMLDocument doc;
|
| 924 | doc.Parse( xml );
|
| 925 | XMLElement* ele = doc.FirstChildElement();
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 926 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 927 | const XMLAttribute* a = ele->FirstAttribute();
|
| 928 | XMLTest( "Attribute order", "1", a->Value() );
|
| 929 | a = a->Next();
|
| 930 | XMLTest( "Attribute order", "2", a->Value() );
|
| 931 | a = a->Next();
|
| 932 | XMLTest( "Attribute order", "3", a->Value() );
|
| 933 | XMLTest( "Attribute order", "attrib3", a->Name() );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 934 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 935 | ele->DeleteAttribute( "attrib2" );
|
| 936 | a = ele->FirstAttribute();
|
| 937 | XMLTest( "Attribute order", "1", a->Value() );
|
| 938 | a = a->Next();
|
| 939 | XMLTest( "Attribute order", "3", a->Value() );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 940 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 941 | ele->DeleteAttribute( "attrib1" );
|
| 942 | ele->DeleteAttribute( "attrib3" );
|
| 943 | XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
|
| 944 | }
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 945 |
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 946 | {
|
| 947 | // Make sure an attribute with a space in it succeeds.
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 948 | static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
|
| 949 | static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
|
| 950 | static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
|
| 951 | XMLDocument doc0;
|
| 952 | doc0.Parse( xml0 );
|
| 953 | XMLDocument doc1;
|
| 954 | doc1.Parse( xml1 );
|
| 955 | XMLDocument doc2;
|
| 956 | doc2.Parse( xml2 );
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 957 |
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 958 | XMLElement* ele = 0;
|
| 959 | ele = doc0.FirstChildElement();
|
| 960 | XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
|
| 961 | ele = doc1.FirstChildElement();
|
| 962 | XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
|
| 963 | ele = doc2.FirstChildElement();
|
| 964 | XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 965 | }
|
| 966 |
|
| 967 | {
|
| 968 | // Make sure we don't go into an infinite loop.
|
| 969 | static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
|
| 970 | XMLDocument doc;
|
| 971 | doc.Parse( xml );
|
| 972 | XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
|
| 973 | XMLElement* ele1 = ele0->NextSiblingElement();
|
| 974 | bool equal = ele0->ShallowEqual( ele1 );
|
| 975 |
|
| 976 | XMLTest( "Infinite loop in shallow equal.", true, equal );
|
| 977 | }
|
| 978 |
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 979 | // -------- Handles ------------
|
| 980 | {
|
| 981 | static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
|
| 982 | XMLDocument doc;
|
| 983 | doc.Parse( xml );
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 984 |
|
| 985 | XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
|
| 986 | XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
|
| 987 |
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 988 | XMLHandle docH( doc );
|
| 989 | ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
|
Lee Thomason | d0b19df | 2012-04-12 08:41:37 -0700 | [diff] [blame] | 990 | XMLTest( "Handle, dne, mutable", false, ele != 0 );
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 991 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 992 |
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 993 | {
|
| 994 | static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
|
| 995 | XMLDocument doc;
|
| 996 | doc.Parse( xml );
|
| 997 | XMLConstHandle docH( doc );
|
| 998 |
|
| 999 | const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
|
| 1000 | XMLTest( "Handle, success, const", ele->Value(), "sub" );
|
| 1001 |
|
| 1002 | ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
|
Lee Thomason | d0b19df | 2012-04-12 08:41:37 -0700 | [diff] [blame] | 1003 | XMLTest( "Handle, dne, const", false, ele != 0 );
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1004 | }
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 1005 | {
|
| 1006 | // Default Declaration & BOM
|
| 1007 | XMLDocument doc;
|
| 1008 | doc.InsertEndChild( doc.NewDeclaration() );
|
| 1009 | doc.SetBOM( true );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1010 |
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 1011 | XMLPrinter printer;
|
| 1012 | doc.Print( &printer );
|
| 1013 |
|
| 1014 | static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
| 1015 | XMLTest( "BOM and default declaration", printer.CStr(), result, false );
|
Lee Thomason (grinliz) | 48ea0bc | 2012-05-26 14:41:14 -0700 | [diff] [blame] | 1016 | XMLTest( "CStrSize", printer.CStrSize(), 42, false );
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 1017 | }
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1018 | {
|
| 1019 | const char* xml = "<ipxml ws='1'><info bla=' /></ipxml>";
|
| 1020 | XMLDocument doc;
|
| 1021 | doc.Parse( xml );
|
| 1022 | XMLTest( "Ill formed XML", true, doc.Error() );
|
| 1023 | }
|
| 1024 |
|
| 1025 | // QueryXYZText
|
| 1026 | {
|
| 1027 | const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
|
| 1028 | XMLDocument doc;
|
| 1029 | doc.Parse( xml );
|
| 1030 |
|
| 1031 | const XMLElement* pointElement = doc.RootElement();
|
| 1032 |
|
| 1033 | int intValue = 0;
|
| 1034 | unsigned unsignedValue = 0;
|
| 1035 | float floatValue = 0;
|
| 1036 | double doubleValue = 0;
|
| 1037 | bool boolValue = false;
|
| 1038 |
|
| 1039 | pointElement->FirstChildElement( "y" )->QueryIntText( &intValue );
|
| 1040 | pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
|
| 1041 | pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
|
| 1042 | pointElement->FirstChildElement( "x" )->QueryDoubleText( &doubleValue );
|
| 1043 | pointElement->FirstChildElement( "valid" )->QueryBoolText( &boolValue );
|
| 1044 |
|
| 1045 |
|
| 1046 | XMLTest( "QueryIntText", intValue, 1, false );
|
| 1047 | XMLTest( "QueryUnsignedText", unsignedValue, (unsigned)1, false );
|
| 1048 | XMLTest( "QueryFloatText", floatValue, 1.2f, false );
|
| 1049 | XMLTest( "QueryDoubleText", doubleValue, 1.2, false );
|
| 1050 | XMLTest( "QueryBoolText", boolValue, true, false );
|
| 1051 | }
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1052 |
|
Lee Thomason (grinliz) | 5fbacbe | 2012-09-08 21:40:53 -0700 | [diff] [blame] | 1053 | {
|
| 1054 | const char* xml = "<element><_sub/><:sub/><sub:sub/><sub-sub/></element>";
|
| 1055 | XMLDocument doc;
|
| 1056 | doc.Parse( xml );
|
| 1057 | XMLTest( "Non-alpha element lead letter parses.", doc.Error(), false );
|
| 1058 | }
|
Martinsh Shaiters | 23e7ae6 | 2013-01-26 20:15:44 +0200 | [diff] [blame] | 1059 |
|
| 1060 | {
|
| 1061 | const char* xml = "<element _attr1=\"foo\" :attr2=\"bar\"></element>";
|
| 1062 | XMLDocument doc;
|
Martinsh Shaiters | 95b3e65 | 2013-01-26 23:08:10 +0200 | [diff] [blame] | 1063 | doc.Parse( xml );
|
Martinsh Shaiters | 23e7ae6 | 2013-01-26 20:15:44 +0200 | [diff] [blame] | 1064 | XMLTest("Non-alpha attribute lead character parses.", doc.Error(), false);
|
| 1065 | }
|
Martinsh Shaiters | 95b3e65 | 2013-01-26 23:08:10 +0200 | [diff] [blame] | 1066 |
|
| 1067 | {
|
| 1068 | const char* xml = "<3lement></3lement>";
|
| 1069 | XMLDocument doc;
|
| 1070 | doc.Parse( xml );
|
| 1071 | XMLTest("Element names with lead digit fail to parse.", doc.Error(), true);
|
| 1072 | }
|
Lee Thomason (grinliz) | 62d1c5a | 2012-09-08 21:44:12 -0700 | [diff] [blame] | 1073 |
|
Lee Thomason (grinliz) | e2bcb32 | 2012-09-17 17:58:25 -0700 | [diff] [blame] | 1074 | {
|
| 1075 | const char* xml = "<element/>WOA THIS ISN'T GOING TO PARSE";
|
| 1076 | XMLDocument doc;
|
| 1077 | doc.Parse( xml, 10 );
|
Lee Thomason (grinliz) | e2bcb32 | 2012-09-17 17:58:25 -0700 | [diff] [blame] | 1078 | XMLTest( "Set length of incoming data", doc.Error(), false );
|
| 1079 | }
|
| 1080 |
|
Martinsh Shaiters | 53ab79a | 2013-01-30 11:21:36 +0200 | [diff] [blame] | 1081 | {
|
| 1082 | XMLDocument doc;
|
| 1083 | doc.LoadFile( "resources/dream.xml" );
|
| 1084 | doc.Clear();
|
| 1085 | XMLTest( "Document Clear()'s", doc.NoChildren(), true );
|
| 1086 | }
|
| 1087 |
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 1088 | // ----------- Whitespace ------------
|
| 1089 | {
|
| 1090 | const char* xml = "<element>"
|
| 1091 | "<a> This \nis ' text ' </a>"
|
| 1092 | "<b> This is ' text ' \n</b>"
|
| 1093 | "<c>This is ' \n\n text '</c>"
|
| 1094 | "</element>";
|
| 1095 | XMLDocument doc( true, COLLAPSE_WHITESPACE );
|
| 1096 | doc.Parse( xml );
|
| 1097 |
|
| 1098 | const XMLElement* element = doc.FirstChildElement();
|
| 1099 | for( const XMLElement* parent = element->FirstChildElement();
|
| 1100 | parent;
|
| 1101 | parent = parent->NextSiblingElement() )
|
| 1102 | {
|
| 1103 | XMLTest( "Whitespace collapse", "This is ' text '", parent->GetText() );
|
| 1104 | }
|
| 1105 | }
|
Lee Thomason (grinliz) | 0fa8299 | 2012-09-08 21:53:47 -0700 | [diff] [blame] | 1106 |
|
Lee Thomason | ae9ab07 | 2012-10-24 10:17:53 -0700 | [diff] [blame] | 1107 | #if 0
|
| 1108 | {
|
| 1109 | // Passes if assert doesn't fire.
|
| 1110 | XMLDocument xmlDoc;
|
| 1111 |
|
| 1112 | xmlDoc.NewDeclaration();
|
| 1113 | xmlDoc.NewComment("Configuration file");
|
| 1114 |
|
| 1115 | XMLElement *root = xmlDoc.NewElement("settings");
|
| 1116 | root->SetAttribute("version", 2);
|
| 1117 | }
|
| 1118 | #endif
|
| 1119 |
|
Lee Thomason (grinliz) | 0fa8299 | 2012-09-08 21:53:47 -0700 | [diff] [blame] | 1120 | {
|
| 1121 | const char* xml = "<element> </element>";
|
| 1122 | XMLDocument doc( true, COLLAPSE_WHITESPACE );
|
| 1123 | doc.Parse( xml );
|
| 1124 | XMLTest( "Whitespace all space", true, 0 == doc.FirstChildElement()->FirstChild() );
|
| 1125 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1126 |
|
Lee Thomason (grinliz) | fc6320e | 2012-09-23 20:25:50 -0700 | [diff] [blame] | 1127 | #if 0 // the question being explored is what kind of print to use:
|
| 1128 | // https://github.com/leethomason/tinyxml2/issues/63
|
| 1129 | {
|
| 1130 | const char* xml = "<element attrA='123456789.123456789' attrB='1.001e9'/>";
|
| 1131 | XMLDocument doc;
|
| 1132 | doc.Parse( xml );
|
| 1133 | doc.FirstChildElement()->SetAttribute( "attrA", 123456789.123456789 );
|
| 1134 | doc.FirstChildElement()->SetAttribute( "attrB", 1.001e9 );
|
| 1135 | doc.Print();
|
| 1136 | }
|
| 1137 | #endif
|
| 1138 |
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 1139 | {
|
| 1140 | // An assert should not fire.
|
| 1141 | const char* xml = "<element/>";
|
| 1142 | XMLDocument doc;
|
| 1143 | doc.Parse( xml );
|
| 1144 | XMLElement* ele = doc.NewElement( "unused" ); // This will get cleaned up with the 'doc' going out of scope.
|
| 1145 | XMLTest( "Tracking unused elements", true, ele != 0, false );
|
| 1146 | }
|
| 1147 |
|
Lee Thomason | a6412ac | 2012-12-13 15:39:11 -0800 | [diff] [blame] | 1148 |
|
| 1149 | {
|
| 1150 | const char* xml = "<parent><child>abc</child></parent>";
|
| 1151 | XMLDocument doc;
|
| 1152 | doc.Parse( xml );
|
| 1153 | XMLElement* ele = doc.FirstChildElement( "parent")->FirstChildElement( "child");
|
| 1154 |
|
| 1155 | XMLPrinter printer;
|
| 1156 | ele->Accept( &printer );
|
| 1157 | XMLTest( "Printing of sub-element", "<child>abc</child>\n", printer.CStr(), false );
|
| 1158 | }
|
| 1159 |
|
| 1160 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1161 | // ----------- Performance tracking --------------
|
| 1162 | {
|
| 1163 | #if defined( _MSC_VER )
|
| 1164 | __int64 start, end, freq;
|
| 1165 | QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
|
| 1166 | #endif
|
| 1167 |
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 1168 | FILE* fp = fopen( "resources/dream.xml", "r" );
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1169 | fseek( fp, 0, SEEK_END );
|
| 1170 | long size = ftell( fp );
|
| 1171 | fseek( fp, 0, SEEK_SET );
|
| 1172 |
|
| 1173 | char* mem = new char[size+1];
|
| 1174 | fread( mem, size, 1, fp );
|
| 1175 | fclose( fp );
|
| 1176 | mem[size] = 0;
|
| 1177 |
|
| 1178 | #if defined( _MSC_VER )
|
| 1179 | QueryPerformanceCounter( (LARGE_INTEGER*) &start );
|
| 1180 | #else
|
| 1181 | clock_t cstart = clock();
|
| 1182 | #endif
|
| 1183 | static const int COUNT = 10;
|
| 1184 | for( int i=0; i<COUNT; ++i ) {
|
| 1185 | XMLDocument doc;
|
| 1186 | doc.Parse( mem );
|
| 1187 | }
|
| 1188 | #if defined( _MSC_VER )
|
| 1189 | QueryPerformanceCounter( (LARGE_INTEGER*) &end );
|
| 1190 | #else
|
| 1191 | clock_t cend = clock();
|
| 1192 | #endif
|
| 1193 |
|
| 1194 | delete [] mem;
|
| 1195 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1196 | static const char* note =
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1197 | #ifdef DEBUG
|
| 1198 | "DEBUG";
|
| 1199 | #else
|
| 1200 | "Release";
|
| 1201 | #endif
|
| 1202 |
|
| 1203 | #if defined( _MSC_VER )
|
| 1204 | printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
|
| 1205 | #else
|
| 1206 | printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
|
| 1207 | #endif
|
| 1208 | }
|
| 1209 |
|
Lee Thomason (grinliz) | 7ca5558 | 2012-03-07 21:54:57 -0800 | [diff] [blame] | 1210 | #if defined( _MSC_VER ) && defined( DEBUG )
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1211 | _CrtMemCheckpoint( &endMemState );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1212 | //_CrtMemDumpStatistics( &endMemState );
|
| 1213 |
|
| 1214 | _CrtMemState diffMemState;
|
| 1215 | _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
|
| 1216 | _CrtMemDumpStatistics( &diffMemState );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1217 | //printf( "new total=%d\n", gNewTotal );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1218 | #endif
|
| 1219 |
|
| 1220 | printf ("\nPass %d, Fail %d\n", gPass, gFail);
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1221 | return 0;
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 1222 | }
|