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