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