Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1 | #ifndef TINYXML_INCLUDED
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2 | #define TINYXML2_INCLUDED
|
| 3 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 4 | /*
|
| 5 | TODO
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 6 | X const and non-const versions of API
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 7 | X memory pool the class construction
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 8 | X attribute accessors
|
| 9 | X node navigation
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 10 | - handles
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 11 | X visit pattern - change streamer?
|
| 12 | X make constructors protected
|
| 13 | X hide copy constructor
|
| 14 | X hide = operator
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 15 | X UTF8 support: isAlpha, etc.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame^] | 16 | - string buffer for sets. (Grr.)
|
| 17 | - MS BOM
|
| 18 | - print to memory buffer
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 19 | - tests from xml1
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame^] | 20 | - xml1 tests especially UTF-8
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 21 | - perf test: xml1
|
| 22 | - perf test: xenowar
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 23 | */
|
| 24 |
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 25 | #include <limits.h>
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 26 | #include <ctype.h>
|
| 27 | #include <stdio.h>
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 28 | #include <memory.h>
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 29 |
|
| 30 | #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
|
| 31 | #ifndef DEBUG
|
| 32 | #define DEBUG
|
| 33 | #endif
|
| 34 | #endif
|
| 35 |
|
| 36 |
|
| 37 | #if defined(DEBUG)
|
| 38 | #if defined(_MSC_VER)
|
| 39 | #define TIXMLASSERT( x ) if ( !(x)) { _asm { int 3 } } //if ( !(x)) WinDebugBreak()
|
| 40 | #elif defined (ANDROID_NDK)
|
| 41 | #include <android/log.h>
|
| 42 | #define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
|
| 43 | #else
|
| 44 | #include <assert.h>
|
| 45 | #define TIXMLASSERT assert
|
| 46 | #endif
|
| 47 | #else
|
| 48 | #define TIXMLASSERT( x ) {}
|
| 49 | #endif
|
| 50 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 51 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame^] | 52 | // Deprecated library function hell. Compilers want to use the |
| 53 | // new safe versions. This probably doesn't fully address the problem, |
| 54 | // but it gets closer. There are too many compilers for me to fully |
| 55 | // test. If you get compilation troubles, undefine TIXML_SAFE |
| 56 | |
| 57 | #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) |
| 58 | // Microsoft visual studio, version 2005 and higher. |
| 59 | #define TIXML_SNPRINTF _snprintf_s |
| 60 | #define TIXML_SSCANF sscanf_s |
| 61 | #elif defined(_MSC_VER) && (_MSC_VER >= 1200 ) |
| 62 | // Microsoft visual studio, version 6 and higher. |
| 63 | //#pragma message( "Using _sn* functions." ) |
| 64 | #define TIXML_SNPRINTF _snprintf |
| 65 | #define TIXML_SSCANF sscanf |
| 66 | #elif defined(__GNUC__) && (__GNUC__ >= 3 ) |
| 67 | // GCC version 3 and higher.s |
| 68 | //#warning( "Using sn* functions." ) |
| 69 | #define TIXML_SNPRINTF snprintf |
| 70 | #define TIXML_SSCANF sscanf |
| 71 | #else |
| 72 | #define TIXML_SNPRINTF snprintf |
| 73 | #define TIXML_SSCANF sscanf |
| 74 | #endif |
| 75 |
|
| 76 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 77 | namespace tinyxml2
|
| 78 | {
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 79 | class XMLDocument;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 80 | class XMLElement;
|
| 81 | class XMLAttribute;
|
| 82 | class XMLComment;
|
| 83 | class XMLNode;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 84 | class XMLText;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 85 | class XMLDeclaration;
|
| 86 | class XMLUnknown;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 87 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 88 | class XMLStreamer;
|
| 89 |
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 90 | class StrPair
|
| 91 | {
|
Lee Thomason | d34f52c | 2012-01-20 12:55:24 -0800 | [diff] [blame] | 92 | public:
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 93 | enum {
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 94 | NEEDS_ENTITY_PROCESSING = 0x01,
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 95 | NEEDS_NEWLINE_NORMALIZATION = 0x02,
|
| 96 |
|
| 97 | TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
|
| 98 | ATTRIBUTE_NAME = 0,
|
| 99 | ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
|
| 100 | COMMENT = NEEDS_NEWLINE_NORMALIZATION,
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 101 | };
|
| 102 |
|
| 103 | StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 104 | void Set( char* start, char* end, int flags ) {
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 105 | this->start = start; this->end = end; this->flags = flags | NEEDS_FLUSH;
|
| 106 | }
|
| 107 | const char* GetStr();
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 108 | bool Empty() const { return start == end; }
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 109 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 110 | void SetInternedStr( const char* str ) { this->start = (char*) str; this->end = 0; this->flags = 0; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 111 | char* ParseText( char* in, const char* endTag, int strFlags );
|
| 112 | char* ParseName( char* in );
|
| 113 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 114 |
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 115 | private:
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 116 | enum {
|
| 117 | NEEDS_FLUSH = 0x100
|
| 118 | };
|
| 119 |
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 120 | // After parsing, if *end != 0, it can be set to zero.
|
| 121 | int flags;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 122 | char* start;
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 123 | char* end;
|
| 124 | };
|
| 125 |
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 126 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 127 | template <class T, int INIT>
|
| 128 | class DynArray
|
| 129 | {
|
| 130 | public:
|
| 131 | DynArray< T, INIT >()
|
| 132 | {
|
| 133 | mem = pool;
|
| 134 | allocated = INIT;
|
| 135 | size = 0;
|
| 136 | }
|
| 137 | ~DynArray()
|
| 138 | {
|
| 139 | if ( mem != pool ) {
|
| 140 | delete mem;
|
| 141 | }
|
| 142 | }
|
| 143 | void Push( T t )
|
| 144 | {
|
| 145 | EnsureCapacity( size+1 );
|
| 146 | mem[size++] = t;
|
| 147 | }
|
| 148 |
|
| 149 | T* PushArr( int count )
|
| 150 | {
|
| 151 | EnsureCapacity( size+count );
|
| 152 | T* ret = &mem[size];
|
| 153 | size += count;
|
| 154 | return ret;
|
| 155 | }
|
| 156 | T Pop() {
|
| 157 | return mem[--size];
|
| 158 | }
|
| 159 | void PopArr( int count )
|
| 160 | {
|
| 161 | TIXMLASSERT( size >= count );
|
| 162 | size -= count;
|
| 163 | }
|
| 164 |
|
| 165 | bool Empty() const { return size == 0; }
|
| 166 | T& operator[](int i) { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; }
|
| 167 | const T& operator[](int i) const { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; }
|
| 168 | int Size() const { return size; }
|
| 169 | const T* Mem() const { return mem; }
|
| 170 | T* Mem() { return mem; }
|
| 171 |
|
| 172 |
|
| 173 | private:
|
| 174 | void EnsureCapacity( int cap ) {
|
| 175 | if ( cap > allocated ) {
|
| 176 | int newAllocated = cap * 2;
|
| 177 | T* newMem = new T[newAllocated];
|
| 178 | memcpy( newMem, mem, sizeof(T)*size ); // warning: not using constructors, only works for PODs
|
| 179 | if ( mem != pool ) delete [] mem;
|
| 180 | mem = newMem;
|
| 181 | allocated = newAllocated;
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | T* mem;
|
| 186 | T pool[INIT];
|
| 187 | int allocated; // objects allocated
|
| 188 | int size; // number objects in use
|
| 189 | };
|
| 190 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 191 |
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 192 | class MemPool
|
| 193 | {
|
| 194 | public:
|
| 195 | MemPool() {}
|
| 196 | virtual ~MemPool() {}
|
| 197 |
|
| 198 | virtual int ItemSize() const = 0;
|
| 199 | virtual void* Alloc() = 0;
|
| 200 | virtual void Free( void* ) = 0;
|
| 201 | };
|
| 202 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 203 |
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 204 | template< int SIZE >
|
| 205 | class MemPoolT : public MemPool
|
| 206 | {
|
| 207 | public:
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 208 | MemPoolT() : root(0), currentAllocs(0), nAllocs(0), maxAllocs(0) {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 209 | ~MemPoolT() {
|
| 210 | // Delete the blocks.
|
| 211 | for( int i=0; i<blockPtrs.Size(); ++i ) {
|
| 212 | delete blockPtrs[i];
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | virtual int ItemSize() const { return SIZE; }
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 217 | int CurrentAllocs() const { return currentAllocs; }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 218 |
|
| 219 | virtual void* Alloc() {
|
| 220 | if ( !root ) {
|
| 221 | // Need a new block.
|
| 222 | Block* block = new Block();
|
| 223 | blockPtrs.Push( block );
|
| 224 |
|
| 225 | for( int i=0; i<COUNT-1; ++i ) {
|
| 226 | block->chunk[i].next = &block->chunk[i+1];
|
| 227 | }
|
| 228 | block->chunk[COUNT-1].next = 0;
|
| 229 | root = block->chunk;
|
| 230 | }
|
| 231 | void* result = root;
|
| 232 | root = root->next;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 233 |
|
| 234 | ++currentAllocs;
|
| 235 | if ( currentAllocs > maxAllocs ) maxAllocs = currentAllocs;
|
| 236 | nAllocs++;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 237 | return result;
|
| 238 | }
|
| 239 | virtual void Free( void* mem ) {
|
| 240 | if ( !mem ) return;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 241 | --currentAllocs;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 242 | Chunk* chunk = (Chunk*)mem;
|
| 243 | memset( chunk, 0xfe, sizeof(Chunk) );
|
| 244 | chunk->next = root;
|
| 245 | root = chunk;
|
| 246 | }
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 247 | void Trace( const char* name ) {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 248 | printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
|
| 249 | name, maxAllocs, maxAllocs*SIZE/1024, currentAllocs, SIZE, nAllocs, blockPtrs.Size() );
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 250 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 251 |
|
| 252 | private:
|
| 253 | enum { COUNT = 1024/SIZE };
|
| 254 | union Chunk {
|
| 255 | Chunk* next;
|
| 256 | char mem[SIZE];
|
| 257 | };
|
| 258 | struct Block {
|
| 259 | Chunk chunk[COUNT];
|
| 260 | };
|
| 261 | DynArray< Block*, 10 > blockPtrs;
|
| 262 | Chunk* root;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 263 |
|
| 264 | int currentAllocs;
|
| 265 | int nAllocs;
|
| 266 | int maxAllocs;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 267 | };
|
| 268 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 269 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 270 |
|
| 271 | /**
|
| 272 | Implements the interface to the "Visitor pattern" (see the Accept() method.)
|
| 273 | If you call the Accept() method, it requires being passed a XMLVisitor
|
| 274 | class to handle callbacks. For nodes that contain other nodes (Document, Element)
|
| 275 | you will get called with a VisitEnter/VisitExit pair. Nodes that are always leaves
|
| 276 | are simply called with Visit().
|
| 277 |
|
| 278 | If you return 'true' from a Visit method, recursive parsing will continue. If you return
|
| 279 | false, <b>no children of this node or its sibilings</b> will be Visited.
|
| 280 |
|
| 281 | All flavors of Visit methods have a default implementation that returns 'true' (continue
|
| 282 | visiting). You need to only override methods that are interesting to you.
|
| 283 |
|
| 284 | Generally Accept() is called on the TiXmlDocument, although all nodes suppert Visiting.
|
| 285 |
|
| 286 | You should never change the document from a callback.
|
| 287 |
|
| 288 | @sa XMLNode::Accept()
|
| 289 | */
|
| 290 | class XMLVisitor
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 291 | {
|
| 292 | public:
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 293 | virtual ~XMLVisitor() {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 294 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 295 | /// Visit a document.
|
| 296 | virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { return true; }
|
| 297 | /// Visit a document.
|
| 298 | virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; }
|
| 299 |
|
| 300 | /// Visit an element.
|
| 301 | virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) { return true; }
|
| 302 | /// Visit an element.
|
| 303 | virtual bool VisitExit( const XMLElement& /*element*/ ) { return true; }
|
| 304 |
|
| 305 | /// Visit a declaration
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 306 | virtual bool Visit( const XMLDeclaration& /*declaration*/ ) { return true; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 307 | /// Visit a text node
|
| 308 | virtual bool Visit( const XMLText& /*text*/ ) { return true; }
|
| 309 | /// Visit a comment node
|
| 310 | virtual bool Visit( const XMLComment& /*comment*/ ) { return true; }
|
| 311 | /// Visit an unknown node
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 312 | virtual bool Visit( const XMLUnknown& /*unknown*/ ) { return true; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 313 | };
|
| 314 |
|
| 315 |
|
| 316 | class XMLUtil
|
| 317 | {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 318 | public:
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 319 | // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
|
| 320 | // correct, but simple, and usually works.
|
| 321 | static const char* SkipWhiteSpace( const char* p ) { while( IsUTF8Continuation(*p) || isspace( *p ) ) { ++p; } return p; }
|
| 322 | static char* SkipWhiteSpace( char* p ) { while( IsUTF8Continuation(*p) || isspace( *p ) ) { ++p; } return p; }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 323 |
|
| 324 | inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
|
| 325 | int n = 0;
|
Lee Thomason | d34f52c | 2012-01-20 12:55:24 -0800 | [diff] [blame] | 326 | if ( p == q ) {
|
| 327 | return true;
|
| 328 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 329 | while( *p && *q && *p == *q && n<nChar ) {
|
| 330 | ++p; ++q; ++n;
|
| 331 | }
|
| 332 | if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
|
| 333 | return true;
|
| 334 | }
|
| 335 | return false;
|
| 336 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 337 | inline static int IsUTF8Continuation( unsigned char p ) { return p & 0x80; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 338 | inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte < 128 ) ? isalnum( anyByte ) : 1; }
|
| 339 | inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte < 128 ) ? isalpha( anyByte ) : 1; }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 340 | };
|
| 341 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 342 |
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 343 | class XMLNode
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 344 | {
|
| 345 | friend class XMLDocument;
|
| 346 | friend class XMLElement;
|
| 347 | public:
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 348 | const XMLDocument* GetDocument() const { return document; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 349 | XMLDocument* GetDocument() { return document; }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 350 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 351 | virtual XMLElement* ToElement() { return 0; }
|
| 352 | virtual XMLText* ToText() { return 0; }
|
| 353 | virtual XMLComment* ToComment() { return 0; }
|
| 354 | virtual XMLDocument* ToDocument() { return 0; }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 355 | virtual XMLDeclaration* ToDeclaration() { return 0; }
|
| 356 | virtual XMLUnknown* ToUnknown() { return 0; }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 357 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 358 | virtual const XMLElement* ToElement() const { return 0; }
|
| 359 | virtual const XMLText* ToText() const { return 0; }
|
| 360 | virtual const XMLComment* ToComment() const { return 0; }
|
| 361 | virtual const XMLDocument* ToDocument() const { return 0; }
|
| 362 | virtual const XMLDeclaration* ToDeclaration() const { return 0; }
|
| 363 | virtual const XMLUnknown* ToUnknown() const { return 0; }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 364 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 365 | const char* Value() const { return value.GetStr(); }
|
| 366 | void SetValue( const char* val ) { value.SetInternedStr( val ); }
|
| 367 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 368 | const XMLNode* Parent() const { return parent; }
|
| 369 | XMLNode* Parent() { return parent; }
|
| 370 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 371 | /// Returns true if this node has no children.
|
| 372 | bool NoChildren() const { return !firstChild; }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 373 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 374 | const XMLNode* FirstChild() const { return firstChild; }
|
| 375 | XMLNode* FirstChild() { return firstChild; }
|
| 376 | const XMLElement* FirstChildElement( const char* value=0 ) const;
|
| 377 | XMLElement* FirstChildElement( const char* value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( value )); }
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 378 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 379 | const XMLNode* LastChild() const { return lastChild; }
|
| 380 | XMLNode* LastChild() { return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() ); }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 381 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 382 | const XMLElement* LastChildElement( const char* value=0 ) const;
|
| 383 | XMLElement* LastChildElement( const char* value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(value) ); }
|
| 384 |
|
| 385 | const XMLNode* PreviousSibling() const { return prev; }
|
| 386 | XMLNode* PreviousSibling() { return prev; }
|
| 387 |
|
| 388 | const XMLNode* PreviousSiblingElement( const char* value=0 ) const ;
|
| 389 | XMLNode* PreviousSiblingElement( const char* value=0 ) { return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( value ) ); }
|
| 390 |
|
| 391 | const XMLNode* NextSibling() const { return next; }
|
| 392 | XMLNode* NextSibling() { return next; }
|
| 393 |
|
| 394 | const XMLNode* NextSiblingElement( const char* value=0 ) const;
|
| 395 | XMLNode* NextSiblingElement( const char* value=0 ) { return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->NextSiblingElement( value ) ); }
|
| 396 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame^] | 397 | /**
|
| 398 |
|
| 399 | Tests: Programmatic DOM
|
| 400 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 401 | XMLNode* InsertEndChild( XMLNode* addThis );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame^] | 402 | /**
|
| 403 |
|
| 404 | Tests: Programmatic DOM
|
| 405 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 406 | XMLNode* InsertFirstChild( XMLNode* addThis );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame^] | 407 | /**
|
| 408 |
|
| 409 | Tests: Programmatic DOM
|
| 410 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 411 | XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
|
| 412 |
|
| 413 | void ClearChildren();
|
| 414 | void DeleteChild( XMLNode* node );
|
| 415 |
|
| 416 | virtual bool Accept( XMLVisitor* visitor ) const = 0;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 417 | //virtual void Print( XMLStreamer* streamer );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 418 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 419 | virtual char* ParseDeep( char* );
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 420 | virtual bool IsClosingElement() const { return false; }
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 421 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 422 | protected:
|
| 423 | XMLNode( XMLDocument* );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 424 | virtual ~XMLNode();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 425 | XMLNode( const XMLNode& ); // not supported
|
| 426 | void operator=( const XMLNode& ); // not supported
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 427 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 428 | XMLDocument* document;
|
| 429 | XMLNode* parent;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 430 | mutable StrPair value;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 431 |
|
| 432 | XMLNode* firstChild;
|
| 433 | XMLNode* lastChild;
|
| 434 |
|
| 435 | XMLNode* prev;
|
| 436 | XMLNode* next;
|
| 437 |
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 438 | private:
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 439 | MemPool* memPool;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 440 | void Unlink( XMLNode* child );
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 441 | };
|
| 442 |
|
| 443 |
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 444 | class XMLText : public XMLNode
|
| 445 | {
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 446 | friend class XMLBase;
|
| 447 | friend class XMLDocument;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 448 | public:
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 449 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 450 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 451 | virtual XMLText* ToText() { return this; }
|
| 452 | virtual const XMLText* ToText() const { return this; }
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 453 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 454 | void SetCData( bool value ) { isCData = true; }
|
| 455 | bool CData() const { return isCData; }
|
| 456 |
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 457 | char* ParseDeep( char* );
|
| 458 |
|
| 459 | protected:
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 460 | XMLText( XMLDocument* doc ) : XMLNode( doc ), isCData( false ) {}
|
| 461 | virtual ~XMLText() {}
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 462 | XMLText( const XMLText& ); // not supported
|
| 463 | void operator=( const XMLText& ); // not supported
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 464 |
|
| 465 | private:
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 466 | bool isCData;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 467 | };
|
| 468 |
|
| 469 |
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 470 | class XMLComment : public XMLNode
|
| 471 | {
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 472 | friend class XMLDocument;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 473 | public:
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 474 | virtual XMLComment* ToComment() { return this; }
|
| 475 | virtual const XMLComment* ToComment() const { return this; }
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 476 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 477 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 478 |
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 479 | char* ParseDeep( char* );
|
| 480 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 481 | protected:
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 482 | XMLComment( XMLDocument* doc );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 483 | virtual ~XMLComment();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 484 | XMLComment( const XMLComment& ); // not supported
|
| 485 | void operator=( const XMLComment& ); // not supported
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 486 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 487 | private:
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 488 | };
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 489 |
|
| 490 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 491 | class XMLDeclaration : public XMLNode
|
| 492 | {
|
| 493 | friend class XMLDocument;
|
| 494 | public:
|
| 495 | virtual XMLDeclaration* ToDeclaration() { return this; }
|
| 496 | virtual const XMLDeclaration* ToDeclaration() const { return this; }
|
| 497 |
|
| 498 | virtual bool Accept( XMLVisitor* visitor ) const;
|
| 499 |
|
| 500 | char* ParseDeep( char* );
|
| 501 |
|
| 502 | protected:
|
| 503 | XMLDeclaration( XMLDocument* doc );
|
| 504 | virtual ~XMLDeclaration();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 505 | XMLDeclaration( const XMLDeclaration& ); // not supported
|
| 506 | void operator=( const XMLDeclaration& ); // not supported
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 507 | };
|
| 508 |
|
| 509 |
|
| 510 | class XMLUnknown : public XMLNode
|
| 511 | {
|
| 512 | friend class XMLDocument;
|
| 513 | public:
|
| 514 | virtual XMLUnknown* ToUnknown() { return this; }
|
| 515 | virtual const XMLUnknown* ToUnknown() const { return this; }
|
| 516 |
|
| 517 | virtual bool Accept( XMLVisitor* visitor ) const;
|
| 518 |
|
| 519 | char* ParseDeep( char* );
|
| 520 |
|
| 521 | protected:
|
| 522 | XMLUnknown( XMLDocument* doc );
|
| 523 | virtual ~XMLUnknown();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 524 | XMLUnknown( const XMLUnknown& ); // not supported
|
| 525 | void operator=( const XMLUnknown& ); // not supported
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 526 | };
|
| 527 |
|
| 528 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame^] | 529 | enum {
|
| 530 | ATTRIBUTE_SUCCESS,
|
| 531 | NO_ATTRIBUTE,
|
| 532 | WRONG_ATTRIBUTE_TYPE
|
| 533 | };
|
| 534 |
|
| 535 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 536 | class XMLAttribute
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 537 | {
|
| 538 | friend class XMLElement;
|
| 539 | public:
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 540 | const char* Name() const { return name.GetStr(); }
|
| 541 | const char* Value() const { return value.GetStr(); }
|
| 542 | const XMLAttribute* Next() const { return next; }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 543 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame^] | 544 | int QueryIntAttribute( int* value ) const;
|
| 545 | int QueryUnsignedAttribute( unsigned int* value ) const;
|
| 546 | int QueryBoolAttribute( bool* value ) const;
|
| 547 | int QueryDoubleAttribute( double* value ) const;
|
| 548 | int QueryFloatAttribute( float* value ) const;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 549 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame^] | 550 | void SetAttribute( const char* value );
|
| 551 |
|
| 552 | // NOTE: other sets aren't supported...need to deal with memory model?
|
| 553 | /*
|
| 554 | void SetAttribute( int value );
|
| 555 | void SetAttribute( unsigned value );
|
| 556 | void SetAttribute( bool value );
|
| 557 | void SetAttribute( double value );
|
| 558 | */
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 559 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 560 | private:
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 561 | XMLAttribute( XMLElement* element ) : next( 0 ) {}
|
| 562 | virtual ~XMLAttribute() {}
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 563 | XMLAttribute( const XMLAttribute& ); // not supported
|
| 564 | void operator=( const XMLAttribute& ); // not supported
|
| 565 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 566 | char* ParseDeep( char* p );
|
| 567 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 568 | mutable StrPair name;
|
| 569 | mutable StrPair value;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 570 | XMLAttribute* next;
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 571 | MemPool* memPool;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 572 | };
|
| 573 |
|
| 574 |
|
| 575 | class XMLElement : public XMLNode
|
| 576 | {
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 577 | friend class XMLBase;
|
| 578 | friend class XMLDocument;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 579 | public:
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 580 | const char* Name() const { return Value(); }
|
| 581 | void SetName( const char* str ) { SetValue( str ); }
|
| 582 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 583 | virtual XMLElement* ToElement() { return this; }
|
| 584 | virtual const XMLElement* ToElement() const { return this; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 585 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 586 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 587 | const char* Attribute( const char* name ) const;
|
| 588 |
|
| 589 | int QueryIntAttribute( const char* name, int* value ) const;
|
| 590 | int QueryUnsignedAttribute( const char* name, unsigned int* value ) const;
|
| 591 | int QueryBoolAttribute( const char* name, bool* value ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 592 | int QueryDoubleAttribute( const char* name, double* _value ) const;
|
| 593 | int QueryFloatAttribute( const char* name, float* _value ) const;
|
| 594 |
|
| 595 | void SetAttribute( const char* name, const char* value );
|
| 596 | void SetAttribute( const char* name, int value );
|
| 597 | void SetAttribute( const char* name, unsigned value );
|
| 598 | void SetAttribute( const char* name, bool value );
|
| 599 | void SetAttribute( const char* name, double value );
|
| 600 |
|
| 601 | void RemoveAttribute( const char* name );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 602 |
|
| 603 | const XMLAttribute* FirstAttribute() const { return rootAttribute; }
|
| 604 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 605 | const char* GetText() const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 606 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 607 | // internal:
|
| 608 | virtual bool IsClosingElement() const { return closing; }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 609 | char* ParseDeep( char* p );
|
| 610 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 611 | private:
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 612 | XMLElement( XMLDocument* doc );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 613 | virtual ~XMLElement();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 614 | XMLElement( const XMLElement& ); // not supported
|
| 615 | void operator=( const XMLElement& ); // not supported
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 616 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 617 | char* ParseAttributes( char* p, bool *closedElement );
|
| 618 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 619 | bool closing;
|
| 620 | XMLAttribute* rootAttribute;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 621 | XMLAttribute* lastAttribute; // fixme: remove
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 622 | };
|
| 623 |
|
| 624 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 625 | class XMLDocument : public XMLNode
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 626 | {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 627 | friend class XMLElement;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 628 | public:
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 629 | XMLDocument();
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 630 | ~XMLDocument();
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 631 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 632 | virtual XMLDocument* ToDocument() { return this; }
|
| 633 | virtual const XMLDocument* ToDocument() const { return this; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 634 |
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 635 | int Parse( const char* );
|
| 636 | int Load( const char* );
|
| 637 | int Load( FILE* );
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 638 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 639 | void Print( XMLStreamer* streamer=0 );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 640 | virtual bool Accept( XMLVisitor* visitor ) const;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 641 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame^] | 642 | /**
|
| 643 | Testing: Programmatic DOM
|
| 644 | */
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 645 | XMLElement* NewElement( const char* name );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame^] | 646 | /**
|
| 647 | Testing: Programmatic DOM
|
| 648 | */
|
| 649 | XMLComment* NewComment( const char* comment );
|
| 650 | /**
|
| 651 | Testing: Programmatic DOM
|
| 652 | */
|
| 653 | XMLText* NewText( const char* text );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 654 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 655 | enum {
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 656 | NO_ERROR = 0,
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 657 | ERROR_ELEMENT_MISMATCH,
|
| 658 | ERROR_PARSING_ELEMENT,
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 659 | ERROR_PARSING_ATTRIBUTE,
|
| 660 | ERROR_IDENTIFYING_TAG
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 661 | };
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 662 | void SetError( int error, const char* str1, const char* str2 );
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 663 |
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 664 | bool Error() const { return errorID != NO_ERROR; }
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 665 | int GetErrorID() const { return errorID; }
|
| 666 | const char* GetErrorStr1() const { return errorStr1; }
|
| 667 | const char* GetErrorStr2() const { return errorStr2; }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 668 |
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 669 | char* Identify( char* p, XMLNode** node );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 670 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 671 | private:
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 672 | XMLDocument( const XMLDocument& ); // not supported
|
| 673 | void operator=( const XMLDocument& ); // not supported
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 674 | void InitDocument();
|
| 675 |
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 676 | int errorID;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 677 | const char* errorStr1;
|
| 678 | const char* errorStr2;
|
| 679 | char* charBuffer;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 680 |
|
| 681 | MemPoolT< sizeof(XMLElement) > elementPool;
|
| 682 | MemPoolT< sizeof(XMLAttribute) > attributePool;
|
| 683 | MemPoolT< sizeof(XMLText) > textPool;
|
| 684 | MemPoolT< sizeof(XMLComment) > commentPool;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 685 | };
|
| 686 |
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 687 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 688 | class XMLStreamer : public XMLVisitor
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 689 | {
|
| 690 | public:
|
| 691 | XMLStreamer( FILE* file );
|
| 692 | ~XMLStreamer() {}
|
| 693 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 694 | void OpenElement( const char* name );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 695 | void PushAttribute( const char* name, const char* value );
|
| 696 | void CloseElement();
|
| 697 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 698 | void PushText( const char* text, bool cdata=false );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 699 | void PushComment( const char* comment );
|
| 700 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 701 | virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { return true; }
|
| 702 | virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; }
|
| 703 |
|
| 704 | virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
|
| 705 | virtual bool VisitExit( const XMLElement& element );
|
| 706 |
|
| 707 | virtual bool Visit( const XMLText& text );
|
| 708 | virtual bool Visit( const XMLComment& comment );
|
| 709 |
|
| 710 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 711 | private:
|
| 712 | void SealElement();
|
| 713 | void PrintSpace( int depth );
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 714 | void PrintString( const char* ); // prints out, after detecting entities.
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 715 |
|
| 716 | FILE* fp;
|
| 717 | int depth;
|
| 718 | bool elementJustOpened;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 719 | int textDepth;
|
| 720 |
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 721 | enum {
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 722 | ENTITY_RANGE = 64
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 723 | };
|
| 724 | bool entityFlag[ENTITY_RANGE];
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 725 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 726 | DynArray< const char*, 10 > stack;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 727 | };
|
| 728 |
|
| 729 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 730 | }; // tinyxml2
|
| 731 |
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 732 |
|
| 733 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 734 | #endif // TINYXML2_INCLUDED |