Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2 | * tree.c : implementation of access function for an XML tree. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3 | * |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 4 | * References: |
| 5 | * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/ |
| 6 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7 | * See Copyright for the status of this software. |
| 8 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 9 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 13 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 14 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 16 | #include <string.h> /* for memset() only ! */ |
| 17 | |
| 18 | #ifdef HAVE_CTYPE_H |
| 19 | #include <ctype.h> |
| 20 | #endif |
| 21 | #ifdef HAVE_STDLIB_H |
| 22 | #include <stdlib.h> |
| 23 | #endif |
| 24 | #ifdef HAVE_ZLIB_H |
| 25 | #include <zlib.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <libxml/xmlmemory.h> |
| 29 | #include <libxml/tree.h> |
| 30 | #include <libxml/parser.h> |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 31 | #include <libxml/uri.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 32 | #include <libxml/entities.h> |
| 33 | #include <libxml/valid.h> |
| 34 | #include <libxml/xmlerror.h> |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 35 | #include <libxml/parserInternals.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 36 | #include <libxml/globals.h> |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 37 | #ifdef LIBXML_HTML_ENABLED |
| 38 | #include <libxml/HTMLtree.h> |
| 39 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 40 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 41 | int __xmlRegisterCallbacks = 0; |
| 42 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 43 | xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns); |
| 44 | |
| 45 | /************************************************************************ |
| 46 | * * |
| 47 | * A few static variables and macros * |
| 48 | * * |
| 49 | ************************************************************************/ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 50 | /* #undef xmlStringText */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 51 | const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 52 | /* #undef xmlStringTextNoenc */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 53 | const xmlChar xmlStringTextNoenc[] = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 54 | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 55 | /* #undef xmlStringComment */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 56 | const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
| 57 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 58 | static int xmlCompressMode = 0; |
| 59 | static int xmlCheckDTD = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 60 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 61 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
| 62 | xmlNodePtr ulccur = (n)->children; \ |
| 63 | if (ulccur == NULL) { \ |
| 64 | (n)->last = NULL; \ |
| 65 | } else { \ |
| 66 | while (ulccur->next != NULL) { \ |
| 67 | ulccur->parent = (n); \ |
| 68 | ulccur = ulccur->next; \ |
| 69 | } \ |
| 70 | ulccur->parent = (n); \ |
| 71 | (n)->last = ulccur; \ |
| 72 | }} |
| 73 | |
| 74 | /* #define DEBUG_BUFFER */ |
| 75 | /* #define DEBUG_TREE */ |
| 76 | |
| 77 | /************************************************************************ |
| 78 | * * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 79 | * Functions to move to entities.c once the * |
| 80 | * API freeze is smoothen and they can be made public. * |
| 81 | * * |
| 82 | ************************************************************************/ |
| 83 | #include <libxml/hash.h> |
| 84 | |
| 85 | /** |
| 86 | * xmlGetEntityFromDtd: |
| 87 | * @dtd: A pointer to the DTD to search |
| 88 | * @name: The entity name |
| 89 | * |
| 90 | * Do an entity lookup in the DTD entity hash table and |
| 91 | * return the corresponding entity, if found. |
| 92 | * |
| 93 | * Returns A pointer to the entity structure or NULL if not found. |
| 94 | */ |
| 95 | static xmlEntityPtr |
| 96 | xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 97 | xmlEntitiesTablePtr table; |
| 98 | |
| 99 | if((dtd != NULL) && (dtd->entities != NULL)) { |
| 100 | table = (xmlEntitiesTablePtr) dtd->entities; |
| 101 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 102 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 103 | } |
| 104 | return(NULL); |
| 105 | } |
| 106 | /** |
| 107 | * xmlGetParameterEntityFromDtd: |
| 108 | * @dtd: A pointer to the DTD to search |
| 109 | * @name: The entity name |
| 110 | * |
| 111 | * Do an entity lookup in the DTD pararmeter entity hash table and |
| 112 | * return the corresponding entity, if found. |
| 113 | * |
| 114 | * Returns A pointer to the entity structure or NULL if not found. |
| 115 | */ |
| 116 | static xmlEntityPtr |
| 117 | xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 118 | xmlEntitiesTablePtr table; |
| 119 | |
| 120 | if ((dtd != NULL) && (dtd->pentities != NULL)) { |
| 121 | table = (xmlEntitiesTablePtr) dtd->pentities; |
| 122 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 123 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 124 | } |
| 125 | return(NULL); |
| 126 | } |
| 127 | |
| 128 | /************************************************************************ |
| 129 | * * |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 130 | * QName handling helper * |
| 131 | * * |
| 132 | ************************************************************************/ |
| 133 | |
| 134 | /** |
| 135 | * xmlBuildQName: |
| 136 | * @ncname: the Name |
| 137 | * @prefix: the prefix |
| 138 | * @memory: preallocated memory |
| 139 | * @len: preallocated memory length |
| 140 | * |
| 141 | * Builds the QName @prefix:@ncname in @memory if there is enough space |
| 142 | * and prefix is not NULL nor empty, otherwise allocate a new string. |
| 143 | * If prefix is NULL or empty it returns ncname. |
| 144 | * |
| 145 | * Returns the new string which must be freed by the caller if different from |
| 146 | * @memory and @ncname or NULL in case of error |
| 147 | */ |
| 148 | xmlChar * |
| 149 | xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix, |
| 150 | xmlChar *memory, int len) { |
| 151 | int lenn, lenp; |
| 152 | xmlChar *ret; |
| 153 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 154 | if (ncname == NULL) return(NULL); |
| 155 | if (prefix == NULL) return((xmlChar *) ncname); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 156 | |
| 157 | lenn = strlen((char *) ncname); |
| 158 | lenp = strlen((char *) prefix); |
| 159 | |
| 160 | if ((memory == NULL) || (len < lenn + lenp + 2)) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 161 | ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 162 | if (ret == NULL) return(NULL); |
| 163 | } else { |
| 164 | ret = memory; |
| 165 | } |
| 166 | memcpy(&ret[0], prefix, lenp); |
| 167 | ret[lenp] = ':'; |
| 168 | memcpy(&ret[lenp + 1], ncname, lenn); |
| 169 | ret[lenn + lenp + 1] = 0; |
| 170 | return(ret); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * xmlSplitQName2: |
| 175 | * @name: the full QName |
| 176 | * @prefix: a xmlChar ** |
| 177 | * |
| 178 | * parse an XML qualified name string |
| 179 | * |
| 180 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 181 | * |
| 182 | * [NS 6] Prefix ::= NCName |
| 183 | * |
| 184 | * [NS 7] LocalPart ::= NCName |
| 185 | * |
| 186 | * Returns NULL if not a QName, otherwise the local part, and prefix |
| 187 | * is updated to get the Prefix if any. |
| 188 | */ |
| 189 | |
| 190 | xmlChar * |
| 191 | xmlSplitQName2(const xmlChar *name, xmlChar **prefix) { |
| 192 | int len = 0; |
| 193 | xmlChar *ret = NULL; |
| 194 | |
| 195 | *prefix = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 196 | if (name == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 197 | |
| 198 | #ifndef XML_XML_NAMESPACE |
| 199 | /* xml: prefix is not really a namespace */ |
| 200 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 201 | (name[2] == 'l') && (name[3] == ':')) |
| 202 | return(NULL); |
| 203 | #endif |
| 204 | |
| 205 | /* nasty but valid */ |
| 206 | if (name[0] == ':') |
| 207 | return(NULL); |
| 208 | |
| 209 | /* |
| 210 | * we are not trying to validate but just to cut, and yes it will |
| 211 | * work even if this is as set of UTF-8 encoded chars |
| 212 | */ |
| 213 | while ((name[len] != 0) && (name[len] != ':')) |
| 214 | len++; |
| 215 | |
| 216 | if (name[len] == 0) |
| 217 | return(NULL); |
| 218 | |
| 219 | *prefix = xmlStrndup(name, len); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 220 | if (*prefix == NULL) { |
| 221 | xmlGenericError(xmlGenericErrorContext, |
| 222 | "xmlSplitQName2 : out of memory!\n"); |
| 223 | return(NULL); |
| 224 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 225 | ret = xmlStrdup(&name[len + 1]); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 226 | if (ret == NULL) { |
| 227 | xmlGenericError(xmlGenericErrorContext, |
| 228 | "xmlSplitQName2 : out of memory!\n"); |
| 229 | if (*prefix != NULL) { |
| 230 | xmlFree(*prefix); |
| 231 | *prefix = NULL; |
| 232 | } |
| 233 | return(NULL); |
| 234 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 235 | |
| 236 | return(ret); |
| 237 | } |
| 238 | |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 239 | /** |
| 240 | * xmlSplitQName3: |
| 241 | * @name: the full QName |
| 242 | * @len: an int * |
| 243 | * |
| 244 | * parse an XML qualified name string,i |
| 245 | * |
| 246 | * returns NULL if it is not a Qualified Name, otherwise, update len |
| 247 | * with the lenght in byte of the prefix and return a pointer |
| 248 | */ |
| 249 | |
| 250 | const xmlChar * |
| 251 | xmlSplitQName3(const xmlChar *name, int *len) { |
| 252 | int l = 0; |
| 253 | |
| 254 | if (name == NULL) return(NULL); |
| 255 | if (len == NULL) return(NULL); |
| 256 | |
| 257 | /* nasty but valid */ |
| 258 | if (name[0] == ':') |
| 259 | return(NULL); |
| 260 | |
| 261 | /* |
| 262 | * we are not trying to validate but just to cut, and yes it will |
| 263 | * work even if this is as set of UTF-8 encoded chars |
| 264 | */ |
| 265 | while ((name[l] != 0) && (name[l] != ':')) |
| 266 | l++; |
| 267 | |
| 268 | if (name[l] == 0) |
| 269 | return(NULL); |
| 270 | |
| 271 | *len = l; |
| 272 | |
| 273 | return(&name[l+1]); |
| 274 | } |
| 275 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 276 | /************************************************************************ |
| 277 | * * |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 278 | * Check Name, NCName and QName strings * |
| 279 | * * |
| 280 | ************************************************************************/ |
| 281 | |
| 282 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l) |
| 283 | |
| 284 | /** |
| 285 | * xmlValidateNCName: |
| 286 | * @value: the value to check |
| 287 | * @space: allow spaces in front and end of the string |
| 288 | * |
| 289 | * Check that a value conforms to the lexical space of NCName |
| 290 | * |
| 291 | * Returns 0 if this validates, a positive error code number otherwise |
| 292 | * and -1 in case of internal or API error. |
| 293 | */ |
| 294 | int |
| 295 | xmlValidateNCName(const xmlChar *value, int space) { |
| 296 | const xmlChar *cur = value; |
| 297 | int c,l; |
| 298 | |
| 299 | /* |
| 300 | * First quick algorithm for ASCII range |
| 301 | */ |
| 302 | if (space) |
| 303 | while (IS_BLANK(*cur)) cur++; |
| 304 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 305 | (*cur == '_')) |
| 306 | cur++; |
| 307 | else |
| 308 | goto try_complex; |
| 309 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 310 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 311 | ((*cur >= '0') && (*cur <= '9')) || |
| 312 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 313 | cur++; |
| 314 | if (space) |
| 315 | while (IS_BLANK(*cur)) cur++; |
| 316 | if (*cur == 0) |
| 317 | return(0); |
| 318 | |
| 319 | try_complex: |
| 320 | /* |
| 321 | * Second check for chars outside the ASCII range |
| 322 | */ |
| 323 | cur = value; |
| 324 | c = CUR_SCHAR(cur, l); |
| 325 | if (space) { |
| 326 | while (IS_BLANK(c)) { |
| 327 | cur += l; |
| 328 | c = CUR_SCHAR(cur, l); |
| 329 | } |
| 330 | } |
| 331 | if ((!xmlIsLetter(c)) && (c != '_')) |
| 332 | return(1); |
| 333 | cur += l; |
| 334 | c = CUR_SCHAR(cur, l); |
| 335 | while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || |
| 336 | (c == '-') || (c == '_') || xmlIsCombining(c) || |
| 337 | xmlIsExtender(c)) { |
| 338 | cur += l; |
| 339 | c = CUR_SCHAR(cur, l); |
| 340 | } |
| 341 | if (space) { |
| 342 | while (IS_BLANK(c)) { |
| 343 | cur += l; |
| 344 | c = CUR_SCHAR(cur, l); |
| 345 | } |
| 346 | } |
| 347 | if (c != 0) |
| 348 | return(1); |
| 349 | |
| 350 | return(0); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * xmlValidateQName: |
| 355 | * @value: the value to check |
| 356 | * @space: allow spaces in front and end of the string |
| 357 | * |
| 358 | * Check that a value conforms to the lexical space of QName |
| 359 | * |
| 360 | * Returns 0 if this validates, a positive error code number otherwise |
| 361 | * and -1 in case of internal or API error. |
| 362 | */ |
| 363 | int |
| 364 | xmlValidateQName(const xmlChar *value, int space) { |
| 365 | const xmlChar *cur = value; |
| 366 | int c,l; |
| 367 | |
| 368 | /* |
| 369 | * First quick algorithm for ASCII range |
| 370 | */ |
| 371 | if (space) |
| 372 | while (IS_BLANK(*cur)) cur++; |
| 373 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 374 | (*cur == '_')) |
| 375 | cur++; |
| 376 | else |
| 377 | goto try_complex; |
| 378 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 379 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 380 | ((*cur >= '0') && (*cur <= '9')) || |
| 381 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 382 | cur++; |
| 383 | if (*cur == ':') { |
| 384 | cur++; |
| 385 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 386 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 387 | (*cur == '_')) |
| 388 | cur++; |
| 389 | else |
| 390 | goto try_complex; |
| 391 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 392 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 393 | ((*cur >= '0') && (*cur <= '9')) || |
| 394 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 395 | cur++; |
| 396 | } |
| 397 | if (space) |
| 398 | while (IS_BLANK(*cur)) cur++; |
| 399 | if (*cur == 0) |
| 400 | return(0); |
| 401 | |
| 402 | try_complex: |
| 403 | /* |
| 404 | * Second check for chars outside the ASCII range |
| 405 | */ |
| 406 | cur = value; |
| 407 | c = CUR_SCHAR(cur, l); |
| 408 | if (space) { |
| 409 | while (IS_BLANK(c)) { |
| 410 | cur += l; |
| 411 | c = CUR_SCHAR(cur, l); |
| 412 | } |
| 413 | } |
| 414 | if ((!xmlIsLetter(c)) && (c != '_')) |
| 415 | return(1); |
| 416 | cur += l; |
| 417 | c = CUR_SCHAR(cur, l); |
| 418 | while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || |
| 419 | (c == '-') || (c == '_') || xmlIsCombining(c) || |
| 420 | xmlIsExtender(c)) { |
| 421 | cur += l; |
| 422 | c = CUR_SCHAR(cur, l); |
| 423 | } |
| 424 | if (c == ':') { |
| 425 | cur += l; |
| 426 | c = CUR_SCHAR(cur, l); |
| 427 | if ((!xmlIsLetter(c)) && (c != '_')) |
| 428 | return(1); |
| 429 | cur += l; |
| 430 | c = CUR_SCHAR(cur, l); |
| 431 | while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || |
| 432 | (c == '-') || (c == '_') || xmlIsCombining(c) || |
| 433 | xmlIsExtender(c)) { |
| 434 | cur += l; |
| 435 | c = CUR_SCHAR(cur, l); |
| 436 | } |
| 437 | } |
| 438 | if (space) { |
| 439 | while (IS_BLANK(c)) { |
| 440 | cur += l; |
| 441 | c = CUR_SCHAR(cur, l); |
| 442 | } |
| 443 | } |
| 444 | if (c != 0) |
| 445 | return(1); |
| 446 | return(0); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * xmlValidateName: |
| 451 | * @value: the value to check |
| 452 | * @space: allow spaces in front and end of the string |
| 453 | * |
| 454 | * Check that a value conforms to the lexical space of Name |
| 455 | * |
| 456 | * Returns 0 if this validates, a positive error code number otherwise |
| 457 | * and -1 in case of internal or API error. |
| 458 | */ |
| 459 | int |
| 460 | xmlValidateName(const xmlChar *value, int space) { |
| 461 | const xmlChar *cur = value; |
| 462 | int c,l; |
| 463 | |
| 464 | /* |
| 465 | * First quick algorithm for ASCII range |
| 466 | */ |
| 467 | if (space) |
| 468 | while (IS_BLANK(*cur)) cur++; |
| 469 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 470 | (*cur == '_') || (*cur == ':')) |
| 471 | cur++; |
| 472 | else |
| 473 | goto try_complex; |
| 474 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 475 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 476 | ((*cur >= '0') && (*cur <= '9')) || |
| 477 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 478 | cur++; |
| 479 | if (space) |
| 480 | while (IS_BLANK(*cur)) cur++; |
| 481 | if (*cur == 0) |
| 482 | return(0); |
| 483 | |
| 484 | try_complex: |
| 485 | /* |
| 486 | * Second check for chars outside the ASCII range |
| 487 | */ |
| 488 | cur = value; |
| 489 | c = CUR_SCHAR(cur, l); |
| 490 | if (space) { |
| 491 | while (IS_BLANK(c)) { |
| 492 | cur += l; |
| 493 | c = CUR_SCHAR(cur, l); |
| 494 | } |
| 495 | } |
| 496 | if ((!xmlIsLetter(c)) && (c != '_') && (c != ':')) |
| 497 | return(1); |
| 498 | cur += l; |
| 499 | c = CUR_SCHAR(cur, l); |
| 500 | while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || (c == ':') || |
| 501 | (c == '-') || (c == '_') || xmlIsCombining(c) || xmlIsExtender(c)) { |
| 502 | cur += l; |
| 503 | c = CUR_SCHAR(cur, l); |
| 504 | } |
| 505 | if (space) { |
| 506 | while (IS_BLANK(c)) { |
| 507 | cur += l; |
| 508 | c = CUR_SCHAR(cur, l); |
| 509 | } |
| 510 | } |
| 511 | if (c != 0) |
| 512 | return(1); |
| 513 | return(0); |
| 514 | } |
| 515 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 516 | /** |
| 517 | * xmlValidateNMToken: |
| 518 | * @value: the value to check |
| 519 | * @space: allow spaces in front and end of the string |
| 520 | * |
| 521 | * Check that a value conforms to the lexical space of NMToken |
| 522 | * |
| 523 | * Returns 0 if this validates, a positive error code number otherwise |
| 524 | * and -1 in case of internal or API error. |
| 525 | */ |
| 526 | int |
| 527 | xmlValidateNMToken(const xmlChar *value, int space) { |
| 528 | const xmlChar *cur = value; |
| 529 | int c,l; |
| 530 | |
| 531 | /* |
| 532 | * First quick algorithm for ASCII range |
| 533 | */ |
| 534 | if (space) |
| 535 | while (IS_BLANK(*cur)) cur++; |
| 536 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 537 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 538 | ((*cur >= '0') && (*cur <= '9')) || |
| 539 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 540 | cur++; |
| 541 | else |
| 542 | goto try_complex; |
| 543 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 544 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 545 | ((*cur >= '0') && (*cur <= '9')) || |
| 546 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 547 | cur++; |
| 548 | if (space) |
| 549 | while (IS_BLANK(*cur)) cur++; |
| 550 | if (*cur == 0) |
| 551 | return(0); |
| 552 | |
| 553 | try_complex: |
| 554 | /* |
| 555 | * Second check for chars outside the ASCII range |
| 556 | */ |
| 557 | cur = value; |
| 558 | c = CUR_SCHAR(cur, l); |
| 559 | if (space) { |
| 560 | while (IS_BLANK(c)) { |
| 561 | cur += l; |
| 562 | c = CUR_SCHAR(cur, l); |
| 563 | } |
| 564 | } |
| 565 | if (!(xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || (c == ':') || |
| 566 | (c == '-') || (c == '_') || xmlIsCombining(c) || xmlIsExtender(c))) |
| 567 | return(1); |
| 568 | cur += l; |
| 569 | c = CUR_SCHAR(cur, l); |
| 570 | while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || (c == ':') || |
| 571 | (c == '-') || (c == '_') || xmlIsCombining(c) || xmlIsExtender(c)) { |
| 572 | cur += l; |
| 573 | c = CUR_SCHAR(cur, l); |
| 574 | } |
| 575 | if (space) { |
| 576 | while (IS_BLANK(c)) { |
| 577 | cur += l; |
| 578 | c = CUR_SCHAR(cur, l); |
| 579 | } |
| 580 | } |
| 581 | if (c != 0) |
| 582 | return(1); |
| 583 | return(0); |
| 584 | } |
| 585 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 586 | /************************************************************************ |
| 587 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 588 | * Allocation and deallocation of basic structures * |
| 589 | * * |
| 590 | ************************************************************************/ |
| 591 | |
| 592 | /** |
| 593 | * xmlSetBufferAllocationScheme: |
| 594 | * @scheme: allocation method to use |
| 595 | * |
| 596 | * Set the buffer allocation method. Types are |
| 597 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 598 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 599 | * improves performance |
| 600 | */ |
| 601 | void |
| 602 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
| 603 | xmlBufferAllocScheme = scheme; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * xmlGetBufferAllocationScheme: |
| 608 | * |
| 609 | * Types are |
| 610 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 611 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 612 | * improves performance |
| 613 | * |
| 614 | * Returns the current allocation scheme |
| 615 | */ |
| 616 | xmlBufferAllocationScheme |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 617 | xmlGetBufferAllocationScheme(void) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 618 | return(xmlBufferAllocScheme); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /** |
| 622 | * xmlNewNs: |
| 623 | * @node: the element carrying the namespace |
| 624 | * @href: the URI associated |
| 625 | * @prefix: the prefix for the namespace |
| 626 | * |
| 627 | * Creation of a new Namespace. This function will refuse to create |
| 628 | * a namespace with a similar prefix than an existing one present on this |
| 629 | * node. |
| 630 | * We use href==NULL in the case of an element creation where the namespace |
| 631 | * was not defined. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 632 | * Returns a new namespace pointer or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 633 | */ |
| 634 | xmlNsPtr |
| 635 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
| 636 | xmlNsPtr cur; |
| 637 | |
| 638 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 639 | return(NULL); |
| 640 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 641 | if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) |
| 642 | return(NULL); |
| 643 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 644 | /* |
| 645 | * Allocate a new Namespace and fill the fields. |
| 646 | */ |
| 647 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 648 | if (cur == NULL) { |
| 649 | xmlGenericError(xmlGenericErrorContext, |
| 650 | "xmlNewNs : malloc failed\n"); |
| 651 | return(NULL); |
| 652 | } |
| 653 | memset(cur, 0, sizeof(xmlNs)); |
| 654 | cur->type = XML_LOCAL_NAMESPACE; |
| 655 | |
| 656 | if (href != NULL) |
| 657 | cur->href = xmlStrdup(href); |
| 658 | if (prefix != NULL) |
| 659 | cur->prefix = xmlStrdup(prefix); |
| 660 | |
| 661 | /* |
| 662 | * Add it at the end to preserve parsing order ... |
| 663 | * and checks for existing use of the prefix |
| 664 | */ |
| 665 | if (node != NULL) { |
| 666 | if (node->nsDef == NULL) { |
| 667 | node->nsDef = cur; |
| 668 | } else { |
| 669 | xmlNsPtr prev = node->nsDef; |
| 670 | |
| 671 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 672 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 673 | xmlFreeNs(cur); |
| 674 | return(NULL); |
| 675 | } |
| 676 | while (prev->next != NULL) { |
| 677 | prev = prev->next; |
| 678 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 679 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 680 | xmlFreeNs(cur); |
| 681 | return(NULL); |
| 682 | } |
| 683 | } |
| 684 | prev->next = cur; |
| 685 | } |
| 686 | } |
| 687 | return(cur); |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * xmlSetNs: |
| 692 | * @node: a node in the document |
| 693 | * @ns: a namespace pointer |
| 694 | * |
| 695 | * Associate a namespace to a node, a posteriori. |
| 696 | */ |
| 697 | void |
| 698 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
| 699 | if (node == NULL) { |
| 700 | #ifdef DEBUG_TREE |
| 701 | xmlGenericError(xmlGenericErrorContext, |
| 702 | "xmlSetNs: node == NULL\n"); |
| 703 | #endif |
| 704 | return; |
| 705 | } |
| 706 | node->ns = ns; |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * xmlFreeNs: |
| 711 | * @cur: the namespace pointer |
| 712 | * |
| 713 | * Free up the structures associated to a namespace |
| 714 | */ |
| 715 | void |
| 716 | xmlFreeNs(xmlNsPtr cur) { |
| 717 | if (cur == NULL) { |
| 718 | #ifdef DEBUG_TREE |
| 719 | xmlGenericError(xmlGenericErrorContext, |
| 720 | "xmlFreeNs : ns == NULL\n"); |
| 721 | #endif |
| 722 | return; |
| 723 | } |
| 724 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 725 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 726 | xmlFree(cur); |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * xmlFreeNsList: |
| 731 | * @cur: the first namespace pointer |
| 732 | * |
| 733 | * Free up all the structures associated to the chained namespaces. |
| 734 | */ |
| 735 | void |
| 736 | xmlFreeNsList(xmlNsPtr cur) { |
| 737 | xmlNsPtr next; |
| 738 | if (cur == NULL) { |
| 739 | #ifdef DEBUG_TREE |
| 740 | xmlGenericError(xmlGenericErrorContext, |
| 741 | "xmlFreeNsList : ns == NULL\n"); |
| 742 | #endif |
| 743 | return; |
| 744 | } |
| 745 | while (cur != NULL) { |
| 746 | next = cur->next; |
| 747 | xmlFreeNs(cur); |
| 748 | cur = next; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * xmlNewDtd: |
| 754 | * @doc: the document pointer |
| 755 | * @name: the DTD name |
| 756 | * @ExternalID: the external ID |
| 757 | * @SystemID: the system ID |
| 758 | * |
| 759 | * Creation of a new DTD for the external subset. To create an |
| 760 | * internal subset, use xmlCreateIntSubset(). |
| 761 | * |
| 762 | * Returns a pointer to the new DTD structure |
| 763 | */ |
| 764 | xmlDtdPtr |
| 765 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 766 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 767 | xmlDtdPtr cur; |
| 768 | |
| 769 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
| 770 | #ifdef DEBUG_TREE |
| 771 | xmlGenericError(xmlGenericErrorContext, |
| 772 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
| 773 | /* !!! */ (char *) name, doc->name, |
| 774 | /* !!! */ (char *)doc->extSubset->name); |
| 775 | #endif |
| 776 | return(NULL); |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Allocate a new DTD and fill the fields. |
| 781 | */ |
| 782 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 783 | if (cur == NULL) { |
| 784 | xmlGenericError(xmlGenericErrorContext, |
| 785 | "xmlNewDtd : malloc failed\n"); |
| 786 | return(NULL); |
| 787 | } |
| 788 | memset(cur, 0 , sizeof(xmlDtd)); |
| 789 | cur->type = XML_DTD_NODE; |
| 790 | |
| 791 | if (name != NULL) |
| 792 | cur->name = xmlStrdup(name); |
| 793 | if (ExternalID != NULL) |
| 794 | cur->ExternalID = xmlStrdup(ExternalID); |
| 795 | if (SystemID != NULL) |
| 796 | cur->SystemID = xmlStrdup(SystemID); |
| 797 | if (doc != NULL) |
| 798 | doc->extSubset = cur; |
| 799 | cur->doc = doc; |
| 800 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 801 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 802 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 803 | return(cur); |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * xmlGetIntSubset: |
| 808 | * @doc: the document pointer |
| 809 | * |
| 810 | * Get the internal subset of a document |
| 811 | * Returns a pointer to the DTD structure or NULL if not found |
| 812 | */ |
| 813 | |
| 814 | xmlDtdPtr |
| 815 | xmlGetIntSubset(xmlDocPtr doc) { |
| 816 | xmlNodePtr cur; |
| 817 | |
| 818 | if (doc == NULL) |
| 819 | return(NULL); |
| 820 | cur = doc->children; |
| 821 | while (cur != NULL) { |
| 822 | if (cur->type == XML_DTD_NODE) |
| 823 | return((xmlDtdPtr) cur); |
| 824 | cur = cur->next; |
| 825 | } |
| 826 | return((xmlDtdPtr) doc->intSubset); |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * xmlCreateIntSubset: |
| 831 | * @doc: the document pointer |
| 832 | * @name: the DTD name |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 833 | * @ExternalID: the external (PUBLIC) ID |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 834 | * @SystemID: the system ID |
| 835 | * |
| 836 | * Create the internal subset of a document |
| 837 | * Returns a pointer to the new DTD structure |
| 838 | */ |
| 839 | xmlDtdPtr |
| 840 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 841 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 842 | xmlDtdPtr cur; |
| 843 | |
| 844 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
| 845 | #ifdef DEBUG_TREE |
| 846 | xmlGenericError(xmlGenericErrorContext, |
| 847 | |
| 848 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 849 | doc->name); |
| 850 | #endif |
| 851 | return(NULL); |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * Allocate a new DTD and fill the fields. |
| 856 | */ |
| 857 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 858 | if (cur == NULL) { |
| 859 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 860 | "xmlCreateIntSubset : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 861 | return(NULL); |
| 862 | } |
| 863 | memset(cur, 0, sizeof(xmlDtd)); |
| 864 | cur->type = XML_DTD_NODE; |
| 865 | |
| 866 | if (name != NULL) |
| 867 | cur->name = xmlStrdup(name); |
| 868 | if (ExternalID != NULL) |
| 869 | cur->ExternalID = xmlStrdup(ExternalID); |
| 870 | if (SystemID != NULL) |
| 871 | cur->SystemID = xmlStrdup(SystemID); |
| 872 | if (doc != NULL) { |
| 873 | doc->intSubset = cur; |
| 874 | cur->parent = doc; |
| 875 | cur->doc = doc; |
| 876 | if (doc->children == NULL) { |
| 877 | doc->children = (xmlNodePtr) cur; |
| 878 | doc->last = (xmlNodePtr) cur; |
| 879 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 880 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 881 | xmlNodePtr prev; |
| 882 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 883 | prev = doc->children; |
| 884 | prev->prev = (xmlNodePtr) cur; |
| 885 | cur->next = prev; |
| 886 | doc->children = (xmlNodePtr) cur; |
| 887 | } else { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 888 | xmlNodePtr next; |
| 889 | |
| 890 | next = doc->children; |
| 891 | while ((next != NULL) && (next->type != XML_ELEMENT_NODE)) |
| 892 | next = next->next; |
| 893 | if (next == NULL) { |
| 894 | cur->prev = doc->last; |
| 895 | cur->prev->next = (xmlNodePtr) cur; |
| 896 | cur->next = NULL; |
| 897 | doc->last = (xmlNodePtr) cur; |
| 898 | } else { |
| 899 | cur->next = next; |
| 900 | cur->prev = next->prev; |
| 901 | if (cur->prev == NULL) |
| 902 | doc->children = (xmlNodePtr) cur; |
| 903 | else |
| 904 | cur->prev->next = (xmlNodePtr) cur; |
| 905 | next->prev = (xmlNodePtr) cur; |
| 906 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 910 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 911 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 912 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 913 | return(cur); |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * xmlFreeDtd: |
| 918 | * @cur: the DTD structure to free up |
| 919 | * |
| 920 | * Free a DTD structure. |
| 921 | */ |
| 922 | void |
| 923 | xmlFreeDtd(xmlDtdPtr cur) { |
| 924 | if (cur == NULL) { |
| 925 | #ifdef DEBUG_TREE |
| 926 | xmlGenericError(xmlGenericErrorContext, |
| 927 | "xmlFreeDtd : DTD == NULL\n"); |
| 928 | #endif |
| 929 | return; |
| 930 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 931 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 932 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 933 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 934 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 935 | if (cur->children != NULL) { |
| 936 | xmlNodePtr next, c = cur->children; |
| 937 | |
| 938 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 939 | * Cleanup all the DTD comments they are not in the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 940 | * indexes. |
| 941 | */ |
| 942 | while (c != NULL) { |
| 943 | next = c->next; |
Daniel Veillard | d72c7e3 | 2003-05-12 21:55:03 +0000 | [diff] [blame] | 944 | if ((c->type == XML_COMMENT_NODE) || (c->type == XML_PI_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 945 | xmlUnlinkNode(c); |
| 946 | xmlFreeNode(c); |
| 947 | } |
| 948 | c = next; |
| 949 | } |
| 950 | } |
| 951 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 952 | if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID); |
| 953 | if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID); |
| 954 | /* TODO !!! */ |
| 955 | if (cur->notations != NULL) |
| 956 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
| 957 | |
| 958 | if (cur->elements != NULL) |
| 959 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
| 960 | if (cur->attributes != NULL) |
| 961 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
| 962 | if (cur->entities != NULL) |
| 963 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 964 | if (cur->pentities != NULL) |
| 965 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
| 966 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 967 | xmlFree(cur); |
| 968 | } |
| 969 | |
| 970 | /** |
| 971 | * xmlNewDoc: |
| 972 | * @version: xmlChar string giving the version of XML "1.0" |
| 973 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 974 | * Creates a new XML document |
| 975 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 976 | * Returns a new document |
| 977 | */ |
| 978 | xmlDocPtr |
| 979 | xmlNewDoc(const xmlChar *version) { |
| 980 | xmlDocPtr cur; |
| 981 | |
| 982 | if (version == NULL) |
| 983 | version = (const xmlChar *) "1.0"; |
| 984 | |
| 985 | /* |
| 986 | * Allocate a new document and fill the fields. |
| 987 | */ |
| 988 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
| 989 | if (cur == NULL) { |
| 990 | xmlGenericError(xmlGenericErrorContext, |
| 991 | "xmlNewDoc : malloc failed\n"); |
| 992 | return(NULL); |
| 993 | } |
| 994 | memset(cur, 0, sizeof(xmlDoc)); |
| 995 | cur->type = XML_DOCUMENT_NODE; |
| 996 | |
| 997 | cur->version = xmlStrdup(version); |
| 998 | cur->standalone = -1; |
| 999 | cur->compression = -1; /* not initialized */ |
| 1000 | cur->doc = cur; |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 1001 | /* |
| 1002 | * The in memory encoding is always UTF8 |
| 1003 | * This field will never change and would |
| 1004 | * be obsolete if not for binary compatibility. |
| 1005 | */ |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 1006 | cur->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1007 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1008 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1009 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1010 | return(cur); |
| 1011 | } |
| 1012 | |
| 1013 | /** |
| 1014 | * xmlFreeDoc: |
| 1015 | * @cur: pointer to the document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1016 | * |
| 1017 | * Free up all the structures used by a document, tree included. |
| 1018 | */ |
| 1019 | void |
| 1020 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1021 | xmlDtdPtr extSubset, intSubset; |
| 1022 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1023 | if (cur == NULL) { |
| 1024 | #ifdef DEBUG_TREE |
| 1025 | xmlGenericError(xmlGenericErrorContext, |
| 1026 | "xmlFreeDoc : document == NULL\n"); |
| 1027 | #endif |
| 1028 | return; |
| 1029 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1030 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1031 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1032 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1033 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1034 | /* |
| 1035 | * Do this before freeing the children list to avoid ID lookups |
| 1036 | */ |
| 1037 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
| 1038 | cur->ids = NULL; |
| 1039 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
| 1040 | cur->refs = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1041 | extSubset = cur->extSubset; |
| 1042 | intSubset = cur->intSubset; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 1043 | if (intSubset == extSubset) |
| 1044 | extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1045 | if (extSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1046 | xmlUnlinkNode((xmlNodePtr) cur->extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1047 | cur->extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1048 | xmlFreeDtd(extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1049 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1050 | if (intSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1051 | xmlUnlinkNode((xmlNodePtr) cur->intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1052 | cur->intSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1053 | xmlFreeDtd(intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 1057 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1058 | if (cur->version != NULL) xmlFree((char *) cur->version); |
| 1059 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 1060 | if (cur->encoding != NULL) xmlFree((char *) cur->encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1061 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1062 | if (cur->URL != NULL) xmlFree((char *) cur->URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1063 | xmlFree(cur); |
| 1064 | } |
| 1065 | |
| 1066 | /** |
| 1067 | * xmlStringLenGetNodeList: |
| 1068 | * @doc: the document |
| 1069 | * @value: the value of the text |
| 1070 | * @len: the length of the string value |
| 1071 | * |
| 1072 | * Parse the value string and build the node list associated. Should |
| 1073 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1074 | * Returns a pointer to the first child |
| 1075 | */ |
| 1076 | xmlNodePtr |
| 1077 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
| 1078 | xmlNodePtr ret = NULL, last = NULL; |
| 1079 | xmlNodePtr node; |
| 1080 | xmlChar *val; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1081 | const xmlChar *cur = value, *end = cur + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1082 | const xmlChar *q; |
| 1083 | xmlEntityPtr ent; |
| 1084 | |
| 1085 | if (value == NULL) return(NULL); |
| 1086 | |
| 1087 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1088 | while ((cur < end) && (*cur != 0)) { |
| 1089 | if (cur[0] == '&') { |
| 1090 | int charval = 0; |
| 1091 | xmlChar tmp; |
| 1092 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1093 | /* |
| 1094 | * Save the current text. |
| 1095 | */ |
| 1096 | if (cur != q) { |
| 1097 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1098 | xmlNodeAddContentLen(last, q, cur - q); |
| 1099 | } else { |
| 1100 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1101 | if (node == NULL) return(ret); |
| 1102 | if (last == NULL) |
| 1103 | last = ret = node; |
| 1104 | else { |
| 1105 | last->next = node; |
| 1106 | node->prev = last; |
| 1107 | last = node; |
| 1108 | } |
| 1109 | } |
| 1110 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1111 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1112 | if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) { |
| 1113 | cur += 3; |
| 1114 | if (cur < end) |
| 1115 | tmp = *cur; |
| 1116 | else |
| 1117 | tmp = 0; |
| 1118 | while (tmp != ';') { /* Non input consuming loop */ |
| 1119 | if ((tmp >= '0') && (tmp <= '9')) |
| 1120 | charval = charval * 16 + (tmp - '0'); |
| 1121 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1122 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1123 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1124 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1125 | else { |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1126 | xmlGenericError(xmlGenericErrorContext, |
| 1127 | "xmlStringGetNodeList: invalid hexadecimal charvalue\n"); |
| 1128 | charval = 0; |
| 1129 | break; |
| 1130 | } |
| 1131 | cur++; |
| 1132 | if (cur < end) |
| 1133 | tmp = *cur; |
| 1134 | else |
| 1135 | tmp = 0; |
| 1136 | } |
| 1137 | if (tmp == ';') |
| 1138 | cur++; |
| 1139 | q = cur; |
| 1140 | } else if ((cur + 1 < end) && (cur[1] == '#')) { |
| 1141 | cur += 2; |
| 1142 | if (cur < end) |
| 1143 | tmp = *cur; |
| 1144 | else |
| 1145 | tmp = 0; |
| 1146 | while (tmp != ';') { /* Non input consuming loops */ |
| 1147 | if ((tmp >= '0') && (tmp <= '9')) |
| 1148 | charval = charval * 10 + (tmp - '0'); |
| 1149 | else { |
| 1150 | xmlGenericError(xmlGenericErrorContext, |
| 1151 | "xmlStringGetNodeList: invalid decimal charvalue\n"); |
| 1152 | charval = 0; |
| 1153 | break; |
| 1154 | } |
| 1155 | cur++; |
| 1156 | if (cur < end) |
| 1157 | tmp = *cur; |
| 1158 | else |
| 1159 | tmp = 0; |
| 1160 | } |
| 1161 | if (tmp == ';') |
| 1162 | cur++; |
| 1163 | q = cur; |
| 1164 | } else { |
| 1165 | /* |
| 1166 | * Read the entity string |
| 1167 | */ |
| 1168 | cur++; |
| 1169 | q = cur; |
| 1170 | while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++; |
| 1171 | if ((cur >= end) || (*cur == 0)) { |
| 1172 | #ifdef DEBUG_TREE |
| 1173 | xmlGenericError(xmlGenericErrorContext, |
| 1174 | "xmlStringGetNodeList: unterminated entity %30s\n", q); |
| 1175 | #endif |
| 1176 | return(ret); |
| 1177 | } |
| 1178 | if (cur != q) { |
| 1179 | /* |
| 1180 | * Predefined entities don't generate nodes |
| 1181 | */ |
| 1182 | val = xmlStrndup(q, cur - q); |
| 1183 | ent = xmlGetDocEntity(doc, val); |
| 1184 | if ((ent != NULL) && |
| 1185 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1186 | if (last == NULL) { |
| 1187 | node = xmlNewDocText(doc, ent->content); |
| 1188 | last = ret = node; |
| 1189 | } else if (last->type != XML_TEXT_NODE) { |
| 1190 | node = xmlNewDocText(doc, ent->content); |
| 1191 | last = xmlAddNextSibling(last, node); |
| 1192 | } else |
| 1193 | xmlNodeAddContent(last, ent->content); |
| 1194 | |
| 1195 | } else { |
| 1196 | /* |
| 1197 | * Create a new REFERENCE_REF node |
| 1198 | */ |
| 1199 | node = xmlNewReference(doc, val); |
| 1200 | if (node == NULL) { |
| 1201 | if (val != NULL) xmlFree(val); |
| 1202 | return(ret); |
| 1203 | } |
| 1204 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1205 | xmlNodePtr temp; |
| 1206 | |
| 1207 | ent->children = xmlStringGetNodeList(doc, |
| 1208 | (const xmlChar*)node->content); |
| 1209 | ent->owner = 1; |
| 1210 | temp = ent->children; |
| 1211 | while (temp) { |
| 1212 | temp->parent = (xmlNodePtr)ent; |
| 1213 | temp = temp->next; |
| 1214 | } |
| 1215 | } |
| 1216 | if (last == NULL) { |
| 1217 | last = ret = node; |
| 1218 | } else { |
| 1219 | last = xmlAddNextSibling(last, node); |
| 1220 | } |
| 1221 | } |
| 1222 | xmlFree(val); |
| 1223 | } |
| 1224 | cur++; |
| 1225 | q = cur; |
| 1226 | } |
| 1227 | if (charval != 0) { |
| 1228 | xmlChar buf[10]; |
| 1229 | int l; |
| 1230 | |
| 1231 | l = xmlCopyCharMultiByte(buf, charval); |
| 1232 | buf[l] = 0; |
| 1233 | node = xmlNewDocText(doc, buf); |
| 1234 | if (node != NULL) { |
| 1235 | if (last == NULL) { |
| 1236 | last = ret = node; |
| 1237 | } else { |
| 1238 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1239 | } |
| 1240 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1241 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1242 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1243 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1244 | cur++; |
| 1245 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1246 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1247 | /* |
| 1248 | * Handle the last piece of text. |
| 1249 | */ |
| 1250 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1251 | xmlNodeAddContentLen(last, q, cur - q); |
| 1252 | } else { |
| 1253 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1254 | if (node == NULL) return(ret); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1255 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1256 | last = ret = node; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1257 | } else { |
| 1258 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | return(ret); |
| 1263 | } |
| 1264 | |
| 1265 | /** |
| 1266 | * xmlStringGetNodeList: |
| 1267 | * @doc: the document |
| 1268 | * @value: the value of the attribute |
| 1269 | * |
| 1270 | * Parse the value string and build the node list associated. Should |
| 1271 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1272 | * Returns a pointer to the first child |
| 1273 | */ |
| 1274 | xmlNodePtr |
| 1275 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
| 1276 | xmlNodePtr ret = NULL, last = NULL; |
| 1277 | xmlNodePtr node; |
| 1278 | xmlChar *val; |
| 1279 | const xmlChar *cur = value; |
| 1280 | const xmlChar *q; |
| 1281 | xmlEntityPtr ent; |
| 1282 | |
| 1283 | if (value == NULL) return(NULL); |
| 1284 | |
| 1285 | q = cur; |
| 1286 | while (*cur != 0) { |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1287 | if (cur[0] == '&') { |
| 1288 | int charval = 0; |
| 1289 | xmlChar tmp; |
| 1290 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1291 | /* |
| 1292 | * Save the current text. |
| 1293 | */ |
| 1294 | if (cur != q) { |
| 1295 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1296 | xmlNodeAddContentLen(last, q, cur - q); |
| 1297 | } else { |
| 1298 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1299 | if (node == NULL) return(ret); |
| 1300 | if (last == NULL) |
| 1301 | last = ret = node; |
| 1302 | else { |
| 1303 | last->next = node; |
| 1304 | node->prev = last; |
| 1305 | last = node; |
| 1306 | } |
| 1307 | } |
| 1308 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1309 | q = cur; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1310 | if ((cur[1] == '#') && (cur[2] == 'x')) { |
| 1311 | cur += 3; |
| 1312 | tmp = *cur; |
| 1313 | while (tmp != ';') { /* Non input consuming loop */ |
| 1314 | if ((tmp >= '0') && (tmp <= '9')) |
| 1315 | charval = charval * 16 + (tmp - '0'); |
| 1316 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1317 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1318 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1319 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1320 | else { |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1321 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1322 | "xmlStringGetNodeList: invalid hexadecimal charvalue\n"); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1323 | charval = 0; |
| 1324 | break; |
| 1325 | } |
| 1326 | cur++; |
| 1327 | tmp = *cur; |
| 1328 | } |
| 1329 | if (tmp == ';') |
| 1330 | cur++; |
| 1331 | q = cur; |
| 1332 | } else if (cur[1] == '#') { |
| 1333 | cur += 2; |
| 1334 | tmp = *cur; |
| 1335 | while (tmp != ';') { /* Non input consuming loops */ |
| 1336 | if ((tmp >= '0') && (tmp <= '9')) |
| 1337 | charval = charval * 10 + (tmp - '0'); |
| 1338 | else { |
| 1339 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1340 | "xmlStringGetNodeList: invalid decimal charvalue\n"); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1341 | charval = 0; |
| 1342 | break; |
| 1343 | } |
| 1344 | cur++; |
| 1345 | tmp = *cur; |
| 1346 | } |
| 1347 | if (tmp == ';') |
| 1348 | cur++; |
| 1349 | q = cur; |
| 1350 | } else { |
| 1351 | /* |
| 1352 | * Read the entity string |
| 1353 | */ |
| 1354 | cur++; |
| 1355 | q = cur; |
| 1356 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 1357 | if (*cur == 0) { |
| 1358 | #ifdef DEBUG_TREE |
| 1359 | xmlGenericError(xmlGenericErrorContext, |
| 1360 | "xmlStringGetNodeList: unterminated entity %30s\n", q); |
| 1361 | #endif |
| 1362 | return(ret); |
| 1363 | } |
| 1364 | if (cur != q) { |
| 1365 | /* |
| 1366 | * Predefined entities don't generate nodes |
| 1367 | */ |
| 1368 | val = xmlStrndup(q, cur - q); |
| 1369 | ent = xmlGetDocEntity(doc, val); |
| 1370 | if ((ent != NULL) && |
| 1371 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1372 | if (last == NULL) { |
| 1373 | node = xmlNewDocText(doc, ent->content); |
| 1374 | last = ret = node; |
Daniel Veillard | 6f42c13 | 2002-01-06 23:05:13 +0000 | [diff] [blame] | 1375 | } else if (last->type != XML_TEXT_NODE) { |
| 1376 | node = xmlNewDocText(doc, ent->content); |
| 1377 | last = xmlAddNextSibling(last, node); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1378 | } else |
| 1379 | xmlNodeAddContent(last, ent->content); |
| 1380 | |
| 1381 | } else { |
| 1382 | /* |
| 1383 | * Create a new REFERENCE_REF node |
| 1384 | */ |
| 1385 | node = xmlNewReference(doc, val); |
| 1386 | if (node == NULL) { |
| 1387 | if (val != NULL) xmlFree(val); |
| 1388 | return(ret); |
| 1389 | } |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1390 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1391 | xmlNodePtr temp; |
| 1392 | |
| 1393 | ent->children = xmlStringGetNodeList(doc, |
| 1394 | (const xmlChar*)node->content); |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 1395 | ent->owner = 1; |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1396 | temp = ent->children; |
| 1397 | while (temp) { |
| 1398 | temp->parent = (xmlNodePtr)ent; |
| 1399 | temp = temp->next; |
| 1400 | } |
| 1401 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1402 | if (last == NULL) { |
| 1403 | last = ret = node; |
| 1404 | } else { |
| 1405 | last = xmlAddNextSibling(last, node); |
| 1406 | } |
| 1407 | } |
| 1408 | xmlFree(val); |
| 1409 | } |
| 1410 | cur++; |
| 1411 | q = cur; |
| 1412 | } |
| 1413 | if (charval != 0) { |
| 1414 | xmlChar buf[10]; |
| 1415 | int len; |
| 1416 | |
| 1417 | len = xmlCopyCharMultiByte(buf, charval); |
| 1418 | buf[len] = 0; |
| 1419 | node = xmlNewDocText(doc, buf); |
| 1420 | if (node != NULL) { |
| 1421 | if (last == NULL) { |
| 1422 | last = ret = node; |
| 1423 | } else { |
| 1424 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1425 | } |
| 1426 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1427 | |
| 1428 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1429 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1430 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1431 | cur++; |
| 1432 | } |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 1433 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1434 | /* |
| 1435 | * Handle the last piece of text. |
| 1436 | */ |
| 1437 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1438 | xmlNodeAddContentLen(last, q, cur - q); |
| 1439 | } else { |
| 1440 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1441 | if (node == NULL) return(ret); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1442 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1443 | last = ret = node; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1444 | } else { |
| 1445 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | return(ret); |
| 1450 | } |
| 1451 | |
| 1452 | /** |
| 1453 | * xmlNodeListGetString: |
| 1454 | * @doc: the document |
| 1455 | * @list: a Node list |
| 1456 | * @inLine: should we replace entity contents or show their external form |
| 1457 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1458 | * Build the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1459 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1460 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1461 | * Returns a pointer to the string copy, the caller must free it with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1462 | */ |
| 1463 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1464 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1465 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1466 | xmlNodePtr node = list; |
| 1467 | xmlChar *ret = NULL; |
| 1468 | xmlEntityPtr ent; |
| 1469 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1470 | if (list == NULL) |
| 1471 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1472 | |
| 1473 | while (node != NULL) { |
| 1474 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1475 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1476 | if (inLine) { |
| 1477 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1478 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1479 | xmlChar *buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1480 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1481 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
| 1482 | if (buffer != NULL) { |
| 1483 | ret = xmlStrcat(ret, buffer); |
| 1484 | xmlFree(buffer); |
| 1485 | } |
| 1486 | } |
| 1487 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1488 | if (inLine) { |
| 1489 | ent = xmlGetDocEntity(doc, node->name); |
| 1490 | if (ent != NULL) { |
| 1491 | xmlChar *buffer; |
| 1492 | |
| 1493 | /* an entity content can be any "well balanced chunk", |
| 1494 | * i.e. the result of the content [43] production: |
| 1495 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1496 | * So it can contain text, CDATA section or nested |
| 1497 | * entity reference nodes (among others). |
| 1498 | * -> we recursive call xmlNodeListGetString() |
| 1499 | * which handles these types */ |
| 1500 | buffer = xmlNodeListGetString(doc, ent->children, 1); |
| 1501 | if (buffer != NULL) { |
| 1502 | ret = xmlStrcat(ret, buffer); |
| 1503 | xmlFree(buffer); |
| 1504 | } |
| 1505 | } else { |
| 1506 | ret = xmlStrcat(ret, node->content); |
| 1507 | } |
| 1508 | } else { |
| 1509 | xmlChar buf[2]; |
| 1510 | |
| 1511 | buf[0] = '&'; |
| 1512 | buf[1] = 0; |
| 1513 | ret = xmlStrncat(ret, buf, 1); |
| 1514 | ret = xmlStrcat(ret, node->name); |
| 1515 | buf[0] = ';'; |
| 1516 | buf[1] = 0; |
| 1517 | ret = xmlStrncat(ret, buf, 1); |
| 1518 | } |
| 1519 | } |
| 1520 | #if 0 |
| 1521 | else { |
| 1522 | xmlGenericError(xmlGenericErrorContext, |
| 1523 | "xmlGetNodeListString : invalid node type %d\n", |
| 1524 | node->type); |
| 1525 | } |
| 1526 | #endif |
| 1527 | node = node->next; |
| 1528 | } |
| 1529 | return (ret); |
| 1530 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1531 | /** |
| 1532 | * xmlNodeListGetRawString: |
| 1533 | * @doc: the document |
| 1534 | * @list: a Node list |
| 1535 | * @inLine: should we replace entity contents or show their external form |
| 1536 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1537 | * Builds the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1538 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 1539 | * this function doesn't do any character encoding handling. |
| 1540 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1541 | * Returns a pointer to the string copy, the caller must free it with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1542 | */ |
| 1543 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1544 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1545 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1546 | xmlNodePtr node = list; |
| 1547 | xmlChar *ret = NULL; |
| 1548 | xmlEntityPtr ent; |
| 1549 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1550 | if (list == NULL) |
| 1551 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1552 | |
| 1553 | while (node != NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1554 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1555 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1556 | if (inLine) { |
| 1557 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1558 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1559 | xmlChar *buffer; |
| 1560 | |
| 1561 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 1562 | if (buffer != NULL) { |
| 1563 | ret = xmlStrcat(ret, buffer); |
| 1564 | xmlFree(buffer); |
| 1565 | } |
| 1566 | } |
| 1567 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1568 | if (inLine) { |
| 1569 | ent = xmlGetDocEntity(doc, node->name); |
| 1570 | if (ent != NULL) { |
| 1571 | xmlChar *buffer; |
| 1572 | |
| 1573 | /* an entity content can be any "well balanced chunk", |
| 1574 | * i.e. the result of the content [43] production: |
| 1575 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1576 | * So it can contain text, CDATA section or nested |
| 1577 | * entity reference nodes (among others). |
| 1578 | * -> we recursive call xmlNodeListGetRawString() |
| 1579 | * which handles these types */ |
| 1580 | buffer = |
| 1581 | xmlNodeListGetRawString(doc, ent->children, 1); |
| 1582 | if (buffer != NULL) { |
| 1583 | ret = xmlStrcat(ret, buffer); |
| 1584 | xmlFree(buffer); |
| 1585 | } |
| 1586 | } else { |
| 1587 | ret = xmlStrcat(ret, node->content); |
| 1588 | } |
| 1589 | } else { |
| 1590 | xmlChar buf[2]; |
| 1591 | |
| 1592 | buf[0] = '&'; |
| 1593 | buf[1] = 0; |
| 1594 | ret = xmlStrncat(ret, buf, 1); |
| 1595 | ret = xmlStrcat(ret, node->name); |
| 1596 | buf[0] = ';'; |
| 1597 | buf[1] = 0; |
| 1598 | ret = xmlStrncat(ret, buf, 1); |
| 1599 | } |
| 1600 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1601 | #if 0 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1602 | else { |
| 1603 | xmlGenericError(xmlGenericErrorContext, |
| 1604 | "xmlGetNodeListString : invalid node type %d\n", |
| 1605 | node->type); |
| 1606 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1607 | #endif |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1608 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1609 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1610 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /** |
| 1614 | * xmlNewProp: |
| 1615 | * @node: the holding node |
| 1616 | * @name: the name of the attribute |
| 1617 | * @value: the value of the attribute |
| 1618 | * |
| 1619 | * Create a new property carried by a node. |
| 1620 | * Returns a pointer to the attribute |
| 1621 | */ |
| 1622 | xmlAttrPtr |
| 1623 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 1624 | xmlAttrPtr cur; |
| 1625 | xmlDocPtr doc = NULL; |
| 1626 | |
| 1627 | if (name == NULL) { |
| 1628 | #ifdef DEBUG_TREE |
| 1629 | xmlGenericError(xmlGenericErrorContext, |
| 1630 | "xmlNewProp : name == NULL\n"); |
| 1631 | #endif |
| 1632 | return(NULL); |
| 1633 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 1634 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 1635 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1636 | |
| 1637 | /* |
| 1638 | * Allocate a new property and fill the fields. |
| 1639 | */ |
| 1640 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1641 | if (cur == NULL) { |
| 1642 | xmlGenericError(xmlGenericErrorContext, |
| 1643 | "xmlNewProp : malloc failed\n"); |
| 1644 | return(NULL); |
| 1645 | } |
| 1646 | memset(cur, 0, sizeof(xmlAttr)); |
| 1647 | cur->type = XML_ATTRIBUTE_NODE; |
| 1648 | |
| 1649 | cur->parent = node; |
| 1650 | if (node != NULL) { |
| 1651 | doc = node->doc; |
| 1652 | cur->doc = doc; |
| 1653 | } |
| 1654 | cur->name = xmlStrdup(name); |
| 1655 | if (value != NULL) { |
| 1656 | xmlChar *buffer; |
| 1657 | xmlNodePtr tmp; |
| 1658 | |
| 1659 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1660 | cur->children = xmlStringGetNodeList(doc, buffer); |
| 1661 | cur->last = NULL; |
| 1662 | tmp = cur->children; |
| 1663 | while (tmp != NULL) { |
| 1664 | tmp->parent = (xmlNodePtr) cur; |
| 1665 | tmp->doc = doc; |
| 1666 | if (tmp->next == NULL) |
| 1667 | cur->last = tmp; |
| 1668 | tmp = tmp->next; |
| 1669 | } |
| 1670 | xmlFree(buffer); |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | * Add it at the end to preserve parsing order ... |
| 1675 | */ |
| 1676 | if (node != NULL) { |
| 1677 | if (node->properties == NULL) { |
| 1678 | node->properties = cur; |
| 1679 | } else { |
| 1680 | xmlAttrPtr prev = node->properties; |
| 1681 | |
| 1682 | while (prev->next != NULL) prev = prev->next; |
| 1683 | prev->next = cur; |
| 1684 | cur->prev = prev; |
| 1685 | } |
| 1686 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1687 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1688 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1689 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1690 | return(cur); |
| 1691 | } |
| 1692 | |
| 1693 | /** |
| 1694 | * xmlNewNsProp: |
| 1695 | * @node: the holding node |
| 1696 | * @ns: the namespace |
| 1697 | * @name: the name of the attribute |
| 1698 | * @value: the value of the attribute |
| 1699 | * |
| 1700 | * Create a new property tagged with a namespace and carried by a node. |
| 1701 | * Returns a pointer to the attribute |
| 1702 | */ |
| 1703 | xmlAttrPtr |
| 1704 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 1705 | const xmlChar *value) { |
| 1706 | xmlAttrPtr cur; |
Daniel Veillard | a682b21 | 2001-06-07 19:59:42 +0000 | [diff] [blame] | 1707 | xmlDocPtr doc = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1708 | |
| 1709 | if (name == NULL) { |
| 1710 | #ifdef DEBUG_TREE |
| 1711 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1712 | "xmlNewNsProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1713 | #endif |
| 1714 | return(NULL); |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * Allocate a new property and fill the fields. |
| 1719 | */ |
| 1720 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1721 | if (cur == NULL) { |
| 1722 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1723 | "xmlNewNsProp : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1724 | return(NULL); |
| 1725 | } |
| 1726 | memset(cur, 0, sizeof(xmlAttr)); |
| 1727 | cur->type = XML_ATTRIBUTE_NODE; |
| 1728 | |
| 1729 | cur->parent = node; |
Daniel Veillard | a682b21 | 2001-06-07 19:59:42 +0000 | [diff] [blame] | 1730 | if (node != NULL) { |
| 1731 | doc = node->doc; |
| 1732 | cur->doc = doc; |
| 1733 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1734 | cur->ns = ns; |
| 1735 | cur->name = xmlStrdup(name); |
| 1736 | if (value != NULL) { |
| 1737 | xmlChar *buffer; |
| 1738 | xmlNodePtr tmp; |
| 1739 | |
Daniel Veillard | a682b21 | 2001-06-07 19:59:42 +0000 | [diff] [blame] | 1740 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1741 | cur->children = xmlStringGetNodeList(doc, buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1742 | cur->last = NULL; |
| 1743 | tmp = cur->children; |
| 1744 | while (tmp != NULL) { |
| 1745 | tmp->parent = (xmlNodePtr) cur; |
| 1746 | if (tmp->next == NULL) |
| 1747 | cur->last = tmp; |
| 1748 | tmp = tmp->next; |
| 1749 | } |
| 1750 | xmlFree(buffer); |
| 1751 | } |
| 1752 | |
| 1753 | /* |
| 1754 | * Add it at the end to preserve parsing order ... |
| 1755 | */ |
| 1756 | if (node != NULL) { |
| 1757 | if (node->properties == NULL) { |
| 1758 | node->properties = cur; |
| 1759 | } else { |
| 1760 | xmlAttrPtr prev = node->properties; |
| 1761 | |
| 1762 | while (prev->next != NULL) prev = prev->next; |
| 1763 | prev->next = cur; |
| 1764 | cur->prev = prev; |
| 1765 | } |
| 1766 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1767 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1768 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1769 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1770 | return(cur); |
| 1771 | } |
| 1772 | |
| 1773 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1774 | * xmlNewNsPropEatName: |
| 1775 | * @node: the holding node |
| 1776 | * @ns: the namespace |
| 1777 | * @name: the name of the attribute |
| 1778 | * @value: the value of the attribute |
| 1779 | * |
| 1780 | * Create a new property tagged with a namespace and carried by a node. |
| 1781 | * Returns a pointer to the attribute |
| 1782 | */ |
| 1783 | xmlAttrPtr |
| 1784 | xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name, |
| 1785 | const xmlChar *value) { |
| 1786 | xmlAttrPtr cur; |
| 1787 | xmlDocPtr doc = NULL; |
| 1788 | |
| 1789 | if (name == NULL) { |
| 1790 | #ifdef DEBUG_TREE |
| 1791 | xmlGenericError(xmlGenericErrorContext, |
| 1792 | "xmlNewNsPropEatName : name == NULL\n"); |
| 1793 | #endif |
| 1794 | return(NULL); |
| 1795 | } |
| 1796 | |
| 1797 | /* |
| 1798 | * Allocate a new property and fill the fields. |
| 1799 | */ |
| 1800 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1801 | if (cur == NULL) { |
| 1802 | xmlGenericError(xmlGenericErrorContext, |
| 1803 | "xmlNewNsPropEatName : malloc failed\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1804 | xmlFree(name); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1805 | return(NULL); |
| 1806 | } |
| 1807 | memset(cur, 0, sizeof(xmlAttr)); |
| 1808 | cur->type = XML_ATTRIBUTE_NODE; |
| 1809 | |
| 1810 | cur->parent = node; |
| 1811 | if (node != NULL) { |
| 1812 | doc = node->doc; |
| 1813 | cur->doc = doc; |
| 1814 | } |
| 1815 | cur->ns = ns; |
| 1816 | cur->name = name; |
| 1817 | if (value != NULL) { |
| 1818 | xmlChar *buffer; |
| 1819 | xmlNodePtr tmp; |
| 1820 | |
| 1821 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1822 | cur->children = xmlStringGetNodeList(doc, buffer); |
| 1823 | cur->last = NULL; |
| 1824 | tmp = cur->children; |
| 1825 | while (tmp != NULL) { |
| 1826 | tmp->parent = (xmlNodePtr) cur; |
| 1827 | if (tmp->next == NULL) |
| 1828 | cur->last = tmp; |
| 1829 | tmp = tmp->next; |
| 1830 | } |
| 1831 | xmlFree(buffer); |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * Add it at the end to preserve parsing order ... |
| 1836 | */ |
| 1837 | if (node != NULL) { |
| 1838 | if (node->properties == NULL) { |
| 1839 | node->properties = cur; |
| 1840 | } else { |
| 1841 | xmlAttrPtr prev = node->properties; |
| 1842 | |
| 1843 | while (prev->next != NULL) prev = prev->next; |
| 1844 | prev->next = cur; |
| 1845 | cur->prev = prev; |
| 1846 | } |
| 1847 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1848 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1849 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1850 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1851 | return(cur); |
| 1852 | } |
| 1853 | |
| 1854 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1855 | * xmlNewDocProp: |
| 1856 | * @doc: the document |
| 1857 | * @name: the name of the attribute |
| 1858 | * @value: the value of the attribute |
| 1859 | * |
| 1860 | * Create a new property carried by a document. |
| 1861 | * Returns a pointer to the attribute |
| 1862 | */ |
| 1863 | xmlAttrPtr |
| 1864 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
| 1865 | xmlAttrPtr cur; |
| 1866 | |
| 1867 | if (name == NULL) { |
| 1868 | #ifdef DEBUG_TREE |
| 1869 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1870 | "xmlNewDocProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1871 | #endif |
| 1872 | return(NULL); |
| 1873 | } |
| 1874 | |
| 1875 | /* |
| 1876 | * Allocate a new property and fill the fields. |
| 1877 | */ |
| 1878 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1879 | if (cur == NULL) { |
| 1880 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1881 | "xmlNewDocProp : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1882 | return(NULL); |
| 1883 | } |
| 1884 | memset(cur, 0, sizeof(xmlAttr)); |
| 1885 | cur->type = XML_ATTRIBUTE_NODE; |
| 1886 | |
| 1887 | cur->name = xmlStrdup(name); |
| 1888 | cur->doc = doc; |
| 1889 | if (value != NULL) { |
| 1890 | xmlNodePtr tmp; |
| 1891 | |
| 1892 | cur->children = xmlStringGetNodeList(doc, value); |
| 1893 | cur->last = NULL; |
| 1894 | |
| 1895 | tmp = cur->children; |
| 1896 | while (tmp != NULL) { |
| 1897 | tmp->parent = (xmlNodePtr) cur; |
| 1898 | if (tmp->next == NULL) |
| 1899 | cur->last = tmp; |
| 1900 | tmp = tmp->next; |
| 1901 | } |
| 1902 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1903 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1904 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1905 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1906 | return(cur); |
| 1907 | } |
| 1908 | |
| 1909 | /** |
| 1910 | * xmlFreePropList: |
| 1911 | * @cur: the first property in the list |
| 1912 | * |
| 1913 | * Free a property and all its siblings, all the children are freed too. |
| 1914 | */ |
| 1915 | void |
| 1916 | xmlFreePropList(xmlAttrPtr cur) { |
| 1917 | xmlAttrPtr next; |
| 1918 | if (cur == NULL) { |
| 1919 | #ifdef DEBUG_TREE |
| 1920 | xmlGenericError(xmlGenericErrorContext, |
| 1921 | "xmlFreePropList : property == NULL\n"); |
| 1922 | #endif |
| 1923 | return; |
| 1924 | } |
| 1925 | while (cur != NULL) { |
| 1926 | next = cur->next; |
| 1927 | xmlFreeProp(cur); |
| 1928 | cur = next; |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | /** |
| 1933 | * xmlFreeProp: |
| 1934 | * @cur: an attribute |
| 1935 | * |
| 1936 | * Free one attribute, all the content is freed too |
| 1937 | */ |
| 1938 | void |
| 1939 | xmlFreeProp(xmlAttrPtr cur) { |
| 1940 | if (cur == NULL) { |
| 1941 | #ifdef DEBUG_TREE |
| 1942 | xmlGenericError(xmlGenericErrorContext, |
| 1943 | "xmlFreeProp : property == NULL\n"); |
| 1944 | #endif |
| 1945 | return; |
| 1946 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1947 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1948 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1949 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1950 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1951 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1952 | if ((cur->parent != NULL) && (cur->parent->doc != NULL) && |
| 1953 | ((cur->parent->doc->intSubset != NULL) || |
| 1954 | (cur->parent->doc->extSubset != NULL))) { |
| 1955 | if (xmlIsID(cur->parent->doc, cur->parent, cur)) |
| 1956 | xmlRemoveID(cur->parent->doc, cur); |
| 1957 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1958 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 1959 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1960 | xmlFree(cur); |
| 1961 | } |
| 1962 | |
| 1963 | /** |
| 1964 | * xmlRemoveProp: |
| 1965 | * @cur: an attribute |
| 1966 | * |
| 1967 | * Unlink and free one attribute, all the content is freed too |
| 1968 | * Note this doesn't work for namespace definition attributes |
| 1969 | * |
| 1970 | * Returns 0 if success and -1 in case of error. |
| 1971 | */ |
| 1972 | int |
| 1973 | xmlRemoveProp(xmlAttrPtr cur) { |
| 1974 | xmlAttrPtr tmp; |
| 1975 | if (cur == NULL) { |
| 1976 | #ifdef DEBUG_TREE |
| 1977 | xmlGenericError(xmlGenericErrorContext, |
| 1978 | "xmlRemoveProp : cur == NULL\n"); |
| 1979 | #endif |
| 1980 | return(-1); |
| 1981 | } |
| 1982 | if (cur->parent == NULL) { |
| 1983 | #ifdef DEBUG_TREE |
| 1984 | xmlGenericError(xmlGenericErrorContext, |
| 1985 | "xmlRemoveProp : cur->parent == NULL\n"); |
| 1986 | #endif |
| 1987 | return(-1); |
| 1988 | } |
| 1989 | tmp = cur->parent->properties; |
| 1990 | if (tmp == cur) { |
| 1991 | cur->parent->properties = cur->next; |
| 1992 | xmlFreeProp(cur); |
| 1993 | return(0); |
| 1994 | } |
| 1995 | while (tmp != NULL) { |
| 1996 | if (tmp->next == cur) { |
| 1997 | tmp->next = cur->next; |
| 1998 | if (tmp->next != NULL) |
| 1999 | tmp->next->prev = tmp; |
| 2000 | xmlFreeProp(cur); |
| 2001 | return(0); |
| 2002 | } |
| 2003 | tmp = tmp->next; |
| 2004 | } |
| 2005 | #ifdef DEBUG_TREE |
| 2006 | xmlGenericError(xmlGenericErrorContext, |
| 2007 | "xmlRemoveProp : attribute not owned by its node\n"); |
| 2008 | #endif |
| 2009 | return(-1); |
| 2010 | } |
| 2011 | |
| 2012 | /** |
| 2013 | * xmlNewPI: |
| 2014 | * @name: the processing instruction name |
| 2015 | * @content: the PI content |
| 2016 | * |
| 2017 | * Creation of a processing instruction element. |
| 2018 | * Returns a pointer to the new node object. |
| 2019 | */ |
| 2020 | xmlNodePtr |
| 2021 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
| 2022 | xmlNodePtr cur; |
| 2023 | |
| 2024 | if (name == NULL) { |
| 2025 | #ifdef DEBUG_TREE |
| 2026 | xmlGenericError(xmlGenericErrorContext, |
| 2027 | "xmlNewPI : name == NULL\n"); |
| 2028 | #endif |
| 2029 | return(NULL); |
| 2030 | } |
| 2031 | |
| 2032 | /* |
| 2033 | * Allocate a new node and fill the fields. |
| 2034 | */ |
| 2035 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2036 | if (cur == NULL) { |
| 2037 | xmlGenericError(xmlGenericErrorContext, |
| 2038 | "xmlNewPI : malloc failed\n"); |
| 2039 | return(NULL); |
| 2040 | } |
| 2041 | memset(cur, 0, sizeof(xmlNode)); |
| 2042 | cur->type = XML_PI_NODE; |
| 2043 | |
| 2044 | cur->name = xmlStrdup(name); |
| 2045 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2046 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2047 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2048 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2049 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2050 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2051 | return(cur); |
| 2052 | } |
| 2053 | |
| 2054 | /** |
| 2055 | * xmlNewNode: |
| 2056 | * @ns: namespace if any |
| 2057 | * @name: the node name |
| 2058 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2059 | * Creation of a new node element. @ns is optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2060 | * |
| 2061 | * Returns a pointer to the new node object. |
| 2062 | */ |
| 2063 | xmlNodePtr |
| 2064 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 2065 | xmlNodePtr cur; |
| 2066 | |
| 2067 | if (name == NULL) { |
| 2068 | #ifdef DEBUG_TREE |
| 2069 | xmlGenericError(xmlGenericErrorContext, |
| 2070 | "xmlNewNode : name == NULL\n"); |
| 2071 | #endif |
| 2072 | return(NULL); |
| 2073 | } |
| 2074 | |
| 2075 | /* |
| 2076 | * Allocate a new node and fill the fields. |
| 2077 | */ |
| 2078 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2079 | if (cur == NULL) { |
| 2080 | xmlGenericError(xmlGenericErrorContext, |
| 2081 | "xmlNewNode : malloc failed\n"); |
| 2082 | return(NULL); |
| 2083 | } |
| 2084 | memset(cur, 0, sizeof(xmlNode)); |
| 2085 | cur->type = XML_ELEMENT_NODE; |
| 2086 | |
| 2087 | cur->name = xmlStrdup(name); |
| 2088 | cur->ns = ns; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2089 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2090 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2091 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2092 | return(cur); |
| 2093 | } |
| 2094 | |
| 2095 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2096 | * xmlNewNodeEatName: |
| 2097 | * @ns: namespace if any |
| 2098 | * @name: the node name |
| 2099 | * |
| 2100 | * Creation of a new node element. @ns is optional (NULL). |
| 2101 | * |
| 2102 | * Returns a pointer to the new node object. |
| 2103 | */ |
| 2104 | xmlNodePtr |
| 2105 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
| 2106 | xmlNodePtr cur; |
| 2107 | |
| 2108 | if (name == NULL) { |
| 2109 | #ifdef DEBUG_TREE |
| 2110 | xmlGenericError(xmlGenericErrorContext, |
| 2111 | "xmlNewNode : name == NULL\n"); |
| 2112 | #endif |
| 2113 | return(NULL); |
| 2114 | } |
| 2115 | |
| 2116 | /* |
| 2117 | * Allocate a new node and fill the fields. |
| 2118 | */ |
| 2119 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2120 | if (cur == NULL) { |
| 2121 | xmlGenericError(xmlGenericErrorContext, |
| 2122 | "xmlNewNode : malloc failed\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2123 | xmlFree(name); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2124 | return(NULL); |
| 2125 | } |
| 2126 | memset(cur, 0, sizeof(xmlNode)); |
| 2127 | cur->type = XML_ELEMENT_NODE; |
| 2128 | |
| 2129 | cur->name = name; |
| 2130 | cur->ns = ns; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2131 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2132 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2133 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2134 | return(cur); |
| 2135 | } |
| 2136 | |
| 2137 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2138 | * xmlNewDocNode: |
| 2139 | * @doc: the document |
| 2140 | * @ns: namespace if any |
| 2141 | * @name: the node name |
| 2142 | * @content: the XML text content if any |
| 2143 | * |
| 2144 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2145 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2146 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2147 | * references, but XML special chars need to be escaped first by using |
| 2148 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2149 | * need entities support. |
| 2150 | * |
| 2151 | * Returns a pointer to the new node object. |
| 2152 | */ |
| 2153 | xmlNodePtr |
| 2154 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2155 | const xmlChar *name, const xmlChar *content) { |
| 2156 | xmlNodePtr cur; |
| 2157 | |
| 2158 | cur = xmlNewNode(ns, name); |
| 2159 | if (cur != NULL) { |
| 2160 | cur->doc = doc; |
| 2161 | if (content != NULL) { |
| 2162 | cur->children = xmlStringGetNodeList(doc, content); |
| 2163 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2164 | } |
| 2165 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2166 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2167 | return(cur); |
| 2168 | } |
| 2169 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2170 | /** |
| 2171 | * xmlNewDocNodeEatName: |
| 2172 | * @doc: the document |
| 2173 | * @ns: namespace if any |
| 2174 | * @name: the node name |
| 2175 | * @content: the XML text content if any |
| 2176 | * |
| 2177 | * Creation of a new node element within a document. @ns and @content |
| 2178 | * are optional (NULL). |
| 2179 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2180 | * references, but XML special chars need to be escaped first by using |
| 2181 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2182 | * need entities support. |
| 2183 | * |
| 2184 | * Returns a pointer to the new node object. |
| 2185 | */ |
| 2186 | xmlNodePtr |
| 2187 | xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns, |
| 2188 | xmlChar *name, const xmlChar *content) { |
| 2189 | xmlNodePtr cur; |
| 2190 | |
| 2191 | cur = xmlNewNodeEatName(ns, name); |
| 2192 | if (cur != NULL) { |
| 2193 | cur->doc = doc; |
| 2194 | if (content != NULL) { |
| 2195 | cur->children = xmlStringGetNodeList(doc, content); |
| 2196 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2197 | } |
| 2198 | } |
| 2199 | return(cur); |
| 2200 | } |
| 2201 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2202 | |
| 2203 | /** |
| 2204 | * xmlNewDocRawNode: |
| 2205 | * @doc: the document |
| 2206 | * @ns: namespace if any |
| 2207 | * @name: the node name |
| 2208 | * @content: the text content if any |
| 2209 | * |
| 2210 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2211 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2212 | * |
| 2213 | * Returns a pointer to the new node object. |
| 2214 | */ |
| 2215 | xmlNodePtr |
| 2216 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2217 | const xmlChar *name, const xmlChar *content) { |
| 2218 | xmlNodePtr cur; |
| 2219 | |
| 2220 | cur = xmlNewNode(ns, name); |
| 2221 | if (cur != NULL) { |
| 2222 | cur->doc = doc; |
| 2223 | if (content != NULL) { |
| 2224 | cur->children = xmlNewDocText(doc, content); |
| 2225 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2226 | } |
| 2227 | } |
| 2228 | return(cur); |
| 2229 | } |
| 2230 | |
| 2231 | /** |
| 2232 | * xmlNewDocFragment: |
| 2233 | * @doc: the document owning the fragment |
| 2234 | * |
| 2235 | * Creation of a new Fragment node. |
| 2236 | * Returns a pointer to the new node object. |
| 2237 | */ |
| 2238 | xmlNodePtr |
| 2239 | xmlNewDocFragment(xmlDocPtr doc) { |
| 2240 | xmlNodePtr cur; |
| 2241 | |
| 2242 | /* |
| 2243 | * Allocate a new DocumentFragment node and fill the fields. |
| 2244 | */ |
| 2245 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2246 | if (cur == NULL) { |
| 2247 | xmlGenericError(xmlGenericErrorContext, |
| 2248 | "xmlNewDocFragment : malloc failed\n"); |
| 2249 | return(NULL); |
| 2250 | } |
| 2251 | memset(cur, 0, sizeof(xmlNode)); |
| 2252 | cur->type = XML_DOCUMENT_FRAG_NODE; |
| 2253 | |
| 2254 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2255 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2256 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2257 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2258 | return(cur); |
| 2259 | } |
| 2260 | |
| 2261 | /** |
| 2262 | * xmlNewText: |
| 2263 | * @content: the text content |
| 2264 | * |
| 2265 | * Creation of a new text node. |
| 2266 | * Returns a pointer to the new node object. |
| 2267 | */ |
| 2268 | xmlNodePtr |
| 2269 | xmlNewText(const xmlChar *content) { |
| 2270 | xmlNodePtr cur; |
| 2271 | |
| 2272 | /* |
| 2273 | * Allocate a new node and fill the fields. |
| 2274 | */ |
| 2275 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2276 | if (cur == NULL) { |
| 2277 | xmlGenericError(xmlGenericErrorContext, |
| 2278 | "xmlNewText : malloc failed\n"); |
| 2279 | return(NULL); |
| 2280 | } |
| 2281 | memset(cur, 0, sizeof(xmlNode)); |
| 2282 | cur->type = XML_TEXT_NODE; |
| 2283 | |
| 2284 | cur->name = xmlStringText; |
| 2285 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2286 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2287 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2288 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2289 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2290 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2291 | return(cur); |
| 2292 | } |
| 2293 | |
| 2294 | /** |
| 2295 | * xmlNewTextChild: |
| 2296 | * @parent: the parent node |
| 2297 | * @ns: a namespace if any |
| 2298 | * @name: the name of the child |
| 2299 | * @content: the text content of the child if any. |
| 2300 | * |
| 2301 | * Creation of a new child element, added at the end of @parent children list. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2302 | * @ns and @content parameters are optional (NULL). If content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2303 | * a child TEXT node will be created containing the string content. |
| 2304 | * |
| 2305 | * Returns a pointer to the new node object. |
| 2306 | */ |
| 2307 | xmlNodePtr |
| 2308 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2309 | const xmlChar *name, const xmlChar *content) { |
| 2310 | xmlNodePtr cur, prev; |
| 2311 | |
| 2312 | if (parent == NULL) { |
| 2313 | #ifdef DEBUG_TREE |
| 2314 | xmlGenericError(xmlGenericErrorContext, |
| 2315 | "xmlNewTextChild : parent == NULL\n"); |
| 2316 | #endif |
| 2317 | return(NULL); |
| 2318 | } |
| 2319 | |
| 2320 | if (name == NULL) { |
| 2321 | #ifdef DEBUG_TREE |
| 2322 | xmlGenericError(xmlGenericErrorContext, |
| 2323 | "xmlNewTextChild : name == NULL\n"); |
| 2324 | #endif |
| 2325 | return(NULL); |
| 2326 | } |
| 2327 | |
| 2328 | /* |
| 2329 | * Allocate a new node |
| 2330 | */ |
| 2331 | if (ns == NULL) |
| 2332 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 2333 | else |
| 2334 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 2335 | if (cur == NULL) return(NULL); |
| 2336 | |
| 2337 | /* |
| 2338 | * add the new element at the end of the children list. |
| 2339 | */ |
| 2340 | cur->type = XML_ELEMENT_NODE; |
| 2341 | cur->parent = parent; |
| 2342 | cur->doc = parent->doc; |
| 2343 | if (parent->children == NULL) { |
| 2344 | parent->children = cur; |
| 2345 | parent->last = cur; |
| 2346 | } else { |
| 2347 | prev = parent->last; |
| 2348 | prev->next = cur; |
| 2349 | cur->prev = prev; |
| 2350 | parent->last = cur; |
| 2351 | } |
| 2352 | |
| 2353 | return(cur); |
| 2354 | } |
| 2355 | |
| 2356 | /** |
| 2357 | * xmlNewCharRef: |
| 2358 | * @doc: the document |
| 2359 | * @name: the char ref string, starting with # or "&# ... ;" |
| 2360 | * |
| 2361 | * Creation of a new character reference node. |
| 2362 | * Returns a pointer to the new node object. |
| 2363 | */ |
| 2364 | xmlNodePtr |
| 2365 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 2366 | xmlNodePtr cur; |
| 2367 | |
| 2368 | /* |
| 2369 | * Allocate a new node and fill the fields. |
| 2370 | */ |
| 2371 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2372 | if (cur == NULL) { |
| 2373 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2374 | "xmlNewCharRef : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2375 | return(NULL); |
| 2376 | } |
| 2377 | memset(cur, 0, sizeof(xmlNode)); |
| 2378 | cur->type = XML_ENTITY_REF_NODE; |
| 2379 | |
| 2380 | cur->doc = doc; |
| 2381 | if (name[0] == '&') { |
| 2382 | int len; |
| 2383 | name++; |
| 2384 | len = xmlStrlen(name); |
| 2385 | if (name[len - 1] == ';') |
| 2386 | cur->name = xmlStrndup(name, len - 1); |
| 2387 | else |
| 2388 | cur->name = xmlStrndup(name, len); |
| 2389 | } else |
| 2390 | cur->name = xmlStrdup(name); |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2391 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2392 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2393 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2394 | return(cur); |
| 2395 | } |
| 2396 | |
| 2397 | /** |
| 2398 | * xmlNewReference: |
| 2399 | * @doc: the document |
| 2400 | * @name: the reference name, or the reference string with & and ; |
| 2401 | * |
| 2402 | * Creation of a new reference node. |
| 2403 | * Returns a pointer to the new node object. |
| 2404 | */ |
| 2405 | xmlNodePtr |
| 2406 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 2407 | xmlNodePtr cur; |
| 2408 | xmlEntityPtr ent; |
| 2409 | |
| 2410 | /* |
| 2411 | * Allocate a new node and fill the fields. |
| 2412 | */ |
| 2413 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2414 | if (cur == NULL) { |
| 2415 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2416 | "xmlNewReference : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2417 | return(NULL); |
| 2418 | } |
| 2419 | memset(cur, 0, sizeof(xmlNode)); |
| 2420 | cur->type = XML_ENTITY_REF_NODE; |
| 2421 | |
| 2422 | cur->doc = doc; |
| 2423 | if (name[0] == '&') { |
| 2424 | int len; |
| 2425 | name++; |
| 2426 | len = xmlStrlen(name); |
| 2427 | if (name[len - 1] == ';') |
| 2428 | cur->name = xmlStrndup(name, len - 1); |
| 2429 | else |
| 2430 | cur->name = xmlStrndup(name, len); |
| 2431 | } else |
| 2432 | cur->name = xmlStrdup(name); |
| 2433 | |
| 2434 | ent = xmlGetDocEntity(doc, cur->name); |
| 2435 | if (ent != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2436 | cur->content = ent->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2437 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2438 | * The parent pointer in entity is a DTD pointer and thus is NOT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2439 | * updated. Not sure if this is 100% correct. |
| 2440 | * -George |
| 2441 | */ |
| 2442 | cur->children = (xmlNodePtr) ent; |
| 2443 | cur->last = (xmlNodePtr) ent; |
| 2444 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2445 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2446 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2447 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2448 | return(cur); |
| 2449 | } |
| 2450 | |
| 2451 | /** |
| 2452 | * xmlNewDocText: |
| 2453 | * @doc: the document |
| 2454 | * @content: the text content |
| 2455 | * |
| 2456 | * Creation of a new text node within a document. |
| 2457 | * Returns a pointer to the new node object. |
| 2458 | */ |
| 2459 | xmlNodePtr |
| 2460 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 2461 | xmlNodePtr cur; |
| 2462 | |
| 2463 | cur = xmlNewText(content); |
| 2464 | if (cur != NULL) cur->doc = doc; |
| 2465 | return(cur); |
| 2466 | } |
| 2467 | |
| 2468 | /** |
| 2469 | * xmlNewTextLen: |
| 2470 | * @content: the text content |
| 2471 | * @len: the text len. |
| 2472 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2473 | * Creation of a new text node with an extra parameter for the content's length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2474 | * Returns a pointer to the new node object. |
| 2475 | */ |
| 2476 | xmlNodePtr |
| 2477 | xmlNewTextLen(const xmlChar *content, int len) { |
| 2478 | xmlNodePtr cur; |
| 2479 | |
| 2480 | /* |
| 2481 | * Allocate a new node and fill the fields. |
| 2482 | */ |
| 2483 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2484 | if (cur == NULL) { |
| 2485 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2486 | "xmlNewTextLen : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2487 | return(NULL); |
| 2488 | } |
| 2489 | memset(cur, 0, sizeof(xmlNode)); |
| 2490 | cur->type = XML_TEXT_NODE; |
| 2491 | |
| 2492 | cur->name = xmlStringText; |
| 2493 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2494 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2495 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2496 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2497 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2498 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2499 | return(cur); |
| 2500 | } |
| 2501 | |
| 2502 | /** |
| 2503 | * xmlNewDocTextLen: |
| 2504 | * @doc: the document |
| 2505 | * @content: the text content |
| 2506 | * @len: the text len. |
| 2507 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2508 | * Creation of a new text node with an extra content length parameter. The |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2509 | * text node pertain to a given document. |
| 2510 | * Returns a pointer to the new node object. |
| 2511 | */ |
| 2512 | xmlNodePtr |
| 2513 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2514 | xmlNodePtr cur; |
| 2515 | |
| 2516 | cur = xmlNewTextLen(content, len); |
| 2517 | if (cur != NULL) cur->doc = doc; |
| 2518 | return(cur); |
| 2519 | } |
| 2520 | |
| 2521 | /** |
| 2522 | * xmlNewComment: |
| 2523 | * @content: the comment content |
| 2524 | * |
| 2525 | * Creation of a new node containing a comment. |
| 2526 | * Returns a pointer to the new node object. |
| 2527 | */ |
| 2528 | xmlNodePtr |
| 2529 | xmlNewComment(const xmlChar *content) { |
| 2530 | xmlNodePtr cur; |
| 2531 | |
| 2532 | /* |
| 2533 | * Allocate a new node and fill the fields. |
| 2534 | */ |
| 2535 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2536 | if (cur == NULL) { |
| 2537 | xmlGenericError(xmlGenericErrorContext, |
| 2538 | "xmlNewComment : malloc failed\n"); |
| 2539 | return(NULL); |
| 2540 | } |
| 2541 | memset(cur, 0, sizeof(xmlNode)); |
| 2542 | cur->type = XML_COMMENT_NODE; |
| 2543 | |
| 2544 | cur->name = xmlStringComment; |
| 2545 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2546 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2547 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2548 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2549 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2550 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2551 | return(cur); |
| 2552 | } |
| 2553 | |
| 2554 | /** |
| 2555 | * xmlNewCDataBlock: |
| 2556 | * @doc: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2557 | * @content: the CDATA block content content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2558 | * @len: the length of the block |
| 2559 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2560 | * Creation of a new node containing a CDATA block. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2561 | * Returns a pointer to the new node object. |
| 2562 | */ |
| 2563 | xmlNodePtr |
| 2564 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2565 | xmlNodePtr cur; |
| 2566 | |
| 2567 | /* |
| 2568 | * Allocate a new node and fill the fields. |
| 2569 | */ |
| 2570 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2571 | if (cur == NULL) { |
| 2572 | xmlGenericError(xmlGenericErrorContext, |
| 2573 | "xmlNewCDataBlock : malloc failed\n"); |
| 2574 | return(NULL); |
| 2575 | } |
| 2576 | memset(cur, 0, sizeof(xmlNode)); |
| 2577 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2578 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2579 | |
| 2580 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2581 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2582 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2583 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2584 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2585 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2586 | return(cur); |
| 2587 | } |
| 2588 | |
| 2589 | /** |
| 2590 | * xmlNewDocComment: |
| 2591 | * @doc: the document |
| 2592 | * @content: the comment content |
| 2593 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2594 | * Creation of a new node containing a comment within a document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2595 | * Returns a pointer to the new node object. |
| 2596 | */ |
| 2597 | xmlNodePtr |
| 2598 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 2599 | xmlNodePtr cur; |
| 2600 | |
| 2601 | cur = xmlNewComment(content); |
| 2602 | if (cur != NULL) cur->doc = doc; |
| 2603 | return(cur); |
| 2604 | } |
| 2605 | |
| 2606 | /** |
| 2607 | * xmlSetTreeDoc: |
| 2608 | * @tree: the top element |
| 2609 | * @doc: the document |
| 2610 | * |
| 2611 | * update all nodes under the tree to point to the right document |
| 2612 | */ |
| 2613 | void |
| 2614 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2615 | xmlAttrPtr prop; |
| 2616 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2617 | if (tree == NULL) |
| 2618 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2619 | if (tree->doc != doc) { |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2620 | if(tree->type == XML_ELEMENT_NODE) { |
| 2621 | prop = tree->properties; |
| 2622 | while (prop != NULL) { |
| 2623 | prop->doc = doc; |
| 2624 | xmlSetListDoc(prop->children, doc); |
| 2625 | prop = prop->next; |
| 2626 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2627 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2628 | if (tree->children != NULL) |
| 2629 | xmlSetListDoc(tree->children, doc); |
| 2630 | tree->doc = doc; |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | /** |
| 2635 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2636 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2637 | * @doc: the document |
| 2638 | * |
| 2639 | * update all nodes in the list to point to the right document |
| 2640 | */ |
| 2641 | void |
| 2642 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2643 | xmlNodePtr cur; |
| 2644 | |
| 2645 | if (list == NULL) |
| 2646 | return; |
| 2647 | cur = list; |
| 2648 | while (cur != NULL) { |
| 2649 | if (cur->doc != doc) |
| 2650 | xmlSetTreeDoc(cur, doc); |
| 2651 | cur = cur->next; |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | |
| 2656 | /** |
| 2657 | * xmlNewChild: |
| 2658 | * @parent: the parent node |
| 2659 | * @ns: a namespace if any |
| 2660 | * @name: the name of the child |
| 2661 | * @content: the XML content of the child if any. |
| 2662 | * |
| 2663 | * Creation of a new child element, added at the end of @parent children list. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2664 | * @ns and @content parameters are optional (NULL). If content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2665 | * a child list containing the TEXTs and ENTITY_REFs node will be created. |
| 2666 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2667 | * references, but XML special chars need to be escaped first by using |
| 2668 | * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities |
| 2669 | * support is not needed. |
| 2670 | * |
| 2671 | * Returns a pointer to the new node object. |
| 2672 | */ |
| 2673 | xmlNodePtr |
| 2674 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2675 | const xmlChar *name, const xmlChar *content) { |
| 2676 | xmlNodePtr cur, prev; |
| 2677 | |
| 2678 | if (parent == NULL) { |
| 2679 | #ifdef DEBUG_TREE |
| 2680 | xmlGenericError(xmlGenericErrorContext, |
| 2681 | "xmlNewChild : parent == NULL\n"); |
| 2682 | #endif |
| 2683 | return(NULL); |
| 2684 | } |
| 2685 | |
| 2686 | if (name == NULL) { |
| 2687 | #ifdef DEBUG_TREE |
| 2688 | xmlGenericError(xmlGenericErrorContext, |
| 2689 | "xmlNewChild : name == NULL\n"); |
| 2690 | #endif |
| 2691 | return(NULL); |
| 2692 | } |
| 2693 | |
| 2694 | /* |
| 2695 | * Allocate a new node |
| 2696 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2697 | if (parent->type == XML_ELEMENT_NODE) { |
| 2698 | if (ns == NULL) |
| 2699 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2700 | else |
| 2701 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2702 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2703 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2704 | if (ns == NULL) |
| 2705 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2706 | else |
| 2707 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2708 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2709 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2710 | } else { |
| 2711 | return(NULL); |
| 2712 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2713 | if (cur == NULL) return(NULL); |
| 2714 | |
| 2715 | /* |
| 2716 | * add the new element at the end of the children list. |
| 2717 | */ |
| 2718 | cur->type = XML_ELEMENT_NODE; |
| 2719 | cur->parent = parent; |
| 2720 | cur->doc = parent->doc; |
| 2721 | if (parent->children == NULL) { |
| 2722 | parent->children = cur; |
| 2723 | parent->last = cur; |
| 2724 | } else { |
| 2725 | prev = parent->last; |
| 2726 | prev->next = cur; |
| 2727 | cur->prev = prev; |
| 2728 | parent->last = cur; |
| 2729 | } |
| 2730 | |
| 2731 | return(cur); |
| 2732 | } |
| 2733 | |
| 2734 | /** |
| 2735 | * xmlAddNextSibling: |
| 2736 | * @cur: the child node |
| 2737 | * @elem: the new node |
| 2738 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2739 | * Add a new node @elem as the next sibling of @cur |
| 2740 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2741 | * first unlinked from its existing context. |
| 2742 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2743 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2744 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2745 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2746 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2747 | */ |
| 2748 | xmlNodePtr |
| 2749 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2750 | if (cur == NULL) { |
| 2751 | #ifdef DEBUG_TREE |
| 2752 | xmlGenericError(xmlGenericErrorContext, |
| 2753 | "xmlAddNextSibling : cur == NULL\n"); |
| 2754 | #endif |
| 2755 | return(NULL); |
| 2756 | } |
| 2757 | if (elem == NULL) { |
| 2758 | #ifdef DEBUG_TREE |
| 2759 | xmlGenericError(xmlGenericErrorContext, |
| 2760 | "xmlAddNextSibling : elem == NULL\n"); |
| 2761 | #endif |
| 2762 | return(NULL); |
| 2763 | } |
| 2764 | |
| 2765 | xmlUnlinkNode(elem); |
| 2766 | |
| 2767 | if (elem->type == XML_TEXT_NODE) { |
| 2768 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2769 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2770 | xmlFreeNode(elem); |
| 2771 | return(cur); |
| 2772 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2773 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 2774 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2775 | xmlChar *tmp; |
| 2776 | |
| 2777 | tmp = xmlStrdup(elem->content); |
| 2778 | tmp = xmlStrcat(tmp, cur->next->content); |
| 2779 | xmlNodeSetContent(cur->next, tmp); |
| 2780 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2781 | xmlFreeNode(elem); |
| 2782 | return(cur->next); |
| 2783 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2784 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2785 | /* check if an attribute with the same name exists */ |
| 2786 | xmlAttrPtr attr; |
| 2787 | |
| 2788 | if (elem->ns == NULL) |
| 2789 | attr = xmlHasProp(cur->parent, elem->name); |
| 2790 | else |
| 2791 | attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href); |
| 2792 | if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) { |
| 2793 | /* different instance, destroy it (attributes must be unique) */ |
| 2794 | xmlFreeProp(attr); |
| 2795 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | if (elem->doc != cur->doc) { |
| 2799 | xmlSetTreeDoc(elem, cur->doc); |
| 2800 | } |
| 2801 | elem->parent = cur->parent; |
| 2802 | elem->prev = cur; |
| 2803 | elem->next = cur->next; |
| 2804 | cur->next = elem; |
| 2805 | if (elem->next != NULL) |
| 2806 | elem->next->prev = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2807 | if ((elem->parent != NULL) && (elem->parent->last == cur) && (elem->type != XML_ATTRIBUTE_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2808 | elem->parent->last = elem; |
| 2809 | return(elem); |
| 2810 | } |
| 2811 | |
| 2812 | /** |
| 2813 | * xmlAddPrevSibling: |
| 2814 | * @cur: the child node |
| 2815 | * @elem: the new node |
| 2816 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2817 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2818 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2819 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2820 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2821 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2822 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2823 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2824 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2825 | */ |
| 2826 | xmlNodePtr |
| 2827 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2828 | if (cur == NULL) { |
| 2829 | #ifdef DEBUG_TREE |
| 2830 | xmlGenericError(xmlGenericErrorContext, |
| 2831 | "xmlAddPrevSibling : cur == NULL\n"); |
| 2832 | #endif |
| 2833 | return(NULL); |
| 2834 | } |
| 2835 | if (elem == NULL) { |
| 2836 | #ifdef DEBUG_TREE |
| 2837 | xmlGenericError(xmlGenericErrorContext, |
| 2838 | "xmlAddPrevSibling : elem == NULL\n"); |
| 2839 | #endif |
| 2840 | return(NULL); |
| 2841 | } |
| 2842 | |
| 2843 | xmlUnlinkNode(elem); |
| 2844 | |
| 2845 | if (elem->type == XML_TEXT_NODE) { |
| 2846 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2847 | xmlChar *tmp; |
| 2848 | |
| 2849 | tmp = xmlStrdup(elem->content); |
| 2850 | tmp = xmlStrcat(tmp, cur->content); |
| 2851 | xmlNodeSetContent(cur, tmp); |
| 2852 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2853 | xmlFreeNode(elem); |
| 2854 | return(cur); |
| 2855 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2856 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 2857 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2858 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2859 | xmlFreeNode(elem); |
| 2860 | return(cur->prev); |
| 2861 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2862 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2863 | /* check if an attribute with the same name exists */ |
| 2864 | xmlAttrPtr attr; |
| 2865 | |
| 2866 | if (elem->ns == NULL) |
| 2867 | attr = xmlHasProp(cur->parent, elem->name); |
| 2868 | else |
| 2869 | attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href); |
| 2870 | if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) { |
| 2871 | /* different instance, destroy it (attributes must be unique) */ |
| 2872 | xmlFreeProp(attr); |
| 2873 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | if (elem->doc != cur->doc) { |
| 2877 | xmlSetTreeDoc(elem, cur->doc); |
| 2878 | } |
| 2879 | elem->parent = cur->parent; |
| 2880 | elem->next = cur; |
| 2881 | elem->prev = cur->prev; |
| 2882 | cur->prev = elem; |
| 2883 | if (elem->prev != NULL) |
| 2884 | elem->prev->next = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2885 | if (elem->parent != NULL) { |
| 2886 | if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2887 | if (elem->parent->properties == (xmlAttrPtr) cur) { |
| 2888 | elem->parent->properties = (xmlAttrPtr) elem; |
| 2889 | } |
| 2890 | } else { |
| 2891 | if (elem->parent->children == cur) { |
| 2892 | elem->parent->children = elem; |
| 2893 | } |
| 2894 | } |
| 2895 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2896 | return(elem); |
| 2897 | } |
| 2898 | |
| 2899 | /** |
| 2900 | * xmlAddSibling: |
| 2901 | * @cur: the child node |
| 2902 | * @elem: the new node |
| 2903 | * |
| 2904 | * Add a new element @elem to the list of siblings of @cur |
| 2905 | * merging adjacent TEXT nodes (@elem may be freed) |
| 2906 | * If the new element was already inserted in a document it is |
| 2907 | * first unlinked from its existing context. |
| 2908 | * |
| 2909 | * Returns the new element or NULL in case of error. |
| 2910 | */ |
| 2911 | xmlNodePtr |
| 2912 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2913 | xmlNodePtr parent; |
| 2914 | |
| 2915 | if (cur == NULL) { |
| 2916 | #ifdef DEBUG_TREE |
| 2917 | xmlGenericError(xmlGenericErrorContext, |
| 2918 | "xmlAddSibling : cur == NULL\n"); |
| 2919 | #endif |
| 2920 | return(NULL); |
| 2921 | } |
| 2922 | |
| 2923 | if (elem == NULL) { |
| 2924 | #ifdef DEBUG_TREE |
| 2925 | xmlGenericError(xmlGenericErrorContext, |
| 2926 | "xmlAddSibling : elem == NULL\n"); |
| 2927 | #endif |
| 2928 | return(NULL); |
| 2929 | } |
| 2930 | |
| 2931 | /* |
| 2932 | * Constant time is we can rely on the ->parent->last to find |
| 2933 | * the last sibling. |
| 2934 | */ |
| 2935 | if ((cur->parent != NULL) && |
| 2936 | (cur->parent->children != NULL) && |
| 2937 | (cur->parent->last != NULL) && |
| 2938 | (cur->parent->last->next == NULL)) { |
| 2939 | cur = cur->parent->last; |
| 2940 | } else { |
| 2941 | while (cur->next != NULL) cur = cur->next; |
| 2942 | } |
| 2943 | |
| 2944 | xmlUnlinkNode(elem); |
| 2945 | |
| 2946 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2947 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2948 | xmlFreeNode(elem); |
| 2949 | return(cur); |
| 2950 | } |
| 2951 | |
| 2952 | if (elem->doc != cur->doc) { |
| 2953 | xmlSetTreeDoc(elem, cur->doc); |
| 2954 | } |
| 2955 | parent = cur->parent; |
| 2956 | elem->prev = cur; |
| 2957 | elem->next = NULL; |
| 2958 | elem->parent = parent; |
| 2959 | cur->next = elem; |
| 2960 | if (parent != NULL) |
| 2961 | parent->last = elem; |
| 2962 | |
| 2963 | return(elem); |
| 2964 | } |
| 2965 | |
| 2966 | /** |
| 2967 | * xmlAddChildList: |
| 2968 | * @parent: the parent node |
| 2969 | * @cur: the first node in the list |
| 2970 | * |
| 2971 | * Add a list of node at the end of the child list of the parent |
| 2972 | * merging adjacent TEXT nodes (@cur may be freed) |
| 2973 | * |
| 2974 | * Returns the last child or NULL in case of error. |
| 2975 | */ |
| 2976 | xmlNodePtr |
| 2977 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 2978 | xmlNodePtr prev; |
| 2979 | |
| 2980 | if (parent == NULL) { |
| 2981 | #ifdef DEBUG_TREE |
| 2982 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2983 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2984 | #endif |
| 2985 | return(NULL); |
| 2986 | } |
| 2987 | |
| 2988 | if (cur == NULL) { |
| 2989 | #ifdef DEBUG_TREE |
| 2990 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2991 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2992 | #endif |
| 2993 | return(NULL); |
| 2994 | } |
| 2995 | |
| 2996 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 2997 | (cur->doc != parent->doc)) { |
| 2998 | #ifdef DEBUG_TREE |
| 2999 | xmlGenericError(xmlGenericErrorContext, |
| 3000 | "Elements moved to a different document\n"); |
| 3001 | #endif |
| 3002 | } |
| 3003 | |
| 3004 | /* |
| 3005 | * add the first element at the end of the children list. |
| 3006 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3007 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3008 | if (parent->children == NULL) { |
| 3009 | parent->children = cur; |
| 3010 | } else { |
| 3011 | /* |
| 3012 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 3013 | */ |
| 3014 | if ((cur->type == XML_TEXT_NODE) && |
| 3015 | (parent->last->type == XML_TEXT_NODE) && |
| 3016 | (cur->name == parent->last->name)) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3017 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3018 | /* |
| 3019 | * if it's the only child, nothing more to be done. |
| 3020 | */ |
| 3021 | if (cur->next == NULL) { |
| 3022 | xmlFreeNode(cur); |
| 3023 | return(parent->last); |
| 3024 | } |
| 3025 | prev = cur; |
| 3026 | cur = cur->next; |
| 3027 | xmlFreeNode(prev); |
| 3028 | } |
| 3029 | prev = parent->last; |
| 3030 | prev->next = cur; |
| 3031 | cur->prev = prev; |
| 3032 | } |
| 3033 | while (cur->next != NULL) { |
| 3034 | cur->parent = parent; |
| 3035 | if (cur->doc != parent->doc) { |
| 3036 | xmlSetTreeDoc(cur, parent->doc); |
| 3037 | } |
| 3038 | cur = cur->next; |
| 3039 | } |
| 3040 | cur->parent = parent; |
| 3041 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 3042 | parent->last = cur; |
| 3043 | |
| 3044 | return(cur); |
| 3045 | } |
| 3046 | |
| 3047 | /** |
| 3048 | * xmlAddChild: |
| 3049 | * @parent: the parent node |
| 3050 | * @cur: the child node |
| 3051 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3052 | * Add a new node to @parent, at the end of the child (or property) list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3053 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3054 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 3055 | * If there is an attribute with equal name, it is first destroyed. |
| 3056 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3057 | * Returns the child or NULL in case of error. |
| 3058 | */ |
| 3059 | xmlNodePtr |
| 3060 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 3061 | xmlNodePtr prev; |
| 3062 | |
| 3063 | if (parent == NULL) { |
| 3064 | #ifdef DEBUG_TREE |
| 3065 | xmlGenericError(xmlGenericErrorContext, |
| 3066 | "xmlAddChild : parent == NULL\n"); |
| 3067 | #endif |
| 3068 | return(NULL); |
| 3069 | } |
| 3070 | |
| 3071 | if (cur == NULL) { |
| 3072 | #ifdef DEBUG_TREE |
| 3073 | xmlGenericError(xmlGenericErrorContext, |
| 3074 | "xmlAddChild : child == NULL\n"); |
| 3075 | #endif |
| 3076 | return(NULL); |
| 3077 | } |
| 3078 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3079 | /* |
| 3080 | * If cur is a TEXT node, merge its content with adjacent TEXT nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3081 | * cur is then freed. |
| 3082 | */ |
| 3083 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3084 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3085 | (parent->content != NULL) && |
| 3086 | (parent != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3087 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3088 | xmlFreeNode(cur); |
| 3089 | return(parent); |
| 3090 | } |
| 3091 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3092 | (parent->last->name == cur->name) && |
| 3093 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3094 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3095 | xmlFreeNode(cur); |
| 3096 | return(parent->last); |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | /* |
| 3101 | * add the new element at the end of the children list. |
| 3102 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3103 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3104 | cur->parent = parent; |
| 3105 | if (cur->doc != parent->doc) { |
| 3106 | xmlSetTreeDoc(cur, parent->doc); |
| 3107 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3108 | /* this check prevents a loop on tree-traversions if a developer |
| 3109 | * tries to add a node to its parent multiple times |
| 3110 | */ |
| 3111 | if (prev == parent) |
| 3112 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3113 | |
| 3114 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3115 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3116 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3117 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3118 | (parent->content != NULL) && |
| 3119 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3120 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3121 | xmlFreeNode(cur); |
| 3122 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3123 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3124 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3125 | if (parent->properties == NULL) { |
| 3126 | parent->properties = (xmlAttrPtr) cur; |
| 3127 | } else { |
| 3128 | /* check if an attribute with the same name exists */ |
| 3129 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3130 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3131 | if (cur->ns == NULL) |
| 3132 | lastattr = xmlHasProp(parent, cur->name); |
| 3133 | else |
| 3134 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
| 3135 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur)) { |
| 3136 | /* different instance, destroy it (attributes must be unique) */ |
| 3137 | xmlFreeProp(lastattr); |
| 3138 | } |
| 3139 | /* find the end */ |
| 3140 | lastattr = parent->properties; |
| 3141 | while (lastattr->next != NULL) { |
| 3142 | lastattr = lastattr->next; |
| 3143 | } |
| 3144 | lastattr->next = (xmlAttrPtr) cur; |
| 3145 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3146 | } |
| 3147 | } else { |
| 3148 | if (parent->children == NULL) { |
| 3149 | parent->children = cur; |
| 3150 | parent->last = cur; |
| 3151 | } else { |
| 3152 | prev = parent->last; |
| 3153 | prev->next = cur; |
| 3154 | cur->prev = prev; |
| 3155 | parent->last = cur; |
| 3156 | } |
| 3157 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3158 | return(cur); |
| 3159 | } |
| 3160 | |
| 3161 | /** |
| 3162 | * xmlGetLastChild: |
| 3163 | * @parent: the parent node |
| 3164 | * |
| 3165 | * Search the last child of a node. |
| 3166 | * Returns the last child or NULL if none. |
| 3167 | */ |
| 3168 | xmlNodePtr |
| 3169 | xmlGetLastChild(xmlNodePtr parent) { |
| 3170 | if (parent == NULL) { |
| 3171 | #ifdef DEBUG_TREE |
| 3172 | xmlGenericError(xmlGenericErrorContext, |
| 3173 | "xmlGetLastChild : parent == NULL\n"); |
| 3174 | #endif |
| 3175 | return(NULL); |
| 3176 | } |
| 3177 | return(parent->last); |
| 3178 | } |
| 3179 | |
| 3180 | /** |
| 3181 | * xmlFreeNodeList: |
| 3182 | * @cur: the first node in the list |
| 3183 | * |
| 3184 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3185 | * the children are freed too. |
| 3186 | */ |
| 3187 | void |
| 3188 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3189 | xmlNodePtr next; |
| 3190 | if (cur == NULL) { |
| 3191 | #ifdef DEBUG_TREE |
| 3192 | xmlGenericError(xmlGenericErrorContext, |
| 3193 | "xmlFreeNodeList : node == NULL\n"); |
| 3194 | #endif |
| 3195 | return; |
| 3196 | } |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3197 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3198 | xmlFreeNsList((xmlNsPtr) cur); |
| 3199 | return; |
| 3200 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3201 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3202 | #ifdef LIBXML_DOCB_ENABLED |
| 3203 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3204 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3205 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3206 | xmlFreeDoc((xmlDocPtr) cur); |
| 3207 | return; |
| 3208 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3209 | while (cur != NULL) { |
| 3210 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3211 | /* unroll to speed up freeing the document */ |
| 3212 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3213 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3214 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3215 | xmlDeregisterNodeDefaultValue(cur); |
| 3216 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3217 | if ((cur->children != NULL) && |
| 3218 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3219 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3220 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3221 | (cur->type == XML_XINCLUDE_START) || |
| 3222 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3223 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3224 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3225 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3226 | (cur->type != XML_XINCLUDE_START) && |
| 3227 | (cur->type != XML_XINCLUDE_END) && |
| 3228 | (cur->type != XML_ENTITY_REF_NODE)) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3229 | if (cur->content != NULL) xmlFree(cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3230 | } |
| 3231 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3232 | (cur->type == XML_XINCLUDE_START) || |
| 3233 | (cur->type == XML_XINCLUDE_END)) && |
| 3234 | (cur->nsDef != NULL)) |
| 3235 | xmlFreeNsList(cur->nsDef); |
| 3236 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3237 | /* |
| 3238 | * When a node is a text node or a comment, it uses a global static |
| 3239 | * variable for the name of the node. |
| 3240 | * |
| 3241 | * The xmlStrEqual comparisons need to be done when (happened with |
| 3242 | * XML::libXML and XML::libXSLT) the library is included twice |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3243 | * statically in the binary and a tree allocated by one occurrence |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3244 | * of the lib gets freed by the other occurrence, in this case |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3245 | * the string addresses compare are not sufficient. |
| 3246 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3247 | if ((cur->name != NULL) && |
| 3248 | (cur->name != xmlStringText) && |
| 3249 | (cur->name != xmlStringTextNoenc) && |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3250 | (cur->name != xmlStringComment)) { |
| 3251 | if (cur->type == XML_TEXT_NODE) { |
| 3252 | if ((!xmlStrEqual(cur->name, xmlStringText)) && |
| 3253 | (!xmlStrEqual(cur->name, xmlStringTextNoenc))) |
| 3254 | xmlFree((char *) cur->name); |
| 3255 | } else if (cur->type == XML_COMMENT_NODE) { |
| 3256 | if (!xmlStrEqual(cur->name, xmlStringComment)) |
| 3257 | xmlFree((char *) cur->name); |
| 3258 | } else |
| 3259 | xmlFree((char *) cur->name); |
| 3260 | } |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3261 | /* TODO : derecursivate this function */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3262 | xmlFree(cur); |
| 3263 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3264 | cur = next; |
| 3265 | } |
| 3266 | } |
| 3267 | |
| 3268 | /** |
| 3269 | * xmlFreeNode: |
| 3270 | * @cur: the node |
| 3271 | * |
| 3272 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3273 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3274 | */ |
| 3275 | void |
| 3276 | xmlFreeNode(xmlNodePtr cur) { |
| 3277 | if (cur == NULL) { |
| 3278 | #ifdef DEBUG_TREE |
| 3279 | xmlGenericError(xmlGenericErrorContext, |
| 3280 | "xmlFreeNode : node == NULL\n"); |
| 3281 | #endif |
| 3282 | return; |
| 3283 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3284 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3285 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3286 | if (cur->type == XML_DTD_NODE) { |
| 3287 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3288 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3289 | } |
| 3290 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3291 | xmlFreeNs((xmlNsPtr) cur); |
| 3292 | return; |
| 3293 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3294 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3295 | xmlFreeProp((xmlAttrPtr) cur); |
| 3296 | return; |
| 3297 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3298 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3299 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3300 | xmlDeregisterNodeDefaultValue(cur); |
| 3301 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3302 | if ((cur->children != NULL) && |
| 3303 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3304 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3305 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3306 | (cur->type == XML_XINCLUDE_START) || |
| 3307 | (cur->type == XML_XINCLUDE_END)) && |
| 3308 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3309 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3310 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3311 | (cur->content != NULL) && |
| 3312 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3313 | (cur->type != XML_XINCLUDE_END) && |
| 3314 | (cur->type != XML_XINCLUDE_START)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3315 | xmlFree(cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3318 | /* |
| 3319 | * When a node is a text node or a comment, it uses a global static |
| 3320 | * variable for the name of the node. |
| 3321 | * |
| 3322 | * The xmlStrEqual comparisons need to be done when (happened with |
| 3323 | * XML::libXML and XML::libXSLT) the library is included twice statically |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3324 | * in the binary and a tree allocated by one occurence of the lib gets |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3325 | * freed by the other occurrence, in this case the string addresses compare |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3326 | * are not sufficient. |
| 3327 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3328 | if ((cur->name != NULL) && |
| 3329 | (cur->name != xmlStringText) && |
| 3330 | (cur->name != xmlStringTextNoenc) && |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3331 | (cur->name != xmlStringComment)) { |
| 3332 | if (cur->type == XML_TEXT_NODE) { |
| 3333 | if ((!xmlStrEqual(cur->name, xmlStringText)) && |
| 3334 | (!xmlStrEqual(cur->name, xmlStringTextNoenc))) |
| 3335 | xmlFree((char *) cur->name); |
| 3336 | } else if (cur->type == XML_COMMENT_NODE) { |
| 3337 | if (!xmlStrEqual(cur->name, xmlStringComment)) |
| 3338 | xmlFree((char *) cur->name); |
| 3339 | } else |
| 3340 | xmlFree((char *) cur->name); |
| 3341 | } |
| 3342 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3343 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3344 | (cur->type == XML_XINCLUDE_START) || |
| 3345 | (cur->type == XML_XINCLUDE_END)) && |
| 3346 | (cur->nsDef != NULL)) |
| 3347 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3348 | xmlFree(cur); |
| 3349 | } |
| 3350 | |
| 3351 | /** |
| 3352 | * xmlUnlinkNode: |
| 3353 | * @cur: the node |
| 3354 | * |
| 3355 | * Unlink a node from it's current context, the node is not freed |
| 3356 | */ |
| 3357 | void |
| 3358 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3359 | if (cur == NULL) { |
| 3360 | #ifdef DEBUG_TREE |
| 3361 | xmlGenericError(xmlGenericErrorContext, |
| 3362 | "xmlUnlinkNode : node == NULL\n"); |
| 3363 | #endif |
| 3364 | return; |
| 3365 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3366 | if (cur->type == XML_DTD_NODE) { |
| 3367 | xmlDocPtr doc; |
| 3368 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3369 | if (doc != NULL) { |
| 3370 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3371 | doc->intSubset = NULL; |
| 3372 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3373 | doc->extSubset = NULL; |
| 3374 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3375 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3376 | if (cur->parent != NULL) { |
| 3377 | xmlNodePtr parent; |
| 3378 | parent = cur->parent; |
| 3379 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3380 | if (parent->properties == (xmlAttrPtr) cur) |
| 3381 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3382 | } else { |
| 3383 | if (parent->children == cur) |
| 3384 | parent->children = cur->next; |
| 3385 | if (parent->last == cur) |
| 3386 | parent->last = cur->prev; |
| 3387 | } |
| 3388 | cur->parent = NULL; |
| 3389 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3390 | if (cur->next != NULL) |
| 3391 | cur->next->prev = cur->prev; |
| 3392 | if (cur->prev != NULL) |
| 3393 | cur->prev->next = cur->next; |
| 3394 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | /** |
| 3398 | * xmlReplaceNode: |
| 3399 | * @old: the old node |
| 3400 | * @cur: the node |
| 3401 | * |
| 3402 | * Unlink the old node from it's current context, prune the new one |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3403 | * at the same place. If @cur was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3404 | * first unlinked from its existing context. |
| 3405 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3406 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3407 | */ |
| 3408 | xmlNodePtr |
| 3409 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
| 3410 | if (old == NULL) { |
| 3411 | #ifdef DEBUG_TREE |
| 3412 | xmlGenericError(xmlGenericErrorContext, |
| 3413 | "xmlReplaceNode : old == NULL\n"); |
| 3414 | #endif |
| 3415 | return(NULL); |
| 3416 | } |
| 3417 | if (cur == NULL) { |
| 3418 | xmlUnlinkNode(old); |
| 3419 | return(old); |
| 3420 | } |
| 3421 | if (cur == old) { |
| 3422 | return(old); |
| 3423 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3424 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3425 | #ifdef DEBUG_TREE |
| 3426 | xmlGenericError(xmlGenericErrorContext, |
| 3427 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3428 | #endif |
| 3429 | return(old); |
| 3430 | } |
| 3431 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3432 | #ifdef DEBUG_TREE |
| 3433 | xmlGenericError(xmlGenericErrorContext, |
| 3434 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3435 | #endif |
| 3436 | return(old); |
| 3437 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3438 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3439 | #ifdef DEBUG_TREE |
| 3440 | xmlGenericError(xmlGenericErrorContext, |
| 3441 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3442 | #endif |
| 3443 | return(old); |
| 3444 | } |
| 3445 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3446 | #ifdef DEBUG_TREE |
| 3447 | xmlGenericError(xmlGenericErrorContext, |
| 3448 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3449 | #endif |
| 3450 | return(old); |
| 3451 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3452 | xmlUnlinkNode(cur); |
| 3453 | cur->doc = old->doc; |
| 3454 | cur->parent = old->parent; |
| 3455 | cur->next = old->next; |
| 3456 | if (cur->next != NULL) |
| 3457 | cur->next->prev = cur; |
| 3458 | cur->prev = old->prev; |
| 3459 | if (cur->prev != NULL) |
| 3460 | cur->prev->next = cur; |
| 3461 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3462 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3463 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3464 | cur->parent->properties = ((xmlAttrPtr) cur); |
| 3465 | } else { |
| 3466 | if (cur->parent->children == old) |
| 3467 | cur->parent->children = cur; |
| 3468 | if (cur->parent->last == old) |
| 3469 | cur->parent->last = cur; |
| 3470 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3471 | } |
| 3472 | old->next = old->prev = NULL; |
| 3473 | old->parent = NULL; |
| 3474 | return(old); |
| 3475 | } |
| 3476 | |
| 3477 | /************************************************************************ |
| 3478 | * * |
| 3479 | * Copy operations * |
| 3480 | * * |
| 3481 | ************************************************************************/ |
| 3482 | |
| 3483 | /** |
| 3484 | * xmlCopyNamespace: |
| 3485 | * @cur: the namespace |
| 3486 | * |
| 3487 | * Do a copy of the namespace. |
| 3488 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3489 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3490 | */ |
| 3491 | xmlNsPtr |
| 3492 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3493 | xmlNsPtr ret; |
| 3494 | |
| 3495 | if (cur == NULL) return(NULL); |
| 3496 | switch (cur->type) { |
| 3497 | case XML_LOCAL_NAMESPACE: |
| 3498 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3499 | break; |
| 3500 | default: |
| 3501 | #ifdef DEBUG_TREE |
| 3502 | xmlGenericError(xmlGenericErrorContext, |
| 3503 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3504 | #endif |
| 3505 | return(NULL); |
| 3506 | } |
| 3507 | return(ret); |
| 3508 | } |
| 3509 | |
| 3510 | /** |
| 3511 | * xmlCopyNamespaceList: |
| 3512 | * @cur: the first namespace |
| 3513 | * |
| 3514 | * Do a copy of an namespace list. |
| 3515 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3516 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3517 | */ |
| 3518 | xmlNsPtr |
| 3519 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3520 | xmlNsPtr ret = NULL; |
| 3521 | xmlNsPtr p = NULL,q; |
| 3522 | |
| 3523 | while (cur != NULL) { |
| 3524 | q = xmlCopyNamespace(cur); |
| 3525 | if (p == NULL) { |
| 3526 | ret = p = q; |
| 3527 | } else { |
| 3528 | p->next = q; |
| 3529 | p = q; |
| 3530 | } |
| 3531 | cur = cur->next; |
| 3532 | } |
| 3533 | return(ret); |
| 3534 | } |
| 3535 | |
| 3536 | static xmlNodePtr |
| 3537 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 3538 | /** |
| 3539 | * xmlCopyProp: |
| 3540 | * @target: the element where the attribute will be grafted |
| 3541 | * @cur: the attribute |
| 3542 | * |
| 3543 | * Do a copy of the attribute. |
| 3544 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3545 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3546 | */ |
| 3547 | xmlAttrPtr |
| 3548 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 3549 | xmlAttrPtr ret; |
| 3550 | |
| 3551 | if (cur == NULL) return(NULL); |
| 3552 | if (target != NULL) |
| 3553 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
| 3554 | else if (cur->parent != NULL) |
| 3555 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 3556 | else if (cur->children != NULL) |
| 3557 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 3558 | else |
| 3559 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 3560 | if (ret == NULL) return(NULL); |
| 3561 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3562 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3563 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3564 | xmlNsPtr ns; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3565 | /* |
| 3566 | * if (target->doc) |
| 3567 | * ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3568 | * else if (cur->doc) / * target may not yet have a doc : KPI * / |
| 3569 | * ns = xmlSearchNs(cur->doc, target, cur->ns->prefix); |
| 3570 | * else |
| 3571 | * ns = NULL; |
| 3572 | * ret->ns = ns; |
| 3573 | */ |
| 3574 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3575 | if (ns == NULL) { |
| 3576 | /* |
| 3577 | * Humm, we are copying an element whose namespace is defined |
| 3578 | * out of the new tree scope. Search it in the original tree |
| 3579 | * and add it at the top of the new tree |
| 3580 | */ |
| 3581 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 3582 | if (ns != NULL) { |
| 3583 | xmlNodePtr root = target; |
| 3584 | xmlNodePtr pred = NULL; |
| 3585 | |
| 3586 | while (root->parent != NULL) { |
| 3587 | pred = root; |
| 3588 | root = root->parent; |
| 3589 | } |
| 3590 | if (root == (xmlNodePtr) target->doc) { |
| 3591 | /* correct possibly cycling above the document elt */ |
| 3592 | root = pred; |
| 3593 | } |
| 3594 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 3595 | } |
| 3596 | } else { |
| 3597 | /* |
| 3598 | * we have to find something appropriate here since |
| 3599 | * we cant be sure, that the namespce we found is identified |
| 3600 | * by the prefix |
| 3601 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3602 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3603 | /* this is the nice case */ |
| 3604 | ret->ns = ns; |
| 3605 | } else { |
| 3606 | /* |
| 3607 | * we are in trouble: we need a new reconcilied namespace. |
| 3608 | * This is expensive |
| 3609 | */ |
| 3610 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 3611 | } |
| 3612 | } |
| 3613 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3614 | } else |
| 3615 | ret->ns = NULL; |
| 3616 | |
| 3617 | if (cur->children != NULL) { |
| 3618 | xmlNodePtr tmp; |
| 3619 | |
| 3620 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 3621 | ret->last = NULL; |
| 3622 | tmp = ret->children; |
| 3623 | while (tmp != NULL) { |
| 3624 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 3625 | if (tmp->next == NULL) |
| 3626 | ret->last = tmp; |
| 3627 | tmp = tmp->next; |
| 3628 | } |
| 3629 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3630 | /* |
| 3631 | * Try to handle IDs |
| 3632 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 3633 | if ((target!= NULL) && (cur!= NULL) && |
| 3634 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3635 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 3636 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 3637 | xmlChar *id; |
| 3638 | |
| 3639 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 3640 | if (id != NULL) { |
| 3641 | xmlAddID(NULL, target->doc, id, ret); |
| 3642 | xmlFree(id); |
| 3643 | } |
| 3644 | } |
| 3645 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3646 | return(ret); |
| 3647 | } |
| 3648 | |
| 3649 | /** |
| 3650 | * xmlCopyPropList: |
| 3651 | * @target: the element where the attributes will be grafted |
| 3652 | * @cur: the first attribute |
| 3653 | * |
| 3654 | * Do a copy of an attribute list. |
| 3655 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3656 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3657 | */ |
| 3658 | xmlAttrPtr |
| 3659 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 3660 | xmlAttrPtr ret = NULL; |
| 3661 | xmlAttrPtr p = NULL,q; |
| 3662 | |
| 3663 | while (cur != NULL) { |
| 3664 | q = xmlCopyProp(target, cur); |
| 3665 | if (p == NULL) { |
| 3666 | ret = p = q; |
| 3667 | } else { |
| 3668 | p->next = q; |
| 3669 | q->prev = p; |
| 3670 | p = q; |
| 3671 | } |
| 3672 | cur = cur->next; |
| 3673 | } |
| 3674 | return(ret); |
| 3675 | } |
| 3676 | |
| 3677 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3678 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3679 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3680 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3681 | * tricky reason: namespaces. Doing a direct copy of a node |
| 3682 | * say RPM:Copyright without changing the namespace pointer to |
| 3683 | * something else can produce stale links. One way to do it is |
| 3684 | * to keep a reference counter but this doesn't work as soon |
| 3685 | * as one move the element or the subtree out of the scope of |
| 3686 | * the existing namespace. The actual solution seems to add |
| 3687 | * a copy of the namespace at the top of the copied tree if |
| 3688 | * not available in the subtree. |
| 3689 | * Hence two functions, the public front-end call the inner ones |
| 3690 | */ |
| 3691 | |
| 3692 | static xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3693 | xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3694 | int recursive) { |
| 3695 | xmlNodePtr ret; |
| 3696 | |
| 3697 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3698 | switch (node->type) { |
| 3699 | case XML_TEXT_NODE: |
| 3700 | case XML_CDATA_SECTION_NODE: |
| 3701 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 3702 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3703 | case XML_ENTITY_REF_NODE: |
| 3704 | case XML_ENTITY_NODE: |
| 3705 | case XML_PI_NODE: |
| 3706 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3707 | case XML_XINCLUDE_START: |
| 3708 | case XML_XINCLUDE_END: |
| 3709 | break; |
| 3710 | case XML_ATTRIBUTE_NODE: |
| 3711 | return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node)); |
| 3712 | case XML_NAMESPACE_DECL: |
| 3713 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
| 3714 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3715 | case XML_DOCUMENT_NODE: |
| 3716 | case XML_HTML_DOCUMENT_NODE: |
| 3717 | #ifdef LIBXML_DOCB_ENABLED |
| 3718 | case XML_DOCB_DOCUMENT_NODE: |
| 3719 | #endif |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3720 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, recursive)); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3721 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3722 | case XML_NOTATION_NODE: |
| 3723 | case XML_DTD_NODE: |
| 3724 | case XML_ELEMENT_DECL: |
| 3725 | case XML_ATTRIBUTE_DECL: |
| 3726 | case XML_ENTITY_DECL: |
| 3727 | return(NULL); |
| 3728 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3729 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3730 | /* |
| 3731 | * Allocate a new node and fill the fields. |
| 3732 | */ |
| 3733 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 3734 | if (ret == NULL) { |
| 3735 | xmlGenericError(xmlGenericErrorContext, |
| 3736 | "xmlStaticCopyNode : malloc failed\n"); |
| 3737 | return(NULL); |
| 3738 | } |
| 3739 | memset(ret, 0, sizeof(xmlNode)); |
| 3740 | ret->type = node->type; |
| 3741 | |
| 3742 | ret->doc = doc; |
| 3743 | ret->parent = parent; |
| 3744 | if (node->name == xmlStringText) |
| 3745 | ret->name = xmlStringText; |
| 3746 | else if (node->name == xmlStringTextNoenc) |
| 3747 | ret->name = xmlStringTextNoenc; |
| 3748 | else if (node->name == xmlStringComment) |
| 3749 | ret->name = xmlStringComment; |
| 3750 | else if (node->name != NULL) |
| 3751 | ret->name = xmlStrdup(node->name); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3752 | if ((node->type != XML_ELEMENT_NODE) && |
| 3753 | (node->content != NULL) && |
| 3754 | (node->type != XML_ENTITY_REF_NODE) && |
| 3755 | (node->type != XML_XINCLUDE_END) && |
| 3756 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3757 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3758 | }else{ |
| 3759 | if (node->type == XML_ELEMENT_NODE) |
| 3760 | ret->content = (void*)(long) node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3761 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3762 | if (parent != NULL) { |
| 3763 | xmlNodePtr tmp; |
| 3764 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3765 | /* |
| 3766 | * this is a tricky part for the node register thing: |
| 3767 | * in case ret does get coalesced in xmlAddChild |
| 3768 | * the deregister-node callback is called; so we register ret now already |
| 3769 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3770 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3771 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 3772 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3773 | tmp = xmlAddChild(parent, ret); |
| 3774 | /* node could have coalesced */ |
| 3775 | if (tmp != ret) |
| 3776 | return(tmp); |
| 3777 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3778 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3779 | if (!recursive) |
| 3780 | goto out; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3781 | if (node->nsDef != NULL) |
| 3782 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 3783 | |
| 3784 | if (node->ns != NULL) { |
| 3785 | xmlNsPtr ns; |
| 3786 | |
| 3787 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 3788 | if (ns == NULL) { |
| 3789 | /* |
| 3790 | * Humm, we are copying an element whose namespace is defined |
| 3791 | * out of the new tree scope. Search it in the original tree |
| 3792 | * and add it at the top of the new tree |
| 3793 | */ |
| 3794 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 3795 | if (ns != NULL) { |
| 3796 | xmlNodePtr root = ret; |
| 3797 | |
| 3798 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 3799 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3800 | } |
| 3801 | } else { |
| 3802 | /* |
| 3803 | * reference the existing namespace definition in our own tree. |
| 3804 | */ |
| 3805 | ret->ns = ns; |
| 3806 | } |
| 3807 | } |
| 3808 | if (node->properties != NULL) |
| 3809 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3810 | if (node->type == XML_ENTITY_REF_NODE) { |
| 3811 | if ((doc == NULL) || (node->doc != doc)) { |
| 3812 | /* |
| 3813 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3814 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3815 | * we cannot keep the reference. Try to find it in the |
| 3816 | * target document. |
| 3817 | */ |
| 3818 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 3819 | } else { |
| 3820 | ret->children = node->children; |
| 3821 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3822 | ret->last = ret->children; |
| 3823 | } else if (node->children != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3824 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3825 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 3826 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3827 | |
| 3828 | out: |
| 3829 | /* if parent != NULL we already registered the node above */ |
| 3830 | if (parent == NULL && xmlRegisterNodeDefaultValue) |
| 3831 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3832 | return(ret); |
| 3833 | } |
| 3834 | |
| 3835 | static xmlNodePtr |
| 3836 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 3837 | xmlNodePtr ret = NULL; |
| 3838 | xmlNodePtr p = NULL,q; |
| 3839 | |
| 3840 | while (node != NULL) { |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3841 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 3842 | if (doc == NULL) { |
| 3843 | node = node->next; |
| 3844 | continue; |
| 3845 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3846 | if (doc->intSubset == NULL) { |
| 3847 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
| 3848 | q->doc = doc; |
| 3849 | q->parent = parent; |
| 3850 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3851 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3852 | } else { |
| 3853 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3854 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3855 | } |
| 3856 | } else |
| 3857 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3858 | if (ret == NULL) { |
| 3859 | q->prev = NULL; |
| 3860 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3861 | } else if (p != q) { |
| 3862 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3863 | p->next = q; |
| 3864 | q->prev = p; |
| 3865 | p = q; |
| 3866 | } |
| 3867 | node = node->next; |
| 3868 | } |
| 3869 | return(ret); |
| 3870 | } |
| 3871 | |
| 3872 | /** |
| 3873 | * xmlCopyNode: |
| 3874 | * @node: the node |
| 3875 | * @recursive: if 1 do a recursive copy. |
| 3876 | * |
| 3877 | * Do a copy of the node. |
| 3878 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3879 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3880 | */ |
| 3881 | xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3882 | xmlCopyNode(const xmlNodePtr node, int recursive) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3883 | xmlNodePtr ret; |
| 3884 | |
| 3885 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 3886 | return(ret); |
| 3887 | } |
| 3888 | |
| 3889 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3890 | * xmlDocCopyNode: |
| 3891 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3892 | * @doc: the document |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3893 | * @recursive: if 1 do a recursive copy. |
| 3894 | * |
| 3895 | * Do a copy of the node to a given document. |
| 3896 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3897 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3898 | */ |
| 3899 | xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3900 | xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int recursive) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3901 | xmlNodePtr ret; |
| 3902 | |
| 3903 | ret = xmlStaticCopyNode(node, doc, NULL, recursive); |
| 3904 | return(ret); |
| 3905 | } |
| 3906 | |
| 3907 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3908 | * xmlCopyNodeList: |
| 3909 | * @node: the first node in the list. |
| 3910 | * |
| 3911 | * Do a recursive copy of the node list. |
| 3912 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3913 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3914 | */ |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3915 | xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3916 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 3917 | return(ret); |
| 3918 | } |
| 3919 | |
| 3920 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3921 | * xmlCopyDtd: |
| 3922 | * @dtd: the dtd |
| 3923 | * |
| 3924 | * Do a copy of the dtd. |
| 3925 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3926 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3927 | */ |
| 3928 | xmlDtdPtr |
| 3929 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 3930 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3931 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3932 | |
| 3933 | if (dtd == NULL) return(NULL); |
| 3934 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 3935 | if (ret == NULL) return(NULL); |
| 3936 | if (dtd->entities != NULL) |
| 3937 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 3938 | (xmlEntitiesTablePtr) dtd->entities); |
| 3939 | if (dtd->notations != NULL) |
| 3940 | ret->notations = (void *) xmlCopyNotationTable( |
| 3941 | (xmlNotationTablePtr) dtd->notations); |
| 3942 | if (dtd->elements != NULL) |
| 3943 | ret->elements = (void *) xmlCopyElementTable( |
| 3944 | (xmlElementTablePtr) dtd->elements); |
| 3945 | if (dtd->attributes != NULL) |
| 3946 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 3947 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3948 | if (dtd->pentities != NULL) |
| 3949 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 3950 | (xmlEntitiesTablePtr) dtd->pentities); |
| 3951 | |
| 3952 | cur = dtd->children; |
| 3953 | while (cur != NULL) { |
| 3954 | q = NULL; |
| 3955 | |
| 3956 | if (cur->type == XML_ENTITY_DECL) { |
| 3957 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 3958 | switch (tmp->etype) { |
| 3959 | case XML_INTERNAL_GENERAL_ENTITY: |
| 3960 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 3961 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 3962 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 3963 | break; |
| 3964 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 3965 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 3966 | q = (xmlNodePtr) |
| 3967 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 3968 | break; |
| 3969 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 3970 | break; |
| 3971 | } |
| 3972 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 3973 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 3974 | q = (xmlNodePtr) |
| 3975 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 3976 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 3977 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
| 3978 | q = (xmlNodePtr) |
| 3979 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 3980 | } else if (cur->type == XML_COMMENT_NODE) { |
| 3981 | q = xmlCopyNode(cur, 0); |
| 3982 | } |
| 3983 | |
| 3984 | if (q == NULL) { |
| 3985 | cur = cur->next; |
| 3986 | continue; |
| 3987 | } |
| 3988 | |
| 3989 | if (p == NULL) |
| 3990 | ret->children = q; |
| 3991 | else |
| 3992 | p->next = q; |
| 3993 | |
| 3994 | q->prev = p; |
| 3995 | q->parent = (xmlNodePtr) ret; |
| 3996 | q->next = NULL; |
| 3997 | ret->last = q; |
| 3998 | p = q; |
| 3999 | cur = cur->next; |
| 4000 | } |
| 4001 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4002 | return(ret); |
| 4003 | } |
| 4004 | |
| 4005 | /** |
| 4006 | * xmlCopyDoc: |
| 4007 | * @doc: the document |
| 4008 | * @recursive: if 1 do a recursive copy. |
| 4009 | * |
| 4010 | * Do a copy of the document info. If recursive, the content tree will |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4011 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4012 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4013 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4014 | */ |
| 4015 | xmlDocPtr |
| 4016 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 4017 | xmlDocPtr ret; |
| 4018 | |
| 4019 | if (doc == NULL) return(NULL); |
| 4020 | ret = xmlNewDoc(doc->version); |
| 4021 | if (ret == NULL) return(NULL); |
| 4022 | if (doc->name != NULL) |
| 4023 | ret->name = xmlMemStrdup(doc->name); |
| 4024 | if (doc->encoding != NULL) |
| 4025 | ret->encoding = xmlStrdup(doc->encoding); |
| 4026 | ret->charset = doc->charset; |
| 4027 | ret->compression = doc->compression; |
| 4028 | ret->standalone = doc->standalone; |
| 4029 | if (!recursive) return(ret); |
| 4030 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4031 | ret->last = NULL; |
| 4032 | ret->children = NULL; |
| 4033 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4034 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4035 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4036 | ret->intSubset->parent = ret; |
| 4037 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4038 | if (doc->oldNs != NULL) |
| 4039 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 4040 | if (doc->children != NULL) { |
| 4041 | xmlNodePtr tmp; |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4042 | |
| 4043 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 4044 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4045 | ret->last = NULL; |
| 4046 | tmp = ret->children; |
| 4047 | while (tmp != NULL) { |
| 4048 | if (tmp->next == NULL) |
| 4049 | ret->last = tmp; |
| 4050 | tmp = tmp->next; |
| 4051 | } |
| 4052 | } |
| 4053 | return(ret); |
| 4054 | } |
| 4055 | |
| 4056 | /************************************************************************ |
| 4057 | * * |
| 4058 | * Content access functions * |
| 4059 | * * |
| 4060 | ************************************************************************/ |
| 4061 | |
| 4062 | /** |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4063 | * xmlGetLineNo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4064 | * @node: valid node |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4065 | * |
| 4066 | * Get line number of node. this requires activation of this option |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4067 | * before invoking the parser by calling xmlLineNumbersDefault(1) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4068 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4069 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4070 | */ |
| 4071 | long |
| 4072 | xmlGetLineNo(xmlNodePtr node) |
| 4073 | { |
| 4074 | long result = -1; |
| 4075 | |
| 4076 | if (!node) |
| 4077 | return result; |
| 4078 | if (node->type == XML_ELEMENT_NODE) |
| 4079 | result = (long) node->content; |
| 4080 | else if ((node->prev != NULL) && |
| 4081 | ((node->prev->type == XML_ELEMENT_NODE) || |
| 4082 | (node->prev->type == XML_TEXT_NODE))) |
| 4083 | result = xmlGetLineNo(node->prev); |
| 4084 | else if ((node->parent != NULL) && |
| 4085 | ((node->parent->type == XML_ELEMENT_NODE) || |
| 4086 | (node->parent->type == XML_TEXT_NODE))) |
| 4087 | result = xmlGetLineNo(node->parent); |
| 4088 | |
| 4089 | return result; |
| 4090 | } |
| 4091 | |
| 4092 | /** |
| 4093 | * xmlGetNodePath: |
| 4094 | * @node: a node |
| 4095 | * |
| 4096 | * Build a structure based Path for the given node |
| 4097 | * |
| 4098 | * Returns the new path or NULL in case of error. The caller must free |
| 4099 | * the returned string |
| 4100 | */ |
| 4101 | xmlChar * |
| 4102 | xmlGetNodePath(xmlNodePtr node) |
| 4103 | { |
| 4104 | xmlNodePtr cur, tmp, next; |
| 4105 | xmlChar *buffer = NULL, *temp; |
| 4106 | size_t buf_len; |
| 4107 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4108 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4109 | const char *name; |
| 4110 | char nametemp[100]; |
| 4111 | int occur = 0; |
| 4112 | |
| 4113 | if (node == NULL) |
| 4114 | return (NULL); |
| 4115 | |
| 4116 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4117 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4118 | if (buffer == NULL) |
| 4119 | return (NULL); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4120 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4121 | if (buf == NULL) { |
| 4122 | xmlFree(buffer); |
| 4123 | return (NULL); |
| 4124 | } |
| 4125 | |
| 4126 | buffer[0] = 0; |
| 4127 | cur = node; |
| 4128 | do { |
| 4129 | name = ""; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4130 | sep = "?"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4131 | occur = 0; |
| 4132 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4133 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4134 | if (buffer[0] == '/') |
| 4135 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4136 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4137 | next = NULL; |
| 4138 | } else if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4139 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4140 | name = (const char *) cur->name; |
| 4141 | if (cur->ns) { |
| 4142 | snprintf(nametemp, sizeof(nametemp) - 1, |
| 4143 | "%s:%s", cur->ns->prefix, cur->name); |
| 4144 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4145 | name = nametemp; |
| 4146 | } |
| 4147 | next = cur->parent; |
| 4148 | |
| 4149 | /* |
| 4150 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4151 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4152 | */ |
| 4153 | tmp = cur->prev; |
| 4154 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4155 | if ((tmp->type == XML_ELEMENT_NODE) && |
| 4156 | (xmlStrEqual(cur->name, tmp->name))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4157 | occur++; |
| 4158 | tmp = tmp->prev; |
| 4159 | } |
| 4160 | if (occur == 0) { |
| 4161 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4162 | while (tmp != NULL && occur == 0) { |
| 4163 | if ((tmp->type == XML_ELEMENT_NODE) && |
| 4164 | (xmlStrEqual(cur->name, tmp->name))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4165 | occur++; |
| 4166 | tmp = tmp->next; |
| 4167 | } |
| 4168 | if (occur != 0) |
| 4169 | occur = 1; |
| 4170 | } else |
| 4171 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4172 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4173 | sep = "/"; |
| 4174 | name = "comment()"; |
| 4175 | next = cur->parent; |
| 4176 | |
| 4177 | /* |
| 4178 | * Thumbler index computation |
| 4179 | */ |
| 4180 | tmp = cur->prev; |
| 4181 | while (tmp != NULL) { |
| 4182 | if (tmp->type == XML_COMMENT_NODE) |
| 4183 | occur++; |
| 4184 | tmp = tmp->prev; |
| 4185 | } |
| 4186 | if (occur == 0) { |
| 4187 | tmp = cur->next; |
| 4188 | while (tmp != NULL && occur == 0) { |
| 4189 | if (tmp->type == XML_COMMENT_NODE) |
| 4190 | occur++; |
| 4191 | tmp = tmp->next; |
| 4192 | } |
| 4193 | if (occur != 0) |
| 4194 | occur = 1; |
| 4195 | } else |
| 4196 | occur++; |
| 4197 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4198 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4199 | sep = "/"; |
| 4200 | name = "text()"; |
| 4201 | next = cur->parent; |
| 4202 | |
| 4203 | /* |
| 4204 | * Thumbler index computation |
| 4205 | */ |
| 4206 | tmp = cur->prev; |
| 4207 | while (tmp != NULL) { |
| 4208 | if ((cur->type == XML_TEXT_NODE) || |
| 4209 | (cur->type == XML_CDATA_SECTION_NODE)) |
| 4210 | occur++; |
| 4211 | tmp = tmp->prev; |
| 4212 | } |
| 4213 | if (occur == 0) { |
| 4214 | tmp = cur->next; |
| 4215 | while (tmp != NULL && occur == 0) { |
| 4216 | if ((cur->type == XML_TEXT_NODE) || |
| 4217 | (cur->type == XML_CDATA_SECTION_NODE)) |
| 4218 | occur++; |
| 4219 | tmp = tmp->next; |
| 4220 | } |
| 4221 | if (occur != 0) |
| 4222 | occur = 1; |
| 4223 | } else |
| 4224 | occur++; |
| 4225 | } else if (cur->type == XML_PI_NODE) { |
| 4226 | sep = "/"; |
| 4227 | snprintf(nametemp, sizeof(nametemp) - 1, |
| 4228 | "processing-instruction('%s')", cur->name); |
| 4229 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4230 | name = nametemp; |
| 4231 | |
| 4232 | next = cur->parent; |
| 4233 | |
| 4234 | /* |
| 4235 | * Thumbler index computation |
| 4236 | */ |
| 4237 | tmp = cur->prev; |
| 4238 | while (tmp != NULL) { |
| 4239 | if ((tmp->type == XML_PI_NODE) && |
| 4240 | (xmlStrEqual(cur->name, tmp->name))) |
| 4241 | occur++; |
| 4242 | tmp = tmp->prev; |
| 4243 | } |
| 4244 | if (occur == 0) { |
| 4245 | tmp = cur->next; |
| 4246 | while (tmp != NULL && occur == 0) { |
| 4247 | if ((tmp->type == XML_PI_NODE) && |
| 4248 | (xmlStrEqual(cur->name, tmp->name))) |
| 4249 | occur++; |
| 4250 | tmp = tmp->next; |
| 4251 | } |
| 4252 | if (occur != 0) |
| 4253 | occur = 1; |
| 4254 | } else |
| 4255 | occur++; |
| 4256 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4257 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4258 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4259 | name = (const char *) (((xmlAttrPtr) cur)->name); |
| 4260 | next = ((xmlAttrPtr) cur)->parent; |
| 4261 | } else { |
| 4262 | next = cur->parent; |
| 4263 | } |
| 4264 | |
| 4265 | /* |
| 4266 | * Make sure there is enough room |
| 4267 | */ |
| 4268 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4269 | buf_len = |
| 4270 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4271 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4272 | if (temp == NULL) { |
| 4273 | xmlFree(buf); |
| 4274 | xmlFree(buffer); |
| 4275 | return (NULL); |
| 4276 | } |
| 4277 | buffer = temp; |
| 4278 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4279 | if (temp == NULL) { |
| 4280 | xmlFree(buf); |
| 4281 | xmlFree(buffer); |
| 4282 | return (NULL); |
| 4283 | } |
| 4284 | buf = temp; |
| 4285 | } |
| 4286 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4287 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4288 | sep, name, (char *) buffer); |
| 4289 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4290 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4291 | sep, name, occur, (char *) buffer); |
| 4292 | snprintf((char *) buffer, buf_len, "%s", buf); |
| 4293 | cur = next; |
| 4294 | } while (cur != NULL); |
| 4295 | xmlFree(buf); |
| 4296 | return (buffer); |
| 4297 | } |
| 4298 | |
| 4299 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4300 | * xmlDocGetRootElement: |
| 4301 | * @doc: the document |
| 4302 | * |
| 4303 | * Get the root element of the document (doc->children is a list |
| 4304 | * containing possibly comments, PIs, etc ...). |
| 4305 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4306 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4307 | */ |
| 4308 | xmlNodePtr |
| 4309 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 4310 | xmlNodePtr ret; |
| 4311 | |
| 4312 | if (doc == NULL) return(NULL); |
| 4313 | ret = doc->children; |
| 4314 | while (ret != NULL) { |
| 4315 | if (ret->type == XML_ELEMENT_NODE) |
| 4316 | return(ret); |
| 4317 | ret = ret->next; |
| 4318 | } |
| 4319 | return(ret); |
| 4320 | } |
| 4321 | |
| 4322 | /** |
| 4323 | * xmlDocSetRootElement: |
| 4324 | * @doc: the document |
| 4325 | * @root: the new document root element |
| 4326 | * |
| 4327 | * Set the root element of the document (doc->children is a list |
| 4328 | * containing possibly comments, PIs, etc ...). |
| 4329 | * |
| 4330 | * Returns the old root element if any was found |
| 4331 | */ |
| 4332 | xmlNodePtr |
| 4333 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4334 | xmlNodePtr old = NULL; |
| 4335 | |
| 4336 | if (doc == NULL) return(NULL); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4337 | if (root == NULL) |
| 4338 | return(NULL); |
| 4339 | xmlUnlinkNode(root); |
| 4340 | root->doc = doc; |
| 4341 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4342 | old = doc->children; |
| 4343 | while (old != NULL) { |
| 4344 | if (old->type == XML_ELEMENT_NODE) |
| 4345 | break; |
| 4346 | old = old->next; |
| 4347 | } |
| 4348 | if (old == NULL) { |
| 4349 | if (doc->children == NULL) { |
| 4350 | doc->children = root; |
| 4351 | doc->last = root; |
| 4352 | } else { |
| 4353 | xmlAddSibling(doc->children, root); |
| 4354 | } |
| 4355 | } else { |
| 4356 | xmlReplaceNode(old, root); |
| 4357 | } |
| 4358 | return(old); |
| 4359 | } |
| 4360 | |
| 4361 | /** |
| 4362 | * xmlNodeSetLang: |
| 4363 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4364 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4365 | * |
| 4366 | * Set the language of a node, i.e. the values of the xml:lang |
| 4367 | * attribute. |
| 4368 | */ |
| 4369 | void |
| 4370 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4371 | xmlNsPtr ns; |
| 4372 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4373 | if (cur == NULL) return; |
| 4374 | switch(cur->type) { |
| 4375 | case XML_TEXT_NODE: |
| 4376 | case XML_CDATA_SECTION_NODE: |
| 4377 | case XML_COMMENT_NODE: |
| 4378 | case XML_DOCUMENT_NODE: |
| 4379 | case XML_DOCUMENT_TYPE_NODE: |
| 4380 | case XML_DOCUMENT_FRAG_NODE: |
| 4381 | case XML_NOTATION_NODE: |
| 4382 | case XML_HTML_DOCUMENT_NODE: |
| 4383 | case XML_DTD_NODE: |
| 4384 | case XML_ELEMENT_DECL: |
| 4385 | case XML_ATTRIBUTE_DECL: |
| 4386 | case XML_ENTITY_DECL: |
| 4387 | case XML_PI_NODE: |
| 4388 | case XML_ENTITY_REF_NODE: |
| 4389 | case XML_ENTITY_NODE: |
| 4390 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4391 | #ifdef LIBXML_DOCB_ENABLED |
| 4392 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4393 | #endif |
| 4394 | case XML_XINCLUDE_START: |
| 4395 | case XML_XINCLUDE_END: |
| 4396 | return; |
| 4397 | case XML_ELEMENT_NODE: |
| 4398 | case XML_ATTRIBUTE_NODE: |
| 4399 | break; |
| 4400 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4401 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4402 | if (ns == NULL) |
| 4403 | return; |
| 4404 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
| 4407 | /** |
| 4408 | * xmlNodeGetLang: |
| 4409 | * @cur: the node being checked |
| 4410 | * |
| 4411 | * Searches the language of a node, i.e. the values of the xml:lang |
| 4412 | * attribute or the one carried by the nearest ancestor. |
| 4413 | * |
| 4414 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4415 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4416 | */ |
| 4417 | xmlChar * |
| 4418 | xmlNodeGetLang(xmlNodePtr cur) { |
| 4419 | xmlChar *lang; |
| 4420 | |
| 4421 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 4422 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4423 | if (lang != NULL) |
| 4424 | return(lang); |
| 4425 | cur = cur->parent; |
| 4426 | } |
| 4427 | return(NULL); |
| 4428 | } |
| 4429 | |
| 4430 | |
| 4431 | /** |
| 4432 | * xmlNodeSetSpacePreserve: |
| 4433 | * @cur: the node being changed |
| 4434 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 4435 | * |
| 4436 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 4437 | * value of the xml:space attribute. |
| 4438 | */ |
| 4439 | void |
| 4440 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4441 | xmlNsPtr ns; |
| 4442 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4443 | if (cur == NULL) return; |
| 4444 | switch(cur->type) { |
| 4445 | case XML_TEXT_NODE: |
| 4446 | case XML_CDATA_SECTION_NODE: |
| 4447 | case XML_COMMENT_NODE: |
| 4448 | case XML_DOCUMENT_NODE: |
| 4449 | case XML_DOCUMENT_TYPE_NODE: |
| 4450 | case XML_DOCUMENT_FRAG_NODE: |
| 4451 | case XML_NOTATION_NODE: |
| 4452 | case XML_HTML_DOCUMENT_NODE: |
| 4453 | case XML_DTD_NODE: |
| 4454 | case XML_ELEMENT_DECL: |
| 4455 | case XML_ATTRIBUTE_DECL: |
| 4456 | case XML_ENTITY_DECL: |
| 4457 | case XML_PI_NODE: |
| 4458 | case XML_ENTITY_REF_NODE: |
| 4459 | case XML_ENTITY_NODE: |
| 4460 | case XML_NAMESPACE_DECL: |
| 4461 | case XML_XINCLUDE_START: |
| 4462 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4463 | #ifdef LIBXML_DOCB_ENABLED |
| 4464 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4465 | #endif |
| 4466 | return; |
| 4467 | case XML_ELEMENT_NODE: |
| 4468 | case XML_ATTRIBUTE_NODE: |
| 4469 | break; |
| 4470 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4471 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4472 | if (ns == NULL) |
| 4473 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4474 | switch (val) { |
| 4475 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4476 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4477 | break; |
| 4478 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4479 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4480 | break; |
| 4481 | } |
| 4482 | } |
| 4483 | |
| 4484 | /** |
| 4485 | * xmlNodeGetSpacePreserve: |
| 4486 | * @cur: the node being checked |
| 4487 | * |
| 4488 | * Searches the space preserving behaviour of a node, i.e. the values |
| 4489 | * of the xml:space attribute or the one carried by the nearest |
| 4490 | * ancestor. |
| 4491 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4492 | * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4493 | */ |
| 4494 | int |
| 4495 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 4496 | xmlChar *space; |
| 4497 | |
| 4498 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4499 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4500 | if (space != NULL) { |
| 4501 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 4502 | xmlFree(space); |
| 4503 | return(1); |
| 4504 | } |
| 4505 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 4506 | xmlFree(space); |
| 4507 | return(0); |
| 4508 | } |
| 4509 | xmlFree(space); |
| 4510 | } |
| 4511 | cur = cur->parent; |
| 4512 | } |
| 4513 | return(-1); |
| 4514 | } |
| 4515 | |
| 4516 | /** |
| 4517 | * xmlNodeSetName: |
| 4518 | * @cur: the node being changed |
| 4519 | * @name: the new tag name |
| 4520 | * |
| 4521 | * Set (or reset) the name of a node. |
| 4522 | */ |
| 4523 | void |
| 4524 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
| 4525 | if (cur == NULL) return; |
| 4526 | if (name == NULL) return; |
| 4527 | switch(cur->type) { |
| 4528 | case XML_TEXT_NODE: |
| 4529 | case XML_CDATA_SECTION_NODE: |
| 4530 | case XML_COMMENT_NODE: |
| 4531 | case XML_DOCUMENT_TYPE_NODE: |
| 4532 | case XML_DOCUMENT_FRAG_NODE: |
| 4533 | case XML_NOTATION_NODE: |
| 4534 | case XML_HTML_DOCUMENT_NODE: |
| 4535 | case XML_NAMESPACE_DECL: |
| 4536 | case XML_XINCLUDE_START: |
| 4537 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4538 | #ifdef LIBXML_DOCB_ENABLED |
| 4539 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4540 | #endif |
| 4541 | return; |
| 4542 | case XML_ELEMENT_NODE: |
| 4543 | case XML_ATTRIBUTE_NODE: |
| 4544 | case XML_PI_NODE: |
| 4545 | case XML_ENTITY_REF_NODE: |
| 4546 | case XML_ENTITY_NODE: |
| 4547 | case XML_DTD_NODE: |
| 4548 | case XML_DOCUMENT_NODE: |
| 4549 | case XML_ELEMENT_DECL: |
| 4550 | case XML_ATTRIBUTE_DECL: |
| 4551 | case XML_ENTITY_DECL: |
| 4552 | break; |
| 4553 | } |
| 4554 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 4555 | cur->name = xmlStrdup(name); |
| 4556 | } |
| 4557 | |
| 4558 | /** |
| 4559 | * xmlNodeSetBase: |
| 4560 | * @cur: the node being changed |
| 4561 | * @uri: the new base URI |
| 4562 | * |
| 4563 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 4564 | * xml:base attribute. |
| 4565 | */ |
| 4566 | void |
| 4567 | xmlNodeSetBase(xmlNodePtr cur, xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4568 | xmlNsPtr ns; |
| 4569 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4570 | if (cur == NULL) return; |
| 4571 | switch(cur->type) { |
| 4572 | case XML_TEXT_NODE: |
| 4573 | case XML_CDATA_SECTION_NODE: |
| 4574 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4575 | case XML_DOCUMENT_TYPE_NODE: |
| 4576 | case XML_DOCUMENT_FRAG_NODE: |
| 4577 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4578 | case XML_DTD_NODE: |
| 4579 | case XML_ELEMENT_DECL: |
| 4580 | case XML_ATTRIBUTE_DECL: |
| 4581 | case XML_ENTITY_DECL: |
| 4582 | case XML_PI_NODE: |
| 4583 | case XML_ENTITY_REF_NODE: |
| 4584 | case XML_ENTITY_NODE: |
| 4585 | case XML_NAMESPACE_DECL: |
| 4586 | case XML_XINCLUDE_START: |
| 4587 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4588 | return; |
| 4589 | case XML_ELEMENT_NODE: |
| 4590 | case XML_ATTRIBUTE_NODE: |
| 4591 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 4592 | case XML_DOCUMENT_NODE: |
| 4593 | #ifdef LIBXML_DOCB_ENABLED |
| 4594 | case XML_DOCB_DOCUMENT_NODE: |
| 4595 | #endif |
| 4596 | case XML_HTML_DOCUMENT_NODE: { |
| 4597 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 4598 | |
| 4599 | if (doc->URL != NULL) |
| 4600 | xmlFree((xmlChar *) doc->URL); |
| 4601 | if (uri == NULL) |
| 4602 | doc->URL = NULL; |
| 4603 | else |
| 4604 | doc->URL = xmlStrdup(uri); |
| 4605 | return; |
| 4606 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4607 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4608 | |
| 4609 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4610 | if (ns == NULL) |
| 4611 | return; |
| 4612 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4616 | * xmlNodeGetBase: |
| 4617 | * @doc: the document the node pertains to |
| 4618 | * @cur: the node being checked |
| 4619 | * |
| 4620 | * Searches for the BASE URL. The code should work on both XML |
| 4621 | * and HTML document even if base mechanisms are completely different. |
| 4622 | * It returns the base as defined in RFC 2396 sections |
| 4623 | * 5.1.1. Base URI within Document Content |
| 4624 | * and |
| 4625 | * 5.1.2. Base URI from the Encapsulating Entity |
| 4626 | * However it does not return the document base (5.1.3), use |
| 4627 | * xmlDocumentGetBase() for this |
| 4628 | * |
| 4629 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4630 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4631 | */ |
| 4632 | xmlChar * |
| 4633 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4634 | xmlChar *oldbase = NULL; |
| 4635 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4636 | |
| 4637 | if ((cur == NULL) && (doc == NULL)) |
| 4638 | return(NULL); |
| 4639 | if (doc == NULL) doc = cur->doc; |
| 4640 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 4641 | cur = doc->children; |
| 4642 | while ((cur != NULL) && (cur->name != NULL)) { |
| 4643 | if (cur->type != XML_ELEMENT_NODE) { |
| 4644 | cur = cur->next; |
| 4645 | continue; |
| 4646 | } |
| 4647 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 4648 | cur = cur->children; |
| 4649 | continue; |
| 4650 | } |
| 4651 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 4652 | cur = cur->children; |
| 4653 | continue; |
| 4654 | } |
| 4655 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 4656 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 4657 | } |
| 4658 | cur = cur->next; |
| 4659 | } |
| 4660 | return(NULL); |
| 4661 | } |
| 4662 | while (cur != NULL) { |
| 4663 | if (cur->type == XML_ENTITY_DECL) { |
| 4664 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 4665 | return(xmlStrdup(ent->URI)); |
| 4666 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4667 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4668 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4669 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4670 | if (oldbase != NULL) { |
| 4671 | newbase = xmlBuildURI(oldbase, base); |
| 4672 | if (newbase != NULL) { |
| 4673 | xmlFree(oldbase); |
| 4674 | xmlFree(base); |
| 4675 | oldbase = newbase; |
| 4676 | } else { |
| 4677 | xmlFree(oldbase); |
| 4678 | xmlFree(base); |
| 4679 | return(NULL); |
| 4680 | } |
| 4681 | } else { |
| 4682 | oldbase = base; |
| 4683 | } |
| 4684 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 4685 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 4686 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 4687 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4688 | } |
| 4689 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4690 | cur = cur->parent; |
| 4691 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4692 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 4693 | if (oldbase == NULL) |
| 4694 | return(xmlStrdup(doc->URL)); |
| 4695 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 4696 | xmlFree(oldbase); |
| 4697 | return(newbase); |
| 4698 | } |
| 4699 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | /** |
| 4703 | * xmlNodeGetContent: |
| 4704 | * @cur: the node being read |
| 4705 | * |
| 4706 | * Read the value of a node, this can be either the text carried |
| 4707 | * directly by this node if it's a TEXT node or the aggregate string |
| 4708 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4709 | * Entity references are substituted. |
| 4710 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4711 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4712 | */ |
| 4713 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4714 | xmlNodeGetContent(xmlNodePtr cur) |
| 4715 | { |
| 4716 | if (cur == NULL) |
| 4717 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4718 | switch (cur->type) { |
| 4719 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4720 | case XML_ELEMENT_NODE:{ |
| 4721 | xmlNodePtr tmp = cur; |
| 4722 | xmlBufferPtr buffer; |
| 4723 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4724 | |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 4725 | buffer = xmlBufferCreateSize(64); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4726 | if (buffer == NULL) |
| 4727 | return (NULL); |
| 4728 | while (tmp != NULL) { |
| 4729 | switch (tmp->type) { |
| 4730 | case XML_CDATA_SECTION_NODE: |
| 4731 | case XML_TEXT_NODE: |
| 4732 | if (tmp->content != NULL) |
| 4733 | xmlBufferCat(buffer, tmp->content); |
| 4734 | break; |
| 4735 | case XML_ENTITY_REF_NODE:{ |
| 4736 | /* recursive substitution of entity references */ |
| 4737 | xmlChar *cont = xmlNodeGetContent(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4738 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4739 | if (cont) { |
| 4740 | xmlBufferCat(buffer, |
| 4741 | (const xmlChar *) cont); |
| 4742 | xmlFree(cont); |
| 4743 | } |
| 4744 | break; |
| 4745 | } |
| 4746 | default: |
| 4747 | break; |
| 4748 | } |
| 4749 | /* |
| 4750 | * Skip to next node |
| 4751 | */ |
| 4752 | if (tmp->children != NULL) { |
| 4753 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4754 | tmp = tmp->children; |
| 4755 | continue; |
| 4756 | } |
| 4757 | } |
| 4758 | if (tmp == cur) |
| 4759 | break; |
Daniel Veillard | 6c83120 | 2001-03-07 15:57:53 +0000 | [diff] [blame] | 4760 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4761 | if (tmp->next != NULL) { |
| 4762 | tmp = tmp->next; |
| 4763 | continue; |
| 4764 | } |
| 4765 | |
| 4766 | do { |
| 4767 | tmp = tmp->parent; |
| 4768 | if (tmp == NULL) |
| 4769 | break; |
| 4770 | if (tmp == cur) { |
| 4771 | tmp = NULL; |
| 4772 | break; |
| 4773 | } |
| 4774 | if (tmp->next != NULL) { |
| 4775 | tmp = tmp->next; |
| 4776 | break; |
| 4777 | } |
| 4778 | } while (tmp != NULL); |
| 4779 | } |
| 4780 | ret = buffer->content; |
| 4781 | buffer->content = NULL; |
| 4782 | xmlBufferFree(buffer); |
| 4783 | return (ret); |
| 4784 | } |
| 4785 | case XML_ATTRIBUTE_NODE:{ |
| 4786 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 4787 | |
| 4788 | if (attr->parent != NULL) |
| 4789 | return (xmlNodeListGetString |
| 4790 | (attr->parent->doc, attr->children, 1)); |
| 4791 | else |
| 4792 | return (xmlNodeListGetString(NULL, attr->children, 1)); |
| 4793 | break; |
| 4794 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4795 | case XML_COMMENT_NODE: |
| 4796 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4797 | if (cur->content != NULL) |
| 4798 | return (xmlStrdup(cur->content)); |
| 4799 | return (NULL); |
| 4800 | case XML_ENTITY_REF_NODE:{ |
| 4801 | xmlEntityPtr ent; |
| 4802 | xmlNodePtr tmp; |
| 4803 | xmlBufferPtr buffer; |
| 4804 | xmlChar *ret; |
| 4805 | |
| 4806 | /* lookup entity declaration */ |
| 4807 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 4808 | if (ent == NULL) |
| 4809 | return (NULL); |
| 4810 | |
| 4811 | buffer = xmlBufferCreate(); |
| 4812 | if (buffer == NULL) |
| 4813 | return (NULL); |
| 4814 | |
| 4815 | /* an entity content can be any "well balanced chunk", |
| 4816 | * i.e. the result of the content [43] production: |
| 4817 | * http://www.w3.org/TR/REC-xml#NT-content |
| 4818 | * -> we iterate through child nodes and recursive call |
| 4819 | * xmlNodeGetContent() which handles all possible node types */ |
| 4820 | tmp = ent->children; |
| 4821 | while (tmp) { |
| 4822 | xmlChar *cont = xmlNodeGetContent(tmp); |
| 4823 | |
| 4824 | if (cont) { |
| 4825 | xmlBufferCat(buffer, (const xmlChar *) cont); |
| 4826 | xmlFree(cont); |
| 4827 | } |
| 4828 | tmp = tmp->next; |
| 4829 | } |
| 4830 | |
| 4831 | ret = buffer->content; |
| 4832 | buffer->content = NULL; |
| 4833 | xmlBufferFree(buffer); |
| 4834 | return (ret); |
| 4835 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4836 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4837 | case XML_DOCUMENT_TYPE_NODE: |
| 4838 | case XML_NOTATION_NODE: |
| 4839 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4840 | case XML_XINCLUDE_START: |
| 4841 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4842 | return (NULL); |
| 4843 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4844 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4845 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4846 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4847 | case XML_HTML_DOCUMENT_NODE: { |
| 4848 | xmlChar *tmp; |
| 4849 | xmlChar *res = NULL; |
| 4850 | |
| 4851 | cur = cur->children; |
| 4852 | while (cur!= NULL) { |
| 4853 | if ((cur->type == XML_ELEMENT_NODE) || |
| 4854 | (cur->type == XML_TEXT_NODE) || |
| 4855 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4856 | tmp = xmlNodeGetContent(cur); |
| 4857 | if (tmp != NULL) { |
| 4858 | if (res == NULL) |
| 4859 | res = tmp; |
| 4860 | else { |
| 4861 | res = xmlStrcat(res, tmp); |
| 4862 | xmlFree(tmp); |
| 4863 | } |
| 4864 | } |
| 4865 | } |
| 4866 | cur = cur->next; |
| 4867 | } |
| 4868 | return(res); |
| 4869 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4870 | case XML_NAMESPACE_DECL: { |
| 4871 | xmlChar *tmp; |
| 4872 | |
| 4873 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 4874 | return (tmp); |
| 4875 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4876 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4877 | /* TODO !!! */ |
| 4878 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4879 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4880 | /* TODO !!! */ |
| 4881 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4882 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4883 | /* TODO !!! */ |
| 4884 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4885 | case XML_CDATA_SECTION_NODE: |
| 4886 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4887 | if (cur->content != NULL) |
| 4888 | return (xmlStrdup(cur->content)); |
| 4889 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4890 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4891 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4892 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4893 | /** |
| 4894 | * xmlNodeSetContent: |
| 4895 | * @cur: the node being modified |
| 4896 | * @content: the new value of the content |
| 4897 | * |
| 4898 | * Replace the content of a node. |
| 4899 | */ |
| 4900 | void |
| 4901 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 4902 | if (cur == NULL) { |
| 4903 | #ifdef DEBUG_TREE |
| 4904 | xmlGenericError(xmlGenericErrorContext, |
| 4905 | "xmlNodeSetContent : node == NULL\n"); |
| 4906 | #endif |
| 4907 | return; |
| 4908 | } |
| 4909 | switch (cur->type) { |
| 4910 | case XML_DOCUMENT_FRAG_NODE: |
| 4911 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 4912 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4913 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 4914 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 4915 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 4916 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4917 | case XML_TEXT_NODE: |
| 4918 | case XML_CDATA_SECTION_NODE: |
| 4919 | case XML_ENTITY_REF_NODE: |
| 4920 | case XML_ENTITY_NODE: |
| 4921 | case XML_PI_NODE: |
| 4922 | case XML_COMMENT_NODE: |
| 4923 | if (cur->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4924 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4925 | } |
| 4926 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 4927 | cur->last = cur->children = NULL; |
| 4928 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4929 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4930 | } else |
| 4931 | cur->content = NULL; |
| 4932 | break; |
| 4933 | case XML_DOCUMENT_NODE: |
| 4934 | case XML_HTML_DOCUMENT_NODE: |
| 4935 | case XML_DOCUMENT_TYPE_NODE: |
| 4936 | case XML_XINCLUDE_START: |
| 4937 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4938 | #ifdef LIBXML_DOCB_ENABLED |
| 4939 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4940 | #endif |
| 4941 | break; |
| 4942 | case XML_NOTATION_NODE: |
| 4943 | break; |
| 4944 | case XML_DTD_NODE: |
| 4945 | break; |
| 4946 | case XML_NAMESPACE_DECL: |
| 4947 | break; |
| 4948 | case XML_ELEMENT_DECL: |
| 4949 | /* TODO !!! */ |
| 4950 | break; |
| 4951 | case XML_ATTRIBUTE_DECL: |
| 4952 | /* TODO !!! */ |
| 4953 | break; |
| 4954 | case XML_ENTITY_DECL: |
| 4955 | /* TODO !!! */ |
| 4956 | break; |
| 4957 | } |
| 4958 | } |
| 4959 | |
| 4960 | /** |
| 4961 | * xmlNodeSetContentLen: |
| 4962 | * @cur: the node being modified |
| 4963 | * @content: the new value of the content |
| 4964 | * @len: the size of @content |
| 4965 | * |
| 4966 | * Replace the content of a node. |
| 4967 | */ |
| 4968 | void |
| 4969 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 4970 | if (cur == NULL) { |
| 4971 | #ifdef DEBUG_TREE |
| 4972 | xmlGenericError(xmlGenericErrorContext, |
| 4973 | "xmlNodeSetContentLen : node == NULL\n"); |
| 4974 | #endif |
| 4975 | return; |
| 4976 | } |
| 4977 | switch (cur->type) { |
| 4978 | case XML_DOCUMENT_FRAG_NODE: |
| 4979 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 4980 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4981 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 4982 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 4983 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 4984 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4985 | case XML_TEXT_NODE: |
| 4986 | case XML_CDATA_SECTION_NODE: |
| 4987 | case XML_ENTITY_REF_NODE: |
| 4988 | case XML_ENTITY_NODE: |
| 4989 | case XML_PI_NODE: |
| 4990 | case XML_COMMENT_NODE: |
| 4991 | case XML_NOTATION_NODE: |
| 4992 | if (cur->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4993 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4994 | } |
| 4995 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 4996 | cur->children = cur->last = NULL; |
| 4997 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4998 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4999 | } else |
| 5000 | cur->content = NULL; |
| 5001 | break; |
| 5002 | case XML_DOCUMENT_NODE: |
| 5003 | case XML_DTD_NODE: |
| 5004 | case XML_HTML_DOCUMENT_NODE: |
| 5005 | case XML_DOCUMENT_TYPE_NODE: |
| 5006 | case XML_NAMESPACE_DECL: |
| 5007 | case XML_XINCLUDE_START: |
| 5008 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5009 | #ifdef LIBXML_DOCB_ENABLED |
| 5010 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5011 | #endif |
| 5012 | break; |
| 5013 | case XML_ELEMENT_DECL: |
| 5014 | /* TODO !!! */ |
| 5015 | break; |
| 5016 | case XML_ATTRIBUTE_DECL: |
| 5017 | /* TODO !!! */ |
| 5018 | break; |
| 5019 | case XML_ENTITY_DECL: |
| 5020 | /* TODO !!! */ |
| 5021 | break; |
| 5022 | } |
| 5023 | } |
| 5024 | |
| 5025 | /** |
| 5026 | * xmlNodeAddContentLen: |
| 5027 | * @cur: the node being modified |
| 5028 | * @content: extra content |
| 5029 | * @len: the size of @content |
| 5030 | * |
| 5031 | * Append the extra substring to the node content. |
| 5032 | */ |
| 5033 | void |
| 5034 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5035 | if (cur == NULL) { |
| 5036 | #ifdef DEBUG_TREE |
| 5037 | xmlGenericError(xmlGenericErrorContext, |
| 5038 | "xmlNodeAddContentLen : node == NULL\n"); |
| 5039 | #endif |
| 5040 | return; |
| 5041 | } |
| 5042 | if (len <= 0) return; |
| 5043 | switch (cur->type) { |
| 5044 | case XML_DOCUMENT_FRAG_NODE: |
| 5045 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5046 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5047 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 5048 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5049 | newNode = xmlNewTextLen(content, len); |
| 5050 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5051 | tmp = xmlAddChild(cur, newNode); |
| 5052 | if (tmp != newNode) |
| 5053 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5054 | if ((last != NULL) && (last->next == newNode)) { |
| 5055 | xmlTextMerge(last, newNode); |
| 5056 | } |
| 5057 | } |
| 5058 | break; |
| 5059 | } |
| 5060 | case XML_ATTRIBUTE_NODE: |
| 5061 | break; |
| 5062 | case XML_TEXT_NODE: |
| 5063 | case XML_CDATA_SECTION_NODE: |
| 5064 | case XML_ENTITY_REF_NODE: |
| 5065 | case XML_ENTITY_NODE: |
| 5066 | case XML_PI_NODE: |
| 5067 | case XML_COMMENT_NODE: |
| 5068 | case XML_NOTATION_NODE: |
| 5069 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5070 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5071 | } |
| 5072 | case XML_DOCUMENT_NODE: |
| 5073 | case XML_DTD_NODE: |
| 5074 | case XML_HTML_DOCUMENT_NODE: |
| 5075 | case XML_DOCUMENT_TYPE_NODE: |
| 5076 | case XML_NAMESPACE_DECL: |
| 5077 | case XML_XINCLUDE_START: |
| 5078 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5079 | #ifdef LIBXML_DOCB_ENABLED |
| 5080 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5081 | #endif |
| 5082 | break; |
| 5083 | case XML_ELEMENT_DECL: |
| 5084 | case XML_ATTRIBUTE_DECL: |
| 5085 | case XML_ENTITY_DECL: |
| 5086 | break; |
| 5087 | } |
| 5088 | } |
| 5089 | |
| 5090 | /** |
| 5091 | * xmlNodeAddContent: |
| 5092 | * @cur: the node being modified |
| 5093 | * @content: extra content |
| 5094 | * |
| 5095 | * Append the extra substring to the node content. |
| 5096 | */ |
| 5097 | void |
| 5098 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5099 | int len; |
| 5100 | |
| 5101 | if (cur == NULL) { |
| 5102 | #ifdef DEBUG_TREE |
| 5103 | xmlGenericError(xmlGenericErrorContext, |
| 5104 | "xmlNodeAddContent : node == NULL\n"); |
| 5105 | #endif |
| 5106 | return; |
| 5107 | } |
| 5108 | if (content == NULL) return; |
| 5109 | len = xmlStrlen(content); |
| 5110 | xmlNodeAddContentLen(cur, content, len); |
| 5111 | } |
| 5112 | |
| 5113 | /** |
| 5114 | * xmlTextMerge: |
| 5115 | * @first: the first text node |
| 5116 | * @second: the second text node being merged |
| 5117 | * |
| 5118 | * Merge two text nodes into one |
| 5119 | * Returns the first text node augmented |
| 5120 | */ |
| 5121 | xmlNodePtr |
| 5122 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5123 | if (first == NULL) return(second); |
| 5124 | if (second == NULL) return(first); |
| 5125 | if (first->type != XML_TEXT_NODE) return(first); |
| 5126 | if (second->type != XML_TEXT_NODE) return(first); |
| 5127 | if (second->name != first->name) |
| 5128 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5129 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5130 | xmlUnlinkNode(second); |
| 5131 | xmlFreeNode(second); |
| 5132 | return(first); |
| 5133 | } |
| 5134 | |
| 5135 | /** |
| 5136 | * xmlGetNsList: |
| 5137 | * @doc: the document |
| 5138 | * @node: the current node |
| 5139 | * |
| 5140 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5141 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5142 | * that need to be freed by the caller or NULL if no |
| 5143 | * namespace if defined |
| 5144 | */ |
| 5145 | xmlNsPtr * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5146 | xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) |
| 5147 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5148 | xmlNsPtr cur; |
| 5149 | xmlNsPtr *ret = NULL; |
| 5150 | int nbns = 0; |
| 5151 | int maxns = 10; |
| 5152 | int i; |
| 5153 | |
| 5154 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5155 | if (node->type == XML_ELEMENT_NODE) { |
| 5156 | cur = node->nsDef; |
| 5157 | while (cur != NULL) { |
| 5158 | if (ret == NULL) { |
| 5159 | ret = |
| 5160 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5161 | sizeof(xmlNsPtr)); |
| 5162 | if (ret == NULL) { |
| 5163 | xmlGenericError(xmlGenericErrorContext, |
| 5164 | "xmlGetNsList : out of memory!\n"); |
| 5165 | return (NULL); |
| 5166 | } |
| 5167 | ret[nbns] = NULL; |
| 5168 | } |
| 5169 | for (i = 0; i < nbns; i++) { |
| 5170 | if ((cur->prefix == ret[i]->prefix) || |
| 5171 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5172 | break; |
| 5173 | } |
| 5174 | if (i >= nbns) { |
| 5175 | if (nbns >= maxns) { |
| 5176 | maxns *= 2; |
| 5177 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5178 | (maxns + |
| 5179 | 1) * |
| 5180 | sizeof(xmlNsPtr)); |
| 5181 | if (ret == NULL) { |
| 5182 | xmlGenericError(xmlGenericErrorContext, |
| 5183 | "xmlGetNsList : realloc failed!\n"); |
| 5184 | return (NULL); |
| 5185 | } |
| 5186 | } |
| 5187 | ret[nbns++] = cur; |
| 5188 | ret[nbns] = NULL; |
| 5189 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5190 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5191 | cur = cur->next; |
| 5192 | } |
| 5193 | } |
| 5194 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5195 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5196 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | /** |
| 5200 | * xmlSearchNs: |
| 5201 | * @doc: the document |
| 5202 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5203 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5204 | * |
| 5205 | * Search a Ns registered under a given name space for a document. |
| 5206 | * recurse on the parents until it finds the defined namespace |
| 5207 | * or return NULL otherwise. |
| 5208 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5209 | * We don't allow to cross entities boundaries. If you don't declare |
| 5210 | * the namespace within those you will be in troubles !!! A warning |
| 5211 | * is generated to cover this case. |
| 5212 | * |
| 5213 | * Returns the namespace pointer or NULL. |
| 5214 | */ |
| 5215 | xmlNsPtr |
| 5216 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
| 5217 | xmlNsPtr cur; |
| 5218 | |
| 5219 | if (node == NULL) return(NULL); |
| 5220 | if ((nameSpace != NULL) && |
| 5221 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5222 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5223 | /* |
| 5224 | * The XML-1.0 namespace is normally held on the root |
| 5225 | * element. In this case exceptionally create it on the |
| 5226 | * node element. |
| 5227 | */ |
| 5228 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5229 | if (cur == NULL) { |
| 5230 | xmlGenericError(xmlGenericErrorContext, |
| 5231 | "xmlSearchNs : malloc failed\n"); |
| 5232 | return(NULL); |
| 5233 | } |
| 5234 | memset(cur, 0, sizeof(xmlNs)); |
| 5235 | cur->type = XML_LOCAL_NAMESPACE; |
| 5236 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5237 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5238 | cur->next = node->nsDef; |
| 5239 | node->nsDef = cur; |
| 5240 | return(cur); |
| 5241 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5242 | if (doc->oldNs == NULL) { |
| 5243 | /* |
| 5244 | * Allocate a new Namespace and fill the fields. |
| 5245 | */ |
| 5246 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5247 | if (doc->oldNs == NULL) { |
| 5248 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5249 | "xmlSearchNs : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5250 | return(NULL); |
| 5251 | } |
| 5252 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5253 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
| 5254 | |
| 5255 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5256 | doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5257 | } |
| 5258 | return(doc->oldNs); |
| 5259 | } |
| 5260 | while (node != NULL) { |
| 5261 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5262 | (node->type == XML_ENTITY_NODE) || |
| 5263 | (node->type == XML_ENTITY_DECL)) |
| 5264 | return(NULL); |
| 5265 | if (node->type == XML_ELEMENT_NODE) { |
| 5266 | cur = node->nsDef; |
| 5267 | while (cur != NULL) { |
| 5268 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5269 | (cur->href != NULL)) |
| 5270 | return(cur); |
| 5271 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5272 | (cur->href != NULL) && |
| 5273 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5274 | return(cur); |
| 5275 | cur = cur->next; |
| 5276 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5277 | cur = node->ns; |
| 5278 | if (cur != NULL) { |
| 5279 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5280 | (cur->href != NULL)) |
| 5281 | return(cur); |
| 5282 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5283 | (cur->href != NULL) && |
| 5284 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5285 | return(cur); |
| 5286 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5287 | } |
| 5288 | node = node->parent; |
| 5289 | } |
| 5290 | return(NULL); |
| 5291 | } |
| 5292 | |
| 5293 | /** |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5294 | * xmlNsInScope: |
| 5295 | * @doc: the document |
| 5296 | * @node: the current node |
| 5297 | * @ancestor: the ancestor carrying the namespace |
| 5298 | * @prefix: the namespace prefix |
| 5299 | * |
| 5300 | * Verify that the given namespace held on @ancestor is still in scope |
| 5301 | * on node. |
| 5302 | * |
| 5303 | * Returns 1 if true, 0 if false and -1 in case of error. |
| 5304 | */ |
| 5305 | static int |
| 5306 | xmlNsInScope(xmlDocPtr doc, xmlNodePtr node, xmlNodePtr ancestor, |
| 5307 | const xmlChar * prefix) |
| 5308 | { |
| 5309 | xmlNsPtr tst; |
| 5310 | |
| 5311 | while ((node != NULL) && (node != ancestor)) { |
| 5312 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5313 | (node->type == XML_ENTITY_NODE) || |
| 5314 | (node->type == XML_ENTITY_DECL)) |
| 5315 | return (-1); |
| 5316 | if (node->type == XML_ELEMENT_NODE) { |
| 5317 | tst = node->nsDef; |
| 5318 | while (tst != NULL) { |
| 5319 | if ((tst->prefix == NULL) |
| 5320 | && (prefix == NULL)) |
| 5321 | return (0); |
| 5322 | if ((tst->prefix != NULL) |
| 5323 | && (prefix != NULL) |
| 5324 | && (xmlStrEqual(tst->prefix, prefix))) |
| 5325 | return (0); |
| 5326 | tst = tst->next; |
| 5327 | } |
| 5328 | } |
| 5329 | node = node->parent; |
| 5330 | } |
| 5331 | if (node != ancestor) |
| 5332 | return (-1); |
| 5333 | return (1); |
| 5334 | } |
| 5335 | |
| 5336 | |
| 5337 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5338 | * xmlSearchNsByHref: |
| 5339 | * @doc: the document |
| 5340 | * @node: the current node |
| 5341 | * @href: the namespace value |
| 5342 | * |
| 5343 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 5344 | * the defined namespace or return NULL otherwise. |
| 5345 | * Returns the namespace pointer or NULL. |
| 5346 | */ |
| 5347 | xmlNsPtr |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5348 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) |
| 5349 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5350 | xmlNsPtr cur; |
| 5351 | xmlNodePtr orig = node; |
| 5352 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5353 | if ((node == NULL) || (href == NULL)) |
| 5354 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5355 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5356 | /* |
| 5357 | * Only the document can hold the XML spec namespace. |
| 5358 | */ |
| 5359 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5360 | /* |
| 5361 | * The XML-1.0 namespace is normally held on the root |
| 5362 | * element. In this case exceptionally create it on the |
| 5363 | * node element. |
| 5364 | */ |
| 5365 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5366 | if (cur == NULL) { |
| 5367 | xmlGenericError(xmlGenericErrorContext, |
| 5368 | "xmlSearchNs : malloc failed\n"); |
| 5369 | return (NULL); |
| 5370 | } |
| 5371 | memset(cur, 0, sizeof(xmlNs)); |
| 5372 | cur->type = XML_LOCAL_NAMESPACE; |
| 5373 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5374 | cur->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5375 | cur->next = node->nsDef; |
| 5376 | node->nsDef = cur; |
| 5377 | return (cur); |
| 5378 | } |
| 5379 | if (doc->oldNs == NULL) { |
| 5380 | /* |
| 5381 | * Allocate a new Namespace and fill the fields. |
| 5382 | */ |
| 5383 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5384 | if (doc->oldNs == NULL) { |
| 5385 | xmlGenericError(xmlGenericErrorContext, |
| 5386 | "xmlSearchNsByHref : malloc failed\n"); |
| 5387 | return (NULL); |
| 5388 | } |
| 5389 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5390 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5391 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5392 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5393 | doc->oldNs->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5394 | } |
| 5395 | return (doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5396 | } |
| 5397 | while (node != NULL) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5398 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5399 | (node->type == XML_ENTITY_NODE) || |
| 5400 | (node->type == XML_ENTITY_DECL)) |
| 5401 | return (NULL); |
| 5402 | if (node->type == XML_ELEMENT_NODE) { |
| 5403 | cur = node->nsDef; |
| 5404 | while (cur != NULL) { |
| 5405 | if ((cur->href != NULL) && (href != NULL) && |
| 5406 | (xmlStrEqual(cur->href, href))) { |
| 5407 | if (xmlNsInScope(doc, orig, node, cur->href) == 1) |
| 5408 | return (cur); |
| 5409 | } |
| 5410 | cur = cur->next; |
| 5411 | } |
| 5412 | cur = node->ns; |
| 5413 | if (cur != NULL) { |
| 5414 | if ((cur->href != NULL) && (href != NULL) && |
| 5415 | (xmlStrEqual(cur->href, href))) { |
| 5416 | if (xmlNsInScope(doc, orig, node, cur->href) == 1) |
| 5417 | return (cur); |
| 5418 | } |
| 5419 | } |
| 5420 | } |
| 5421 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5422 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5423 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5424 | } |
| 5425 | |
| 5426 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5427 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5428 | * @doc: the document |
| 5429 | * @tree: a node expected to hold the new namespace |
| 5430 | * @ns: the original namespace |
| 5431 | * |
| 5432 | * This function tries to locate a namespace definition in a tree |
| 5433 | * ancestors, or create a new namespace definition node similar to |
| 5434 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 5435 | * null (default namespace) or reused within the subtree defined by |
| 5436 | * @tree or on one of its ancestors then a new prefix is generated. |
| 5437 | * Returns the (new) namespace definition or NULL in case of error |
| 5438 | */ |
| 5439 | xmlNsPtr |
| 5440 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 5441 | xmlNsPtr def; |
| 5442 | xmlChar prefix[50]; |
| 5443 | int counter = 1; |
| 5444 | |
| 5445 | if (tree == NULL) { |
| 5446 | #ifdef DEBUG_TREE |
| 5447 | xmlGenericError(xmlGenericErrorContext, |
| 5448 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 5449 | #endif |
| 5450 | return(NULL); |
| 5451 | } |
| 5452 | if (ns == NULL) { |
| 5453 | #ifdef DEBUG_TREE |
| 5454 | xmlGenericError(xmlGenericErrorContext, |
| 5455 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 5456 | #endif |
| 5457 | return(NULL); |
| 5458 | } |
| 5459 | /* |
| 5460 | * Search an existing namespace definition inherited. |
| 5461 | */ |
| 5462 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 5463 | if (def != NULL) |
| 5464 | return(def); |
| 5465 | |
| 5466 | /* |
| 5467 | * Find a close prefix which is not already in use. |
| 5468 | * Let's strip namespace prefixes longer than 20 chars ! |
| 5469 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5470 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5471 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5472 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5473 | snprintf((char *) prefix, sizeof(prefix), "%.20s", ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5474 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5475 | def = xmlSearchNs(doc, tree, prefix); |
| 5476 | while (def != NULL) { |
| 5477 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5478 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5479 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5480 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5481 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5482 | def = xmlSearchNs(doc, tree, prefix); |
| 5483 | } |
| 5484 | |
| 5485 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5486 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5487 | */ |
| 5488 | def = xmlNewNs(tree, ns->href, prefix); |
| 5489 | return(def); |
| 5490 | } |
| 5491 | |
| 5492 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5493 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5494 | * @doc: the document |
| 5495 | * @tree: a node defining the subtree to reconciliate |
| 5496 | * |
| 5497 | * This function checks that all the namespaces declared within the given |
| 5498 | * tree are properly declared. This is needed for example after Copy or Cut |
| 5499 | * and then paste operations. The subtree may still hold pointers to |
| 5500 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5501 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5502 | * the new environment. If not possible the new namespaces are redeclared |
| 5503 | * on @tree at the top of the given subtree. |
| 5504 | * Returns the number of namespace declarations created or -1 in case of error. |
| 5505 | */ |
| 5506 | int |
| 5507 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 5508 | xmlNsPtr *oldNs = NULL; |
| 5509 | xmlNsPtr *newNs = NULL; |
| 5510 | int sizeCache = 0; |
| 5511 | int nbCache = 0; |
| 5512 | |
| 5513 | xmlNsPtr n; |
| 5514 | xmlNodePtr node = tree; |
| 5515 | xmlAttrPtr attr; |
| 5516 | int ret = 0, i; |
| 5517 | |
| 5518 | while (node != NULL) { |
| 5519 | /* |
| 5520 | * Reconciliate the node namespace |
| 5521 | */ |
| 5522 | if (node->ns != NULL) { |
| 5523 | /* |
| 5524 | * initialize the cache if needed |
| 5525 | */ |
| 5526 | if (sizeCache == 0) { |
| 5527 | sizeCache = 10; |
| 5528 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5529 | sizeof(xmlNsPtr)); |
| 5530 | if (oldNs == NULL) { |
| 5531 | xmlGenericError(xmlGenericErrorContext, |
| 5532 | "xmlReconciliateNs : memory pbm\n"); |
| 5533 | return(-1); |
| 5534 | } |
| 5535 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5536 | sizeof(xmlNsPtr)); |
| 5537 | if (newNs == NULL) { |
| 5538 | xmlGenericError(xmlGenericErrorContext, |
| 5539 | "xmlReconciliateNs : memory pbm\n"); |
| 5540 | xmlFree(oldNs); |
| 5541 | return(-1); |
| 5542 | } |
| 5543 | } |
| 5544 | for (i = 0;i < nbCache;i++) { |
| 5545 | if (oldNs[i] == node->ns) { |
| 5546 | node->ns = newNs[i]; |
| 5547 | break; |
| 5548 | } |
| 5549 | } |
| 5550 | if (i == nbCache) { |
| 5551 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5552 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5553 | */ |
| 5554 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 5555 | if (n != NULL) { /* :-( what if else ??? */ |
| 5556 | /* |
| 5557 | * check if we need to grow the cache buffers. |
| 5558 | */ |
| 5559 | if (sizeCache <= nbCache) { |
| 5560 | sizeCache *= 2; |
| 5561 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5562 | sizeof(xmlNsPtr)); |
| 5563 | if (oldNs == NULL) { |
| 5564 | xmlGenericError(xmlGenericErrorContext, |
| 5565 | "xmlReconciliateNs : memory pbm\n"); |
| 5566 | xmlFree(newNs); |
| 5567 | return(-1); |
| 5568 | } |
| 5569 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5570 | sizeof(xmlNsPtr)); |
| 5571 | if (newNs == NULL) { |
| 5572 | xmlGenericError(xmlGenericErrorContext, |
| 5573 | "xmlReconciliateNs : memory pbm\n"); |
| 5574 | xmlFree(oldNs); |
| 5575 | return(-1); |
| 5576 | } |
| 5577 | } |
| 5578 | newNs[nbCache] = n; |
| 5579 | oldNs[nbCache++] = node->ns; |
| 5580 | node->ns = n; |
| 5581 | } |
| 5582 | } |
| 5583 | } |
| 5584 | /* |
| 5585 | * now check for namespace hold by attributes on the node. |
| 5586 | */ |
| 5587 | attr = node->properties; |
| 5588 | while (attr != NULL) { |
| 5589 | if (attr->ns != NULL) { |
| 5590 | /* |
| 5591 | * initialize the cache if needed |
| 5592 | */ |
| 5593 | if (sizeCache == 0) { |
| 5594 | sizeCache = 10; |
| 5595 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5596 | sizeof(xmlNsPtr)); |
| 5597 | if (oldNs == NULL) { |
| 5598 | xmlGenericError(xmlGenericErrorContext, |
| 5599 | "xmlReconciliateNs : memory pbm\n"); |
| 5600 | return(-1); |
| 5601 | } |
| 5602 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5603 | sizeof(xmlNsPtr)); |
| 5604 | if (newNs == NULL) { |
| 5605 | xmlGenericError(xmlGenericErrorContext, |
| 5606 | "xmlReconciliateNs : memory pbm\n"); |
| 5607 | xmlFree(oldNs); |
| 5608 | return(-1); |
| 5609 | } |
| 5610 | } |
| 5611 | for (i = 0;i < nbCache;i++) { |
| 5612 | if (oldNs[i] == attr->ns) { |
Daniel Veillard | ce66ce1 | 2002-10-28 19:01:59 +0000 | [diff] [blame] | 5613 | attr->ns = newNs[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5614 | break; |
| 5615 | } |
| 5616 | } |
| 5617 | if (i == nbCache) { |
| 5618 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5619 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5620 | */ |
| 5621 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 5622 | if (n != NULL) { /* :-( what if else ??? */ |
| 5623 | /* |
| 5624 | * check if we need to grow the cache buffers. |
| 5625 | */ |
| 5626 | if (sizeCache <= nbCache) { |
| 5627 | sizeCache *= 2; |
| 5628 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5629 | sizeof(xmlNsPtr)); |
| 5630 | if (oldNs == NULL) { |
| 5631 | xmlGenericError(xmlGenericErrorContext, |
| 5632 | "xmlReconciliateNs : memory pbm\n"); |
| 5633 | xmlFree(newNs); |
| 5634 | return(-1); |
| 5635 | } |
| 5636 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5637 | sizeof(xmlNsPtr)); |
| 5638 | if (newNs == NULL) { |
| 5639 | xmlGenericError(xmlGenericErrorContext, |
| 5640 | "xmlReconciliateNs : memory pbm\n"); |
| 5641 | xmlFree(oldNs); |
| 5642 | return(-1); |
| 5643 | } |
| 5644 | } |
| 5645 | newNs[nbCache] = n; |
| 5646 | oldNs[nbCache++] = attr->ns; |
| 5647 | attr->ns = n; |
| 5648 | } |
| 5649 | } |
| 5650 | } |
| 5651 | attr = attr->next; |
| 5652 | } |
| 5653 | |
| 5654 | /* |
| 5655 | * Browse the full subtree, deep first |
| 5656 | */ |
| 5657 | if (node->children != NULL) { |
| 5658 | /* deep first */ |
| 5659 | node = node->children; |
| 5660 | } else if ((node != tree) && (node->next != NULL)) { |
| 5661 | /* then siblings */ |
| 5662 | node = node->next; |
| 5663 | } else if (node != tree) { |
| 5664 | /* go up to parents->next if needed */ |
| 5665 | while (node != tree) { |
| 5666 | if (node->parent != NULL) |
| 5667 | node = node->parent; |
| 5668 | if ((node != tree) && (node->next != NULL)) { |
| 5669 | node = node->next; |
| 5670 | break; |
| 5671 | } |
| 5672 | if (node->parent == NULL) { |
| 5673 | node = NULL; |
| 5674 | break; |
| 5675 | } |
| 5676 | } |
| 5677 | /* exit condition */ |
| 5678 | if (node == tree) |
| 5679 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 5680 | } else |
| 5681 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5682 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5683 | if (oldNs != NULL) |
| 5684 | xmlFree(oldNs); |
| 5685 | if (newNs != NULL) |
| 5686 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5687 | return(ret); |
| 5688 | } |
| 5689 | |
| 5690 | /** |
| 5691 | * xmlHasProp: |
| 5692 | * @node: the node |
| 5693 | * @name: the attribute name |
| 5694 | * |
| 5695 | * Search an attribute associated to a node |
| 5696 | * This function also looks in DTD attribute declaration for #FIXED or |
| 5697 | * default declaration values unless DTD use has been turned off. |
| 5698 | * |
| 5699 | * Returns the attribute or the attribute declaration or NULL if |
| 5700 | * neither was found. |
| 5701 | */ |
| 5702 | xmlAttrPtr |
| 5703 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 5704 | xmlAttrPtr prop; |
| 5705 | xmlDocPtr doc; |
| 5706 | |
| 5707 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 5708 | /* |
| 5709 | * Check on the properties attached to the node |
| 5710 | */ |
| 5711 | prop = node->properties; |
| 5712 | while (prop != NULL) { |
| 5713 | if (xmlStrEqual(prop->name, name)) { |
| 5714 | return(prop); |
| 5715 | } |
| 5716 | prop = prop->next; |
| 5717 | } |
| 5718 | if (!xmlCheckDTD) return(NULL); |
| 5719 | |
| 5720 | /* |
| 5721 | * Check if there is a default declaration in the internal |
| 5722 | * or external subsets |
| 5723 | */ |
| 5724 | doc = node->doc; |
| 5725 | if (doc != NULL) { |
| 5726 | xmlAttributePtr attrDecl; |
| 5727 | if (doc->intSubset != NULL) { |
| 5728 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5729 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5730 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5731 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5732 | /* return attribute declaration only if a default value is given |
| 5733 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5734 | return((xmlAttrPtr) attrDecl); |
| 5735 | } |
| 5736 | } |
| 5737 | return(NULL); |
| 5738 | } |
| 5739 | |
| 5740 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5741 | * xmlHasNsProp: |
| 5742 | * @node: the node |
| 5743 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5744 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5745 | * |
| 5746 | * Search for an attribute associated to a node |
| 5747 | * This attribute has to be anchored in the namespace specified. |
| 5748 | * This does the entity substitution. |
| 5749 | * This function looks in DTD attribute declaration for #FIXED or |
| 5750 | * default declaration values unless DTD use has been turned off. |
| 5751 | * |
| 5752 | * Returns the attribute or the attribute declaration or NULL |
| 5753 | * if neither was found. |
| 5754 | */ |
| 5755 | xmlAttrPtr |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5756 | xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5757 | xmlAttrPtr prop; |
| 5758 | xmlDocPtr doc; |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5759 | |
| 5760 | if (node == NULL) |
| 5761 | return(NULL); |
| 5762 | |
| 5763 | prop = node->properties; |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5764 | if (nameSpace == NULL) |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5765 | return(xmlHasProp(node, name)); |
| 5766 | while (prop != NULL) { |
| 5767 | /* |
| 5768 | * One need to have |
| 5769 | * - same attribute names |
| 5770 | * - and the attribute carrying that namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5771 | */ |
| 5772 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | e3c81b5 | 2001-06-17 14:50:34 +0000 | [diff] [blame] | 5773 | ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, nameSpace)))) { |
| 5774 | return(prop); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5775 | } |
| 5776 | prop = prop->next; |
| 5777 | } |
| 5778 | if (!xmlCheckDTD) return(NULL); |
| 5779 | |
| 5780 | /* |
| 5781 | * Check if there is a default declaration in the internal |
| 5782 | * or external subsets |
| 5783 | */ |
| 5784 | doc = node->doc; |
| 5785 | if (doc != NULL) { |
| 5786 | if (doc->intSubset != NULL) { |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5787 | xmlAttributePtr attrDecl = NULL; |
| 5788 | xmlNsPtr *nsList, *cur; |
| 5789 | xmlChar *ename; |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5790 | |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5791 | nsList = xmlGetNsList(node->doc, node); |
| 5792 | if (nsList == NULL) |
| 5793 | return(NULL); |
| 5794 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 5795 | ename = xmlStrdup(node->ns->prefix); |
| 5796 | ename = xmlStrcat(ename, BAD_CAST ":"); |
| 5797 | ename = xmlStrcat(ename, node->name); |
| 5798 | } else { |
| 5799 | ename = xmlStrdup(node->name); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5800 | } |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5801 | if (ename == NULL) { |
| 5802 | xmlFree(nsList); |
| 5803 | return(NULL); |
| 5804 | } |
| 5805 | |
| 5806 | cur = nsList; |
| 5807 | while (*cur != NULL) { |
| 5808 | if (xmlStrEqual((*cur)->href, nameSpace)) { |
| 5809 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename, |
| 5810 | name, (*cur)->prefix); |
| 5811 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5812 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename, |
| 5813 | name, (*cur)->prefix); |
| 5814 | } |
| 5815 | cur++; |
| 5816 | } |
| 5817 | xmlFree(nsList); |
| 5818 | xmlFree(ename); |
| 5819 | return((xmlAttrPtr) attrDecl); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5820 | } |
| 5821 | } |
| 5822 | return(NULL); |
| 5823 | } |
| 5824 | |
| 5825 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5826 | * xmlGetProp: |
| 5827 | * @node: the node |
| 5828 | * @name: the attribute name |
| 5829 | * |
| 5830 | * Search and get the value of an attribute associated to a node |
| 5831 | * This does the entity substitution. |
| 5832 | * This function looks in DTD attribute declaration for #FIXED or |
| 5833 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 5834 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5835 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 5836 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5837 | * |
| 5838 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5839 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5840 | */ |
| 5841 | xmlChar * |
| 5842 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
| 5843 | xmlAttrPtr prop; |
| 5844 | xmlDocPtr doc; |
| 5845 | |
| 5846 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 5847 | /* |
| 5848 | * Check on the properties attached to the node |
| 5849 | */ |
| 5850 | prop = node->properties; |
| 5851 | while (prop != NULL) { |
| 5852 | if (xmlStrEqual(prop->name, name)) { |
| 5853 | xmlChar *ret; |
| 5854 | |
| 5855 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 5856 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 5857 | return(ret); |
| 5858 | } |
| 5859 | prop = prop->next; |
| 5860 | } |
| 5861 | if (!xmlCheckDTD) return(NULL); |
| 5862 | |
| 5863 | /* |
| 5864 | * Check if there is a default declaration in the internal |
| 5865 | * or external subsets |
| 5866 | */ |
| 5867 | doc = node->doc; |
| 5868 | if (doc != NULL) { |
| 5869 | xmlAttributePtr attrDecl; |
| 5870 | if (doc->intSubset != NULL) { |
| 5871 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5872 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5873 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5874 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5875 | /* return attribute declaration only if a default value is given |
| 5876 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5877 | return(xmlStrdup(attrDecl->defaultValue)); |
| 5878 | } |
| 5879 | } |
| 5880 | return(NULL); |
| 5881 | } |
| 5882 | |
| 5883 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5884 | * xmlGetNoNsProp: |
| 5885 | * @node: the node |
| 5886 | * @name: the attribute name |
| 5887 | * |
| 5888 | * Search and get the value of an attribute associated to a node |
| 5889 | * This does the entity substitution. |
| 5890 | * This function looks in DTD attribute declaration for #FIXED or |
| 5891 | * default declaration values unless DTD use has been turned off. |
| 5892 | * This function is similar to xmlGetProp except it will accept only |
| 5893 | * an attribute in no namespace. |
| 5894 | * |
| 5895 | * Returns the attribute value or NULL if not found. |
| 5896 | * It's up to the caller to free the memory with xmlFree(). |
| 5897 | */ |
| 5898 | xmlChar * |
| 5899 | xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) { |
| 5900 | xmlAttrPtr prop; |
| 5901 | xmlDocPtr doc; |
| 5902 | |
| 5903 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 5904 | /* |
| 5905 | * Check on the properties attached to the node |
| 5906 | */ |
| 5907 | prop = node->properties; |
| 5908 | while (prop != NULL) { |
| 5909 | if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) { |
| 5910 | xmlChar *ret; |
| 5911 | |
| 5912 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 5913 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 5914 | return(ret); |
| 5915 | } |
| 5916 | prop = prop->next; |
| 5917 | } |
| 5918 | if (!xmlCheckDTD) return(NULL); |
| 5919 | |
| 5920 | /* |
| 5921 | * Check if there is a default declaration in the internal |
| 5922 | * or external subsets |
| 5923 | */ |
| 5924 | doc = node->doc; |
| 5925 | if (doc != NULL) { |
| 5926 | xmlAttributePtr attrDecl; |
| 5927 | if (doc->intSubset != NULL) { |
| 5928 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5929 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5930 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5931 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5932 | /* return attribute declaration only if a default value is given |
| 5933 | (that includes #FIXED declarations) */ |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5934 | return(xmlStrdup(attrDecl->defaultValue)); |
| 5935 | } |
| 5936 | } |
| 5937 | return(NULL); |
| 5938 | } |
| 5939 | |
| 5940 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5941 | * xmlGetNsProp: |
| 5942 | * @node: the node |
| 5943 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5944 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5945 | * |
| 5946 | * Search and get the value of an attribute associated to a node |
| 5947 | * This attribute has to be anchored in the namespace specified. |
| 5948 | * This does the entity substitution. |
| 5949 | * This function looks in DTD attribute declaration for #FIXED or |
| 5950 | * default declaration values unless DTD use has been turned off. |
| 5951 | * |
| 5952 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5953 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5954 | */ |
| 5955 | xmlChar * |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5956 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5957 | xmlAttrPtr prop; |
| 5958 | xmlDocPtr doc; |
| 5959 | xmlNsPtr ns; |
| 5960 | |
| 5961 | if (node == NULL) |
| 5962 | return(NULL); |
| 5963 | |
| 5964 | prop = node->properties; |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5965 | if (nameSpace == NULL) |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5966 | return(xmlGetNoNsProp(node, name)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5967 | while (prop != NULL) { |
| 5968 | /* |
| 5969 | * One need to have |
| 5970 | * - same attribute names |
| 5971 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5972 | */ |
| 5973 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | e8fc08e | 2001-06-07 19:35:47 +0000 | [diff] [blame] | 5974 | ((prop->ns != NULL) && |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5975 | (xmlStrEqual(prop->ns->href, nameSpace)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5976 | xmlChar *ret; |
| 5977 | |
| 5978 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 5979 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 5980 | return(ret); |
| 5981 | } |
| 5982 | prop = prop->next; |
| 5983 | } |
| 5984 | if (!xmlCheckDTD) return(NULL); |
| 5985 | |
| 5986 | /* |
| 5987 | * Check if there is a default declaration in the internal |
| 5988 | * or external subsets |
| 5989 | */ |
| 5990 | doc = node->doc; |
| 5991 | if (doc != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5992 | if (doc->intSubset != NULL) { |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 5993 | xmlAttributePtr attrDecl; |
| 5994 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5995 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5996 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5997 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 5998 | |
| 5999 | if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) { |
| 6000 | /* |
| 6001 | * The DTD declaration only allows a prefix search |
| 6002 | */ |
| 6003 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6004 | if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6005 | return(xmlStrdup(attrDecl->defaultValue)); |
| 6006 | } |
| 6007 | } |
| 6008 | } |
| 6009 | return(NULL); |
| 6010 | } |
| 6011 | |
| 6012 | /** |
| 6013 | * xmlSetProp: |
| 6014 | * @node: the node |
| 6015 | * @name: the attribute name |
| 6016 | * @value: the attribute value |
| 6017 | * |
| 6018 | * Set (or reset) an attribute carried by a node. |
| 6019 | * Returns the attribute pointer. |
| 6020 | */ |
| 6021 | xmlAttrPtr |
| 6022 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6023 | xmlAttrPtr prop; |
| 6024 | xmlDocPtr doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6025 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6026 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6027 | return(NULL); |
| 6028 | doc = node->doc; |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6029 | prop = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6030 | while (prop != NULL) { |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6031 | if ((xmlStrEqual(prop->name, name)) && |
| 6032 | (prop->ns == NULL)){ |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6033 | xmlNodePtr oldprop = prop->children; |
| 6034 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6035 | prop->children = NULL; |
| 6036 | prop->last = NULL; |
| 6037 | if (value != NULL) { |
| 6038 | xmlChar *buffer; |
| 6039 | xmlNodePtr tmp; |
| 6040 | |
| 6041 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6042 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 6043 | prop->last = NULL; |
| 6044 | prop->doc = doc; |
| 6045 | tmp = prop->children; |
| 6046 | while (tmp != NULL) { |
| 6047 | tmp->parent = (xmlNodePtr) prop; |
| 6048 | tmp->doc = doc; |
| 6049 | if (tmp->next == NULL) |
| 6050 | prop->last = tmp; |
| 6051 | tmp = tmp->next; |
| 6052 | } |
| 6053 | xmlFree(buffer); |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6054 | } |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6055 | if (oldprop != NULL) |
| 6056 | xmlFreeNodeList(oldprop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6057 | return(prop); |
| 6058 | } |
| 6059 | prop = prop->next; |
| 6060 | } |
| 6061 | prop = xmlNewProp(node, name, value); |
| 6062 | return(prop); |
| 6063 | } |
| 6064 | |
| 6065 | /** |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6066 | * xmlUnsetProp: |
| 6067 | * @node: the node |
| 6068 | * @name: the attribute name |
| 6069 | * |
| 6070 | * Remove an attribute carried by a node. |
| 6071 | * Returns 0 if successful, -1 if not found |
| 6072 | */ |
| 6073 | int |
| 6074 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 6075 | xmlAttrPtr prop, prev = NULL;; |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6076 | |
| 6077 | if ((node == NULL) || (name == NULL)) |
| 6078 | return(-1); |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 6079 | prop = node->properties; |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6080 | while (prop != NULL) { |
| 6081 | if ((xmlStrEqual(prop->name, name)) && |
| 6082 | (prop->ns == NULL)) { |
| 6083 | if (prev == NULL) |
| 6084 | node->properties = prop->next; |
| 6085 | else |
| 6086 | prev->next = prop->next; |
| 6087 | xmlFreeProp(prop); |
| 6088 | return(0); |
| 6089 | } |
| 6090 | prev = prop; |
| 6091 | prop = prop->next; |
| 6092 | } |
| 6093 | return(-1); |
| 6094 | } |
| 6095 | |
| 6096 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6097 | * xmlSetNsProp: |
| 6098 | * @node: the node |
| 6099 | * @ns: the namespace definition |
| 6100 | * @name: the attribute name |
| 6101 | * @value: the attribute value |
| 6102 | * |
| 6103 | * Set (or reset) an attribute carried by a node. |
| 6104 | * The ns structure must be in scope, this is not checked. |
| 6105 | * |
| 6106 | * Returns the attribute pointer. |
| 6107 | */ |
| 6108 | xmlAttrPtr |
| 6109 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 6110 | const xmlChar *value) { |
| 6111 | xmlAttrPtr prop; |
| 6112 | |
| 6113 | if ((node == NULL) || (name == NULL)) |
| 6114 | return(NULL); |
| 6115 | |
| 6116 | if (ns == NULL) |
| 6117 | return(xmlSetProp(node, name, value)); |
| 6118 | if (ns->href == NULL) |
| 6119 | return(NULL); |
| 6120 | prop = node->properties; |
| 6121 | |
| 6122 | while (prop != NULL) { |
| 6123 | /* |
| 6124 | * One need to have |
| 6125 | * - same attribute names |
| 6126 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6127 | */ |
| 6128 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | a57c26e | 2002-08-01 12:52:24 +0000 | [diff] [blame] | 6129 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6130 | if (prop->children != NULL) |
| 6131 | xmlFreeNodeList(prop->children); |
| 6132 | prop->children = NULL; |
| 6133 | prop->last = NULL; |
| 6134 | prop->ns = ns; |
| 6135 | if (value != NULL) { |
| 6136 | xmlChar *buffer; |
| 6137 | xmlNodePtr tmp; |
| 6138 | |
| 6139 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6140 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 6141 | prop->last = NULL; |
| 6142 | tmp = prop->children; |
| 6143 | while (tmp != NULL) { |
| 6144 | tmp->parent = (xmlNodePtr) prop; |
| 6145 | if (tmp->next == NULL) |
| 6146 | prop->last = tmp; |
| 6147 | tmp = tmp->next; |
| 6148 | } |
| 6149 | xmlFree(buffer); |
| 6150 | } |
| 6151 | return(prop); |
| 6152 | } |
| 6153 | prop = prop->next; |
| 6154 | } |
| 6155 | prop = xmlNewNsProp(node, ns, name, value); |
| 6156 | return(prop); |
| 6157 | } |
| 6158 | |
| 6159 | /** |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6160 | * xmlUnsetNsProp: |
| 6161 | * @node: the node |
| 6162 | * @ns: the namespace definition |
| 6163 | * @name: the attribute name |
| 6164 | * |
| 6165 | * Remove an attribute carried by a node. |
| 6166 | * Returns 0 if successful, -1 if not found |
| 6167 | */ |
| 6168 | int |
| 6169 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
| 6170 | xmlAttrPtr prop = node->properties, prev = NULL;; |
| 6171 | |
| 6172 | if ((node == NULL) || (name == NULL)) |
| 6173 | return(-1); |
| 6174 | if (ns == NULL) |
| 6175 | return(xmlUnsetProp(node, name)); |
| 6176 | if (ns->href == NULL) |
| 6177 | return(-1); |
| 6178 | while (prop != NULL) { |
| 6179 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | 0bf2900 | 2002-08-01 12:54:11 +0000 | [diff] [blame] | 6180 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6181 | if (prev == NULL) |
| 6182 | node->properties = prop->next; |
| 6183 | else |
| 6184 | prev->next = prop->next; |
| 6185 | xmlFreeProp(prop); |
| 6186 | return(0); |
| 6187 | } |
| 6188 | prev = prop; |
| 6189 | prop = prop->next; |
| 6190 | } |
| 6191 | return(-1); |
| 6192 | } |
| 6193 | |
| 6194 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6195 | * xmlNodeIsText: |
| 6196 | * @node: the node |
| 6197 | * |
| 6198 | * Is this node a Text node ? |
| 6199 | * Returns 1 yes, 0 no |
| 6200 | */ |
| 6201 | int |
| 6202 | xmlNodeIsText(xmlNodePtr node) { |
| 6203 | if (node == NULL) return(0); |
| 6204 | |
| 6205 | if (node->type == XML_TEXT_NODE) return(1); |
| 6206 | return(0); |
| 6207 | } |
| 6208 | |
| 6209 | /** |
| 6210 | * xmlIsBlankNode: |
| 6211 | * @node: the node |
| 6212 | * |
| 6213 | * Checks whether this node is an empty or whitespace only |
| 6214 | * (and possibly ignorable) text-node. |
| 6215 | * |
| 6216 | * Returns 1 yes, 0 no |
| 6217 | */ |
| 6218 | int |
| 6219 | xmlIsBlankNode(xmlNodePtr node) { |
| 6220 | const xmlChar *cur; |
| 6221 | if (node == NULL) return(0); |
| 6222 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6223 | if ((node->type != XML_TEXT_NODE) && |
| 6224 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6225 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6226 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6227 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6228 | while (*cur != 0) { |
| 6229 | if (!IS_BLANK(*cur)) return(0); |
| 6230 | cur++; |
| 6231 | } |
| 6232 | |
| 6233 | return(1); |
| 6234 | } |
| 6235 | |
| 6236 | /** |
| 6237 | * xmlTextConcat: |
| 6238 | * @node: the node |
| 6239 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6240 | * @len: @content length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6241 | * |
| 6242 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6243 | * |
| 6244 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6245 | */ |
| 6246 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6247 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6248 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6249 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6250 | |
| 6251 | if ((node->type != XML_TEXT_NODE) && |
| 6252 | (node->type != XML_CDATA_SECTION_NODE)) { |
| 6253 | #ifdef DEBUG_TREE |
| 6254 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6255 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6256 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6257 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6258 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6259 | node->content = xmlStrncat(node->content, content, len); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6260 | if (node->content == NULL) |
| 6261 | return(-1); |
| 6262 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6263 | } |
| 6264 | |
| 6265 | /************************************************************************ |
| 6266 | * * |
| 6267 | * Output : to a FILE or in memory * |
| 6268 | * * |
| 6269 | ************************************************************************/ |
| 6270 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6271 | /** |
| 6272 | * xmlBufferCreate: |
| 6273 | * |
| 6274 | * routine to create an XML buffer. |
| 6275 | * returns the new structure. |
| 6276 | */ |
| 6277 | xmlBufferPtr |
| 6278 | xmlBufferCreate(void) { |
| 6279 | xmlBufferPtr ret; |
| 6280 | |
| 6281 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6282 | if (ret == NULL) { |
| 6283 | xmlGenericError(xmlGenericErrorContext, |
| 6284 | "xmlBufferCreate : out of memory!\n"); |
| 6285 | return(NULL); |
| 6286 | } |
| 6287 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 6288 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6289 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6290 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6291 | if (ret->content == NULL) { |
| 6292 | xmlGenericError(xmlGenericErrorContext, |
| 6293 | "xmlBufferCreate : out of memory!\n"); |
| 6294 | xmlFree(ret); |
| 6295 | return(NULL); |
| 6296 | } |
| 6297 | ret->content[0] = 0; |
| 6298 | return(ret); |
| 6299 | } |
| 6300 | |
| 6301 | /** |
| 6302 | * xmlBufferCreateSize: |
| 6303 | * @size: initial size of buffer |
| 6304 | * |
| 6305 | * routine to create an XML buffer. |
| 6306 | * returns the new structure. |
| 6307 | */ |
| 6308 | xmlBufferPtr |
| 6309 | xmlBufferCreateSize(size_t size) { |
| 6310 | xmlBufferPtr ret; |
| 6311 | |
| 6312 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6313 | if (ret == NULL) { |
| 6314 | xmlGenericError(xmlGenericErrorContext, |
| 6315 | "xmlBufferCreate : out of memory!\n"); |
| 6316 | return(NULL); |
| 6317 | } |
| 6318 | ret->use = 0; |
| 6319 | ret->alloc = xmlBufferAllocScheme; |
| 6320 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 6321 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6322 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6323 | if (ret->content == NULL) { |
| 6324 | xmlGenericError(xmlGenericErrorContext, |
| 6325 | "xmlBufferCreate : out of memory!\n"); |
| 6326 | xmlFree(ret); |
| 6327 | return(NULL); |
| 6328 | } |
| 6329 | ret->content[0] = 0; |
| 6330 | } else |
| 6331 | ret->content = NULL; |
| 6332 | return(ret); |
| 6333 | } |
| 6334 | |
| 6335 | /** |
| 6336 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6337 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6338 | * @scheme: allocation scheme to use |
| 6339 | * |
| 6340 | * Sets the allocation scheme for this buffer |
| 6341 | */ |
| 6342 | void |
| 6343 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 6344 | xmlBufferAllocationScheme scheme) { |
| 6345 | if (buf == NULL) { |
| 6346 | #ifdef DEBUG_BUFFER |
| 6347 | xmlGenericError(xmlGenericErrorContext, |
| 6348 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 6349 | #endif |
| 6350 | return; |
| 6351 | } |
| 6352 | |
| 6353 | buf->alloc = scheme; |
| 6354 | } |
| 6355 | |
| 6356 | /** |
| 6357 | * xmlBufferFree: |
| 6358 | * @buf: the buffer to free |
| 6359 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 6360 | * Frees an XML buffer. It frees both the content and the structure which |
| 6361 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6362 | */ |
| 6363 | void |
| 6364 | xmlBufferFree(xmlBufferPtr buf) { |
| 6365 | if (buf == NULL) { |
| 6366 | #ifdef DEBUG_BUFFER |
| 6367 | xmlGenericError(xmlGenericErrorContext, |
| 6368 | "xmlBufferFree: buf == NULL\n"); |
| 6369 | #endif |
| 6370 | return; |
| 6371 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 6372 | if (buf->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6373 | xmlFree(buf->content); |
| 6374 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6375 | xmlFree(buf); |
| 6376 | } |
| 6377 | |
| 6378 | /** |
| 6379 | * xmlBufferEmpty: |
| 6380 | * @buf: the buffer |
| 6381 | * |
| 6382 | * empty a buffer. |
| 6383 | */ |
| 6384 | void |
| 6385 | xmlBufferEmpty(xmlBufferPtr buf) { |
| 6386 | if (buf->content == NULL) return; |
| 6387 | buf->use = 0; |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 6388 | memset(buf->content, 0, buf->size); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6389 | } |
| 6390 | |
| 6391 | /** |
| 6392 | * xmlBufferShrink: |
| 6393 | * @buf: the buffer to dump |
| 6394 | * @len: the number of xmlChar to remove |
| 6395 | * |
| 6396 | * Remove the beginning of an XML buffer. |
| 6397 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6398 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6399 | */ |
| 6400 | int |
| 6401 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
| 6402 | if (len == 0) return(0); |
| 6403 | if (len > buf->use) return(-1); |
| 6404 | |
| 6405 | buf->use -= len; |
| 6406 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
| 6407 | |
| 6408 | buf->content[buf->use] = 0; |
| 6409 | return(len); |
| 6410 | } |
| 6411 | |
| 6412 | /** |
| 6413 | * xmlBufferGrow: |
| 6414 | * @buf: the buffer |
| 6415 | * @len: the minimum free size to allocate |
| 6416 | * |
| 6417 | * Grow the available space of an XML buffer. |
| 6418 | * |
| 6419 | * Returns the new available space or -1 in case of error |
| 6420 | */ |
| 6421 | int |
| 6422 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 6423 | int size; |
| 6424 | xmlChar *newbuf; |
| 6425 | |
| 6426 | if (len + buf->use < buf->size) return(0); |
| 6427 | |
| 6428 | size = buf->use + len + 100; |
| 6429 | |
| 6430 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
| 6431 | if (newbuf == NULL) return(-1); |
| 6432 | buf->content = newbuf; |
| 6433 | buf->size = size; |
| 6434 | return(buf->size - buf->use); |
| 6435 | } |
| 6436 | |
| 6437 | /** |
| 6438 | * xmlBufferDump: |
| 6439 | * @file: the file output |
| 6440 | * @buf: the buffer to dump |
| 6441 | * |
| 6442 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6443 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6444 | */ |
| 6445 | int |
| 6446 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 6447 | int ret; |
| 6448 | |
| 6449 | if (buf == NULL) { |
| 6450 | #ifdef DEBUG_BUFFER |
| 6451 | xmlGenericError(xmlGenericErrorContext, |
| 6452 | "xmlBufferDump: buf == NULL\n"); |
| 6453 | #endif |
| 6454 | return(0); |
| 6455 | } |
| 6456 | if (buf->content == NULL) { |
| 6457 | #ifdef DEBUG_BUFFER |
| 6458 | xmlGenericError(xmlGenericErrorContext, |
| 6459 | "xmlBufferDump: buf->content == NULL\n"); |
| 6460 | #endif |
| 6461 | return(0); |
| 6462 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 6463 | if (file == NULL) |
| 6464 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6465 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 6466 | return(ret); |
| 6467 | } |
| 6468 | |
| 6469 | /** |
| 6470 | * xmlBufferContent: |
| 6471 | * @buf: the buffer |
| 6472 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6473 | * Function to extract the content of a buffer |
| 6474 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6475 | * Returns the internal content |
| 6476 | */ |
| 6477 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6478 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6479 | xmlBufferContent(const xmlBufferPtr buf) |
| 6480 | { |
| 6481 | if(!buf) |
| 6482 | return NULL; |
| 6483 | |
| 6484 | return buf->content; |
| 6485 | } |
| 6486 | |
| 6487 | /** |
| 6488 | * xmlBufferLength: |
| 6489 | * @buf: the buffer |
| 6490 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6491 | * Function to get the length of a buffer |
| 6492 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6493 | * Returns the length of data in the internal content |
| 6494 | */ |
| 6495 | |
| 6496 | int |
| 6497 | xmlBufferLength(const xmlBufferPtr buf) |
| 6498 | { |
| 6499 | if(!buf) |
| 6500 | return 0; |
| 6501 | |
| 6502 | return buf->use; |
| 6503 | } |
| 6504 | |
| 6505 | /** |
| 6506 | * xmlBufferResize: |
| 6507 | * @buf: the buffer to resize |
| 6508 | * @size: the desired size |
| 6509 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6510 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6511 | * |
| 6512 | * Returns 0 in case of problems, 1 otherwise |
| 6513 | */ |
| 6514 | int |
| 6515 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 6516 | { |
| 6517 | unsigned int newSize; |
| 6518 | xmlChar* rebuf = NULL; |
| 6519 | |
| 6520 | /*take care of empty case*/ |
| 6521 | newSize = (buf->size ? buf->size*2 : size); |
| 6522 | |
| 6523 | /* Don't resize if we don't have to */ |
| 6524 | if (size < buf->size) |
| 6525 | return 1; |
| 6526 | |
| 6527 | /* figure out new size */ |
| 6528 | switch (buf->alloc){ |
| 6529 | case XML_BUFFER_ALLOC_DOUBLEIT: |
| 6530 | while (size > newSize) newSize *= 2; |
| 6531 | break; |
| 6532 | case XML_BUFFER_ALLOC_EXACT: |
| 6533 | newSize = size+10; |
| 6534 | break; |
| 6535 | default: |
| 6536 | newSize = size+10; |
| 6537 | break; |
| 6538 | } |
| 6539 | |
| 6540 | if (buf->content == NULL) |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6541 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6542 | else if (buf->size - buf->use < 100) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6543 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 6544 | newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6545 | } else { |
| 6546 | /* |
| 6547 | * if we are reallocating a buffer far from being full, it's |
| 6548 | * better to make a new allocation and copy only the used range |
| 6549 | * and free the old one. |
| 6550 | */ |
| 6551 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
| 6552 | if (rebuf != NULL) { |
| 6553 | memcpy(rebuf, buf->content, buf->use); |
| 6554 | xmlFree(buf->content); |
| 6555 | } |
Daniel Veillard | e598408 | 2003-08-19 22:21:13 +0000 | [diff] [blame] | 6556 | rebuf[buf->use] = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6557 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6558 | if (rebuf == NULL) { |
| 6559 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6560 | "xmlBufferResize : out of memory!\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6561 | return 0; |
| 6562 | } |
| 6563 | buf->content = rebuf; |
| 6564 | buf->size = newSize; |
| 6565 | |
| 6566 | return 1; |
| 6567 | } |
| 6568 | |
| 6569 | /** |
| 6570 | * xmlBufferAdd: |
| 6571 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6572 | * @str: the #xmlChar string |
| 6573 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6574 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6575 | * Add a string range to an XML buffer. if len == -1, the length of |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6576 | * str is recomputed. |
| 6577 | */ |
| 6578 | void |
| 6579 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6580 | unsigned int needSize; |
| 6581 | |
| 6582 | if (str == NULL) { |
| 6583 | #ifdef DEBUG_BUFFER |
| 6584 | xmlGenericError(xmlGenericErrorContext, |
| 6585 | "xmlBufferAdd: str == NULL\n"); |
| 6586 | #endif |
| 6587 | return; |
| 6588 | } |
| 6589 | if (len < -1) { |
| 6590 | #ifdef DEBUG_BUFFER |
| 6591 | xmlGenericError(xmlGenericErrorContext, |
| 6592 | "xmlBufferAdd: len < 0\n"); |
| 6593 | #endif |
| 6594 | return; |
| 6595 | } |
| 6596 | if (len == 0) return; |
| 6597 | |
| 6598 | if (len < 0) |
| 6599 | len = xmlStrlen(str); |
| 6600 | |
| 6601 | if (len <= 0) return; |
| 6602 | |
| 6603 | needSize = buf->use + len + 2; |
| 6604 | if (needSize > buf->size){ |
| 6605 | if (!xmlBufferResize(buf, needSize)){ |
| 6606 | xmlGenericError(xmlGenericErrorContext, |
| 6607 | "xmlBufferAdd : out of memory!\n"); |
| 6608 | return; |
| 6609 | } |
| 6610 | } |
| 6611 | |
| 6612 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 6613 | buf->use += len; |
| 6614 | buf->content[buf->use] = 0; |
| 6615 | } |
| 6616 | |
| 6617 | /** |
| 6618 | * xmlBufferAddHead: |
| 6619 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6620 | * @str: the #xmlChar string |
| 6621 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6622 | * |
| 6623 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6624 | * if len == -1, the length of @str is recomputed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6625 | */ |
| 6626 | void |
| 6627 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6628 | unsigned int needSize; |
| 6629 | |
| 6630 | if (str == NULL) { |
| 6631 | #ifdef DEBUG_BUFFER |
| 6632 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6633 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6634 | #endif |
| 6635 | return; |
| 6636 | } |
| 6637 | if (len < -1) { |
| 6638 | #ifdef DEBUG_BUFFER |
| 6639 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6640 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6641 | #endif |
| 6642 | return; |
| 6643 | } |
| 6644 | if (len == 0) return; |
| 6645 | |
| 6646 | if (len < 0) |
| 6647 | len = xmlStrlen(str); |
| 6648 | |
| 6649 | if (len <= 0) return; |
| 6650 | |
| 6651 | needSize = buf->use + len + 2; |
| 6652 | if (needSize > buf->size){ |
| 6653 | if (!xmlBufferResize(buf, needSize)){ |
| 6654 | xmlGenericError(xmlGenericErrorContext, |
| 6655 | "xmlBufferAddHead : out of memory!\n"); |
| 6656 | return; |
| 6657 | } |
| 6658 | } |
| 6659 | |
| 6660 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 6661 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 6662 | buf->use += len; |
| 6663 | buf->content[buf->use] = 0; |
| 6664 | } |
| 6665 | |
| 6666 | /** |
| 6667 | * xmlBufferCat: |
| 6668 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6669 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6670 | * |
| 6671 | * Append a zero terminated string to an XML buffer. |
| 6672 | */ |
| 6673 | void |
| 6674 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
| 6675 | if (str != NULL) |
| 6676 | xmlBufferAdd(buf, str, -1); |
| 6677 | } |
| 6678 | |
| 6679 | /** |
| 6680 | * xmlBufferCCat: |
| 6681 | * @buf: the buffer to dump |
| 6682 | * @str: the C char string |
| 6683 | * |
| 6684 | * Append a zero terminated C string to an XML buffer. |
| 6685 | */ |
| 6686 | void |
| 6687 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 6688 | const char *cur; |
| 6689 | |
| 6690 | if (str == NULL) { |
| 6691 | #ifdef DEBUG_BUFFER |
| 6692 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6693 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6694 | #endif |
| 6695 | return; |
| 6696 | } |
| 6697 | for (cur = str;*cur != 0;cur++) { |
| 6698 | if (buf->use + 10 >= buf->size) { |
| 6699 | if (!xmlBufferResize(buf, buf->use+10)){ |
| 6700 | xmlGenericError(xmlGenericErrorContext, |
| 6701 | "xmlBufferCCat : out of memory!\n"); |
| 6702 | return; |
| 6703 | } |
| 6704 | } |
| 6705 | buf->content[buf->use++] = *cur; |
| 6706 | } |
| 6707 | buf->content[buf->use] = 0; |
| 6708 | } |
| 6709 | |
| 6710 | /** |
| 6711 | * xmlBufferWriteCHAR: |
| 6712 | * @buf: the XML buffer |
| 6713 | * @string: the string to add |
| 6714 | * |
| 6715 | * routine which manages and grows an output buffer. This one adds |
| 6716 | * xmlChars at the end of the buffer. |
| 6717 | */ |
| 6718 | void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6719 | xmlBufferWriteCHAR |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6720 | (xmlBufferPtr buf, const xmlChar *string) { |
| 6721 | xmlBufferCat(buf, string); |
| 6722 | } |
| 6723 | |
| 6724 | /** |
| 6725 | * xmlBufferWriteChar: |
| 6726 | * @buf: the XML buffer output |
| 6727 | * @string: the string to add |
| 6728 | * |
| 6729 | * routine which manage and grows an output buffer. This one add |
| 6730 | * C chars at the end of the array. |
| 6731 | */ |
| 6732 | void |
| 6733 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
| 6734 | xmlBufferCCat(buf, string); |
| 6735 | } |
| 6736 | |
| 6737 | |
| 6738 | /** |
| 6739 | * xmlBufferWriteQuotedString: |
| 6740 | * @buf: the XML buffer output |
| 6741 | * @string: the string to add |
| 6742 | * |
| 6743 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6744 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6745 | * quote or double-quotes internally |
| 6746 | */ |
| 6747 | void |
| 6748 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6749 | const xmlChar *cur, *base; |
| 6750 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 6751 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6752 | #ifdef DEBUG_BUFFER |
| 6753 | xmlGenericError(xmlGenericErrorContext, |
| 6754 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 6755 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6756 | xmlBufferCCat(buf, "\""); |
| 6757 | base = cur = string; |
| 6758 | while(*cur != 0){ |
| 6759 | if(*cur == '"'){ |
| 6760 | if (base != cur) |
| 6761 | xmlBufferAdd(buf, base, cur - base); |
| 6762 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 6763 | cur++; |
| 6764 | base = cur; |
| 6765 | } |
| 6766 | else { |
| 6767 | cur++; |
| 6768 | } |
| 6769 | } |
| 6770 | if (base != cur) |
| 6771 | xmlBufferAdd(buf, base, cur - base); |
| 6772 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6773 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6774 | else{ |
| 6775 | xmlBufferCCat(buf, "\'"); |
| 6776 | xmlBufferCat(buf, string); |
| 6777 | xmlBufferCCat(buf, "\'"); |
| 6778 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6779 | } else { |
| 6780 | xmlBufferCCat(buf, "\""); |
| 6781 | xmlBufferCat(buf, string); |
| 6782 | xmlBufferCCat(buf, "\""); |
| 6783 | } |
| 6784 | } |
| 6785 | |
| 6786 | |
| 6787 | /************************************************************************ |
| 6788 | * * |
| 6789 | * Dumping XML tree content to a simple buffer * |
| 6790 | * * |
| 6791 | ************************************************************************/ |
| 6792 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6793 | /** |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6794 | * xmlAttrSerializeContent: |
| 6795 | * @buf: the XML buffer output |
| 6796 | * @doc: the document |
| 6797 | * @attr: the attribute pointer |
| 6798 | * |
| 6799 | * Serialize the attribute in the buffer |
| 6800 | */ |
| 6801 | static void |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6802 | xmlAttrSerializeContent(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr attr) |
| 6803 | { |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6804 | const xmlChar *cur, *base; |
| 6805 | xmlNodePtr children; |
| 6806 | |
| 6807 | children = attr->children; |
| 6808 | while (children != NULL) { |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6809 | switch (children->type) { |
| 6810 | case XML_TEXT_NODE: |
| 6811 | base = cur = children->content; |
| 6812 | while (*cur != 0) { |
| 6813 | if (*cur == '\n') { |
| 6814 | if (base != cur) |
| 6815 | xmlBufferAdd(buf, base, cur - base); |
| 6816 | xmlBufferAdd(buf, BAD_CAST " ", 5); |
| 6817 | cur++; |
| 6818 | base = cur; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 6819 | } else if (*cur == '\r') { |
| 6820 | if (base != cur) |
| 6821 | xmlBufferAdd(buf, base, cur - base); |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 6822 | xmlBufferAdd(buf, BAD_CAST " ", 5); |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 6823 | cur++; |
| 6824 | base = cur; |
| 6825 | } else if (*cur == '\t') { |
| 6826 | if (base != cur) |
| 6827 | xmlBufferAdd(buf, base, cur - base); |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 6828 | xmlBufferAdd(buf, BAD_CAST "	", 4); |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 6829 | cur++; |
| 6830 | base = cur; |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6831 | #if 0 |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6832 | } else if (*cur == '\'') { |
| 6833 | if (base != cur) |
| 6834 | xmlBufferAdd(buf, base, cur - base); |
| 6835 | xmlBufferAdd(buf, BAD_CAST "'", 6); |
| 6836 | cur++; |
| 6837 | base = cur; |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6838 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6839 | } else if (*cur == '"') { |
| 6840 | if (base != cur) |
| 6841 | xmlBufferAdd(buf, base, cur - base); |
| 6842 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 6843 | cur++; |
| 6844 | base = cur; |
| 6845 | } else if (*cur == '<') { |
| 6846 | if (base != cur) |
| 6847 | xmlBufferAdd(buf, base, cur - base); |
| 6848 | xmlBufferAdd(buf, BAD_CAST "<", 4); |
| 6849 | cur++; |
| 6850 | base = cur; |
| 6851 | } else if (*cur == '>') { |
| 6852 | if (base != cur) |
| 6853 | xmlBufferAdd(buf, base, cur - base); |
| 6854 | xmlBufferAdd(buf, BAD_CAST ">", 4); |
| 6855 | cur++; |
| 6856 | base = cur; |
| 6857 | } else if (*cur == '&') { |
| 6858 | if (base != cur) |
| 6859 | xmlBufferAdd(buf, base, cur - base); |
| 6860 | xmlBufferAdd(buf, BAD_CAST "&", 5); |
| 6861 | cur++; |
| 6862 | base = cur; |
| 6863 | } else if ((*cur >= 0x80) && ((doc == NULL) || |
| 6864 | (doc->encoding == |
| 6865 | NULL))) { |
| 6866 | /* |
| 6867 | * We assume we have UTF-8 content. |
| 6868 | */ |
| 6869 | char tmp[10]; |
| 6870 | int val = 0, l = 1; |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6871 | |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6872 | if (base != cur) |
| 6873 | xmlBufferAdd(buf, base, cur - base); |
| 6874 | if (*cur < 0xC0) { |
| 6875 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6876 | "xmlAttrSerializeContent : input not UTF-8\n"); |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6877 | if (doc != NULL) |
| 6878 | doc->encoding = |
| 6879 | xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 6880 | snprintf(tmp, sizeof(tmp), "&#%d;", *cur); |
| 6881 | tmp[sizeof(tmp) - 1] = 0; |
| 6882 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 6883 | cur++; |
| 6884 | base = cur; |
| 6885 | continue; |
| 6886 | } else if (*cur < 0xE0) { |
| 6887 | val = (cur[0]) & 0x1F; |
| 6888 | val <<= 6; |
| 6889 | val |= (cur[1]) & 0x3F; |
| 6890 | l = 2; |
| 6891 | } else if (*cur < 0xF0) { |
| 6892 | val = (cur[0]) & 0x0F; |
| 6893 | val <<= 6; |
| 6894 | val |= (cur[1]) & 0x3F; |
| 6895 | val <<= 6; |
| 6896 | val |= (cur[2]) & 0x3F; |
| 6897 | l = 3; |
| 6898 | } else if (*cur < 0xF8) { |
| 6899 | val = (cur[0]) & 0x07; |
| 6900 | val <<= 6; |
| 6901 | val |= (cur[1]) & 0x3F; |
| 6902 | val <<= 6; |
| 6903 | val |= (cur[2]) & 0x3F; |
| 6904 | val <<= 6; |
| 6905 | val |= (cur[3]) & 0x3F; |
| 6906 | l = 4; |
| 6907 | } |
| 6908 | if ((l == 1) || (!IS_CHAR(val))) { |
| 6909 | xmlGenericError(xmlGenericErrorContext, |
| 6910 | "xmlAttrSerializeContent : char out of range\n"); |
| 6911 | if (doc != NULL) |
| 6912 | doc->encoding = |
| 6913 | xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 6914 | snprintf(tmp, sizeof(tmp), "&#%d;", *cur); |
| 6915 | tmp[sizeof(tmp) - 1] = 0; |
| 6916 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 6917 | cur++; |
| 6918 | base = cur; |
| 6919 | continue; |
| 6920 | } |
| 6921 | /* |
| 6922 | * We could do multiple things here. Just save |
| 6923 | * as a char ref |
| 6924 | */ |
| 6925 | snprintf(tmp, sizeof(tmp), "&#x%X;", val); |
| 6926 | tmp[sizeof(tmp) - 1] = 0; |
| 6927 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 6928 | cur += l; |
| 6929 | base = cur; |
| 6930 | } else { |
| 6931 | cur++; |
| 6932 | } |
| 6933 | } |
| 6934 | if (base != cur) |
| 6935 | xmlBufferAdd(buf, base, cur - base); |
| 6936 | break; |
| 6937 | case XML_ENTITY_REF_NODE: |
| 6938 | xmlBufferAdd(buf, BAD_CAST "&", 1); |
| 6939 | xmlBufferAdd(buf, children->name, |
| 6940 | xmlStrlen(children->name)); |
| 6941 | xmlBufferAdd(buf, BAD_CAST ";", 1); |
| 6942 | break; |
| 6943 | default: |
| 6944 | /* should not happen unless we have a badly built tree */ |
| 6945 | break; |
| 6946 | } |
| 6947 | children = children->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6948 | } |
| 6949 | } |
| 6950 | |
| 6951 | /** |
| 6952 | * xmlNodeDump: |
| 6953 | * @buf: the XML buffer output |
| 6954 | * @doc: the document |
| 6955 | * @cur: the current node |
| 6956 | * @level: the imbrication level for indenting |
| 6957 | * @format: is formatting allowed |
| 6958 | * |
| 6959 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 6960 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 6961 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6962 | * |
| 6963 | * Returns the number of bytes written to the buffer or -1 in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6964 | */ |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6965 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6966 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6967 | int format) |
| 6968 | { |
| 6969 | unsigned int use; |
| 6970 | int ret; |
| 6971 | xmlOutputBufferPtr outbuf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6972 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 6973 | xmlInitParser(); |
| 6974 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6975 | if (cur == NULL) { |
| 6976 | #ifdef DEBUG_TREE |
| 6977 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6978 | "xmlNodeDump : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6979 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6980 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6981 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6982 | if (buf == NULL) { |
| 6983 | #ifdef DEBUG_TREE |
| 6984 | xmlGenericError(xmlGenericErrorContext, |
| 6985 | "xmlNodeDump : buf == NULL\n"); |
| 6986 | #endif |
| 6987 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6988 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6989 | outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); |
| 6990 | if (outbuf == NULL) { |
| 6991 | xmlGenericError(xmlGenericErrorContext, |
| 6992 | "xmlNodeDump: out of memory!\n"); |
| 6993 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6994 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6995 | memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer)); |
| 6996 | outbuf->buffer = buf; |
| 6997 | outbuf->encoder = NULL; |
| 6998 | outbuf->writecallback = NULL; |
| 6999 | outbuf->closecallback = NULL; |
| 7000 | outbuf->context = NULL; |
| 7001 | outbuf->written = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7002 | |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7003 | use = buf->use; |
| 7004 | xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL); |
| 7005 | xmlFree(outbuf); |
| 7006 | ret = buf->use - use; |
| 7007 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7008 | } |
| 7009 | |
| 7010 | /** |
| 7011 | * xmlElemDump: |
| 7012 | * @f: the FILE * for the output |
| 7013 | * @doc: the document |
| 7014 | * @cur: the current node |
| 7015 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7016 | * Dump an XML/HTML node, recursive behaviour, children are printed too. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7017 | */ |
| 7018 | void |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7019 | xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur) |
| 7020 | { |
| 7021 | xmlOutputBufferPtr outbuf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7022 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7023 | xmlInitParser(); |
| 7024 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7025 | if (cur == NULL) { |
| 7026 | #ifdef DEBUG_TREE |
| 7027 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7028 | "xmlElemDump : cur == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7029 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7030 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7031 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7032 | #ifdef DEBUG_TREE |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7033 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7034 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7035 | "xmlElemDump : doc == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7036 | } |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7037 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7038 | |
| 7039 | outbuf = xmlOutputBufferCreateFile(f, NULL); |
| 7040 | if (outbuf == NULL) |
| 7041 | return; |
| 7042 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7043 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7044 | htmlNodeDumpOutput(outbuf, doc, cur, NULL); |
| 7045 | #else |
| 7046 | xmlGenericError(xmlGenericErrorContext, |
| 7047 | "HTML support not compiled in\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7048 | #endif /* LIBXML_HTML_ENABLED */ |
| 7049 | } else |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7050 | xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL); |
| 7051 | xmlOutputBufferClose(outbuf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7052 | } |
| 7053 | |
| 7054 | /************************************************************************ |
| 7055 | * * |
| 7056 | * Dumping XML tree content to an I/O output buffer * |
| 7057 | * * |
| 7058 | ************************************************************************/ |
| 7059 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7060 | static void |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7061 | xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 7062 | int level, int format, const char *encoding); |
| 7063 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7064 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 7065 | int level, int format, const char *encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7066 | static void |
| 7067 | xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7068 | xmlNodePtr cur, int level, int format, const char *encoding); |
| 7069 | |
Daniel Veillard | 5ecaf7f | 2003-01-09 13:19:33 +0000 | [diff] [blame] | 7070 | void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur); |
| 7071 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7072 | /** |
| 7073 | * xmlNsDumpOutput: |
| 7074 | * @buf: the XML buffer output |
| 7075 | * @cur: a namespace |
| 7076 | * |
| 7077 | * Dump a local Namespace definition. |
| 7078 | * Should be called in the context of attributes dumps. |
| 7079 | */ |
| 7080 | static void |
| 7081 | xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 7082 | if (cur == NULL) { |
| 7083 | #ifdef DEBUG_TREE |
| 7084 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7085 | "xmlNsDumpOutput : Ns == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7086 | #endif |
| 7087 | return; |
| 7088 | } |
| 7089 | if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 7090 | if (xmlStrEqual(cur->prefix, BAD_CAST "xml")) |
| 7091 | return; |
| 7092 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7093 | /* Within the context of an element attributes */ |
| 7094 | if (cur->prefix != NULL) { |
| 7095 | xmlOutputBufferWriteString(buf, " xmlns:"); |
| 7096 | xmlOutputBufferWriteString(buf, (const char *)cur->prefix); |
| 7097 | } else |
| 7098 | xmlOutputBufferWriteString(buf, " xmlns"); |
| 7099 | xmlOutputBufferWriteString(buf, "="); |
| 7100 | xmlBufferWriteQuotedString(buf->buffer, cur->href); |
| 7101 | } |
| 7102 | } |
| 7103 | |
| 7104 | /** |
| 7105 | * xmlNsListDumpOutput: |
| 7106 | * @buf: the XML buffer output |
| 7107 | * @cur: the first namespace |
| 7108 | * |
| 7109 | * Dump a list of local Namespace definitions. |
| 7110 | * Should be called in the context of attributes dumps. |
| 7111 | */ |
Daniel Veillard | 5ecaf7f | 2003-01-09 13:19:33 +0000 | [diff] [blame] | 7112 | void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7113 | xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 7114 | while (cur != NULL) { |
| 7115 | xmlNsDumpOutput(buf, cur); |
| 7116 | cur = cur->next; |
| 7117 | } |
| 7118 | } |
| 7119 | |
| 7120 | /** |
| 7121 | * xmlDtdDumpOutput: |
| 7122 | * @buf: the XML buffer output |
| 7123 | * @doc: the document |
| 7124 | * @encoding: an optional encoding string |
| 7125 | * |
| 7126 | * Dump the XML document DTD, if any. |
| 7127 | */ |
| 7128 | static void |
| 7129 | xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) { |
| 7130 | if (dtd == NULL) { |
| 7131 | #ifdef DEBUG_TREE |
| 7132 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7133 | "xmlDtdDumpOutput : no internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7134 | #endif |
| 7135 | return; |
| 7136 | } |
| 7137 | xmlOutputBufferWriteString(buf, "<!DOCTYPE "); |
| 7138 | xmlOutputBufferWriteString(buf, (const char *)dtd->name); |
| 7139 | if (dtd->ExternalID != NULL) { |
| 7140 | xmlOutputBufferWriteString(buf, " PUBLIC "); |
| 7141 | xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID); |
| 7142 | xmlOutputBufferWriteString(buf, " "); |
| 7143 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 7144 | } else if (dtd->SystemID != NULL) { |
| 7145 | xmlOutputBufferWriteString(buf, " SYSTEM "); |
| 7146 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 7147 | } |
| 7148 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 7149 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 7150 | xmlOutputBufferWriteString(buf, ">"); |
| 7151 | return; |
| 7152 | } |
| 7153 | xmlOutputBufferWriteString(buf, " [\n"); |
| 7154 | xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding); |
| 7155 | xmlOutputBufferWriteString(buf, "]>"); |
| 7156 | } |
| 7157 | |
| 7158 | /** |
| 7159 | * xmlAttrDumpOutput: |
| 7160 | * @buf: the XML buffer output |
| 7161 | * @doc: the document |
| 7162 | * @cur: the attribute pointer |
| 7163 | * @encoding: an optional encoding string |
| 7164 | * |
| 7165 | * Dump an XML attribute |
| 7166 | */ |
| 7167 | static void |
| 7168 | xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 7169 | const char *encoding ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7170 | if (cur == NULL) { |
| 7171 | #ifdef DEBUG_TREE |
| 7172 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7173 | "xmlAttrDumpOutput : property == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7174 | #endif |
| 7175 | return; |
| 7176 | } |
| 7177 | xmlOutputBufferWriteString(buf, " "); |
| 7178 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7179 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7180 | xmlOutputBufferWriteString(buf, ":"); |
| 7181 | } |
| 7182 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 7183 | xmlOutputBufferWriteString(buf, "=\""); |
| 7184 | xmlAttrSerializeContent(buf->buffer, doc, cur); |
| 7185 | xmlOutputBufferWriteString(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7186 | } |
| 7187 | |
| 7188 | /** |
| 7189 | * xmlAttrListDumpOutput: |
| 7190 | * @buf: the XML buffer output |
| 7191 | * @doc: the document |
| 7192 | * @cur: the first attribute pointer |
| 7193 | * @encoding: an optional encoding string |
| 7194 | * |
| 7195 | * Dump a list of XML attributes |
| 7196 | */ |
| 7197 | static void |
| 7198 | xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7199 | xmlAttrPtr cur, const char *encoding) { |
| 7200 | if (cur == NULL) { |
| 7201 | #ifdef DEBUG_TREE |
| 7202 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7203 | "xmlAttrListDumpOutput : property == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7204 | #endif |
| 7205 | return; |
| 7206 | } |
| 7207 | while (cur != NULL) { |
| 7208 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 7209 | cur = cur->next; |
| 7210 | } |
| 7211 | } |
| 7212 | |
| 7213 | |
| 7214 | |
| 7215 | /** |
| 7216 | * xmlNodeListDumpOutput: |
| 7217 | * @buf: the XML buffer output |
| 7218 | * @doc: the document |
| 7219 | * @cur: the first node |
| 7220 | * @level: the imbrication level for indenting |
| 7221 | * @format: is formatting allowed |
| 7222 | * @encoding: an optional encoding string |
| 7223 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7224 | * Dump an XML node list, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7225 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7226 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7227 | */ |
| 7228 | static void |
| 7229 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7230 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 7231 | int i; |
| 7232 | |
| 7233 | if (cur == NULL) { |
| 7234 | #ifdef DEBUG_TREE |
| 7235 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7236 | "xmlNodeListDumpOutput : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7237 | #endif |
| 7238 | return; |
| 7239 | } |
| 7240 | while (cur != NULL) { |
| 7241 | if ((format) && (xmlIndentTreeOutput) && |
| 7242 | (cur->type == XML_ELEMENT_NODE)) |
| 7243 | for (i = 0;i < level;i++) |
Aleksey Sanin | 2300256 | 2002-05-24 07:18:40 +0000 | [diff] [blame] | 7244 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7245 | xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7246 | if (format) { |
| 7247 | xmlOutputBufferWriteString(buf, "\n"); |
| 7248 | } |
| 7249 | cur = cur->next; |
| 7250 | } |
| 7251 | } |
| 7252 | |
| 7253 | /** |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7254 | * xmlNodeDumpOutputInternal: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7255 | * @buf: the XML buffer output |
| 7256 | * @doc: the document |
| 7257 | * @cur: the current node |
| 7258 | * @level: the imbrication level for indenting |
| 7259 | * @format: is formatting allowed |
| 7260 | * @encoding: an optional encoding string |
| 7261 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7262 | * Dump an XML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7263 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7264 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7265 | */ |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7266 | static void |
| 7267 | xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7268 | xmlNodePtr cur, int level, int format, const char *encoding) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7269 | int i; |
| 7270 | xmlNodePtr tmp; |
| 7271 | |
| 7272 | if (cur == NULL) { |
| 7273 | #ifdef DEBUG_TREE |
| 7274 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7275 | "xmlNodeDumpOutput : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7276 | #endif |
| 7277 | return; |
| 7278 | } |
| 7279 | if (cur->type == XML_XINCLUDE_START) |
| 7280 | return; |
| 7281 | if (cur->type == XML_XINCLUDE_END) |
| 7282 | return; |
| 7283 | if (cur->type == XML_DTD_NODE) { |
| 7284 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 7285 | return; |
| 7286 | } |
| 7287 | if (cur->type == XML_ELEMENT_DECL) { |
| 7288 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 7289 | return; |
| 7290 | } |
| 7291 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 7292 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 7293 | return; |
| 7294 | } |
| 7295 | if (cur->type == XML_ENTITY_DECL) { |
| 7296 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 7297 | return; |
| 7298 | } |
| 7299 | if (cur->type == XML_TEXT_NODE) { |
| 7300 | if (cur->content != NULL) { |
| 7301 | if ((cur->name == xmlStringText) || |
| 7302 | (cur->name != xmlStringTextNoenc)) { |
| 7303 | xmlChar *buffer; |
| 7304 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7305 | if (encoding == NULL) |
| 7306 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7307 | else |
| 7308 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7309 | if (buffer != NULL) { |
| 7310 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7311 | xmlFree(buffer); |
| 7312 | } |
| 7313 | } else { |
| 7314 | /* |
| 7315 | * Disable escaping, needed for XSLT |
| 7316 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7317 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7318 | } |
| 7319 | } |
| 7320 | |
| 7321 | return; |
| 7322 | } |
| 7323 | if (cur->type == XML_PI_NODE) { |
| 7324 | if (cur->content != NULL) { |
| 7325 | xmlOutputBufferWriteString(buf, "<?"); |
| 7326 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7327 | if (cur->content != NULL) { |
| 7328 | xmlOutputBufferWriteString(buf, " "); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7329 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7330 | } |
| 7331 | xmlOutputBufferWriteString(buf, "?>"); |
| 7332 | } else { |
| 7333 | xmlOutputBufferWriteString(buf, "<?"); |
| 7334 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7335 | xmlOutputBufferWriteString(buf, "?>"); |
| 7336 | } |
| 7337 | return; |
| 7338 | } |
| 7339 | if (cur->type == XML_COMMENT_NODE) { |
| 7340 | if (cur->content != NULL) { |
| 7341 | xmlOutputBufferWriteString(buf, "<!--"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7342 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7343 | xmlOutputBufferWriteString(buf, "-->"); |
| 7344 | } |
| 7345 | return; |
| 7346 | } |
| 7347 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 7348 | xmlOutputBufferWriteString(buf, "&"); |
| 7349 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7350 | xmlOutputBufferWriteString(buf, ";"); |
| 7351 | return; |
| 7352 | } |
| 7353 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 7354 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 7355 | if (cur->content != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7356 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7357 | xmlOutputBufferWriteString(buf, "]]>"); |
| 7358 | return; |
| 7359 | } |
Daniel Veillard | 7b72ee5 | 2003-02-27 23:24:53 +0000 | [diff] [blame] | 7360 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 7361 | xmlAttrDumpOutput(buf,doc, (xmlAttrPtr) cur,encoding); |
Daniel Veillard | 7b72ee5 | 2003-02-27 23:24:53 +0000 | [diff] [blame] | 7362 | return; |
| 7363 | } |
Daniel Veillard | 6aa2f60 | 2003-02-10 00:01:56 +0000 | [diff] [blame] | 7364 | if (cur->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7365 | xmlNsDumpOutput(buf, (xmlNsPtr) cur); |
Daniel Veillard | 6aa2f60 | 2003-02-10 00:01:56 +0000 | [diff] [blame] | 7366 | return; |
| 7367 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7368 | |
| 7369 | if (format == 1) { |
| 7370 | tmp = cur->children; |
| 7371 | while (tmp != NULL) { |
| 7372 | if ((tmp->type == XML_TEXT_NODE) || |
| 7373 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 7374 | format = 0; |
| 7375 | break; |
| 7376 | } |
| 7377 | tmp = tmp->next; |
| 7378 | } |
| 7379 | } |
| 7380 | xmlOutputBufferWriteString(buf, "<"); |
| 7381 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7382 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7383 | xmlOutputBufferWriteString(buf, ":"); |
| 7384 | } |
| 7385 | |
| 7386 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7387 | if (cur->nsDef) |
| 7388 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 7389 | if (cur->properties != NULL) |
| 7390 | xmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 7391 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 7392 | if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) && |
| 7393 | (cur->children == NULL) && (!xmlSaveNoEmptyTags)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7394 | xmlOutputBufferWriteString(buf, "/>"); |
| 7395 | return; |
| 7396 | } |
| 7397 | xmlOutputBufferWriteString(buf, ">"); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 7398 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7399 | xmlChar *buffer; |
| 7400 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7401 | if (encoding == NULL) |
| 7402 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7403 | else |
| 7404 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7405 | if (buffer != NULL) { |
| 7406 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7407 | xmlFree(buffer); |
| 7408 | } |
| 7409 | } |
| 7410 | if (cur->children != NULL) { |
| 7411 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 7412 | xmlNodeListDumpOutput(buf, doc, cur->children, |
| 7413 | (level >= 0?level+1:-1), format, encoding); |
| 7414 | if ((xmlIndentTreeOutput) && (format)) |
| 7415 | for (i = 0;i < level;i++) |
Aleksey Sanin | 2300256 | 2002-05-24 07:18:40 +0000 | [diff] [blame] | 7416 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7417 | } |
| 7418 | xmlOutputBufferWriteString(buf, "</"); |
| 7419 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7420 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7421 | xmlOutputBufferWriteString(buf, ":"); |
| 7422 | } |
| 7423 | |
| 7424 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7425 | xmlOutputBufferWriteString(buf, ">"); |
| 7426 | } |
| 7427 | |
| 7428 | /** |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7429 | * xmlNodeDumpOutput: |
| 7430 | * @buf: the XML buffer output |
| 7431 | * @doc: the document |
| 7432 | * @cur: the current node |
| 7433 | * @level: the imbrication level for indenting |
| 7434 | * @format: is formatting allowed |
| 7435 | * @encoding: an optional encoding string |
| 7436 | * |
| 7437 | * Dump an XML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7438 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7439 | * or xmlKeepBlanksDefault(0) was called |
| 7440 | */ |
| 7441 | void |
| 7442 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7443 | int level, int format, const char *encoding) |
| 7444 | { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7445 | #ifdef LIBXML_HTML_ENABLED |
| 7446 | xmlDtdPtr dtd; |
| 7447 | int is_xhtml = 0; |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7448 | #endif |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7449 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7450 | xmlInitParser(); |
| 7451 | |
| 7452 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7453 | dtd = xmlGetIntSubset(doc); |
| 7454 | if (dtd != NULL) { |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7455 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 7456 | if (is_xhtml < 0) |
| 7457 | is_xhtml = 0; |
| 7458 | if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) && |
| 7459 | (cur->type == XML_ELEMENT_NODE) && |
| 7460 | (xmlStrEqual(cur->name, BAD_CAST "html"))) { |
| 7461 | if (encoding != NULL) |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 7462 | htmlSetMetaEncoding((htmlDocPtr) doc, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7463 | (const xmlChar *) encoding); |
| 7464 | else |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 7465 | htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8"); |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7466 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7467 | } |
| 7468 | |
| 7469 | if (is_xhtml) |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7470 | xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7471 | else |
| 7472 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7473 | xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7474 | } |
| 7475 | |
| 7476 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7477 | * xmlDocContentDumpOutput: |
| 7478 | * @buf: the XML buffer output |
| 7479 | * @cur: the document |
| 7480 | * @encoding: an optional encoding string |
| 7481 | * @format: should formatting spaces been added |
| 7482 | * |
| 7483 | * Dump an XML document. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7484 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7485 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7486 | */ |
| 7487 | static void |
| 7488 | xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 7489 | const char *encoding, int format) { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7490 | #ifdef LIBXML_HTML_ENABLED |
| 7491 | xmlDtdPtr dtd; |
| 7492 | int is_xhtml = 0; |
| 7493 | #endif |
| 7494 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7495 | xmlInitParser(); |
| 7496 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7497 | xmlOutputBufferWriteString(buf, "<?xml version="); |
| 7498 | if (cur->version != NULL) |
| 7499 | xmlBufferWriteQuotedString(buf->buffer, cur->version); |
| 7500 | else |
| 7501 | xmlOutputBufferWriteString(buf, "\"1.0\""); |
| 7502 | if (encoding == NULL) { |
| 7503 | if (cur->encoding != NULL) |
| 7504 | encoding = (const char *) cur->encoding; |
| 7505 | else if (cur->charset != XML_CHAR_ENCODING_UTF8) |
| 7506 | encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset); |
| 7507 | } |
| 7508 | if (encoding != NULL) { |
| 7509 | xmlOutputBufferWriteString(buf, " encoding="); |
| 7510 | xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding); |
| 7511 | } |
| 7512 | switch (cur->standalone) { |
| 7513 | case 0: |
| 7514 | xmlOutputBufferWriteString(buf, " standalone=\"no\""); |
| 7515 | break; |
| 7516 | case 1: |
| 7517 | xmlOutputBufferWriteString(buf, " standalone=\"yes\""); |
| 7518 | break; |
| 7519 | } |
| 7520 | xmlOutputBufferWriteString(buf, "?>\n"); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7521 | |
| 7522 | #ifdef LIBXML_HTML_ENABLED |
| 7523 | dtd = xmlGetIntSubset(cur); |
| 7524 | if (dtd != NULL) { |
| 7525 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 7526 | if (is_xhtml < 0) is_xhtml = 0; |
| 7527 | } |
| 7528 | if (is_xhtml) { |
| 7529 | if (encoding != NULL) |
| 7530 | htmlSetMetaEncoding(cur, (const xmlChar *) encoding); |
| 7531 | else |
| 7532 | htmlSetMetaEncoding(cur, BAD_CAST "UTF-8"); |
| 7533 | } |
| 7534 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7535 | if (cur->children != NULL) { |
| 7536 | xmlNodePtr child = cur->children; |
| 7537 | |
| 7538 | while (child != NULL) { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7539 | #ifdef LIBXML_HTML_ENABLED |
| 7540 | if (is_xhtml) |
| 7541 | xhtmlNodeDumpOutput(buf, cur, child, 0, format, encoding); |
| 7542 | else |
| 7543 | #endif |
| 7544 | xmlNodeDumpOutputInternal(buf, cur, child, 0, format, encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7545 | xmlOutputBufferWriteString(buf, "\n"); |
| 7546 | child = child->next; |
| 7547 | } |
| 7548 | } |
| 7549 | } |
| 7550 | |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7551 | #ifdef LIBXML_HTML_ENABLED |
| 7552 | /************************************************************************ |
| 7553 | * * |
| 7554 | * Functions specific to XHTML serialization * |
| 7555 | * * |
| 7556 | ************************************************************************/ |
| 7557 | |
| 7558 | #define XHTML_STRICT_PUBLIC_ID BAD_CAST \ |
| 7559 | "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 7560 | #define XHTML_STRICT_SYSTEM_ID BAD_CAST \ |
| 7561 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" |
| 7562 | #define XHTML_FRAME_PUBLIC_ID BAD_CAST \ |
| 7563 | "-//W3C//DTD XHTML 1.0 Frameset//EN" |
| 7564 | #define XHTML_FRAME_SYSTEM_ID BAD_CAST \ |
| 7565 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd" |
| 7566 | #define XHTML_TRANS_PUBLIC_ID BAD_CAST \ |
| 7567 | "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 7568 | #define XHTML_TRANS_SYSTEM_ID BAD_CAST \ |
| 7569 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" |
| 7570 | |
| 7571 | #define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml" |
| 7572 | /** |
| 7573 | * xmlIsXHTML: |
| 7574 | * @systemID: the system identifier |
| 7575 | * @publicID: the public identifier |
| 7576 | * |
| 7577 | * Try to find if the document correspond to an XHTML DTD |
| 7578 | * |
| 7579 | * Returns 1 if true, 0 if not and -1 in case of error |
| 7580 | */ |
| 7581 | int |
| 7582 | xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) { |
| 7583 | if ((systemID == NULL) && (publicID == NULL)) |
| 7584 | return(-1); |
| 7585 | if (publicID != NULL) { |
| 7586 | if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1); |
| 7587 | if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1); |
| 7588 | if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1); |
| 7589 | } |
| 7590 | if (systemID != NULL) { |
| 7591 | if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1); |
| 7592 | if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1); |
| 7593 | if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1); |
| 7594 | } |
| 7595 | return(0); |
| 7596 | } |
| 7597 | |
| 7598 | /** |
| 7599 | * xhtmlIsEmpty: |
| 7600 | * @node: the node |
| 7601 | * |
| 7602 | * Check if a node is an empty xhtml node |
| 7603 | * |
| 7604 | * Returns 1 if the node is an empty node, 0 if not and -1 in case of error |
| 7605 | */ |
| 7606 | static int |
| 7607 | xhtmlIsEmpty(xmlNodePtr node) { |
| 7608 | if (node == NULL) |
| 7609 | return(-1); |
| 7610 | if (node->type != XML_ELEMENT_NODE) |
| 7611 | return(0); |
| 7612 | if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME))) |
| 7613 | return(0); |
| 7614 | if (node->children != NULL) |
| 7615 | return(0); |
| 7616 | switch (node->name[0]) { |
| 7617 | case 'a': |
| 7618 | if (xmlStrEqual(node->name, BAD_CAST "area")) |
| 7619 | return(1); |
| 7620 | return(0); |
| 7621 | case 'b': |
| 7622 | if (xmlStrEqual(node->name, BAD_CAST "br")) |
| 7623 | return(1); |
| 7624 | if (xmlStrEqual(node->name, BAD_CAST "base")) |
| 7625 | return(1); |
| 7626 | if (xmlStrEqual(node->name, BAD_CAST "basefont")) |
| 7627 | return(1); |
| 7628 | return(0); |
| 7629 | case 'c': |
| 7630 | if (xmlStrEqual(node->name, BAD_CAST "col")) |
| 7631 | return(1); |
| 7632 | return(0); |
| 7633 | case 'f': |
| 7634 | if (xmlStrEqual(node->name, BAD_CAST "frame")) |
| 7635 | return(1); |
| 7636 | return(0); |
| 7637 | case 'h': |
| 7638 | if (xmlStrEqual(node->name, BAD_CAST "hr")) |
| 7639 | return(1); |
| 7640 | return(0); |
| 7641 | case 'i': |
| 7642 | if (xmlStrEqual(node->name, BAD_CAST "img")) |
| 7643 | return(1); |
| 7644 | if (xmlStrEqual(node->name, BAD_CAST "input")) |
| 7645 | return(1); |
| 7646 | if (xmlStrEqual(node->name, BAD_CAST "isindex")) |
| 7647 | return(1); |
| 7648 | return(0); |
| 7649 | case 'l': |
| 7650 | if (xmlStrEqual(node->name, BAD_CAST "link")) |
| 7651 | return(1); |
| 7652 | return(0); |
| 7653 | case 'm': |
| 7654 | if (xmlStrEqual(node->name, BAD_CAST "meta")) |
| 7655 | return(1); |
| 7656 | return(0); |
| 7657 | case 'p': |
| 7658 | if (xmlStrEqual(node->name, BAD_CAST "param")) |
| 7659 | return(1); |
| 7660 | return(0); |
| 7661 | } |
| 7662 | return(0); |
| 7663 | } |
| 7664 | |
| 7665 | /** |
| 7666 | * xhtmlAttrListDumpOutput: |
| 7667 | * @buf: the XML buffer output |
| 7668 | * @doc: the document |
| 7669 | * @cur: the first attribute pointer |
| 7670 | * @encoding: an optional encoding string |
| 7671 | * |
| 7672 | * Dump a list of XML attributes |
| 7673 | */ |
| 7674 | static void |
| 7675 | xhtmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7676 | xmlAttrPtr cur, const char *encoding) { |
| 7677 | xmlAttrPtr xml_lang = NULL; |
| 7678 | xmlAttrPtr lang = NULL; |
| 7679 | xmlAttrPtr name = NULL; |
| 7680 | xmlAttrPtr id = NULL; |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7681 | xmlNodePtr parent; |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7682 | |
| 7683 | if (cur == NULL) { |
| 7684 | #ifdef DEBUG_TREE |
| 7685 | xmlGenericError(xmlGenericErrorContext, |
| 7686 | "xmlAttrListDumpOutput : property == NULL\n"); |
| 7687 | #endif |
| 7688 | return; |
| 7689 | } |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7690 | parent = cur->parent; |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7691 | while (cur != NULL) { |
| 7692 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id"))) |
| 7693 | id = cur; |
| 7694 | else |
| 7695 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name"))) |
| 7696 | name = cur; |
| 7697 | else |
| 7698 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang"))) |
| 7699 | lang = cur; |
| 7700 | else |
| 7701 | if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) && |
| 7702 | (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml"))) |
| 7703 | xml_lang = cur; |
| 7704 | else if ((cur->ns == NULL) && |
| 7705 | ((cur->children == NULL) || |
| 7706 | (cur->children->content == NULL) || |
| 7707 | (cur->children->content[0] == 0)) && |
| 7708 | (htmlIsBooleanAttr(cur->name))) { |
| 7709 | if (cur->children != NULL) |
| 7710 | xmlFreeNode(cur->children); |
| 7711 | cur->children = xmlNewText(cur->name); |
| 7712 | if (cur->children != NULL) |
| 7713 | cur->children->parent = (xmlNodePtr) cur; |
| 7714 | } |
| 7715 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 7716 | cur = cur->next; |
| 7717 | } |
| 7718 | /* |
| 7719 | * C.8 |
| 7720 | */ |
| 7721 | if ((name != NULL) && (id == NULL)) { |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7722 | if ((parent != NULL) && (parent->name != NULL) && |
| 7723 | ((xmlStrEqual(parent->name, BAD_CAST "a")) || |
| 7724 | (xmlStrEqual(parent->name, BAD_CAST "p")) || |
| 7725 | (xmlStrEqual(parent->name, BAD_CAST "div")) || |
| 7726 | (xmlStrEqual(parent->name, BAD_CAST "img")) || |
| 7727 | (xmlStrEqual(parent->name, BAD_CAST "map")) || |
| 7728 | (xmlStrEqual(parent->name, BAD_CAST "applet")) || |
| 7729 | (xmlStrEqual(parent->name, BAD_CAST "form")) || |
| 7730 | (xmlStrEqual(parent->name, BAD_CAST "frame")) || |
| 7731 | (xmlStrEqual(parent->name, BAD_CAST "iframe")))) { |
| 7732 | xmlOutputBufferWriteString(buf, " id=\""); |
| 7733 | xmlAttrSerializeContent(buf->buffer, doc, name); |
| 7734 | xmlOutputBufferWriteString(buf, "\""); |
| 7735 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7736 | } |
| 7737 | /* |
| 7738 | * C.7. |
| 7739 | */ |
| 7740 | if ((lang != NULL) && (xml_lang == NULL)) { |
| 7741 | xmlOutputBufferWriteString(buf, " xml:lang=\""); |
| 7742 | xmlAttrSerializeContent(buf->buffer, doc, lang); |
| 7743 | xmlOutputBufferWriteString(buf, "\""); |
| 7744 | } else |
| 7745 | if ((xml_lang != NULL) && (lang == NULL)) { |
| 7746 | xmlOutputBufferWriteString(buf, " lang=\""); |
| 7747 | xmlAttrSerializeContent(buf->buffer, doc, xml_lang); |
| 7748 | xmlOutputBufferWriteString(buf, "\""); |
| 7749 | } |
| 7750 | } |
| 7751 | |
| 7752 | /** |
| 7753 | * xhtmlNodeListDumpOutput: |
| 7754 | * @buf: the XML buffer output |
| 7755 | * @doc: the XHTML document |
| 7756 | * @cur: the first node |
| 7757 | * @level: the imbrication level for indenting |
| 7758 | * @format: is formatting allowed |
| 7759 | * @encoding: an optional encoding string |
| 7760 | * |
| 7761 | * Dump an XML node list, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7762 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7763 | * or xmlKeepBlanksDefault(0) was called |
| 7764 | */ |
| 7765 | static void |
| 7766 | xhtmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7767 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 7768 | int i; |
| 7769 | |
| 7770 | if (cur == NULL) { |
| 7771 | #ifdef DEBUG_TREE |
| 7772 | xmlGenericError(xmlGenericErrorContext, |
| 7773 | "xhtmlNodeListDumpOutput : node == NULL\n"); |
| 7774 | #endif |
| 7775 | return; |
| 7776 | } |
| 7777 | while (cur != NULL) { |
| 7778 | if ((format) && (xmlIndentTreeOutput) && |
| 7779 | (cur->type == XML_ELEMENT_NODE)) |
| 7780 | for (i = 0;i < level;i++) |
| 7781 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
| 7782 | xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
| 7783 | if (format) { |
| 7784 | xmlOutputBufferWriteString(buf, "\n"); |
| 7785 | } |
| 7786 | cur = cur->next; |
| 7787 | } |
| 7788 | } |
| 7789 | |
| 7790 | /** |
| 7791 | * xhtmlNodeDumpOutput: |
| 7792 | * @buf: the XML buffer output |
| 7793 | * @doc: the XHTML document |
| 7794 | * @cur: the current node |
| 7795 | * @level: the imbrication level for indenting |
| 7796 | * @format: is formatting allowed |
| 7797 | * @encoding: an optional encoding string |
| 7798 | * |
| 7799 | * Dump an XHTML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7800 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7801 | * or xmlKeepBlanksDefault(0) was called |
| 7802 | */ |
| 7803 | static void |
| 7804 | xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 7805 | int level, int format, const char *encoding) { |
| 7806 | int i; |
| 7807 | xmlNodePtr tmp; |
| 7808 | |
| 7809 | if (cur == NULL) { |
| 7810 | #ifdef DEBUG_TREE |
| 7811 | xmlGenericError(xmlGenericErrorContext, |
| 7812 | "xmlNodeDumpOutput : node == NULL\n"); |
| 7813 | #endif |
| 7814 | return; |
| 7815 | } |
| 7816 | if (cur->type == XML_XINCLUDE_START) |
| 7817 | return; |
| 7818 | if (cur->type == XML_XINCLUDE_END) |
| 7819 | return; |
| 7820 | if (cur->type == XML_DTD_NODE) { |
| 7821 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 7822 | return; |
| 7823 | } |
| 7824 | if (cur->type == XML_ELEMENT_DECL) { |
| 7825 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 7826 | return; |
| 7827 | } |
| 7828 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 7829 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 7830 | return; |
| 7831 | } |
| 7832 | if (cur->type == XML_ENTITY_DECL) { |
| 7833 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 7834 | return; |
| 7835 | } |
| 7836 | if (cur->type == XML_TEXT_NODE) { |
| 7837 | if (cur->content != NULL) { |
| 7838 | if ((cur->name == xmlStringText) || |
| 7839 | (cur->name != xmlStringTextNoenc)) { |
| 7840 | xmlChar *buffer; |
| 7841 | |
| 7842 | if (encoding == NULL) |
| 7843 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7844 | else |
| 7845 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 7846 | if (buffer != NULL) { |
| 7847 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7848 | xmlFree(buffer); |
| 7849 | } |
| 7850 | } else { |
| 7851 | /* |
| 7852 | * Disable escaping, needed for XSLT |
| 7853 | */ |
| 7854 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
| 7855 | } |
| 7856 | } |
| 7857 | |
| 7858 | return; |
| 7859 | } |
| 7860 | if (cur->type == XML_PI_NODE) { |
| 7861 | if (cur->content != NULL) { |
| 7862 | xmlOutputBufferWriteString(buf, "<?"); |
| 7863 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7864 | if (cur->content != NULL) { |
| 7865 | xmlOutputBufferWriteString(buf, " "); |
| 7866 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 7867 | } |
| 7868 | xmlOutputBufferWriteString(buf, "?>"); |
| 7869 | } else { |
| 7870 | xmlOutputBufferWriteString(buf, "<?"); |
| 7871 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7872 | xmlOutputBufferWriteString(buf, "?>"); |
| 7873 | } |
| 7874 | return; |
| 7875 | } |
| 7876 | if (cur->type == XML_COMMENT_NODE) { |
| 7877 | if (cur->content != NULL) { |
| 7878 | xmlOutputBufferWriteString(buf, "<!--"); |
| 7879 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 7880 | xmlOutputBufferWriteString(buf, "-->"); |
| 7881 | } |
| 7882 | return; |
| 7883 | } |
| 7884 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 7885 | xmlOutputBufferWriteString(buf, "&"); |
| 7886 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7887 | xmlOutputBufferWriteString(buf, ";"); |
| 7888 | return; |
| 7889 | } |
| 7890 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 7891 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 7892 | if (cur->content != NULL) |
| 7893 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 7894 | xmlOutputBufferWriteString(buf, "]]>"); |
| 7895 | return; |
| 7896 | } |
| 7897 | |
| 7898 | if (format == 1) { |
| 7899 | tmp = cur->children; |
| 7900 | while (tmp != NULL) { |
| 7901 | if ((tmp->type == XML_TEXT_NODE) || |
| 7902 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 7903 | format = 0; |
| 7904 | break; |
| 7905 | } |
| 7906 | tmp = tmp->next; |
| 7907 | } |
| 7908 | } |
| 7909 | xmlOutputBufferWriteString(buf, "<"); |
| 7910 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7911 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7912 | xmlOutputBufferWriteString(buf, ":"); |
| 7913 | } |
| 7914 | |
| 7915 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7916 | if (cur->nsDef) |
| 7917 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 7918 | if ((xmlStrEqual(cur->name, BAD_CAST "html") && |
| 7919 | (cur->ns == NULL) && (cur->nsDef == NULL))) { |
| 7920 | /* |
| 7921 | * 3.1.1. Strictly Conforming Documents A.3.1.1 3/ |
| 7922 | */ |
| 7923 | xmlOutputBufferWriteString(buf, |
| 7924 | " xmlns=\"http://www.w3.org/1999/xhtml\""); |
| 7925 | } |
| 7926 | if (cur->properties != NULL) |
| 7927 | xhtmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 7928 | |
| 7929 | if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) { |
| 7930 | if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) && |
| 7931 | (xhtmlIsEmpty(cur) == 1)) { |
| 7932 | /* |
| 7933 | * C.2. Empty Elements |
| 7934 | */ |
| 7935 | xmlOutputBufferWriteString(buf, " />"); |
| 7936 | } else { |
| 7937 | /* |
| 7938 | * C.3. Element Minimization and Empty Element Content |
| 7939 | */ |
| 7940 | xmlOutputBufferWriteString(buf, "></"); |
| 7941 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7942 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7943 | xmlOutputBufferWriteString(buf, ":"); |
| 7944 | } |
| 7945 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7946 | xmlOutputBufferWriteString(buf, ">"); |
| 7947 | } |
| 7948 | return; |
| 7949 | } |
| 7950 | xmlOutputBufferWriteString(buf, ">"); |
| 7951 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
| 7952 | xmlChar *buffer; |
| 7953 | |
| 7954 | if (encoding == NULL) |
| 7955 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7956 | else |
| 7957 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 7958 | if (buffer != NULL) { |
| 7959 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7960 | xmlFree(buffer); |
| 7961 | } |
| 7962 | } |
| 7963 | |
| 7964 | /* |
| 7965 | * 4.8. Script and Style elements |
| 7966 | */ |
| 7967 | if ((cur->type == XML_ELEMENT_NODE) && |
| 7968 | ((xmlStrEqual(cur->name, BAD_CAST "script")) || |
| 7969 | (xmlStrEqual(cur->name, BAD_CAST "style"))) && |
| 7970 | ((cur->ns == NULL) || |
| 7971 | (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) { |
| 7972 | xmlNodePtr child = cur->children; |
| 7973 | |
| 7974 | while (child != NULL) { |
| 7975 | if ((child->type == XML_TEXT_NODE) || |
| 7976 | (child->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 64b3528 | 2002-12-04 15:10:40 +0000 | [diff] [blame] | 7977 | /* |
| 7978 | * Apparently CDATA escaping for style just break on IE, |
| 7979 | * mozilla and galeon, so ... |
| 7980 | */ |
| 7981 | if (xmlStrEqual(cur->name, BAD_CAST "style") && |
| 7982 | (xmlStrchr(child->content, '<') == NULL) && |
| 7983 | (xmlStrchr(child->content, '>') == NULL) && |
| 7984 | (xmlStrchr(child->content, '&') == NULL)) { |
| 7985 | xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding); |
| 7986 | } else { |
| 7987 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 7988 | if (child->content != NULL) |
| 7989 | xmlOutputBufferWriteString(buf, |
| 7990 | (const char *)child->content); |
| 7991 | xmlOutputBufferWriteString(buf, "]]>"); |
| 7992 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7993 | } else { |
| 7994 | xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding); |
| 7995 | } |
| 7996 | child = child->next; |
| 7997 | } |
| 7998 | } else if (cur->children != NULL) { |
| 7999 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 8000 | xhtmlNodeListDumpOutput(buf, doc, cur->children, |
| 8001 | (level >= 0?level+1:-1), format, encoding); |
| 8002 | if ((xmlIndentTreeOutput) && (format)) |
| 8003 | for (i = 0;i < level;i++) |
| 8004 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
| 8005 | } |
| 8006 | xmlOutputBufferWriteString(buf, "</"); |
| 8007 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 8008 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 8009 | xmlOutputBufferWriteString(buf, ":"); |
| 8010 | } |
| 8011 | |
| 8012 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 8013 | xmlOutputBufferWriteString(buf, ">"); |
| 8014 | } |
| 8015 | #endif |
| 8016 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8017 | /************************************************************************ |
| 8018 | * * |
| 8019 | * Saving functions front-ends * |
| 8020 | * * |
| 8021 | ************************************************************************/ |
| 8022 | |
| 8023 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 8024 | * xmlDocDumpFormatMemoryEnc: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8025 | * @out_doc: Document to generate XML text from |
| 8026 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 8027 | * @doc_txt_len: Length of the generated XML text |
| 8028 | * @txt_encoding: Character encoding to use when generating XML text |
| 8029 | * @format: should formatting spaces been added |
| 8030 | * |
| 8031 | * Dump the current DOM tree into memory using the character encoding specified |
| 8032 | * by the caller. Note it is up to the caller of this function to free the |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 8033 | * allocated memory with xmlFree(). |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8034 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 8035 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8036 | */ |
| 8037 | |
| 8038 | void |
| 8039 | xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8040 | int * doc_txt_len, const char * txt_encoding, |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8041 | int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8042 | int dummy = 0; |
| 8043 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8044 | xmlOutputBufferPtr out_buff = NULL; |
| 8045 | xmlCharEncodingHandlerPtr conv_hdlr = NULL; |
| 8046 | |
| 8047 | if (doc_txt_len == NULL) { |
| 8048 | doc_txt_len = &dummy; /* Continue, caller just won't get length */ |
| 8049 | } |
| 8050 | |
| 8051 | if (doc_txt_ptr == NULL) { |
| 8052 | *doc_txt_len = 0; |
| 8053 | xmlGenericError(xmlGenericErrorContext, |
| 8054 | "xmlDocDumpFormatMemoryEnc: Null return buffer pointer."); |
| 8055 | return; |
| 8056 | } |
| 8057 | |
| 8058 | *doc_txt_ptr = NULL; |
| 8059 | *doc_txt_len = 0; |
| 8060 | |
| 8061 | if (out_doc == NULL) { |
| 8062 | /* No document, no output */ |
| 8063 | xmlGenericError(xmlGenericErrorContext, |
| 8064 | "xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.\n"); |
| 8065 | return; |
| 8066 | } |
| 8067 | |
| 8068 | /* |
| 8069 | * Validate the encoding value, if provided. |
| 8070 | * This logic is copied from xmlSaveFileEnc. |
| 8071 | */ |
| 8072 | |
| 8073 | if (txt_encoding == NULL) |
| 8074 | txt_encoding = (const char *) out_doc->encoding; |
| 8075 | if (txt_encoding != NULL) { |
Daniel Veillard | e132611 | 2003-06-05 09:32:20 +0000 | [diff] [blame] | 8076 | conv_hdlr = xmlFindCharEncodingHandler(txt_encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8077 | if ( conv_hdlr == NULL ) { |
| 8078 | xmlGenericError(xmlGenericErrorContext, |
| 8079 | "%s: %s %s '%s'\n", |
| 8080 | "xmlDocDumpFormatMemoryEnc", |
| 8081 | "Failed to identify encoding handler for", |
| 8082 | "character set", |
| 8083 | txt_encoding); |
| 8084 | return; |
| 8085 | } |
| 8086 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8087 | |
| 8088 | if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) { |
| 8089 | xmlGenericError(xmlGenericErrorContext, |
| 8090 | "xmlDocDumpFormatMemoryEnc: Failed to allocate output buffer.\n"); |
| 8091 | return; |
| 8092 | } |
| 8093 | |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8094 | xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8095 | xmlOutputBufferFlush(out_buff); |
| 8096 | if (out_buff->conv != NULL) { |
| 8097 | *doc_txt_len = out_buff->conv->use; |
| 8098 | *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len); |
| 8099 | } else { |
| 8100 | *doc_txt_len = out_buff->buffer->use; |
| 8101 | *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len); |
| 8102 | } |
| 8103 | (void)xmlOutputBufferClose(out_buff); |
| 8104 | |
| 8105 | if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) { |
| 8106 | *doc_txt_len = 0; |
| 8107 | xmlGenericError(xmlGenericErrorContext, |
| 8108 | "xmlDocDumpFormatMemoryEnc: %s\n", |
| 8109 | "Failed to allocate memory for document text representation."); |
| 8110 | } |
| 8111 | |
| 8112 | return; |
| 8113 | } |
| 8114 | |
| 8115 | /** |
| 8116 | * xmlDocDumpMemory: |
| 8117 | * @cur: the document |
| 8118 | * @mem: OUT: the memory pointer |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 8119 | * @size: OUT: the memory length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8120 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8121 | * Dump an XML document in memory and return the #xmlChar * and it's size. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 8122 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8123 | */ |
| 8124 | void |
| 8125 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
| 8126 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0); |
| 8127 | } |
| 8128 | |
| 8129 | /** |
| 8130 | * xmlDocDumpFormatMemory: |
| 8131 | * @cur: the document |
| 8132 | * @mem: OUT: the memory pointer |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 8133 | * @size: OUT: the memory length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8134 | * @format: should formatting spaces been added |
| 8135 | * |
| 8136 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8137 | * Dump an XML document in memory and return the #xmlChar * and it's size. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 8138 | * It's up to the caller to free the memory with xmlFree(). |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8139 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 8140 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8141 | */ |
| 8142 | void |
| 8143 | xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) { |
| 8144 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format); |
| 8145 | } |
| 8146 | |
| 8147 | /** |
| 8148 | * xmlDocDumpMemoryEnc: |
| 8149 | * @out_doc: Document to generate XML text from |
| 8150 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 8151 | * @doc_txt_len: Length of the generated XML text |
| 8152 | * @txt_encoding: Character encoding to use when generating XML text |
| 8153 | * |
| 8154 | * Dump the current DOM tree into memory using the character encoding specified |
| 8155 | * by the caller. Note it is up to the caller of this function to free the |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 8156 | * allocated memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8157 | */ |
| 8158 | |
| 8159 | void |
| 8160 | xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 8161 | int * doc_txt_len, const char * txt_encoding) { |
| 8162 | xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len, |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8163 | txt_encoding, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8164 | } |
| 8165 | |
| 8166 | /** |
| 8167 | * xmlGetDocCompressMode: |
| 8168 | * @doc: the document |
| 8169 | * |
| 8170 | * get the compression ratio for a document, ZLIB based |
| 8171 | * Returns 0 (uncompressed) to 9 (max compression) |
| 8172 | */ |
| 8173 | int |
| 8174 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 8175 | if (doc == NULL) return(-1); |
| 8176 | return(doc->compression); |
| 8177 | } |
| 8178 | |
| 8179 | /** |
| 8180 | * xmlSetDocCompressMode: |
| 8181 | * @doc: the document |
| 8182 | * @mode: the compression ratio |
| 8183 | * |
| 8184 | * set the compression ratio for a document, ZLIB based |
| 8185 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 8186 | */ |
| 8187 | void |
| 8188 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 8189 | if (doc == NULL) return; |
| 8190 | if (mode < 0) doc->compression = 0; |
| 8191 | else if (mode > 9) doc->compression = 9; |
| 8192 | else doc->compression = mode; |
| 8193 | } |
| 8194 | |
| 8195 | /** |
| 8196 | * xmlGetCompressMode: |
| 8197 | * |
| 8198 | * get the default compression mode used, ZLIB based. |
| 8199 | * Returns 0 (uncompressed) to 9 (max compression) |
| 8200 | */ |
| 8201 | int |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8202 | xmlGetCompressMode(void) |
| 8203 | { |
| 8204 | return (xmlCompressMode); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8205 | } |
| 8206 | |
| 8207 | /** |
| 8208 | * xmlSetCompressMode: |
| 8209 | * @mode: the compression ratio |
| 8210 | * |
| 8211 | * set the default compression mode used, ZLIB based |
| 8212 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 8213 | */ |
| 8214 | void |
| 8215 | xmlSetCompressMode(int mode) { |
| 8216 | if (mode < 0) xmlCompressMode = 0; |
| 8217 | else if (mode > 9) xmlCompressMode = 9; |
| 8218 | else xmlCompressMode = mode; |
| 8219 | } |
| 8220 | |
| 8221 | /** |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8222 | * xmlDocFormatDump: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8223 | * @f: the FILE* |
| 8224 | * @cur: the document |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8225 | * @format: should formatting spaces been added |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8226 | * |
| 8227 | * Dump an XML document to an open FILE. |
| 8228 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8229 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8230 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8231 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8232 | */ |
| 8233 | int |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8234 | xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8235 | xmlOutputBufferPtr buf; |
| 8236 | const char * encoding; |
| 8237 | xmlCharEncodingHandlerPtr handler = NULL; |
| 8238 | int ret; |
| 8239 | |
| 8240 | if (cur == NULL) { |
| 8241 | #ifdef DEBUG_TREE |
| 8242 | xmlGenericError(xmlGenericErrorContext, |
| 8243 | "xmlDocDump : document == NULL\n"); |
| 8244 | #endif |
| 8245 | return(-1); |
| 8246 | } |
| 8247 | encoding = (const char *) cur->encoding; |
| 8248 | |
| 8249 | if (encoding != NULL) { |
Daniel Veillard | e132611 | 2003-06-05 09:32:20 +0000 | [diff] [blame] | 8250 | handler = xmlFindCharEncodingHandler(encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8251 | if (handler == NULL) { |
| 8252 | xmlFree((char *) cur->encoding); |
| 8253 | cur->encoding = NULL; |
| 8254 | } |
| 8255 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8256 | buf = xmlOutputBufferCreateFile(f, handler); |
| 8257 | if (buf == NULL) return(-1); |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8258 | xmlDocContentDumpOutput(buf, cur, NULL, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8259 | |
| 8260 | ret = xmlOutputBufferClose(buf); |
| 8261 | return(ret); |
| 8262 | } |
| 8263 | |
| 8264 | /** |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8265 | * xmlDocDump: |
| 8266 | * @f: the FILE* |
| 8267 | * @cur: the document |
| 8268 | * |
| 8269 | * Dump an XML document to an open FILE. |
| 8270 | * |
| 8271 | * returns: the number of bytes written or -1 in case of failure. |
| 8272 | */ |
| 8273 | int |
| 8274 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
| 8275 | return(xmlDocFormatDump (f, cur, 0)); |
| 8276 | } |
| 8277 | |
| 8278 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8279 | * xmlSaveFileTo: |
| 8280 | * @buf: an output I/O buffer |
| 8281 | * @cur: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8282 | * @encoding: the encoding if any assuming the I/O layer handles the trancoding |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8283 | * |
| 8284 | * Dump an XML document to an I/O buffer. |
| 8285 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8286 | * returns: the number of bytes written or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8287 | */ |
| 8288 | int |
CET 2001 Daniel Veillard | 5a37bde | 2001-11-01 14:31:22 +0000 | [diff] [blame] | 8289 | xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8290 | int ret; |
| 8291 | |
| 8292 | if (buf == NULL) return(0); |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8293 | xmlDocContentDumpOutput(buf, cur, encoding, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8294 | ret = xmlOutputBufferClose(buf); |
| 8295 | return(ret); |
| 8296 | } |
| 8297 | |
| 8298 | /** |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8299 | * xmlSaveFormatFileTo: |
| 8300 | * @buf: an output I/O buffer |
| 8301 | * @cur: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8302 | * @encoding: the encoding if any assuming the I/O layer handles the trancoding |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8303 | * @format: should formatting spaces been added |
| 8304 | * |
| 8305 | * Dump an XML document to an I/O buffer. |
| 8306 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8307 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8308 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8309 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8310 | */ |
| 8311 | int |
CET 2001 Daniel Veillard | 5a37bde | 2001-11-01 14:31:22 +0000 | [diff] [blame] | 8312 | xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) { |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8313 | int ret; |
| 8314 | |
| 8315 | if (buf == NULL) return(0); |
| 8316 | xmlDocContentDumpOutput(buf, cur, encoding, format); |
| 8317 | ret = xmlOutputBufferClose(buf); |
| 8318 | return(ret); |
| 8319 | } |
| 8320 | |
| 8321 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 8322 | * xmlSaveFormatFileEnc: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8323 | * @filename: the filename or URL to output |
| 8324 | * @cur: the document being saved |
| 8325 | * @encoding: the name of the encoding to use or NULL. |
| 8326 | * @format: should formatting spaces be added. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8327 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 8328 | * Dump an XML document to a file or an URL. |
| 8329 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8330 | * Returns the number of bytes written or -1 in case of error. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8331 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8332 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8333 | */ |
| 8334 | int |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8335 | xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur, |
| 8336 | const char * encoding, int format ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8337 | xmlOutputBufferPtr buf; |
| 8338 | xmlCharEncodingHandlerPtr handler = NULL; |
| 8339 | int ret; |
| 8340 | |
Daniel Veillard | 4dbe77a | 2003-01-14 00:17:42 +0000 | [diff] [blame] | 8341 | if (cur == NULL) |
| 8342 | return(-1); |
| 8343 | |
Daniel Veillard | fb25a51 | 2002-01-13 20:32:08 +0000 | [diff] [blame] | 8344 | if (encoding == NULL) |
| 8345 | encoding = (const char *) cur->encoding; |
| 8346 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8347 | if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8348 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8349 | handler = xmlFindCharEncodingHandler(encoding); |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 8350 | if (handler == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8351 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8352 | } |
| 8353 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8354 | #ifdef HAVE_ZLIB_H |
| 8355 | if (cur->compression < 0) cur->compression = xmlCompressMode; |
| 8356 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8357 | /* |
| 8358 | * save the content to a temp buffer. |
| 8359 | */ |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8360 | buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8361 | if (buf == NULL) return(-1); |
| 8362 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8363 | xmlDocContentDumpOutput(buf, cur, encoding, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8364 | |
| 8365 | ret = xmlOutputBufferClose(buf); |
| 8366 | return(ret); |
| 8367 | } |
| 8368 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8369 | |
| 8370 | /** |
| 8371 | * xmlSaveFileEnc: |
| 8372 | * @filename: the filename (or URL) |
| 8373 | * @cur: the document |
| 8374 | * @encoding: the name of an encoding (or NULL) |
| 8375 | * |
| 8376 | * Dump an XML document, converting it to the given encoding |
| 8377 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8378 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8379 | */ |
| 8380 | int |
| 8381 | xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 8382 | return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) ); |
| 8383 | } |
| 8384 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8385 | /** |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8386 | * xmlSaveFormatFile: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8387 | * @filename: the filename (or URL) |
| 8388 | * @cur: the document |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8389 | * @format: should formatting spaces been added |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8390 | * |
| 8391 | * Dump an XML document to a file. Will use compression if |
| 8392 | * compiled in and enabled. If @filename is "-" the stdout file is |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8393 | * used. If @format is set then the document will be indented on output. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8394 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8395 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8396 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8397 | * returns: the number of bytes written or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8398 | */ |
| 8399 | int |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8400 | xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8401 | return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8402 | } |
| 8403 | |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8404 | /** |
| 8405 | * xmlSaveFile: |
| 8406 | * @filename: the filename (or URL) |
| 8407 | * @cur: the document |
| 8408 | * |
| 8409 | * Dump an XML document to a file. Will use compression if |
| 8410 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 8411 | * used. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8412 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8413 | */ |
| 8414 | int |
| 8415 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8416 | return(xmlSaveFormatFileEnc(filename, cur, NULL, 0)); |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8417 | } |
| 8418 | |