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 |
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 24 | #include "tinyxml2.h"
|
| 25 |
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 26 | #include <new> // yes, this one new style header, is in the Android SDK.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 27 | # ifdef ANDROID_NDK
|
| 28 | # include <stddef.h>
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 29 | #else
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 30 | # include <cstddef>
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 31 | #endif
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 32 |
|
| 33 | using namespace tinyxml2;
|
| 34 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 35 | static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 36 | static const char LF = LINE_FEED;
|
| 37 | static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
|
| 38 | static const char CR = CARRIAGE_RETURN;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 39 | static const char SINGLE_QUOTE = '\'';
|
| 40 | static const char DOUBLE_QUOTE = '\"';
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 41 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 42 | // Bunch of unicode info at:
|
| 43 | // http://www.unicode.org/faq/utf_bom.html
|
| 44 | // ef bb bf (Microsoft "lead bytes") - designates UTF-8
|
| 45 |
|
| 46 | static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
|
| 47 | static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
|
| 48 | static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 49 |
|
| 50 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 51 | #define DELETE_NODE( node ) { \
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 52 | if ( node ) { \
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 53 | MemPool* pool = node->_memPool; \
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 54 | node->~XMLNode(); \
|
| 55 | pool->Free( node ); \
|
| 56 | } \
|
| 57 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 58 | #define DELETE_ATTRIBUTE( attrib ) { \
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 59 | if ( attrib ) { \
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 60 | MemPool* pool = attrib->_memPool; \
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 61 | attrib->~XMLAttribute(); \
|
| 62 | pool->Free( attrib ); \
|
| 63 | } \
|
| 64 | }
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 65 |
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 66 | struct Entity {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 67 | const char* pattern;
|
| 68 | int length;
|
| 69 | char value;
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 70 | };
|
| 71 |
|
| 72 | static const int NUM_ENTITIES = 5;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 73 | static const Entity entities[NUM_ENTITIES] = {
|
| 74 | { "quot", 4, DOUBLE_QUOTE },
|
| 75 | { "amp", 3, '&' },
|
| 76 | { "apos", 4, SINGLE_QUOTE },
|
| 77 | { "lt", 2, '<' },
|
| 78 | { "gt", 2, '>' }
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 79 | };
|
| 80 |
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 81 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 82 | StrPair::~StrPair()
|
| 83 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 84 | Reset();
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 85 | }
|
| 86 |
|
| 87 |
|
| 88 | void StrPair::Reset()
|
| 89 | {
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 90 | if ( _flags & NEEDS_DELETE ) {
|
| 91 | delete [] _start;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 92 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 93 | _flags = 0;
|
| 94 | _start = 0;
|
| 95 | _end = 0;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 96 | }
|
| 97 |
|
| 98 |
|
| 99 | void StrPair::SetStr( const char* str, int flags )
|
| 100 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 101 | Reset();
|
| 102 | size_t len = strlen( str );
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 103 | _start = new char[ len+1 ];
|
| 104 | memcpy( _start, str, len+1 );
|
| 105 | _end = _start + len;
|
| 106 | _flags = flags | NEEDS_DELETE;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 107 | }
|
| 108 |
|
| 109 |
|
| 110 | char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
|
| 111 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 112 | TIXMLASSERT( endTag && *endTag );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 113 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 114 | char* start = p; // fixme: hides a member
|
| 115 | char endChar = *endTag;
|
| 116 | size_t length = strlen( endTag );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 117 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 118 | // Inner loop of text parsing.
|
| 119 | while ( *p ) {
|
| 120 | if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
|
| 121 | Set( start, p, strFlags );
|
| 122 | return p + length;
|
| 123 | }
|
| 124 | ++p;
|
| 125 | }
|
| 126 | return 0;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 127 | }
|
| 128 |
|
| 129 |
|
| 130 | char* StrPair::ParseName( char* p )
|
| 131 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 132 | char* start = p;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 133 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 134 | if ( !start || !(*start) ) {
|
| 135 | return 0;
|
| 136 | }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 137 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 138 | while( *p && (
|
| 139 | XMLUtil::IsAlphaNum( (unsigned char) *p )
|
| 140 | || *p == '_'
|
| 141 | || *p == ':'
|
| 142 | || (*p == '-' && p>start ) // can be in a name, but not lead it.
|
| 143 | || (*p == '.' && p>start ) )) { // can be in a name, but not lead it.
|
| 144 | ++p;
|
| 145 | }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 146 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 147 | if ( p > start ) {
|
| 148 | Set( start, p, 0 );
|
| 149 | return p;
|
| 150 | }
|
| 151 | return 0;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 152 | }
|
| 153 |
|
| 154 |
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 155 | void StrPair::CollapseWhitespace()
|
| 156 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 157 | // Trim leading space.
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 158 | _start = XMLUtil::SkipWhiteSpace( _start );
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 159 |
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 160 | if ( _start && *_start ) {
|
| 161 | char* p = _start; // the read pointer
|
| 162 | char* q = _start; // the write pointer
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 163 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 164 | while( *p ) {
|
| 165 | if ( XMLUtil::IsWhiteSpace( *p )) {
|
| 166 | p = XMLUtil::SkipWhiteSpace( p );
|
| 167 | if ( *p == 0 ) {
|
| 168 | break; // don't write to q; this trims the trailing space.
|
| 169 | }
|
| 170 | *q = ' ';
|
| 171 | ++q;
|
| 172 | }
|
| 173 | *q = *p;
|
| 174 | ++q;
|
| 175 | ++p;
|
| 176 | }
|
| 177 | *q = 0;
|
| 178 | }
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 179 | }
|
| 180 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 181 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 182 | const char* StrPair::GetStr()
|
| 183 | {
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 184 | if ( _flags & NEEDS_FLUSH ) {
|
| 185 | *_end = 0;
|
| 186 | _flags ^= NEEDS_FLUSH;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 187 |
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 188 | if ( _flags ) {
|
| 189 | char* p = _start; // the read pointer
|
| 190 | char* q = _start; // the write pointer
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 191 |
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 192 | while( p < _end ) {
|
| 193 | if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 194 | // CR-LF pair becomes LF
|
| 195 | // CR alone becomes LF
|
| 196 | // LF-CR becomes LF
|
| 197 | if ( *(p+1) == LF ) {
|
| 198 | p += 2;
|
| 199 | }
|
| 200 | else {
|
| 201 | ++p;
|
| 202 | }
|
| 203 | *q++ = LF;
|
| 204 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 205 | else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 206 | if ( *(p+1) == CR ) {
|
| 207 | p += 2;
|
| 208 | }
|
| 209 | else {
|
| 210 | ++p;
|
| 211 | }
|
| 212 | *q++ = LF;
|
| 213 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 214 | else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 215 | // Entities handled by tinyXML2:
|
| 216 | // - special entities in the entity table [in/out]
|
| 217 | // - numeric character reference [in]
|
| 218 | // 中 or 中
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 219 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 220 | if ( *(p+1) == '#' ) {
|
| 221 | char buf[10] = { 0 };
|
| 222 | int len;
|
| 223 | p = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
|
| 224 | for( int i=0; i<len; ++i ) {
|
| 225 | *q++ = buf[i];
|
| 226 | }
|
| 227 | TIXMLASSERT( q <= p );
|
| 228 | }
|
| 229 | else {
|
| 230 | int i=0;
|
| 231 | for(; i<NUM_ENTITIES; ++i ) {
|
| 232 | if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
|
| 233 | && *(p+entities[i].length+1) == ';' ) {
|
| 234 | // Found an entity convert;
|
| 235 | *q = entities[i].value;
|
| 236 | ++q;
|
| 237 | p += entities[i].length + 2;
|
| 238 | break;
|
| 239 | }
|
| 240 | }
|
| 241 | if ( i == NUM_ENTITIES ) {
|
| 242 | // fixme: treat as error?
|
| 243 | ++p;
|
| 244 | ++q;
|
| 245 | }
|
| 246 | }
|
| 247 | }
|
| 248 | else {
|
| 249 | *q = *p;
|
| 250 | ++p;
|
| 251 | ++q;
|
| 252 | }
|
| 253 | }
|
| 254 | *q = 0;
|
| 255 | }
|
| 256 | // The loop below has plenty going on, and this
|
| 257 | // is a less useful mode. Break it out.
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 258 | if ( _flags & COLLAPSE_WHITESPACE ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 259 | CollapseWhitespace();
|
| 260 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 261 | _flags = (_flags & NEEDS_DELETE);
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 262 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 263 | return _start;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 264 | }
|
| 265 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 266 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 267 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 268 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 269 | // --------- XMLUtil ----------- //
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 270 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 271 | const char* XMLUtil::ReadBOM( const char* p, bool* bom )
|
| 272 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 273 | *bom = false;
|
| 274 | const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
|
| 275 | // Check for BOM:
|
| 276 | if ( *(pu+0) == TIXML_UTF_LEAD_0
|
| 277 | && *(pu+1) == TIXML_UTF_LEAD_1
|
| 278 | && *(pu+2) == TIXML_UTF_LEAD_2 ) {
|
| 279 | *bom = true;
|
| 280 | p += 3;
|
| 281 | }
|
| 282 | return p;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 283 | }
|
| 284 |
|
| 285 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 286 | void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
|
| 287 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 288 | const unsigned long BYTE_MASK = 0xBF;
|
| 289 | const unsigned long BYTE_MARK = 0x80;
|
| 290 | const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 291 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 292 | if (input < 0x80) {
|
| 293 | *length = 1;
|
| 294 | }
|
| 295 | else if ( input < 0x800 ) {
|
| 296 | *length = 2;
|
| 297 | }
|
| 298 | else if ( input < 0x10000 ) {
|
| 299 | *length = 3;
|
| 300 | }
|
| 301 | else if ( input < 0x200000 ) {
|
| 302 | *length = 4;
|
| 303 | }
|
| 304 | else {
|
| 305 | *length = 0; // This code won't covert this correctly anyway.
|
| 306 | return;
|
| 307 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 308 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 309 | output += *length;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 310 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 311 | // Scary scary fall throughs.
|
| 312 | switch (*length) {
|
| 313 | case 4:
|
| 314 | --output;
|
| 315 | *output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
| 316 | input >>= 6;
|
| 317 | case 3:
|
| 318 | --output;
|
| 319 | *output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
| 320 | input >>= 6;
|
| 321 | case 2:
|
| 322 | --output;
|
| 323 | *output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
| 324 | input >>= 6;
|
| 325 | case 1:
|
| 326 | --output;
|
| 327 | *output = (char)(input | FIRST_BYTE_MARK[*length]);
|
| 328 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 329 | }
|
| 330 |
|
| 331 |
|
| 332 | const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
|
| 333 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 334 | // Presume an entity, and pull it out.
|
| 335 | *length = 0;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 336 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 337 | if ( *(p+1) == '#' && *(p+2) ) {
|
| 338 | unsigned long ucs = 0;
|
| 339 | ptrdiff_t delta = 0;
|
| 340 | unsigned mult = 1;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 341 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 342 | if ( *(p+2) == 'x' ) {
|
| 343 | // Hexadecimal.
|
| 344 | if ( !*(p+3) ) {
|
| 345 | return 0;
|
| 346 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 347 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 348 | const char* q = p+3;
|
| 349 | q = strchr( q, ';' );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 350 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 351 | if ( !q || !*q ) {
|
| 352 | return 0;
|
| 353 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 354 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 355 | delta = q-p;
|
| 356 | --q;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 357 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 358 | while ( *q != 'x' ) {
|
| 359 | if ( *q >= '0' && *q <= '9' ) {
|
| 360 | ucs += mult * (*q - '0');
|
| 361 | }
|
| 362 | else if ( *q >= 'a' && *q <= 'f' ) {
|
| 363 | ucs += mult * (*q - 'a' + 10);
|
| 364 | }
|
| 365 | else if ( *q >= 'A' && *q <= 'F' ) {
|
| 366 | ucs += mult * (*q - 'A' + 10 );
|
| 367 | }
|
| 368 | else {
|
| 369 | return 0;
|
| 370 | }
|
| 371 | mult *= 16;
|
| 372 | --q;
|
| 373 | }
|
| 374 | }
|
| 375 | else {
|
| 376 | // Decimal.
|
| 377 | if ( !*(p+2) ) {
|
| 378 | return 0;
|
| 379 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 380 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 381 | const char* q = p+2;
|
| 382 | q = strchr( q, ';' );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 383 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 384 | if ( !q || !*q ) {
|
| 385 | return 0;
|
| 386 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 387 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 388 | delta = q-p;
|
| 389 | --q;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 390 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 391 | while ( *q != '#' ) {
|
| 392 | if ( *q >= '0' && *q <= '9' ) {
|
| 393 | ucs += mult * (*q - '0');
|
| 394 | }
|
| 395 | else {
|
| 396 | return 0;
|
| 397 | }
|
| 398 | mult *= 10;
|
| 399 | --q;
|
| 400 | }
|
| 401 | }
|
| 402 | // convert the UCS to UTF-8
|
| 403 | ConvertUTF32ToUTF8( ucs, value, length );
|
| 404 | return p + delta + 1;
|
| 405 | }
|
| 406 | return p+1;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 407 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 408 |
|
| 409 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 410 | void XMLUtil::ToStr( int v, char* buffer, int bufferSize )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 411 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 412 | TIXML_SNPRINTF( buffer, bufferSize, "%d", v );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 413 | }
|
| 414 |
|
| 415 |
|
| 416 | void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize )
|
| 417 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 418 | TIXML_SNPRINTF( buffer, bufferSize, "%u", v );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 419 | }
|
| 420 |
|
| 421 |
|
| 422 | void XMLUtil::ToStr( bool v, char* buffer, int bufferSize )
|
| 423 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 424 | TIXML_SNPRINTF( buffer, bufferSize, "%d", v ? 1 : 0 );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 425 | }
|
| 426 |
|
| 427 |
|
| 428 | void XMLUtil::ToStr( float v, char* buffer, int bufferSize )
|
| 429 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 430 | TIXML_SNPRINTF( buffer, bufferSize, "%g", v );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 431 | }
|
| 432 |
|
| 433 |
|
| 434 | void XMLUtil::ToStr( double v, char* buffer, int bufferSize )
|
| 435 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 436 | TIXML_SNPRINTF( buffer, bufferSize, "%g", v );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 437 | }
|
| 438 |
|
| 439 |
|
| 440 | bool XMLUtil::ToInt( const char* str, int* value )
|
| 441 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 442 | if ( TIXML_SSCANF( str, "%d", value ) == 1 ) {
|
| 443 | return true;
|
| 444 | }
|
| 445 | return false;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 446 | }
|
| 447 |
|
| 448 | bool XMLUtil::ToUnsigned( const char* str, unsigned *value )
|
| 449 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 450 | if ( TIXML_SSCANF( str, "%u", value ) == 1 ) {
|
| 451 | return true;
|
| 452 | }
|
| 453 | return false;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 454 | }
|
| 455 |
|
| 456 | bool XMLUtil::ToBool( const char* str, bool* value )
|
| 457 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 458 | int ival = 0;
|
| 459 | if ( ToInt( str, &ival )) {
|
| 460 | *value = (ival==0) ? false : true;
|
| 461 | return true;
|
| 462 | }
|
| 463 | if ( StringEqual( str, "true" ) ) {
|
| 464 | *value = true;
|
| 465 | return true;
|
| 466 | }
|
| 467 | else if ( StringEqual( str, "false" ) ) {
|
| 468 | *value = false;
|
| 469 | return true;
|
| 470 | }
|
| 471 | return false;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 472 | }
|
| 473 |
|
| 474 |
|
| 475 | bool XMLUtil::ToFloat( const char* str, float* value )
|
| 476 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 477 | if ( TIXML_SSCANF( str, "%f", value ) == 1 ) {
|
| 478 | return true;
|
| 479 | }
|
| 480 | return false;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 481 | }
|
| 482 |
|
| 483 | bool XMLUtil::ToDouble( const char* str, double* value )
|
| 484 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 485 | if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) {
|
| 486 | return true;
|
| 487 | }
|
| 488 | return false;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 489 | }
|
| 490 |
|
| 491 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 492 | char* XMLDocument::Identify( char* p, XMLNode** node )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 493 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 494 | XMLNode* returnNode = 0;
|
| 495 | char* start = p;
|
| 496 | p = XMLUtil::SkipWhiteSpace( p );
|
| 497 | if( !p || !*p ) {
|
| 498 | return p;
|
| 499 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 500 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 501 | // What is this thing?
|
| 502 | // - Elements start with a letter or underscore, but xml is reserved.
|
| 503 | // - Comments: <!--
|
| 504 | // - Decleration: <?
|
| 505 | // - Everthing else is unknown to tinyxml.
|
| 506 | //
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 507 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 508 | static const char* xmlHeader = { "<?" };
|
| 509 | static const char* commentHeader = { "<!--" };
|
| 510 | static const char* dtdHeader = { "<!" };
|
| 511 | static const char* cdataHeader = { "<![CDATA[" };
|
| 512 | static const char* elementHeader = { "<" }; // and a header for everything else; check last.
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 513 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 514 | static const int xmlHeaderLen = 2;
|
| 515 | static const int commentHeaderLen = 4;
|
| 516 | static const int dtdHeaderLen = 2;
|
| 517 | static const int cdataHeaderLen = 9;
|
| 518 | static const int elementHeaderLen = 1;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 519 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 520 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 521 | #pragma warning ( push )
|
| 522 | #pragma warning ( disable : 4127 )
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 523 | #endif
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 524 | TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
|
| 525 | TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 526 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 527 | #pragma warning (pop)
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 528 | #endif
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 529 | if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 530 | returnNode = new (_commentPool.Alloc()) XMLDeclaration( this );
|
| 531 | returnNode->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 532 | p += xmlHeaderLen;
|
| 533 | }
|
| 534 | else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 535 | returnNode = new (_commentPool.Alloc()) XMLComment( this );
|
| 536 | returnNode->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 537 | p += commentHeaderLen;
|
| 538 | }
|
| 539 | else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 540 | XMLText* text = new (_textPool.Alloc()) XMLText( this );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 541 | returnNode = text;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 542 | returnNode->_memPool = &_textPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 543 | p += cdataHeaderLen;
|
| 544 | text->SetCData( true );
|
| 545 | }
|
| 546 | else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 547 | returnNode = new (_commentPool.Alloc()) XMLUnknown( this );
|
| 548 | returnNode->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 549 | p += dtdHeaderLen;
|
| 550 | }
|
| 551 | else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 552 | returnNode = new (_elementPool.Alloc()) XMLElement( this );
|
| 553 | returnNode->_memPool = &_elementPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 554 | p += elementHeaderLen;
|
| 555 | }
|
| 556 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 557 | returnNode = new (_textPool.Alloc()) XMLText( this );
|
| 558 | returnNode->_memPool = &_textPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 559 | p = start; // Back it up, all the text counts.
|
| 560 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 561 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 562 | *node = returnNode;
|
| 563 | return p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 564 | }
|
| 565 |
|
| 566 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 567 | bool XMLDocument::Accept( XMLVisitor* visitor ) const
|
| 568 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 569 | if ( visitor->VisitEnter( *this ) ) {
|
| 570 | for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
|
| 571 | if ( !node->Accept( visitor ) ) {
|
| 572 | break;
|
| 573 | }
|
| 574 | }
|
| 575 | }
|
| 576 | return visitor->VisitExit( *this );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 577 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 578 |
|
| 579 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 580 | // --------- XMLNode ----------- //
|
| 581 |
|
| 582 | XMLNode::XMLNode( XMLDocument* doc ) :
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 583 | _document( doc ),
|
| 584 | _parent( 0 ),
|
| 585 | _firstChild( 0 ), _lastChild( 0 ),
|
| 586 | _prev( 0 ), _next( 0 )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 587 | {
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 588 | }
|
| 589 |
|
| 590 |
|
| 591 | XMLNode::~XMLNode()
|
| 592 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 593 | DeleteChildren();
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 594 | if ( _parent ) {
|
| 595 | _parent->Unlink( this );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 596 | }
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 597 | }
|
| 598 |
|
| 599 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 600 | void XMLNode::SetValue( const char* str, bool staticMem )
|
| 601 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 602 | if ( staticMem ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 603 | _value.SetInternedStr( str );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 604 | }
|
| 605 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 606 | _value.SetStr( str );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 607 | }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 608 | }
|
| 609 |
|
| 610 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 611 | void XMLNode::DeleteChildren()
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 612 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 613 | while( _firstChild ) {
|
| 614 | XMLNode* node = _firstChild;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 615 | Unlink( node );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 616 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 617 | DELETE_NODE( node );
|
| 618 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 619 | _firstChild = _lastChild = 0;
|
Lee Thomason | d923c67 | 2012-01-23 08:44:25 -0800 | [diff] [blame] | 620 | }
|
| 621 |
|
| 622 |
|
| 623 | void XMLNode::Unlink( XMLNode* child )
|
| 624 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 625 | TIXMLASSERT( child->_parent == this );
|
| 626 | if ( child == _firstChild ) {
|
| 627 | _firstChild = _firstChild->_next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 628 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 629 | if ( child == _lastChild ) {
|
| 630 | _lastChild = _lastChild->_prev;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 631 | }
|
Lee Thomason | d923c67 | 2012-01-23 08:44:25 -0800 | [diff] [blame] | 632 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 633 | if ( child->_prev ) {
|
| 634 | child->_prev->_next = child->_next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 635 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 636 | if ( child->_next ) {
|
| 637 | child->_next->_prev = child->_prev;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 638 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 639 | child->_parent = 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 640 | }
|
| 641 |
|
| 642 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 643 | void XMLNode::DeleteChild( XMLNode* node )
|
| 644 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 645 | TIXMLASSERT( node->_parent == this );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 646 | DELETE_NODE( node );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 647 | }
|
| 648 |
|
| 649 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 650 | XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
|
| 651 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 652 | if ( _lastChild ) {
|
| 653 | TIXMLASSERT( _firstChild );
|
| 654 | TIXMLASSERT( _lastChild->_next == 0 );
|
| 655 | _lastChild->_next = addThis;
|
| 656 | addThis->_prev = _lastChild;
|
| 657 | _lastChild = addThis;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 658 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 659 | addThis->_next = 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 660 | }
|
| 661 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 662 | TIXMLASSERT( _firstChild == 0 );
|
| 663 | _firstChild = _lastChild = addThis;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 664 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 665 | addThis->_prev = 0;
|
| 666 | addThis->_next = 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 667 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 668 | addThis->_parent = this;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 669 | return addThis;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 670 | }
|
| 671 |
|
| 672 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 673 | XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
|
| 674 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 675 | if ( _firstChild ) {
|
| 676 | TIXMLASSERT( _lastChild );
|
| 677 | TIXMLASSERT( _firstChild->_prev == 0 );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 678 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 679 | _firstChild->_prev = addThis;
|
| 680 | addThis->_next = _firstChild;
|
| 681 | _firstChild = addThis;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 682 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 683 | addThis->_prev = 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 684 | }
|
| 685 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 686 | TIXMLASSERT( _lastChild == 0 );
|
| 687 | _firstChild = _lastChild = addThis;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 688 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 689 | addThis->_prev = 0;
|
| 690 | addThis->_next = 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 691 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 692 | addThis->_parent = this;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 693 | return addThis;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 694 | }
|
| 695 |
|
| 696 |
|
| 697 | XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
|
| 698 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 699 | TIXMLASSERT( afterThis->_parent == this );
|
| 700 | if ( afterThis->_parent != this ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 701 | return 0;
|
| 702 | }
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 703 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 704 | if ( afterThis->_next == 0 ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 705 | // The last node or the only node.
|
| 706 | return InsertEndChild( addThis );
|
| 707 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 708 | addThis->_prev = afterThis;
|
| 709 | addThis->_next = afterThis->_next;
|
| 710 | afterThis->_next->_prev = addThis;
|
| 711 | afterThis->_next = addThis;
|
| 712 | addThis->_parent = this;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 713 | return addThis;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 714 | }
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 719 | const XMLElement* XMLNode::FirstChildElement( const char* value ) const
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 720 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 721 | for( XMLNode* node=_firstChild; node; node=node->_next ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 722 | XMLElement* element = node->ToElement();
|
| 723 | if ( element ) {
|
| 724 | if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
|
| 725 | return element;
|
| 726 | }
|
| 727 | }
|
| 728 | }
|
| 729 | return 0;
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 730 | }
|
| 731 |
|
| 732 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 733 | const XMLElement* XMLNode::LastChildElement( const char* value ) const
|
| 734 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 735 | for( XMLNode* node=_lastChild; node; node=node->_prev ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 736 | XMLElement* element = node->ToElement();
|
| 737 | if ( element ) {
|
| 738 | if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
|
| 739 | return element;
|
| 740 | }
|
| 741 | }
|
| 742 | }
|
| 743 | return 0;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 744 | }
|
| 745 |
|
| 746 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 747 | const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
|
| 748 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 749 | for( XMLNode* element=this->_next; element; element = element->_next ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 750 | if ( element->ToElement()
|
| 751 | && (!value || XMLUtil::StringEqual( value, element->Value() ))) {
|
| 752 | return element->ToElement();
|
| 753 | }
|
| 754 | }
|
| 755 | return 0;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 756 | }
|
| 757 |
|
| 758 |
|
| 759 | const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
|
| 760 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 761 | for( XMLNode* element=_prev; element; element = element->_prev ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 762 | if ( element->ToElement()
|
| 763 | && (!value || XMLUtil::StringEqual( value, element->Value() ))) {
|
| 764 | return element->ToElement();
|
| 765 | }
|
| 766 | }
|
| 767 | return 0;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 768 | }
|
| 769 |
|
| 770 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 771 | char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 772 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 773 | // This is a recursive method, but thinking about it "at the current level"
|
| 774 | // it is a pretty simple flat list:
|
| 775 | // <foo/>
|
| 776 | // <!-- comment -->
|
| 777 | //
|
| 778 | // With a special case:
|
| 779 | // <foo>
|
| 780 | // </foo>
|
| 781 | // <!-- comment -->
|
| 782 | //
|
| 783 | // Where the closing element (/foo) *must* be the next thing after the opening
|
| 784 | // element, and the names must match. BUT the tricky bit is that the closing
|
| 785 | // element will be read by the child.
|
| 786 | //
|
| 787 | // 'endTag' is the end tag for this node, it is returned by a call to a child.
|
| 788 | // 'parentEnd' is the end tag for the parent, which is filled in and returned.
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 789 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 790 | while( p && *p ) {
|
| 791 | XMLNode* node = 0;
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 792 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 793 | p = _document->Identify( p, &node );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 794 | if ( p == 0 || node == 0 ) {
|
| 795 | break;
|
| 796 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 797 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 798 | StrPair endTag;
|
| 799 | p = node->ParseDeep( p, &endTag );
|
| 800 | if ( !p ) {
|
| 801 | DELETE_NODE( node );
|
| 802 | node = 0;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 803 | if ( !_document->Error() ) {
|
| 804 | _document->SetError( XML_ERROR_PARSING, 0, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 805 | }
|
| 806 | break;
|
| 807 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 808 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 809 | // We read the end tag. Return it to the parent.
|
| 810 | if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
|
| 811 | if ( parentEnd ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 812 | *parentEnd = static_cast<XMLElement*>(node)->_value;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 813 | }
|
| 814 | DELETE_NODE( node );
|
| 815 | return p;
|
| 816 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 817 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 818 | // Handle an end tag returned to this level.
|
| 819 | // And handle a bunch of annoying errors.
|
| 820 | XMLElement* ele = node->ToElement();
|
| 821 | if ( ele ) {
|
| 822 | if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 823 | _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 824 | p = 0;
|
| 825 | }
|
| 826 | else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 827 | _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 828 | p = 0;
|
| 829 | }
|
| 830 | else if ( !endTag.Empty() ) {
|
| 831 | if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 832 | _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 833 | p = 0;
|
| 834 | }
|
| 835 | }
|
| 836 | }
|
| 837 | if ( p == 0 ) {
|
| 838 | DELETE_NODE( node );
|
| 839 | node = 0;
|
| 840 | }
|
| 841 | if ( node ) {
|
| 842 | this->InsertEndChild( node );
|
| 843 | }
|
| 844 | }
|
| 845 | return 0;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 846 | }
|
| 847 |
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 848 | // --------- XMLText ---------- //
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 849 | char* XMLText::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 850 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 851 | const char* start = p;
|
| 852 | if ( this->CData() ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 853 | p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 854 | if ( !p ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 855 | _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 856 | }
|
| 857 | return p;
|
| 858 | }
|
| 859 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 860 | int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES;
|
| 861 | if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 862 | flags |= StrPair::COLLAPSE_WHITESPACE;
|
| 863 | }
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 864 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 865 | p = _value.ParseText( p, "<", flags );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 866 | if ( !p ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 867 | _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 868 | }
|
| 869 | if ( p && *p ) {
|
| 870 | return p-1;
|
| 871 | }
|
| 872 | }
|
| 873 | return 0;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 874 | }
|
| 875 |
|
| 876 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 877 | XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
|
| 878 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 879 | if ( !doc ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 880 | doc = _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 881 | }
|
| 882 | XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
|
| 883 | text->SetCData( this->CData() );
|
| 884 | return text;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 885 | }
|
| 886 |
|
| 887 |
|
| 888 | bool XMLText::ShallowEqual( const XMLNode* compare ) const
|
| 889 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 890 | return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() ));
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 891 | }
|
| 892 |
|
| 893 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 894 | bool XMLText::Accept( XMLVisitor* visitor ) const
|
| 895 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 896 | return visitor->Visit( *this );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 897 | }
|
| 898 |
|
| 899 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 900 | // --------- XMLComment ---------- //
|
| 901 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 902 | XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 903 | {
|
| 904 | }
|
| 905 |
|
| 906 |
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 907 | XMLComment::~XMLComment()
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 908 | {
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 909 | }
|
| 910 |
|
| 911 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 912 | char* XMLComment::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 913 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 914 | // Comment parses as text.
|
| 915 | const char* start = p;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 916 | p = _value.ParseText( p, "-->", StrPair::COMMENT );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 917 | if ( p == 0 ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 918 | _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 919 | }
|
| 920 | return p;
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 921 | }
|
| 922 |
|
| 923 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 924 | XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
|
| 925 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 926 | if ( !doc ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 927 | doc = _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 928 | }
|
| 929 | XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
|
| 930 | return comment;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 931 | }
|
| 932 |
|
| 933 |
|
| 934 | bool XMLComment::ShallowEqual( const XMLNode* compare ) const
|
| 935 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 936 | return ( compare->ToComment() && XMLUtil::StringEqual( compare->ToComment()->Value(), Value() ));
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 937 | }
|
| 938 |
|
| 939 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 940 | bool XMLComment::Accept( XMLVisitor* visitor ) const
|
| 941 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 942 | return visitor->Visit( *this );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 943 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 944 |
|
| 945 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 946 | // --------- XMLDeclaration ---------- //
|
| 947 |
|
| 948 | XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
|
| 949 | {
|
| 950 | }
|
| 951 |
|
| 952 |
|
| 953 | XMLDeclaration::~XMLDeclaration()
|
| 954 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 955 | //printf( "~XMLDeclaration\n" );
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 956 | }
|
| 957 |
|
| 958 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 959 | char* XMLDeclaration::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 960 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 961 | // Declaration parses as text.
|
| 962 | const char* start = p;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 963 | p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 964 | if ( p == 0 ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 965 | _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 966 | }
|
| 967 | return p;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 968 | }
|
| 969 |
|
| 970 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 971 | XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
|
| 972 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 973 | if ( !doc ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 974 | doc = _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 975 | }
|
| 976 | XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
|
| 977 | return dec;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 978 | }
|
| 979 |
|
| 980 |
|
| 981 | bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
|
| 982 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 983 | return ( compare->ToDeclaration() && XMLUtil::StringEqual( compare->ToDeclaration()->Value(), Value() ));
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 984 | }
|
| 985 |
|
| 986 |
|
| 987 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 988 | bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
|
| 989 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 990 | return visitor->Visit( *this );
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 991 | }
|
| 992 |
|
| 993 | // --------- XMLUnknown ---------- //
|
| 994 |
|
| 995 | XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
|
| 996 | {
|
| 997 | }
|
| 998 |
|
| 999 |
|
| 1000 | XMLUnknown::~XMLUnknown()
|
| 1001 | {
|
| 1002 | }
|
| 1003 |
|
| 1004 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 1005 | char* XMLUnknown::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1006 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1007 | // Unknown parses as text.
|
| 1008 | const char* start = p;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1009 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1010 | p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1011 | if ( !p ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1012 | _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1013 | }
|
| 1014 | return p;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1015 | }
|
| 1016 |
|
| 1017 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1018 | XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
|
| 1019 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1020 | if ( !doc ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1021 | doc = _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1022 | }
|
| 1023 | XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
|
| 1024 | return text;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1025 | }
|
| 1026 |
|
| 1027 |
|
| 1028 | bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
|
| 1029 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1030 | return ( compare->ToUnknown() && XMLUtil::StringEqual( compare->ToUnknown()->Value(), Value() ));
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1031 | }
|
| 1032 |
|
| 1033 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1034 | bool XMLUnknown::Accept( XMLVisitor* visitor ) const
|
| 1035 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1036 | return visitor->Visit( *this );
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1037 | }
|
| 1038 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1039 | // --------- XMLAttribute ---------- //
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1040 | char* XMLAttribute::ParseDeep( char* p, bool processEntities )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1041 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1042 | // Parse using the name rules: bug fix, was using ParseText before
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1043 | p = _name.ParseName( p );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1044 | if ( !p || !*p ) {
|
| 1045 | return 0;
|
| 1046 | }
|
Lee Thomason | 22aead1 | 2012-01-23 13:29:35 -0800 | [diff] [blame] | 1047 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1048 | // Skip white space before =
|
| 1049 | p = XMLUtil::SkipWhiteSpace( p );
|
| 1050 | if ( !p || *p != '=' ) {
|
| 1051 | return 0;
|
| 1052 | }
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 1053 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1054 | ++p; // move up to opening quote
|
| 1055 | p = XMLUtil::SkipWhiteSpace( p );
|
| 1056 | if ( *p != '\"' && *p != '\'' ) {
|
| 1057 | return 0;
|
| 1058 | }
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 1059 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1060 | char endTag[2] = { *p, 0 };
|
| 1061 | ++p; // move past opening quote
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 1062 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1063 | p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1064 | return p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1065 | }
|
| 1066 |
|
| 1067 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1068 | void XMLAttribute::SetName( const char* n )
|
| 1069 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1070 | _name.SetStr( n );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1071 | }
|
| 1072 |
|
| 1073 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1074 | int XMLAttribute::QueryIntValue( int* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1075 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1076 | if ( XMLUtil::ToInt( Value(), value )) {
|
| 1077 | return XML_NO_ERROR;
|
| 1078 | }
|
| 1079 | return XML_WRONG_ATTRIBUTE_TYPE;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1080 | }
|
| 1081 |
|
| 1082 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1083 | int XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1084 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1085 | if ( XMLUtil::ToUnsigned( Value(), value )) {
|
| 1086 | return XML_NO_ERROR;
|
| 1087 | }
|
| 1088 | return XML_WRONG_ATTRIBUTE_TYPE;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1089 | }
|
| 1090 |
|
| 1091 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1092 | int XMLAttribute::QueryBoolValue( bool* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1093 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1094 | if ( XMLUtil::ToBool( Value(), value )) {
|
| 1095 | return XML_NO_ERROR;
|
| 1096 | }
|
| 1097 | return XML_WRONG_ATTRIBUTE_TYPE;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1098 | }
|
| 1099 |
|
| 1100 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1101 | int XMLAttribute::QueryFloatValue( float* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1102 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1103 | if ( XMLUtil::ToFloat( Value(), value )) {
|
| 1104 | return XML_NO_ERROR;
|
| 1105 | }
|
| 1106 | return XML_WRONG_ATTRIBUTE_TYPE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1107 | }
|
| 1108 |
|
| 1109 |
|
| 1110 | int XMLAttribute::QueryDoubleValue( double* value ) const
|
| 1111 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1112 | if ( XMLUtil::ToDouble( Value(), value )) {
|
| 1113 | return XML_NO_ERROR;
|
| 1114 | }
|
| 1115 | return XML_WRONG_ATTRIBUTE_TYPE;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1116 | }
|
| 1117 |
|
| 1118 |
|
| 1119 | void XMLAttribute::SetAttribute( const char* v )
|
| 1120 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1121 | _value.SetStr( v );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1122 | }
|
| 1123 |
|
| 1124 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1125 | void XMLAttribute::SetAttribute( int v )
|
| 1126 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1127 | char buf[BUF_SIZE];
|
| 1128 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1129 | _value.SetStr( buf );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1130 | }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1131 |
|
| 1132 |
|
| 1133 | void XMLAttribute::SetAttribute( unsigned v )
|
| 1134 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1135 | char buf[BUF_SIZE];
|
| 1136 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1137 | _value.SetStr( buf );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1138 | }
|
| 1139 |
|
| 1140 |
|
| 1141 | void XMLAttribute::SetAttribute( bool v )
|
| 1142 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1143 | char buf[BUF_SIZE];
|
| 1144 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1145 | _value.SetStr( buf );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1146 | }
|
| 1147 |
|
| 1148 | void XMLAttribute::SetAttribute( double v )
|
| 1149 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1150 | char buf[BUF_SIZE];
|
| 1151 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1152 | _value.SetStr( buf );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1153 | }
|
| 1154 |
|
| 1155 | void XMLAttribute::SetAttribute( float v )
|
| 1156 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1157 | char buf[BUF_SIZE];
|
| 1158 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1159 | _value.SetStr( buf );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1160 | }
|
| 1161 |
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 1162 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1163 | // --------- XMLElement ---------- //
|
| 1164 | XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1165 | _closingType( 0 ),
|
| 1166 | _rootAttribute( 0 )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1167 | {
|
| 1168 | }
|
| 1169 |
|
| 1170 |
|
| 1171 | XMLElement::~XMLElement()
|
| 1172 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1173 | while( _rootAttribute ) {
|
| 1174 | XMLAttribute* next = _rootAttribute->_next;
|
| 1175 | DELETE_ATTRIBUTE( _rootAttribute );
|
| 1176 | _rootAttribute = next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1177 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1178 | }
|
| 1179 |
|
| 1180 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1181 | XMLAttribute* XMLElement::FindAttribute( const char* name )
|
| 1182 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1183 | XMLAttribute* a = 0;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1184 | for( a=_rootAttribute; a; a = a->_next ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1185 | if ( XMLUtil::StringEqual( a->Name(), name ) ) {
|
| 1186 | return a;
|
| 1187 | }
|
| 1188 | }
|
| 1189 | return 0;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1190 | }
|
| 1191 |
|
| 1192 |
|
| 1193 | const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
|
| 1194 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1195 | XMLAttribute* a = 0;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1196 | for( a=_rootAttribute; a; a = a->_next ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1197 | if ( XMLUtil::StringEqual( a->Name(), name ) ) {
|
| 1198 | return a;
|
| 1199 | }
|
| 1200 | }
|
| 1201 | return 0;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1202 | }
|
| 1203 |
|
| 1204 |
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 1205 | const char* XMLElement::Attribute( const char* name, const char* value ) const
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1206 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1207 | const XMLAttribute* a = FindAttribute( name );
|
| 1208 | if ( !a ) {
|
| 1209 | return 0;
|
| 1210 | }
|
| 1211 | if ( !value || XMLUtil::StringEqual( a->Value(), value )) {
|
| 1212 | return a->Value();
|
| 1213 | }
|
| 1214 | return 0;
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 1215 | }
|
| 1216 |
|
| 1217 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1218 | const char* XMLElement::GetText() const
|
| 1219 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1220 | if ( FirstChild() && FirstChild()->ToText() ) {
|
| 1221 | return FirstChild()->ToText()->Value();
|
| 1222 | }
|
| 1223 | return 0;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1224 | }
|
| 1225 |
|
| 1226 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1227 | int XMLElement::QueryIntText( int* _value ) const
|
| 1228 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1229 | if ( FirstChild() && FirstChild()->ToText() ) {
|
| 1230 | const char* t = FirstChild()->ToText()->Value();
|
| 1231 | if ( XMLUtil::ToInt( t, _value ) ) {
|
| 1232 | return XML_SUCCESS;
|
| 1233 | }
|
| 1234 | return XML_CAN_NOT_CONVERT_TEXT;
|
| 1235 | }
|
| 1236 | return XML_NO_TEXT_NODE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1237 | }
|
| 1238 |
|
| 1239 |
|
| 1240 | int XMLElement::QueryUnsignedText( unsigned* _value ) const
|
| 1241 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1242 | if ( FirstChild() && FirstChild()->ToText() ) {
|
| 1243 | const char* t = FirstChild()->ToText()->Value();
|
| 1244 | if ( XMLUtil::ToUnsigned( t, _value ) ) {
|
| 1245 | return XML_SUCCESS;
|
| 1246 | }
|
| 1247 | return XML_CAN_NOT_CONVERT_TEXT;
|
| 1248 | }
|
| 1249 | return XML_NO_TEXT_NODE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1250 | }
|
| 1251 |
|
| 1252 |
|
| 1253 | int XMLElement::QueryBoolText( bool* _value ) const
|
| 1254 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1255 | if ( FirstChild() && FirstChild()->ToText() ) {
|
| 1256 | const char* t = FirstChild()->ToText()->Value();
|
| 1257 | if ( XMLUtil::ToBool( t, _value ) ) {
|
| 1258 | return XML_SUCCESS;
|
| 1259 | }
|
| 1260 | return XML_CAN_NOT_CONVERT_TEXT;
|
| 1261 | }
|
| 1262 | return XML_NO_TEXT_NODE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1263 | }
|
| 1264 |
|
| 1265 |
|
| 1266 | int XMLElement::QueryDoubleText( double* _value ) const
|
| 1267 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1268 | if ( FirstChild() && FirstChild()->ToText() ) {
|
| 1269 | const char* t = FirstChild()->ToText()->Value();
|
| 1270 | if ( XMLUtil::ToDouble( t, _value ) ) {
|
| 1271 | return XML_SUCCESS;
|
| 1272 | }
|
| 1273 | return XML_CAN_NOT_CONVERT_TEXT;
|
| 1274 | }
|
| 1275 | return XML_NO_TEXT_NODE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1276 | }
|
| 1277 |
|
| 1278 |
|
| 1279 | int XMLElement::QueryFloatText( float* _value ) const
|
| 1280 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1281 | if ( FirstChild() && FirstChild()->ToText() ) {
|
| 1282 | const char* t = FirstChild()->ToText()->Value();
|
| 1283 | if ( XMLUtil::ToFloat( t, _value ) ) {
|
| 1284 | return XML_SUCCESS;
|
| 1285 | }
|
| 1286 | return XML_CAN_NOT_CONVERT_TEXT;
|
| 1287 | }
|
| 1288 | return XML_NO_TEXT_NODE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1289 | }
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1293 | XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
|
| 1294 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1295 | XMLAttribute* last = 0;
|
| 1296 | XMLAttribute* attrib = 0;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1297 | for( attrib = _rootAttribute;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1298 | attrib;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1299 | last = attrib, attrib = attrib->_next ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1300 | if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {
|
| 1301 | break;
|
| 1302 | }
|
| 1303 | }
|
| 1304 | if ( !attrib ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1305 | attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
|
| 1306 | attrib->_memPool = &_document->_attributePool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1307 | if ( last ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1308 | last->_next = attrib;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1309 | }
|
| 1310 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1311 | _rootAttribute = attrib;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1312 | }
|
| 1313 | attrib->SetName( name );
|
| 1314 | }
|
| 1315 | return attrib;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1316 | }
|
| 1317 |
|
| 1318 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1319 | void XMLElement::DeleteAttribute( const char* name )
|
| 1320 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1321 | XMLAttribute* prev = 0;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1322 | for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1323 | if ( XMLUtil::StringEqual( name, a->Name() ) ) {
|
| 1324 | if ( prev ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1325 | prev->_next = a->_next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1326 | }
|
| 1327 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1328 | _rootAttribute = a->_next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1329 | }
|
| 1330 | DELETE_ATTRIBUTE( a );
|
| 1331 | break;
|
| 1332 | }
|
| 1333 | prev = a;
|
| 1334 | }
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1335 | }
|
| 1336 |
|
| 1337 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1338 | char* XMLElement::ParseAttributes( char* p )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1339 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1340 | const char* start = p;
|
| 1341 | XMLAttribute* prevAttribute = 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1342 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1343 | // Read the attributes.
|
| 1344 | while( p ) {
|
| 1345 | p = XMLUtil::SkipWhiteSpace( p );
|
| 1346 | if ( !p || !(*p) ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1347 | _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1348 | return 0;
|
| 1349 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1350 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1351 | // attribute.
|
| 1352 | if ( XMLUtil::IsAlpha( *p ) ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1353 | XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
|
| 1354 | attrib->_memPool = &_document->_attributePool;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1355 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1356 | p = attrib->ParseDeep( p, _document->ProcessEntities() );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1357 | if ( !p || Attribute( attrib->Name() ) ) {
|
| 1358 | DELETE_ATTRIBUTE( attrib );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1359 | _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1360 | return 0;
|
| 1361 | }
|
| 1362 | // There is a minor bug here: if the attribute in the source xml
|
| 1363 | // document is duplicated, it will not be detected and the
|
| 1364 | // attribute will be doubly added. However, tracking the 'prevAttribute'
|
| 1365 | // avoids re-scanning the attribute list. Preferring performance for
|
| 1366 | // now, may reconsider in the future.
|
| 1367 | if ( prevAttribute ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1368 | prevAttribute->_next = attrib;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1369 | }
|
| 1370 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1371 | _rootAttribute = attrib;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1372 | }
|
| 1373 | prevAttribute = attrib;
|
| 1374 | }
|
| 1375 | // end of the tag
|
| 1376 | else if ( *p == '/' && *(p+1) == '>' ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1377 | _closingType = CLOSED;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1378 | return p+2; // done; sealed element.
|
| 1379 | }
|
| 1380 | // end of the tag
|
| 1381 | else if ( *p == '>' ) {
|
| 1382 | ++p;
|
| 1383 | break;
|
| 1384 | }
|
| 1385 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1386 | _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1387 | return 0;
|
| 1388 | }
|
| 1389 | }
|
| 1390 | return p;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1391 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1392 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1393 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1394 | //
|
| 1395 | // <ele></ele>
|
| 1396 | // <ele>foo<b>bar</b></ele>
|
| 1397 | //
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 1398 | char* XMLElement::ParseDeep( char* p, StrPair* strPair )
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1399 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1400 | // Read the element name.
|
| 1401 | p = XMLUtil::SkipWhiteSpace( p );
|
| 1402 | if ( !p ) {
|
| 1403 | return 0;
|
| 1404 | }
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1405 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1406 | // The closing element is the </element> form. It is
|
| 1407 | // parsed just like a regular element then deleted from
|
| 1408 | // the DOM.
|
| 1409 | if ( *p == '/' ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1410 | _closingType = CLOSING;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1411 | ++p;
|
| 1412 | }
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1413 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1414 | p = _value.ParseName( p );
|
| 1415 | if ( _value.Empty() ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1416 | return 0;
|
| 1417 | }
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1418 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1419 | p = ParseAttributes( p );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1420 | if ( !p || !*p || _closingType ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1421 | return p;
|
| 1422 | }
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1423 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1424 | p = XMLNode::ParseDeep( p, strPair );
|
| 1425 | return p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1426 | }
|
| 1427 |
|
| 1428 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1429 |
|
| 1430 | XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
|
| 1431 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1432 | if ( !doc ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1433 | doc = _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1434 | }
|
| 1435 | XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
|
| 1436 | for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
|
| 1437 | element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
|
| 1438 | }
|
| 1439 | return element;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1440 | }
|
| 1441 |
|
| 1442 |
|
| 1443 | bool XMLElement::ShallowEqual( const XMLNode* compare ) const
|
| 1444 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1445 | const XMLElement* other = compare->ToElement();
|
| 1446 | if ( other && XMLUtil::StringEqual( other->Value(), Value() )) {
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1447 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1448 | const XMLAttribute* a=FirstAttribute();
|
| 1449 | const XMLAttribute* b=other->FirstAttribute();
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1450 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1451 | while ( a && b ) {
|
| 1452 | if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
|
| 1453 | return false;
|
| 1454 | }
|
| 1455 | a = a->Next();
|
| 1456 | b = b->Next();
|
| 1457 | }
|
| 1458 | if ( a || b ) {
|
| 1459 | // different count
|
| 1460 | return false;
|
| 1461 | }
|
| 1462 | return true;
|
| 1463 | }
|
| 1464 | return false;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1465 | }
|
| 1466 |
|
| 1467 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1468 | bool XMLElement::Accept( XMLVisitor* visitor ) const
|
| 1469 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1470 | if ( visitor->VisitEnter( *this, _rootAttribute ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1471 | for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
|
| 1472 | if ( !node->Accept( visitor ) ) {
|
| 1473 | break;
|
| 1474 | }
|
| 1475 | }
|
| 1476 | }
|
| 1477 | return visitor->VisitExit( *this );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1478 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1479 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1480 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1481 | // --------- XMLDocument ----------- //
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1482 | XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) :
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1483 | XMLNode( 0 ),
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1484 | _writeBOM( false ),
|
| 1485 | _processEntities( processEntities ),
|
| 1486 | _errorID( 0 ),
|
| 1487 | _whitespace( whitespace ),
|
| 1488 | _errorStr1( 0 ),
|
| 1489 | _errorStr2( 0 ),
|
| 1490 | _charBuffer( 0 )
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1491 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1492 | _document = this; // avoid warning about 'this' in initializer list
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1493 | }
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1494 |
|
| 1495 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1496 | XMLDocument::~XMLDocument()
|
| 1497 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1498 | DeleteChildren();
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1499 | delete [] _charBuffer;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1500 |
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 1501 | #if 0
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1502 | textPool.Trace( "text" );
|
| 1503 | elementPool.Trace( "element" );
|
| 1504 | commentPool.Trace( "comment" );
|
| 1505 | attributePool.Trace( "attribute" );
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 1506 | #endif
|
| 1507 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1508 | TIXMLASSERT( _textPool.CurrentAllocs() == 0 );
|
| 1509 | TIXMLASSERT( _elementPool.CurrentAllocs() == 0 );
|
| 1510 | TIXMLASSERT( _commentPool.CurrentAllocs() == 0 );
|
| 1511 | TIXMLASSERT( _attributePool.CurrentAllocs() == 0 );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1512 | }
|
| 1513 |
|
| 1514 |
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1515 | void XMLDocument::InitDocument()
|
| 1516 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1517 | _errorID = XML_NO_ERROR;
|
| 1518 | _errorStr1 = 0;
|
| 1519 | _errorStr2 = 0;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1520 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1521 | delete [] _charBuffer;
|
| 1522 | _charBuffer = 0;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1523 | }
|
| 1524 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1525 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1526 | XMLElement* XMLDocument::NewElement( const char* name )
|
| 1527 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1528 | XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this );
|
| 1529 | ele->_memPool = &_elementPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1530 | ele->SetName( name );
|
| 1531 | return ele;
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1532 | }
|
| 1533 |
|
| 1534 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1535 | XMLComment* XMLDocument::NewComment( const char* str )
|
| 1536 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1537 | XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this );
|
| 1538 | comment->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1539 | comment->SetValue( str );
|
| 1540 | return comment;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1541 | }
|
| 1542 |
|
| 1543 |
|
| 1544 | XMLText* XMLDocument::NewText( const char* str )
|
| 1545 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1546 | XMLText* text = new (_textPool.Alloc()) XMLText( this );
|
| 1547 | text->_memPool = &_textPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1548 | text->SetValue( str );
|
| 1549 | return text;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1550 | }
|
| 1551 |
|
| 1552 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1553 | XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
|
| 1554 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1555 | XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this );
|
| 1556 | dec->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1557 | dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
|
| 1558 | return dec;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1559 | }
|
| 1560 |
|
| 1561 |
|
| 1562 | XMLUnknown* XMLDocument::NewUnknown( const char* str )
|
| 1563 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1564 | XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this );
|
| 1565 | unk->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1566 | unk->SetValue( str );
|
| 1567 | return unk;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1568 | }
|
| 1569 |
|
| 1570 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1571 | int XMLDocument::LoadFile( const char* filename )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1572 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1573 | DeleteChildren();
|
| 1574 | InitDocument();
|
| 1575 | FILE* fp = 0;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1576 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1577 | #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
|
| 1578 | errno_t err = fopen_s(&fp, filename, "rb" );
|
| 1579 | if ( !fp || err) {
|
| 1580 | #else
|
| 1581 | fp = fopen( filename, "rb" );
|
| 1582 | if ( !fp) {
|
| 1583 | #endif
|
| 1584 | SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1585 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1586 | }
|
| 1587 | LoadFile( fp );
|
| 1588 | fclose( fp );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1589 | return _errorID;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1590 | }
|
| 1591 |
|
| 1592 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1593 | int XMLDocument::LoadFile( FILE* fp )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1594 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1595 | DeleteChildren();
|
| 1596 | InitDocument();
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1597 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1598 | fseek( fp, 0, SEEK_END );
|
| 1599 | size_t size = ftell( fp );
|
| 1600 | fseek( fp, 0, SEEK_SET );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1601 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1602 | if ( size == 0 ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1603 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1604 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1605 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1606 | _charBuffer = new char[size+1];
|
| 1607 | size_t read = fread( _charBuffer, 1, size, fp );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1608 | if ( read != size ) {
|
| 1609 | SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1610 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1611 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1612 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1613 | _charBuffer[size] = 0;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1614 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1615 | const char* p = _charBuffer;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1616 | p = XMLUtil::SkipWhiteSpace( p );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1617 | p = XMLUtil::ReadBOM( p, &_writeBOM );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1618 | if ( !p || !*p ) {
|
| 1619 | SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1620 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1621 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1622 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1623 | ParseDeep( _charBuffer + (p-_charBuffer), 0 );
|
| 1624 | return _errorID;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1625 | }
|
| 1626 |
|
| 1627 |
|
Robert Reif | 312a20f | 2012-09-08 19:33:57 -0400 | [diff] [blame] | 1628 | int XMLDocument::SaveFile( const char* filename, bool compact )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1629 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1630 | FILE* fp = 0;
|
| 1631 | #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
|
| 1632 | errno_t err = fopen_s(&fp, filename, "w" );
|
| 1633 | if ( !fp || err) {
|
| 1634 | #else
|
| 1635 | fp = fopen( filename, "w" );
|
| 1636 | if ( !fp) {
|
| 1637 | #endif
|
| 1638 | SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1639 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1640 | }
|
| 1641 | SaveFile(fp, compact);
|
| 1642 | fclose( fp );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1643 | return _errorID;
|
Ken Miller | 81da1fb | 2012-04-09 23:32:26 -0500 | [diff] [blame] | 1644 | }
|
| 1645 |
|
| 1646 |
|
Robert Reif | 312a20f | 2012-09-08 19:33:57 -0400 | [diff] [blame] | 1647 | int XMLDocument::SaveFile( FILE* fp, bool compact )
|
Ken Miller | 81da1fb | 2012-04-09 23:32:26 -0500 | [diff] [blame] | 1648 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1649 | XMLPrinter stream( fp, compact );
|
| 1650 | Print( &stream );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1651 | return _errorID;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1652 | }
|
| 1653 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1654 |
|
Lee Thomason (grinliz) | e2bcb32 | 2012-09-17 17:58:25 -0700 | [diff] [blame] | 1655 | int XMLDocument::Parse( const char* p, size_t len )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1656 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1657 | DeleteChildren();
|
| 1658 | InitDocument();
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1659 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1660 | if ( !p || !*p ) {
|
| 1661 | SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1662 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1663 | }
|
| 1664 | if ( len == (size_t)(-1) ) {
|
| 1665 | len = strlen( p );
|
| 1666 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1667 | _charBuffer = new char[ len+1 ];
|
| 1668 | memcpy( _charBuffer, p, len );
|
| 1669 | _charBuffer[len] = 0;
|
Lee Thomason (grinliz) | e2bcb32 | 2012-09-17 17:58:25 -0700 | [diff] [blame] | 1670 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1671 | p = XMLUtil::SkipWhiteSpace( p );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1672 | p = XMLUtil::ReadBOM( p, &_writeBOM );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1673 | if ( !p || !*p ) {
|
| 1674 | SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1675 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1676 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1677 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1678 | ParseDeep( _charBuffer, 0 );
|
| 1679 | return _errorID;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1680 | }
|
| 1681 |
|
| 1682 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1683 | void XMLDocument::Print( XMLPrinter* streamer )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1684 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1685 | XMLPrinter stdStreamer( stdout );
|
| 1686 | if ( !streamer ) {
|
| 1687 | streamer = &stdStreamer;
|
| 1688 | }
|
| 1689 | Accept( streamer );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1690 | }
|
| 1691 |
|
| 1692 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1693 | void XMLDocument::SetError( int error, const char* str1, const char* str2 )
|
| 1694 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1695 | _errorID = error;
|
| 1696 | _errorStr1 = str1;
|
| 1697 | _errorStr2 = str2;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1698 | }
|
| 1699 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1700 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1701 | void XMLDocument::PrintError() const
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1702 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1703 | if ( _errorID ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1704 | static const int LEN = 20;
|
| 1705 | char buf1[LEN] = { 0 };
|
| 1706 | char buf2[LEN] = { 0 };
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1707 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1708 | if ( _errorStr1 ) {
|
| 1709 | TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1710 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1711 | if ( _errorStr2 ) {
|
| 1712 | TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1713 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1714 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1715 | printf( "XMLDocument error id=%d str1=%s str2=%s\n",
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1716 | _errorID, buf1, buf2 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1717 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1718 | }
|
| 1719 |
|
| 1720 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1721 | XMLPrinter::XMLPrinter( FILE* file, bool compact ) :
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1722 | _elementJustOpened( false ),
|
| 1723 | _firstElement( true ),
|
| 1724 | _fp( file ),
|
| 1725 | _depth( 0 ),
|
| 1726 | _textDepth( -1 ),
|
| 1727 | _processEntities( true ),
|
| 1728 | _compactMode( compact )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1729 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1730 | for( int i=0; i<ENTITY_RANGE; ++i ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1731 | _entityFlag[i] = false;
|
| 1732 | _restrictedEntityFlag[i] = false;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1733 | }
|
| 1734 | for( int i=0; i<NUM_ENTITIES; ++i ) {
|
| 1735 | TIXMLASSERT( entities[i].value < ENTITY_RANGE );
|
| 1736 | if ( entities[i].value < ENTITY_RANGE ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1737 | _entityFlag[ (int)entities[i].value ] = true;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1738 | }
|
| 1739 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1740 | _restrictedEntityFlag[(int)'&'] = true;
|
| 1741 | _restrictedEntityFlag[(int)'<'] = true;
|
| 1742 | _restrictedEntityFlag[(int)'>'] = true; // not required, but consistency is nice
|
| 1743 | _buffer.Push( 0 );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1744 | }
|
| 1745 |
|
| 1746 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1747 | void XMLPrinter::Print( const char* format, ... )
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1748 | {
|
| 1749 | va_list va;
|
| 1750 | va_start( va, format );
|
| 1751 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1752 | if ( _fp ) {
|
| 1753 | vfprintf( _fp, format, va );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1754 | }
|
| 1755 | else {
|
| 1756 | // This seems brutally complex. Haven't figured out a better
|
| 1757 | // way on windows.
|
| 1758 | #ifdef _MSC_VER
|
| 1759 | int len = -1;
|
| 1760 | int expand = 1000;
|
| 1761 | while ( len < 0 ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1762 | len = vsnprintf_s( _accumulator.Mem(), _accumulator.Capacity(), _TRUNCATE, format, va );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1763 | if ( len < 0 ) {
|
| 1764 | expand *= 3/2;
|
Lee Thomason | 1aa8fc4 | 2012-10-13 20:01:30 -0700 | [diff] [blame^] | 1765 | _accumulator.PushArr( expand );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1766 | }
|
| 1767 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1768 | char* p = _buffer.PushArr( len ) - 1;
|
| 1769 | memcpy( p, _accumulator.Mem(), len+1 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1770 | #else
|
| 1771 | int len = vsnprintf( 0, 0, format, va );
|
| 1772 | // Close out and re-start the va-args
|
| 1773 | va_end( va );
|
| 1774 | va_start( va, format );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1775 | char* p = _buffer.PushArr( len ) - 1;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1776 | vsnprintf( p, len+1, format, va );
|
| 1777 | #endif
|
| 1778 | }
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1779 | va_end( va );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1780 | }
|
| 1781 |
|
| 1782 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1783 | void XMLPrinter::PrintSpace( int depth )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1784 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1785 | for( int i=0; i<depth; ++i ) {
|
| 1786 | Print( " " );
|
| 1787 | }
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1788 | }
|
| 1789 |
|
| 1790 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1791 | void XMLPrinter::PrintString( const char* p, bool restricted )
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1792 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1793 | // Look for runs of bytes between entities to print.
|
| 1794 | const char* q = p;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1795 | const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag;
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1796 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1797 | if ( _processEntities ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1798 | while ( *q ) {
|
| 1799 | // Remember, char is sometimes signed. (How many times has that bitten me?)
|
| 1800 | if ( *q > 0 && *q < ENTITY_RANGE ) {
|
| 1801 | // Check for entities. If one is found, flush
|
| 1802 | // the stream up until the entity, write the
|
| 1803 | // entity, and keep looking.
|
| 1804 | if ( flag[(unsigned)(*q)] ) {
|
| 1805 | while ( p < q ) {
|
| 1806 | Print( "%c", *p );
|
| 1807 | ++p;
|
| 1808 | }
|
| 1809 | for( int i=0; i<NUM_ENTITIES; ++i ) {
|
| 1810 | if ( entities[i].value == *q ) {
|
| 1811 | Print( "&%s;", entities[i].pattern );
|
| 1812 | break;
|
| 1813 | }
|
| 1814 | }
|
| 1815 | ++p;
|
| 1816 | }
|
| 1817 | }
|
| 1818 | ++q;
|
| 1819 | }
|
| 1820 | }
|
| 1821 | // Flush the remaining string. This will be the entire
|
| 1822 | // string if an entity wasn't found.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1823 | if ( !_processEntities || (q-p > 0) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1824 | Print( "%s", p );
|
| 1825 | }
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1826 | }
|
| 1827 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1828 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1829 | void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1830 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1831 | static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
|
| 1832 | if ( writeBOM ) {
|
| 1833 | Print( "%s", bom );
|
| 1834 | }
|
| 1835 | if ( writeDec ) {
|
| 1836 | PushDeclaration( "xml version=\"1.0\"" );
|
| 1837 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1838 | }
|
| 1839 |
|
| 1840 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1841 | void XMLPrinter::OpenElement( const char* name )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1842 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1843 | if ( _elementJustOpened ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1844 | SealElement();
|
| 1845 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1846 | _stack.Push( name );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1847 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1848 | if ( _textDepth < 0 && !_firstElement && !_compactMode ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1849 | Print( "\n" );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1850 | PrintSpace( _depth );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1851 | }
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1852 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1853 | Print( "<%s", name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1854 | _elementJustOpened = true;
|
| 1855 | _firstElement = false;
|
| 1856 | ++_depth;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1857 | }
|
| 1858 |
|
| 1859 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1860 | void XMLPrinter::PushAttribute( const char* name, const char* value )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1861 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1862 | TIXMLASSERT( _elementJustOpened );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1863 | Print( " %s=\"", name );
|
| 1864 | PrintString( value, false );
|
| 1865 | Print( "\"" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1866 | }
|
| 1867 |
|
| 1868 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1869 | void XMLPrinter::PushAttribute( const char* name, int v )
|
| 1870 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1871 | char buf[BUF_SIZE];
|
| 1872 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 1873 | PushAttribute( name, buf );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1874 | }
|
| 1875 |
|
| 1876 |
|
| 1877 | void XMLPrinter::PushAttribute( const char* name, unsigned v )
|
| 1878 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1879 | char buf[BUF_SIZE];
|
| 1880 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 1881 | PushAttribute( name, buf );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1882 | }
|
| 1883 |
|
| 1884 |
|
| 1885 | void XMLPrinter::PushAttribute( const char* name, bool v )
|
| 1886 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1887 | char buf[BUF_SIZE];
|
| 1888 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 1889 | PushAttribute( name, buf );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1890 | }
|
| 1891 |
|
| 1892 |
|
| 1893 | void XMLPrinter::PushAttribute( const char* name, double v )
|
| 1894 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1895 | char buf[BUF_SIZE];
|
| 1896 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 1897 | PushAttribute( name, buf );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1898 | }
|
| 1899 |
|
| 1900 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1901 | void XMLPrinter::CloseElement()
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1902 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1903 | --_depth;
|
| 1904 | const char* name = _stack.Pop();
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1905 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1906 | if ( _elementJustOpened ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1907 | Print( "/>" );
|
| 1908 | }
|
| 1909 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1910 | if ( _textDepth < 0 && !_compactMode) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1911 | Print( "\n" );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1912 | PrintSpace( _depth );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1913 | }
|
| 1914 | Print( "</%s>", name );
|
| 1915 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1916 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1917 | if ( _textDepth == _depth ) {
|
| 1918 | _textDepth = -1;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1919 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1920 | if ( _depth == 0 && !_compactMode) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1921 | Print( "\n" );
|
| 1922 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1923 | _elementJustOpened = false;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1924 | }
|
| 1925 |
|
| 1926 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1927 | void XMLPrinter::SealElement()
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1928 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1929 | _elementJustOpened = false;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1930 | Print( ">" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1931 | }
|
| 1932 |
|
| 1933 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1934 | void XMLPrinter::PushText( const char* text, bool cdata )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1935 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1936 | _textDepth = _depth-1;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1937 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1938 | if ( _elementJustOpened ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1939 | SealElement();
|
| 1940 | }
|
| 1941 | if ( cdata ) {
|
| 1942 | Print( "<![CDATA[" );
|
| 1943 | Print( "%s", text );
|
| 1944 | Print( "]]>" );
|
| 1945 | }
|
| 1946 | else {
|
| 1947 | PrintString( text, true );
|
| 1948 | }
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1949 | }
|
| 1950 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1951 | void XMLPrinter::PushText( int value )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1952 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1953 | char buf[BUF_SIZE];
|
| 1954 | XMLUtil::ToStr( value, buf, BUF_SIZE );
|
| 1955 | PushText( buf, false );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1956 | }
|
| 1957 |
|
| 1958 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1959 | void XMLPrinter::PushText( unsigned value )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1960 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1961 | char buf[BUF_SIZE];
|
| 1962 | XMLUtil::ToStr( value, buf, BUF_SIZE );
|
| 1963 | PushText( buf, false );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1964 | }
|
| 1965 |
|
| 1966 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1967 | void XMLPrinter::PushText( bool value )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1968 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1969 | char buf[BUF_SIZE];
|
| 1970 | XMLUtil::ToStr( value, buf, BUF_SIZE );
|
| 1971 | PushText( buf, false );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1972 | }
|
| 1973 |
|
| 1974 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1975 | void XMLPrinter::PushText( float value )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1976 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1977 | char buf[BUF_SIZE];
|
| 1978 | XMLUtil::ToStr( value, buf, BUF_SIZE );
|
| 1979 | PushText( buf, false );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1980 | }
|
| 1981 |
|
| 1982 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1983 | void XMLPrinter::PushText( double value )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1984 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1985 | char buf[BUF_SIZE];
|
| 1986 | XMLUtil::ToStr( value, buf, BUF_SIZE );
|
| 1987 | PushText( buf, false );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1988 | }
|
| 1989 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1990 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1991 | void XMLPrinter::PushComment( const char* comment )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1992 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1993 | if ( _elementJustOpened ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1994 | SealElement();
|
| 1995 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1996 | if ( _textDepth < 0 && !_firstElement && !_compactMode) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1997 | Print( "\n" );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1998 | PrintSpace( _depth );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1999 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2000 | _firstElement = false;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2001 | Print( "<!--%s-->", comment );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2002 | }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2003 |
|
| 2004 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2005 | void XMLPrinter::PushDeclaration( const char* value )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2006 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2007 | if ( _elementJustOpened ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2008 | SealElement();
|
| 2009 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2010 | if ( _textDepth < 0 && !_firstElement && !_compactMode) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2011 | Print( "\n" );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2012 | PrintSpace( _depth );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2013 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2014 | _firstElement = false;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2015 | Print( "<?%s?>", value );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2016 | }
|
| 2017 |
|
| 2018 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2019 | void XMLPrinter::PushUnknown( const char* value )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2020 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2021 | if ( _elementJustOpened ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2022 | SealElement();
|
| 2023 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2024 | if ( _textDepth < 0 && !_firstElement && !_compactMode) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2025 | Print( "\n" );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2026 | PrintSpace( _depth );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2027 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2028 | _firstElement = false;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2029 | Print( "<!%s>", value );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2030 | }
|
| 2031 |
|
| 2032 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2033 | bool XMLPrinter::VisitEnter( const XMLDocument& doc )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 2034 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2035 | _processEntities = doc.ProcessEntities();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2036 | if ( doc.HasBOM() ) {
|
| 2037 | PushHeader( true, false );
|
| 2038 | }
|
| 2039 | return true;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 2040 | }
|
| 2041 |
|
| 2042 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2043 | bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2044 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2045 | OpenElement( element.Name() );
|
| 2046 | while ( attribute ) {
|
| 2047 | PushAttribute( attribute->Name(), attribute->Value() );
|
| 2048 | attribute = attribute->Next();
|
| 2049 | }
|
| 2050 | return true;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2051 | }
|
| 2052 |
|
| 2053 |
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 2054 | bool XMLPrinter::VisitExit( const XMLElement& )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2055 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2056 | CloseElement();
|
| 2057 | return true;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2058 | }
|
| 2059 |
|
| 2060 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2061 | bool XMLPrinter::Visit( const XMLText& text )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2062 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2063 | PushText( text.Value(), text.CData() );
|
| 2064 | return true;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2065 | }
|
| 2066 |
|
| 2067 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2068 | bool XMLPrinter::Visit( const XMLComment& comment )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2069 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2070 | PushComment( comment.Value() );
|
| 2071 | return true;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2072 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2073 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2074 | bool XMLPrinter::Visit( const XMLDeclaration& declaration )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2075 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2076 | PushDeclaration( declaration.Value() );
|
| 2077 | return true;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2078 | }
|
| 2079 |
|
| 2080 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2081 | bool XMLPrinter::Visit( const XMLUnknown& unknown )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2082 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2083 | PushUnknown( unknown.Value() );
|
| 2084 | return true;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2085 | }
|