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 | |
| 154 | if ((ncname == NULL) || (*ncname == 0)) return(NULL); |
| 155 | if ((prefix == NULL) || (*prefix == 0)) return((xmlChar *) ncname); |
| 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; |
| 1081 | const xmlChar *cur = value; |
| 1082 | const xmlChar *q; |
| 1083 | xmlEntityPtr ent; |
| 1084 | |
| 1085 | if (value == NULL) return(NULL); |
| 1086 | |
| 1087 | q = cur; |
| 1088 | while ((*cur != 0) && (cur - value < len)) { |
| 1089 | if (*cur == '&') { |
| 1090 | /* |
| 1091 | * Save the current text. |
| 1092 | */ |
| 1093 | if (cur != q) { |
| 1094 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1095 | xmlNodeAddContentLen(last, q, cur - q); |
| 1096 | } else { |
| 1097 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1098 | if (node == NULL) return(ret); |
| 1099 | if (last == NULL) |
| 1100 | last = ret = node; |
| 1101 | else { |
| 1102 | last->next = node; |
| 1103 | node->prev = last; |
| 1104 | last = node; |
| 1105 | } |
| 1106 | } |
| 1107 | } |
| 1108 | /* |
| 1109 | * Read the entity string |
| 1110 | */ |
| 1111 | cur++; |
| 1112 | q = cur; |
| 1113 | while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++; |
| 1114 | if ((*cur == 0) || (cur - value >= len)) { |
| 1115 | #ifdef DEBUG_TREE |
| 1116 | xmlGenericError(xmlGenericErrorContext, |
| 1117 | "xmlStringLenGetNodeList: unterminated entity %30s\n", q); |
| 1118 | #endif |
| 1119 | return(ret); |
| 1120 | } |
| 1121 | if (cur != q) { |
| 1122 | /* |
| 1123 | * Predefined entities don't generate nodes |
| 1124 | */ |
| 1125 | val = xmlStrndup(q, cur - q); |
| 1126 | ent = xmlGetDocEntity(doc, val); |
| 1127 | if ((ent != NULL) && |
| 1128 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1129 | if (last == NULL) { |
| 1130 | node = xmlNewDocText(doc, ent->content); |
| 1131 | last = ret = node; |
| 1132 | } else |
| 1133 | xmlNodeAddContent(last, ent->content); |
| 1134 | |
| 1135 | } else { |
| 1136 | /* |
| 1137 | * Create a new REFERENCE_REF node |
| 1138 | */ |
| 1139 | node = xmlNewReference(doc, val); |
| 1140 | if (node == NULL) { |
| 1141 | if (val != NULL) xmlFree(val); |
| 1142 | return(ret); |
| 1143 | } |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1144 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1145 | xmlNodePtr tmp; |
| 1146 | |
| 1147 | ent->children = |
| 1148 | xmlStringGetNodeList(doc, (const xmlChar*)node->content); |
| 1149 | tmp = ent->children; |
| 1150 | while (tmp) { |
| 1151 | tmp->parent = (xmlNodePtr)ent; |
| 1152 | tmp = tmp->next; |
| 1153 | } |
| 1154 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1155 | if (last == NULL) |
| 1156 | last = ret = node; |
| 1157 | else { |
| 1158 | last->next = node; |
| 1159 | node->prev = last; |
| 1160 | last = node; |
| 1161 | } |
| 1162 | } |
| 1163 | xmlFree(val); |
| 1164 | } |
| 1165 | cur++; |
| 1166 | q = cur; |
| 1167 | } else |
| 1168 | cur++; |
| 1169 | } |
| 1170 | if (cur != q) { |
| 1171 | /* |
| 1172 | * Handle the last piece of text. |
| 1173 | */ |
| 1174 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1175 | xmlNodeAddContentLen(last, q, cur - q); |
| 1176 | } else { |
| 1177 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1178 | if (node == NULL) return(ret); |
| 1179 | if (last == NULL) |
| 1180 | last = ret = node; |
| 1181 | else { |
| 1182 | last->next = node; |
| 1183 | node->prev = last; |
| 1184 | last = node; |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | return(ret); |
| 1189 | } |
| 1190 | |
| 1191 | /** |
| 1192 | * xmlStringGetNodeList: |
| 1193 | * @doc: the document |
| 1194 | * @value: the value of the attribute |
| 1195 | * |
| 1196 | * Parse the value string and build the node list associated. Should |
| 1197 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1198 | * Returns a pointer to the first child |
| 1199 | */ |
| 1200 | xmlNodePtr |
| 1201 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
| 1202 | xmlNodePtr ret = NULL, last = NULL; |
| 1203 | xmlNodePtr node; |
| 1204 | xmlChar *val; |
| 1205 | const xmlChar *cur = value; |
| 1206 | const xmlChar *q; |
| 1207 | xmlEntityPtr ent; |
| 1208 | |
| 1209 | if (value == NULL) return(NULL); |
| 1210 | |
| 1211 | q = cur; |
| 1212 | while (*cur != 0) { |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1213 | if (cur[0] == '&') { |
| 1214 | int charval = 0; |
| 1215 | xmlChar tmp; |
| 1216 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1217 | /* |
| 1218 | * Save the current text. |
| 1219 | */ |
| 1220 | if (cur != q) { |
| 1221 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1222 | xmlNodeAddContentLen(last, q, cur - q); |
| 1223 | } else { |
| 1224 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1225 | if (node == NULL) return(ret); |
| 1226 | if (last == NULL) |
| 1227 | last = ret = node; |
| 1228 | else { |
| 1229 | last->next = node; |
| 1230 | node->prev = last; |
| 1231 | last = node; |
| 1232 | } |
| 1233 | } |
| 1234 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1235 | q = cur; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1236 | if ((cur[1] == '#') && (cur[2] == 'x')) { |
| 1237 | cur += 3; |
| 1238 | tmp = *cur; |
| 1239 | while (tmp != ';') { /* Non input consuming loop */ |
| 1240 | if ((tmp >= '0') && (tmp <= '9')) |
| 1241 | charval = charval * 16 + (tmp - '0'); |
| 1242 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1243 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1244 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1245 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1246 | else { |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1247 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1248 | "xmlStringGetNodeList: invalid hexadecimal charvalue\n"); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1249 | charval = 0; |
| 1250 | break; |
| 1251 | } |
| 1252 | cur++; |
| 1253 | tmp = *cur; |
| 1254 | } |
| 1255 | if (tmp == ';') |
| 1256 | cur++; |
| 1257 | q = cur; |
| 1258 | } else if (cur[1] == '#') { |
| 1259 | cur += 2; |
| 1260 | tmp = *cur; |
| 1261 | while (tmp != ';') { /* Non input consuming loops */ |
| 1262 | if ((tmp >= '0') && (tmp <= '9')) |
| 1263 | charval = charval * 10 + (tmp - '0'); |
| 1264 | else { |
| 1265 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1266 | "xmlStringGetNodeList: invalid decimal charvalue\n"); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1267 | charval = 0; |
| 1268 | break; |
| 1269 | } |
| 1270 | cur++; |
| 1271 | tmp = *cur; |
| 1272 | } |
| 1273 | if (tmp == ';') |
| 1274 | cur++; |
| 1275 | q = cur; |
| 1276 | } else { |
| 1277 | /* |
| 1278 | * Read the entity string |
| 1279 | */ |
| 1280 | cur++; |
| 1281 | q = cur; |
| 1282 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 1283 | if (*cur == 0) { |
| 1284 | #ifdef DEBUG_TREE |
| 1285 | xmlGenericError(xmlGenericErrorContext, |
| 1286 | "xmlStringGetNodeList: unterminated entity %30s\n", q); |
| 1287 | #endif |
| 1288 | return(ret); |
| 1289 | } |
| 1290 | if (cur != q) { |
| 1291 | /* |
| 1292 | * Predefined entities don't generate nodes |
| 1293 | */ |
| 1294 | val = xmlStrndup(q, cur - q); |
| 1295 | ent = xmlGetDocEntity(doc, val); |
| 1296 | if ((ent != NULL) && |
| 1297 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1298 | if (last == NULL) { |
| 1299 | node = xmlNewDocText(doc, ent->content); |
| 1300 | last = ret = node; |
Daniel Veillard | 6f42c13 | 2002-01-06 23:05:13 +0000 | [diff] [blame] | 1301 | } else if (last->type != XML_TEXT_NODE) { |
| 1302 | node = xmlNewDocText(doc, ent->content); |
| 1303 | last = xmlAddNextSibling(last, node); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1304 | } else |
| 1305 | xmlNodeAddContent(last, ent->content); |
| 1306 | |
| 1307 | } else { |
| 1308 | /* |
| 1309 | * Create a new REFERENCE_REF node |
| 1310 | */ |
| 1311 | node = xmlNewReference(doc, val); |
| 1312 | if (node == NULL) { |
| 1313 | if (val != NULL) xmlFree(val); |
| 1314 | return(ret); |
| 1315 | } |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1316 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1317 | xmlNodePtr temp; |
| 1318 | |
| 1319 | ent->children = xmlStringGetNodeList(doc, |
| 1320 | (const xmlChar*)node->content); |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 1321 | ent->owner = 1; |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1322 | temp = ent->children; |
| 1323 | while (temp) { |
| 1324 | temp->parent = (xmlNodePtr)ent; |
| 1325 | temp = temp->next; |
| 1326 | } |
| 1327 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1328 | if (last == NULL) { |
| 1329 | last = ret = node; |
| 1330 | } else { |
| 1331 | last = xmlAddNextSibling(last, node); |
| 1332 | } |
| 1333 | } |
| 1334 | xmlFree(val); |
| 1335 | } |
| 1336 | cur++; |
| 1337 | q = cur; |
| 1338 | } |
| 1339 | if (charval != 0) { |
| 1340 | xmlChar buf[10]; |
| 1341 | int len; |
| 1342 | |
| 1343 | len = xmlCopyCharMultiByte(buf, charval); |
| 1344 | buf[len] = 0; |
| 1345 | node = xmlNewDocText(doc, buf); |
| 1346 | if (node != NULL) { |
| 1347 | if (last == NULL) { |
| 1348 | last = ret = node; |
| 1349 | } else { |
| 1350 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1351 | } |
| 1352 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1353 | |
| 1354 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1355 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1356 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1357 | cur++; |
| 1358 | } |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 1359 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1360 | /* |
| 1361 | * Handle the last piece of text. |
| 1362 | */ |
| 1363 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1364 | xmlNodeAddContentLen(last, q, cur - q); |
| 1365 | } else { |
| 1366 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1367 | if (node == NULL) return(ret); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1368 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1369 | last = ret = node; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1370 | } else { |
| 1371 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | return(ret); |
| 1376 | } |
| 1377 | |
| 1378 | /** |
| 1379 | * xmlNodeListGetString: |
| 1380 | * @doc: the document |
| 1381 | * @list: a Node list |
| 1382 | * @inLine: should we replace entity contents or show their external form |
| 1383 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1384 | * Build the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1385 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1386 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1387 | * 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] | 1388 | */ |
| 1389 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1390 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1391 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1392 | xmlNodePtr node = list; |
| 1393 | xmlChar *ret = NULL; |
| 1394 | xmlEntityPtr ent; |
| 1395 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1396 | if (list == NULL) |
| 1397 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1398 | |
| 1399 | while (node != NULL) { |
| 1400 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1401 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1402 | if (inLine) { |
| 1403 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1404 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1405 | xmlChar *buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1406 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1407 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
| 1408 | if (buffer != NULL) { |
| 1409 | ret = xmlStrcat(ret, buffer); |
| 1410 | xmlFree(buffer); |
| 1411 | } |
| 1412 | } |
| 1413 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1414 | if (inLine) { |
| 1415 | ent = xmlGetDocEntity(doc, node->name); |
| 1416 | if (ent != NULL) { |
| 1417 | xmlChar *buffer; |
| 1418 | |
| 1419 | /* an entity content can be any "well balanced chunk", |
| 1420 | * i.e. the result of the content [43] production: |
| 1421 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1422 | * So it can contain text, CDATA section or nested |
| 1423 | * entity reference nodes (among others). |
| 1424 | * -> we recursive call xmlNodeListGetString() |
| 1425 | * which handles these types */ |
| 1426 | buffer = xmlNodeListGetString(doc, ent->children, 1); |
| 1427 | if (buffer != NULL) { |
| 1428 | ret = xmlStrcat(ret, buffer); |
| 1429 | xmlFree(buffer); |
| 1430 | } |
| 1431 | } else { |
| 1432 | ret = xmlStrcat(ret, node->content); |
| 1433 | } |
| 1434 | } else { |
| 1435 | xmlChar buf[2]; |
| 1436 | |
| 1437 | buf[0] = '&'; |
| 1438 | buf[1] = 0; |
| 1439 | ret = xmlStrncat(ret, buf, 1); |
| 1440 | ret = xmlStrcat(ret, node->name); |
| 1441 | buf[0] = ';'; |
| 1442 | buf[1] = 0; |
| 1443 | ret = xmlStrncat(ret, buf, 1); |
| 1444 | } |
| 1445 | } |
| 1446 | #if 0 |
| 1447 | else { |
| 1448 | xmlGenericError(xmlGenericErrorContext, |
| 1449 | "xmlGetNodeListString : invalid node type %d\n", |
| 1450 | node->type); |
| 1451 | } |
| 1452 | #endif |
| 1453 | node = node->next; |
| 1454 | } |
| 1455 | return (ret); |
| 1456 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1457 | /** |
| 1458 | * xmlNodeListGetRawString: |
| 1459 | * @doc: the document |
| 1460 | * @list: a Node list |
| 1461 | * @inLine: should we replace entity contents or show their external form |
| 1462 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1463 | * Builds the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1464 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 1465 | * this function doesn't do any character encoding handling. |
| 1466 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1467 | * 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] | 1468 | */ |
| 1469 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1470 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1471 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1472 | xmlNodePtr node = list; |
| 1473 | xmlChar *ret = NULL; |
| 1474 | xmlEntityPtr ent; |
| 1475 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1476 | if (list == NULL) |
| 1477 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1478 | |
| 1479 | while (node != NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1480 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1481 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1482 | if (inLine) { |
| 1483 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1484 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1485 | xmlChar *buffer; |
| 1486 | |
| 1487 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 1488 | if (buffer != NULL) { |
| 1489 | ret = xmlStrcat(ret, buffer); |
| 1490 | xmlFree(buffer); |
| 1491 | } |
| 1492 | } |
| 1493 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1494 | if (inLine) { |
| 1495 | ent = xmlGetDocEntity(doc, node->name); |
| 1496 | if (ent != NULL) { |
| 1497 | xmlChar *buffer; |
| 1498 | |
| 1499 | /* an entity content can be any "well balanced chunk", |
| 1500 | * i.e. the result of the content [43] production: |
| 1501 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1502 | * So it can contain text, CDATA section or nested |
| 1503 | * entity reference nodes (among others). |
| 1504 | * -> we recursive call xmlNodeListGetRawString() |
| 1505 | * which handles these types */ |
| 1506 | buffer = |
| 1507 | xmlNodeListGetRawString(doc, ent->children, 1); |
| 1508 | if (buffer != NULL) { |
| 1509 | ret = xmlStrcat(ret, buffer); |
| 1510 | xmlFree(buffer); |
| 1511 | } |
| 1512 | } else { |
| 1513 | ret = xmlStrcat(ret, node->content); |
| 1514 | } |
| 1515 | } else { |
| 1516 | xmlChar buf[2]; |
| 1517 | |
| 1518 | buf[0] = '&'; |
| 1519 | buf[1] = 0; |
| 1520 | ret = xmlStrncat(ret, buf, 1); |
| 1521 | ret = xmlStrcat(ret, node->name); |
| 1522 | buf[0] = ';'; |
| 1523 | buf[1] = 0; |
| 1524 | ret = xmlStrncat(ret, buf, 1); |
| 1525 | } |
| 1526 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1527 | #if 0 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1528 | else { |
| 1529 | xmlGenericError(xmlGenericErrorContext, |
| 1530 | "xmlGetNodeListString : invalid node type %d\n", |
| 1531 | node->type); |
| 1532 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1533 | #endif |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1534 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1535 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1536 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | /** |
| 1540 | * xmlNewProp: |
| 1541 | * @node: the holding node |
| 1542 | * @name: the name of the attribute |
| 1543 | * @value: the value of the attribute |
| 1544 | * |
| 1545 | * Create a new property carried by a node. |
| 1546 | * Returns a pointer to the attribute |
| 1547 | */ |
| 1548 | xmlAttrPtr |
| 1549 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 1550 | xmlAttrPtr cur; |
| 1551 | xmlDocPtr doc = NULL; |
| 1552 | |
| 1553 | if (name == NULL) { |
| 1554 | #ifdef DEBUG_TREE |
| 1555 | xmlGenericError(xmlGenericErrorContext, |
| 1556 | "xmlNewProp : name == NULL\n"); |
| 1557 | #endif |
| 1558 | return(NULL); |
| 1559 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 1560 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 1561 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1562 | |
| 1563 | /* |
| 1564 | * Allocate a new property and fill the fields. |
| 1565 | */ |
| 1566 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1567 | if (cur == NULL) { |
| 1568 | xmlGenericError(xmlGenericErrorContext, |
| 1569 | "xmlNewProp : malloc failed\n"); |
| 1570 | return(NULL); |
| 1571 | } |
| 1572 | memset(cur, 0, sizeof(xmlAttr)); |
| 1573 | cur->type = XML_ATTRIBUTE_NODE; |
| 1574 | |
| 1575 | cur->parent = node; |
| 1576 | if (node != NULL) { |
| 1577 | doc = node->doc; |
| 1578 | cur->doc = doc; |
| 1579 | } |
| 1580 | cur->name = xmlStrdup(name); |
| 1581 | if (value != NULL) { |
| 1582 | xmlChar *buffer; |
| 1583 | xmlNodePtr tmp; |
| 1584 | |
| 1585 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1586 | cur->children = xmlStringGetNodeList(doc, buffer); |
| 1587 | cur->last = NULL; |
| 1588 | tmp = cur->children; |
| 1589 | while (tmp != NULL) { |
| 1590 | tmp->parent = (xmlNodePtr) cur; |
| 1591 | tmp->doc = doc; |
| 1592 | if (tmp->next == NULL) |
| 1593 | cur->last = tmp; |
| 1594 | tmp = tmp->next; |
| 1595 | } |
| 1596 | xmlFree(buffer); |
| 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * Add it at the end to preserve parsing order ... |
| 1601 | */ |
| 1602 | if (node != NULL) { |
| 1603 | if (node->properties == NULL) { |
| 1604 | node->properties = cur; |
| 1605 | } else { |
| 1606 | xmlAttrPtr prev = node->properties; |
| 1607 | |
| 1608 | while (prev->next != NULL) prev = prev->next; |
| 1609 | prev->next = cur; |
| 1610 | cur->prev = prev; |
| 1611 | } |
| 1612 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1613 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1614 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1615 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1616 | return(cur); |
| 1617 | } |
| 1618 | |
| 1619 | /** |
| 1620 | * xmlNewNsProp: |
| 1621 | * @node: the holding node |
| 1622 | * @ns: the namespace |
| 1623 | * @name: the name of the attribute |
| 1624 | * @value: the value of the attribute |
| 1625 | * |
| 1626 | * Create a new property tagged with a namespace and carried by a node. |
| 1627 | * Returns a pointer to the attribute |
| 1628 | */ |
| 1629 | xmlAttrPtr |
| 1630 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 1631 | const xmlChar *value) { |
| 1632 | xmlAttrPtr cur; |
Daniel Veillard | a682b21 | 2001-06-07 19:59:42 +0000 | [diff] [blame] | 1633 | xmlDocPtr doc = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1634 | |
| 1635 | if (name == NULL) { |
| 1636 | #ifdef DEBUG_TREE |
| 1637 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1638 | "xmlNewNsProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1639 | #endif |
| 1640 | return(NULL); |
| 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | * Allocate a new property and fill the fields. |
| 1645 | */ |
| 1646 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1647 | if (cur == NULL) { |
| 1648 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1649 | "xmlNewNsProp : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1650 | return(NULL); |
| 1651 | } |
| 1652 | memset(cur, 0, sizeof(xmlAttr)); |
| 1653 | cur->type = XML_ATTRIBUTE_NODE; |
| 1654 | |
| 1655 | cur->parent = node; |
Daniel Veillard | a682b21 | 2001-06-07 19:59:42 +0000 | [diff] [blame] | 1656 | if (node != NULL) { |
| 1657 | doc = node->doc; |
| 1658 | cur->doc = doc; |
| 1659 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1660 | cur->ns = ns; |
| 1661 | cur->name = xmlStrdup(name); |
| 1662 | if (value != NULL) { |
| 1663 | xmlChar *buffer; |
| 1664 | xmlNodePtr tmp; |
| 1665 | |
Daniel Veillard | a682b21 | 2001-06-07 19:59:42 +0000 | [diff] [blame] | 1666 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1667 | cur->children = xmlStringGetNodeList(doc, buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1668 | cur->last = NULL; |
| 1669 | tmp = cur->children; |
| 1670 | while (tmp != NULL) { |
| 1671 | tmp->parent = (xmlNodePtr) cur; |
| 1672 | if (tmp->next == NULL) |
| 1673 | cur->last = tmp; |
| 1674 | tmp = tmp->next; |
| 1675 | } |
| 1676 | xmlFree(buffer); |
| 1677 | } |
| 1678 | |
| 1679 | /* |
| 1680 | * Add it at the end to preserve parsing order ... |
| 1681 | */ |
| 1682 | if (node != NULL) { |
| 1683 | if (node->properties == NULL) { |
| 1684 | node->properties = cur; |
| 1685 | } else { |
| 1686 | xmlAttrPtr prev = node->properties; |
| 1687 | |
| 1688 | while (prev->next != NULL) prev = prev->next; |
| 1689 | prev->next = cur; |
| 1690 | cur->prev = prev; |
| 1691 | } |
| 1692 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1693 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1694 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1695 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1696 | return(cur); |
| 1697 | } |
| 1698 | |
| 1699 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1700 | * xmlNewNsPropEatName: |
| 1701 | * @node: the holding node |
| 1702 | * @ns: the namespace |
| 1703 | * @name: the name of the attribute |
| 1704 | * @value: the value of the attribute |
| 1705 | * |
| 1706 | * Create a new property tagged with a namespace and carried by a node. |
| 1707 | * Returns a pointer to the attribute |
| 1708 | */ |
| 1709 | xmlAttrPtr |
| 1710 | xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name, |
| 1711 | const xmlChar *value) { |
| 1712 | xmlAttrPtr cur; |
| 1713 | xmlDocPtr doc = NULL; |
| 1714 | |
| 1715 | if (name == NULL) { |
| 1716 | #ifdef DEBUG_TREE |
| 1717 | xmlGenericError(xmlGenericErrorContext, |
| 1718 | "xmlNewNsPropEatName : name == NULL\n"); |
| 1719 | #endif |
| 1720 | return(NULL); |
| 1721 | } |
| 1722 | |
| 1723 | /* |
| 1724 | * Allocate a new property and fill the fields. |
| 1725 | */ |
| 1726 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1727 | if (cur == NULL) { |
| 1728 | xmlGenericError(xmlGenericErrorContext, |
| 1729 | "xmlNewNsPropEatName : malloc failed\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1730 | xmlFree(name); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1731 | return(NULL); |
| 1732 | } |
| 1733 | memset(cur, 0, sizeof(xmlAttr)); |
| 1734 | cur->type = XML_ATTRIBUTE_NODE; |
| 1735 | |
| 1736 | cur->parent = node; |
| 1737 | if (node != NULL) { |
| 1738 | doc = node->doc; |
| 1739 | cur->doc = doc; |
| 1740 | } |
| 1741 | cur->ns = ns; |
| 1742 | cur->name = name; |
| 1743 | if (value != NULL) { |
| 1744 | xmlChar *buffer; |
| 1745 | xmlNodePtr tmp; |
| 1746 | |
| 1747 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1748 | cur->children = xmlStringGetNodeList(doc, buffer); |
| 1749 | cur->last = NULL; |
| 1750 | tmp = cur->children; |
| 1751 | while (tmp != NULL) { |
| 1752 | tmp->parent = (xmlNodePtr) cur; |
| 1753 | if (tmp->next == NULL) |
| 1754 | cur->last = tmp; |
| 1755 | tmp = tmp->next; |
| 1756 | } |
| 1757 | xmlFree(buffer); |
| 1758 | } |
| 1759 | |
| 1760 | /* |
| 1761 | * Add it at the end to preserve parsing order ... |
| 1762 | */ |
| 1763 | if (node != NULL) { |
| 1764 | if (node->properties == NULL) { |
| 1765 | node->properties = cur; |
| 1766 | } else { |
| 1767 | xmlAttrPtr prev = node->properties; |
| 1768 | |
| 1769 | while (prev->next != NULL) prev = prev->next; |
| 1770 | prev->next = cur; |
| 1771 | cur->prev = prev; |
| 1772 | } |
| 1773 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1774 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1775 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1776 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1777 | return(cur); |
| 1778 | } |
| 1779 | |
| 1780 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1781 | * xmlNewDocProp: |
| 1782 | * @doc: the document |
| 1783 | * @name: the name of the attribute |
| 1784 | * @value: the value of the attribute |
| 1785 | * |
| 1786 | * Create a new property carried by a document. |
| 1787 | * Returns a pointer to the attribute |
| 1788 | */ |
| 1789 | xmlAttrPtr |
| 1790 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
| 1791 | xmlAttrPtr cur; |
| 1792 | |
| 1793 | if (name == NULL) { |
| 1794 | #ifdef DEBUG_TREE |
| 1795 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1796 | "xmlNewDocProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1797 | #endif |
| 1798 | return(NULL); |
| 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | * Allocate a new property and fill the fields. |
| 1803 | */ |
| 1804 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1805 | if (cur == NULL) { |
| 1806 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1807 | "xmlNewDocProp : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1808 | return(NULL); |
| 1809 | } |
| 1810 | memset(cur, 0, sizeof(xmlAttr)); |
| 1811 | cur->type = XML_ATTRIBUTE_NODE; |
| 1812 | |
| 1813 | cur->name = xmlStrdup(name); |
| 1814 | cur->doc = doc; |
| 1815 | if (value != NULL) { |
| 1816 | xmlNodePtr tmp; |
| 1817 | |
| 1818 | cur->children = xmlStringGetNodeList(doc, value); |
| 1819 | cur->last = NULL; |
| 1820 | |
| 1821 | tmp = cur->children; |
| 1822 | while (tmp != NULL) { |
| 1823 | tmp->parent = (xmlNodePtr) cur; |
| 1824 | if (tmp->next == NULL) |
| 1825 | cur->last = tmp; |
| 1826 | tmp = tmp->next; |
| 1827 | } |
| 1828 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1829 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1830 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1831 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1832 | return(cur); |
| 1833 | } |
| 1834 | |
| 1835 | /** |
| 1836 | * xmlFreePropList: |
| 1837 | * @cur: the first property in the list |
| 1838 | * |
| 1839 | * Free a property and all its siblings, all the children are freed too. |
| 1840 | */ |
| 1841 | void |
| 1842 | xmlFreePropList(xmlAttrPtr cur) { |
| 1843 | xmlAttrPtr next; |
| 1844 | if (cur == NULL) { |
| 1845 | #ifdef DEBUG_TREE |
| 1846 | xmlGenericError(xmlGenericErrorContext, |
| 1847 | "xmlFreePropList : property == NULL\n"); |
| 1848 | #endif |
| 1849 | return; |
| 1850 | } |
| 1851 | while (cur != NULL) { |
| 1852 | next = cur->next; |
| 1853 | xmlFreeProp(cur); |
| 1854 | cur = next; |
| 1855 | } |
| 1856 | } |
| 1857 | |
| 1858 | /** |
| 1859 | * xmlFreeProp: |
| 1860 | * @cur: an attribute |
| 1861 | * |
| 1862 | * Free one attribute, all the content is freed too |
| 1863 | */ |
| 1864 | void |
| 1865 | xmlFreeProp(xmlAttrPtr cur) { |
| 1866 | if (cur == NULL) { |
| 1867 | #ifdef DEBUG_TREE |
| 1868 | xmlGenericError(xmlGenericErrorContext, |
| 1869 | "xmlFreeProp : property == NULL\n"); |
| 1870 | #endif |
| 1871 | return; |
| 1872 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1873 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1874 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1875 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1876 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1877 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1878 | if ((cur->parent != NULL) && (cur->parent->doc != NULL) && |
| 1879 | ((cur->parent->doc->intSubset != NULL) || |
| 1880 | (cur->parent->doc->extSubset != NULL))) { |
| 1881 | if (xmlIsID(cur->parent->doc, cur->parent, cur)) |
| 1882 | xmlRemoveID(cur->parent->doc, cur); |
| 1883 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1884 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 1885 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1886 | xmlFree(cur); |
| 1887 | } |
| 1888 | |
| 1889 | /** |
| 1890 | * xmlRemoveProp: |
| 1891 | * @cur: an attribute |
| 1892 | * |
| 1893 | * Unlink and free one attribute, all the content is freed too |
| 1894 | * Note this doesn't work for namespace definition attributes |
| 1895 | * |
| 1896 | * Returns 0 if success and -1 in case of error. |
| 1897 | */ |
| 1898 | int |
| 1899 | xmlRemoveProp(xmlAttrPtr cur) { |
| 1900 | xmlAttrPtr tmp; |
| 1901 | if (cur == NULL) { |
| 1902 | #ifdef DEBUG_TREE |
| 1903 | xmlGenericError(xmlGenericErrorContext, |
| 1904 | "xmlRemoveProp : cur == NULL\n"); |
| 1905 | #endif |
| 1906 | return(-1); |
| 1907 | } |
| 1908 | if (cur->parent == NULL) { |
| 1909 | #ifdef DEBUG_TREE |
| 1910 | xmlGenericError(xmlGenericErrorContext, |
| 1911 | "xmlRemoveProp : cur->parent == NULL\n"); |
| 1912 | #endif |
| 1913 | return(-1); |
| 1914 | } |
| 1915 | tmp = cur->parent->properties; |
| 1916 | if (tmp == cur) { |
| 1917 | cur->parent->properties = cur->next; |
| 1918 | xmlFreeProp(cur); |
| 1919 | return(0); |
| 1920 | } |
| 1921 | while (tmp != NULL) { |
| 1922 | if (tmp->next == cur) { |
| 1923 | tmp->next = cur->next; |
| 1924 | if (tmp->next != NULL) |
| 1925 | tmp->next->prev = tmp; |
| 1926 | xmlFreeProp(cur); |
| 1927 | return(0); |
| 1928 | } |
| 1929 | tmp = tmp->next; |
| 1930 | } |
| 1931 | #ifdef DEBUG_TREE |
| 1932 | xmlGenericError(xmlGenericErrorContext, |
| 1933 | "xmlRemoveProp : attribute not owned by its node\n"); |
| 1934 | #endif |
| 1935 | return(-1); |
| 1936 | } |
| 1937 | |
| 1938 | /** |
| 1939 | * xmlNewPI: |
| 1940 | * @name: the processing instruction name |
| 1941 | * @content: the PI content |
| 1942 | * |
| 1943 | * Creation of a processing instruction element. |
| 1944 | * Returns a pointer to the new node object. |
| 1945 | */ |
| 1946 | xmlNodePtr |
| 1947 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
| 1948 | xmlNodePtr cur; |
| 1949 | |
| 1950 | if (name == NULL) { |
| 1951 | #ifdef DEBUG_TREE |
| 1952 | xmlGenericError(xmlGenericErrorContext, |
| 1953 | "xmlNewPI : name == NULL\n"); |
| 1954 | #endif |
| 1955 | return(NULL); |
| 1956 | } |
| 1957 | |
| 1958 | /* |
| 1959 | * Allocate a new node and fill the fields. |
| 1960 | */ |
| 1961 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1962 | if (cur == NULL) { |
| 1963 | xmlGenericError(xmlGenericErrorContext, |
| 1964 | "xmlNewPI : malloc failed\n"); |
| 1965 | return(NULL); |
| 1966 | } |
| 1967 | memset(cur, 0, sizeof(xmlNode)); |
| 1968 | cur->type = XML_PI_NODE; |
| 1969 | |
| 1970 | cur->name = xmlStrdup(name); |
| 1971 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1972 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1973 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1974 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1975 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1976 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1977 | return(cur); |
| 1978 | } |
| 1979 | |
| 1980 | /** |
| 1981 | * xmlNewNode: |
| 1982 | * @ns: namespace if any |
| 1983 | * @name: the node name |
| 1984 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1985 | * Creation of a new node element. @ns is optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1986 | * |
| 1987 | * Returns a pointer to the new node object. |
| 1988 | */ |
| 1989 | xmlNodePtr |
| 1990 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 1991 | xmlNodePtr cur; |
| 1992 | |
| 1993 | if (name == NULL) { |
| 1994 | #ifdef DEBUG_TREE |
| 1995 | xmlGenericError(xmlGenericErrorContext, |
| 1996 | "xmlNewNode : name == NULL\n"); |
| 1997 | #endif |
| 1998 | return(NULL); |
| 1999 | } |
| 2000 | |
| 2001 | /* |
| 2002 | * Allocate a new node and fill the fields. |
| 2003 | */ |
| 2004 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2005 | if (cur == NULL) { |
| 2006 | xmlGenericError(xmlGenericErrorContext, |
| 2007 | "xmlNewNode : malloc failed\n"); |
| 2008 | return(NULL); |
| 2009 | } |
| 2010 | memset(cur, 0, sizeof(xmlNode)); |
| 2011 | cur->type = XML_ELEMENT_NODE; |
| 2012 | |
| 2013 | cur->name = xmlStrdup(name); |
| 2014 | cur->ns = ns; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2015 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2016 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2017 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2018 | return(cur); |
| 2019 | } |
| 2020 | |
| 2021 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2022 | * xmlNewNodeEatName: |
| 2023 | * @ns: namespace if any |
| 2024 | * @name: the node name |
| 2025 | * |
| 2026 | * Creation of a new node element. @ns is optional (NULL). |
| 2027 | * |
| 2028 | * Returns a pointer to the new node object. |
| 2029 | */ |
| 2030 | xmlNodePtr |
| 2031 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
| 2032 | xmlNodePtr cur; |
| 2033 | |
| 2034 | if (name == NULL) { |
| 2035 | #ifdef DEBUG_TREE |
| 2036 | xmlGenericError(xmlGenericErrorContext, |
| 2037 | "xmlNewNode : name == NULL\n"); |
| 2038 | #endif |
| 2039 | return(NULL); |
| 2040 | } |
| 2041 | |
| 2042 | /* |
| 2043 | * Allocate a new node and fill the fields. |
| 2044 | */ |
| 2045 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2046 | if (cur == NULL) { |
| 2047 | xmlGenericError(xmlGenericErrorContext, |
| 2048 | "xmlNewNode : malloc failed\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2049 | xmlFree(name); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2050 | return(NULL); |
| 2051 | } |
| 2052 | memset(cur, 0, sizeof(xmlNode)); |
| 2053 | cur->type = XML_ELEMENT_NODE; |
| 2054 | |
| 2055 | cur->name = name; |
| 2056 | cur->ns = ns; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2057 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2058 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2059 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2060 | return(cur); |
| 2061 | } |
| 2062 | |
| 2063 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2064 | * xmlNewDocNode: |
| 2065 | * @doc: the document |
| 2066 | * @ns: namespace if any |
| 2067 | * @name: the node name |
| 2068 | * @content: the XML text content if any |
| 2069 | * |
| 2070 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2071 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2072 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2073 | * references, but XML special chars need to be escaped first by using |
| 2074 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2075 | * need entities support. |
| 2076 | * |
| 2077 | * Returns a pointer to the new node object. |
| 2078 | */ |
| 2079 | xmlNodePtr |
| 2080 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2081 | const xmlChar *name, const xmlChar *content) { |
| 2082 | xmlNodePtr cur; |
| 2083 | |
| 2084 | cur = xmlNewNode(ns, name); |
| 2085 | if (cur != NULL) { |
| 2086 | cur->doc = doc; |
| 2087 | if (content != NULL) { |
| 2088 | cur->children = xmlStringGetNodeList(doc, content); |
| 2089 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2090 | } |
| 2091 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2092 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2093 | return(cur); |
| 2094 | } |
| 2095 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2096 | /** |
| 2097 | * xmlNewDocNodeEatName: |
| 2098 | * @doc: the document |
| 2099 | * @ns: namespace if any |
| 2100 | * @name: the node name |
| 2101 | * @content: the XML text content if any |
| 2102 | * |
| 2103 | * Creation of a new node element within a document. @ns and @content |
| 2104 | * are optional (NULL). |
| 2105 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2106 | * references, but XML special chars need to be escaped first by using |
| 2107 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2108 | * need entities support. |
| 2109 | * |
| 2110 | * Returns a pointer to the new node object. |
| 2111 | */ |
| 2112 | xmlNodePtr |
| 2113 | xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns, |
| 2114 | xmlChar *name, const xmlChar *content) { |
| 2115 | xmlNodePtr cur; |
| 2116 | |
| 2117 | cur = xmlNewNodeEatName(ns, name); |
| 2118 | if (cur != NULL) { |
| 2119 | cur->doc = doc; |
| 2120 | if (content != NULL) { |
| 2121 | cur->children = xmlStringGetNodeList(doc, content); |
| 2122 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2123 | } |
| 2124 | } |
| 2125 | return(cur); |
| 2126 | } |
| 2127 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2128 | |
| 2129 | /** |
| 2130 | * xmlNewDocRawNode: |
| 2131 | * @doc: the document |
| 2132 | * @ns: namespace if any |
| 2133 | * @name: the node name |
| 2134 | * @content: the text content if any |
| 2135 | * |
| 2136 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2137 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2138 | * |
| 2139 | * Returns a pointer to the new node object. |
| 2140 | */ |
| 2141 | xmlNodePtr |
| 2142 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2143 | const xmlChar *name, const xmlChar *content) { |
| 2144 | xmlNodePtr cur; |
| 2145 | |
| 2146 | cur = xmlNewNode(ns, name); |
| 2147 | if (cur != NULL) { |
| 2148 | cur->doc = doc; |
| 2149 | if (content != NULL) { |
| 2150 | cur->children = xmlNewDocText(doc, content); |
| 2151 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2152 | } |
| 2153 | } |
| 2154 | return(cur); |
| 2155 | } |
| 2156 | |
| 2157 | /** |
| 2158 | * xmlNewDocFragment: |
| 2159 | * @doc: the document owning the fragment |
| 2160 | * |
| 2161 | * Creation of a new Fragment node. |
| 2162 | * Returns a pointer to the new node object. |
| 2163 | */ |
| 2164 | xmlNodePtr |
| 2165 | xmlNewDocFragment(xmlDocPtr doc) { |
| 2166 | xmlNodePtr cur; |
| 2167 | |
| 2168 | /* |
| 2169 | * Allocate a new DocumentFragment node and fill the fields. |
| 2170 | */ |
| 2171 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2172 | if (cur == NULL) { |
| 2173 | xmlGenericError(xmlGenericErrorContext, |
| 2174 | "xmlNewDocFragment : malloc failed\n"); |
| 2175 | return(NULL); |
| 2176 | } |
| 2177 | memset(cur, 0, sizeof(xmlNode)); |
| 2178 | cur->type = XML_DOCUMENT_FRAG_NODE; |
| 2179 | |
| 2180 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2181 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2182 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2183 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2184 | return(cur); |
| 2185 | } |
| 2186 | |
| 2187 | /** |
| 2188 | * xmlNewText: |
| 2189 | * @content: the text content |
| 2190 | * |
| 2191 | * Creation of a new text node. |
| 2192 | * Returns a pointer to the new node object. |
| 2193 | */ |
| 2194 | xmlNodePtr |
| 2195 | xmlNewText(const xmlChar *content) { |
| 2196 | xmlNodePtr cur; |
| 2197 | |
| 2198 | /* |
| 2199 | * Allocate a new node and fill the fields. |
| 2200 | */ |
| 2201 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2202 | if (cur == NULL) { |
| 2203 | xmlGenericError(xmlGenericErrorContext, |
| 2204 | "xmlNewText : malloc failed\n"); |
| 2205 | return(NULL); |
| 2206 | } |
| 2207 | memset(cur, 0, sizeof(xmlNode)); |
| 2208 | cur->type = XML_TEXT_NODE; |
| 2209 | |
| 2210 | cur->name = xmlStringText; |
| 2211 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2212 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2213 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2214 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2215 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2216 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2217 | return(cur); |
| 2218 | } |
| 2219 | |
| 2220 | /** |
| 2221 | * xmlNewTextChild: |
| 2222 | * @parent: the parent node |
| 2223 | * @ns: a namespace if any |
| 2224 | * @name: the name of the child |
| 2225 | * @content: the text content of the child if any. |
| 2226 | * |
| 2227 | * 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] | 2228 | * @ns and @content parameters are optional (NULL). If content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2229 | * a child TEXT node will be created containing the string content. |
| 2230 | * |
| 2231 | * Returns a pointer to the new node object. |
| 2232 | */ |
| 2233 | xmlNodePtr |
| 2234 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2235 | const xmlChar *name, const xmlChar *content) { |
| 2236 | xmlNodePtr cur, prev; |
| 2237 | |
| 2238 | if (parent == NULL) { |
| 2239 | #ifdef DEBUG_TREE |
| 2240 | xmlGenericError(xmlGenericErrorContext, |
| 2241 | "xmlNewTextChild : parent == NULL\n"); |
| 2242 | #endif |
| 2243 | return(NULL); |
| 2244 | } |
| 2245 | |
| 2246 | if (name == NULL) { |
| 2247 | #ifdef DEBUG_TREE |
| 2248 | xmlGenericError(xmlGenericErrorContext, |
| 2249 | "xmlNewTextChild : name == NULL\n"); |
| 2250 | #endif |
| 2251 | return(NULL); |
| 2252 | } |
| 2253 | |
| 2254 | /* |
| 2255 | * Allocate a new node |
| 2256 | */ |
| 2257 | if (ns == NULL) |
| 2258 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 2259 | else |
| 2260 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 2261 | if (cur == NULL) return(NULL); |
| 2262 | |
| 2263 | /* |
| 2264 | * add the new element at the end of the children list. |
| 2265 | */ |
| 2266 | cur->type = XML_ELEMENT_NODE; |
| 2267 | cur->parent = parent; |
| 2268 | cur->doc = parent->doc; |
| 2269 | if (parent->children == NULL) { |
| 2270 | parent->children = cur; |
| 2271 | parent->last = cur; |
| 2272 | } else { |
| 2273 | prev = parent->last; |
| 2274 | prev->next = cur; |
| 2275 | cur->prev = prev; |
| 2276 | parent->last = cur; |
| 2277 | } |
| 2278 | |
| 2279 | return(cur); |
| 2280 | } |
| 2281 | |
| 2282 | /** |
| 2283 | * xmlNewCharRef: |
| 2284 | * @doc: the document |
| 2285 | * @name: the char ref string, starting with # or "&# ... ;" |
| 2286 | * |
| 2287 | * Creation of a new character reference node. |
| 2288 | * Returns a pointer to the new node object. |
| 2289 | */ |
| 2290 | xmlNodePtr |
| 2291 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 2292 | xmlNodePtr cur; |
| 2293 | |
| 2294 | /* |
| 2295 | * Allocate a new node and fill the fields. |
| 2296 | */ |
| 2297 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2298 | if (cur == NULL) { |
| 2299 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2300 | "xmlNewCharRef : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2301 | return(NULL); |
| 2302 | } |
| 2303 | memset(cur, 0, sizeof(xmlNode)); |
| 2304 | cur->type = XML_ENTITY_REF_NODE; |
| 2305 | |
| 2306 | cur->doc = doc; |
| 2307 | if (name[0] == '&') { |
| 2308 | int len; |
| 2309 | name++; |
| 2310 | len = xmlStrlen(name); |
| 2311 | if (name[len - 1] == ';') |
| 2312 | cur->name = xmlStrndup(name, len - 1); |
| 2313 | else |
| 2314 | cur->name = xmlStrndup(name, len); |
| 2315 | } else |
| 2316 | cur->name = xmlStrdup(name); |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2317 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2318 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2319 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2320 | return(cur); |
| 2321 | } |
| 2322 | |
| 2323 | /** |
| 2324 | * xmlNewReference: |
| 2325 | * @doc: the document |
| 2326 | * @name: the reference name, or the reference string with & and ; |
| 2327 | * |
| 2328 | * Creation of a new reference node. |
| 2329 | * Returns a pointer to the new node object. |
| 2330 | */ |
| 2331 | xmlNodePtr |
| 2332 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 2333 | xmlNodePtr cur; |
| 2334 | xmlEntityPtr ent; |
| 2335 | |
| 2336 | /* |
| 2337 | * Allocate a new node and fill the fields. |
| 2338 | */ |
| 2339 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2340 | if (cur == NULL) { |
| 2341 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2342 | "xmlNewReference : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2343 | return(NULL); |
| 2344 | } |
| 2345 | memset(cur, 0, sizeof(xmlNode)); |
| 2346 | cur->type = XML_ENTITY_REF_NODE; |
| 2347 | |
| 2348 | cur->doc = doc; |
| 2349 | if (name[0] == '&') { |
| 2350 | int len; |
| 2351 | name++; |
| 2352 | len = xmlStrlen(name); |
| 2353 | if (name[len - 1] == ';') |
| 2354 | cur->name = xmlStrndup(name, len - 1); |
| 2355 | else |
| 2356 | cur->name = xmlStrndup(name, len); |
| 2357 | } else |
| 2358 | cur->name = xmlStrdup(name); |
| 2359 | |
| 2360 | ent = xmlGetDocEntity(doc, cur->name); |
| 2361 | if (ent != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2362 | cur->content = ent->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2363 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2364 | * 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] | 2365 | * updated. Not sure if this is 100% correct. |
| 2366 | * -George |
| 2367 | */ |
| 2368 | cur->children = (xmlNodePtr) ent; |
| 2369 | cur->last = (xmlNodePtr) ent; |
| 2370 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2371 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2372 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2373 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2374 | return(cur); |
| 2375 | } |
| 2376 | |
| 2377 | /** |
| 2378 | * xmlNewDocText: |
| 2379 | * @doc: the document |
| 2380 | * @content: the text content |
| 2381 | * |
| 2382 | * Creation of a new text node within a document. |
| 2383 | * Returns a pointer to the new node object. |
| 2384 | */ |
| 2385 | xmlNodePtr |
| 2386 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 2387 | xmlNodePtr cur; |
| 2388 | |
| 2389 | cur = xmlNewText(content); |
| 2390 | if (cur != NULL) cur->doc = doc; |
| 2391 | return(cur); |
| 2392 | } |
| 2393 | |
| 2394 | /** |
| 2395 | * xmlNewTextLen: |
| 2396 | * @content: the text content |
| 2397 | * @len: the text len. |
| 2398 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2399 | * 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] | 2400 | * Returns a pointer to the new node object. |
| 2401 | */ |
| 2402 | xmlNodePtr |
| 2403 | xmlNewTextLen(const xmlChar *content, int len) { |
| 2404 | xmlNodePtr cur; |
| 2405 | |
| 2406 | /* |
| 2407 | * Allocate a new node and fill the fields. |
| 2408 | */ |
| 2409 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2410 | if (cur == NULL) { |
| 2411 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2412 | "xmlNewTextLen : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2413 | return(NULL); |
| 2414 | } |
| 2415 | memset(cur, 0, sizeof(xmlNode)); |
| 2416 | cur->type = XML_TEXT_NODE; |
| 2417 | |
| 2418 | cur->name = xmlStringText; |
| 2419 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2420 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2421 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2422 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2423 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2424 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2425 | return(cur); |
| 2426 | } |
| 2427 | |
| 2428 | /** |
| 2429 | * xmlNewDocTextLen: |
| 2430 | * @doc: the document |
| 2431 | * @content: the text content |
| 2432 | * @len: the text len. |
| 2433 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2434 | * 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] | 2435 | * text node pertain to a given document. |
| 2436 | * Returns a pointer to the new node object. |
| 2437 | */ |
| 2438 | xmlNodePtr |
| 2439 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2440 | xmlNodePtr cur; |
| 2441 | |
| 2442 | cur = xmlNewTextLen(content, len); |
| 2443 | if (cur != NULL) cur->doc = doc; |
| 2444 | return(cur); |
| 2445 | } |
| 2446 | |
| 2447 | /** |
| 2448 | * xmlNewComment: |
| 2449 | * @content: the comment content |
| 2450 | * |
| 2451 | * Creation of a new node containing a comment. |
| 2452 | * Returns a pointer to the new node object. |
| 2453 | */ |
| 2454 | xmlNodePtr |
| 2455 | xmlNewComment(const xmlChar *content) { |
| 2456 | xmlNodePtr cur; |
| 2457 | |
| 2458 | /* |
| 2459 | * Allocate a new node and fill the fields. |
| 2460 | */ |
| 2461 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2462 | if (cur == NULL) { |
| 2463 | xmlGenericError(xmlGenericErrorContext, |
| 2464 | "xmlNewComment : malloc failed\n"); |
| 2465 | return(NULL); |
| 2466 | } |
| 2467 | memset(cur, 0, sizeof(xmlNode)); |
| 2468 | cur->type = XML_COMMENT_NODE; |
| 2469 | |
| 2470 | cur->name = xmlStringComment; |
| 2471 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2472 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2473 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2474 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2475 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2476 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2477 | return(cur); |
| 2478 | } |
| 2479 | |
| 2480 | /** |
| 2481 | * xmlNewCDataBlock: |
| 2482 | * @doc: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2483 | * @content: the CDATA block content content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2484 | * @len: the length of the block |
| 2485 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2486 | * Creation of a new node containing a CDATA block. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2487 | * Returns a pointer to the new node object. |
| 2488 | */ |
| 2489 | xmlNodePtr |
| 2490 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2491 | xmlNodePtr cur; |
| 2492 | |
| 2493 | /* |
| 2494 | * Allocate a new node and fill the fields. |
| 2495 | */ |
| 2496 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2497 | if (cur == NULL) { |
| 2498 | xmlGenericError(xmlGenericErrorContext, |
| 2499 | "xmlNewCDataBlock : malloc failed\n"); |
| 2500 | return(NULL); |
| 2501 | } |
| 2502 | memset(cur, 0, sizeof(xmlNode)); |
| 2503 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2504 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2505 | |
| 2506 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2507 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2508 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2509 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2510 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2511 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2512 | return(cur); |
| 2513 | } |
| 2514 | |
| 2515 | /** |
| 2516 | * xmlNewDocComment: |
| 2517 | * @doc: the document |
| 2518 | * @content: the comment content |
| 2519 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2520 | * Creation of a new node containing a comment within a document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2521 | * Returns a pointer to the new node object. |
| 2522 | */ |
| 2523 | xmlNodePtr |
| 2524 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 2525 | xmlNodePtr cur; |
| 2526 | |
| 2527 | cur = xmlNewComment(content); |
| 2528 | if (cur != NULL) cur->doc = doc; |
| 2529 | return(cur); |
| 2530 | } |
| 2531 | |
| 2532 | /** |
| 2533 | * xmlSetTreeDoc: |
| 2534 | * @tree: the top element |
| 2535 | * @doc: the document |
| 2536 | * |
| 2537 | * update all nodes under the tree to point to the right document |
| 2538 | */ |
| 2539 | void |
| 2540 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2541 | xmlAttrPtr prop; |
| 2542 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2543 | if (tree == NULL) |
| 2544 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2545 | if (tree->doc != doc) { |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2546 | if(tree->type == XML_ELEMENT_NODE) { |
| 2547 | prop = tree->properties; |
| 2548 | while (prop != NULL) { |
| 2549 | prop->doc = doc; |
| 2550 | xmlSetListDoc(prop->children, doc); |
| 2551 | prop = prop->next; |
| 2552 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2553 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2554 | if (tree->children != NULL) |
| 2555 | xmlSetListDoc(tree->children, doc); |
| 2556 | tree->doc = doc; |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | /** |
| 2561 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2562 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2563 | * @doc: the document |
| 2564 | * |
| 2565 | * update all nodes in the list to point to the right document |
| 2566 | */ |
| 2567 | void |
| 2568 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2569 | xmlNodePtr cur; |
| 2570 | |
| 2571 | if (list == NULL) |
| 2572 | return; |
| 2573 | cur = list; |
| 2574 | while (cur != NULL) { |
| 2575 | if (cur->doc != doc) |
| 2576 | xmlSetTreeDoc(cur, doc); |
| 2577 | cur = cur->next; |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | |
| 2582 | /** |
| 2583 | * xmlNewChild: |
| 2584 | * @parent: the parent node |
| 2585 | * @ns: a namespace if any |
| 2586 | * @name: the name of the child |
| 2587 | * @content: the XML content of the child if any. |
| 2588 | * |
| 2589 | * 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] | 2590 | * @ns and @content parameters are optional (NULL). If content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2591 | * a child list containing the TEXTs and ENTITY_REFs node will be created. |
| 2592 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2593 | * references, but XML special chars need to be escaped first by using |
| 2594 | * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities |
| 2595 | * support is not needed. |
| 2596 | * |
| 2597 | * Returns a pointer to the new node object. |
| 2598 | */ |
| 2599 | xmlNodePtr |
| 2600 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2601 | const xmlChar *name, const xmlChar *content) { |
| 2602 | xmlNodePtr cur, prev; |
| 2603 | |
| 2604 | if (parent == NULL) { |
| 2605 | #ifdef DEBUG_TREE |
| 2606 | xmlGenericError(xmlGenericErrorContext, |
| 2607 | "xmlNewChild : parent == NULL\n"); |
| 2608 | #endif |
| 2609 | return(NULL); |
| 2610 | } |
| 2611 | |
| 2612 | if (name == NULL) { |
| 2613 | #ifdef DEBUG_TREE |
| 2614 | xmlGenericError(xmlGenericErrorContext, |
| 2615 | "xmlNewChild : name == NULL\n"); |
| 2616 | #endif |
| 2617 | return(NULL); |
| 2618 | } |
| 2619 | |
| 2620 | /* |
| 2621 | * Allocate a new node |
| 2622 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2623 | if (parent->type == XML_ELEMENT_NODE) { |
| 2624 | if (ns == NULL) |
| 2625 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2626 | else |
| 2627 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2628 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2629 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2630 | if (ns == NULL) |
| 2631 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2632 | else |
| 2633 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2634 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2635 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2636 | } else { |
| 2637 | return(NULL); |
| 2638 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2639 | if (cur == NULL) return(NULL); |
| 2640 | |
| 2641 | /* |
| 2642 | * add the new element at the end of the children list. |
| 2643 | */ |
| 2644 | cur->type = XML_ELEMENT_NODE; |
| 2645 | cur->parent = parent; |
| 2646 | cur->doc = parent->doc; |
| 2647 | if (parent->children == NULL) { |
| 2648 | parent->children = cur; |
| 2649 | parent->last = cur; |
| 2650 | } else { |
| 2651 | prev = parent->last; |
| 2652 | prev->next = cur; |
| 2653 | cur->prev = prev; |
| 2654 | parent->last = cur; |
| 2655 | } |
| 2656 | |
| 2657 | return(cur); |
| 2658 | } |
| 2659 | |
| 2660 | /** |
| 2661 | * xmlAddNextSibling: |
| 2662 | * @cur: the child node |
| 2663 | * @elem: the new node |
| 2664 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2665 | * Add a new node @elem as the next sibling of @cur |
| 2666 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2667 | * first unlinked from its existing context. |
| 2668 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2669 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2670 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2671 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2672 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2673 | */ |
| 2674 | xmlNodePtr |
| 2675 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2676 | if (cur == NULL) { |
| 2677 | #ifdef DEBUG_TREE |
| 2678 | xmlGenericError(xmlGenericErrorContext, |
| 2679 | "xmlAddNextSibling : cur == NULL\n"); |
| 2680 | #endif |
| 2681 | return(NULL); |
| 2682 | } |
| 2683 | if (elem == NULL) { |
| 2684 | #ifdef DEBUG_TREE |
| 2685 | xmlGenericError(xmlGenericErrorContext, |
| 2686 | "xmlAddNextSibling : elem == NULL\n"); |
| 2687 | #endif |
| 2688 | return(NULL); |
| 2689 | } |
| 2690 | |
| 2691 | xmlUnlinkNode(elem); |
| 2692 | |
| 2693 | if (elem->type == XML_TEXT_NODE) { |
| 2694 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2695 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2696 | xmlFreeNode(elem); |
| 2697 | return(cur); |
| 2698 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2699 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 2700 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2701 | xmlChar *tmp; |
| 2702 | |
| 2703 | tmp = xmlStrdup(elem->content); |
| 2704 | tmp = xmlStrcat(tmp, cur->next->content); |
| 2705 | xmlNodeSetContent(cur->next, tmp); |
| 2706 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2707 | xmlFreeNode(elem); |
| 2708 | return(cur->next); |
| 2709 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2710 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2711 | /* check if an attribute with the same name exists */ |
| 2712 | xmlAttrPtr attr; |
| 2713 | |
| 2714 | if (elem->ns == NULL) |
| 2715 | attr = xmlHasProp(cur->parent, elem->name); |
| 2716 | else |
| 2717 | attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href); |
| 2718 | if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) { |
| 2719 | /* different instance, destroy it (attributes must be unique) */ |
| 2720 | xmlFreeProp(attr); |
| 2721 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | if (elem->doc != cur->doc) { |
| 2725 | xmlSetTreeDoc(elem, cur->doc); |
| 2726 | } |
| 2727 | elem->parent = cur->parent; |
| 2728 | elem->prev = cur; |
| 2729 | elem->next = cur->next; |
| 2730 | cur->next = elem; |
| 2731 | if (elem->next != NULL) |
| 2732 | elem->next->prev = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2733 | 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] | 2734 | elem->parent->last = elem; |
| 2735 | return(elem); |
| 2736 | } |
| 2737 | |
| 2738 | /** |
| 2739 | * xmlAddPrevSibling: |
| 2740 | * @cur: the child node |
| 2741 | * @elem: the new node |
| 2742 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2743 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2744 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2745 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2746 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2747 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2748 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2749 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2750 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2751 | */ |
| 2752 | xmlNodePtr |
| 2753 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2754 | if (cur == NULL) { |
| 2755 | #ifdef DEBUG_TREE |
| 2756 | xmlGenericError(xmlGenericErrorContext, |
| 2757 | "xmlAddPrevSibling : cur == NULL\n"); |
| 2758 | #endif |
| 2759 | return(NULL); |
| 2760 | } |
| 2761 | if (elem == NULL) { |
| 2762 | #ifdef DEBUG_TREE |
| 2763 | xmlGenericError(xmlGenericErrorContext, |
| 2764 | "xmlAddPrevSibling : elem == NULL\n"); |
| 2765 | #endif |
| 2766 | return(NULL); |
| 2767 | } |
| 2768 | |
| 2769 | xmlUnlinkNode(elem); |
| 2770 | |
| 2771 | if (elem->type == XML_TEXT_NODE) { |
| 2772 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2773 | xmlChar *tmp; |
| 2774 | |
| 2775 | tmp = xmlStrdup(elem->content); |
| 2776 | tmp = xmlStrcat(tmp, cur->content); |
| 2777 | xmlNodeSetContent(cur, tmp); |
| 2778 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2779 | xmlFreeNode(elem); |
| 2780 | return(cur); |
| 2781 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2782 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 2783 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2784 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2785 | xmlFreeNode(elem); |
| 2786 | return(cur->prev); |
| 2787 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2788 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2789 | /* check if an attribute with the same name exists */ |
| 2790 | xmlAttrPtr attr; |
| 2791 | |
| 2792 | if (elem->ns == NULL) |
| 2793 | attr = xmlHasProp(cur->parent, elem->name); |
| 2794 | else |
| 2795 | attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href); |
| 2796 | if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) { |
| 2797 | /* different instance, destroy it (attributes must be unique) */ |
| 2798 | xmlFreeProp(attr); |
| 2799 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | if (elem->doc != cur->doc) { |
| 2803 | xmlSetTreeDoc(elem, cur->doc); |
| 2804 | } |
| 2805 | elem->parent = cur->parent; |
| 2806 | elem->next = cur; |
| 2807 | elem->prev = cur->prev; |
| 2808 | cur->prev = elem; |
| 2809 | if (elem->prev != NULL) |
| 2810 | elem->prev->next = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2811 | if (elem->parent != NULL) { |
| 2812 | if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2813 | if (elem->parent->properties == (xmlAttrPtr) cur) { |
| 2814 | elem->parent->properties = (xmlAttrPtr) elem; |
| 2815 | } |
| 2816 | } else { |
| 2817 | if (elem->parent->children == cur) { |
| 2818 | elem->parent->children = elem; |
| 2819 | } |
| 2820 | } |
| 2821 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2822 | return(elem); |
| 2823 | } |
| 2824 | |
| 2825 | /** |
| 2826 | * xmlAddSibling: |
| 2827 | * @cur: the child node |
| 2828 | * @elem: the new node |
| 2829 | * |
| 2830 | * Add a new element @elem to the list of siblings of @cur |
| 2831 | * merging adjacent TEXT nodes (@elem may be freed) |
| 2832 | * If the new element was already inserted in a document it is |
| 2833 | * first unlinked from its existing context. |
| 2834 | * |
| 2835 | * Returns the new element or NULL in case of error. |
| 2836 | */ |
| 2837 | xmlNodePtr |
| 2838 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2839 | xmlNodePtr parent; |
| 2840 | |
| 2841 | if (cur == NULL) { |
| 2842 | #ifdef DEBUG_TREE |
| 2843 | xmlGenericError(xmlGenericErrorContext, |
| 2844 | "xmlAddSibling : cur == NULL\n"); |
| 2845 | #endif |
| 2846 | return(NULL); |
| 2847 | } |
| 2848 | |
| 2849 | if (elem == NULL) { |
| 2850 | #ifdef DEBUG_TREE |
| 2851 | xmlGenericError(xmlGenericErrorContext, |
| 2852 | "xmlAddSibling : elem == NULL\n"); |
| 2853 | #endif |
| 2854 | return(NULL); |
| 2855 | } |
| 2856 | |
| 2857 | /* |
| 2858 | * Constant time is we can rely on the ->parent->last to find |
| 2859 | * the last sibling. |
| 2860 | */ |
| 2861 | if ((cur->parent != NULL) && |
| 2862 | (cur->parent->children != NULL) && |
| 2863 | (cur->parent->last != NULL) && |
| 2864 | (cur->parent->last->next == NULL)) { |
| 2865 | cur = cur->parent->last; |
| 2866 | } else { |
| 2867 | while (cur->next != NULL) cur = cur->next; |
| 2868 | } |
| 2869 | |
| 2870 | xmlUnlinkNode(elem); |
| 2871 | |
| 2872 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2873 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2874 | xmlFreeNode(elem); |
| 2875 | return(cur); |
| 2876 | } |
| 2877 | |
| 2878 | if (elem->doc != cur->doc) { |
| 2879 | xmlSetTreeDoc(elem, cur->doc); |
| 2880 | } |
| 2881 | parent = cur->parent; |
| 2882 | elem->prev = cur; |
| 2883 | elem->next = NULL; |
| 2884 | elem->parent = parent; |
| 2885 | cur->next = elem; |
| 2886 | if (parent != NULL) |
| 2887 | parent->last = elem; |
| 2888 | |
| 2889 | return(elem); |
| 2890 | } |
| 2891 | |
| 2892 | /** |
| 2893 | * xmlAddChildList: |
| 2894 | * @parent: the parent node |
| 2895 | * @cur: the first node in the list |
| 2896 | * |
| 2897 | * Add a list of node at the end of the child list of the parent |
| 2898 | * merging adjacent TEXT nodes (@cur may be freed) |
| 2899 | * |
| 2900 | * Returns the last child or NULL in case of error. |
| 2901 | */ |
| 2902 | xmlNodePtr |
| 2903 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 2904 | xmlNodePtr prev; |
| 2905 | |
| 2906 | if (parent == NULL) { |
| 2907 | #ifdef DEBUG_TREE |
| 2908 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2909 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2910 | #endif |
| 2911 | return(NULL); |
| 2912 | } |
| 2913 | |
| 2914 | if (cur == NULL) { |
| 2915 | #ifdef DEBUG_TREE |
| 2916 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2917 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2918 | #endif |
| 2919 | return(NULL); |
| 2920 | } |
| 2921 | |
| 2922 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 2923 | (cur->doc != parent->doc)) { |
| 2924 | #ifdef DEBUG_TREE |
| 2925 | xmlGenericError(xmlGenericErrorContext, |
| 2926 | "Elements moved to a different document\n"); |
| 2927 | #endif |
| 2928 | } |
| 2929 | |
| 2930 | /* |
| 2931 | * add the first element at the end of the children list. |
| 2932 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2933 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2934 | if (parent->children == NULL) { |
| 2935 | parent->children = cur; |
| 2936 | } else { |
| 2937 | /* |
| 2938 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 2939 | */ |
| 2940 | if ((cur->type == XML_TEXT_NODE) && |
| 2941 | (parent->last->type == XML_TEXT_NODE) && |
| 2942 | (cur->name == parent->last->name)) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2943 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2944 | /* |
| 2945 | * if it's the only child, nothing more to be done. |
| 2946 | */ |
| 2947 | if (cur->next == NULL) { |
| 2948 | xmlFreeNode(cur); |
| 2949 | return(parent->last); |
| 2950 | } |
| 2951 | prev = cur; |
| 2952 | cur = cur->next; |
| 2953 | xmlFreeNode(prev); |
| 2954 | } |
| 2955 | prev = parent->last; |
| 2956 | prev->next = cur; |
| 2957 | cur->prev = prev; |
| 2958 | } |
| 2959 | while (cur->next != NULL) { |
| 2960 | cur->parent = parent; |
| 2961 | if (cur->doc != parent->doc) { |
| 2962 | xmlSetTreeDoc(cur, parent->doc); |
| 2963 | } |
| 2964 | cur = cur->next; |
| 2965 | } |
| 2966 | cur->parent = parent; |
| 2967 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 2968 | parent->last = cur; |
| 2969 | |
| 2970 | return(cur); |
| 2971 | } |
| 2972 | |
| 2973 | /** |
| 2974 | * xmlAddChild: |
| 2975 | * @parent: the parent node |
| 2976 | * @cur: the child node |
| 2977 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2978 | * 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] | 2979 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2980 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2981 | * If there is an attribute with equal name, it is first destroyed. |
| 2982 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2983 | * Returns the child or NULL in case of error. |
| 2984 | */ |
| 2985 | xmlNodePtr |
| 2986 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 2987 | xmlNodePtr prev; |
| 2988 | |
| 2989 | if (parent == NULL) { |
| 2990 | #ifdef DEBUG_TREE |
| 2991 | xmlGenericError(xmlGenericErrorContext, |
| 2992 | "xmlAddChild : parent == NULL\n"); |
| 2993 | #endif |
| 2994 | return(NULL); |
| 2995 | } |
| 2996 | |
| 2997 | if (cur == NULL) { |
| 2998 | #ifdef DEBUG_TREE |
| 2999 | xmlGenericError(xmlGenericErrorContext, |
| 3000 | "xmlAddChild : child == NULL\n"); |
| 3001 | #endif |
| 3002 | return(NULL); |
| 3003 | } |
| 3004 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3005 | /* |
| 3006 | * 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] | 3007 | * cur is then freed. |
| 3008 | */ |
| 3009 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3010 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3011 | (parent->content != NULL) && |
| 3012 | (parent != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3013 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3014 | xmlFreeNode(cur); |
| 3015 | return(parent); |
| 3016 | } |
| 3017 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3018 | (parent->last->name == cur->name) && |
| 3019 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3020 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3021 | xmlFreeNode(cur); |
| 3022 | return(parent->last); |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | /* |
| 3027 | * add the new element at the end of the children list. |
| 3028 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3029 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3030 | cur->parent = parent; |
| 3031 | if (cur->doc != parent->doc) { |
| 3032 | xmlSetTreeDoc(cur, parent->doc); |
| 3033 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3034 | /* this check prevents a loop on tree-traversions if a developer |
| 3035 | * tries to add a node to its parent multiple times |
| 3036 | */ |
| 3037 | if (prev == parent) |
| 3038 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3039 | |
| 3040 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3041 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3042 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3043 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3044 | (parent->content != NULL) && |
| 3045 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3046 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3047 | xmlFreeNode(cur); |
| 3048 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3049 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3050 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3051 | if (parent->properties == NULL) { |
| 3052 | parent->properties = (xmlAttrPtr) cur; |
| 3053 | } else { |
| 3054 | /* check if an attribute with the same name exists */ |
| 3055 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3056 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3057 | if (cur->ns == NULL) |
| 3058 | lastattr = xmlHasProp(parent, cur->name); |
| 3059 | else |
| 3060 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
| 3061 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur)) { |
| 3062 | /* different instance, destroy it (attributes must be unique) */ |
| 3063 | xmlFreeProp(lastattr); |
| 3064 | } |
| 3065 | /* find the end */ |
| 3066 | lastattr = parent->properties; |
| 3067 | while (lastattr->next != NULL) { |
| 3068 | lastattr = lastattr->next; |
| 3069 | } |
| 3070 | lastattr->next = (xmlAttrPtr) cur; |
| 3071 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3072 | } |
| 3073 | } else { |
| 3074 | if (parent->children == NULL) { |
| 3075 | parent->children = cur; |
| 3076 | parent->last = cur; |
| 3077 | } else { |
| 3078 | prev = parent->last; |
| 3079 | prev->next = cur; |
| 3080 | cur->prev = prev; |
| 3081 | parent->last = cur; |
| 3082 | } |
| 3083 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3084 | return(cur); |
| 3085 | } |
| 3086 | |
| 3087 | /** |
| 3088 | * xmlGetLastChild: |
| 3089 | * @parent: the parent node |
| 3090 | * |
| 3091 | * Search the last child of a node. |
| 3092 | * Returns the last child or NULL if none. |
| 3093 | */ |
| 3094 | xmlNodePtr |
| 3095 | xmlGetLastChild(xmlNodePtr parent) { |
| 3096 | if (parent == NULL) { |
| 3097 | #ifdef DEBUG_TREE |
| 3098 | xmlGenericError(xmlGenericErrorContext, |
| 3099 | "xmlGetLastChild : parent == NULL\n"); |
| 3100 | #endif |
| 3101 | return(NULL); |
| 3102 | } |
| 3103 | return(parent->last); |
| 3104 | } |
| 3105 | |
| 3106 | /** |
| 3107 | * xmlFreeNodeList: |
| 3108 | * @cur: the first node in the list |
| 3109 | * |
| 3110 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3111 | * the children are freed too. |
| 3112 | */ |
| 3113 | void |
| 3114 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3115 | xmlNodePtr next; |
| 3116 | if (cur == NULL) { |
| 3117 | #ifdef DEBUG_TREE |
| 3118 | xmlGenericError(xmlGenericErrorContext, |
| 3119 | "xmlFreeNodeList : node == NULL\n"); |
| 3120 | #endif |
| 3121 | return; |
| 3122 | } |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3123 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3124 | xmlFreeNsList((xmlNsPtr) cur); |
| 3125 | return; |
| 3126 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3127 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3128 | #ifdef LIBXML_DOCB_ENABLED |
| 3129 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3130 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3131 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3132 | xmlFreeDoc((xmlDocPtr) cur); |
| 3133 | return; |
| 3134 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3135 | while (cur != NULL) { |
| 3136 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3137 | /* unroll to speed up freeing the document */ |
| 3138 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3139 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3140 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3141 | xmlDeregisterNodeDefaultValue(cur); |
| 3142 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3143 | if ((cur->children != NULL) && |
| 3144 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3145 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3146 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3147 | (cur->type == XML_XINCLUDE_START) || |
| 3148 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3149 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3150 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3151 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3152 | (cur->type != XML_XINCLUDE_START) && |
| 3153 | (cur->type != XML_XINCLUDE_END) && |
| 3154 | (cur->type != XML_ENTITY_REF_NODE)) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3155 | if (cur->content != NULL) xmlFree(cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3156 | } |
| 3157 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3158 | (cur->type == XML_XINCLUDE_START) || |
| 3159 | (cur->type == XML_XINCLUDE_END)) && |
| 3160 | (cur->nsDef != NULL)) |
| 3161 | xmlFreeNsList(cur->nsDef); |
| 3162 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3163 | /* |
| 3164 | * When a node is a text node or a comment, it uses a global static |
| 3165 | * variable for the name of the node. |
| 3166 | * |
| 3167 | * The xmlStrEqual comparisons need to be done when (happened with |
| 3168 | * XML::libXML and XML::libXSLT) the library is included twice |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3169 | * statically in the binary and a tree allocated by one occurrence |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3170 | * of the lib gets freed by the other occurrence, in this case |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3171 | * the string addresses compare are not sufficient. |
| 3172 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3173 | if ((cur->name != NULL) && |
| 3174 | (cur->name != xmlStringText) && |
| 3175 | (cur->name != xmlStringTextNoenc) && |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3176 | (cur->name != xmlStringComment)) { |
| 3177 | if (cur->type == XML_TEXT_NODE) { |
| 3178 | if ((!xmlStrEqual(cur->name, xmlStringText)) && |
| 3179 | (!xmlStrEqual(cur->name, xmlStringTextNoenc))) |
| 3180 | xmlFree((char *) cur->name); |
| 3181 | } else if (cur->type == XML_COMMENT_NODE) { |
| 3182 | if (!xmlStrEqual(cur->name, xmlStringComment)) |
| 3183 | xmlFree((char *) cur->name); |
| 3184 | } else |
| 3185 | xmlFree((char *) cur->name); |
| 3186 | } |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3187 | /* TODO : derecursivate this function */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3188 | xmlFree(cur); |
| 3189 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3190 | cur = next; |
| 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | /** |
| 3195 | * xmlFreeNode: |
| 3196 | * @cur: the node |
| 3197 | * |
| 3198 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3199 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3200 | */ |
| 3201 | void |
| 3202 | xmlFreeNode(xmlNodePtr cur) { |
| 3203 | if (cur == NULL) { |
| 3204 | #ifdef DEBUG_TREE |
| 3205 | xmlGenericError(xmlGenericErrorContext, |
| 3206 | "xmlFreeNode : node == NULL\n"); |
| 3207 | #endif |
| 3208 | return; |
| 3209 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3210 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3211 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3212 | if (cur->type == XML_DTD_NODE) { |
| 3213 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3214 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3215 | } |
| 3216 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3217 | xmlFreeNs((xmlNsPtr) cur); |
| 3218 | return; |
| 3219 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3220 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3221 | xmlFreeProp((xmlAttrPtr) cur); |
| 3222 | return; |
| 3223 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3224 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3225 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3226 | xmlDeregisterNodeDefaultValue(cur); |
| 3227 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3228 | if ((cur->children != NULL) && |
| 3229 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3230 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3231 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3232 | (cur->type == XML_XINCLUDE_START) || |
| 3233 | (cur->type == XML_XINCLUDE_END)) && |
| 3234 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3235 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3236 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3237 | (cur->content != NULL) && |
| 3238 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3239 | (cur->type != XML_XINCLUDE_END) && |
| 3240 | (cur->type != XML_XINCLUDE_START)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3241 | xmlFree(cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3244 | /* |
| 3245 | * When a node is a text node or a comment, it uses a global static |
| 3246 | * variable for the name of the node. |
| 3247 | * |
| 3248 | * The xmlStrEqual comparisons need to be done when (happened with |
| 3249 | * XML::libXML and XML::libXSLT) the library is included twice statically |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3250 | * 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] | 3251 | * freed by the other occurrence, in this case the string addresses compare |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3252 | * are not sufficient. |
| 3253 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3254 | if ((cur->name != NULL) && |
| 3255 | (cur->name != xmlStringText) && |
| 3256 | (cur->name != xmlStringTextNoenc) && |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3257 | (cur->name != xmlStringComment)) { |
| 3258 | if (cur->type == XML_TEXT_NODE) { |
| 3259 | if ((!xmlStrEqual(cur->name, xmlStringText)) && |
| 3260 | (!xmlStrEqual(cur->name, xmlStringTextNoenc))) |
| 3261 | xmlFree((char *) cur->name); |
| 3262 | } else if (cur->type == XML_COMMENT_NODE) { |
| 3263 | if (!xmlStrEqual(cur->name, xmlStringComment)) |
| 3264 | xmlFree((char *) cur->name); |
| 3265 | } else |
| 3266 | xmlFree((char *) cur->name); |
| 3267 | } |
| 3268 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3269 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3270 | (cur->type == XML_XINCLUDE_START) || |
| 3271 | (cur->type == XML_XINCLUDE_END)) && |
| 3272 | (cur->nsDef != NULL)) |
| 3273 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3274 | xmlFree(cur); |
| 3275 | } |
| 3276 | |
| 3277 | /** |
| 3278 | * xmlUnlinkNode: |
| 3279 | * @cur: the node |
| 3280 | * |
| 3281 | * Unlink a node from it's current context, the node is not freed |
| 3282 | */ |
| 3283 | void |
| 3284 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3285 | if (cur == NULL) { |
| 3286 | #ifdef DEBUG_TREE |
| 3287 | xmlGenericError(xmlGenericErrorContext, |
| 3288 | "xmlUnlinkNode : node == NULL\n"); |
| 3289 | #endif |
| 3290 | return; |
| 3291 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3292 | if (cur->type == XML_DTD_NODE) { |
| 3293 | xmlDocPtr doc; |
| 3294 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3295 | if (doc != NULL) { |
| 3296 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3297 | doc->intSubset = NULL; |
| 3298 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3299 | doc->extSubset = NULL; |
| 3300 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3301 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3302 | if (cur->parent != NULL) { |
| 3303 | xmlNodePtr parent; |
| 3304 | parent = cur->parent; |
| 3305 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3306 | if (parent->properties == (xmlAttrPtr) cur) |
| 3307 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3308 | } else { |
| 3309 | if (parent->children == cur) |
| 3310 | parent->children = cur->next; |
| 3311 | if (parent->last == cur) |
| 3312 | parent->last = cur->prev; |
| 3313 | } |
| 3314 | cur->parent = NULL; |
| 3315 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3316 | if (cur->next != NULL) |
| 3317 | cur->next->prev = cur->prev; |
| 3318 | if (cur->prev != NULL) |
| 3319 | cur->prev->next = cur->next; |
| 3320 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | /** |
| 3324 | * xmlReplaceNode: |
| 3325 | * @old: the old node |
| 3326 | * @cur: the node |
| 3327 | * |
| 3328 | * 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] | 3329 | * 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] | 3330 | * first unlinked from its existing context. |
| 3331 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3332 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3333 | */ |
| 3334 | xmlNodePtr |
| 3335 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
| 3336 | if (old == NULL) { |
| 3337 | #ifdef DEBUG_TREE |
| 3338 | xmlGenericError(xmlGenericErrorContext, |
| 3339 | "xmlReplaceNode : old == NULL\n"); |
| 3340 | #endif |
| 3341 | return(NULL); |
| 3342 | } |
| 3343 | if (cur == NULL) { |
| 3344 | xmlUnlinkNode(old); |
| 3345 | return(old); |
| 3346 | } |
| 3347 | if (cur == old) { |
| 3348 | return(old); |
| 3349 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3350 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3351 | #ifdef DEBUG_TREE |
| 3352 | xmlGenericError(xmlGenericErrorContext, |
| 3353 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3354 | #endif |
| 3355 | return(old); |
| 3356 | } |
| 3357 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3358 | #ifdef DEBUG_TREE |
| 3359 | xmlGenericError(xmlGenericErrorContext, |
| 3360 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3361 | #endif |
| 3362 | return(old); |
| 3363 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3364 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3365 | #ifdef DEBUG_TREE |
| 3366 | xmlGenericError(xmlGenericErrorContext, |
| 3367 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3368 | #endif |
| 3369 | return(old); |
| 3370 | } |
| 3371 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3372 | #ifdef DEBUG_TREE |
| 3373 | xmlGenericError(xmlGenericErrorContext, |
| 3374 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3375 | #endif |
| 3376 | return(old); |
| 3377 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3378 | xmlUnlinkNode(cur); |
| 3379 | cur->doc = old->doc; |
| 3380 | cur->parent = old->parent; |
| 3381 | cur->next = old->next; |
| 3382 | if (cur->next != NULL) |
| 3383 | cur->next->prev = cur; |
| 3384 | cur->prev = old->prev; |
| 3385 | if (cur->prev != NULL) |
| 3386 | cur->prev->next = cur; |
| 3387 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3388 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3389 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3390 | cur->parent->properties = ((xmlAttrPtr) cur); |
| 3391 | } else { |
| 3392 | if (cur->parent->children == old) |
| 3393 | cur->parent->children = cur; |
| 3394 | if (cur->parent->last == old) |
| 3395 | cur->parent->last = cur; |
| 3396 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3397 | } |
| 3398 | old->next = old->prev = NULL; |
| 3399 | old->parent = NULL; |
| 3400 | return(old); |
| 3401 | } |
| 3402 | |
| 3403 | /************************************************************************ |
| 3404 | * * |
| 3405 | * Copy operations * |
| 3406 | * * |
| 3407 | ************************************************************************/ |
| 3408 | |
| 3409 | /** |
| 3410 | * xmlCopyNamespace: |
| 3411 | * @cur: the namespace |
| 3412 | * |
| 3413 | * Do a copy of the namespace. |
| 3414 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3415 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3416 | */ |
| 3417 | xmlNsPtr |
| 3418 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3419 | xmlNsPtr ret; |
| 3420 | |
| 3421 | if (cur == NULL) return(NULL); |
| 3422 | switch (cur->type) { |
| 3423 | case XML_LOCAL_NAMESPACE: |
| 3424 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3425 | break; |
| 3426 | default: |
| 3427 | #ifdef DEBUG_TREE |
| 3428 | xmlGenericError(xmlGenericErrorContext, |
| 3429 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3430 | #endif |
| 3431 | return(NULL); |
| 3432 | } |
| 3433 | return(ret); |
| 3434 | } |
| 3435 | |
| 3436 | /** |
| 3437 | * xmlCopyNamespaceList: |
| 3438 | * @cur: the first namespace |
| 3439 | * |
| 3440 | * Do a copy of an namespace list. |
| 3441 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3442 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3443 | */ |
| 3444 | xmlNsPtr |
| 3445 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3446 | xmlNsPtr ret = NULL; |
| 3447 | xmlNsPtr p = NULL,q; |
| 3448 | |
| 3449 | while (cur != NULL) { |
| 3450 | q = xmlCopyNamespace(cur); |
| 3451 | if (p == NULL) { |
| 3452 | ret = p = q; |
| 3453 | } else { |
| 3454 | p->next = q; |
| 3455 | p = q; |
| 3456 | } |
| 3457 | cur = cur->next; |
| 3458 | } |
| 3459 | return(ret); |
| 3460 | } |
| 3461 | |
| 3462 | static xmlNodePtr |
| 3463 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 3464 | /** |
| 3465 | * xmlCopyProp: |
| 3466 | * @target: the element where the attribute will be grafted |
| 3467 | * @cur: the attribute |
| 3468 | * |
| 3469 | * Do a copy of the attribute. |
| 3470 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3471 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3472 | */ |
| 3473 | xmlAttrPtr |
| 3474 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 3475 | xmlAttrPtr ret; |
| 3476 | |
| 3477 | if (cur == NULL) return(NULL); |
| 3478 | if (target != NULL) |
| 3479 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
| 3480 | else if (cur->parent != NULL) |
| 3481 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 3482 | else if (cur->children != NULL) |
| 3483 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 3484 | else |
| 3485 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 3486 | if (ret == NULL) return(NULL); |
| 3487 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3488 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3489 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3490 | xmlNsPtr ns; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3491 | /* |
| 3492 | * if (target->doc) |
| 3493 | * ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3494 | * else if (cur->doc) / * target may not yet have a doc : KPI * / |
| 3495 | * ns = xmlSearchNs(cur->doc, target, cur->ns->prefix); |
| 3496 | * else |
| 3497 | * ns = NULL; |
| 3498 | * ret->ns = ns; |
| 3499 | */ |
| 3500 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3501 | if (ns == NULL) { |
| 3502 | /* |
| 3503 | * Humm, we are copying an element whose namespace is defined |
| 3504 | * out of the new tree scope. Search it in the original tree |
| 3505 | * and add it at the top of the new tree |
| 3506 | */ |
| 3507 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 3508 | if (ns != NULL) { |
| 3509 | xmlNodePtr root = target; |
| 3510 | xmlNodePtr pred = NULL; |
| 3511 | |
| 3512 | while (root->parent != NULL) { |
| 3513 | pred = root; |
| 3514 | root = root->parent; |
| 3515 | } |
| 3516 | if (root == (xmlNodePtr) target->doc) { |
| 3517 | /* correct possibly cycling above the document elt */ |
| 3518 | root = pred; |
| 3519 | } |
| 3520 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 3521 | } |
| 3522 | } else { |
| 3523 | /* |
| 3524 | * we have to find something appropriate here since |
| 3525 | * we cant be sure, that the namespce we found is identified |
| 3526 | * by the prefix |
| 3527 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3528 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3529 | /* this is the nice case */ |
| 3530 | ret->ns = ns; |
| 3531 | } else { |
| 3532 | /* |
| 3533 | * we are in trouble: we need a new reconcilied namespace. |
| 3534 | * This is expensive |
| 3535 | */ |
| 3536 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 3537 | } |
| 3538 | } |
| 3539 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3540 | } else |
| 3541 | ret->ns = NULL; |
| 3542 | |
| 3543 | if (cur->children != NULL) { |
| 3544 | xmlNodePtr tmp; |
| 3545 | |
| 3546 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 3547 | ret->last = NULL; |
| 3548 | tmp = ret->children; |
| 3549 | while (tmp != NULL) { |
| 3550 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 3551 | if (tmp->next == NULL) |
| 3552 | ret->last = tmp; |
| 3553 | tmp = tmp->next; |
| 3554 | } |
| 3555 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3556 | /* |
| 3557 | * Try to handle IDs |
| 3558 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 3559 | if ((target!= NULL) && (cur!= NULL) && |
| 3560 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3561 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 3562 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 3563 | xmlChar *id; |
| 3564 | |
| 3565 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 3566 | if (id != NULL) { |
| 3567 | xmlAddID(NULL, target->doc, id, ret); |
| 3568 | xmlFree(id); |
| 3569 | } |
| 3570 | } |
| 3571 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3572 | return(ret); |
| 3573 | } |
| 3574 | |
| 3575 | /** |
| 3576 | * xmlCopyPropList: |
| 3577 | * @target: the element where the attributes will be grafted |
| 3578 | * @cur: the first attribute |
| 3579 | * |
| 3580 | * Do a copy of an attribute list. |
| 3581 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3582 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3583 | */ |
| 3584 | xmlAttrPtr |
| 3585 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 3586 | xmlAttrPtr ret = NULL; |
| 3587 | xmlAttrPtr p = NULL,q; |
| 3588 | |
| 3589 | while (cur != NULL) { |
| 3590 | q = xmlCopyProp(target, cur); |
| 3591 | if (p == NULL) { |
| 3592 | ret = p = q; |
| 3593 | } else { |
| 3594 | p->next = q; |
| 3595 | q->prev = p; |
| 3596 | p = q; |
| 3597 | } |
| 3598 | cur = cur->next; |
| 3599 | } |
| 3600 | return(ret); |
| 3601 | } |
| 3602 | |
| 3603 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3604 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3605 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3606 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3607 | * tricky reason: namespaces. Doing a direct copy of a node |
| 3608 | * say RPM:Copyright without changing the namespace pointer to |
| 3609 | * something else can produce stale links. One way to do it is |
| 3610 | * to keep a reference counter but this doesn't work as soon |
| 3611 | * as one move the element or the subtree out of the scope of |
| 3612 | * the existing namespace. The actual solution seems to add |
| 3613 | * a copy of the namespace at the top of the copied tree if |
| 3614 | * not available in the subtree. |
| 3615 | * Hence two functions, the public front-end call the inner ones |
| 3616 | */ |
| 3617 | |
| 3618 | static xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3619 | xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3620 | int recursive) { |
| 3621 | xmlNodePtr ret; |
| 3622 | |
| 3623 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3624 | switch (node->type) { |
| 3625 | case XML_TEXT_NODE: |
| 3626 | case XML_CDATA_SECTION_NODE: |
| 3627 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 3628 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3629 | case XML_ENTITY_REF_NODE: |
| 3630 | case XML_ENTITY_NODE: |
| 3631 | case XML_PI_NODE: |
| 3632 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3633 | case XML_XINCLUDE_START: |
| 3634 | case XML_XINCLUDE_END: |
| 3635 | break; |
| 3636 | case XML_ATTRIBUTE_NODE: |
| 3637 | return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node)); |
| 3638 | case XML_NAMESPACE_DECL: |
| 3639 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
| 3640 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3641 | case XML_DOCUMENT_NODE: |
| 3642 | case XML_HTML_DOCUMENT_NODE: |
| 3643 | #ifdef LIBXML_DOCB_ENABLED |
| 3644 | case XML_DOCB_DOCUMENT_NODE: |
| 3645 | #endif |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3646 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, recursive)); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3647 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3648 | case XML_NOTATION_NODE: |
| 3649 | case XML_DTD_NODE: |
| 3650 | case XML_ELEMENT_DECL: |
| 3651 | case XML_ATTRIBUTE_DECL: |
| 3652 | case XML_ENTITY_DECL: |
| 3653 | return(NULL); |
| 3654 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3655 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3656 | /* |
| 3657 | * Allocate a new node and fill the fields. |
| 3658 | */ |
| 3659 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 3660 | if (ret == NULL) { |
| 3661 | xmlGenericError(xmlGenericErrorContext, |
| 3662 | "xmlStaticCopyNode : malloc failed\n"); |
| 3663 | return(NULL); |
| 3664 | } |
| 3665 | memset(ret, 0, sizeof(xmlNode)); |
| 3666 | ret->type = node->type; |
| 3667 | |
| 3668 | ret->doc = doc; |
| 3669 | ret->parent = parent; |
| 3670 | if (node->name == xmlStringText) |
| 3671 | ret->name = xmlStringText; |
| 3672 | else if (node->name == xmlStringTextNoenc) |
| 3673 | ret->name = xmlStringTextNoenc; |
| 3674 | else if (node->name == xmlStringComment) |
| 3675 | ret->name = xmlStringComment; |
| 3676 | else if (node->name != NULL) |
| 3677 | ret->name = xmlStrdup(node->name); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3678 | if ((node->type != XML_ELEMENT_NODE) && |
| 3679 | (node->content != NULL) && |
| 3680 | (node->type != XML_ENTITY_REF_NODE) && |
| 3681 | (node->type != XML_XINCLUDE_END) && |
| 3682 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3683 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3684 | }else{ |
| 3685 | if (node->type == XML_ELEMENT_NODE) |
| 3686 | ret->content = (void*)(long) node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3687 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3688 | if (parent != NULL) { |
| 3689 | xmlNodePtr tmp; |
| 3690 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3691 | /* |
| 3692 | * this is a tricky part for the node register thing: |
| 3693 | * in case ret does get coalesced in xmlAddChild |
| 3694 | * the deregister-node callback is called; so we register ret now already |
| 3695 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3696 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3697 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 3698 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3699 | tmp = xmlAddChild(parent, ret); |
| 3700 | /* node could have coalesced */ |
| 3701 | if (tmp != ret) |
| 3702 | return(tmp); |
| 3703 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3704 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3705 | if (!recursive) |
| 3706 | goto out; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3707 | if (node->nsDef != NULL) |
| 3708 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 3709 | |
| 3710 | if (node->ns != NULL) { |
| 3711 | xmlNsPtr ns; |
| 3712 | |
| 3713 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 3714 | if (ns == NULL) { |
| 3715 | /* |
| 3716 | * Humm, we are copying an element whose namespace is defined |
| 3717 | * out of the new tree scope. Search it in the original tree |
| 3718 | * and add it at the top of the new tree |
| 3719 | */ |
| 3720 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 3721 | if (ns != NULL) { |
| 3722 | xmlNodePtr root = ret; |
| 3723 | |
| 3724 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 3725 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3726 | } |
| 3727 | } else { |
| 3728 | /* |
| 3729 | * reference the existing namespace definition in our own tree. |
| 3730 | */ |
| 3731 | ret->ns = ns; |
| 3732 | } |
| 3733 | } |
| 3734 | if (node->properties != NULL) |
| 3735 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3736 | if (node->type == XML_ENTITY_REF_NODE) { |
| 3737 | if ((doc == NULL) || (node->doc != doc)) { |
| 3738 | /* |
| 3739 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3740 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3741 | * we cannot keep the reference. Try to find it in the |
| 3742 | * target document. |
| 3743 | */ |
| 3744 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 3745 | } else { |
| 3746 | ret->children = node->children; |
| 3747 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3748 | ret->last = ret->children; |
| 3749 | } else if (node->children != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3750 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3751 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 3752 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3753 | |
| 3754 | out: |
| 3755 | /* if parent != NULL we already registered the node above */ |
| 3756 | if (parent == NULL && xmlRegisterNodeDefaultValue) |
| 3757 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3758 | return(ret); |
| 3759 | } |
| 3760 | |
| 3761 | static xmlNodePtr |
| 3762 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 3763 | xmlNodePtr ret = NULL; |
| 3764 | xmlNodePtr p = NULL,q; |
| 3765 | |
| 3766 | while (node != NULL) { |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3767 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 3768 | if (doc == NULL) { |
| 3769 | node = node->next; |
| 3770 | continue; |
| 3771 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3772 | if (doc->intSubset == NULL) { |
| 3773 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
| 3774 | q->doc = doc; |
| 3775 | q->parent = parent; |
| 3776 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3777 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3778 | } else { |
| 3779 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3780 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3781 | } |
| 3782 | } else |
| 3783 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3784 | if (ret == NULL) { |
| 3785 | q->prev = NULL; |
| 3786 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3787 | } else if (p != q) { |
| 3788 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3789 | p->next = q; |
| 3790 | q->prev = p; |
| 3791 | p = q; |
| 3792 | } |
| 3793 | node = node->next; |
| 3794 | } |
| 3795 | return(ret); |
| 3796 | } |
| 3797 | |
| 3798 | /** |
| 3799 | * xmlCopyNode: |
| 3800 | * @node: the node |
| 3801 | * @recursive: if 1 do a recursive copy. |
| 3802 | * |
| 3803 | * Do a copy of the node. |
| 3804 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3805 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3806 | */ |
| 3807 | xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3808 | xmlCopyNode(const xmlNodePtr node, int recursive) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3809 | xmlNodePtr ret; |
| 3810 | |
| 3811 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 3812 | return(ret); |
| 3813 | } |
| 3814 | |
| 3815 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3816 | * xmlDocCopyNode: |
| 3817 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3818 | * @doc: the document |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3819 | * @recursive: if 1 do a recursive copy. |
| 3820 | * |
| 3821 | * Do a copy of the node to a given document. |
| 3822 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3823 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3824 | */ |
| 3825 | xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3826 | xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int recursive) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3827 | xmlNodePtr ret; |
| 3828 | |
| 3829 | ret = xmlStaticCopyNode(node, doc, NULL, recursive); |
| 3830 | return(ret); |
| 3831 | } |
| 3832 | |
| 3833 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3834 | * xmlCopyNodeList: |
| 3835 | * @node: the first node in the list. |
| 3836 | * |
| 3837 | * Do a recursive copy of the node list. |
| 3838 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3839 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3840 | */ |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3841 | xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3842 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 3843 | return(ret); |
| 3844 | } |
| 3845 | |
| 3846 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3847 | * xmlCopyDtd: |
| 3848 | * @dtd: the dtd |
| 3849 | * |
| 3850 | * Do a copy of the dtd. |
| 3851 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3852 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3853 | */ |
| 3854 | xmlDtdPtr |
| 3855 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 3856 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3857 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3858 | |
| 3859 | if (dtd == NULL) return(NULL); |
| 3860 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 3861 | if (ret == NULL) return(NULL); |
| 3862 | if (dtd->entities != NULL) |
| 3863 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 3864 | (xmlEntitiesTablePtr) dtd->entities); |
| 3865 | if (dtd->notations != NULL) |
| 3866 | ret->notations = (void *) xmlCopyNotationTable( |
| 3867 | (xmlNotationTablePtr) dtd->notations); |
| 3868 | if (dtd->elements != NULL) |
| 3869 | ret->elements = (void *) xmlCopyElementTable( |
| 3870 | (xmlElementTablePtr) dtd->elements); |
| 3871 | if (dtd->attributes != NULL) |
| 3872 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 3873 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3874 | if (dtd->pentities != NULL) |
| 3875 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 3876 | (xmlEntitiesTablePtr) dtd->pentities); |
| 3877 | |
| 3878 | cur = dtd->children; |
| 3879 | while (cur != NULL) { |
| 3880 | q = NULL; |
| 3881 | |
| 3882 | if (cur->type == XML_ENTITY_DECL) { |
| 3883 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 3884 | switch (tmp->etype) { |
| 3885 | case XML_INTERNAL_GENERAL_ENTITY: |
| 3886 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 3887 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 3888 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 3889 | break; |
| 3890 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 3891 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 3892 | q = (xmlNodePtr) |
| 3893 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 3894 | break; |
| 3895 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 3896 | break; |
| 3897 | } |
| 3898 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 3899 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 3900 | q = (xmlNodePtr) |
| 3901 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 3902 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 3903 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
| 3904 | q = (xmlNodePtr) |
| 3905 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 3906 | } else if (cur->type == XML_COMMENT_NODE) { |
| 3907 | q = xmlCopyNode(cur, 0); |
| 3908 | } |
| 3909 | |
| 3910 | if (q == NULL) { |
| 3911 | cur = cur->next; |
| 3912 | continue; |
| 3913 | } |
| 3914 | |
| 3915 | if (p == NULL) |
| 3916 | ret->children = q; |
| 3917 | else |
| 3918 | p->next = q; |
| 3919 | |
| 3920 | q->prev = p; |
| 3921 | q->parent = (xmlNodePtr) ret; |
| 3922 | q->next = NULL; |
| 3923 | ret->last = q; |
| 3924 | p = q; |
| 3925 | cur = cur->next; |
| 3926 | } |
| 3927 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3928 | return(ret); |
| 3929 | } |
| 3930 | |
| 3931 | /** |
| 3932 | * xmlCopyDoc: |
| 3933 | * @doc: the document |
| 3934 | * @recursive: if 1 do a recursive copy. |
| 3935 | * |
| 3936 | * 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] | 3937 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3938 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3939 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3940 | */ |
| 3941 | xmlDocPtr |
| 3942 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 3943 | xmlDocPtr ret; |
| 3944 | |
| 3945 | if (doc == NULL) return(NULL); |
| 3946 | ret = xmlNewDoc(doc->version); |
| 3947 | if (ret == NULL) return(NULL); |
| 3948 | if (doc->name != NULL) |
| 3949 | ret->name = xmlMemStrdup(doc->name); |
| 3950 | if (doc->encoding != NULL) |
| 3951 | ret->encoding = xmlStrdup(doc->encoding); |
| 3952 | ret->charset = doc->charset; |
| 3953 | ret->compression = doc->compression; |
| 3954 | ret->standalone = doc->standalone; |
| 3955 | if (!recursive) return(ret); |
| 3956 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3957 | ret->last = NULL; |
| 3958 | ret->children = NULL; |
| 3959 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3960 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3961 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3962 | ret->intSubset->parent = ret; |
| 3963 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3964 | if (doc->oldNs != NULL) |
| 3965 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 3966 | if (doc->children != NULL) { |
| 3967 | xmlNodePtr tmp; |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3968 | |
| 3969 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 3970 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3971 | ret->last = NULL; |
| 3972 | tmp = ret->children; |
| 3973 | while (tmp != NULL) { |
| 3974 | if (tmp->next == NULL) |
| 3975 | ret->last = tmp; |
| 3976 | tmp = tmp->next; |
| 3977 | } |
| 3978 | } |
| 3979 | return(ret); |
| 3980 | } |
| 3981 | |
| 3982 | /************************************************************************ |
| 3983 | * * |
| 3984 | * Content access functions * |
| 3985 | * * |
| 3986 | ************************************************************************/ |
| 3987 | |
| 3988 | /** |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 3989 | * xmlGetLineNo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3990 | * @node: valid node |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 3991 | * |
| 3992 | * Get line number of node. this requires activation of this option |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3993 | * before invoking the parser by calling xmlLineNumbersDefault(1) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 3994 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3995 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 3996 | */ |
| 3997 | long |
| 3998 | xmlGetLineNo(xmlNodePtr node) |
| 3999 | { |
| 4000 | long result = -1; |
| 4001 | |
| 4002 | if (!node) |
| 4003 | return result; |
| 4004 | if (node->type == XML_ELEMENT_NODE) |
| 4005 | result = (long) node->content; |
| 4006 | else if ((node->prev != NULL) && |
| 4007 | ((node->prev->type == XML_ELEMENT_NODE) || |
| 4008 | (node->prev->type == XML_TEXT_NODE))) |
| 4009 | result = xmlGetLineNo(node->prev); |
| 4010 | else if ((node->parent != NULL) && |
| 4011 | ((node->parent->type == XML_ELEMENT_NODE) || |
| 4012 | (node->parent->type == XML_TEXT_NODE))) |
| 4013 | result = xmlGetLineNo(node->parent); |
| 4014 | |
| 4015 | return result; |
| 4016 | } |
| 4017 | |
| 4018 | /** |
| 4019 | * xmlGetNodePath: |
| 4020 | * @node: a node |
| 4021 | * |
| 4022 | * Build a structure based Path for the given node |
| 4023 | * |
| 4024 | * Returns the new path or NULL in case of error. The caller must free |
| 4025 | * the returned string |
| 4026 | */ |
| 4027 | xmlChar * |
| 4028 | xmlGetNodePath(xmlNodePtr node) |
| 4029 | { |
| 4030 | xmlNodePtr cur, tmp, next; |
| 4031 | xmlChar *buffer = NULL, *temp; |
| 4032 | size_t buf_len; |
| 4033 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4034 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4035 | const char *name; |
| 4036 | char nametemp[100]; |
| 4037 | int occur = 0; |
| 4038 | |
| 4039 | if (node == NULL) |
| 4040 | return (NULL); |
| 4041 | |
| 4042 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4043 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4044 | if (buffer == NULL) |
| 4045 | return (NULL); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4046 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4047 | if (buf == NULL) { |
| 4048 | xmlFree(buffer); |
| 4049 | return (NULL); |
| 4050 | } |
| 4051 | |
| 4052 | buffer[0] = 0; |
| 4053 | cur = node; |
| 4054 | do { |
| 4055 | name = ""; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4056 | sep = "?"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4057 | occur = 0; |
| 4058 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4059 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4060 | if (buffer[0] == '/') |
| 4061 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4062 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4063 | next = NULL; |
| 4064 | } else if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4065 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4066 | name = (const char *) cur->name; |
| 4067 | if (cur->ns) { |
| 4068 | snprintf(nametemp, sizeof(nametemp) - 1, |
| 4069 | "%s:%s", cur->ns->prefix, cur->name); |
| 4070 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4071 | name = nametemp; |
| 4072 | } |
| 4073 | next = cur->parent; |
| 4074 | |
| 4075 | /* |
| 4076 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4077 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4078 | */ |
| 4079 | tmp = cur->prev; |
| 4080 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4081 | if ((tmp->type == XML_ELEMENT_NODE) && |
| 4082 | (xmlStrEqual(cur->name, tmp->name))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4083 | occur++; |
| 4084 | tmp = tmp->prev; |
| 4085 | } |
| 4086 | if (occur == 0) { |
| 4087 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4088 | while (tmp != NULL && occur == 0) { |
| 4089 | if ((tmp->type == XML_ELEMENT_NODE) && |
| 4090 | (xmlStrEqual(cur->name, tmp->name))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4091 | occur++; |
| 4092 | tmp = tmp->next; |
| 4093 | } |
| 4094 | if (occur != 0) |
| 4095 | occur = 1; |
| 4096 | } else |
| 4097 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4098 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4099 | sep = "/"; |
| 4100 | name = "comment()"; |
| 4101 | next = cur->parent; |
| 4102 | |
| 4103 | /* |
| 4104 | * Thumbler index computation |
| 4105 | */ |
| 4106 | tmp = cur->prev; |
| 4107 | while (tmp != NULL) { |
| 4108 | if (tmp->type == XML_COMMENT_NODE) |
| 4109 | occur++; |
| 4110 | tmp = tmp->prev; |
| 4111 | } |
| 4112 | if (occur == 0) { |
| 4113 | tmp = cur->next; |
| 4114 | while (tmp != NULL && occur == 0) { |
| 4115 | if (tmp->type == XML_COMMENT_NODE) |
| 4116 | occur++; |
| 4117 | tmp = tmp->next; |
| 4118 | } |
| 4119 | if (occur != 0) |
| 4120 | occur = 1; |
| 4121 | } else |
| 4122 | occur++; |
| 4123 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4124 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4125 | sep = "/"; |
| 4126 | name = "text()"; |
| 4127 | next = cur->parent; |
| 4128 | |
| 4129 | /* |
| 4130 | * Thumbler index computation |
| 4131 | */ |
| 4132 | tmp = cur->prev; |
| 4133 | while (tmp != NULL) { |
| 4134 | if ((cur->type == XML_TEXT_NODE) || |
| 4135 | (cur->type == XML_CDATA_SECTION_NODE)) |
| 4136 | occur++; |
| 4137 | tmp = tmp->prev; |
| 4138 | } |
| 4139 | if (occur == 0) { |
| 4140 | tmp = cur->next; |
| 4141 | while (tmp != NULL && occur == 0) { |
| 4142 | if ((cur->type == XML_TEXT_NODE) || |
| 4143 | (cur->type == XML_CDATA_SECTION_NODE)) |
| 4144 | occur++; |
| 4145 | tmp = tmp->next; |
| 4146 | } |
| 4147 | if (occur != 0) |
| 4148 | occur = 1; |
| 4149 | } else |
| 4150 | occur++; |
| 4151 | } else if (cur->type == XML_PI_NODE) { |
| 4152 | sep = "/"; |
| 4153 | snprintf(nametemp, sizeof(nametemp) - 1, |
| 4154 | "processing-instruction('%s')", cur->name); |
| 4155 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4156 | name = nametemp; |
| 4157 | |
| 4158 | next = cur->parent; |
| 4159 | |
| 4160 | /* |
| 4161 | * Thumbler index computation |
| 4162 | */ |
| 4163 | tmp = cur->prev; |
| 4164 | while (tmp != NULL) { |
| 4165 | if ((tmp->type == XML_PI_NODE) && |
| 4166 | (xmlStrEqual(cur->name, tmp->name))) |
| 4167 | occur++; |
| 4168 | tmp = tmp->prev; |
| 4169 | } |
| 4170 | if (occur == 0) { |
| 4171 | tmp = cur->next; |
| 4172 | while (tmp != NULL && occur == 0) { |
| 4173 | if ((tmp->type == XML_PI_NODE) && |
| 4174 | (xmlStrEqual(cur->name, tmp->name))) |
| 4175 | occur++; |
| 4176 | tmp = tmp->next; |
| 4177 | } |
| 4178 | if (occur != 0) |
| 4179 | occur = 1; |
| 4180 | } else |
| 4181 | occur++; |
| 4182 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4183 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4184 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4185 | name = (const char *) (((xmlAttrPtr) cur)->name); |
| 4186 | next = ((xmlAttrPtr) cur)->parent; |
| 4187 | } else { |
| 4188 | next = cur->parent; |
| 4189 | } |
| 4190 | |
| 4191 | /* |
| 4192 | * Make sure there is enough room |
| 4193 | */ |
| 4194 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4195 | buf_len = |
| 4196 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4197 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4198 | if (temp == NULL) { |
| 4199 | xmlFree(buf); |
| 4200 | xmlFree(buffer); |
| 4201 | return (NULL); |
| 4202 | } |
| 4203 | buffer = temp; |
| 4204 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4205 | if (temp == NULL) { |
| 4206 | xmlFree(buf); |
| 4207 | xmlFree(buffer); |
| 4208 | return (NULL); |
| 4209 | } |
| 4210 | buf = temp; |
| 4211 | } |
| 4212 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4213 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4214 | sep, name, (char *) buffer); |
| 4215 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4216 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4217 | sep, name, occur, (char *) buffer); |
| 4218 | snprintf((char *) buffer, buf_len, "%s", buf); |
| 4219 | cur = next; |
| 4220 | } while (cur != NULL); |
| 4221 | xmlFree(buf); |
| 4222 | return (buffer); |
| 4223 | } |
| 4224 | |
| 4225 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4226 | * xmlDocGetRootElement: |
| 4227 | * @doc: the document |
| 4228 | * |
| 4229 | * Get the root element of the document (doc->children is a list |
| 4230 | * containing possibly comments, PIs, etc ...). |
| 4231 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4232 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4233 | */ |
| 4234 | xmlNodePtr |
| 4235 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 4236 | xmlNodePtr ret; |
| 4237 | |
| 4238 | if (doc == NULL) return(NULL); |
| 4239 | ret = doc->children; |
| 4240 | while (ret != NULL) { |
| 4241 | if (ret->type == XML_ELEMENT_NODE) |
| 4242 | return(ret); |
| 4243 | ret = ret->next; |
| 4244 | } |
| 4245 | return(ret); |
| 4246 | } |
| 4247 | |
| 4248 | /** |
| 4249 | * xmlDocSetRootElement: |
| 4250 | * @doc: the document |
| 4251 | * @root: the new document root element |
| 4252 | * |
| 4253 | * Set the root element of the document (doc->children is a list |
| 4254 | * containing possibly comments, PIs, etc ...). |
| 4255 | * |
| 4256 | * Returns the old root element if any was found |
| 4257 | */ |
| 4258 | xmlNodePtr |
| 4259 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4260 | xmlNodePtr old = NULL; |
| 4261 | |
| 4262 | if (doc == NULL) return(NULL); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4263 | if (root == NULL) |
| 4264 | return(NULL); |
| 4265 | xmlUnlinkNode(root); |
| 4266 | root->doc = doc; |
| 4267 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4268 | old = doc->children; |
| 4269 | while (old != NULL) { |
| 4270 | if (old->type == XML_ELEMENT_NODE) |
| 4271 | break; |
| 4272 | old = old->next; |
| 4273 | } |
| 4274 | if (old == NULL) { |
| 4275 | if (doc->children == NULL) { |
| 4276 | doc->children = root; |
| 4277 | doc->last = root; |
| 4278 | } else { |
| 4279 | xmlAddSibling(doc->children, root); |
| 4280 | } |
| 4281 | } else { |
| 4282 | xmlReplaceNode(old, root); |
| 4283 | } |
| 4284 | return(old); |
| 4285 | } |
| 4286 | |
| 4287 | /** |
| 4288 | * xmlNodeSetLang: |
| 4289 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4290 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4291 | * |
| 4292 | * Set the language of a node, i.e. the values of the xml:lang |
| 4293 | * attribute. |
| 4294 | */ |
| 4295 | void |
| 4296 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4297 | xmlNsPtr ns; |
| 4298 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4299 | if (cur == NULL) return; |
| 4300 | switch(cur->type) { |
| 4301 | case XML_TEXT_NODE: |
| 4302 | case XML_CDATA_SECTION_NODE: |
| 4303 | case XML_COMMENT_NODE: |
| 4304 | case XML_DOCUMENT_NODE: |
| 4305 | case XML_DOCUMENT_TYPE_NODE: |
| 4306 | case XML_DOCUMENT_FRAG_NODE: |
| 4307 | case XML_NOTATION_NODE: |
| 4308 | case XML_HTML_DOCUMENT_NODE: |
| 4309 | case XML_DTD_NODE: |
| 4310 | case XML_ELEMENT_DECL: |
| 4311 | case XML_ATTRIBUTE_DECL: |
| 4312 | case XML_ENTITY_DECL: |
| 4313 | case XML_PI_NODE: |
| 4314 | case XML_ENTITY_REF_NODE: |
| 4315 | case XML_ENTITY_NODE: |
| 4316 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4317 | #ifdef LIBXML_DOCB_ENABLED |
| 4318 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4319 | #endif |
| 4320 | case XML_XINCLUDE_START: |
| 4321 | case XML_XINCLUDE_END: |
| 4322 | return; |
| 4323 | case XML_ELEMENT_NODE: |
| 4324 | case XML_ATTRIBUTE_NODE: |
| 4325 | break; |
| 4326 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4327 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4328 | if (ns == NULL) |
| 4329 | return; |
| 4330 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | /** |
| 4334 | * xmlNodeGetLang: |
| 4335 | * @cur: the node being checked |
| 4336 | * |
| 4337 | * Searches the language of a node, i.e. the values of the xml:lang |
| 4338 | * attribute or the one carried by the nearest ancestor. |
| 4339 | * |
| 4340 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4341 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4342 | */ |
| 4343 | xmlChar * |
| 4344 | xmlNodeGetLang(xmlNodePtr cur) { |
| 4345 | xmlChar *lang; |
| 4346 | |
| 4347 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 4348 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4349 | if (lang != NULL) |
| 4350 | return(lang); |
| 4351 | cur = cur->parent; |
| 4352 | } |
| 4353 | return(NULL); |
| 4354 | } |
| 4355 | |
| 4356 | |
| 4357 | /** |
| 4358 | * xmlNodeSetSpacePreserve: |
| 4359 | * @cur: the node being changed |
| 4360 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 4361 | * |
| 4362 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 4363 | * value of the xml:space attribute. |
| 4364 | */ |
| 4365 | void |
| 4366 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4367 | xmlNsPtr ns; |
| 4368 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4369 | if (cur == NULL) return; |
| 4370 | switch(cur->type) { |
| 4371 | case XML_TEXT_NODE: |
| 4372 | case XML_CDATA_SECTION_NODE: |
| 4373 | case XML_COMMENT_NODE: |
| 4374 | case XML_DOCUMENT_NODE: |
| 4375 | case XML_DOCUMENT_TYPE_NODE: |
| 4376 | case XML_DOCUMENT_FRAG_NODE: |
| 4377 | case XML_NOTATION_NODE: |
| 4378 | case XML_HTML_DOCUMENT_NODE: |
| 4379 | case XML_DTD_NODE: |
| 4380 | case XML_ELEMENT_DECL: |
| 4381 | case XML_ATTRIBUTE_DECL: |
| 4382 | case XML_ENTITY_DECL: |
| 4383 | case XML_PI_NODE: |
| 4384 | case XML_ENTITY_REF_NODE: |
| 4385 | case XML_ENTITY_NODE: |
| 4386 | case XML_NAMESPACE_DECL: |
| 4387 | case XML_XINCLUDE_START: |
| 4388 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4389 | #ifdef LIBXML_DOCB_ENABLED |
| 4390 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4391 | #endif |
| 4392 | return; |
| 4393 | case XML_ELEMENT_NODE: |
| 4394 | case XML_ATTRIBUTE_NODE: |
| 4395 | break; |
| 4396 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4397 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4398 | if (ns == NULL) |
| 4399 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4400 | switch (val) { |
| 4401 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4402 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4403 | break; |
| 4404 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4405 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4406 | break; |
| 4407 | } |
| 4408 | } |
| 4409 | |
| 4410 | /** |
| 4411 | * xmlNodeGetSpacePreserve: |
| 4412 | * @cur: the node being checked |
| 4413 | * |
| 4414 | * Searches the space preserving behaviour of a node, i.e. the values |
| 4415 | * of the xml:space attribute or the one carried by the nearest |
| 4416 | * ancestor. |
| 4417 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4418 | * 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] | 4419 | */ |
| 4420 | int |
| 4421 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 4422 | xmlChar *space; |
| 4423 | |
| 4424 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4425 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4426 | if (space != NULL) { |
| 4427 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 4428 | xmlFree(space); |
| 4429 | return(1); |
| 4430 | } |
| 4431 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 4432 | xmlFree(space); |
| 4433 | return(0); |
| 4434 | } |
| 4435 | xmlFree(space); |
| 4436 | } |
| 4437 | cur = cur->parent; |
| 4438 | } |
| 4439 | return(-1); |
| 4440 | } |
| 4441 | |
| 4442 | /** |
| 4443 | * xmlNodeSetName: |
| 4444 | * @cur: the node being changed |
| 4445 | * @name: the new tag name |
| 4446 | * |
| 4447 | * Set (or reset) the name of a node. |
| 4448 | */ |
| 4449 | void |
| 4450 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
| 4451 | if (cur == NULL) return; |
| 4452 | if (name == NULL) return; |
| 4453 | switch(cur->type) { |
| 4454 | case XML_TEXT_NODE: |
| 4455 | case XML_CDATA_SECTION_NODE: |
| 4456 | case XML_COMMENT_NODE: |
| 4457 | case XML_DOCUMENT_TYPE_NODE: |
| 4458 | case XML_DOCUMENT_FRAG_NODE: |
| 4459 | case XML_NOTATION_NODE: |
| 4460 | case XML_HTML_DOCUMENT_NODE: |
| 4461 | case XML_NAMESPACE_DECL: |
| 4462 | case XML_XINCLUDE_START: |
| 4463 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4464 | #ifdef LIBXML_DOCB_ENABLED |
| 4465 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4466 | #endif |
| 4467 | return; |
| 4468 | case XML_ELEMENT_NODE: |
| 4469 | case XML_ATTRIBUTE_NODE: |
| 4470 | case XML_PI_NODE: |
| 4471 | case XML_ENTITY_REF_NODE: |
| 4472 | case XML_ENTITY_NODE: |
| 4473 | case XML_DTD_NODE: |
| 4474 | case XML_DOCUMENT_NODE: |
| 4475 | case XML_ELEMENT_DECL: |
| 4476 | case XML_ATTRIBUTE_DECL: |
| 4477 | case XML_ENTITY_DECL: |
| 4478 | break; |
| 4479 | } |
| 4480 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 4481 | cur->name = xmlStrdup(name); |
| 4482 | } |
| 4483 | |
| 4484 | /** |
| 4485 | * xmlNodeSetBase: |
| 4486 | * @cur: the node being changed |
| 4487 | * @uri: the new base URI |
| 4488 | * |
| 4489 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 4490 | * xml:base attribute. |
| 4491 | */ |
| 4492 | void |
| 4493 | xmlNodeSetBase(xmlNodePtr cur, xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4494 | xmlNsPtr ns; |
| 4495 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4496 | if (cur == NULL) return; |
| 4497 | switch(cur->type) { |
| 4498 | case XML_TEXT_NODE: |
| 4499 | case XML_CDATA_SECTION_NODE: |
| 4500 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4501 | case XML_DOCUMENT_TYPE_NODE: |
| 4502 | case XML_DOCUMENT_FRAG_NODE: |
| 4503 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4504 | case XML_DTD_NODE: |
| 4505 | case XML_ELEMENT_DECL: |
| 4506 | case XML_ATTRIBUTE_DECL: |
| 4507 | case XML_ENTITY_DECL: |
| 4508 | case XML_PI_NODE: |
| 4509 | case XML_ENTITY_REF_NODE: |
| 4510 | case XML_ENTITY_NODE: |
| 4511 | case XML_NAMESPACE_DECL: |
| 4512 | case XML_XINCLUDE_START: |
| 4513 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4514 | return; |
| 4515 | case XML_ELEMENT_NODE: |
| 4516 | case XML_ATTRIBUTE_NODE: |
| 4517 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 4518 | case XML_DOCUMENT_NODE: |
| 4519 | #ifdef LIBXML_DOCB_ENABLED |
| 4520 | case XML_DOCB_DOCUMENT_NODE: |
| 4521 | #endif |
| 4522 | case XML_HTML_DOCUMENT_NODE: { |
| 4523 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 4524 | |
| 4525 | if (doc->URL != NULL) |
| 4526 | xmlFree((xmlChar *) doc->URL); |
| 4527 | if (uri == NULL) |
| 4528 | doc->URL = NULL; |
| 4529 | else |
| 4530 | doc->URL = xmlStrdup(uri); |
| 4531 | return; |
| 4532 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4533 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4534 | |
| 4535 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4536 | if (ns == NULL) |
| 4537 | return; |
| 4538 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
| 4541 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4542 | * xmlNodeGetBase: |
| 4543 | * @doc: the document the node pertains to |
| 4544 | * @cur: the node being checked |
| 4545 | * |
| 4546 | * Searches for the BASE URL. The code should work on both XML |
| 4547 | * and HTML document even if base mechanisms are completely different. |
| 4548 | * It returns the base as defined in RFC 2396 sections |
| 4549 | * 5.1.1. Base URI within Document Content |
| 4550 | * and |
| 4551 | * 5.1.2. Base URI from the Encapsulating Entity |
| 4552 | * However it does not return the document base (5.1.3), use |
| 4553 | * xmlDocumentGetBase() for this |
| 4554 | * |
| 4555 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4556 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4557 | */ |
| 4558 | xmlChar * |
| 4559 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4560 | xmlChar *oldbase = NULL; |
| 4561 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4562 | |
| 4563 | if ((cur == NULL) && (doc == NULL)) |
| 4564 | return(NULL); |
| 4565 | if (doc == NULL) doc = cur->doc; |
| 4566 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 4567 | cur = doc->children; |
| 4568 | while ((cur != NULL) && (cur->name != NULL)) { |
| 4569 | if (cur->type != XML_ELEMENT_NODE) { |
| 4570 | cur = cur->next; |
| 4571 | continue; |
| 4572 | } |
| 4573 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 4574 | cur = cur->children; |
| 4575 | continue; |
| 4576 | } |
| 4577 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 4578 | cur = cur->children; |
| 4579 | continue; |
| 4580 | } |
| 4581 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 4582 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 4583 | } |
| 4584 | cur = cur->next; |
| 4585 | } |
| 4586 | return(NULL); |
| 4587 | } |
| 4588 | while (cur != NULL) { |
| 4589 | if (cur->type == XML_ENTITY_DECL) { |
| 4590 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 4591 | return(xmlStrdup(ent->URI)); |
| 4592 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4593 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4594 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4595 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4596 | if (oldbase != NULL) { |
| 4597 | newbase = xmlBuildURI(oldbase, base); |
| 4598 | if (newbase != NULL) { |
| 4599 | xmlFree(oldbase); |
| 4600 | xmlFree(base); |
| 4601 | oldbase = newbase; |
| 4602 | } else { |
| 4603 | xmlFree(oldbase); |
| 4604 | xmlFree(base); |
| 4605 | return(NULL); |
| 4606 | } |
| 4607 | } else { |
| 4608 | oldbase = base; |
| 4609 | } |
| 4610 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 4611 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 4612 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 4613 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4614 | } |
| 4615 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4616 | cur = cur->parent; |
| 4617 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4618 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 4619 | if (oldbase == NULL) |
| 4620 | return(xmlStrdup(doc->URL)); |
| 4621 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 4622 | xmlFree(oldbase); |
| 4623 | return(newbase); |
| 4624 | } |
| 4625 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | /** |
| 4629 | * xmlNodeGetContent: |
| 4630 | * @cur: the node being read |
| 4631 | * |
| 4632 | * Read the value of a node, this can be either the text carried |
| 4633 | * directly by this node if it's a TEXT node or the aggregate string |
| 4634 | * 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] | 4635 | * Entity references are substituted. |
| 4636 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4637 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4638 | */ |
| 4639 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4640 | xmlNodeGetContent(xmlNodePtr cur) |
| 4641 | { |
| 4642 | if (cur == NULL) |
| 4643 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4644 | switch (cur->type) { |
| 4645 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4646 | case XML_ELEMENT_NODE:{ |
| 4647 | xmlNodePtr tmp = cur; |
| 4648 | xmlBufferPtr buffer; |
| 4649 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4650 | |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 4651 | buffer = xmlBufferCreateSize(64); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4652 | if (buffer == NULL) |
| 4653 | return (NULL); |
| 4654 | while (tmp != NULL) { |
| 4655 | switch (tmp->type) { |
| 4656 | case XML_CDATA_SECTION_NODE: |
| 4657 | case XML_TEXT_NODE: |
| 4658 | if (tmp->content != NULL) |
| 4659 | xmlBufferCat(buffer, tmp->content); |
| 4660 | break; |
| 4661 | case XML_ENTITY_REF_NODE:{ |
| 4662 | /* recursive substitution of entity references */ |
| 4663 | xmlChar *cont = xmlNodeGetContent(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4664 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4665 | if (cont) { |
| 4666 | xmlBufferCat(buffer, |
| 4667 | (const xmlChar *) cont); |
| 4668 | xmlFree(cont); |
| 4669 | } |
| 4670 | break; |
| 4671 | } |
| 4672 | default: |
| 4673 | break; |
| 4674 | } |
| 4675 | /* |
| 4676 | * Skip to next node |
| 4677 | */ |
| 4678 | if (tmp->children != NULL) { |
| 4679 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4680 | tmp = tmp->children; |
| 4681 | continue; |
| 4682 | } |
| 4683 | } |
| 4684 | if (tmp == cur) |
| 4685 | break; |
Daniel Veillard | 6c83120 | 2001-03-07 15:57:53 +0000 | [diff] [blame] | 4686 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4687 | if (tmp->next != NULL) { |
| 4688 | tmp = tmp->next; |
| 4689 | continue; |
| 4690 | } |
| 4691 | |
| 4692 | do { |
| 4693 | tmp = tmp->parent; |
| 4694 | if (tmp == NULL) |
| 4695 | break; |
| 4696 | if (tmp == cur) { |
| 4697 | tmp = NULL; |
| 4698 | break; |
| 4699 | } |
| 4700 | if (tmp->next != NULL) { |
| 4701 | tmp = tmp->next; |
| 4702 | break; |
| 4703 | } |
| 4704 | } while (tmp != NULL); |
| 4705 | } |
| 4706 | ret = buffer->content; |
| 4707 | buffer->content = NULL; |
| 4708 | xmlBufferFree(buffer); |
| 4709 | return (ret); |
| 4710 | } |
| 4711 | case XML_ATTRIBUTE_NODE:{ |
| 4712 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 4713 | |
| 4714 | if (attr->parent != NULL) |
| 4715 | return (xmlNodeListGetString |
| 4716 | (attr->parent->doc, attr->children, 1)); |
| 4717 | else |
| 4718 | return (xmlNodeListGetString(NULL, attr->children, 1)); |
| 4719 | break; |
| 4720 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4721 | case XML_COMMENT_NODE: |
| 4722 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4723 | if (cur->content != NULL) |
| 4724 | return (xmlStrdup(cur->content)); |
| 4725 | return (NULL); |
| 4726 | case XML_ENTITY_REF_NODE:{ |
| 4727 | xmlEntityPtr ent; |
| 4728 | xmlNodePtr tmp; |
| 4729 | xmlBufferPtr buffer; |
| 4730 | xmlChar *ret; |
| 4731 | |
| 4732 | /* lookup entity declaration */ |
| 4733 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 4734 | if (ent == NULL) |
| 4735 | return (NULL); |
| 4736 | |
| 4737 | buffer = xmlBufferCreate(); |
| 4738 | if (buffer == NULL) |
| 4739 | return (NULL); |
| 4740 | |
| 4741 | /* an entity content can be any "well balanced chunk", |
| 4742 | * i.e. the result of the content [43] production: |
| 4743 | * http://www.w3.org/TR/REC-xml#NT-content |
| 4744 | * -> we iterate through child nodes and recursive call |
| 4745 | * xmlNodeGetContent() which handles all possible node types */ |
| 4746 | tmp = ent->children; |
| 4747 | while (tmp) { |
| 4748 | xmlChar *cont = xmlNodeGetContent(tmp); |
| 4749 | |
| 4750 | if (cont) { |
| 4751 | xmlBufferCat(buffer, (const xmlChar *) cont); |
| 4752 | xmlFree(cont); |
| 4753 | } |
| 4754 | tmp = tmp->next; |
| 4755 | } |
| 4756 | |
| 4757 | ret = buffer->content; |
| 4758 | buffer->content = NULL; |
| 4759 | xmlBufferFree(buffer); |
| 4760 | return (ret); |
| 4761 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4762 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4763 | case XML_DOCUMENT_TYPE_NODE: |
| 4764 | case XML_NOTATION_NODE: |
| 4765 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4766 | case XML_XINCLUDE_START: |
| 4767 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4768 | return (NULL); |
| 4769 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4770 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4771 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4772 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4773 | case XML_HTML_DOCUMENT_NODE: { |
| 4774 | xmlChar *tmp; |
| 4775 | xmlChar *res = NULL; |
| 4776 | |
| 4777 | cur = cur->children; |
| 4778 | while (cur!= NULL) { |
| 4779 | if ((cur->type == XML_ELEMENT_NODE) || |
| 4780 | (cur->type == XML_TEXT_NODE) || |
| 4781 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4782 | tmp = xmlNodeGetContent(cur); |
| 4783 | if (tmp != NULL) { |
| 4784 | if (res == NULL) |
| 4785 | res = tmp; |
| 4786 | else { |
| 4787 | res = xmlStrcat(res, tmp); |
| 4788 | xmlFree(tmp); |
| 4789 | } |
| 4790 | } |
| 4791 | } |
| 4792 | cur = cur->next; |
| 4793 | } |
| 4794 | return(res); |
| 4795 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4796 | case XML_NAMESPACE_DECL: { |
| 4797 | xmlChar *tmp; |
| 4798 | |
| 4799 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 4800 | return (tmp); |
| 4801 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4802 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4803 | /* TODO !!! */ |
| 4804 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4805 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4806 | /* TODO !!! */ |
| 4807 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4808 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4809 | /* TODO !!! */ |
| 4810 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4811 | case XML_CDATA_SECTION_NODE: |
| 4812 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4813 | if (cur->content != NULL) |
| 4814 | return (xmlStrdup(cur->content)); |
| 4815 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4816 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4817 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4818 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4819 | /** |
| 4820 | * xmlNodeSetContent: |
| 4821 | * @cur: the node being modified |
| 4822 | * @content: the new value of the content |
| 4823 | * |
| 4824 | * Replace the content of a node. |
| 4825 | */ |
| 4826 | void |
| 4827 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 4828 | if (cur == NULL) { |
| 4829 | #ifdef DEBUG_TREE |
| 4830 | xmlGenericError(xmlGenericErrorContext, |
| 4831 | "xmlNodeSetContent : node == NULL\n"); |
| 4832 | #endif |
| 4833 | return; |
| 4834 | } |
| 4835 | switch (cur->type) { |
| 4836 | case XML_DOCUMENT_FRAG_NODE: |
| 4837 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 4838 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4839 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 4840 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 4841 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 4842 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4843 | case XML_TEXT_NODE: |
| 4844 | case XML_CDATA_SECTION_NODE: |
| 4845 | case XML_ENTITY_REF_NODE: |
| 4846 | case XML_ENTITY_NODE: |
| 4847 | case XML_PI_NODE: |
| 4848 | case XML_COMMENT_NODE: |
| 4849 | if (cur->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4850 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4851 | } |
| 4852 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 4853 | cur->last = cur->children = NULL; |
| 4854 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4855 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4856 | } else |
| 4857 | cur->content = NULL; |
| 4858 | break; |
| 4859 | case XML_DOCUMENT_NODE: |
| 4860 | case XML_HTML_DOCUMENT_NODE: |
| 4861 | case XML_DOCUMENT_TYPE_NODE: |
| 4862 | case XML_XINCLUDE_START: |
| 4863 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4864 | #ifdef LIBXML_DOCB_ENABLED |
| 4865 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4866 | #endif |
| 4867 | break; |
| 4868 | case XML_NOTATION_NODE: |
| 4869 | break; |
| 4870 | case XML_DTD_NODE: |
| 4871 | break; |
| 4872 | case XML_NAMESPACE_DECL: |
| 4873 | break; |
| 4874 | case XML_ELEMENT_DECL: |
| 4875 | /* TODO !!! */ |
| 4876 | break; |
| 4877 | case XML_ATTRIBUTE_DECL: |
| 4878 | /* TODO !!! */ |
| 4879 | break; |
| 4880 | case XML_ENTITY_DECL: |
| 4881 | /* TODO !!! */ |
| 4882 | break; |
| 4883 | } |
| 4884 | } |
| 4885 | |
| 4886 | /** |
| 4887 | * xmlNodeSetContentLen: |
| 4888 | * @cur: the node being modified |
| 4889 | * @content: the new value of the content |
| 4890 | * @len: the size of @content |
| 4891 | * |
| 4892 | * Replace the content of a node. |
| 4893 | */ |
| 4894 | void |
| 4895 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 4896 | if (cur == NULL) { |
| 4897 | #ifdef DEBUG_TREE |
| 4898 | xmlGenericError(xmlGenericErrorContext, |
| 4899 | "xmlNodeSetContentLen : node == NULL\n"); |
| 4900 | #endif |
| 4901 | return; |
| 4902 | } |
| 4903 | switch (cur->type) { |
| 4904 | case XML_DOCUMENT_FRAG_NODE: |
| 4905 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 4906 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4907 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 4908 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 4909 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 4910 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4911 | case XML_TEXT_NODE: |
| 4912 | case XML_CDATA_SECTION_NODE: |
| 4913 | case XML_ENTITY_REF_NODE: |
| 4914 | case XML_ENTITY_NODE: |
| 4915 | case XML_PI_NODE: |
| 4916 | case XML_COMMENT_NODE: |
| 4917 | case XML_NOTATION_NODE: |
| 4918 | if (cur->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4919 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4920 | } |
| 4921 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 4922 | cur->children = cur->last = NULL; |
| 4923 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4924 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4925 | } else |
| 4926 | cur->content = NULL; |
| 4927 | break; |
| 4928 | case XML_DOCUMENT_NODE: |
| 4929 | case XML_DTD_NODE: |
| 4930 | case XML_HTML_DOCUMENT_NODE: |
| 4931 | case XML_DOCUMENT_TYPE_NODE: |
| 4932 | case XML_NAMESPACE_DECL: |
| 4933 | case XML_XINCLUDE_START: |
| 4934 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4935 | #ifdef LIBXML_DOCB_ENABLED |
| 4936 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4937 | #endif |
| 4938 | break; |
| 4939 | case XML_ELEMENT_DECL: |
| 4940 | /* TODO !!! */ |
| 4941 | break; |
| 4942 | case XML_ATTRIBUTE_DECL: |
| 4943 | /* TODO !!! */ |
| 4944 | break; |
| 4945 | case XML_ENTITY_DECL: |
| 4946 | /* TODO !!! */ |
| 4947 | break; |
| 4948 | } |
| 4949 | } |
| 4950 | |
| 4951 | /** |
| 4952 | * xmlNodeAddContentLen: |
| 4953 | * @cur: the node being modified |
| 4954 | * @content: extra content |
| 4955 | * @len: the size of @content |
| 4956 | * |
| 4957 | * Append the extra substring to the node content. |
| 4958 | */ |
| 4959 | void |
| 4960 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 4961 | if (cur == NULL) { |
| 4962 | #ifdef DEBUG_TREE |
| 4963 | xmlGenericError(xmlGenericErrorContext, |
| 4964 | "xmlNodeAddContentLen : node == NULL\n"); |
| 4965 | #endif |
| 4966 | return; |
| 4967 | } |
| 4968 | if (len <= 0) return; |
| 4969 | switch (cur->type) { |
| 4970 | case XML_DOCUMENT_FRAG_NODE: |
| 4971 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4972 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4973 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 4974 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4975 | newNode = xmlNewTextLen(content, len); |
| 4976 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4977 | tmp = xmlAddChild(cur, newNode); |
| 4978 | if (tmp != newNode) |
| 4979 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4980 | if ((last != NULL) && (last->next == newNode)) { |
| 4981 | xmlTextMerge(last, newNode); |
| 4982 | } |
| 4983 | } |
| 4984 | break; |
| 4985 | } |
| 4986 | case XML_ATTRIBUTE_NODE: |
| 4987 | break; |
| 4988 | case XML_TEXT_NODE: |
| 4989 | case XML_CDATA_SECTION_NODE: |
| 4990 | case XML_ENTITY_REF_NODE: |
| 4991 | case XML_ENTITY_NODE: |
| 4992 | case XML_PI_NODE: |
| 4993 | case XML_COMMENT_NODE: |
| 4994 | case XML_NOTATION_NODE: |
| 4995 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4996 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4997 | } |
| 4998 | case XML_DOCUMENT_NODE: |
| 4999 | case XML_DTD_NODE: |
| 5000 | case XML_HTML_DOCUMENT_NODE: |
| 5001 | case XML_DOCUMENT_TYPE_NODE: |
| 5002 | case XML_NAMESPACE_DECL: |
| 5003 | case XML_XINCLUDE_START: |
| 5004 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5005 | #ifdef LIBXML_DOCB_ENABLED |
| 5006 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5007 | #endif |
| 5008 | break; |
| 5009 | case XML_ELEMENT_DECL: |
| 5010 | case XML_ATTRIBUTE_DECL: |
| 5011 | case XML_ENTITY_DECL: |
| 5012 | break; |
| 5013 | } |
| 5014 | } |
| 5015 | |
| 5016 | /** |
| 5017 | * xmlNodeAddContent: |
| 5018 | * @cur: the node being modified |
| 5019 | * @content: extra content |
| 5020 | * |
| 5021 | * Append the extra substring to the node content. |
| 5022 | */ |
| 5023 | void |
| 5024 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5025 | int len; |
| 5026 | |
| 5027 | if (cur == NULL) { |
| 5028 | #ifdef DEBUG_TREE |
| 5029 | xmlGenericError(xmlGenericErrorContext, |
| 5030 | "xmlNodeAddContent : node == NULL\n"); |
| 5031 | #endif |
| 5032 | return; |
| 5033 | } |
| 5034 | if (content == NULL) return; |
| 5035 | len = xmlStrlen(content); |
| 5036 | xmlNodeAddContentLen(cur, content, len); |
| 5037 | } |
| 5038 | |
| 5039 | /** |
| 5040 | * xmlTextMerge: |
| 5041 | * @first: the first text node |
| 5042 | * @second: the second text node being merged |
| 5043 | * |
| 5044 | * Merge two text nodes into one |
| 5045 | * Returns the first text node augmented |
| 5046 | */ |
| 5047 | xmlNodePtr |
| 5048 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5049 | if (first == NULL) return(second); |
| 5050 | if (second == NULL) return(first); |
| 5051 | if (first->type != XML_TEXT_NODE) return(first); |
| 5052 | if (second->type != XML_TEXT_NODE) return(first); |
| 5053 | if (second->name != first->name) |
| 5054 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5055 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5056 | xmlUnlinkNode(second); |
| 5057 | xmlFreeNode(second); |
| 5058 | return(first); |
| 5059 | } |
| 5060 | |
| 5061 | /** |
| 5062 | * xmlGetNsList: |
| 5063 | * @doc: the document |
| 5064 | * @node: the current node |
| 5065 | * |
| 5066 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5067 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5068 | * that need to be freed by the caller or NULL if no |
| 5069 | * namespace if defined |
| 5070 | */ |
| 5071 | xmlNsPtr * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5072 | xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) |
| 5073 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5074 | xmlNsPtr cur; |
| 5075 | xmlNsPtr *ret = NULL; |
| 5076 | int nbns = 0; |
| 5077 | int maxns = 10; |
| 5078 | int i; |
| 5079 | |
| 5080 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5081 | if (node->type == XML_ELEMENT_NODE) { |
| 5082 | cur = node->nsDef; |
| 5083 | while (cur != NULL) { |
| 5084 | if (ret == NULL) { |
| 5085 | ret = |
| 5086 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5087 | sizeof(xmlNsPtr)); |
| 5088 | if (ret == NULL) { |
| 5089 | xmlGenericError(xmlGenericErrorContext, |
| 5090 | "xmlGetNsList : out of memory!\n"); |
| 5091 | return (NULL); |
| 5092 | } |
| 5093 | ret[nbns] = NULL; |
| 5094 | } |
| 5095 | for (i = 0; i < nbns; i++) { |
| 5096 | if ((cur->prefix == ret[i]->prefix) || |
| 5097 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5098 | break; |
| 5099 | } |
| 5100 | if (i >= nbns) { |
| 5101 | if (nbns >= maxns) { |
| 5102 | maxns *= 2; |
| 5103 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5104 | (maxns + |
| 5105 | 1) * |
| 5106 | sizeof(xmlNsPtr)); |
| 5107 | if (ret == NULL) { |
| 5108 | xmlGenericError(xmlGenericErrorContext, |
| 5109 | "xmlGetNsList : realloc failed!\n"); |
| 5110 | return (NULL); |
| 5111 | } |
| 5112 | } |
| 5113 | ret[nbns++] = cur; |
| 5114 | ret[nbns] = NULL; |
| 5115 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5116 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5117 | cur = cur->next; |
| 5118 | } |
| 5119 | } |
| 5120 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5121 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5122 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | /** |
| 5126 | * xmlSearchNs: |
| 5127 | * @doc: the document |
| 5128 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5129 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5130 | * |
| 5131 | * Search a Ns registered under a given name space for a document. |
| 5132 | * recurse on the parents until it finds the defined namespace |
| 5133 | * or return NULL otherwise. |
| 5134 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5135 | * We don't allow to cross entities boundaries. If you don't declare |
| 5136 | * the namespace within those you will be in troubles !!! A warning |
| 5137 | * is generated to cover this case. |
| 5138 | * |
| 5139 | * Returns the namespace pointer or NULL. |
| 5140 | */ |
| 5141 | xmlNsPtr |
| 5142 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
| 5143 | xmlNsPtr cur; |
| 5144 | |
| 5145 | if (node == NULL) return(NULL); |
| 5146 | if ((nameSpace != NULL) && |
| 5147 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5148 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5149 | /* |
| 5150 | * The XML-1.0 namespace is normally held on the root |
| 5151 | * element. In this case exceptionally create it on the |
| 5152 | * node element. |
| 5153 | */ |
| 5154 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5155 | if (cur == NULL) { |
| 5156 | xmlGenericError(xmlGenericErrorContext, |
| 5157 | "xmlSearchNs : malloc failed\n"); |
| 5158 | return(NULL); |
| 5159 | } |
| 5160 | memset(cur, 0, sizeof(xmlNs)); |
| 5161 | cur->type = XML_LOCAL_NAMESPACE; |
| 5162 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5163 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5164 | cur->next = node->nsDef; |
| 5165 | node->nsDef = cur; |
| 5166 | return(cur); |
| 5167 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5168 | if (doc->oldNs == NULL) { |
| 5169 | /* |
| 5170 | * Allocate a new Namespace and fill the fields. |
| 5171 | */ |
| 5172 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5173 | if (doc->oldNs == NULL) { |
| 5174 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5175 | "xmlSearchNs : malloc failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5176 | return(NULL); |
| 5177 | } |
| 5178 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5179 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
| 5180 | |
| 5181 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5182 | doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5183 | } |
| 5184 | return(doc->oldNs); |
| 5185 | } |
| 5186 | while (node != NULL) { |
| 5187 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5188 | (node->type == XML_ENTITY_NODE) || |
| 5189 | (node->type == XML_ENTITY_DECL)) |
| 5190 | return(NULL); |
| 5191 | if (node->type == XML_ELEMENT_NODE) { |
| 5192 | cur = node->nsDef; |
| 5193 | while (cur != NULL) { |
| 5194 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5195 | (cur->href != NULL)) |
| 5196 | return(cur); |
| 5197 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5198 | (cur->href != NULL) && |
| 5199 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5200 | return(cur); |
| 5201 | cur = cur->next; |
| 5202 | } |
| 5203 | } |
| 5204 | node = node->parent; |
| 5205 | } |
| 5206 | return(NULL); |
| 5207 | } |
| 5208 | |
| 5209 | /** |
| 5210 | * xmlSearchNsByHref: |
| 5211 | * @doc: the document |
| 5212 | * @node: the current node |
| 5213 | * @href: the namespace value |
| 5214 | * |
| 5215 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 5216 | * the defined namespace or return NULL otherwise. |
| 5217 | * Returns the namespace pointer or NULL. |
| 5218 | */ |
| 5219 | xmlNsPtr |
| 5220 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) { |
| 5221 | xmlNsPtr cur; |
| 5222 | xmlNodePtr orig = node; |
| 5223 | |
| 5224 | if ((node == NULL) || (href == NULL)) return(NULL); |
| 5225 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5226 | /* |
| 5227 | * Only the document can hold the XML spec namespace. |
| 5228 | */ |
Daniel Veillard | c1a0da3 | 2002-08-14 08:32:18 +0000 | [diff] [blame] | 5229 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5230 | /* |
| 5231 | * The XML-1.0 namespace is normally held on the root |
| 5232 | * element. In this case exceptionally create it on the |
| 5233 | * node element. |
| 5234 | */ |
| 5235 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5236 | if (cur == NULL) { |
| 5237 | xmlGenericError(xmlGenericErrorContext, |
| 5238 | "xmlSearchNs : malloc failed\n"); |
| 5239 | return(NULL); |
| 5240 | } |
| 5241 | memset(cur, 0, sizeof(xmlNs)); |
| 5242 | cur->type = XML_LOCAL_NAMESPACE; |
| 5243 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5244 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5245 | cur->next = node->nsDef; |
| 5246 | node->nsDef = cur; |
| 5247 | return(cur); |
| 5248 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5249 | if (doc->oldNs == NULL) { |
| 5250 | /* |
| 5251 | * Allocate a new Namespace and fill the fields. |
| 5252 | */ |
| 5253 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5254 | if (doc->oldNs == NULL) { |
| 5255 | xmlGenericError(xmlGenericErrorContext, |
| 5256 | "xmlSearchNsByHref : malloc failed\n"); |
| 5257 | return(NULL); |
| 5258 | } |
| 5259 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5260 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
| 5261 | |
| 5262 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5263 | doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5264 | } |
| 5265 | return(doc->oldNs); |
| 5266 | } |
| 5267 | while (node != NULL) { |
| 5268 | cur = node->nsDef; |
| 5269 | while (cur != NULL) { |
| 5270 | if ((cur->href != NULL) && (href != NULL) && |
| 5271 | (xmlStrEqual(cur->href, href))) { |
| 5272 | /* |
| 5273 | * Check that the prefix is not shadowed between orig and node |
| 5274 | */ |
| 5275 | xmlNodePtr check = orig; |
| 5276 | xmlNsPtr tst; |
| 5277 | |
| 5278 | while (check != node) { |
| 5279 | tst = check->nsDef; |
| 5280 | while (tst != NULL) { |
| 5281 | if ((tst->prefix == NULL) && (cur->prefix == NULL)) |
| 5282 | goto shadowed; |
| 5283 | if ((tst->prefix != NULL) && (cur->prefix != NULL) && |
| 5284 | (xmlStrEqual(tst->prefix, cur->prefix))) |
| 5285 | goto shadowed; |
| 5286 | tst = tst->next; |
| 5287 | } |
| 5288 | check = check->parent; |
| 5289 | } |
| 5290 | return(cur); |
| 5291 | } |
| 5292 | shadowed: |
| 5293 | cur = cur->next; |
| 5294 | } |
| 5295 | node = node->parent; |
| 5296 | } |
| 5297 | return(NULL); |
| 5298 | } |
| 5299 | |
| 5300 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5301 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5302 | * @doc: the document |
| 5303 | * @tree: a node expected to hold the new namespace |
| 5304 | * @ns: the original namespace |
| 5305 | * |
| 5306 | * This function tries to locate a namespace definition in a tree |
| 5307 | * ancestors, or create a new namespace definition node similar to |
| 5308 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 5309 | * null (default namespace) or reused within the subtree defined by |
| 5310 | * @tree or on one of its ancestors then a new prefix is generated. |
| 5311 | * Returns the (new) namespace definition or NULL in case of error |
| 5312 | */ |
| 5313 | xmlNsPtr |
| 5314 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 5315 | xmlNsPtr def; |
| 5316 | xmlChar prefix[50]; |
| 5317 | int counter = 1; |
| 5318 | |
| 5319 | if (tree == NULL) { |
| 5320 | #ifdef DEBUG_TREE |
| 5321 | xmlGenericError(xmlGenericErrorContext, |
| 5322 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 5323 | #endif |
| 5324 | return(NULL); |
| 5325 | } |
| 5326 | if (ns == NULL) { |
| 5327 | #ifdef DEBUG_TREE |
| 5328 | xmlGenericError(xmlGenericErrorContext, |
| 5329 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 5330 | #endif |
| 5331 | return(NULL); |
| 5332 | } |
| 5333 | /* |
| 5334 | * Search an existing namespace definition inherited. |
| 5335 | */ |
| 5336 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 5337 | if (def != NULL) |
| 5338 | return(def); |
| 5339 | |
| 5340 | /* |
| 5341 | * Find a close prefix which is not already in use. |
| 5342 | * Let's strip namespace prefixes longer than 20 chars ! |
| 5343 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5344 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5345 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5346 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5347 | snprintf((char *) prefix, sizeof(prefix), "%.20s", ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5348 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5349 | def = xmlSearchNs(doc, tree, prefix); |
| 5350 | while (def != NULL) { |
| 5351 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5352 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5353 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5354 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5355 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5356 | def = xmlSearchNs(doc, tree, prefix); |
| 5357 | } |
| 5358 | |
| 5359 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5360 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5361 | */ |
| 5362 | def = xmlNewNs(tree, ns->href, prefix); |
| 5363 | return(def); |
| 5364 | } |
| 5365 | |
| 5366 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5367 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5368 | * @doc: the document |
| 5369 | * @tree: a node defining the subtree to reconciliate |
| 5370 | * |
| 5371 | * This function checks that all the namespaces declared within the given |
| 5372 | * tree are properly declared. This is needed for example after Copy or Cut |
| 5373 | * and then paste operations. The subtree may still hold pointers to |
| 5374 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5375 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5376 | * the new environment. If not possible the new namespaces are redeclared |
| 5377 | * on @tree at the top of the given subtree. |
| 5378 | * Returns the number of namespace declarations created or -1 in case of error. |
| 5379 | */ |
| 5380 | int |
| 5381 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 5382 | xmlNsPtr *oldNs = NULL; |
| 5383 | xmlNsPtr *newNs = NULL; |
| 5384 | int sizeCache = 0; |
| 5385 | int nbCache = 0; |
| 5386 | |
| 5387 | xmlNsPtr n; |
| 5388 | xmlNodePtr node = tree; |
| 5389 | xmlAttrPtr attr; |
| 5390 | int ret = 0, i; |
| 5391 | |
| 5392 | while (node != NULL) { |
| 5393 | /* |
| 5394 | * Reconciliate the node namespace |
| 5395 | */ |
| 5396 | if (node->ns != NULL) { |
| 5397 | /* |
| 5398 | * initialize the cache if needed |
| 5399 | */ |
| 5400 | if (sizeCache == 0) { |
| 5401 | sizeCache = 10; |
| 5402 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5403 | sizeof(xmlNsPtr)); |
| 5404 | if (oldNs == NULL) { |
| 5405 | xmlGenericError(xmlGenericErrorContext, |
| 5406 | "xmlReconciliateNs : memory pbm\n"); |
| 5407 | return(-1); |
| 5408 | } |
| 5409 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5410 | sizeof(xmlNsPtr)); |
| 5411 | if (newNs == NULL) { |
| 5412 | xmlGenericError(xmlGenericErrorContext, |
| 5413 | "xmlReconciliateNs : memory pbm\n"); |
| 5414 | xmlFree(oldNs); |
| 5415 | return(-1); |
| 5416 | } |
| 5417 | } |
| 5418 | for (i = 0;i < nbCache;i++) { |
| 5419 | if (oldNs[i] == node->ns) { |
| 5420 | node->ns = newNs[i]; |
| 5421 | break; |
| 5422 | } |
| 5423 | } |
| 5424 | if (i == nbCache) { |
| 5425 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5426 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5427 | */ |
| 5428 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 5429 | if (n != NULL) { /* :-( what if else ??? */ |
| 5430 | /* |
| 5431 | * check if we need to grow the cache buffers. |
| 5432 | */ |
| 5433 | if (sizeCache <= nbCache) { |
| 5434 | sizeCache *= 2; |
| 5435 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5436 | sizeof(xmlNsPtr)); |
| 5437 | if (oldNs == NULL) { |
| 5438 | xmlGenericError(xmlGenericErrorContext, |
| 5439 | "xmlReconciliateNs : memory pbm\n"); |
| 5440 | xmlFree(newNs); |
| 5441 | return(-1); |
| 5442 | } |
| 5443 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5444 | sizeof(xmlNsPtr)); |
| 5445 | if (newNs == NULL) { |
| 5446 | xmlGenericError(xmlGenericErrorContext, |
| 5447 | "xmlReconciliateNs : memory pbm\n"); |
| 5448 | xmlFree(oldNs); |
| 5449 | return(-1); |
| 5450 | } |
| 5451 | } |
| 5452 | newNs[nbCache] = n; |
| 5453 | oldNs[nbCache++] = node->ns; |
| 5454 | node->ns = n; |
| 5455 | } |
| 5456 | } |
| 5457 | } |
| 5458 | /* |
| 5459 | * now check for namespace hold by attributes on the node. |
| 5460 | */ |
| 5461 | attr = node->properties; |
| 5462 | while (attr != NULL) { |
| 5463 | if (attr->ns != NULL) { |
| 5464 | /* |
| 5465 | * initialize the cache if needed |
| 5466 | */ |
| 5467 | if (sizeCache == 0) { |
| 5468 | sizeCache = 10; |
| 5469 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5470 | sizeof(xmlNsPtr)); |
| 5471 | if (oldNs == NULL) { |
| 5472 | xmlGenericError(xmlGenericErrorContext, |
| 5473 | "xmlReconciliateNs : memory pbm\n"); |
| 5474 | return(-1); |
| 5475 | } |
| 5476 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5477 | sizeof(xmlNsPtr)); |
| 5478 | if (newNs == NULL) { |
| 5479 | xmlGenericError(xmlGenericErrorContext, |
| 5480 | "xmlReconciliateNs : memory pbm\n"); |
| 5481 | xmlFree(oldNs); |
| 5482 | return(-1); |
| 5483 | } |
| 5484 | } |
| 5485 | for (i = 0;i < nbCache;i++) { |
| 5486 | if (oldNs[i] == attr->ns) { |
Daniel Veillard | ce66ce1 | 2002-10-28 19:01:59 +0000 | [diff] [blame] | 5487 | attr->ns = newNs[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5488 | break; |
| 5489 | } |
| 5490 | } |
| 5491 | if (i == nbCache) { |
| 5492 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5493 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5494 | */ |
| 5495 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 5496 | if (n != NULL) { /* :-( what if else ??? */ |
| 5497 | /* |
| 5498 | * check if we need to grow the cache buffers. |
| 5499 | */ |
| 5500 | if (sizeCache <= nbCache) { |
| 5501 | sizeCache *= 2; |
| 5502 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5503 | sizeof(xmlNsPtr)); |
| 5504 | if (oldNs == NULL) { |
| 5505 | xmlGenericError(xmlGenericErrorContext, |
| 5506 | "xmlReconciliateNs : memory pbm\n"); |
| 5507 | xmlFree(newNs); |
| 5508 | return(-1); |
| 5509 | } |
| 5510 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5511 | sizeof(xmlNsPtr)); |
| 5512 | if (newNs == NULL) { |
| 5513 | xmlGenericError(xmlGenericErrorContext, |
| 5514 | "xmlReconciliateNs : memory pbm\n"); |
| 5515 | xmlFree(oldNs); |
| 5516 | return(-1); |
| 5517 | } |
| 5518 | } |
| 5519 | newNs[nbCache] = n; |
| 5520 | oldNs[nbCache++] = attr->ns; |
| 5521 | attr->ns = n; |
| 5522 | } |
| 5523 | } |
| 5524 | } |
| 5525 | attr = attr->next; |
| 5526 | } |
| 5527 | |
| 5528 | /* |
| 5529 | * Browse the full subtree, deep first |
| 5530 | */ |
| 5531 | if (node->children != NULL) { |
| 5532 | /* deep first */ |
| 5533 | node = node->children; |
| 5534 | } else if ((node != tree) && (node->next != NULL)) { |
| 5535 | /* then siblings */ |
| 5536 | node = node->next; |
| 5537 | } else if (node != tree) { |
| 5538 | /* go up to parents->next if needed */ |
| 5539 | while (node != tree) { |
| 5540 | if (node->parent != NULL) |
| 5541 | node = node->parent; |
| 5542 | if ((node != tree) && (node->next != NULL)) { |
| 5543 | node = node->next; |
| 5544 | break; |
| 5545 | } |
| 5546 | if (node->parent == NULL) { |
| 5547 | node = NULL; |
| 5548 | break; |
| 5549 | } |
| 5550 | } |
| 5551 | /* exit condition */ |
| 5552 | if (node == tree) |
| 5553 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 5554 | } else |
| 5555 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5556 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5557 | if (oldNs != NULL) |
| 5558 | xmlFree(oldNs); |
| 5559 | if (newNs != NULL) |
| 5560 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5561 | return(ret); |
| 5562 | } |
| 5563 | |
| 5564 | /** |
| 5565 | * xmlHasProp: |
| 5566 | * @node: the node |
| 5567 | * @name: the attribute name |
| 5568 | * |
| 5569 | * Search an attribute associated to a node |
| 5570 | * This function also looks in DTD attribute declaration for #FIXED or |
| 5571 | * default declaration values unless DTD use has been turned off. |
| 5572 | * |
| 5573 | * Returns the attribute or the attribute declaration or NULL if |
| 5574 | * neither was found. |
| 5575 | */ |
| 5576 | xmlAttrPtr |
| 5577 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 5578 | xmlAttrPtr prop; |
| 5579 | xmlDocPtr doc; |
| 5580 | |
| 5581 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 5582 | /* |
| 5583 | * Check on the properties attached to the node |
| 5584 | */ |
| 5585 | prop = node->properties; |
| 5586 | while (prop != NULL) { |
| 5587 | if (xmlStrEqual(prop->name, name)) { |
| 5588 | return(prop); |
| 5589 | } |
| 5590 | prop = prop->next; |
| 5591 | } |
| 5592 | if (!xmlCheckDTD) return(NULL); |
| 5593 | |
| 5594 | /* |
| 5595 | * Check if there is a default declaration in the internal |
| 5596 | * or external subsets |
| 5597 | */ |
| 5598 | doc = node->doc; |
| 5599 | if (doc != NULL) { |
| 5600 | xmlAttributePtr attrDecl; |
| 5601 | if (doc->intSubset != NULL) { |
| 5602 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5603 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5604 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5605 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5606 | /* return attribute declaration only if a default value is given |
| 5607 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5608 | return((xmlAttrPtr) attrDecl); |
| 5609 | } |
| 5610 | } |
| 5611 | return(NULL); |
| 5612 | } |
| 5613 | |
| 5614 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5615 | * xmlHasNsProp: |
| 5616 | * @node: the node |
| 5617 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5618 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5619 | * |
| 5620 | * Search for an attribute associated to a node |
| 5621 | * This attribute has to be anchored in the namespace specified. |
| 5622 | * This does the entity substitution. |
| 5623 | * This function looks in DTD attribute declaration for #FIXED or |
| 5624 | * default declaration values unless DTD use has been turned off. |
| 5625 | * |
| 5626 | * Returns the attribute or the attribute declaration or NULL |
| 5627 | * if neither was found. |
| 5628 | */ |
| 5629 | xmlAttrPtr |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5630 | xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5631 | xmlAttrPtr prop; |
| 5632 | xmlDocPtr doc; |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5633 | |
| 5634 | if (node == NULL) |
| 5635 | return(NULL); |
| 5636 | |
| 5637 | prop = node->properties; |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5638 | if (nameSpace == NULL) |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5639 | return(xmlHasProp(node, name)); |
| 5640 | while (prop != NULL) { |
| 5641 | /* |
| 5642 | * One need to have |
| 5643 | * - same attribute names |
| 5644 | * - and the attribute carrying that namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5645 | */ |
| 5646 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | e3c81b5 | 2001-06-17 14:50:34 +0000 | [diff] [blame] | 5647 | ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, nameSpace)))) { |
| 5648 | return(prop); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5649 | } |
| 5650 | prop = prop->next; |
| 5651 | } |
| 5652 | if (!xmlCheckDTD) return(NULL); |
| 5653 | |
| 5654 | /* |
| 5655 | * Check if there is a default declaration in the internal |
| 5656 | * or external subsets |
| 5657 | */ |
| 5658 | doc = node->doc; |
| 5659 | if (doc != NULL) { |
| 5660 | if (doc->intSubset != NULL) { |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5661 | xmlAttributePtr attrDecl = NULL; |
| 5662 | xmlNsPtr *nsList, *cur; |
| 5663 | xmlChar *ename; |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5664 | |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5665 | nsList = xmlGetNsList(node->doc, node); |
| 5666 | if (nsList == NULL) |
| 5667 | return(NULL); |
| 5668 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 5669 | ename = xmlStrdup(node->ns->prefix); |
| 5670 | ename = xmlStrcat(ename, BAD_CAST ":"); |
| 5671 | ename = xmlStrcat(ename, node->name); |
| 5672 | } else { |
| 5673 | ename = xmlStrdup(node->name); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5674 | } |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5675 | if (ename == NULL) { |
| 5676 | xmlFree(nsList); |
| 5677 | return(NULL); |
| 5678 | } |
| 5679 | |
| 5680 | cur = nsList; |
| 5681 | while (*cur != NULL) { |
| 5682 | if (xmlStrEqual((*cur)->href, nameSpace)) { |
| 5683 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename, |
| 5684 | name, (*cur)->prefix); |
| 5685 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5686 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename, |
| 5687 | name, (*cur)->prefix); |
| 5688 | } |
| 5689 | cur++; |
| 5690 | } |
| 5691 | xmlFree(nsList); |
| 5692 | xmlFree(ename); |
| 5693 | return((xmlAttrPtr) attrDecl); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5694 | } |
| 5695 | } |
| 5696 | return(NULL); |
| 5697 | } |
| 5698 | |
| 5699 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5700 | * xmlGetProp: |
| 5701 | * @node: the node |
| 5702 | * @name: the attribute name |
| 5703 | * |
| 5704 | * Search and get the value of an attribute associated to a node |
| 5705 | * This does the entity substitution. |
| 5706 | * This function looks in DTD attribute declaration for #FIXED or |
| 5707 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 5708 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5709 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 5710 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5711 | * |
| 5712 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5713 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5714 | */ |
| 5715 | xmlChar * |
| 5716 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
| 5717 | xmlAttrPtr prop; |
| 5718 | xmlDocPtr doc; |
| 5719 | |
| 5720 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 5721 | /* |
| 5722 | * Check on the properties attached to the node |
| 5723 | */ |
| 5724 | prop = node->properties; |
| 5725 | while (prop != NULL) { |
| 5726 | if (xmlStrEqual(prop->name, name)) { |
| 5727 | xmlChar *ret; |
| 5728 | |
| 5729 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 5730 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 5731 | return(ret); |
| 5732 | } |
| 5733 | prop = prop->next; |
| 5734 | } |
| 5735 | if (!xmlCheckDTD) return(NULL); |
| 5736 | |
| 5737 | /* |
| 5738 | * Check if there is a default declaration in the internal |
| 5739 | * or external subsets |
| 5740 | */ |
| 5741 | doc = node->doc; |
| 5742 | if (doc != NULL) { |
| 5743 | xmlAttributePtr attrDecl; |
| 5744 | if (doc->intSubset != NULL) { |
| 5745 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5746 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5747 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5748 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5749 | /* return attribute declaration only if a default value is given |
| 5750 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5751 | return(xmlStrdup(attrDecl->defaultValue)); |
| 5752 | } |
| 5753 | } |
| 5754 | return(NULL); |
| 5755 | } |
| 5756 | |
| 5757 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5758 | * xmlGetNoNsProp: |
| 5759 | * @node: the node |
| 5760 | * @name: the attribute name |
| 5761 | * |
| 5762 | * Search and get the value of an attribute associated to a node |
| 5763 | * This does the entity substitution. |
| 5764 | * This function looks in DTD attribute declaration for #FIXED or |
| 5765 | * default declaration values unless DTD use has been turned off. |
| 5766 | * This function is similar to xmlGetProp except it will accept only |
| 5767 | * an attribute in no namespace. |
| 5768 | * |
| 5769 | * Returns the attribute value or NULL if not found. |
| 5770 | * It's up to the caller to free the memory with xmlFree(). |
| 5771 | */ |
| 5772 | xmlChar * |
| 5773 | xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) { |
| 5774 | xmlAttrPtr prop; |
| 5775 | xmlDocPtr doc; |
| 5776 | |
| 5777 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 5778 | /* |
| 5779 | * Check on the properties attached to the node |
| 5780 | */ |
| 5781 | prop = node->properties; |
| 5782 | while (prop != NULL) { |
| 5783 | if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) { |
| 5784 | xmlChar *ret; |
| 5785 | |
| 5786 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 5787 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 5788 | return(ret); |
| 5789 | } |
| 5790 | prop = prop->next; |
| 5791 | } |
| 5792 | if (!xmlCheckDTD) return(NULL); |
| 5793 | |
| 5794 | /* |
| 5795 | * Check if there is a default declaration in the internal |
| 5796 | * or external subsets |
| 5797 | */ |
| 5798 | doc = node->doc; |
| 5799 | if (doc != NULL) { |
| 5800 | xmlAttributePtr attrDecl; |
| 5801 | if (doc->intSubset != NULL) { |
| 5802 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5803 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5804 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5805 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5806 | /* return attribute declaration only if a default value is given |
| 5807 | (that includes #FIXED declarations) */ |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5808 | return(xmlStrdup(attrDecl->defaultValue)); |
| 5809 | } |
| 5810 | } |
| 5811 | return(NULL); |
| 5812 | } |
| 5813 | |
| 5814 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5815 | * xmlGetNsProp: |
| 5816 | * @node: the node |
| 5817 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5818 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5819 | * |
| 5820 | * Search and get the value of an attribute associated to a node |
| 5821 | * This attribute has to be anchored in the namespace specified. |
| 5822 | * This does the entity substitution. |
| 5823 | * This function looks in DTD attribute declaration for #FIXED or |
| 5824 | * default declaration values unless DTD use has been turned off. |
| 5825 | * |
| 5826 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5827 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5828 | */ |
| 5829 | xmlChar * |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5830 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5831 | xmlAttrPtr prop; |
| 5832 | xmlDocPtr doc; |
| 5833 | xmlNsPtr ns; |
| 5834 | |
| 5835 | if (node == NULL) |
| 5836 | return(NULL); |
| 5837 | |
| 5838 | prop = node->properties; |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5839 | if (nameSpace == NULL) |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5840 | return(xmlGetNoNsProp(node, name)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5841 | while (prop != NULL) { |
| 5842 | /* |
| 5843 | * One need to have |
| 5844 | * - same attribute names |
| 5845 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5846 | */ |
| 5847 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | e8fc08e | 2001-06-07 19:35:47 +0000 | [diff] [blame] | 5848 | ((prop->ns != NULL) && |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5849 | (xmlStrEqual(prop->ns->href, nameSpace)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5850 | xmlChar *ret; |
| 5851 | |
| 5852 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 5853 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 5854 | return(ret); |
| 5855 | } |
| 5856 | prop = prop->next; |
| 5857 | } |
| 5858 | if (!xmlCheckDTD) return(NULL); |
| 5859 | |
| 5860 | /* |
| 5861 | * Check if there is a default declaration in the internal |
| 5862 | * or external subsets |
| 5863 | */ |
| 5864 | doc = node->doc; |
| 5865 | if (doc != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5866 | if (doc->intSubset != NULL) { |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 5867 | xmlAttributePtr attrDecl; |
| 5868 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5869 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5870 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5871 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 5872 | |
| 5873 | if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) { |
| 5874 | /* |
| 5875 | * The DTD declaration only allows a prefix search |
| 5876 | */ |
| 5877 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5878 | if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5879 | return(xmlStrdup(attrDecl->defaultValue)); |
| 5880 | } |
| 5881 | } |
| 5882 | } |
| 5883 | return(NULL); |
| 5884 | } |
| 5885 | |
| 5886 | /** |
| 5887 | * xmlSetProp: |
| 5888 | * @node: the node |
| 5889 | * @name: the attribute name |
| 5890 | * @value: the attribute value |
| 5891 | * |
| 5892 | * Set (or reset) an attribute carried by a node. |
| 5893 | * Returns the attribute pointer. |
| 5894 | */ |
| 5895 | xmlAttrPtr |
| 5896 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 5897 | xmlAttrPtr prop; |
| 5898 | xmlDocPtr doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5899 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 5900 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5901 | return(NULL); |
| 5902 | doc = node->doc; |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 5903 | prop = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5904 | while (prop != NULL) { |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 5905 | if ((xmlStrEqual(prop->name, name)) && |
| 5906 | (prop->ns == NULL)){ |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 5907 | xmlNodePtr oldprop = prop->children; |
| 5908 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5909 | prop->children = NULL; |
| 5910 | prop->last = NULL; |
| 5911 | if (value != NULL) { |
| 5912 | xmlChar *buffer; |
| 5913 | xmlNodePtr tmp; |
| 5914 | |
| 5915 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 5916 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 5917 | prop->last = NULL; |
| 5918 | prop->doc = doc; |
| 5919 | tmp = prop->children; |
| 5920 | while (tmp != NULL) { |
| 5921 | tmp->parent = (xmlNodePtr) prop; |
| 5922 | tmp->doc = doc; |
| 5923 | if (tmp->next == NULL) |
| 5924 | prop->last = tmp; |
| 5925 | tmp = tmp->next; |
| 5926 | } |
| 5927 | xmlFree(buffer); |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 5928 | } |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 5929 | if (oldprop != NULL) |
| 5930 | xmlFreeNodeList(oldprop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5931 | return(prop); |
| 5932 | } |
| 5933 | prop = prop->next; |
| 5934 | } |
| 5935 | prop = xmlNewProp(node, name, value); |
| 5936 | return(prop); |
| 5937 | } |
| 5938 | |
| 5939 | /** |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 5940 | * xmlUnsetProp: |
| 5941 | * @node: the node |
| 5942 | * @name: the attribute name |
| 5943 | * |
| 5944 | * Remove an attribute carried by a node. |
| 5945 | * Returns 0 if successful, -1 if not found |
| 5946 | */ |
| 5947 | int |
| 5948 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 5949 | xmlAttrPtr prop, prev = NULL;; |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 5950 | |
| 5951 | if ((node == NULL) || (name == NULL)) |
| 5952 | return(-1); |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 5953 | prop = node->properties; |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 5954 | while (prop != NULL) { |
| 5955 | if ((xmlStrEqual(prop->name, name)) && |
| 5956 | (prop->ns == NULL)) { |
| 5957 | if (prev == NULL) |
| 5958 | node->properties = prop->next; |
| 5959 | else |
| 5960 | prev->next = prop->next; |
| 5961 | xmlFreeProp(prop); |
| 5962 | return(0); |
| 5963 | } |
| 5964 | prev = prop; |
| 5965 | prop = prop->next; |
| 5966 | } |
| 5967 | return(-1); |
| 5968 | } |
| 5969 | |
| 5970 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5971 | * xmlSetNsProp: |
| 5972 | * @node: the node |
| 5973 | * @ns: the namespace definition |
| 5974 | * @name: the attribute name |
| 5975 | * @value: the attribute value |
| 5976 | * |
| 5977 | * Set (or reset) an attribute carried by a node. |
| 5978 | * The ns structure must be in scope, this is not checked. |
| 5979 | * |
| 5980 | * Returns the attribute pointer. |
| 5981 | */ |
| 5982 | xmlAttrPtr |
| 5983 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 5984 | const xmlChar *value) { |
| 5985 | xmlAttrPtr prop; |
| 5986 | |
| 5987 | if ((node == NULL) || (name == NULL)) |
| 5988 | return(NULL); |
| 5989 | |
| 5990 | if (ns == NULL) |
| 5991 | return(xmlSetProp(node, name, value)); |
| 5992 | if (ns->href == NULL) |
| 5993 | return(NULL); |
| 5994 | prop = node->properties; |
| 5995 | |
| 5996 | while (prop != NULL) { |
| 5997 | /* |
| 5998 | * One need to have |
| 5999 | * - same attribute names |
| 6000 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6001 | */ |
| 6002 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | a57c26e | 2002-08-01 12:52:24 +0000 | [diff] [blame] | 6003 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6004 | if (prop->children != NULL) |
| 6005 | xmlFreeNodeList(prop->children); |
| 6006 | prop->children = NULL; |
| 6007 | prop->last = NULL; |
| 6008 | prop->ns = ns; |
| 6009 | if (value != NULL) { |
| 6010 | xmlChar *buffer; |
| 6011 | xmlNodePtr tmp; |
| 6012 | |
| 6013 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6014 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 6015 | prop->last = NULL; |
| 6016 | tmp = prop->children; |
| 6017 | while (tmp != NULL) { |
| 6018 | tmp->parent = (xmlNodePtr) prop; |
| 6019 | if (tmp->next == NULL) |
| 6020 | prop->last = tmp; |
| 6021 | tmp = tmp->next; |
| 6022 | } |
| 6023 | xmlFree(buffer); |
| 6024 | } |
| 6025 | return(prop); |
| 6026 | } |
| 6027 | prop = prop->next; |
| 6028 | } |
| 6029 | prop = xmlNewNsProp(node, ns, name, value); |
| 6030 | return(prop); |
| 6031 | } |
| 6032 | |
| 6033 | /** |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6034 | * xmlUnsetNsProp: |
| 6035 | * @node: the node |
| 6036 | * @ns: the namespace definition |
| 6037 | * @name: the attribute name |
| 6038 | * |
| 6039 | * Remove an attribute carried by a node. |
| 6040 | * Returns 0 if successful, -1 if not found |
| 6041 | */ |
| 6042 | int |
| 6043 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
| 6044 | xmlAttrPtr prop = node->properties, prev = NULL;; |
| 6045 | |
| 6046 | if ((node == NULL) || (name == NULL)) |
| 6047 | return(-1); |
| 6048 | if (ns == NULL) |
| 6049 | return(xmlUnsetProp(node, name)); |
| 6050 | if (ns->href == NULL) |
| 6051 | return(-1); |
| 6052 | while (prop != NULL) { |
| 6053 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | 0bf2900 | 2002-08-01 12:54:11 +0000 | [diff] [blame] | 6054 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6055 | if (prev == NULL) |
| 6056 | node->properties = prop->next; |
| 6057 | else |
| 6058 | prev->next = prop->next; |
| 6059 | xmlFreeProp(prop); |
| 6060 | return(0); |
| 6061 | } |
| 6062 | prev = prop; |
| 6063 | prop = prop->next; |
| 6064 | } |
| 6065 | return(-1); |
| 6066 | } |
| 6067 | |
| 6068 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6069 | * xmlNodeIsText: |
| 6070 | * @node: the node |
| 6071 | * |
| 6072 | * Is this node a Text node ? |
| 6073 | * Returns 1 yes, 0 no |
| 6074 | */ |
| 6075 | int |
| 6076 | xmlNodeIsText(xmlNodePtr node) { |
| 6077 | if (node == NULL) return(0); |
| 6078 | |
| 6079 | if (node->type == XML_TEXT_NODE) return(1); |
| 6080 | return(0); |
| 6081 | } |
| 6082 | |
| 6083 | /** |
| 6084 | * xmlIsBlankNode: |
| 6085 | * @node: the node |
| 6086 | * |
| 6087 | * Checks whether this node is an empty or whitespace only |
| 6088 | * (and possibly ignorable) text-node. |
| 6089 | * |
| 6090 | * Returns 1 yes, 0 no |
| 6091 | */ |
| 6092 | int |
| 6093 | xmlIsBlankNode(xmlNodePtr node) { |
| 6094 | const xmlChar *cur; |
| 6095 | if (node == NULL) return(0); |
| 6096 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6097 | if ((node->type != XML_TEXT_NODE) && |
| 6098 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6099 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6100 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6101 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6102 | while (*cur != 0) { |
| 6103 | if (!IS_BLANK(*cur)) return(0); |
| 6104 | cur++; |
| 6105 | } |
| 6106 | |
| 6107 | return(1); |
| 6108 | } |
| 6109 | |
| 6110 | /** |
| 6111 | * xmlTextConcat: |
| 6112 | * @node: the node |
| 6113 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6114 | * @len: @content length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6115 | * |
| 6116 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6117 | * |
| 6118 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6119 | */ |
| 6120 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6121 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6122 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6123 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6124 | |
| 6125 | if ((node->type != XML_TEXT_NODE) && |
| 6126 | (node->type != XML_CDATA_SECTION_NODE)) { |
| 6127 | #ifdef DEBUG_TREE |
| 6128 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6129 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6130 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6131 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6132 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6133 | node->content = xmlStrncat(node->content, content, len); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6134 | if (node->content == NULL) |
| 6135 | return(-1); |
| 6136 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6137 | } |
| 6138 | |
| 6139 | /************************************************************************ |
| 6140 | * * |
| 6141 | * Output : to a FILE or in memory * |
| 6142 | * * |
| 6143 | ************************************************************************/ |
| 6144 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6145 | /** |
| 6146 | * xmlBufferCreate: |
| 6147 | * |
| 6148 | * routine to create an XML buffer. |
| 6149 | * returns the new structure. |
| 6150 | */ |
| 6151 | xmlBufferPtr |
| 6152 | xmlBufferCreate(void) { |
| 6153 | xmlBufferPtr ret; |
| 6154 | |
| 6155 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6156 | if (ret == NULL) { |
| 6157 | xmlGenericError(xmlGenericErrorContext, |
| 6158 | "xmlBufferCreate : out of memory!\n"); |
| 6159 | return(NULL); |
| 6160 | } |
| 6161 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 6162 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6163 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6164 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6165 | if (ret->content == NULL) { |
| 6166 | xmlGenericError(xmlGenericErrorContext, |
| 6167 | "xmlBufferCreate : out of memory!\n"); |
| 6168 | xmlFree(ret); |
| 6169 | return(NULL); |
| 6170 | } |
| 6171 | ret->content[0] = 0; |
| 6172 | return(ret); |
| 6173 | } |
| 6174 | |
| 6175 | /** |
| 6176 | * xmlBufferCreateSize: |
| 6177 | * @size: initial size of buffer |
| 6178 | * |
| 6179 | * routine to create an XML buffer. |
| 6180 | * returns the new structure. |
| 6181 | */ |
| 6182 | xmlBufferPtr |
| 6183 | xmlBufferCreateSize(size_t size) { |
| 6184 | xmlBufferPtr ret; |
| 6185 | |
| 6186 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6187 | if (ret == NULL) { |
| 6188 | xmlGenericError(xmlGenericErrorContext, |
| 6189 | "xmlBufferCreate : out of memory!\n"); |
| 6190 | return(NULL); |
| 6191 | } |
| 6192 | ret->use = 0; |
| 6193 | ret->alloc = xmlBufferAllocScheme; |
| 6194 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 6195 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6196 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6197 | if (ret->content == NULL) { |
| 6198 | xmlGenericError(xmlGenericErrorContext, |
| 6199 | "xmlBufferCreate : out of memory!\n"); |
| 6200 | xmlFree(ret); |
| 6201 | return(NULL); |
| 6202 | } |
| 6203 | ret->content[0] = 0; |
| 6204 | } else |
| 6205 | ret->content = NULL; |
| 6206 | return(ret); |
| 6207 | } |
| 6208 | |
| 6209 | /** |
| 6210 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6211 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6212 | * @scheme: allocation scheme to use |
| 6213 | * |
| 6214 | * Sets the allocation scheme for this buffer |
| 6215 | */ |
| 6216 | void |
| 6217 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 6218 | xmlBufferAllocationScheme scheme) { |
| 6219 | if (buf == NULL) { |
| 6220 | #ifdef DEBUG_BUFFER |
| 6221 | xmlGenericError(xmlGenericErrorContext, |
| 6222 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 6223 | #endif |
| 6224 | return; |
| 6225 | } |
| 6226 | |
| 6227 | buf->alloc = scheme; |
| 6228 | } |
| 6229 | |
| 6230 | /** |
| 6231 | * xmlBufferFree: |
| 6232 | * @buf: the buffer to free |
| 6233 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 6234 | * Frees an XML buffer. It frees both the content and the structure which |
| 6235 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6236 | */ |
| 6237 | void |
| 6238 | xmlBufferFree(xmlBufferPtr buf) { |
| 6239 | if (buf == NULL) { |
| 6240 | #ifdef DEBUG_BUFFER |
| 6241 | xmlGenericError(xmlGenericErrorContext, |
| 6242 | "xmlBufferFree: buf == NULL\n"); |
| 6243 | #endif |
| 6244 | return; |
| 6245 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 6246 | if (buf->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6247 | xmlFree(buf->content); |
| 6248 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6249 | xmlFree(buf); |
| 6250 | } |
| 6251 | |
| 6252 | /** |
| 6253 | * xmlBufferEmpty: |
| 6254 | * @buf: the buffer |
| 6255 | * |
| 6256 | * empty a buffer. |
| 6257 | */ |
| 6258 | void |
| 6259 | xmlBufferEmpty(xmlBufferPtr buf) { |
| 6260 | if (buf->content == NULL) return; |
| 6261 | buf->use = 0; |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 6262 | memset(buf->content, 0, buf->size); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6263 | } |
| 6264 | |
| 6265 | /** |
| 6266 | * xmlBufferShrink: |
| 6267 | * @buf: the buffer to dump |
| 6268 | * @len: the number of xmlChar to remove |
| 6269 | * |
| 6270 | * Remove the beginning of an XML buffer. |
| 6271 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6272 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6273 | */ |
| 6274 | int |
| 6275 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
| 6276 | if (len == 0) return(0); |
| 6277 | if (len > buf->use) return(-1); |
| 6278 | |
| 6279 | buf->use -= len; |
| 6280 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
| 6281 | |
| 6282 | buf->content[buf->use] = 0; |
| 6283 | return(len); |
| 6284 | } |
| 6285 | |
| 6286 | /** |
| 6287 | * xmlBufferGrow: |
| 6288 | * @buf: the buffer |
| 6289 | * @len: the minimum free size to allocate |
| 6290 | * |
| 6291 | * Grow the available space of an XML buffer. |
| 6292 | * |
| 6293 | * Returns the new available space or -1 in case of error |
| 6294 | */ |
| 6295 | int |
| 6296 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 6297 | int size; |
| 6298 | xmlChar *newbuf; |
| 6299 | |
| 6300 | if (len + buf->use < buf->size) return(0); |
| 6301 | |
| 6302 | size = buf->use + len + 100; |
| 6303 | |
| 6304 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
| 6305 | if (newbuf == NULL) return(-1); |
| 6306 | buf->content = newbuf; |
| 6307 | buf->size = size; |
| 6308 | return(buf->size - buf->use); |
| 6309 | } |
| 6310 | |
| 6311 | /** |
| 6312 | * xmlBufferDump: |
| 6313 | * @file: the file output |
| 6314 | * @buf: the buffer to dump |
| 6315 | * |
| 6316 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6317 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6318 | */ |
| 6319 | int |
| 6320 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 6321 | int ret; |
| 6322 | |
| 6323 | if (buf == NULL) { |
| 6324 | #ifdef DEBUG_BUFFER |
| 6325 | xmlGenericError(xmlGenericErrorContext, |
| 6326 | "xmlBufferDump: buf == NULL\n"); |
| 6327 | #endif |
| 6328 | return(0); |
| 6329 | } |
| 6330 | if (buf->content == NULL) { |
| 6331 | #ifdef DEBUG_BUFFER |
| 6332 | xmlGenericError(xmlGenericErrorContext, |
| 6333 | "xmlBufferDump: buf->content == NULL\n"); |
| 6334 | #endif |
| 6335 | return(0); |
| 6336 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 6337 | if (file == NULL) |
| 6338 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6339 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 6340 | return(ret); |
| 6341 | } |
| 6342 | |
| 6343 | /** |
| 6344 | * xmlBufferContent: |
| 6345 | * @buf: the buffer |
| 6346 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6347 | * Function to extract the content of a buffer |
| 6348 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6349 | * Returns the internal content |
| 6350 | */ |
| 6351 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6352 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6353 | xmlBufferContent(const xmlBufferPtr buf) |
| 6354 | { |
| 6355 | if(!buf) |
| 6356 | return NULL; |
| 6357 | |
| 6358 | return buf->content; |
| 6359 | } |
| 6360 | |
| 6361 | /** |
| 6362 | * xmlBufferLength: |
| 6363 | * @buf: the buffer |
| 6364 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6365 | * Function to get the length of a buffer |
| 6366 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6367 | * Returns the length of data in the internal content |
| 6368 | */ |
| 6369 | |
| 6370 | int |
| 6371 | xmlBufferLength(const xmlBufferPtr buf) |
| 6372 | { |
| 6373 | if(!buf) |
| 6374 | return 0; |
| 6375 | |
| 6376 | return buf->use; |
| 6377 | } |
| 6378 | |
| 6379 | /** |
| 6380 | * xmlBufferResize: |
| 6381 | * @buf: the buffer to resize |
| 6382 | * @size: the desired size |
| 6383 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6384 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6385 | * |
| 6386 | * Returns 0 in case of problems, 1 otherwise |
| 6387 | */ |
| 6388 | int |
| 6389 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 6390 | { |
| 6391 | unsigned int newSize; |
| 6392 | xmlChar* rebuf = NULL; |
| 6393 | |
| 6394 | /*take care of empty case*/ |
| 6395 | newSize = (buf->size ? buf->size*2 : size); |
| 6396 | |
| 6397 | /* Don't resize if we don't have to */ |
| 6398 | if (size < buf->size) |
| 6399 | return 1; |
| 6400 | |
| 6401 | /* figure out new size */ |
| 6402 | switch (buf->alloc){ |
| 6403 | case XML_BUFFER_ALLOC_DOUBLEIT: |
| 6404 | while (size > newSize) newSize *= 2; |
| 6405 | break; |
| 6406 | case XML_BUFFER_ALLOC_EXACT: |
| 6407 | newSize = size+10; |
| 6408 | break; |
| 6409 | default: |
| 6410 | newSize = size+10; |
| 6411 | break; |
| 6412 | } |
| 6413 | |
| 6414 | if (buf->content == NULL) |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6415 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame^] | 6416 | else if (buf->size - buf->use < 100) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6417 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 6418 | newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame^] | 6419 | } else { |
| 6420 | /* |
| 6421 | * if we are reallocating a buffer far from being full, it's |
| 6422 | * better to make a new allocation and copy only the used range |
| 6423 | * and free the old one. |
| 6424 | */ |
| 6425 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
| 6426 | if (rebuf != NULL) { |
| 6427 | memcpy(rebuf, buf->content, buf->use); |
| 6428 | xmlFree(buf->content); |
| 6429 | } |
| 6430 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6431 | if (rebuf == NULL) { |
| 6432 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6433 | "xmlBufferResize : out of memory!\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6434 | return 0; |
| 6435 | } |
| 6436 | buf->content = rebuf; |
| 6437 | buf->size = newSize; |
| 6438 | |
| 6439 | return 1; |
| 6440 | } |
| 6441 | |
| 6442 | /** |
| 6443 | * xmlBufferAdd: |
| 6444 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6445 | * @str: the #xmlChar string |
| 6446 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6447 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6448 | * 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] | 6449 | * str is recomputed. |
| 6450 | */ |
| 6451 | void |
| 6452 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6453 | unsigned int needSize; |
| 6454 | |
| 6455 | if (str == NULL) { |
| 6456 | #ifdef DEBUG_BUFFER |
| 6457 | xmlGenericError(xmlGenericErrorContext, |
| 6458 | "xmlBufferAdd: str == NULL\n"); |
| 6459 | #endif |
| 6460 | return; |
| 6461 | } |
| 6462 | if (len < -1) { |
| 6463 | #ifdef DEBUG_BUFFER |
| 6464 | xmlGenericError(xmlGenericErrorContext, |
| 6465 | "xmlBufferAdd: len < 0\n"); |
| 6466 | #endif |
| 6467 | return; |
| 6468 | } |
| 6469 | if (len == 0) return; |
| 6470 | |
| 6471 | if (len < 0) |
| 6472 | len = xmlStrlen(str); |
| 6473 | |
| 6474 | if (len <= 0) return; |
| 6475 | |
| 6476 | needSize = buf->use + len + 2; |
| 6477 | if (needSize > buf->size){ |
| 6478 | if (!xmlBufferResize(buf, needSize)){ |
| 6479 | xmlGenericError(xmlGenericErrorContext, |
| 6480 | "xmlBufferAdd : out of memory!\n"); |
| 6481 | return; |
| 6482 | } |
| 6483 | } |
| 6484 | |
| 6485 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 6486 | buf->use += len; |
| 6487 | buf->content[buf->use] = 0; |
| 6488 | } |
| 6489 | |
| 6490 | /** |
| 6491 | * xmlBufferAddHead: |
| 6492 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6493 | * @str: the #xmlChar string |
| 6494 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6495 | * |
| 6496 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6497 | * if len == -1, the length of @str is recomputed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6498 | */ |
| 6499 | void |
| 6500 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6501 | unsigned int needSize; |
| 6502 | |
| 6503 | if (str == NULL) { |
| 6504 | #ifdef DEBUG_BUFFER |
| 6505 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6506 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6507 | #endif |
| 6508 | return; |
| 6509 | } |
| 6510 | if (len < -1) { |
| 6511 | #ifdef DEBUG_BUFFER |
| 6512 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6513 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6514 | #endif |
| 6515 | return; |
| 6516 | } |
| 6517 | if (len == 0) return; |
| 6518 | |
| 6519 | if (len < 0) |
| 6520 | len = xmlStrlen(str); |
| 6521 | |
| 6522 | if (len <= 0) return; |
| 6523 | |
| 6524 | needSize = buf->use + len + 2; |
| 6525 | if (needSize > buf->size){ |
| 6526 | if (!xmlBufferResize(buf, needSize)){ |
| 6527 | xmlGenericError(xmlGenericErrorContext, |
| 6528 | "xmlBufferAddHead : out of memory!\n"); |
| 6529 | return; |
| 6530 | } |
| 6531 | } |
| 6532 | |
| 6533 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 6534 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 6535 | buf->use += len; |
| 6536 | buf->content[buf->use] = 0; |
| 6537 | } |
| 6538 | |
| 6539 | /** |
| 6540 | * xmlBufferCat: |
| 6541 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6542 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6543 | * |
| 6544 | * Append a zero terminated string to an XML buffer. |
| 6545 | */ |
| 6546 | void |
| 6547 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
| 6548 | if (str != NULL) |
| 6549 | xmlBufferAdd(buf, str, -1); |
| 6550 | } |
| 6551 | |
| 6552 | /** |
| 6553 | * xmlBufferCCat: |
| 6554 | * @buf: the buffer to dump |
| 6555 | * @str: the C char string |
| 6556 | * |
| 6557 | * Append a zero terminated C string to an XML buffer. |
| 6558 | */ |
| 6559 | void |
| 6560 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 6561 | const char *cur; |
| 6562 | |
| 6563 | if (str == NULL) { |
| 6564 | #ifdef DEBUG_BUFFER |
| 6565 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6566 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6567 | #endif |
| 6568 | return; |
| 6569 | } |
| 6570 | for (cur = str;*cur != 0;cur++) { |
| 6571 | if (buf->use + 10 >= buf->size) { |
| 6572 | if (!xmlBufferResize(buf, buf->use+10)){ |
| 6573 | xmlGenericError(xmlGenericErrorContext, |
| 6574 | "xmlBufferCCat : out of memory!\n"); |
| 6575 | return; |
| 6576 | } |
| 6577 | } |
| 6578 | buf->content[buf->use++] = *cur; |
| 6579 | } |
| 6580 | buf->content[buf->use] = 0; |
| 6581 | } |
| 6582 | |
| 6583 | /** |
| 6584 | * xmlBufferWriteCHAR: |
| 6585 | * @buf: the XML buffer |
| 6586 | * @string: the string to add |
| 6587 | * |
| 6588 | * routine which manages and grows an output buffer. This one adds |
| 6589 | * xmlChars at the end of the buffer. |
| 6590 | */ |
| 6591 | void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6592 | xmlBufferWriteCHAR |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6593 | (xmlBufferPtr buf, const xmlChar *string) { |
| 6594 | xmlBufferCat(buf, string); |
| 6595 | } |
| 6596 | |
| 6597 | /** |
| 6598 | * xmlBufferWriteChar: |
| 6599 | * @buf: the XML buffer output |
| 6600 | * @string: the string to add |
| 6601 | * |
| 6602 | * routine which manage and grows an output buffer. This one add |
| 6603 | * C chars at the end of the array. |
| 6604 | */ |
| 6605 | void |
| 6606 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
| 6607 | xmlBufferCCat(buf, string); |
| 6608 | } |
| 6609 | |
| 6610 | |
| 6611 | /** |
| 6612 | * xmlBufferWriteQuotedString: |
| 6613 | * @buf: the XML buffer output |
| 6614 | * @string: the string to add |
| 6615 | * |
| 6616 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6617 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6618 | * quote or double-quotes internally |
| 6619 | */ |
| 6620 | void |
| 6621 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6622 | const xmlChar *cur, *base; |
| 6623 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 6624 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6625 | #ifdef DEBUG_BUFFER |
| 6626 | xmlGenericError(xmlGenericErrorContext, |
| 6627 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 6628 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6629 | xmlBufferCCat(buf, "\""); |
| 6630 | base = cur = string; |
| 6631 | while(*cur != 0){ |
| 6632 | if(*cur == '"'){ |
| 6633 | if (base != cur) |
| 6634 | xmlBufferAdd(buf, base, cur - base); |
| 6635 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 6636 | cur++; |
| 6637 | base = cur; |
| 6638 | } |
| 6639 | else { |
| 6640 | cur++; |
| 6641 | } |
| 6642 | } |
| 6643 | if (base != cur) |
| 6644 | xmlBufferAdd(buf, base, cur - base); |
| 6645 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6646 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6647 | else{ |
| 6648 | xmlBufferCCat(buf, "\'"); |
| 6649 | xmlBufferCat(buf, string); |
| 6650 | xmlBufferCCat(buf, "\'"); |
| 6651 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6652 | } else { |
| 6653 | xmlBufferCCat(buf, "\""); |
| 6654 | xmlBufferCat(buf, string); |
| 6655 | xmlBufferCCat(buf, "\""); |
| 6656 | } |
| 6657 | } |
| 6658 | |
| 6659 | |
| 6660 | /************************************************************************ |
| 6661 | * * |
| 6662 | * Dumping XML tree content to a simple buffer * |
| 6663 | * * |
| 6664 | ************************************************************************/ |
| 6665 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6666 | /** |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6667 | * xmlAttrSerializeContent: |
| 6668 | * @buf: the XML buffer output |
| 6669 | * @doc: the document |
| 6670 | * @attr: the attribute pointer |
| 6671 | * |
| 6672 | * Serialize the attribute in the buffer |
| 6673 | */ |
| 6674 | static void |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6675 | xmlAttrSerializeContent(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr attr) |
| 6676 | { |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6677 | const xmlChar *cur, *base; |
| 6678 | xmlNodePtr children; |
| 6679 | |
| 6680 | children = attr->children; |
| 6681 | while (children != NULL) { |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6682 | switch (children->type) { |
| 6683 | case XML_TEXT_NODE: |
| 6684 | base = cur = children->content; |
| 6685 | while (*cur != 0) { |
| 6686 | if (*cur == '\n') { |
| 6687 | if (base != cur) |
| 6688 | xmlBufferAdd(buf, base, cur - base); |
| 6689 | xmlBufferAdd(buf, BAD_CAST " ", 5); |
| 6690 | cur++; |
| 6691 | base = cur; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 6692 | } else if (*cur == '\r') { |
| 6693 | if (base != cur) |
| 6694 | xmlBufferAdd(buf, base, cur - base); |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 6695 | xmlBufferAdd(buf, BAD_CAST " ", 5); |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 6696 | cur++; |
| 6697 | base = cur; |
| 6698 | } else if (*cur == '\t') { |
| 6699 | if (base != cur) |
| 6700 | xmlBufferAdd(buf, base, cur - base); |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 6701 | xmlBufferAdd(buf, BAD_CAST "	", 4); |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 6702 | cur++; |
| 6703 | base = cur; |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6704 | #if 0 |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6705 | } else if (*cur == '\'') { |
| 6706 | if (base != cur) |
| 6707 | xmlBufferAdd(buf, base, cur - base); |
| 6708 | xmlBufferAdd(buf, BAD_CAST "'", 6); |
| 6709 | cur++; |
| 6710 | base = cur; |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6711 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6712 | } else if (*cur == '"') { |
| 6713 | if (base != cur) |
| 6714 | xmlBufferAdd(buf, base, cur - base); |
| 6715 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 6716 | cur++; |
| 6717 | base = cur; |
| 6718 | } else if (*cur == '<') { |
| 6719 | if (base != cur) |
| 6720 | xmlBufferAdd(buf, base, cur - base); |
| 6721 | xmlBufferAdd(buf, BAD_CAST "<", 4); |
| 6722 | cur++; |
| 6723 | base = cur; |
| 6724 | } else if (*cur == '>') { |
| 6725 | if (base != cur) |
| 6726 | xmlBufferAdd(buf, base, cur - base); |
| 6727 | xmlBufferAdd(buf, BAD_CAST ">", 4); |
| 6728 | cur++; |
| 6729 | base = cur; |
| 6730 | } else if (*cur == '&') { |
| 6731 | if (base != cur) |
| 6732 | xmlBufferAdd(buf, base, cur - base); |
| 6733 | xmlBufferAdd(buf, BAD_CAST "&", 5); |
| 6734 | cur++; |
| 6735 | base = cur; |
| 6736 | } else if ((*cur >= 0x80) && ((doc == NULL) || |
| 6737 | (doc->encoding == |
| 6738 | NULL))) { |
| 6739 | /* |
| 6740 | * We assume we have UTF-8 content. |
| 6741 | */ |
| 6742 | char tmp[10]; |
| 6743 | int val = 0, l = 1; |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6744 | |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6745 | if (base != cur) |
| 6746 | xmlBufferAdd(buf, base, cur - base); |
| 6747 | if (*cur < 0xC0) { |
| 6748 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6749 | "xmlAttrSerializeContent : input not UTF-8\n"); |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6750 | if (doc != NULL) |
| 6751 | doc->encoding = |
| 6752 | xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 6753 | snprintf(tmp, sizeof(tmp), "&#%d;", *cur); |
| 6754 | tmp[sizeof(tmp) - 1] = 0; |
| 6755 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 6756 | cur++; |
| 6757 | base = cur; |
| 6758 | continue; |
| 6759 | } else if (*cur < 0xE0) { |
| 6760 | val = (cur[0]) & 0x1F; |
| 6761 | val <<= 6; |
| 6762 | val |= (cur[1]) & 0x3F; |
| 6763 | l = 2; |
| 6764 | } else if (*cur < 0xF0) { |
| 6765 | val = (cur[0]) & 0x0F; |
| 6766 | val <<= 6; |
| 6767 | val |= (cur[1]) & 0x3F; |
| 6768 | val <<= 6; |
| 6769 | val |= (cur[2]) & 0x3F; |
| 6770 | l = 3; |
| 6771 | } else if (*cur < 0xF8) { |
| 6772 | val = (cur[0]) & 0x07; |
| 6773 | val <<= 6; |
| 6774 | val |= (cur[1]) & 0x3F; |
| 6775 | val <<= 6; |
| 6776 | val |= (cur[2]) & 0x3F; |
| 6777 | val <<= 6; |
| 6778 | val |= (cur[3]) & 0x3F; |
| 6779 | l = 4; |
| 6780 | } |
| 6781 | if ((l == 1) || (!IS_CHAR(val))) { |
| 6782 | xmlGenericError(xmlGenericErrorContext, |
| 6783 | "xmlAttrSerializeContent : char out of range\n"); |
| 6784 | if (doc != NULL) |
| 6785 | doc->encoding = |
| 6786 | xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 6787 | snprintf(tmp, sizeof(tmp), "&#%d;", *cur); |
| 6788 | tmp[sizeof(tmp) - 1] = 0; |
| 6789 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 6790 | cur++; |
| 6791 | base = cur; |
| 6792 | continue; |
| 6793 | } |
| 6794 | /* |
| 6795 | * We could do multiple things here. Just save |
| 6796 | * as a char ref |
| 6797 | */ |
| 6798 | snprintf(tmp, sizeof(tmp), "&#x%X;", val); |
| 6799 | tmp[sizeof(tmp) - 1] = 0; |
| 6800 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 6801 | cur += l; |
| 6802 | base = cur; |
| 6803 | } else { |
| 6804 | cur++; |
| 6805 | } |
| 6806 | } |
| 6807 | if (base != cur) |
| 6808 | xmlBufferAdd(buf, base, cur - base); |
| 6809 | break; |
| 6810 | case XML_ENTITY_REF_NODE: |
| 6811 | xmlBufferAdd(buf, BAD_CAST "&", 1); |
| 6812 | xmlBufferAdd(buf, children->name, |
| 6813 | xmlStrlen(children->name)); |
| 6814 | xmlBufferAdd(buf, BAD_CAST ";", 1); |
| 6815 | break; |
| 6816 | default: |
| 6817 | /* should not happen unless we have a badly built tree */ |
| 6818 | break; |
| 6819 | } |
| 6820 | children = children->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6821 | } |
| 6822 | } |
| 6823 | |
| 6824 | /** |
| 6825 | * xmlNodeDump: |
| 6826 | * @buf: the XML buffer output |
| 6827 | * @doc: the document |
| 6828 | * @cur: the current node |
| 6829 | * @level: the imbrication level for indenting |
| 6830 | * @format: is formatting allowed |
| 6831 | * |
| 6832 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 6833 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 6834 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6835 | * |
| 6836 | * 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] | 6837 | */ |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6838 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6839 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6840 | int format) |
| 6841 | { |
| 6842 | unsigned int use; |
| 6843 | int ret; |
| 6844 | xmlOutputBufferPtr outbuf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6845 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 6846 | xmlInitParser(); |
| 6847 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6848 | if (cur == NULL) { |
| 6849 | #ifdef DEBUG_TREE |
| 6850 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6851 | "xmlNodeDump : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6852 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6853 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6854 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6855 | if (buf == NULL) { |
| 6856 | #ifdef DEBUG_TREE |
| 6857 | xmlGenericError(xmlGenericErrorContext, |
| 6858 | "xmlNodeDump : buf == NULL\n"); |
| 6859 | #endif |
| 6860 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6861 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6862 | outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); |
| 6863 | if (outbuf == NULL) { |
| 6864 | xmlGenericError(xmlGenericErrorContext, |
| 6865 | "xmlNodeDump: out of memory!\n"); |
| 6866 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6867 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6868 | memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer)); |
| 6869 | outbuf->buffer = buf; |
| 6870 | outbuf->encoder = NULL; |
| 6871 | outbuf->writecallback = NULL; |
| 6872 | outbuf->closecallback = NULL; |
| 6873 | outbuf->context = NULL; |
| 6874 | outbuf->written = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6875 | |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6876 | use = buf->use; |
| 6877 | xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL); |
| 6878 | xmlFree(outbuf); |
| 6879 | ret = buf->use - use; |
| 6880 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6881 | } |
| 6882 | |
| 6883 | /** |
| 6884 | * xmlElemDump: |
| 6885 | * @f: the FILE * for the output |
| 6886 | * @doc: the document |
| 6887 | * @cur: the current node |
| 6888 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6889 | * Dump an XML/HTML node, recursive behaviour, children are printed too. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6890 | */ |
| 6891 | void |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6892 | xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur) |
| 6893 | { |
| 6894 | xmlOutputBufferPtr outbuf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6895 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 6896 | xmlInitParser(); |
| 6897 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6898 | if (cur == NULL) { |
| 6899 | #ifdef DEBUG_TREE |
| 6900 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6901 | "xmlElemDump : cur == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6902 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6903 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6904 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6905 | #ifdef DEBUG_TREE |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 6906 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6907 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6908 | "xmlElemDump : doc == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6909 | } |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 6910 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6911 | |
| 6912 | outbuf = xmlOutputBufferCreateFile(f, NULL); |
| 6913 | if (outbuf == NULL) |
| 6914 | return; |
| 6915 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6916 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6917 | htmlNodeDumpOutput(outbuf, doc, cur, NULL); |
| 6918 | #else |
| 6919 | xmlGenericError(xmlGenericErrorContext, |
| 6920 | "HTML support not compiled in\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6921 | #endif /* LIBXML_HTML_ENABLED */ |
| 6922 | } else |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6923 | xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL); |
| 6924 | xmlOutputBufferClose(outbuf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6925 | } |
| 6926 | |
| 6927 | /************************************************************************ |
| 6928 | * * |
| 6929 | * Dumping XML tree content to an I/O output buffer * |
| 6930 | * * |
| 6931 | ************************************************************************/ |
| 6932 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6933 | static void |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 6934 | xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 6935 | int level, int format, const char *encoding); |
| 6936 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6937 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 6938 | int level, int format, const char *encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 6939 | static void |
| 6940 | xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 6941 | xmlNodePtr cur, int level, int format, const char *encoding); |
| 6942 | |
Daniel Veillard | 5ecaf7f | 2003-01-09 13:19:33 +0000 | [diff] [blame] | 6943 | void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur); |
| 6944 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6945 | /** |
| 6946 | * xmlNsDumpOutput: |
| 6947 | * @buf: the XML buffer output |
| 6948 | * @cur: a namespace |
| 6949 | * |
| 6950 | * Dump a local Namespace definition. |
| 6951 | * Should be called in the context of attributes dumps. |
| 6952 | */ |
| 6953 | static void |
| 6954 | xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 6955 | if (cur == NULL) { |
| 6956 | #ifdef DEBUG_TREE |
| 6957 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6958 | "xmlNsDumpOutput : Ns == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6959 | #endif |
| 6960 | return; |
| 6961 | } |
| 6962 | if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 6963 | if (xmlStrEqual(cur->prefix, BAD_CAST "xml")) |
| 6964 | return; |
| 6965 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6966 | /* Within the context of an element attributes */ |
| 6967 | if (cur->prefix != NULL) { |
| 6968 | xmlOutputBufferWriteString(buf, " xmlns:"); |
| 6969 | xmlOutputBufferWriteString(buf, (const char *)cur->prefix); |
| 6970 | } else |
| 6971 | xmlOutputBufferWriteString(buf, " xmlns"); |
| 6972 | xmlOutputBufferWriteString(buf, "="); |
| 6973 | xmlBufferWriteQuotedString(buf->buffer, cur->href); |
| 6974 | } |
| 6975 | } |
| 6976 | |
| 6977 | /** |
| 6978 | * xmlNsListDumpOutput: |
| 6979 | * @buf: the XML buffer output |
| 6980 | * @cur: the first namespace |
| 6981 | * |
| 6982 | * Dump a list of local Namespace definitions. |
| 6983 | * Should be called in the context of attributes dumps. |
| 6984 | */ |
Daniel Veillard | 5ecaf7f | 2003-01-09 13:19:33 +0000 | [diff] [blame] | 6985 | void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6986 | xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 6987 | while (cur != NULL) { |
| 6988 | xmlNsDumpOutput(buf, cur); |
| 6989 | cur = cur->next; |
| 6990 | } |
| 6991 | } |
| 6992 | |
| 6993 | /** |
| 6994 | * xmlDtdDumpOutput: |
| 6995 | * @buf: the XML buffer output |
| 6996 | * @doc: the document |
| 6997 | * @encoding: an optional encoding string |
| 6998 | * |
| 6999 | * Dump the XML document DTD, if any. |
| 7000 | */ |
| 7001 | static void |
| 7002 | xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) { |
| 7003 | if (dtd == NULL) { |
| 7004 | #ifdef DEBUG_TREE |
| 7005 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7006 | "xmlDtdDumpOutput : no internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7007 | #endif |
| 7008 | return; |
| 7009 | } |
| 7010 | xmlOutputBufferWriteString(buf, "<!DOCTYPE "); |
| 7011 | xmlOutputBufferWriteString(buf, (const char *)dtd->name); |
| 7012 | if (dtd->ExternalID != NULL) { |
| 7013 | xmlOutputBufferWriteString(buf, " PUBLIC "); |
| 7014 | xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID); |
| 7015 | xmlOutputBufferWriteString(buf, " "); |
| 7016 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 7017 | } else if (dtd->SystemID != NULL) { |
| 7018 | xmlOutputBufferWriteString(buf, " SYSTEM "); |
| 7019 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 7020 | } |
| 7021 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 7022 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 7023 | xmlOutputBufferWriteString(buf, ">"); |
| 7024 | return; |
| 7025 | } |
| 7026 | xmlOutputBufferWriteString(buf, " [\n"); |
| 7027 | xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding); |
| 7028 | xmlOutputBufferWriteString(buf, "]>"); |
| 7029 | } |
| 7030 | |
| 7031 | /** |
| 7032 | * xmlAttrDumpOutput: |
| 7033 | * @buf: the XML buffer output |
| 7034 | * @doc: the document |
| 7035 | * @cur: the attribute pointer |
| 7036 | * @encoding: an optional encoding string |
| 7037 | * |
| 7038 | * Dump an XML attribute |
| 7039 | */ |
| 7040 | static void |
| 7041 | xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 7042 | const char *encoding ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7043 | if (cur == NULL) { |
| 7044 | #ifdef DEBUG_TREE |
| 7045 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7046 | "xmlAttrDumpOutput : property == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7047 | #endif |
| 7048 | return; |
| 7049 | } |
| 7050 | xmlOutputBufferWriteString(buf, " "); |
| 7051 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7052 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7053 | xmlOutputBufferWriteString(buf, ":"); |
| 7054 | } |
| 7055 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 7056 | xmlOutputBufferWriteString(buf, "=\""); |
| 7057 | xmlAttrSerializeContent(buf->buffer, doc, cur); |
| 7058 | xmlOutputBufferWriteString(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7059 | } |
| 7060 | |
| 7061 | /** |
| 7062 | * xmlAttrListDumpOutput: |
| 7063 | * @buf: the XML buffer output |
| 7064 | * @doc: the document |
| 7065 | * @cur: the first attribute pointer |
| 7066 | * @encoding: an optional encoding string |
| 7067 | * |
| 7068 | * Dump a list of XML attributes |
| 7069 | */ |
| 7070 | static void |
| 7071 | xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7072 | xmlAttrPtr cur, const char *encoding) { |
| 7073 | if (cur == NULL) { |
| 7074 | #ifdef DEBUG_TREE |
| 7075 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7076 | "xmlAttrListDumpOutput : property == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7077 | #endif |
| 7078 | return; |
| 7079 | } |
| 7080 | while (cur != NULL) { |
| 7081 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 7082 | cur = cur->next; |
| 7083 | } |
| 7084 | } |
| 7085 | |
| 7086 | |
| 7087 | |
| 7088 | /** |
| 7089 | * xmlNodeListDumpOutput: |
| 7090 | * @buf: the XML buffer output |
| 7091 | * @doc: the document |
| 7092 | * @cur: the first node |
| 7093 | * @level: the imbrication level for indenting |
| 7094 | * @format: is formatting allowed |
| 7095 | * @encoding: an optional encoding string |
| 7096 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7097 | * Dump an XML node list, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7098 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7099 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7100 | */ |
| 7101 | static void |
| 7102 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7103 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 7104 | int i; |
| 7105 | |
| 7106 | if (cur == NULL) { |
| 7107 | #ifdef DEBUG_TREE |
| 7108 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7109 | "xmlNodeListDumpOutput : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7110 | #endif |
| 7111 | return; |
| 7112 | } |
| 7113 | while (cur != NULL) { |
| 7114 | if ((format) && (xmlIndentTreeOutput) && |
| 7115 | (cur->type == XML_ELEMENT_NODE)) |
| 7116 | for (i = 0;i < level;i++) |
Aleksey Sanin | 2300256 | 2002-05-24 07:18:40 +0000 | [diff] [blame] | 7117 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7118 | xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7119 | if (format) { |
| 7120 | xmlOutputBufferWriteString(buf, "\n"); |
| 7121 | } |
| 7122 | cur = cur->next; |
| 7123 | } |
| 7124 | } |
| 7125 | |
| 7126 | /** |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7127 | * xmlNodeDumpOutputInternal: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7128 | * @buf: the XML buffer output |
| 7129 | * @doc: the document |
| 7130 | * @cur: the current node |
| 7131 | * @level: the imbrication level for indenting |
| 7132 | * @format: is formatting allowed |
| 7133 | * @encoding: an optional encoding string |
| 7134 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7135 | * Dump an XML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7136 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7137 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7138 | */ |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7139 | static void |
| 7140 | xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7141 | xmlNodePtr cur, int level, int format, const char *encoding) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7142 | int i; |
| 7143 | xmlNodePtr tmp; |
| 7144 | |
| 7145 | if (cur == NULL) { |
| 7146 | #ifdef DEBUG_TREE |
| 7147 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7148 | "xmlNodeDumpOutput : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7149 | #endif |
| 7150 | return; |
| 7151 | } |
| 7152 | if (cur->type == XML_XINCLUDE_START) |
| 7153 | return; |
| 7154 | if (cur->type == XML_XINCLUDE_END) |
| 7155 | return; |
| 7156 | if (cur->type == XML_DTD_NODE) { |
| 7157 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 7158 | return; |
| 7159 | } |
| 7160 | if (cur->type == XML_ELEMENT_DECL) { |
| 7161 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 7162 | return; |
| 7163 | } |
| 7164 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 7165 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 7166 | return; |
| 7167 | } |
| 7168 | if (cur->type == XML_ENTITY_DECL) { |
| 7169 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 7170 | return; |
| 7171 | } |
| 7172 | if (cur->type == XML_TEXT_NODE) { |
| 7173 | if (cur->content != NULL) { |
| 7174 | if ((cur->name == xmlStringText) || |
| 7175 | (cur->name != xmlStringTextNoenc)) { |
| 7176 | xmlChar *buffer; |
| 7177 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7178 | if (encoding == NULL) |
| 7179 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7180 | else |
| 7181 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7182 | if (buffer != NULL) { |
| 7183 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7184 | xmlFree(buffer); |
| 7185 | } |
| 7186 | } else { |
| 7187 | /* |
| 7188 | * Disable escaping, needed for XSLT |
| 7189 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7190 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7191 | } |
| 7192 | } |
| 7193 | |
| 7194 | return; |
| 7195 | } |
| 7196 | if (cur->type == XML_PI_NODE) { |
| 7197 | if (cur->content != NULL) { |
| 7198 | xmlOutputBufferWriteString(buf, "<?"); |
| 7199 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7200 | if (cur->content != NULL) { |
| 7201 | xmlOutputBufferWriteString(buf, " "); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7202 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7203 | } |
| 7204 | xmlOutputBufferWriteString(buf, "?>"); |
| 7205 | } else { |
| 7206 | xmlOutputBufferWriteString(buf, "<?"); |
| 7207 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7208 | xmlOutputBufferWriteString(buf, "?>"); |
| 7209 | } |
| 7210 | return; |
| 7211 | } |
| 7212 | if (cur->type == XML_COMMENT_NODE) { |
| 7213 | if (cur->content != NULL) { |
| 7214 | xmlOutputBufferWriteString(buf, "<!--"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7215 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7216 | xmlOutputBufferWriteString(buf, "-->"); |
| 7217 | } |
| 7218 | return; |
| 7219 | } |
| 7220 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 7221 | xmlOutputBufferWriteString(buf, "&"); |
| 7222 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7223 | xmlOutputBufferWriteString(buf, ";"); |
| 7224 | return; |
| 7225 | } |
| 7226 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 7227 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 7228 | if (cur->content != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7229 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7230 | xmlOutputBufferWriteString(buf, "]]>"); |
| 7231 | return; |
| 7232 | } |
Daniel Veillard | 7b72ee5 | 2003-02-27 23:24:53 +0000 | [diff] [blame] | 7233 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 7234 | xmlAttrDumpOutput(buf,doc, (xmlAttrPtr) cur,encoding); |
Daniel Veillard | 7b72ee5 | 2003-02-27 23:24:53 +0000 | [diff] [blame] | 7235 | return; |
| 7236 | } |
Daniel Veillard | 6aa2f60 | 2003-02-10 00:01:56 +0000 | [diff] [blame] | 7237 | if (cur->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7238 | xmlNsDumpOutput(buf, (xmlNsPtr) cur); |
Daniel Veillard | 6aa2f60 | 2003-02-10 00:01:56 +0000 | [diff] [blame] | 7239 | return; |
| 7240 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7241 | |
| 7242 | if (format == 1) { |
| 7243 | tmp = cur->children; |
| 7244 | while (tmp != NULL) { |
| 7245 | if ((tmp->type == XML_TEXT_NODE) || |
| 7246 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 7247 | format = 0; |
| 7248 | break; |
| 7249 | } |
| 7250 | tmp = tmp->next; |
| 7251 | } |
| 7252 | } |
| 7253 | xmlOutputBufferWriteString(buf, "<"); |
| 7254 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7255 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7256 | xmlOutputBufferWriteString(buf, ":"); |
| 7257 | } |
| 7258 | |
| 7259 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7260 | if (cur->nsDef) |
| 7261 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 7262 | if (cur->properties != NULL) |
| 7263 | xmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 7264 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 7265 | if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) && |
| 7266 | (cur->children == NULL) && (!xmlSaveNoEmptyTags)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7267 | xmlOutputBufferWriteString(buf, "/>"); |
| 7268 | return; |
| 7269 | } |
| 7270 | xmlOutputBufferWriteString(buf, ">"); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 7271 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7272 | xmlChar *buffer; |
| 7273 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7274 | if (encoding == NULL) |
| 7275 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7276 | else |
| 7277 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7278 | if (buffer != NULL) { |
| 7279 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7280 | xmlFree(buffer); |
| 7281 | } |
| 7282 | } |
| 7283 | if (cur->children != NULL) { |
| 7284 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 7285 | xmlNodeListDumpOutput(buf, doc, cur->children, |
| 7286 | (level >= 0?level+1:-1), format, encoding); |
| 7287 | if ((xmlIndentTreeOutput) && (format)) |
| 7288 | for (i = 0;i < level;i++) |
Aleksey Sanin | 2300256 | 2002-05-24 07:18:40 +0000 | [diff] [blame] | 7289 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7290 | } |
| 7291 | xmlOutputBufferWriteString(buf, "</"); |
| 7292 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7293 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7294 | xmlOutputBufferWriteString(buf, ":"); |
| 7295 | } |
| 7296 | |
| 7297 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7298 | xmlOutputBufferWriteString(buf, ">"); |
| 7299 | } |
| 7300 | |
| 7301 | /** |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7302 | * xmlNodeDumpOutput: |
| 7303 | * @buf: the XML buffer output |
| 7304 | * @doc: the document |
| 7305 | * @cur: the current node |
| 7306 | * @level: the imbrication level for indenting |
| 7307 | * @format: is formatting allowed |
| 7308 | * @encoding: an optional encoding string |
| 7309 | * |
| 7310 | * Dump an XML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7311 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7312 | * or xmlKeepBlanksDefault(0) was called |
| 7313 | */ |
| 7314 | void |
| 7315 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7316 | int level, int format, const char *encoding) |
| 7317 | { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7318 | #ifdef LIBXML_HTML_ENABLED |
| 7319 | xmlDtdPtr dtd; |
| 7320 | int is_xhtml = 0; |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7321 | #endif |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7322 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7323 | xmlInitParser(); |
| 7324 | |
| 7325 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7326 | dtd = xmlGetIntSubset(doc); |
| 7327 | if (dtd != NULL) { |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7328 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 7329 | if (is_xhtml < 0) |
| 7330 | is_xhtml = 0; |
| 7331 | if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) && |
| 7332 | (cur->type == XML_ELEMENT_NODE) && |
| 7333 | (xmlStrEqual(cur->name, BAD_CAST "html"))) { |
| 7334 | if (encoding != NULL) |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 7335 | htmlSetMetaEncoding((htmlDocPtr) doc, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7336 | (const xmlChar *) encoding); |
| 7337 | else |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 7338 | htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8"); |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7339 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7340 | } |
| 7341 | |
| 7342 | if (is_xhtml) |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7343 | xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7344 | else |
| 7345 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7346 | xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7347 | } |
| 7348 | |
| 7349 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7350 | * xmlDocContentDumpOutput: |
| 7351 | * @buf: the XML buffer output |
| 7352 | * @cur: the document |
| 7353 | * @encoding: an optional encoding string |
| 7354 | * @format: should formatting spaces been added |
| 7355 | * |
| 7356 | * Dump an XML document. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7357 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7358 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7359 | */ |
| 7360 | static void |
| 7361 | xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 7362 | const char *encoding, int format) { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7363 | #ifdef LIBXML_HTML_ENABLED |
| 7364 | xmlDtdPtr dtd; |
| 7365 | int is_xhtml = 0; |
| 7366 | #endif |
| 7367 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7368 | xmlInitParser(); |
| 7369 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7370 | xmlOutputBufferWriteString(buf, "<?xml version="); |
| 7371 | if (cur->version != NULL) |
| 7372 | xmlBufferWriteQuotedString(buf->buffer, cur->version); |
| 7373 | else |
| 7374 | xmlOutputBufferWriteString(buf, "\"1.0\""); |
| 7375 | if (encoding == NULL) { |
| 7376 | if (cur->encoding != NULL) |
| 7377 | encoding = (const char *) cur->encoding; |
| 7378 | else if (cur->charset != XML_CHAR_ENCODING_UTF8) |
| 7379 | encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset); |
| 7380 | } |
| 7381 | if (encoding != NULL) { |
| 7382 | xmlOutputBufferWriteString(buf, " encoding="); |
| 7383 | xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding); |
| 7384 | } |
| 7385 | switch (cur->standalone) { |
| 7386 | case 0: |
| 7387 | xmlOutputBufferWriteString(buf, " standalone=\"no\""); |
| 7388 | break; |
| 7389 | case 1: |
| 7390 | xmlOutputBufferWriteString(buf, " standalone=\"yes\""); |
| 7391 | break; |
| 7392 | } |
| 7393 | xmlOutputBufferWriteString(buf, "?>\n"); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7394 | |
| 7395 | #ifdef LIBXML_HTML_ENABLED |
| 7396 | dtd = xmlGetIntSubset(cur); |
| 7397 | if (dtd != NULL) { |
| 7398 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 7399 | if (is_xhtml < 0) is_xhtml = 0; |
| 7400 | } |
| 7401 | if (is_xhtml) { |
| 7402 | if (encoding != NULL) |
| 7403 | htmlSetMetaEncoding(cur, (const xmlChar *) encoding); |
| 7404 | else |
| 7405 | htmlSetMetaEncoding(cur, BAD_CAST "UTF-8"); |
| 7406 | } |
| 7407 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7408 | if (cur->children != NULL) { |
| 7409 | xmlNodePtr child = cur->children; |
| 7410 | |
| 7411 | while (child != NULL) { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7412 | #ifdef LIBXML_HTML_ENABLED |
| 7413 | if (is_xhtml) |
| 7414 | xhtmlNodeDumpOutput(buf, cur, child, 0, format, encoding); |
| 7415 | else |
| 7416 | #endif |
| 7417 | xmlNodeDumpOutputInternal(buf, cur, child, 0, format, encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7418 | xmlOutputBufferWriteString(buf, "\n"); |
| 7419 | child = child->next; |
| 7420 | } |
| 7421 | } |
| 7422 | } |
| 7423 | |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7424 | #ifdef LIBXML_HTML_ENABLED |
| 7425 | /************************************************************************ |
| 7426 | * * |
| 7427 | * Functions specific to XHTML serialization * |
| 7428 | * * |
| 7429 | ************************************************************************/ |
| 7430 | |
| 7431 | #define XHTML_STRICT_PUBLIC_ID BAD_CAST \ |
| 7432 | "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 7433 | #define XHTML_STRICT_SYSTEM_ID BAD_CAST \ |
| 7434 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" |
| 7435 | #define XHTML_FRAME_PUBLIC_ID BAD_CAST \ |
| 7436 | "-//W3C//DTD XHTML 1.0 Frameset//EN" |
| 7437 | #define XHTML_FRAME_SYSTEM_ID BAD_CAST \ |
| 7438 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd" |
| 7439 | #define XHTML_TRANS_PUBLIC_ID BAD_CAST \ |
| 7440 | "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 7441 | #define XHTML_TRANS_SYSTEM_ID BAD_CAST \ |
| 7442 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" |
| 7443 | |
| 7444 | #define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml" |
| 7445 | /** |
| 7446 | * xmlIsXHTML: |
| 7447 | * @systemID: the system identifier |
| 7448 | * @publicID: the public identifier |
| 7449 | * |
| 7450 | * Try to find if the document correspond to an XHTML DTD |
| 7451 | * |
| 7452 | * Returns 1 if true, 0 if not and -1 in case of error |
| 7453 | */ |
| 7454 | int |
| 7455 | xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) { |
| 7456 | if ((systemID == NULL) && (publicID == NULL)) |
| 7457 | return(-1); |
| 7458 | if (publicID != NULL) { |
| 7459 | if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1); |
| 7460 | if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1); |
| 7461 | if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1); |
| 7462 | } |
| 7463 | if (systemID != NULL) { |
| 7464 | if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1); |
| 7465 | if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1); |
| 7466 | if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1); |
| 7467 | } |
| 7468 | return(0); |
| 7469 | } |
| 7470 | |
| 7471 | /** |
| 7472 | * xhtmlIsEmpty: |
| 7473 | * @node: the node |
| 7474 | * |
| 7475 | * Check if a node is an empty xhtml node |
| 7476 | * |
| 7477 | * Returns 1 if the node is an empty node, 0 if not and -1 in case of error |
| 7478 | */ |
| 7479 | static int |
| 7480 | xhtmlIsEmpty(xmlNodePtr node) { |
| 7481 | if (node == NULL) |
| 7482 | return(-1); |
| 7483 | if (node->type != XML_ELEMENT_NODE) |
| 7484 | return(0); |
| 7485 | if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME))) |
| 7486 | return(0); |
| 7487 | if (node->children != NULL) |
| 7488 | return(0); |
| 7489 | switch (node->name[0]) { |
| 7490 | case 'a': |
| 7491 | if (xmlStrEqual(node->name, BAD_CAST "area")) |
| 7492 | return(1); |
| 7493 | return(0); |
| 7494 | case 'b': |
| 7495 | if (xmlStrEqual(node->name, BAD_CAST "br")) |
| 7496 | return(1); |
| 7497 | if (xmlStrEqual(node->name, BAD_CAST "base")) |
| 7498 | return(1); |
| 7499 | if (xmlStrEqual(node->name, BAD_CAST "basefont")) |
| 7500 | return(1); |
| 7501 | return(0); |
| 7502 | case 'c': |
| 7503 | if (xmlStrEqual(node->name, BAD_CAST "col")) |
| 7504 | return(1); |
| 7505 | return(0); |
| 7506 | case 'f': |
| 7507 | if (xmlStrEqual(node->name, BAD_CAST "frame")) |
| 7508 | return(1); |
| 7509 | return(0); |
| 7510 | case 'h': |
| 7511 | if (xmlStrEqual(node->name, BAD_CAST "hr")) |
| 7512 | return(1); |
| 7513 | return(0); |
| 7514 | case 'i': |
| 7515 | if (xmlStrEqual(node->name, BAD_CAST "img")) |
| 7516 | return(1); |
| 7517 | if (xmlStrEqual(node->name, BAD_CAST "input")) |
| 7518 | return(1); |
| 7519 | if (xmlStrEqual(node->name, BAD_CAST "isindex")) |
| 7520 | return(1); |
| 7521 | return(0); |
| 7522 | case 'l': |
| 7523 | if (xmlStrEqual(node->name, BAD_CAST "link")) |
| 7524 | return(1); |
| 7525 | return(0); |
| 7526 | case 'm': |
| 7527 | if (xmlStrEqual(node->name, BAD_CAST "meta")) |
| 7528 | return(1); |
| 7529 | return(0); |
| 7530 | case 'p': |
| 7531 | if (xmlStrEqual(node->name, BAD_CAST "param")) |
| 7532 | return(1); |
| 7533 | return(0); |
| 7534 | } |
| 7535 | return(0); |
| 7536 | } |
| 7537 | |
| 7538 | /** |
| 7539 | * xhtmlAttrListDumpOutput: |
| 7540 | * @buf: the XML buffer output |
| 7541 | * @doc: the document |
| 7542 | * @cur: the first attribute pointer |
| 7543 | * @encoding: an optional encoding string |
| 7544 | * |
| 7545 | * Dump a list of XML attributes |
| 7546 | */ |
| 7547 | static void |
| 7548 | xhtmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7549 | xmlAttrPtr cur, const char *encoding) { |
| 7550 | xmlAttrPtr xml_lang = NULL; |
| 7551 | xmlAttrPtr lang = NULL; |
| 7552 | xmlAttrPtr name = NULL; |
| 7553 | xmlAttrPtr id = NULL; |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7554 | xmlNodePtr parent; |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7555 | |
| 7556 | if (cur == NULL) { |
| 7557 | #ifdef DEBUG_TREE |
| 7558 | xmlGenericError(xmlGenericErrorContext, |
| 7559 | "xmlAttrListDumpOutput : property == NULL\n"); |
| 7560 | #endif |
| 7561 | return; |
| 7562 | } |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7563 | parent = cur->parent; |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7564 | while (cur != NULL) { |
| 7565 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id"))) |
| 7566 | id = cur; |
| 7567 | else |
| 7568 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name"))) |
| 7569 | name = cur; |
| 7570 | else |
| 7571 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang"))) |
| 7572 | lang = cur; |
| 7573 | else |
| 7574 | if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) && |
| 7575 | (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml"))) |
| 7576 | xml_lang = cur; |
| 7577 | else if ((cur->ns == NULL) && |
| 7578 | ((cur->children == NULL) || |
| 7579 | (cur->children->content == NULL) || |
| 7580 | (cur->children->content[0] == 0)) && |
| 7581 | (htmlIsBooleanAttr(cur->name))) { |
| 7582 | if (cur->children != NULL) |
| 7583 | xmlFreeNode(cur->children); |
| 7584 | cur->children = xmlNewText(cur->name); |
| 7585 | if (cur->children != NULL) |
| 7586 | cur->children->parent = (xmlNodePtr) cur; |
| 7587 | } |
| 7588 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 7589 | cur = cur->next; |
| 7590 | } |
| 7591 | /* |
| 7592 | * C.8 |
| 7593 | */ |
| 7594 | if ((name != NULL) && (id == NULL)) { |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7595 | if ((parent != NULL) && (parent->name != NULL) && |
| 7596 | ((xmlStrEqual(parent->name, BAD_CAST "a")) || |
| 7597 | (xmlStrEqual(parent->name, BAD_CAST "p")) || |
| 7598 | (xmlStrEqual(parent->name, BAD_CAST "div")) || |
| 7599 | (xmlStrEqual(parent->name, BAD_CAST "img")) || |
| 7600 | (xmlStrEqual(parent->name, BAD_CAST "map")) || |
| 7601 | (xmlStrEqual(parent->name, BAD_CAST "applet")) || |
| 7602 | (xmlStrEqual(parent->name, BAD_CAST "form")) || |
| 7603 | (xmlStrEqual(parent->name, BAD_CAST "frame")) || |
| 7604 | (xmlStrEqual(parent->name, BAD_CAST "iframe")))) { |
| 7605 | xmlOutputBufferWriteString(buf, " id=\""); |
| 7606 | xmlAttrSerializeContent(buf->buffer, doc, name); |
| 7607 | xmlOutputBufferWriteString(buf, "\""); |
| 7608 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7609 | } |
| 7610 | /* |
| 7611 | * C.7. |
| 7612 | */ |
| 7613 | if ((lang != NULL) && (xml_lang == NULL)) { |
| 7614 | xmlOutputBufferWriteString(buf, " xml:lang=\""); |
| 7615 | xmlAttrSerializeContent(buf->buffer, doc, lang); |
| 7616 | xmlOutputBufferWriteString(buf, "\""); |
| 7617 | } else |
| 7618 | if ((xml_lang != NULL) && (lang == NULL)) { |
| 7619 | xmlOutputBufferWriteString(buf, " lang=\""); |
| 7620 | xmlAttrSerializeContent(buf->buffer, doc, xml_lang); |
| 7621 | xmlOutputBufferWriteString(buf, "\""); |
| 7622 | } |
| 7623 | } |
| 7624 | |
| 7625 | /** |
| 7626 | * xhtmlNodeListDumpOutput: |
| 7627 | * @buf: the XML buffer output |
| 7628 | * @doc: the XHTML document |
| 7629 | * @cur: the first node |
| 7630 | * @level: the imbrication level for indenting |
| 7631 | * @format: is formatting allowed |
| 7632 | * @encoding: an optional encoding string |
| 7633 | * |
| 7634 | * Dump an XML node list, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7635 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7636 | * or xmlKeepBlanksDefault(0) was called |
| 7637 | */ |
| 7638 | static void |
| 7639 | xhtmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7640 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 7641 | int i; |
| 7642 | |
| 7643 | if (cur == NULL) { |
| 7644 | #ifdef DEBUG_TREE |
| 7645 | xmlGenericError(xmlGenericErrorContext, |
| 7646 | "xhtmlNodeListDumpOutput : node == NULL\n"); |
| 7647 | #endif |
| 7648 | return; |
| 7649 | } |
| 7650 | while (cur != NULL) { |
| 7651 | if ((format) && (xmlIndentTreeOutput) && |
| 7652 | (cur->type == XML_ELEMENT_NODE)) |
| 7653 | for (i = 0;i < level;i++) |
| 7654 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
| 7655 | xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
| 7656 | if (format) { |
| 7657 | xmlOutputBufferWriteString(buf, "\n"); |
| 7658 | } |
| 7659 | cur = cur->next; |
| 7660 | } |
| 7661 | } |
| 7662 | |
| 7663 | /** |
| 7664 | * xhtmlNodeDumpOutput: |
| 7665 | * @buf: the XML buffer output |
| 7666 | * @doc: the XHTML document |
| 7667 | * @cur: the current node |
| 7668 | * @level: the imbrication level for indenting |
| 7669 | * @format: is formatting allowed |
| 7670 | * @encoding: an optional encoding string |
| 7671 | * |
| 7672 | * Dump an XHTML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7673 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7674 | * or xmlKeepBlanksDefault(0) was called |
| 7675 | */ |
| 7676 | static void |
| 7677 | xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 7678 | int level, int format, const char *encoding) { |
| 7679 | int i; |
| 7680 | xmlNodePtr tmp; |
| 7681 | |
| 7682 | if (cur == NULL) { |
| 7683 | #ifdef DEBUG_TREE |
| 7684 | xmlGenericError(xmlGenericErrorContext, |
| 7685 | "xmlNodeDumpOutput : node == NULL\n"); |
| 7686 | #endif |
| 7687 | return; |
| 7688 | } |
| 7689 | if (cur->type == XML_XINCLUDE_START) |
| 7690 | return; |
| 7691 | if (cur->type == XML_XINCLUDE_END) |
| 7692 | return; |
| 7693 | if (cur->type == XML_DTD_NODE) { |
| 7694 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 7695 | return; |
| 7696 | } |
| 7697 | if (cur->type == XML_ELEMENT_DECL) { |
| 7698 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 7699 | return; |
| 7700 | } |
| 7701 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 7702 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 7703 | return; |
| 7704 | } |
| 7705 | if (cur->type == XML_ENTITY_DECL) { |
| 7706 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 7707 | return; |
| 7708 | } |
| 7709 | if (cur->type == XML_TEXT_NODE) { |
| 7710 | if (cur->content != NULL) { |
| 7711 | if ((cur->name == xmlStringText) || |
| 7712 | (cur->name != xmlStringTextNoenc)) { |
| 7713 | xmlChar *buffer; |
| 7714 | |
| 7715 | if (encoding == NULL) |
| 7716 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7717 | else |
| 7718 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 7719 | if (buffer != NULL) { |
| 7720 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7721 | xmlFree(buffer); |
| 7722 | } |
| 7723 | } else { |
| 7724 | /* |
| 7725 | * Disable escaping, needed for XSLT |
| 7726 | */ |
| 7727 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
| 7728 | } |
| 7729 | } |
| 7730 | |
| 7731 | return; |
| 7732 | } |
| 7733 | if (cur->type == XML_PI_NODE) { |
| 7734 | if (cur->content != NULL) { |
| 7735 | xmlOutputBufferWriteString(buf, "<?"); |
| 7736 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7737 | if (cur->content != NULL) { |
| 7738 | xmlOutputBufferWriteString(buf, " "); |
| 7739 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 7740 | } |
| 7741 | xmlOutputBufferWriteString(buf, "?>"); |
| 7742 | } else { |
| 7743 | xmlOutputBufferWriteString(buf, "<?"); |
| 7744 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7745 | xmlOutputBufferWriteString(buf, "?>"); |
| 7746 | } |
| 7747 | return; |
| 7748 | } |
| 7749 | if (cur->type == XML_COMMENT_NODE) { |
| 7750 | if (cur->content != NULL) { |
| 7751 | xmlOutputBufferWriteString(buf, "<!--"); |
| 7752 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 7753 | xmlOutputBufferWriteString(buf, "-->"); |
| 7754 | } |
| 7755 | return; |
| 7756 | } |
| 7757 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 7758 | xmlOutputBufferWriteString(buf, "&"); |
| 7759 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7760 | xmlOutputBufferWriteString(buf, ";"); |
| 7761 | return; |
| 7762 | } |
| 7763 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 7764 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 7765 | if (cur->content != NULL) |
| 7766 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 7767 | xmlOutputBufferWriteString(buf, "]]>"); |
| 7768 | return; |
| 7769 | } |
| 7770 | |
| 7771 | if (format == 1) { |
| 7772 | tmp = cur->children; |
| 7773 | while (tmp != NULL) { |
| 7774 | if ((tmp->type == XML_TEXT_NODE) || |
| 7775 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 7776 | format = 0; |
| 7777 | break; |
| 7778 | } |
| 7779 | tmp = tmp->next; |
| 7780 | } |
| 7781 | } |
| 7782 | xmlOutputBufferWriteString(buf, "<"); |
| 7783 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7784 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7785 | xmlOutputBufferWriteString(buf, ":"); |
| 7786 | } |
| 7787 | |
| 7788 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7789 | if (cur->nsDef) |
| 7790 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 7791 | if ((xmlStrEqual(cur->name, BAD_CAST "html") && |
| 7792 | (cur->ns == NULL) && (cur->nsDef == NULL))) { |
| 7793 | /* |
| 7794 | * 3.1.1. Strictly Conforming Documents A.3.1.1 3/ |
| 7795 | */ |
| 7796 | xmlOutputBufferWriteString(buf, |
| 7797 | " xmlns=\"http://www.w3.org/1999/xhtml\""); |
| 7798 | } |
| 7799 | if (cur->properties != NULL) |
| 7800 | xhtmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 7801 | |
| 7802 | if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) { |
| 7803 | if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) && |
| 7804 | (xhtmlIsEmpty(cur) == 1)) { |
| 7805 | /* |
| 7806 | * C.2. Empty Elements |
| 7807 | */ |
| 7808 | xmlOutputBufferWriteString(buf, " />"); |
| 7809 | } else { |
| 7810 | /* |
| 7811 | * C.3. Element Minimization and Empty Element Content |
| 7812 | */ |
| 7813 | xmlOutputBufferWriteString(buf, "></"); |
| 7814 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7815 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7816 | xmlOutputBufferWriteString(buf, ":"); |
| 7817 | } |
| 7818 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7819 | xmlOutputBufferWriteString(buf, ">"); |
| 7820 | } |
| 7821 | return; |
| 7822 | } |
| 7823 | xmlOutputBufferWriteString(buf, ">"); |
| 7824 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
| 7825 | xmlChar *buffer; |
| 7826 | |
| 7827 | if (encoding == NULL) |
| 7828 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7829 | else |
| 7830 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 7831 | if (buffer != NULL) { |
| 7832 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7833 | xmlFree(buffer); |
| 7834 | } |
| 7835 | } |
| 7836 | |
| 7837 | /* |
| 7838 | * 4.8. Script and Style elements |
| 7839 | */ |
| 7840 | if ((cur->type == XML_ELEMENT_NODE) && |
| 7841 | ((xmlStrEqual(cur->name, BAD_CAST "script")) || |
| 7842 | (xmlStrEqual(cur->name, BAD_CAST "style"))) && |
| 7843 | ((cur->ns == NULL) || |
| 7844 | (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) { |
| 7845 | xmlNodePtr child = cur->children; |
| 7846 | |
| 7847 | while (child != NULL) { |
| 7848 | if ((child->type == XML_TEXT_NODE) || |
| 7849 | (child->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 64b3528 | 2002-12-04 15:10:40 +0000 | [diff] [blame] | 7850 | /* |
| 7851 | * Apparently CDATA escaping for style just break on IE, |
| 7852 | * mozilla and galeon, so ... |
| 7853 | */ |
| 7854 | if (xmlStrEqual(cur->name, BAD_CAST "style") && |
| 7855 | (xmlStrchr(child->content, '<') == NULL) && |
| 7856 | (xmlStrchr(child->content, '>') == NULL) && |
| 7857 | (xmlStrchr(child->content, '&') == NULL)) { |
| 7858 | xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding); |
| 7859 | } else { |
| 7860 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 7861 | if (child->content != NULL) |
| 7862 | xmlOutputBufferWriteString(buf, |
| 7863 | (const char *)child->content); |
| 7864 | xmlOutputBufferWriteString(buf, "]]>"); |
| 7865 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7866 | } else { |
| 7867 | xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding); |
| 7868 | } |
| 7869 | child = child->next; |
| 7870 | } |
| 7871 | } else if (cur->children != NULL) { |
| 7872 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 7873 | xhtmlNodeListDumpOutput(buf, doc, cur->children, |
| 7874 | (level >= 0?level+1:-1), format, encoding); |
| 7875 | if ((xmlIndentTreeOutput) && (format)) |
| 7876 | for (i = 0;i < level;i++) |
| 7877 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
| 7878 | } |
| 7879 | xmlOutputBufferWriteString(buf, "</"); |
| 7880 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7881 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7882 | xmlOutputBufferWriteString(buf, ":"); |
| 7883 | } |
| 7884 | |
| 7885 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7886 | xmlOutputBufferWriteString(buf, ">"); |
| 7887 | } |
| 7888 | #endif |
| 7889 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7890 | /************************************************************************ |
| 7891 | * * |
| 7892 | * Saving functions front-ends * |
| 7893 | * * |
| 7894 | ************************************************************************/ |
| 7895 | |
| 7896 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7897 | * xmlDocDumpFormatMemoryEnc: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7898 | * @out_doc: Document to generate XML text from |
| 7899 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 7900 | * @doc_txt_len: Length of the generated XML text |
| 7901 | * @txt_encoding: Character encoding to use when generating XML text |
| 7902 | * @format: should formatting spaces been added |
| 7903 | * |
| 7904 | * Dump the current DOM tree into memory using the character encoding specified |
| 7905 | * 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] | 7906 | * allocated memory with xmlFree(). |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7907 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7908 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7909 | */ |
| 7910 | |
| 7911 | void |
| 7912 | xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7913 | int * doc_txt_len, const char * txt_encoding, |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 7914 | int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7915 | int dummy = 0; |
| 7916 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7917 | xmlOutputBufferPtr out_buff = NULL; |
| 7918 | xmlCharEncodingHandlerPtr conv_hdlr = NULL; |
| 7919 | |
| 7920 | if (doc_txt_len == NULL) { |
| 7921 | doc_txt_len = &dummy; /* Continue, caller just won't get length */ |
| 7922 | } |
| 7923 | |
| 7924 | if (doc_txt_ptr == NULL) { |
| 7925 | *doc_txt_len = 0; |
| 7926 | xmlGenericError(xmlGenericErrorContext, |
| 7927 | "xmlDocDumpFormatMemoryEnc: Null return buffer pointer."); |
| 7928 | return; |
| 7929 | } |
| 7930 | |
| 7931 | *doc_txt_ptr = NULL; |
| 7932 | *doc_txt_len = 0; |
| 7933 | |
| 7934 | if (out_doc == NULL) { |
| 7935 | /* No document, no output */ |
| 7936 | xmlGenericError(xmlGenericErrorContext, |
| 7937 | "xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.\n"); |
| 7938 | return; |
| 7939 | } |
| 7940 | |
| 7941 | /* |
| 7942 | * Validate the encoding value, if provided. |
| 7943 | * This logic is copied from xmlSaveFileEnc. |
| 7944 | */ |
| 7945 | |
| 7946 | if (txt_encoding == NULL) |
| 7947 | txt_encoding = (const char *) out_doc->encoding; |
| 7948 | if (txt_encoding != NULL) { |
Daniel Veillard | e132611 | 2003-06-05 09:32:20 +0000 | [diff] [blame] | 7949 | conv_hdlr = xmlFindCharEncodingHandler(txt_encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7950 | if ( conv_hdlr == NULL ) { |
| 7951 | xmlGenericError(xmlGenericErrorContext, |
| 7952 | "%s: %s %s '%s'\n", |
| 7953 | "xmlDocDumpFormatMemoryEnc", |
| 7954 | "Failed to identify encoding handler for", |
| 7955 | "character set", |
| 7956 | txt_encoding); |
| 7957 | return; |
| 7958 | } |
| 7959 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7960 | |
| 7961 | if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) { |
| 7962 | xmlGenericError(xmlGenericErrorContext, |
| 7963 | "xmlDocDumpFormatMemoryEnc: Failed to allocate output buffer.\n"); |
| 7964 | return; |
| 7965 | } |
| 7966 | |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 7967 | xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7968 | xmlOutputBufferFlush(out_buff); |
| 7969 | if (out_buff->conv != NULL) { |
| 7970 | *doc_txt_len = out_buff->conv->use; |
| 7971 | *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len); |
| 7972 | } else { |
| 7973 | *doc_txt_len = out_buff->buffer->use; |
| 7974 | *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len); |
| 7975 | } |
| 7976 | (void)xmlOutputBufferClose(out_buff); |
| 7977 | |
| 7978 | if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) { |
| 7979 | *doc_txt_len = 0; |
| 7980 | xmlGenericError(xmlGenericErrorContext, |
| 7981 | "xmlDocDumpFormatMemoryEnc: %s\n", |
| 7982 | "Failed to allocate memory for document text representation."); |
| 7983 | } |
| 7984 | |
| 7985 | return; |
| 7986 | } |
| 7987 | |
| 7988 | /** |
| 7989 | * xmlDocDumpMemory: |
| 7990 | * @cur: the document |
| 7991 | * @mem: OUT: the memory pointer |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7992 | * @size: OUT: the memory length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7993 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7994 | * 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] | 7995 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7996 | */ |
| 7997 | void |
| 7998 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
| 7999 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0); |
| 8000 | } |
| 8001 | |
| 8002 | /** |
| 8003 | * xmlDocDumpFormatMemory: |
| 8004 | * @cur: the document |
| 8005 | * @mem: OUT: the memory pointer |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 8006 | * @size: OUT: the memory length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8007 | * @format: should formatting spaces been added |
| 8008 | * |
| 8009 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8010 | * 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] | 8011 | * It's up to the caller to free the memory with xmlFree(). |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8012 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 8013 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8014 | */ |
| 8015 | void |
| 8016 | xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) { |
| 8017 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format); |
| 8018 | } |
| 8019 | |
| 8020 | /** |
| 8021 | * xmlDocDumpMemoryEnc: |
| 8022 | * @out_doc: Document to generate XML text from |
| 8023 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 8024 | * @doc_txt_len: Length of the generated XML text |
| 8025 | * @txt_encoding: Character encoding to use when generating XML text |
| 8026 | * |
| 8027 | * Dump the current DOM tree into memory using the character encoding specified |
| 8028 | * 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] | 8029 | * allocated memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8030 | */ |
| 8031 | |
| 8032 | void |
| 8033 | xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 8034 | int * doc_txt_len, const char * txt_encoding) { |
| 8035 | xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len, |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8036 | txt_encoding, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8037 | } |
| 8038 | |
| 8039 | /** |
| 8040 | * xmlGetDocCompressMode: |
| 8041 | * @doc: the document |
| 8042 | * |
| 8043 | * get the compression ratio for a document, ZLIB based |
| 8044 | * Returns 0 (uncompressed) to 9 (max compression) |
| 8045 | */ |
| 8046 | int |
| 8047 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 8048 | if (doc == NULL) return(-1); |
| 8049 | return(doc->compression); |
| 8050 | } |
| 8051 | |
| 8052 | /** |
| 8053 | * xmlSetDocCompressMode: |
| 8054 | * @doc: the document |
| 8055 | * @mode: the compression ratio |
| 8056 | * |
| 8057 | * set the compression ratio for a document, ZLIB based |
| 8058 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 8059 | */ |
| 8060 | void |
| 8061 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 8062 | if (doc == NULL) return; |
| 8063 | if (mode < 0) doc->compression = 0; |
| 8064 | else if (mode > 9) doc->compression = 9; |
| 8065 | else doc->compression = mode; |
| 8066 | } |
| 8067 | |
| 8068 | /** |
| 8069 | * xmlGetCompressMode: |
| 8070 | * |
| 8071 | * get the default compression mode used, ZLIB based. |
| 8072 | * Returns 0 (uncompressed) to 9 (max compression) |
| 8073 | */ |
| 8074 | int |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 8075 | xmlGetCompressMode(void) |
| 8076 | { |
| 8077 | return (xmlCompressMode); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8078 | } |
| 8079 | |
| 8080 | /** |
| 8081 | * xmlSetCompressMode: |
| 8082 | * @mode: the compression ratio |
| 8083 | * |
| 8084 | * set the default compression mode used, ZLIB based |
| 8085 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 8086 | */ |
| 8087 | void |
| 8088 | xmlSetCompressMode(int mode) { |
| 8089 | if (mode < 0) xmlCompressMode = 0; |
| 8090 | else if (mode > 9) xmlCompressMode = 9; |
| 8091 | else xmlCompressMode = mode; |
| 8092 | } |
| 8093 | |
| 8094 | /** |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8095 | * xmlDocFormatDump: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8096 | * @f: the FILE* |
| 8097 | * @cur: the document |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8098 | * @format: should formatting spaces been added |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8099 | * |
| 8100 | * Dump an XML document to an open FILE. |
| 8101 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8102 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8103 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8104 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8105 | */ |
| 8106 | int |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8107 | xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8108 | xmlOutputBufferPtr buf; |
| 8109 | const char * encoding; |
| 8110 | xmlCharEncodingHandlerPtr handler = NULL; |
| 8111 | int ret; |
| 8112 | |
| 8113 | if (cur == NULL) { |
| 8114 | #ifdef DEBUG_TREE |
| 8115 | xmlGenericError(xmlGenericErrorContext, |
| 8116 | "xmlDocDump : document == NULL\n"); |
| 8117 | #endif |
| 8118 | return(-1); |
| 8119 | } |
| 8120 | encoding = (const char *) cur->encoding; |
| 8121 | |
| 8122 | if (encoding != NULL) { |
Daniel Veillard | e132611 | 2003-06-05 09:32:20 +0000 | [diff] [blame] | 8123 | handler = xmlFindCharEncodingHandler(encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8124 | if (handler == NULL) { |
| 8125 | xmlFree((char *) cur->encoding); |
| 8126 | cur->encoding = NULL; |
| 8127 | } |
| 8128 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8129 | buf = xmlOutputBufferCreateFile(f, handler); |
| 8130 | if (buf == NULL) return(-1); |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8131 | xmlDocContentDumpOutput(buf, cur, NULL, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8132 | |
| 8133 | ret = xmlOutputBufferClose(buf); |
| 8134 | return(ret); |
| 8135 | } |
| 8136 | |
| 8137 | /** |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8138 | * xmlDocDump: |
| 8139 | * @f: the FILE* |
| 8140 | * @cur: the document |
| 8141 | * |
| 8142 | * Dump an XML document to an open FILE. |
| 8143 | * |
| 8144 | * returns: the number of bytes written or -1 in case of failure. |
| 8145 | */ |
| 8146 | int |
| 8147 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
| 8148 | return(xmlDocFormatDump (f, cur, 0)); |
| 8149 | } |
| 8150 | |
| 8151 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8152 | * xmlSaveFileTo: |
| 8153 | * @buf: an output I/O buffer |
| 8154 | * @cur: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8155 | * @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] | 8156 | * |
| 8157 | * Dump an XML document to an I/O buffer. |
| 8158 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8159 | * returns: the number of bytes written or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8160 | */ |
| 8161 | int |
CET 2001 Daniel Veillard | 5a37bde | 2001-11-01 14:31:22 +0000 | [diff] [blame] | 8162 | xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8163 | int ret; |
| 8164 | |
| 8165 | if (buf == NULL) return(0); |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8166 | xmlDocContentDumpOutput(buf, cur, encoding, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8167 | ret = xmlOutputBufferClose(buf); |
| 8168 | return(ret); |
| 8169 | } |
| 8170 | |
| 8171 | /** |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8172 | * xmlSaveFormatFileTo: |
| 8173 | * @buf: an output I/O buffer |
| 8174 | * @cur: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8175 | * @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] | 8176 | * @format: should formatting spaces been added |
| 8177 | * |
| 8178 | * Dump an XML document to an I/O buffer. |
| 8179 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8180 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8181 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8182 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8183 | */ |
| 8184 | int |
CET 2001 Daniel Veillard | 5a37bde | 2001-11-01 14:31:22 +0000 | [diff] [blame] | 8185 | xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) { |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8186 | int ret; |
| 8187 | |
| 8188 | if (buf == NULL) return(0); |
| 8189 | xmlDocContentDumpOutput(buf, cur, encoding, format); |
| 8190 | ret = xmlOutputBufferClose(buf); |
| 8191 | return(ret); |
| 8192 | } |
| 8193 | |
| 8194 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 8195 | * xmlSaveFormatFileEnc: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8196 | * @filename: the filename or URL to output |
| 8197 | * @cur: the document being saved |
| 8198 | * @encoding: the name of the encoding to use or NULL. |
| 8199 | * @format: should formatting spaces be added. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8200 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 8201 | * Dump an XML document to a file or an URL. |
| 8202 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8203 | * Returns the number of bytes written or -1 in case of error. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8204 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8205 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8206 | */ |
| 8207 | int |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8208 | xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur, |
| 8209 | const char * encoding, int format ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8210 | xmlOutputBufferPtr buf; |
| 8211 | xmlCharEncodingHandlerPtr handler = NULL; |
| 8212 | int ret; |
| 8213 | |
Daniel Veillard | 4dbe77a | 2003-01-14 00:17:42 +0000 | [diff] [blame] | 8214 | if (cur == NULL) |
| 8215 | return(-1); |
| 8216 | |
Daniel Veillard | fb25a51 | 2002-01-13 20:32:08 +0000 | [diff] [blame] | 8217 | if (encoding == NULL) |
| 8218 | encoding = (const char *) cur->encoding; |
| 8219 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8220 | if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8221 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8222 | handler = xmlFindCharEncodingHandler(encoding); |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 8223 | if (handler == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8224 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8225 | } |
| 8226 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8227 | #ifdef HAVE_ZLIB_H |
| 8228 | if (cur->compression < 0) cur->compression = xmlCompressMode; |
| 8229 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8230 | /* |
| 8231 | * save the content to a temp buffer. |
| 8232 | */ |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8233 | buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8234 | if (buf == NULL) return(-1); |
| 8235 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8236 | xmlDocContentDumpOutput(buf, cur, encoding, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8237 | |
| 8238 | ret = xmlOutputBufferClose(buf); |
| 8239 | return(ret); |
| 8240 | } |
| 8241 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8242 | |
| 8243 | /** |
| 8244 | * xmlSaveFileEnc: |
| 8245 | * @filename: the filename (or URL) |
| 8246 | * @cur: the document |
| 8247 | * @encoding: the name of an encoding (or NULL) |
| 8248 | * |
| 8249 | * Dump an XML document, converting it to the given encoding |
| 8250 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8251 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8252 | */ |
| 8253 | int |
| 8254 | xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 8255 | return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) ); |
| 8256 | } |
| 8257 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8258 | /** |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8259 | * xmlSaveFormatFile: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8260 | * @filename: the filename (or URL) |
| 8261 | * @cur: the document |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8262 | * @format: should formatting spaces been added |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8263 | * |
| 8264 | * Dump an XML document to a file. Will use compression if |
| 8265 | * compiled in and enabled. If @filename is "-" the stdout file is |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8266 | * 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] | 8267 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8268 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8269 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8270 | * returns: the number of bytes written or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8271 | */ |
| 8272 | int |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8273 | xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8274 | return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8275 | } |
| 8276 | |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8277 | /** |
| 8278 | * xmlSaveFile: |
| 8279 | * @filename: the filename (or URL) |
| 8280 | * @cur: the document |
| 8281 | * |
| 8282 | * Dump an XML document to a file. Will use compression if |
| 8283 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 8284 | * used. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8285 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8286 | */ |
| 8287 | int |
| 8288 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8289 | return(xmlSaveFormatFileEnc(filename, cur, NULL, 0)); |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8290 | } |
| 8291 | |