Lee Thomason (grinliz) | 2812986 | 2012-02-25 21:11:20 -0800 | [diff] [blame] | 1 | /*
|
| 2 | Original code by Lee Thomason (www.grinninglizard.com)
|
| 3 |
|
| 4 | This software is provided 'as-is', without any express or implied
|
| 5 | warranty. In no event will the authors be held liable for any
|
| 6 | damages arising from the use of this software.
|
| 7 |
|
| 8 | Permission is granted to anyone to use this software for any
|
| 9 | purpose, including commercial applications, and to alter it and
|
| 10 | redistribute it freely, subject to the following restrictions:
|
| 11 |
|
| 12 | 1. The origin of this software must not be misrepresented; you must
|
| 13 | not claim that you wrote the original software. If you use this
|
| 14 | software in a product, an acknowledgment in the product documentation
|
| 15 | would be appreciated but is not required.
|
| 16 |
|
| 17 | 2. Altered source versions must be plainly marked as such, and
|
| 18 | must not be misrepresented as being the original software.
|
| 19 |
|
| 20 | 3. This notice may not be removed or altered from any source
|
| 21 | distribution.
|
| 22 | */
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 23 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 24 | #ifndef TINYXML2_INCLUDED
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 25 | #define TINYXML2_INCLUDED
|
| 26 |
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 27 | #if 1
|
| 28 | #include <cctype>
|
| 29 | #include <climits>
|
| 30 | #include <cstdio>
|
| 31 | #include <cstring>
|
| 32 | #else
|
| 33 | // Not completely sure all the interesting systems
|
| 34 | // can handle the new headers; can switch this if
|
| 35 | // there is an include problem.
|
| 36 | #include <limits.h>
|
| 37 | #include <ctype.h>
|
| 38 | #include <stdio.h>
|
| 39 | #include <memory.h> // Needed by mac.
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 40 | #endif
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 41 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame^] | 42 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 43 | /*
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 44 | TODO: add 'lastAttribute' for faster parsing.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 45 | TODO: intern strings instead of allocation.
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 46 | */
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 47 | /*
|
| 48 | gcc: g++ -Wall tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
|
| 49 | */
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 50 |
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 51 | #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
|
| 52 | #ifndef DEBUG
|
| 53 | #define DEBUG
|
| 54 | #endif
|
| 55 | #endif
|
| 56 |
|
| 57 |
|
| 58 | #if defined(DEBUG)
|
| 59 | #if defined(_MSC_VER)
|
Guillermo A. Amaral | 68b0c87 | 2012-03-24 11:07:19 -0700 | [diff] [blame] | 60 | #define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak()
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 61 | #elif defined (ANDROID_NDK)
|
| 62 | #include <android/log.h>
|
| 63 | #define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
|
| 64 | #else
|
| 65 | #include <assert.h>
|
| 66 | #define TIXMLASSERT assert
|
| 67 | #endif
|
| 68 | #else
|
| 69 | #define TIXMLASSERT( x ) {}
|
| 70 | #endif
|
| 71 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 72 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 73 | // Deprecated library function hell. Compilers want to use the
|
| 74 | // new safe versions. This probably doesn't fully address the problem,
|
| 75 | // but it gets closer. There are too many compilers for me to fully
|
| 76 | // test. If you get compilation troubles, undefine TIXML_SAFE
|
| 77 |
|
| 78 | #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
|
| 79 | // Microsoft visual studio, version 2005 and higher.
|
| 80 | #define TIXML_SNPRINTF _snprintf_s
|
| 81 | #define TIXML_SSCANF sscanf_s
|
| 82 | #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
|
| 83 | // Microsoft visual studio, version 6 and higher.
|
| 84 | //#pragma message( "Using _sn* functions." )
|
| 85 | #define TIXML_SNPRINTF _snprintf
|
| 86 | #define TIXML_SSCANF sscanf
|
| 87 | #elif defined(__GNUC__) && (__GNUC__ >= 3 )
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 88 | // GCC version 3 and higher
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 89 | //#warning( "Using sn* functions." )
|
| 90 | #define TIXML_SNPRINTF snprintf
|
| 91 | #define TIXML_SSCANF sscanf
|
| 92 | #else
|
| 93 | #define TIXML_SNPRINTF snprintf
|
| 94 | #define TIXML_SSCANF sscanf
|
| 95 | #endif
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 96 |
|
Lee Thomason | 7f7b162 | 2012-03-24 12:49:03 -0700 | [diff] [blame] | 97 | static const int TIXML2_MAJOR_VERSION = 0;
|
| 98 | static const int TIXML2_MINOR_VERSION = 9;
|
Lee Thomason | e923c5c | 2012-03-24 13:07:06 -0700 | [diff] [blame] | 99 | static const int TIXML2_PATCH_VERSION = 2;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 100 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 101 | namespace tinyxml2
|
| 102 | {
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 103 | class XMLDocument;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 104 | class XMLElement;
|
| 105 | class XMLAttribute;
|
| 106 | class XMLComment;
|
| 107 | class XMLNode;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 108 | class XMLText;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 109 | class XMLDeclaration;
|
| 110 | class XMLUnknown;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 111 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 112 | class XMLPrinter;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 113 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 114 | /*
|
| 115 | A class that wraps strings. Normally stores the start and end
|
| 116 | pointers into the XML file itself, and will apply normalization
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 117 | and entity translation if actually read. Can also store (and memory
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 118 | manage) a traditional char[]
|
| 119 | */
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 120 | class StrPair
|
| 121 | {
|
Lee Thomason | d34f52c | 2012-01-20 12:55:24 -0800 | [diff] [blame] | 122 | public:
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 123 | enum {
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 124 | NEEDS_ENTITY_PROCESSING = 0x01,
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 125 | NEEDS_NEWLINE_NORMALIZATION = 0x02,
|
| 126 |
|
| 127 | TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 128 | TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 129 | ATTRIBUTE_NAME = 0,
|
| 130 | ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 131 | ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
|
Guillermo A. Amaral | b42ba36 | 2012-03-20 00:15:30 -0700 | [diff] [blame] | 132 | COMMENT = NEEDS_NEWLINE_NORMALIZATION
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 133 | };
|
| 134 |
|
| 135 | StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 136 | ~StrPair();
|
| 137 |
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 138 | void Set( char* _start, char* _end, int _flags ) {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 139 | Reset();
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 140 | this->start = _start; this->end = _end; this->flags = _flags | NEEDS_FLUSH;
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 141 | }
|
| 142 | const char* GetStr();
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 143 | bool Empty() const { return start == end; }
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 144 |
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 145 | void SetInternedStr( const char* str ) { Reset(); this->start = const_cast<char*>(str); }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 146 | void SetStr( const char* str, int flags=0 );
|
| 147 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 148 | char* ParseText( char* in, const char* endTag, int strFlags );
|
| 149 | char* ParseName( char* in );
|
| 150 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 151 |
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 152 | private:
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 153 | void Reset();
|
| 154 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 155 | enum {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 156 | NEEDS_FLUSH = 0x100,
|
| 157 | NEEDS_DELETE = 0x200
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 158 | };
|
| 159 |
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 160 | // After parsing, if *end != 0, it can be set to zero.
|
| 161 | int flags;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 162 | char* start;
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 163 | char* end;
|
| 164 | };
|
| 165 |
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 166 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 167 | /*
|
| 168 | A dynamic array of Plain Old Data. Doesn't support constructors, etc.
|
| 169 | Has a small initial memory pool, so that low or no usage will not
|
| 170 | cause a call to new/delete
|
| 171 | */
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 172 | template <class T, int INIT>
|
| 173 | class DynArray
|
| 174 | {
|
| 175 | public:
|
| 176 | DynArray< T, INIT >()
|
| 177 | {
|
| 178 | mem = pool;
|
| 179 | allocated = INIT;
|
| 180 | size = 0;
|
| 181 | }
|
| 182 | ~DynArray()
|
| 183 | {
|
| 184 | if ( mem != pool ) {
|
| 185 | delete mem;
|
| 186 | }
|
| 187 | }
|
| 188 | void Push( T t )
|
| 189 | {
|
| 190 | EnsureCapacity( size+1 );
|
| 191 | mem[size++] = t;
|
| 192 | }
|
| 193 |
|
| 194 | T* PushArr( int count )
|
| 195 | {
|
| 196 | EnsureCapacity( size+count );
|
| 197 | T* ret = &mem[size];
|
| 198 | size += count;
|
| 199 | return ret;
|
| 200 | }
|
| 201 | T Pop() {
|
| 202 | return mem[--size];
|
| 203 | }
|
| 204 | void PopArr( int count )
|
| 205 | {
|
| 206 | TIXMLASSERT( size >= count );
|
| 207 | size -= count;
|
| 208 | }
|
| 209 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 210 | bool Empty() const { return size == 0; }
|
| 211 | T& operator[](int i) { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; }
|
| 212 | const T& operator[](int i) const { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; }
|
| 213 | int Size() const { return size; }
|
| 214 | int Capacity() const { return allocated; }
|
| 215 | const T* Mem() const { return mem; }
|
| 216 | T* Mem() { return mem; }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 217 |
|
| 218 |
|
| 219 | private:
|
| 220 | void EnsureCapacity( int cap ) {
|
| 221 | if ( cap > allocated ) {
|
| 222 | int newAllocated = cap * 2;
|
| 223 | T* newMem = new T[newAllocated];
|
| 224 | memcpy( newMem, mem, sizeof(T)*size ); // warning: not using constructors, only works for PODs
|
| 225 | if ( mem != pool ) delete [] mem;
|
| 226 | mem = newMem;
|
| 227 | allocated = newAllocated;
|
| 228 | }
|
| 229 | }
|
| 230 |
|
| 231 | T* mem;
|
| 232 | T pool[INIT];
|
| 233 | int allocated; // objects allocated
|
| 234 | int size; // number objects in use
|
| 235 | };
|
| 236 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 237 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 238 | /*
|
| 239 | Parent virtual class a a pool for fast allocation
|
| 240 | and deallocation of objects.
|
| 241 | */
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 242 | class MemPool
|
| 243 | {
|
| 244 | public:
|
| 245 | MemPool() {}
|
| 246 | virtual ~MemPool() {}
|
| 247 |
|
| 248 | virtual int ItemSize() const = 0;
|
| 249 | virtual void* Alloc() = 0;
|
| 250 | virtual void Free( void* ) = 0;
|
| 251 | };
|
| 252 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 253 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 254 | /*
|
| 255 | Template child class to create pools of the correct type.
|
| 256 | */
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 257 | template< int SIZE >
|
| 258 | class MemPoolT : public MemPool
|
| 259 | {
|
| 260 | public:
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 261 | MemPoolT() : root(0), currentAllocs(0), nAllocs(0), maxAllocs(0) {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 262 | ~MemPoolT() {
|
| 263 | // Delete the blocks.
|
| 264 | for( int i=0; i<blockPtrs.Size(); ++i ) {
|
| 265 | delete blockPtrs[i];
|
| 266 | }
|
| 267 | }
|
| 268 |
|
| 269 | virtual int ItemSize() const { return SIZE; }
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 270 | int CurrentAllocs() const { return currentAllocs; }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 271 |
|
| 272 | virtual void* Alloc() {
|
| 273 | if ( !root ) {
|
| 274 | // Need a new block.
|
| 275 | Block* block = new Block();
|
| 276 | blockPtrs.Push( block );
|
| 277 |
|
| 278 | for( int i=0; i<COUNT-1; ++i ) {
|
| 279 | block->chunk[i].next = &block->chunk[i+1];
|
| 280 | }
|
| 281 | block->chunk[COUNT-1].next = 0;
|
| 282 | root = block->chunk;
|
| 283 | }
|
| 284 | void* result = root;
|
| 285 | root = root->next;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 286 |
|
| 287 | ++currentAllocs;
|
| 288 | if ( currentAllocs > maxAllocs ) maxAllocs = currentAllocs;
|
| 289 | nAllocs++;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 290 | return result;
|
| 291 | }
|
| 292 | virtual void Free( void* mem ) {
|
| 293 | if ( !mem ) return;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 294 | --currentAllocs;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 295 | Chunk* chunk = (Chunk*)mem;
|
| 296 | memset( chunk, 0xfe, sizeof(Chunk) );
|
| 297 | chunk->next = root;
|
| 298 | root = chunk;
|
| 299 | }
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 300 | void Trace( const char* name ) {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 301 | printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
|
| 302 | name, maxAllocs, maxAllocs*SIZE/1024, currentAllocs, SIZE, nAllocs, blockPtrs.Size() );
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 303 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 304 |
|
| 305 | private:
|
| 306 | enum { COUNT = 1024/SIZE };
|
| 307 | union Chunk {
|
| 308 | Chunk* next;
|
| 309 | char mem[SIZE];
|
| 310 | };
|
| 311 | struct Block {
|
| 312 | Chunk chunk[COUNT];
|
| 313 | };
|
| 314 | DynArray< Block*, 10 > blockPtrs;
|
| 315 | Chunk* root;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 316 |
|
| 317 | int currentAllocs;
|
| 318 | int nAllocs;
|
| 319 | int maxAllocs;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 320 | };
|
| 321 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 322 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 323 |
|
| 324 | /**
|
| 325 | Implements the interface to the "Visitor pattern" (see the Accept() method.)
|
| 326 | If you call the Accept() method, it requires being passed a XMLVisitor
|
| 327 | class to handle callbacks. For nodes that contain other nodes (Document, Element)
|
| 328 | you will get called with a VisitEnter/VisitExit pair. Nodes that are always leaves
|
| 329 | are simply called with Visit().
|
| 330 |
|
| 331 | If you return 'true' from a Visit method, recursive parsing will continue. If you return
|
| 332 | false, <b>no children of this node or its sibilings</b> will be Visited.
|
| 333 |
|
| 334 | All flavors of Visit methods have a default implementation that returns 'true' (continue
|
| 335 | visiting). You need to only override methods that are interesting to you.
|
| 336 |
|
| 337 | Generally Accept() is called on the TiXmlDocument, although all nodes suppert Visiting.
|
| 338 |
|
| 339 | You should never change the document from a callback.
|
| 340 |
|
| 341 | @sa XMLNode::Accept()
|
| 342 | */
|
| 343 | class XMLVisitor
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 344 | {
|
| 345 | public:
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 346 | virtual ~XMLVisitor() {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 347 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 348 | /// Visit a document.
|
| 349 | virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { return true; }
|
| 350 | /// Visit a document.
|
| 351 | virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; }
|
| 352 |
|
| 353 | /// Visit an element.
|
| 354 | virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) { return true; }
|
| 355 | /// Visit an element.
|
| 356 | virtual bool VisitExit( const XMLElement& /*element*/ ) { return true; }
|
| 357 |
|
| 358 | /// Visit a declaration
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 359 | virtual bool Visit( const XMLDeclaration& /*declaration*/ ) { return true; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 360 | /// Visit a text node
|
| 361 | virtual bool Visit( const XMLText& /*text*/ ) { return true; }
|
| 362 | /// Visit a comment node
|
| 363 | virtual bool Visit( const XMLComment& /*comment*/ ) { return true; }
|
| 364 | /// Visit an unknown node
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 365 | virtual bool Visit( const XMLUnknown& /*unknown*/ ) { return true; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 366 | };
|
| 367 |
|
| 368 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 369 | /*
|
| 370 | Utility functionality.
|
| 371 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 372 | class XMLUtil
|
| 373 | {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 374 | public:
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 375 | // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
|
| 376 | // correct, but simple, and usually works.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 377 | static const char* SkipWhiteSpace( const char* p ) { while( !IsUTF8Continuation(*p) && isspace( *p ) ) { ++p; } return p; }
|
| 378 | 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] | 379 |
|
| 380 | inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
|
| 381 | int n = 0;
|
Lee Thomason | d34f52c | 2012-01-20 12:55:24 -0800 | [diff] [blame] | 382 | if ( p == q ) {
|
| 383 | return true;
|
| 384 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 385 | while( *p && *q && *p == *q && n<nChar ) {
|
| 386 | ++p; ++q; ++n;
|
| 387 | }
|
| 388 | if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
|
| 389 | return true;
|
| 390 | }
|
| 391 | return false;
|
| 392 | }
|
Guillermo A. Amaral | b42ba36 | 2012-03-20 00:15:30 -0700 | [diff] [blame] | 393 | inline static int IsUTF8Continuation( const char p ) { return p & 0x80; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 394 | inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte < 128 ) ? isalnum( anyByte ) : 1; }
|
| 395 | inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte < 128 ) ? isalpha( anyByte ) : 1; }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 396 |
|
| 397 | static const char* ReadBOM( const char* p, bool* hasBOM );
|
| 398 | // p is the starting location,
|
| 399 | // the UTF-8 value of the entity will be placed in value, and length filled in.
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 400 | static const char* GetCharacterRef( const char* p, char* value, int* length );
|
| 401 | static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 402 | };
|
| 403 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 404 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 405 | /** XMLNode is a base class for every object that is in the
|
| 406 | XML Document Object Model (DOM), except XMLAttributes.
|
| 407 | Nodes have siblings, a parent, and children which can
|
| 408 | be navigated. A node is always in a XMLDocument.
|
| 409 | The type of a TiXmlNode can be queried, and it can
|
| 410 | be cast to its more defined type.
|
| 411 |
|
| 412 | An XMLDocument allocates memory for all its Nodes.
|
| 413 | When the XMLDocument gets deleted, all its Nodes
|
| 414 | will also be deleted.
|
| 415 |
|
| 416 | @verbatim
|
| 417 | A Document can contain: Element (container or leaf)
|
| 418 | Comment (leaf)
|
| 419 | Unknown (leaf)
|
| 420 | Declaration( leaf )
|
| 421 |
|
| 422 | An Element can contain: Element (container or leaf)
|
| 423 | Text (leaf)
|
| 424 | Attributes (not on tree)
|
| 425 | Comment (leaf)
|
| 426 | Unknown (leaf)
|
| 427 |
|
| 428 | @endverbatim
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 429 | */
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 430 | class XMLNode
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 431 | {
|
| 432 | friend class XMLDocument;
|
| 433 | friend class XMLElement;
|
| 434 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 435 |
|
| 436 | /// Get the XMLDocument that owns this XMLNode.
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 437 | const XMLDocument* GetDocument() const { return document; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 438 | /// Get the XMLDocument that owns this XMLNode.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 439 | XMLDocument* GetDocument() { return document; }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 440 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 441 | virtual XMLElement* ToElement() { return 0; } ///< Safely cast to an Element, or null.
|
| 442 | virtual XMLText* ToText() { return 0; } ///< Safely cast to Text, or null.
|
| 443 | virtual XMLComment* ToComment() { return 0; } ///< Safely cast to a Comment, or null.
|
| 444 | virtual XMLDocument* ToDocument() { return 0; } ///< Safely cast to a Document, or null.
|
| 445 | virtual XMLDeclaration* ToDeclaration() { return 0; } ///< Safely cast to a Declaration, or null.
|
| 446 | virtual XMLUnknown* ToUnknown() { return 0; } ///< Safely cast to an Unknown, or null.
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 447 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 448 | virtual const XMLElement* ToElement() const { return 0; }
|
| 449 | virtual const XMLText* ToText() const { return 0; }
|
| 450 | virtual const XMLComment* ToComment() const { return 0; }
|
| 451 | virtual const XMLDocument* ToDocument() const { return 0; }
|
| 452 | virtual const XMLDeclaration* ToDeclaration() const { return 0; }
|
| 453 | virtual const XMLUnknown* ToUnknown() const { return 0; }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 454 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 455 | /** The meaning of 'value' changes for the specific type.
|
| 456 | @verbatim
|
| 457 | Document: empy
|
| 458 | Element: name of the element
|
| 459 | Comment: the comment text
|
| 460 | Unknown: the tag contents
|
| 461 | Text: the text string
|
| 462 | @endverbatim
|
| 463 | */
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 464 | const char* Value() const { return value.GetStr(); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 465 | /** Set the Value of an XML node.
|
| 466 | @sa Value()
|
| 467 | */
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 468 | void SetValue( const char* val, bool staticMem=false );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 469 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 470 | /// Get the parent of this node on the DOM.
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 471 | const XMLNode* Parent() const { return parent; }
|
| 472 | XMLNode* Parent() { return parent; }
|
| 473 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 474 | /// Returns true if this node has no children.
|
| 475 | bool NoChildren() const { return !firstChild; }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 476 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 477 | /// Get the first child node, or null if none exists.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 478 | const XMLNode* FirstChild() const { return firstChild; }
|
| 479 | XMLNode* FirstChild() { return firstChild; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 480 | /** Get the first child element, or optionally the first child
|
| 481 | element with the specified name.
|
| 482 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 483 | const XMLElement* FirstChildElement( const char* value=0 ) const;
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 484 | 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] | 485 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 486 | /// Get the last child node, or null if none exists.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 487 | const XMLNode* LastChild() const { return lastChild; }
|
| 488 | XMLNode* LastChild() { return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() ); }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 489 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 490 | /** Get the last child element or optionally the last child
|
| 491 | element with the specified name.
|
| 492 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 493 | const XMLElement* LastChildElement( const char* value=0 ) const;
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 494 | XMLElement* LastChildElement( const char* _value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(_value) ); }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 495 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 496 | /// Get the previous (left) sibling node of this node.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 497 | const XMLNode* PreviousSibling() const { return prev; }
|
| 498 | XMLNode* PreviousSibling() { return prev; }
|
| 499 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 500 | /// Get the previous (left) sibling element of this node, with an opitionally supplied name.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 501 | const XMLElement* PreviousSiblingElement( const char* value=0 ) const ;
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 502 | XMLElement* PreviousSiblingElement( const char* _value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( _value ) ); }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 503 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 504 | /// Get the next (right) sibling node of this node.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 505 | const XMLNode* NextSibling() const { return next; }
|
| 506 | XMLNode* NextSibling() { return next; }
|
| 507 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 508 | /// Get the next (right) sibling element of this node, with an opitionally supplied name.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 509 | const XMLElement* NextSiblingElement( const char* value=0 ) const;
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 510 | XMLElement* NextSiblingElement( const char* _value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( _value ) ); }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 511 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 512 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 513 | Add a child node as the last (right) child.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 514 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 515 | XMLNode* InsertEndChild( XMLNode* addThis );
|
Lee Thomason | 618dbf8 | 2012-02-28 12:34:27 -0800 | [diff] [blame] | 516 |
|
| 517 | XMLNode* LinkEndChild( XMLNode* addThis ) { return InsertEndChild( addThis ); }
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 518 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 519 | Add a child node as the first (left) child.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 520 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 521 | XMLNode* InsertFirstChild( XMLNode* addThis );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 522 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 523 | Add a node after the specified child node.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 524 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 525 | XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
|
| 526 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 527 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 528 | Delete all the children of this node.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 529 | */
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 530 | void DeleteChildren();
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 531 |
|
| 532 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 533 | Delete a child of this node.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 534 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 535 | void DeleteChild( XMLNode* node );
|
| 536 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 537 | /**
|
| 538 | Make a copy of this node, but not its children.
|
| 539 | You may pass in a Document pointer that will be
|
| 540 | the owner of the new Node. If the 'document' is
|
| 541 | null, then the node returned will be allocated
|
| 542 | from the current Document. (this->GetDocument())
|
| 543 |
|
| 544 | Note: if called on a XMLDocument, this will return null.
|
| 545 | */
|
| 546 | virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
|
| 547 |
|
| 548 | /**
|
| 549 | Test if 2 nodes are the same, but don't test children.
|
| 550 | The 2 nodes do not need to be in the same Document.
|
| 551 |
|
| 552 | Note: if called on a XMLDocument, this will return false.
|
| 553 | */
|
| 554 | virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
|
| 555 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 556 | /** Accept a hierchical visit the nodes in the TinyXML DOM. Every node in the
|
| 557 | XML tree will be conditionally visited and the host will be called back
|
| 558 | via the TiXmlVisitor interface.
|
| 559 |
|
| 560 | This is essentially a SAX interface for TinyXML. (Note however it doesn't re-parse
|
| 561 | the XML for the callbacks, so the performance of TinyXML is unchanged by using this
|
| 562 | interface versus any other.)
|
| 563 |
|
| 564 | The interface has been based on ideas from:
|
| 565 |
|
| 566 | - http://www.saxproject.org/
|
| 567 | - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
|
| 568 |
|
| 569 | Which are both good references for "visiting".
|
| 570 |
|
| 571 | An example of using Accept():
|
| 572 | @verbatim
|
| 573 | TiXmlPrinter printer;
|
| 574 | tinyxmlDoc.Accept( &printer );
|
| 575 | const char* xmlcstr = printer.CStr();
|
| 576 | @endverbatim
|
| 577 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 578 | virtual bool Accept( XMLVisitor* visitor ) const = 0;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 579 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 580 | // internal
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 581 | virtual char* ParseDeep( char*, StrPair* );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 582 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 583 | protected:
|
| 584 | XMLNode( XMLDocument* );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 585 | virtual ~XMLNode();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 586 | XMLNode( const XMLNode& ); // not supported
|
| 587 | void operator=( const XMLNode& ); // not supported
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 588 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 589 | XMLDocument* document;
|
| 590 | XMLNode* parent;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 591 | mutable StrPair value;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 592 |
|
| 593 | XMLNode* firstChild;
|
| 594 | XMLNode* lastChild;
|
| 595 |
|
| 596 | XMLNode* prev;
|
| 597 | XMLNode* next;
|
| 598 |
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 599 | private:
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 600 | MemPool* memPool;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 601 | void Unlink( XMLNode* child );
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 602 | };
|
| 603 |
|
| 604 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 605 | /** XML text.
|
| 606 |
|
| 607 | Note that a text node can have child element nodes, for example:
|
| 608 | @verbatim
|
| 609 | <root>This is <b>bold</b></root>
|
| 610 | @endverbatim
|
| 611 |
|
| 612 | A text node can have 2 ways to output the next. "normal" output
|
| 613 | and CDATA. It will default to the mode it was parsed from the XML file and
|
| 614 | you generally want to leave it alone, but you can change the output mode with
|
| 615 | SetCDATA() and query it with CDATA().
|
| 616 | */
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 617 | class XMLText : public XMLNode
|
| 618 | {
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 619 | friend class XMLBase;
|
| 620 | friend class XMLDocument;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 621 | public:
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 622 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 623 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 624 | virtual XMLText* ToText() { return this; }
|
| 625 | virtual const XMLText* ToText() const { return this; }
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 626 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 627 | /// Declare whether this should be CDATA or standard text.
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 628 | void SetCData( bool _isCData ) { this->isCData = _isCData; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 629 | /// Returns true if this is a CDATA text element.
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 630 | bool CData() const { return isCData; }
|
| 631 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 632 | char* ParseDeep( char*, StrPair* endTag );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 633 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 634 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
| 635 |
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 636 |
|
| 637 | protected:
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 638 | XMLText( XMLDocument* doc ) : XMLNode( doc ), isCData( false ) {}
|
| 639 | virtual ~XMLText() {}
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 640 | XMLText( const XMLText& ); // not supported
|
| 641 | void operator=( const XMLText& ); // not supported
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 642 |
|
| 643 | private:
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 644 | bool isCData;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 645 | };
|
| 646 |
|
| 647 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 648 | /** An XML Comment. */
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 649 | class XMLComment : public XMLNode
|
| 650 | {
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 651 | friend class XMLDocument;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 652 | public:
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 653 | virtual XMLComment* ToComment() { return this; }
|
| 654 | virtual const XMLComment* ToComment() const { return this; }
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 655 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 656 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 657 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 658 | char* ParseDeep( char*, StrPair* endTag );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 659 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 660 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 661 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 662 | protected:
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 663 | XMLComment( XMLDocument* doc );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 664 | virtual ~XMLComment();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 665 | XMLComment( const XMLComment& ); // not supported
|
| 666 | void operator=( const XMLComment& ); // not supported
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 667 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 668 | private:
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 669 | };
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 670 |
|
| 671 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 672 | /** In correct XML the declaration is the first entry in the file.
|
| 673 | @verbatim
|
| 674 | <?xml version="1.0" standalone="yes"?>
|
| 675 | @endverbatim
|
| 676 |
|
| 677 | TinyXML2 will happily read or write files without a declaration,
|
| 678 | however.
|
| 679 |
|
| 680 | The text of the declaration isn't interpreted. It is parsed
|
| 681 | and written as a string.
|
| 682 | */
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 683 | class XMLDeclaration : public XMLNode
|
| 684 | {
|
| 685 | friend class XMLDocument;
|
| 686 | public:
|
| 687 | virtual XMLDeclaration* ToDeclaration() { return this; }
|
| 688 | virtual const XMLDeclaration* ToDeclaration() const { return this; }
|
| 689 |
|
| 690 | virtual bool Accept( XMLVisitor* visitor ) const;
|
| 691 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 692 | char* ParseDeep( char*, StrPair* endTag );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 693 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 694 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 695 |
|
| 696 | protected:
|
| 697 | XMLDeclaration( XMLDocument* doc );
|
| 698 | virtual ~XMLDeclaration();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 699 | XMLDeclaration( const XMLDeclaration& ); // not supported
|
| 700 | void operator=( const XMLDeclaration& ); // not supported
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 701 | };
|
| 702 |
|
| 703 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 704 | /** Any tag that tinyXml doesn't recognize is saved as an
|
| 705 | unknown. It is a tag of text, but should not be modified.
|
| 706 | It will be written back to the XML, unchanged, when the file
|
| 707 | is saved.
|
| 708 |
|
| 709 | DTD tags get thrown into TiXmlUnknowns.
|
| 710 | */
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 711 | class XMLUnknown : public XMLNode
|
| 712 | {
|
| 713 | friend class XMLDocument;
|
| 714 | public:
|
| 715 | virtual XMLUnknown* ToUnknown() { return this; }
|
| 716 | virtual const XMLUnknown* ToUnknown() const { return this; }
|
| 717 |
|
| 718 | virtual bool Accept( XMLVisitor* visitor ) const;
|
| 719 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 720 | char* ParseDeep( char*, StrPair* endTag );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 721 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 722 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 723 |
|
| 724 | protected:
|
| 725 | XMLUnknown( XMLDocument* doc );
|
| 726 | virtual ~XMLUnknown();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 727 | XMLUnknown( const XMLUnknown& ); // not supported
|
| 728 | void operator=( const XMLUnknown& ); // not supported
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 729 | };
|
| 730 |
|
| 731 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 732 | enum {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 733 | XML_NO_ERROR = 0,
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 734 | XML_SUCCESS = 0,
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 735 |
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 736 | XML_NO_ATTRIBUTE,
|
| 737 | XML_WRONG_ATTRIBUTE_TYPE,
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 738 |
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 739 | XML_ERROR_FILE_NOT_FOUND,
|
Lee Thomason | 7f7b162 | 2012-03-24 12:49:03 -0700 | [diff] [blame] | 740 | XML_ERROR_FILE_COULD_NOT_BE_OPENED,
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 741 | XML_ERROR_ELEMENT_MISMATCH,
|
| 742 | XML_ERROR_PARSING_ELEMENT,
|
| 743 | XML_ERROR_PARSING_ATTRIBUTE,
|
| 744 | XML_ERROR_IDENTIFYING_TAG,
|
| 745 | XML_ERROR_PARSING_TEXT,
|
| 746 | XML_ERROR_PARSING_CDATA,
|
| 747 | XML_ERROR_PARSING_COMMENT,
|
| 748 | XML_ERROR_PARSING_DECLARATION,
|
| 749 | XML_ERROR_PARSING_UNKNOWN,
|
| 750 | XML_ERROR_EMPTY_DOCUMENT,
|
| 751 | XML_ERROR_MISMATCHED_ELEMENT,
|
| 752 | XML_ERROR_PARSING
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 753 | };
|
| 754 |
|
| 755 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 756 | /** An attribute is a name-value pair. Elements have an arbitrary
|
| 757 | number of attributes, each with a unique name.
|
| 758 |
|
| 759 | @note The attributes are not XMLNodes. You may only query the
|
| 760 | Next() attribute in a list.
|
| 761 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 762 | class XMLAttribute
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 763 | {
|
| 764 | friend class XMLElement;
|
| 765 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 766 | const char* Name() const { return name.GetStr(); } ///< The name of the attribute.
|
| 767 | const char* Value() const { return value.GetStr(); } ///< The value of the attribute.
|
| 768 | const XMLAttribute* Next() const { return next; } ///< The next attribute in the list.
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 769 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 770 | /** IntAttribute interprets the attribute as an integer, and returns the value.
|
| 771 | If the value isn't an integer, 0 will be returned. There is no error checking;
|
| 772 | use QueryIntAttribute() if you need error checking.
|
| 773 | */
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 774 | int IntValue() const { int i=0; QueryIntValue( &i ); return i; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 775 | /// Query as an unsigned integer. See IntAttribute()
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 776 | unsigned UnsignedValue() const { unsigned i=0; QueryUnsignedValue( &i ); return i; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 777 | /// Query as a boolean. See IntAttribute()
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 778 | bool BoolValue() const { bool b=false; QueryBoolValue( &b ); return b; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 779 | /// Query as a double. See IntAttribute()
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 780 | double DoubleValue() const { double d=0; QueryDoubleValue( &d ); return d; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 781 | /// Query as a float. See IntAttribute()
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 782 | float FloatValue() const { float f=0; QueryFloatValue( &f ); return f; }
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 783 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 784 | /** QueryIntAttribute interprets the attribute as an integer, and returns the value
|
| 785 | in the provided paremeter. The function will return XML_NO_ERROR on success,
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 786 | and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 787 | */
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 788 | int QueryIntValue( int* value ) const;
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 789 | /// See QueryIntAttribute
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 790 | int QueryUnsignedValue( unsigned int* value ) const;
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 791 | /// See QueryIntAttribute
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 792 | int QueryBoolValue( bool* value ) const;
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 793 | /// See QueryIntAttribute
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 794 | int QueryDoubleValue( double* value ) const;
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 795 | /// See QueryIntAttribute
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 796 | int QueryFloatValue( float* value ) const;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 797 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 798 | /// Set the attribute to a string value.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 799 | void SetAttribute( const char* value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 800 | /// Set the attribute to value.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 801 | void SetAttribute( int value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 802 | /// Set the attribute to value.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 803 | void SetAttribute( unsigned value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 804 | /// Set the attribute to value.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 805 | void SetAttribute( bool value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 806 | /// Set the attribute to value.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 807 | void SetAttribute( double value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 808 | /// Set the attribute to value.
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 809 | void SetAttribute( float value );
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 810 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 811 | private:
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 812 | enum { BUF_SIZE = 200 };
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 813 |
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 814 | XMLAttribute() : next( 0 ) {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 815 | virtual ~XMLAttribute() {}
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 816 | XMLAttribute( const XMLAttribute& ); // not supported
|
| 817 | void operator=( const XMLAttribute& ); // not supported
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 818 | void SetName( const char* name );
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 819 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 820 | char* ParseDeep( char* p, bool processEntities );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 821 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 822 | mutable StrPair name;
|
| 823 | mutable StrPair value;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 824 | XMLAttribute* next;
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 825 | MemPool* memPool;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 826 | };
|
| 827 |
|
| 828 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 829 | /** The element is a container class. It has a value, the element name,
|
| 830 | and can contain other elements, text, comments, and unknowns.
|
| 831 | Elements also contain an arbitrary number of attributes.
|
| 832 | */
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 833 | class XMLElement : public XMLNode
|
| 834 | {
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 835 | friend class XMLBase;
|
| 836 | friend class XMLDocument;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 837 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 838 | /// Get the name of an element (which is the Value() of the node.)
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 839 | const char* Name() const { return Value(); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 840 | /// Set the name of the element.
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 841 | void SetName( const char* str, bool staticMem=false ) { SetValue( str, staticMem ); }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 842 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 843 | virtual XMLElement* ToElement() { return this; }
|
| 844 | virtual const XMLElement* ToElement() const { return this; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 845 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 846 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 847 | /** Given an attribute name, Attribute() returns the value
|
Lee Thomason | 9225815 | 2012-03-24 13:05:39 -0700 | [diff] [blame] | 848 | for the attribute of that name, or null if none
|
| 849 | exists. For example:
|
| 850 |
|
| 851 | @verbatim
|
| 852 | const char* value = ele->Attribute( "foo" );
|
| 853 | @endverbatim
|
| 854 |
|
| 855 | The 'value' parameter is normally null. However, if specified,
|
| 856 | the attribute will only be returned if the 'name' and 'value'
|
| 857 | match. This allow you to write code:
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 858 |
|
| 859 | @verbatim
|
| 860 | if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
|
| 861 | @endverbatim
|
| 862 |
|
| 863 | rather than:
|
| 864 | @verbatim
|
| 865 | if ( ele->Attribute( "foo" ) ) {
|
| 866 | if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
|
| 867 | }
|
| 868 | @endverbatim
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 869 | */
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 870 | const char* Attribute( const char* name, const char* value=0 ) const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 871 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 872 | /** Given an attribute name, IntAttribute() returns the value
|
| 873 | of the attribute interpreted as an integer. 0 will be
|
| 874 | returned if there is an error. For a method with error
|
| 875 | checking, see QueryIntAttribute()
|
| 876 | */
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 877 | int IntAttribute( const char* name ) const { int i=0; QueryIntAttribute( name, &i ); return i; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 878 | /// See IntAttribute()
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 879 | unsigned UnsignedAttribute( const char* name ) const{ unsigned i=0; QueryUnsignedAttribute( name, &i ); return i; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 880 | /// See IntAttribute()
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 881 | bool BoolAttribute( const char* name ) const { bool b=false; QueryBoolAttribute( name, &b ); return b; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 882 | /// See IntAttribute()
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 883 | double DoubleAttribute( const char* name ) const { double d=0; QueryDoubleAttribute( name, &d ); return d; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 884 | /// See IntAttribute()
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 885 | float FloatAttribute( const char* name ) const { float f=0; QueryFloatAttribute( name, &f ); return f; }
|
| 886 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 887 | /** Given an attribute name, QueryIntAttribute() returns
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 888 | XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
|
| 889 | can't be performed, or XML_NO_ATTRIBUTE if the attribute
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 890 | doesn't exist. If successful, the result of the conversion
|
| 891 | will be written to 'value'. If not successful, nothing will
|
| 892 | be written to 'value'. This allows you to provide default
|
| 893 | value:
|
| 894 |
|
| 895 | @verbatim
|
| 896 | int value = 10;
|
| 897 | QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
|
| 898 | @endverbatim
|
| 899 | */
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 900 | int QueryIntAttribute( const char* name, int* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryIntValue( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 901 | /// See QueryIntAttribute()
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 902 | int QueryUnsignedAttribute( const char* name, unsigned int* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryUnsignedValue( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 903 | /// See QueryIntAttribute()
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 904 | int QueryBoolAttribute( const char* name, bool* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryBoolValue( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 905 | /// See QueryIntAttribute()
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 906 | int QueryDoubleAttribute( const char* name, double* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryDoubleValue( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 907 | /// See QueryIntAttribute()
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 908 | int QueryFloatAttribute( const char* name, float* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryFloatValue( _value ); }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 909 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 910 | /// Sets the named attribute to value.
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 911 | void SetAttribute( const char* name, const char* _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 912 | /// Sets the named attribute to value.
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 913 | void SetAttribute( const char* name, int _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 914 | /// Sets the named attribute to value.
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 915 | void SetAttribute( const char* name, unsigned _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 916 | /// Sets the named attribute to value.
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 917 | void SetAttribute( const char* name, bool _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 918 | /// Sets the named attribute to value.
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 919 | void SetAttribute( const char* name, double _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 920 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 921 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 922 | Delete an attribute.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 923 | */
|
| 924 | void DeleteAttribute( const char* name );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 925 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 926 | /// Return the first attribute in the list.
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 927 | const XMLAttribute* FirstAttribute() const { return rootAttribute; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 928 | /// Query a specific attribute in the list.
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 929 | const XMLAttribute* FindAttribute( const char* name ) const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 930 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 931 | /** Convenience function for easy access to the text inside an element. Although easy
|
| 932 | and concise, GetText() is limited compared to getting the TiXmlText child
|
| 933 | and accessing it directly.
|
| 934 |
|
| 935 | If the first child of 'this' is a TiXmlText, the GetText()
|
| 936 | returns the character string of the Text node, else null is returned.
|
| 937 |
|
| 938 | This is a convenient method for getting the text of simple contained text:
|
| 939 | @verbatim
|
| 940 | <foo>This is text</foo>
|
| 941 | const char* str = fooElement->GetText();
|
| 942 | @endverbatim
|
| 943 |
|
| 944 | 'str' will be a pointer to "This is text".
|
| 945 |
|
| 946 | Note that this function can be misleading. If the element foo was created from
|
| 947 | this XML:
|
| 948 | @verbatim
|
| 949 | <foo><b>This is text</b></foo>
|
| 950 | @endverbatim
|
| 951 |
|
| 952 | then the value of str would be null. The first child node isn't a text node, it is
|
| 953 | another element. From this XML:
|
| 954 | @verbatim
|
| 955 | <foo>This is <b>text</b></foo>
|
| 956 | @endverbatim
|
| 957 | GetText() will return "This is ".
|
| 958 | */
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 959 | const char* GetText() const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 960 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 961 | // internal:
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 962 | enum {
|
| 963 | OPEN, // <foo>
|
| 964 | CLOSED, // <foo/>
|
| 965 | CLOSING // </foo>
|
| 966 | };
|
| 967 | int ClosingType() const { return closingType; }
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 968 | char* ParseDeep( char* p, StrPair* endTag );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 969 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 970 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 971 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 972 | private:
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 973 | XMLElement( XMLDocument* doc );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 974 | virtual ~XMLElement();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 975 | XMLElement( const XMLElement& ); // not supported
|
| 976 | void operator=( const XMLElement& ); // not supported
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 977 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 978 | XMLAttribute* FindAttribute( const char* name );
|
| 979 | XMLAttribute* FindOrCreateAttribute( const char* name );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 980 | void LinkAttribute( XMLAttribute* attrib );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 981 | char* ParseAttributes( char* p );
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 982 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 983 | int closingType;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 984 | XMLAttribute* rootAttribute;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 985 | };
|
| 986 |
|
| 987 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 988 | /** A document binds together all the functionality.
|
| 989 | It can be saved, loaded, and printed to the screen.
|
| 990 | All Nodes are connected and allocated to a Document.
|
| 991 | If the Document is deleted, all its Nodes are also deleted.
|
| 992 | */
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 993 | class XMLDocument : public XMLNode
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 994 | {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 995 | friend class XMLElement;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 996 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 997 | /// constructor
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 998 | XMLDocument( bool processEntities = true );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 999 | ~XMLDocument();
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1000 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1001 | virtual XMLDocument* ToDocument() { return this; }
|
| 1002 | virtual const XMLDocument* ToDocument() const { return this; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1003 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1004 | /**
|
| 1005 | Parse an XML file from a character string.
|
| 1006 | Returns XML_NO_ERROR (0) on success, or
|
| 1007 | an errorID.
|
| 1008 | */
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1009 | int Parse( const char* xml );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1010 | /**
|
| 1011 | Load an XML file from disk.
|
| 1012 | Returns XML_NO_ERROR (0) on success, or
|
| 1013 | an errorID.
|
| 1014 | */
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1015 | int LoadFile( const char* filename );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1016 | /**
|
| 1017 | Load an XML file from disk. You are responsible
|
| 1018 | for providing and closing the FILE*.
|
| 1019 |
|
| 1020 | Returns XML_NO_ERROR (0) on success, or
|
| 1021 | an errorID.
|
| 1022 | */
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1023 | int LoadFile( FILE* );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1024 | /**
|
| 1025 | Save the XML file to disk.
|
| 1026 | */
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1027 | void SaveFile( const char* filename );
|
| 1028 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1029 | bool ProcessEntities() const { return processEntities; }
|
| 1030 |
|
| 1031 | /**
|
| 1032 | Returns true if this document has a leading Byte Order Mark of UTF8.
|
| 1033 | */
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1034 | bool HasBOM() const { return writeBOM; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1035 |
|
| 1036 | /** Return the root element of DOM. Equivalent to FirstChildElement().
|
| 1037 | To get the first node, use FirstChild().
|
| 1038 | */
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1039 | XMLElement* RootElement() { return FirstChildElement(); }
|
| 1040 | const XMLElement* RootElement() const { return FirstChildElement(); }
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1041 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1042 | /** Print the Document. If the Printer is not provided, it will
|
| 1043 | print to stdout. If you provide Printer, this can print to a file:
|
| 1044 | @verbatim
|
| 1045 | XMLPrinter printer( fp );
|
| 1046 | doc.Print( &printer );
|
| 1047 | @endverbatim
|
| 1048 |
|
| 1049 | Or you can use a printer to print to memory:
|
| 1050 | @verbatim
|
| 1051 | XMLPrinter printer;
|
| 1052 | doc->Print( &printer );
|
Lee Thomason (grinliz) | 2812986 | 2012-02-25 21:11:20 -0800 | [diff] [blame] | 1053 | // printer.CStr() has a const char* to the XML
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1054 | @endverbatim
|
| 1055 | */
|
| 1056 | void Print( XMLPrinter* streamer=0 );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1057 | virtual bool Accept( XMLVisitor* visitor ) const;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1058 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1059 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1060 | Create a new Element associated with
|
| 1061 | this Document. The memory for the Element
|
| 1062 | is managed by the Document.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1063 | */
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1064 | XMLElement* NewElement( const char* name );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1065 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1066 | Create a new Comment associated with
|
| 1067 | this Document. The memory for the Comment
|
| 1068 | is managed by the Document.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1069 | */
|
| 1070 | XMLComment* NewComment( const char* comment );
|
| 1071 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1072 | Create a new Text associated with
|
| 1073 | this Document. The memory for the Text
|
| 1074 | is managed by the Document.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1075 | */
|
| 1076 | XMLText* NewText( const char* text );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1077 | /**
|
| 1078 | Create a new Declaration associated with
|
| 1079 | this Document. The memory for the object
|
| 1080 | is managed by the Document.
|
| 1081 | */
|
| 1082 | XMLDeclaration* NewDeclaration( const char* text );
|
| 1083 | /**
|
| 1084 | Create a new Unknown associated with
|
| 1085 | this Document. The memory for the object
|
| 1086 | is managed by the Document.
|
| 1087 | */
|
| 1088 | XMLUnknown* NewUnknown( const char* text );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1089 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1090 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1091 | Delete a node associated with this documented.
|
| 1092 | It will be unlinked from the DOM.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1093 | */
|
| 1094 | void DeleteNode( XMLNode* node ) { node->parent->DeleteChild( node ); }
|
| 1095 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1096 | void SetError( int error, const char* str1, const char* str2 );
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1097 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1098 | /// Return true if there was an error parsing the document.
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1099 | bool Error() const { return errorID != XML_NO_ERROR; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1100 | /// Return the errorID.
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1101 | int ErrorID() const { return errorID; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1102 | /// Return a possibly helpful diagnostic location or string.
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1103 | const char* GetErrorStr1() const { return errorStr1; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1104 | /// Return possibly helpful secondary diagnostic location or string.
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1105 | const char* GetErrorStr2() const { return errorStr2; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1106 | /// If there is an error, print it to stdout
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1107 | void PrintError() const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1108 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1109 | // internal
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1110 | char* Identify( char* p, XMLNode** node );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1111 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1112 | virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const { return 0; }
|
| 1113 | virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const { return false; }
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1114 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1115 | private:
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 1116 | XMLDocument( const XMLDocument& ); // not supported
|
| 1117 | void operator=( const XMLDocument& ); // not supported
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1118 | void InitDocument();
|
| 1119 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1120 | bool writeBOM;
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1121 | bool processEntities;
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 1122 | int errorID;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1123 | const char* errorStr1;
|
| 1124 | const char* errorStr2;
|
| 1125 | char* charBuffer;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1126 |
|
| 1127 | MemPoolT< sizeof(XMLElement) > elementPool;
|
| 1128 | MemPoolT< sizeof(XMLAttribute) > attributePool;
|
| 1129 | MemPoolT< sizeof(XMLText) > textPool;
|
| 1130 | MemPoolT< sizeof(XMLComment) > commentPool;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1131 | };
|
| 1132 |
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 1133 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1134 |
|
| 1135 | /**
|
| 1136 | Printing functionality. The XMLPrinter gives you more
|
| 1137 | options than the XMLDocument::Print() method.
|
| 1138 |
|
| 1139 | It can:
|
| 1140 | -# Print to memory.
|
| 1141 | -# Print to a file you provide
|
| 1142 | -# Print XML without a XMLDocument.
|
| 1143 |
|
| 1144 | Print to Memory
|
| 1145 |
|
| 1146 | @verbatim
|
| 1147 | XMLPrinter printer;
|
| 1148 | doc->Print( &printer );
|
| 1149 | SomeFunctior( printer.CStr() );
|
| 1150 | @endverbatim
|
| 1151 |
|
| 1152 | Print to a File
|
| 1153 |
|
| 1154 | You provide the file pointer.
|
| 1155 | @verbatim
|
| 1156 | XMLPrinter printer( fp );
|
| 1157 | doc.Print( &printer );
|
| 1158 | @endverbatim
|
| 1159 |
|
| 1160 | Print without a XMLDocument
|
| 1161 |
|
| 1162 | When loading, an XML parser is very useful. However, sometimes
|
| 1163 | when saving, it just gets in the way. The code is often set up
|
| 1164 | for streaming, and constructing the DOM is just overhead.
|
| 1165 |
|
| 1166 | The Printer supports the streaming case. The following code
|
| 1167 | prints out a trivially simple XML file without ever creating
|
| 1168 | an XML document.
|
| 1169 |
|
| 1170 | @verbatim
|
| 1171 | XMLPrinter printer( fp );
|
| 1172 | printer.OpenElement( "foo" );
|
| 1173 | printer.PushAttribute( "foo", "bar" );
|
| 1174 | printer.CloseElement();
|
| 1175 | @endverbatim
|
| 1176 | */
|
| 1177 | class XMLPrinter : public XMLVisitor
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1178 | {
|
| 1179 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1180 | /** Construct the printer. If the FILE* is specified,
|
| 1181 | this will print to the FILE. Else it will print
|
| 1182 | to memory, and the result is available in CStr()
|
| 1183 | */
|
| 1184 | XMLPrinter( FILE* file=0 );
|
| 1185 | ~XMLPrinter() {}
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1186 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1187 | /** If streaming, write the BOM and declaration. */
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1188 | void PushHeader( bool writeBOM, bool writeDeclaration );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1189 | /** If streaming, start writing an element.
|
| 1190 | The element must be closed with CloseElement()
|
| 1191 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1192 | void OpenElement( const char* name );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1193 | /// If streaming, add an attribute to an open element.
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1194 | void PushAttribute( const char* name, const char* value );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1195 | void PushAttribute( const char* name, int value );
|
| 1196 | void PushAttribute( const char* name, unsigned value );
|
| 1197 | void PushAttribute( const char* name, bool value );
|
| 1198 | void PushAttribute( const char* name, double value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1199 | /// If streaming, close the Element.
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1200 | void CloseElement();
|
| 1201 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1202 | /// Add a text node.
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1203 | void PushText( const char* text, bool cdata=false );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1204 | /// Add a comment
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1205 | void PushComment( const char* comment );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1206 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1207 | void PushDeclaration( const char* value );
|
| 1208 | void PushUnknown( const char* value );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1209 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1210 | virtual bool VisitEnter( const XMLDocument& /*doc*/ );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1211 | virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; }
|
| 1212 |
|
| 1213 | virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
|
| 1214 | virtual bool VisitExit( const XMLElement& element );
|
| 1215 |
|
| 1216 | virtual bool Visit( const XMLText& text );
|
| 1217 | virtual bool Visit( const XMLComment& comment );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1218 | virtual bool Visit( const XMLDeclaration& declaration );
|
| 1219 | virtual bool Visit( const XMLUnknown& unknown );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1220 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1221 | /**
|
| 1222 | If in print to memory mode, return a pointer to
|
| 1223 | the XML file in memory.
|
| 1224 | */
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1225 | const char* CStr() const { return buffer.Mem(); }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1226 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1227 | private:
|
| 1228 | void SealElement();
|
| 1229 | void PrintSpace( int depth );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1230 | void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1231 | void Print( const char* format, ... );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1232 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1233 | bool elementJustOpened;
|
| 1234 | bool firstElement;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1235 | FILE* fp;
|
| 1236 | int depth;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1237 | int textDepth;
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1238 | bool processEntities;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1239 |
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1240 | enum {
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1241 | ENTITY_RANGE = 64,
|
| 1242 | BUF_SIZE = 200
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1243 | };
|
| 1244 | bool entityFlag[ENTITY_RANGE];
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1245 | bool restrictedEntityFlag[ENTITY_RANGE];
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1246 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1247 | DynArray< const char*, 10 > stack;
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1248 | DynArray< char, 20 > buffer, accumulator;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1249 | };
|
| 1250 |
|
| 1251 |
|
Guillermo A. Amaral | b42ba36 | 2012-03-20 00:15:30 -0700 | [diff] [blame] | 1252 | } // tinyxml2
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1253 |
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1254 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame^] | 1255 | // What follows is the docs for the examples.
|
| 1256 | // I'd like the docs to be just before the
|
| 1257 | // actual examples in xmltest.cpp, but I
|
| 1258 | // can't seem to get doxygen to do that. It
|
| 1259 | // would be a wonderful patch if anyone figures
|
| 1260 | // it out.
|
| 1261 |
|
| 1262 | /** @page Example-1 Load an XML File
|
| 1263 | * @dontinclude ./xmltest.cpp
|
| 1264 | * Basic XML file loading.
|
| 1265 | * The basic syntax to load an XML file from
|
| 1266 | * disk and check for an error. (ErrorID()
|
| 1267 | * will return 0 for no error.)
|
| 1268 | * @skip example_1()
|
| 1269 | * @until }
|
| 1270 | */
|
| 1271 |
|
| 1272 |
|
| 1273 | /** @page Example-2 Parse an XML from char buffer
|
| 1274 | * @dontinclude ./xmltest.cpp
|
| 1275 | * Basic XML string parsing.
|
| 1276 | * The basic syntax to parse an XML for
|
| 1277 | * a char* and check for an error. (ErrorID()
|
| 1278 | * will return 0 for no error.)
|
| 1279 | * @skip example_2()
|
| 1280 | * @until }
|
| 1281 | */
|
| 1282 |
|
| 1283 | /** @page Example-3 Get information out of XML
|
| 1284 | @dontinclude ./xmltest.cpp
|
| 1285 | In this example, we navigate a simple XML
|
| 1286 | file, and read some interesting text. Note
|
| 1287 | that this is examlpe doesn't use error
|
| 1288 | checking; working code should check for null
|
| 1289 | pointers when walking an XML tree, or use
|
| 1290 | XMLHandle.
|
| 1291 |
|
| 1292 | (The XML is an excerpt from "dream.xml").
|
| 1293 |
|
| 1294 | @skip example_3
|
| 1295 | @until </PLAY>";
|
| 1296 |
|
| 1297 | The structure of the XML file is:
|
| 1298 |
|
| 1299 | <ul>
|
| 1300 | <li>(declaration)</li>
|
| 1301 | <li>(dtd stuff)</li>
|
| 1302 | <li>Element "PLAY"</li>
|
| 1303 | <ul>
|
| 1304 | <li>Element "TITLE"</li>
|
| 1305 | <ul>
|
| 1306 | <li>Text "A Midsummer Night's Dream"</li>
|
| 1307 | </ul>
|
| 1308 | </ul>
|
| 1309 | </ul>
|
| 1310 |
|
| 1311 | For this example, we want to print out the
|
| 1312 | title of the play. The text of the title (what
|
| 1313 | we want) is child of the "TITLE" element which
|
| 1314 | is a child of the "PLAY" element.
|
| 1315 |
|
| 1316 | We want to skip the declaration and dtd, so the
|
| 1317 | method FirstChildElement() is a good choice. The
|
| 1318 | FirstChildElement() of the Document is the "PLAY"
|
| 1319 | Element, the FirstChildElement() of the "PLAY" Element
|
| 1320 | is the "TITLE" Element.
|
| 1321 |
|
| 1322 | @until ( "TITLE" );
|
| 1323 |
|
| 1324 | We can then use the convenience function GetText()
|
| 1325 | to get the title of the play.
|
| 1326 |
|
| 1327 | @until title );
|
| 1328 |
|
| 1329 | Text is just another Node in the XML DOM. And in
|
| 1330 | fact you should be a little cautious with it, as
|
| 1331 | text nodes can contain elements.
|
| 1332 |
|
| 1333 | @verbatim
|
| 1334 | Consider: A Midsummer Night's <b>Dream</b>
|
| 1335 | @endverbatim
|
| 1336 |
|
| 1337 | It is more correct to actually query the Text Node
|
| 1338 | if in doubt:
|
| 1339 |
|
| 1340 | @until title );
|
| 1341 |
|
| 1342 | Noting that here we use FirstChild() since we are
|
| 1343 | looking for XMLText, not an element, and ToText()
|
| 1344 | is a cast from a Node to a XMLText.
|
| 1345 | */
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1346 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1347 | #endif // TINYXML2_INCLUDED
|