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 | * * |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 47 | * Tree memory error handler * |
| 48 | * * |
| 49 | ************************************************************************/ |
| 50 | /** |
| 51 | * xmlTreeErrMemory: |
| 52 | * @extra: extra informations |
| 53 | * |
| 54 | * Handle an out of memory condition |
| 55 | */ |
| 56 | static void |
| 57 | xmlTreeErrMemory(const char *extra) |
| 58 | { |
| 59 | __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * xmlTreeErr: |
| 64 | * @code: the error number |
| 65 | * @extra: extra informations |
| 66 | * |
| 67 | * Handle an out of memory condition |
| 68 | */ |
| 69 | static void |
| 70 | xmlTreeErr(int code, xmlNodePtr node, const char *extra) |
| 71 | { |
| 72 | const char *msg = NULL; |
| 73 | |
| 74 | switch(code) { |
| 75 | case XML_TREE_INVALID_HEX: |
| 76 | msg = "invalid hexadecimal character value"; |
| 77 | break; |
| 78 | case XML_TREE_INVALID_DEC: |
| 79 | msg = "invalid decimal character value"; |
| 80 | break; |
| 81 | case XML_TREE_UNTERMINATED_ENTITY: |
| 82 | msg = "unterminated entity reference %15s"; |
| 83 | break; |
| 84 | default: |
| 85 | msg = "unexpected error number"; |
| 86 | } |
| 87 | __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra); |
| 88 | } |
| 89 | |
| 90 | /************************************************************************ |
| 91 | * * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 92 | * A few static variables and macros * |
| 93 | * * |
| 94 | ************************************************************************/ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 95 | /* #undef xmlStringText */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 96 | const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 97 | /* #undef xmlStringTextNoenc */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 98 | const xmlChar xmlStringTextNoenc[] = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 99 | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 100 | /* #undef xmlStringComment */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 101 | const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
| 102 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 103 | static int xmlCompressMode = 0; |
| 104 | static int xmlCheckDTD = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 105 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 106 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
| 107 | xmlNodePtr ulccur = (n)->children; \ |
| 108 | if (ulccur == NULL) { \ |
| 109 | (n)->last = NULL; \ |
| 110 | } else { \ |
| 111 | while (ulccur->next != NULL) { \ |
| 112 | ulccur->parent = (n); \ |
| 113 | ulccur = ulccur->next; \ |
| 114 | } \ |
| 115 | ulccur->parent = (n); \ |
| 116 | (n)->last = ulccur; \ |
| 117 | }} |
| 118 | |
| 119 | /* #define DEBUG_BUFFER */ |
| 120 | /* #define DEBUG_TREE */ |
| 121 | |
| 122 | /************************************************************************ |
| 123 | * * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 124 | * Functions to move to entities.c once the * |
| 125 | * API freeze is smoothen and they can be made public. * |
| 126 | * * |
| 127 | ************************************************************************/ |
| 128 | #include <libxml/hash.h> |
| 129 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 130 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 131 | /** |
| 132 | * xmlGetEntityFromDtd: |
| 133 | * @dtd: A pointer to the DTD to search |
| 134 | * @name: The entity name |
| 135 | * |
| 136 | * Do an entity lookup in the DTD entity hash table and |
| 137 | * return the corresponding entity, if found. |
| 138 | * |
| 139 | * Returns A pointer to the entity structure or NULL if not found. |
| 140 | */ |
| 141 | static xmlEntityPtr |
| 142 | xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 143 | xmlEntitiesTablePtr table; |
| 144 | |
| 145 | if((dtd != NULL) && (dtd->entities != NULL)) { |
| 146 | table = (xmlEntitiesTablePtr) dtd->entities; |
| 147 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 148 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 149 | } |
| 150 | return(NULL); |
| 151 | } |
| 152 | /** |
| 153 | * xmlGetParameterEntityFromDtd: |
| 154 | * @dtd: A pointer to the DTD to search |
| 155 | * @name: The entity name |
| 156 | * |
| 157 | * Do an entity lookup in the DTD pararmeter entity hash table and |
| 158 | * return the corresponding entity, if found. |
| 159 | * |
| 160 | * Returns A pointer to the entity structure or NULL if not found. |
| 161 | */ |
| 162 | static xmlEntityPtr |
| 163 | xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 164 | xmlEntitiesTablePtr table; |
| 165 | |
| 166 | if ((dtd != NULL) && (dtd->pentities != NULL)) { |
| 167 | table = (xmlEntitiesTablePtr) dtd->pentities; |
| 168 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 169 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 170 | } |
| 171 | return(NULL); |
| 172 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 173 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 174 | |
| 175 | /************************************************************************ |
| 176 | * * |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 177 | * QName handling helper * |
| 178 | * * |
| 179 | ************************************************************************/ |
| 180 | |
| 181 | /** |
| 182 | * xmlBuildQName: |
| 183 | * @ncname: the Name |
| 184 | * @prefix: the prefix |
| 185 | * @memory: preallocated memory |
| 186 | * @len: preallocated memory length |
| 187 | * |
| 188 | * Builds the QName @prefix:@ncname in @memory if there is enough space |
| 189 | * and prefix is not NULL nor empty, otherwise allocate a new string. |
| 190 | * If prefix is NULL or empty it returns ncname. |
| 191 | * |
| 192 | * Returns the new string which must be freed by the caller if different from |
| 193 | * @memory and @ncname or NULL in case of error |
| 194 | */ |
| 195 | xmlChar * |
| 196 | xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix, |
| 197 | xmlChar *memory, int len) { |
| 198 | int lenn, lenp; |
| 199 | xmlChar *ret; |
| 200 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 201 | if (ncname == NULL) return(NULL); |
| 202 | if (prefix == NULL) return((xmlChar *) ncname); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 203 | |
| 204 | lenn = strlen((char *) ncname); |
| 205 | lenp = strlen((char *) prefix); |
| 206 | |
| 207 | if ((memory == NULL) || (len < lenn + lenp + 2)) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 208 | ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 209 | if (ret == NULL) { |
| 210 | xmlTreeErrMemory("building QName"); |
| 211 | return(NULL); |
| 212 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 213 | } else { |
| 214 | ret = memory; |
| 215 | } |
| 216 | memcpy(&ret[0], prefix, lenp); |
| 217 | ret[lenp] = ':'; |
| 218 | memcpy(&ret[lenp + 1], ncname, lenn); |
| 219 | ret[lenn + lenp + 1] = 0; |
| 220 | return(ret); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * xmlSplitQName2: |
| 225 | * @name: the full QName |
| 226 | * @prefix: a xmlChar ** |
| 227 | * |
| 228 | * parse an XML qualified name string |
| 229 | * |
| 230 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 231 | * |
| 232 | * [NS 6] Prefix ::= NCName |
| 233 | * |
| 234 | * [NS 7] LocalPart ::= NCName |
| 235 | * |
| 236 | * Returns NULL if not a QName, otherwise the local part, and prefix |
| 237 | * is updated to get the Prefix if any. |
| 238 | */ |
| 239 | |
| 240 | xmlChar * |
| 241 | xmlSplitQName2(const xmlChar *name, xmlChar **prefix) { |
| 242 | int len = 0; |
| 243 | xmlChar *ret = NULL; |
| 244 | |
| 245 | *prefix = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 246 | if (name == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 247 | |
| 248 | #ifndef XML_XML_NAMESPACE |
| 249 | /* xml: prefix is not really a namespace */ |
| 250 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 251 | (name[2] == 'l') && (name[3] == ':')) |
| 252 | return(NULL); |
| 253 | #endif |
| 254 | |
| 255 | /* nasty but valid */ |
| 256 | if (name[0] == ':') |
| 257 | return(NULL); |
| 258 | |
| 259 | /* |
| 260 | * we are not trying to validate but just to cut, and yes it will |
| 261 | * work even if this is as set of UTF-8 encoded chars |
| 262 | */ |
| 263 | while ((name[len] != 0) && (name[len] != ':')) |
| 264 | len++; |
| 265 | |
| 266 | if (name[len] == 0) |
| 267 | return(NULL); |
| 268 | |
| 269 | *prefix = xmlStrndup(name, len); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 270 | if (*prefix == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 271 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 272 | return(NULL); |
| 273 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 274 | ret = xmlStrdup(&name[len + 1]); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 275 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 276 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 277 | if (*prefix != NULL) { |
| 278 | xmlFree(*prefix); |
| 279 | *prefix = NULL; |
| 280 | } |
| 281 | return(NULL); |
| 282 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 283 | |
| 284 | return(ret); |
| 285 | } |
| 286 | |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 287 | /** |
| 288 | * xmlSplitQName3: |
| 289 | * @name: the full QName |
| 290 | * @len: an int * |
| 291 | * |
| 292 | * parse an XML qualified name string,i |
| 293 | * |
| 294 | * returns NULL if it is not a Qualified Name, otherwise, update len |
| 295 | * with the lenght in byte of the prefix and return a pointer |
| 296 | */ |
| 297 | |
| 298 | const xmlChar * |
| 299 | xmlSplitQName3(const xmlChar *name, int *len) { |
| 300 | int l = 0; |
| 301 | |
| 302 | if (name == NULL) return(NULL); |
| 303 | if (len == NULL) return(NULL); |
| 304 | |
| 305 | /* nasty but valid */ |
| 306 | if (name[0] == ':') |
| 307 | return(NULL); |
| 308 | |
| 309 | /* |
| 310 | * we are not trying to validate but just to cut, and yes it will |
| 311 | * work even if this is as set of UTF-8 encoded chars |
| 312 | */ |
| 313 | while ((name[l] != 0) && (name[l] != ':')) |
| 314 | l++; |
| 315 | |
| 316 | if (name[l] == 0) |
| 317 | return(NULL); |
| 318 | |
| 319 | *len = l; |
| 320 | |
| 321 | return(&name[l+1]); |
| 322 | } |
| 323 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 324 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 325 | /************************************************************************ |
| 326 | * * |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 327 | * Check Name, NCName and QName strings * |
| 328 | * * |
| 329 | ************************************************************************/ |
| 330 | |
| 331 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l) |
| 332 | |
| 333 | /** |
| 334 | * xmlValidateNCName: |
| 335 | * @value: the value to check |
| 336 | * @space: allow spaces in front and end of the string |
| 337 | * |
| 338 | * Check that a value conforms to the lexical space of NCName |
| 339 | * |
| 340 | * Returns 0 if this validates, a positive error code number otherwise |
| 341 | * and -1 in case of internal or API error. |
| 342 | */ |
| 343 | int |
| 344 | xmlValidateNCName(const xmlChar *value, int space) { |
| 345 | const xmlChar *cur = value; |
| 346 | int c,l; |
| 347 | |
| 348 | /* |
| 349 | * First quick algorithm for ASCII range |
| 350 | */ |
| 351 | if (space) |
| 352 | while (IS_BLANK(*cur)) cur++; |
| 353 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 354 | (*cur == '_')) |
| 355 | cur++; |
| 356 | else |
| 357 | goto try_complex; |
| 358 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 359 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 360 | ((*cur >= '0') && (*cur <= '9')) || |
| 361 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 362 | cur++; |
| 363 | if (space) |
| 364 | while (IS_BLANK(*cur)) cur++; |
| 365 | if (*cur == 0) |
| 366 | return(0); |
| 367 | |
| 368 | try_complex: |
| 369 | /* |
| 370 | * Second check for chars outside the ASCII range |
| 371 | */ |
| 372 | cur = value; |
| 373 | c = CUR_SCHAR(cur, l); |
| 374 | if (space) { |
| 375 | while (IS_BLANK(c)) { |
| 376 | cur += l; |
| 377 | c = CUR_SCHAR(cur, l); |
| 378 | } |
| 379 | } |
| 380 | if ((!xmlIsLetter(c)) && (c != '_')) |
| 381 | return(1); |
| 382 | cur += l; |
| 383 | c = CUR_SCHAR(cur, l); |
| 384 | while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || |
| 385 | (c == '-') || (c == '_') || xmlIsCombining(c) || |
| 386 | xmlIsExtender(c)) { |
| 387 | cur += l; |
| 388 | c = CUR_SCHAR(cur, l); |
| 389 | } |
| 390 | if (space) { |
| 391 | while (IS_BLANK(c)) { |
| 392 | cur += l; |
| 393 | c = CUR_SCHAR(cur, l); |
| 394 | } |
| 395 | } |
| 396 | if (c != 0) |
| 397 | return(1); |
| 398 | |
| 399 | return(0); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * xmlValidateQName: |
| 404 | * @value: the value to check |
| 405 | * @space: allow spaces in front and end of the string |
| 406 | * |
| 407 | * Check that a value conforms to the lexical space of QName |
| 408 | * |
| 409 | * Returns 0 if this validates, a positive error code number otherwise |
| 410 | * and -1 in case of internal or API error. |
| 411 | */ |
| 412 | int |
| 413 | xmlValidateQName(const xmlChar *value, int space) { |
| 414 | const xmlChar *cur = value; |
| 415 | int c,l; |
| 416 | |
| 417 | /* |
| 418 | * First quick algorithm for ASCII range |
| 419 | */ |
| 420 | if (space) |
| 421 | while (IS_BLANK(*cur)) cur++; |
| 422 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 423 | (*cur == '_')) |
| 424 | cur++; |
| 425 | else |
| 426 | goto try_complex; |
| 427 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 428 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 429 | ((*cur >= '0') && (*cur <= '9')) || |
| 430 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 431 | cur++; |
| 432 | if (*cur == ':') { |
| 433 | cur++; |
| 434 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 435 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 436 | (*cur == '_')) |
| 437 | cur++; |
| 438 | else |
| 439 | goto try_complex; |
| 440 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 441 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 442 | ((*cur >= '0') && (*cur <= '9')) || |
| 443 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 444 | cur++; |
| 445 | } |
| 446 | if (space) |
| 447 | while (IS_BLANK(*cur)) cur++; |
| 448 | if (*cur == 0) |
| 449 | return(0); |
| 450 | |
| 451 | try_complex: |
| 452 | /* |
| 453 | * Second check for chars outside the ASCII range |
| 454 | */ |
| 455 | cur = value; |
| 456 | c = CUR_SCHAR(cur, l); |
| 457 | if (space) { |
| 458 | while (IS_BLANK(c)) { |
| 459 | cur += l; |
| 460 | c = CUR_SCHAR(cur, l); |
| 461 | } |
| 462 | } |
| 463 | if ((!xmlIsLetter(c)) && (c != '_')) |
| 464 | return(1); |
| 465 | cur += l; |
| 466 | c = CUR_SCHAR(cur, l); |
| 467 | while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || |
| 468 | (c == '-') || (c == '_') || xmlIsCombining(c) || |
| 469 | xmlIsExtender(c)) { |
| 470 | cur += l; |
| 471 | c = CUR_SCHAR(cur, l); |
| 472 | } |
| 473 | if (c == ':') { |
| 474 | cur += l; |
| 475 | c = CUR_SCHAR(cur, l); |
| 476 | if ((!xmlIsLetter(c)) && (c != '_')) |
| 477 | return(1); |
| 478 | cur += l; |
| 479 | c = CUR_SCHAR(cur, l); |
| 480 | while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || |
| 481 | (c == '-') || (c == '_') || xmlIsCombining(c) || |
| 482 | xmlIsExtender(c)) { |
| 483 | cur += l; |
| 484 | c = CUR_SCHAR(cur, l); |
| 485 | } |
| 486 | } |
| 487 | if (space) { |
| 488 | while (IS_BLANK(c)) { |
| 489 | cur += l; |
| 490 | c = CUR_SCHAR(cur, l); |
| 491 | } |
| 492 | } |
| 493 | if (c != 0) |
| 494 | return(1); |
| 495 | return(0); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * xmlValidateName: |
| 500 | * @value: the value to check |
| 501 | * @space: allow spaces in front and end of the string |
| 502 | * |
| 503 | * Check that a value conforms to the lexical space of Name |
| 504 | * |
| 505 | * Returns 0 if this validates, a positive error code number otherwise |
| 506 | * and -1 in case of internal or API error. |
| 507 | */ |
| 508 | int |
| 509 | xmlValidateName(const xmlChar *value, int space) { |
| 510 | const xmlChar *cur = value; |
| 511 | int c,l; |
| 512 | |
| 513 | /* |
| 514 | * First quick algorithm for ASCII range |
| 515 | */ |
| 516 | if (space) |
| 517 | while (IS_BLANK(*cur)) cur++; |
| 518 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 519 | (*cur == '_') || (*cur == ':')) |
| 520 | cur++; |
| 521 | else |
| 522 | goto try_complex; |
| 523 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 524 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 525 | ((*cur >= '0') && (*cur <= '9')) || |
| 526 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 527 | cur++; |
| 528 | if (space) |
| 529 | while (IS_BLANK(*cur)) cur++; |
| 530 | if (*cur == 0) |
| 531 | return(0); |
| 532 | |
| 533 | try_complex: |
| 534 | /* |
| 535 | * Second check for chars outside the ASCII range |
| 536 | */ |
| 537 | cur = value; |
| 538 | c = CUR_SCHAR(cur, l); |
| 539 | if (space) { |
| 540 | while (IS_BLANK(c)) { |
| 541 | cur += l; |
| 542 | c = CUR_SCHAR(cur, l); |
| 543 | } |
| 544 | } |
| 545 | if ((!xmlIsLetter(c)) && (c != '_') && (c != ':')) |
| 546 | return(1); |
| 547 | cur += l; |
| 548 | c = CUR_SCHAR(cur, l); |
| 549 | while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || (c == ':') || |
| 550 | (c == '-') || (c == '_') || xmlIsCombining(c) || xmlIsExtender(c)) { |
| 551 | cur += l; |
| 552 | c = CUR_SCHAR(cur, l); |
| 553 | } |
| 554 | if (space) { |
| 555 | while (IS_BLANK(c)) { |
| 556 | cur += l; |
| 557 | c = CUR_SCHAR(cur, l); |
| 558 | } |
| 559 | } |
| 560 | if (c != 0) |
| 561 | return(1); |
| 562 | return(0); |
| 563 | } |
| 564 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 565 | /** |
| 566 | * xmlValidateNMToken: |
| 567 | * @value: the value to check |
| 568 | * @space: allow spaces in front and end of the string |
| 569 | * |
| 570 | * Check that a value conforms to the lexical space of NMToken |
| 571 | * |
| 572 | * Returns 0 if this validates, a positive error code number otherwise |
| 573 | * and -1 in case of internal or API error. |
| 574 | */ |
| 575 | int |
| 576 | xmlValidateNMToken(const xmlChar *value, int space) { |
| 577 | const xmlChar *cur = value; |
| 578 | int c,l; |
| 579 | |
| 580 | /* |
| 581 | * First quick algorithm for ASCII range |
| 582 | */ |
| 583 | if (space) |
| 584 | while (IS_BLANK(*cur)) cur++; |
| 585 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 586 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 587 | ((*cur >= '0') && (*cur <= '9')) || |
| 588 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 589 | cur++; |
| 590 | else |
| 591 | goto try_complex; |
| 592 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 593 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 594 | ((*cur >= '0') && (*cur <= '9')) || |
| 595 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 596 | cur++; |
| 597 | if (space) |
| 598 | while (IS_BLANK(*cur)) cur++; |
| 599 | if (*cur == 0) |
| 600 | return(0); |
| 601 | |
| 602 | try_complex: |
| 603 | /* |
| 604 | * Second check for chars outside the ASCII range |
| 605 | */ |
| 606 | cur = value; |
| 607 | c = CUR_SCHAR(cur, l); |
| 608 | if (space) { |
| 609 | while (IS_BLANK(c)) { |
| 610 | cur += l; |
| 611 | c = CUR_SCHAR(cur, l); |
| 612 | } |
| 613 | } |
| 614 | if (!(xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || (c == ':') || |
| 615 | (c == '-') || (c == '_') || xmlIsCombining(c) || xmlIsExtender(c))) |
| 616 | return(1); |
| 617 | cur += l; |
| 618 | c = CUR_SCHAR(cur, l); |
| 619 | while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || (c == ':') || |
| 620 | (c == '-') || (c == '_') || xmlIsCombining(c) || xmlIsExtender(c)) { |
| 621 | cur += l; |
| 622 | c = CUR_SCHAR(cur, l); |
| 623 | } |
| 624 | if (space) { |
| 625 | while (IS_BLANK(c)) { |
| 626 | cur += l; |
| 627 | c = CUR_SCHAR(cur, l); |
| 628 | } |
| 629 | } |
| 630 | if (c != 0) |
| 631 | return(1); |
| 632 | return(0); |
| 633 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 634 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 635 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 636 | /************************************************************************ |
| 637 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 638 | * Allocation and deallocation of basic structures * |
| 639 | * * |
| 640 | ************************************************************************/ |
| 641 | |
| 642 | /** |
| 643 | * xmlSetBufferAllocationScheme: |
| 644 | * @scheme: allocation method to use |
| 645 | * |
| 646 | * Set the buffer allocation method. Types are |
| 647 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 648 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 649 | * improves performance |
| 650 | */ |
| 651 | void |
| 652 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
| 653 | xmlBufferAllocScheme = scheme; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * xmlGetBufferAllocationScheme: |
| 658 | * |
| 659 | * Types are |
| 660 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 661 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 662 | * improves performance |
| 663 | * |
| 664 | * Returns the current allocation scheme |
| 665 | */ |
| 666 | xmlBufferAllocationScheme |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 667 | xmlGetBufferAllocationScheme(void) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 668 | return(xmlBufferAllocScheme); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /** |
| 672 | * xmlNewNs: |
| 673 | * @node: the element carrying the namespace |
| 674 | * @href: the URI associated |
| 675 | * @prefix: the prefix for the namespace |
| 676 | * |
| 677 | * Creation of a new Namespace. This function will refuse to create |
| 678 | * a namespace with a similar prefix than an existing one present on this |
| 679 | * node. |
| 680 | * We use href==NULL in the case of an element creation where the namespace |
| 681 | * was not defined. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 682 | * Returns a new namespace pointer or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 683 | */ |
| 684 | xmlNsPtr |
| 685 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
| 686 | xmlNsPtr cur; |
| 687 | |
| 688 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 689 | return(NULL); |
| 690 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 691 | if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) |
| 692 | return(NULL); |
| 693 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 694 | /* |
| 695 | * Allocate a new Namespace and fill the fields. |
| 696 | */ |
| 697 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 698 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 699 | xmlTreeErrMemory("building namespace"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 700 | return(NULL); |
| 701 | } |
| 702 | memset(cur, 0, sizeof(xmlNs)); |
| 703 | cur->type = XML_LOCAL_NAMESPACE; |
| 704 | |
| 705 | if (href != NULL) |
| 706 | cur->href = xmlStrdup(href); |
| 707 | if (prefix != NULL) |
| 708 | cur->prefix = xmlStrdup(prefix); |
| 709 | |
| 710 | /* |
| 711 | * Add it at the end to preserve parsing order ... |
| 712 | * and checks for existing use of the prefix |
| 713 | */ |
| 714 | if (node != NULL) { |
| 715 | if (node->nsDef == NULL) { |
| 716 | node->nsDef = cur; |
| 717 | } else { |
| 718 | xmlNsPtr prev = node->nsDef; |
| 719 | |
| 720 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 721 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 722 | xmlFreeNs(cur); |
| 723 | return(NULL); |
| 724 | } |
| 725 | while (prev->next != NULL) { |
| 726 | prev = prev->next; |
| 727 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 728 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 729 | xmlFreeNs(cur); |
| 730 | return(NULL); |
| 731 | } |
| 732 | } |
| 733 | prev->next = cur; |
| 734 | } |
| 735 | } |
| 736 | return(cur); |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * xmlSetNs: |
| 741 | * @node: a node in the document |
| 742 | * @ns: a namespace pointer |
| 743 | * |
| 744 | * Associate a namespace to a node, a posteriori. |
| 745 | */ |
| 746 | void |
| 747 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
| 748 | if (node == NULL) { |
| 749 | #ifdef DEBUG_TREE |
| 750 | xmlGenericError(xmlGenericErrorContext, |
| 751 | "xmlSetNs: node == NULL\n"); |
| 752 | #endif |
| 753 | return; |
| 754 | } |
| 755 | node->ns = ns; |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * xmlFreeNs: |
| 760 | * @cur: the namespace pointer |
| 761 | * |
| 762 | * Free up the structures associated to a namespace |
| 763 | */ |
| 764 | void |
| 765 | xmlFreeNs(xmlNsPtr cur) { |
| 766 | if (cur == NULL) { |
| 767 | #ifdef DEBUG_TREE |
| 768 | xmlGenericError(xmlGenericErrorContext, |
| 769 | "xmlFreeNs : ns == NULL\n"); |
| 770 | #endif |
| 771 | return; |
| 772 | } |
| 773 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 774 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 775 | xmlFree(cur); |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * xmlFreeNsList: |
| 780 | * @cur: the first namespace pointer |
| 781 | * |
| 782 | * Free up all the structures associated to the chained namespaces. |
| 783 | */ |
| 784 | void |
| 785 | xmlFreeNsList(xmlNsPtr cur) { |
| 786 | xmlNsPtr next; |
| 787 | if (cur == NULL) { |
| 788 | #ifdef DEBUG_TREE |
| 789 | xmlGenericError(xmlGenericErrorContext, |
| 790 | "xmlFreeNsList : ns == NULL\n"); |
| 791 | #endif |
| 792 | return; |
| 793 | } |
| 794 | while (cur != NULL) { |
| 795 | next = cur->next; |
| 796 | xmlFreeNs(cur); |
| 797 | cur = next; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /** |
| 802 | * xmlNewDtd: |
| 803 | * @doc: the document pointer |
| 804 | * @name: the DTD name |
| 805 | * @ExternalID: the external ID |
| 806 | * @SystemID: the system ID |
| 807 | * |
| 808 | * Creation of a new DTD for the external subset. To create an |
| 809 | * internal subset, use xmlCreateIntSubset(). |
| 810 | * |
| 811 | * Returns a pointer to the new DTD structure |
| 812 | */ |
| 813 | xmlDtdPtr |
| 814 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 815 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 816 | xmlDtdPtr cur; |
| 817 | |
| 818 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
| 819 | #ifdef DEBUG_TREE |
| 820 | xmlGenericError(xmlGenericErrorContext, |
| 821 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
| 822 | /* !!! */ (char *) name, doc->name, |
| 823 | /* !!! */ (char *)doc->extSubset->name); |
| 824 | #endif |
| 825 | return(NULL); |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * Allocate a new DTD and fill the fields. |
| 830 | */ |
| 831 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 832 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 833 | xmlTreeErrMemory("building DTD"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 834 | return(NULL); |
| 835 | } |
| 836 | memset(cur, 0 , sizeof(xmlDtd)); |
| 837 | cur->type = XML_DTD_NODE; |
| 838 | |
| 839 | if (name != NULL) |
| 840 | cur->name = xmlStrdup(name); |
| 841 | if (ExternalID != NULL) |
| 842 | cur->ExternalID = xmlStrdup(ExternalID); |
| 843 | if (SystemID != NULL) |
| 844 | cur->SystemID = xmlStrdup(SystemID); |
| 845 | if (doc != NULL) |
| 846 | doc->extSubset = cur; |
| 847 | cur->doc = doc; |
| 848 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 849 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 850 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 851 | return(cur); |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * xmlGetIntSubset: |
| 856 | * @doc: the document pointer |
| 857 | * |
| 858 | * Get the internal subset of a document |
| 859 | * Returns a pointer to the DTD structure or NULL if not found |
| 860 | */ |
| 861 | |
| 862 | xmlDtdPtr |
| 863 | xmlGetIntSubset(xmlDocPtr doc) { |
| 864 | xmlNodePtr cur; |
| 865 | |
| 866 | if (doc == NULL) |
| 867 | return(NULL); |
| 868 | cur = doc->children; |
| 869 | while (cur != NULL) { |
| 870 | if (cur->type == XML_DTD_NODE) |
| 871 | return((xmlDtdPtr) cur); |
| 872 | cur = cur->next; |
| 873 | } |
| 874 | return((xmlDtdPtr) doc->intSubset); |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * xmlCreateIntSubset: |
| 879 | * @doc: the document pointer |
| 880 | * @name: the DTD name |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 881 | * @ExternalID: the external (PUBLIC) ID |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 882 | * @SystemID: the system ID |
| 883 | * |
| 884 | * Create the internal subset of a document |
| 885 | * Returns a pointer to the new DTD structure |
| 886 | */ |
| 887 | xmlDtdPtr |
| 888 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 889 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 890 | xmlDtdPtr cur; |
| 891 | |
| 892 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
| 893 | #ifdef DEBUG_TREE |
| 894 | xmlGenericError(xmlGenericErrorContext, |
| 895 | |
| 896 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 897 | doc->name); |
| 898 | #endif |
| 899 | return(NULL); |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * Allocate a new DTD and fill the fields. |
| 904 | */ |
| 905 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 906 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 907 | xmlTreeErrMemory("building internal subset"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 908 | return(NULL); |
| 909 | } |
| 910 | memset(cur, 0, sizeof(xmlDtd)); |
| 911 | cur->type = XML_DTD_NODE; |
| 912 | |
| 913 | if (name != NULL) |
| 914 | cur->name = xmlStrdup(name); |
| 915 | if (ExternalID != NULL) |
| 916 | cur->ExternalID = xmlStrdup(ExternalID); |
| 917 | if (SystemID != NULL) |
| 918 | cur->SystemID = xmlStrdup(SystemID); |
| 919 | if (doc != NULL) { |
| 920 | doc->intSubset = cur; |
| 921 | cur->parent = doc; |
| 922 | cur->doc = doc; |
| 923 | if (doc->children == NULL) { |
| 924 | doc->children = (xmlNodePtr) cur; |
| 925 | doc->last = (xmlNodePtr) cur; |
| 926 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 927 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 928 | xmlNodePtr prev; |
| 929 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 930 | prev = doc->children; |
| 931 | prev->prev = (xmlNodePtr) cur; |
| 932 | cur->next = prev; |
| 933 | doc->children = (xmlNodePtr) cur; |
| 934 | } else { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 935 | xmlNodePtr next; |
| 936 | |
| 937 | next = doc->children; |
| 938 | while ((next != NULL) && (next->type != XML_ELEMENT_NODE)) |
| 939 | next = next->next; |
| 940 | if (next == NULL) { |
| 941 | cur->prev = doc->last; |
| 942 | cur->prev->next = (xmlNodePtr) cur; |
| 943 | cur->next = NULL; |
| 944 | doc->last = (xmlNodePtr) cur; |
| 945 | } else { |
| 946 | cur->next = next; |
| 947 | cur->prev = next->prev; |
| 948 | if (cur->prev == NULL) |
| 949 | doc->children = (xmlNodePtr) cur; |
| 950 | else |
| 951 | cur->prev->next = (xmlNodePtr) cur; |
| 952 | next->prev = (xmlNodePtr) cur; |
| 953 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 957 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 958 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 959 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 960 | return(cur); |
| 961 | } |
| 962 | |
| 963 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 964 | * DICT_FREE: |
| 965 | * @str: a string |
| 966 | * |
| 967 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 968 | * current scope |
| 969 | */ |
| 970 | #define DICT_FREE(str) \ |
| 971 | if ((str) && ((!dict) || \ |
| 972 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 973 | xmlFree((char *)(str)); |
| 974 | |
| 975 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 976 | * xmlFreeDtd: |
| 977 | * @cur: the DTD structure to free up |
| 978 | * |
| 979 | * Free a DTD structure. |
| 980 | */ |
| 981 | void |
| 982 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 983 | xmlDictPtr dict = NULL; |
| 984 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 985 | if (cur == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 986 | return; |
| 987 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 988 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 989 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 990 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 991 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 992 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 993 | if (cur->children != NULL) { |
| 994 | xmlNodePtr next, c = cur->children; |
| 995 | |
| 996 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 997 | * Cleanup all the DTD comments they are not in the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 998 | * indexes. |
| 999 | */ |
| 1000 | while (c != NULL) { |
| 1001 | next = c->next; |
Daniel Veillard | d72c7e3 | 2003-05-12 21:55:03 +0000 | [diff] [blame] | 1002 | if ((c->type == XML_COMMENT_NODE) || (c->type == XML_PI_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1003 | xmlUnlinkNode(c); |
| 1004 | xmlFreeNode(c); |
| 1005 | } |
| 1006 | c = next; |
| 1007 | } |
| 1008 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1009 | DICT_FREE(cur->name) |
| 1010 | DICT_FREE(cur->SystemID) |
| 1011 | DICT_FREE(cur->ExternalID) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1012 | /* TODO !!! */ |
| 1013 | if (cur->notations != NULL) |
| 1014 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
| 1015 | |
| 1016 | if (cur->elements != NULL) |
| 1017 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
| 1018 | if (cur->attributes != NULL) |
| 1019 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
| 1020 | if (cur->entities != NULL) |
| 1021 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 1022 | if (cur->pentities != NULL) |
| 1023 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
| 1024 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1025 | xmlFree(cur); |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * xmlNewDoc: |
| 1030 | * @version: xmlChar string giving the version of XML "1.0" |
| 1031 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1032 | * Creates a new XML document |
| 1033 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1034 | * Returns a new document |
| 1035 | */ |
| 1036 | xmlDocPtr |
| 1037 | xmlNewDoc(const xmlChar *version) { |
| 1038 | xmlDocPtr cur; |
| 1039 | |
| 1040 | if (version == NULL) |
| 1041 | version = (const xmlChar *) "1.0"; |
| 1042 | |
| 1043 | /* |
| 1044 | * Allocate a new document and fill the fields. |
| 1045 | */ |
| 1046 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
| 1047 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1048 | xmlTreeErrMemory("building doc"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1049 | return(NULL); |
| 1050 | } |
| 1051 | memset(cur, 0, sizeof(xmlDoc)); |
| 1052 | cur->type = XML_DOCUMENT_NODE; |
| 1053 | |
| 1054 | cur->version = xmlStrdup(version); |
| 1055 | cur->standalone = -1; |
| 1056 | cur->compression = -1; /* not initialized */ |
| 1057 | cur->doc = cur; |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 1058 | /* |
| 1059 | * The in memory encoding is always UTF8 |
| 1060 | * This field will never change and would |
| 1061 | * be obsolete if not for binary compatibility. |
| 1062 | */ |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 1063 | cur->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1064 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1065 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1066 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1067 | return(cur); |
| 1068 | } |
| 1069 | |
| 1070 | /** |
| 1071 | * xmlFreeDoc: |
| 1072 | * @cur: pointer to the document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1073 | * |
| 1074 | * Free up all the structures used by a document, tree included. |
| 1075 | */ |
| 1076 | void |
| 1077 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1078 | xmlDtdPtr extSubset, intSubset; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1079 | xmlDictPtr dict = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1080 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1081 | if (cur == NULL) { |
| 1082 | #ifdef DEBUG_TREE |
| 1083 | xmlGenericError(xmlGenericErrorContext, |
| 1084 | "xmlFreeDoc : document == NULL\n"); |
| 1085 | #endif |
| 1086 | return; |
| 1087 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1088 | if (cur != NULL) dict = cur->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1089 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1090 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1091 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1092 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1093 | /* |
| 1094 | * Do this before freeing the children list to avoid ID lookups |
| 1095 | */ |
| 1096 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
| 1097 | cur->ids = NULL; |
| 1098 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
| 1099 | cur->refs = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1100 | extSubset = cur->extSubset; |
| 1101 | intSubset = cur->intSubset; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 1102 | if (intSubset == extSubset) |
| 1103 | extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1104 | if (extSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1105 | xmlUnlinkNode((xmlNodePtr) cur->extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1106 | cur->extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1107 | xmlFreeDtd(extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1108 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1109 | if (intSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1110 | xmlUnlinkNode((xmlNodePtr) cur->intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1111 | cur->intSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1112 | xmlFreeDtd(intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1116 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1117 | |
| 1118 | DICT_FREE(cur->version) |
| 1119 | DICT_FREE(cur->name) |
| 1120 | DICT_FREE(cur->encoding) |
| 1121 | DICT_FREE(cur->URL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1122 | xmlFree(cur); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1123 | if (dict) xmlDictFree(dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /** |
| 1127 | * xmlStringLenGetNodeList: |
| 1128 | * @doc: the document |
| 1129 | * @value: the value of the text |
| 1130 | * @len: the length of the string value |
| 1131 | * |
| 1132 | * Parse the value string and build the node list associated. Should |
| 1133 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1134 | * Returns a pointer to the first child |
| 1135 | */ |
| 1136 | xmlNodePtr |
| 1137 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
| 1138 | xmlNodePtr ret = NULL, last = NULL; |
| 1139 | xmlNodePtr node; |
| 1140 | xmlChar *val; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1141 | const xmlChar *cur = value, *end = cur + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1142 | const xmlChar *q; |
| 1143 | xmlEntityPtr ent; |
| 1144 | |
| 1145 | if (value == NULL) return(NULL); |
| 1146 | |
| 1147 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1148 | while ((cur < end) && (*cur != 0)) { |
| 1149 | if (cur[0] == '&') { |
| 1150 | int charval = 0; |
| 1151 | xmlChar tmp; |
| 1152 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1153 | /* |
| 1154 | * Save the current text. |
| 1155 | */ |
| 1156 | if (cur != q) { |
| 1157 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1158 | xmlNodeAddContentLen(last, q, cur - q); |
| 1159 | } else { |
| 1160 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1161 | if (node == NULL) return(ret); |
| 1162 | if (last == NULL) |
| 1163 | last = ret = node; |
| 1164 | else { |
| 1165 | last->next = node; |
| 1166 | node->prev = last; |
| 1167 | last = node; |
| 1168 | } |
| 1169 | } |
| 1170 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1171 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1172 | if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) { |
| 1173 | cur += 3; |
| 1174 | if (cur < end) |
| 1175 | tmp = *cur; |
| 1176 | else |
| 1177 | tmp = 0; |
| 1178 | while (tmp != ';') { /* Non input consuming loop */ |
| 1179 | if ((tmp >= '0') && (tmp <= '9')) |
| 1180 | charval = charval * 16 + (tmp - '0'); |
| 1181 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1182 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1183 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1184 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1185 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1186 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1187 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1188 | charval = 0; |
| 1189 | break; |
| 1190 | } |
| 1191 | cur++; |
| 1192 | if (cur < end) |
| 1193 | tmp = *cur; |
| 1194 | else |
| 1195 | tmp = 0; |
| 1196 | } |
| 1197 | if (tmp == ';') |
| 1198 | cur++; |
| 1199 | q = cur; |
| 1200 | } else if ((cur + 1 < end) && (cur[1] == '#')) { |
| 1201 | cur += 2; |
| 1202 | if (cur < end) |
| 1203 | tmp = *cur; |
| 1204 | else |
| 1205 | tmp = 0; |
| 1206 | while (tmp != ';') { /* Non input consuming loops */ |
| 1207 | if ((tmp >= '0') && (tmp <= '9')) |
| 1208 | charval = charval * 10 + (tmp - '0'); |
| 1209 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1210 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1211 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1212 | charval = 0; |
| 1213 | break; |
| 1214 | } |
| 1215 | cur++; |
| 1216 | if (cur < end) |
| 1217 | tmp = *cur; |
| 1218 | else |
| 1219 | tmp = 0; |
| 1220 | } |
| 1221 | if (tmp == ';') |
| 1222 | cur++; |
| 1223 | q = cur; |
| 1224 | } else { |
| 1225 | /* |
| 1226 | * Read the entity string |
| 1227 | */ |
| 1228 | cur++; |
| 1229 | q = cur; |
| 1230 | while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++; |
| 1231 | if ((cur >= end) || (*cur == 0)) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1232 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc, |
| 1233 | (const char *) q); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1234 | return(ret); |
| 1235 | } |
| 1236 | if (cur != q) { |
| 1237 | /* |
| 1238 | * Predefined entities don't generate nodes |
| 1239 | */ |
| 1240 | val = xmlStrndup(q, cur - q); |
| 1241 | ent = xmlGetDocEntity(doc, val); |
| 1242 | if ((ent != NULL) && |
| 1243 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1244 | if (last == NULL) { |
| 1245 | node = xmlNewDocText(doc, ent->content); |
| 1246 | last = ret = node; |
| 1247 | } else if (last->type != XML_TEXT_NODE) { |
| 1248 | node = xmlNewDocText(doc, ent->content); |
| 1249 | last = xmlAddNextSibling(last, node); |
| 1250 | } else |
| 1251 | xmlNodeAddContent(last, ent->content); |
| 1252 | |
| 1253 | } else { |
| 1254 | /* |
| 1255 | * Create a new REFERENCE_REF node |
| 1256 | */ |
| 1257 | node = xmlNewReference(doc, val); |
| 1258 | if (node == NULL) { |
| 1259 | if (val != NULL) xmlFree(val); |
| 1260 | return(ret); |
| 1261 | } |
| 1262 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1263 | xmlNodePtr temp; |
| 1264 | |
| 1265 | ent->children = xmlStringGetNodeList(doc, |
| 1266 | (const xmlChar*)node->content); |
| 1267 | ent->owner = 1; |
| 1268 | temp = ent->children; |
| 1269 | while (temp) { |
| 1270 | temp->parent = (xmlNodePtr)ent; |
| 1271 | temp = temp->next; |
| 1272 | } |
| 1273 | } |
| 1274 | if (last == NULL) { |
| 1275 | last = ret = node; |
| 1276 | } else { |
| 1277 | last = xmlAddNextSibling(last, node); |
| 1278 | } |
| 1279 | } |
| 1280 | xmlFree(val); |
| 1281 | } |
| 1282 | cur++; |
| 1283 | q = cur; |
| 1284 | } |
| 1285 | if (charval != 0) { |
| 1286 | xmlChar buf[10]; |
| 1287 | int l; |
| 1288 | |
| 1289 | l = xmlCopyCharMultiByte(buf, charval); |
| 1290 | buf[l] = 0; |
| 1291 | node = xmlNewDocText(doc, buf); |
| 1292 | if (node != NULL) { |
| 1293 | if (last == NULL) { |
| 1294 | last = ret = node; |
| 1295 | } else { |
| 1296 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1297 | } |
| 1298 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1299 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1300 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1301 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1302 | cur++; |
| 1303 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1304 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1305 | /* |
| 1306 | * Handle the last piece of text. |
| 1307 | */ |
| 1308 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1309 | xmlNodeAddContentLen(last, q, cur - q); |
| 1310 | } else { |
| 1311 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1312 | if (node == NULL) return(ret); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1313 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1314 | last = ret = node; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1315 | } else { |
| 1316 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1317 | } |
| 1318 | } |
| 1319 | } |
| 1320 | return(ret); |
| 1321 | } |
| 1322 | |
| 1323 | /** |
| 1324 | * xmlStringGetNodeList: |
| 1325 | * @doc: the document |
| 1326 | * @value: the value of the attribute |
| 1327 | * |
| 1328 | * Parse the value string and build the node list associated. Should |
| 1329 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1330 | * Returns a pointer to the first child |
| 1331 | */ |
| 1332 | xmlNodePtr |
| 1333 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
| 1334 | xmlNodePtr ret = NULL, last = NULL; |
| 1335 | xmlNodePtr node; |
| 1336 | xmlChar *val; |
| 1337 | const xmlChar *cur = value; |
| 1338 | const xmlChar *q; |
| 1339 | xmlEntityPtr ent; |
| 1340 | |
| 1341 | if (value == NULL) return(NULL); |
| 1342 | |
| 1343 | q = cur; |
| 1344 | while (*cur != 0) { |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1345 | if (cur[0] == '&') { |
| 1346 | int charval = 0; |
| 1347 | xmlChar tmp; |
| 1348 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1349 | /* |
| 1350 | * Save the current text. |
| 1351 | */ |
| 1352 | if (cur != q) { |
| 1353 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1354 | xmlNodeAddContentLen(last, q, cur - q); |
| 1355 | } else { |
| 1356 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1357 | if (node == NULL) return(ret); |
| 1358 | if (last == NULL) |
| 1359 | last = ret = node; |
| 1360 | else { |
| 1361 | last->next = node; |
| 1362 | node->prev = last; |
| 1363 | last = node; |
| 1364 | } |
| 1365 | } |
| 1366 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1367 | q = cur; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1368 | if ((cur[1] == '#') && (cur[2] == 'x')) { |
| 1369 | cur += 3; |
| 1370 | tmp = *cur; |
| 1371 | while (tmp != ';') { /* Non input consuming loop */ |
| 1372 | if ((tmp >= '0') && (tmp <= '9')) |
| 1373 | charval = charval * 16 + (tmp - '0'); |
| 1374 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1375 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1376 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1377 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1378 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1379 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1380 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1381 | charval = 0; |
| 1382 | break; |
| 1383 | } |
| 1384 | cur++; |
| 1385 | tmp = *cur; |
| 1386 | } |
| 1387 | if (tmp == ';') |
| 1388 | cur++; |
| 1389 | q = cur; |
| 1390 | } else if (cur[1] == '#') { |
| 1391 | cur += 2; |
| 1392 | tmp = *cur; |
| 1393 | while (tmp != ';') { /* Non input consuming loops */ |
| 1394 | if ((tmp >= '0') && (tmp <= '9')) |
| 1395 | charval = charval * 10 + (tmp - '0'); |
| 1396 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1397 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1398 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1399 | charval = 0; |
| 1400 | break; |
| 1401 | } |
| 1402 | cur++; |
| 1403 | tmp = *cur; |
| 1404 | } |
| 1405 | if (tmp == ';') |
| 1406 | cur++; |
| 1407 | q = cur; |
| 1408 | } else { |
| 1409 | /* |
| 1410 | * Read the entity string |
| 1411 | */ |
| 1412 | cur++; |
| 1413 | q = cur; |
| 1414 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 1415 | if (*cur == 0) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1416 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, |
| 1417 | (xmlNodePtr) doc, (const char *) q); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1418 | return(ret); |
| 1419 | } |
| 1420 | if (cur != q) { |
| 1421 | /* |
| 1422 | * Predefined entities don't generate nodes |
| 1423 | */ |
| 1424 | val = xmlStrndup(q, cur - q); |
| 1425 | ent = xmlGetDocEntity(doc, val); |
| 1426 | if ((ent != NULL) && |
| 1427 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1428 | if (last == NULL) { |
| 1429 | node = xmlNewDocText(doc, ent->content); |
| 1430 | last = ret = node; |
Daniel Veillard | 6f42c13 | 2002-01-06 23:05:13 +0000 | [diff] [blame] | 1431 | } else if (last->type != XML_TEXT_NODE) { |
| 1432 | node = xmlNewDocText(doc, ent->content); |
| 1433 | last = xmlAddNextSibling(last, node); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1434 | } else |
| 1435 | xmlNodeAddContent(last, ent->content); |
| 1436 | |
| 1437 | } else { |
| 1438 | /* |
| 1439 | * Create a new REFERENCE_REF node |
| 1440 | */ |
| 1441 | node = xmlNewReference(doc, val); |
| 1442 | if (node == NULL) { |
| 1443 | if (val != NULL) xmlFree(val); |
| 1444 | return(ret); |
| 1445 | } |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1446 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1447 | xmlNodePtr temp; |
| 1448 | |
| 1449 | ent->children = xmlStringGetNodeList(doc, |
| 1450 | (const xmlChar*)node->content); |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 1451 | ent->owner = 1; |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1452 | temp = ent->children; |
| 1453 | while (temp) { |
| 1454 | temp->parent = (xmlNodePtr)ent; |
| 1455 | temp = temp->next; |
| 1456 | } |
| 1457 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1458 | if (last == NULL) { |
| 1459 | last = ret = node; |
| 1460 | } else { |
| 1461 | last = xmlAddNextSibling(last, node); |
| 1462 | } |
| 1463 | } |
| 1464 | xmlFree(val); |
| 1465 | } |
| 1466 | cur++; |
| 1467 | q = cur; |
| 1468 | } |
| 1469 | if (charval != 0) { |
| 1470 | xmlChar buf[10]; |
| 1471 | int len; |
| 1472 | |
| 1473 | len = xmlCopyCharMultiByte(buf, charval); |
| 1474 | buf[len] = 0; |
| 1475 | node = xmlNewDocText(doc, buf); |
| 1476 | if (node != NULL) { |
| 1477 | if (last == NULL) { |
| 1478 | last = ret = node; |
| 1479 | } else { |
| 1480 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1481 | } |
| 1482 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1483 | |
| 1484 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1485 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1486 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1487 | cur++; |
| 1488 | } |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 1489 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1490 | /* |
| 1491 | * Handle the last piece of text. |
| 1492 | */ |
| 1493 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1494 | xmlNodeAddContentLen(last, q, cur - q); |
| 1495 | } else { |
| 1496 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1497 | if (node == NULL) return(ret); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1498 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1499 | last = ret = node; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1500 | } else { |
| 1501 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | return(ret); |
| 1506 | } |
| 1507 | |
| 1508 | /** |
| 1509 | * xmlNodeListGetString: |
| 1510 | * @doc: the document |
| 1511 | * @list: a Node list |
| 1512 | * @inLine: should we replace entity contents or show their external form |
| 1513 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1514 | * Build the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1515 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1516 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1517 | * 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] | 1518 | */ |
| 1519 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1520 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1521 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1522 | xmlNodePtr node = list; |
| 1523 | xmlChar *ret = NULL; |
| 1524 | xmlEntityPtr ent; |
| 1525 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1526 | if (list == NULL) |
| 1527 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1528 | |
| 1529 | while (node != NULL) { |
| 1530 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1531 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1532 | if (inLine) { |
| 1533 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1534 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1535 | xmlChar *buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1536 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1537 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
| 1538 | if (buffer != NULL) { |
| 1539 | ret = xmlStrcat(ret, buffer); |
| 1540 | xmlFree(buffer); |
| 1541 | } |
| 1542 | } |
| 1543 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1544 | if (inLine) { |
| 1545 | ent = xmlGetDocEntity(doc, node->name); |
| 1546 | if (ent != NULL) { |
| 1547 | xmlChar *buffer; |
| 1548 | |
| 1549 | /* an entity content can be any "well balanced chunk", |
| 1550 | * i.e. the result of the content [43] production: |
| 1551 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1552 | * So it can contain text, CDATA section or nested |
| 1553 | * entity reference nodes (among others). |
| 1554 | * -> we recursive call xmlNodeListGetString() |
| 1555 | * which handles these types */ |
| 1556 | buffer = xmlNodeListGetString(doc, ent->children, 1); |
| 1557 | if (buffer != NULL) { |
| 1558 | ret = xmlStrcat(ret, buffer); |
| 1559 | xmlFree(buffer); |
| 1560 | } |
| 1561 | } else { |
| 1562 | ret = xmlStrcat(ret, node->content); |
| 1563 | } |
| 1564 | } else { |
| 1565 | xmlChar buf[2]; |
| 1566 | |
| 1567 | buf[0] = '&'; |
| 1568 | buf[1] = 0; |
| 1569 | ret = xmlStrncat(ret, buf, 1); |
| 1570 | ret = xmlStrcat(ret, node->name); |
| 1571 | buf[0] = ';'; |
| 1572 | buf[1] = 0; |
| 1573 | ret = xmlStrncat(ret, buf, 1); |
| 1574 | } |
| 1575 | } |
| 1576 | #if 0 |
| 1577 | else { |
| 1578 | xmlGenericError(xmlGenericErrorContext, |
| 1579 | "xmlGetNodeListString : invalid node type %d\n", |
| 1580 | node->type); |
| 1581 | } |
| 1582 | #endif |
| 1583 | node = node->next; |
| 1584 | } |
| 1585 | return (ret); |
| 1586 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1587 | |
| 1588 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1589 | /** |
| 1590 | * xmlNodeListGetRawString: |
| 1591 | * @doc: the document |
| 1592 | * @list: a Node list |
| 1593 | * @inLine: should we replace entity contents or show their external form |
| 1594 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1595 | * Builds the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1596 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 1597 | * this function doesn't do any character encoding handling. |
| 1598 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1599 | * 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] | 1600 | */ |
| 1601 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1602 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1603 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1604 | xmlNodePtr node = list; |
| 1605 | xmlChar *ret = NULL; |
| 1606 | xmlEntityPtr ent; |
| 1607 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1608 | if (list == NULL) |
| 1609 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1610 | |
| 1611 | while (node != NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1612 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1613 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1614 | if (inLine) { |
| 1615 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1616 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1617 | xmlChar *buffer; |
| 1618 | |
| 1619 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 1620 | if (buffer != NULL) { |
| 1621 | ret = xmlStrcat(ret, buffer); |
| 1622 | xmlFree(buffer); |
| 1623 | } |
| 1624 | } |
| 1625 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1626 | if (inLine) { |
| 1627 | ent = xmlGetDocEntity(doc, node->name); |
| 1628 | if (ent != NULL) { |
| 1629 | xmlChar *buffer; |
| 1630 | |
| 1631 | /* an entity content can be any "well balanced chunk", |
| 1632 | * i.e. the result of the content [43] production: |
| 1633 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1634 | * So it can contain text, CDATA section or nested |
| 1635 | * entity reference nodes (among others). |
| 1636 | * -> we recursive call xmlNodeListGetRawString() |
| 1637 | * which handles these types */ |
| 1638 | buffer = |
| 1639 | xmlNodeListGetRawString(doc, ent->children, 1); |
| 1640 | if (buffer != NULL) { |
| 1641 | ret = xmlStrcat(ret, buffer); |
| 1642 | xmlFree(buffer); |
| 1643 | } |
| 1644 | } else { |
| 1645 | ret = xmlStrcat(ret, node->content); |
| 1646 | } |
| 1647 | } else { |
| 1648 | xmlChar buf[2]; |
| 1649 | |
| 1650 | buf[0] = '&'; |
| 1651 | buf[1] = 0; |
| 1652 | ret = xmlStrncat(ret, buf, 1); |
| 1653 | ret = xmlStrcat(ret, node->name); |
| 1654 | buf[0] = ';'; |
| 1655 | buf[1] = 0; |
| 1656 | ret = xmlStrncat(ret, buf, 1); |
| 1657 | } |
| 1658 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1659 | #if 0 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1660 | else { |
| 1661 | xmlGenericError(xmlGenericErrorContext, |
| 1662 | "xmlGetNodeListString : invalid node type %d\n", |
| 1663 | node->type); |
| 1664 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1665 | #endif |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1666 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1667 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1668 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1669 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1670 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1671 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1672 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1673 | /** |
| 1674 | * xmlNewProp: |
| 1675 | * @node: the holding node |
| 1676 | * @name: the name of the attribute |
| 1677 | * @value: the value of the attribute |
| 1678 | * |
| 1679 | * Create a new property carried by a node. |
| 1680 | * Returns a pointer to the attribute |
| 1681 | */ |
| 1682 | xmlAttrPtr |
| 1683 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 1684 | xmlAttrPtr cur; |
| 1685 | xmlDocPtr doc = NULL; |
| 1686 | |
| 1687 | if (name == NULL) { |
| 1688 | #ifdef DEBUG_TREE |
| 1689 | xmlGenericError(xmlGenericErrorContext, |
| 1690 | "xmlNewProp : name == NULL\n"); |
| 1691 | #endif |
| 1692 | return(NULL); |
| 1693 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 1694 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 1695 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1696 | |
| 1697 | /* |
| 1698 | * Allocate a new property and fill the fields. |
| 1699 | */ |
| 1700 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1701 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1702 | xmlTreeErrMemory("building attribute"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1703 | return(NULL); |
| 1704 | } |
| 1705 | memset(cur, 0, sizeof(xmlAttr)); |
| 1706 | cur->type = XML_ATTRIBUTE_NODE; |
| 1707 | |
| 1708 | cur->parent = node; |
| 1709 | if (node != NULL) { |
| 1710 | doc = node->doc; |
| 1711 | cur->doc = doc; |
| 1712 | } |
| 1713 | cur->name = xmlStrdup(name); |
| 1714 | if (value != NULL) { |
| 1715 | xmlChar *buffer; |
| 1716 | xmlNodePtr tmp; |
| 1717 | |
| 1718 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1719 | cur->children = xmlStringGetNodeList(doc, buffer); |
| 1720 | cur->last = NULL; |
| 1721 | tmp = cur->children; |
| 1722 | while (tmp != NULL) { |
| 1723 | tmp->parent = (xmlNodePtr) cur; |
| 1724 | tmp->doc = doc; |
| 1725 | if (tmp->next == NULL) |
| 1726 | cur->last = tmp; |
| 1727 | tmp = tmp->next; |
| 1728 | } |
| 1729 | xmlFree(buffer); |
| 1730 | } |
| 1731 | |
| 1732 | /* |
| 1733 | * Add it at the end to preserve parsing order ... |
| 1734 | */ |
| 1735 | if (node != NULL) { |
| 1736 | if (node->properties == NULL) { |
| 1737 | node->properties = cur; |
| 1738 | } else { |
| 1739 | xmlAttrPtr prev = node->properties; |
| 1740 | |
| 1741 | while (prev->next != NULL) prev = prev->next; |
| 1742 | prev->next = cur; |
| 1743 | cur->prev = prev; |
| 1744 | } |
| 1745 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1746 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1747 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1748 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1749 | return(cur); |
| 1750 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1751 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1752 | |
| 1753 | /** |
| 1754 | * xmlNewNsProp: |
| 1755 | * @node: the holding node |
| 1756 | * @ns: the namespace |
| 1757 | * @name: the name of the attribute |
| 1758 | * @value: the value of the attribute |
| 1759 | * |
| 1760 | * Create a new property tagged with a namespace and carried by a node. |
| 1761 | * Returns a pointer to the attribute |
| 1762 | */ |
| 1763 | xmlAttrPtr |
| 1764 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 1765 | const xmlChar *value) { |
| 1766 | xmlAttrPtr cur; |
Daniel Veillard | a682b21 | 2001-06-07 19:59:42 +0000 | [diff] [blame] | 1767 | xmlDocPtr doc = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1768 | |
| 1769 | if (name == NULL) { |
| 1770 | #ifdef DEBUG_TREE |
| 1771 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1772 | "xmlNewNsProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1773 | #endif |
| 1774 | return(NULL); |
| 1775 | } |
| 1776 | |
| 1777 | /* |
| 1778 | * Allocate a new property and fill the fields. |
| 1779 | */ |
| 1780 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1781 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1782 | xmlTreeErrMemory("building attribute"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1783 | return(NULL); |
| 1784 | } |
| 1785 | memset(cur, 0, sizeof(xmlAttr)); |
| 1786 | cur->type = XML_ATTRIBUTE_NODE; |
| 1787 | |
| 1788 | cur->parent = node; |
Daniel Veillard | a682b21 | 2001-06-07 19:59:42 +0000 | [diff] [blame] | 1789 | if (node != NULL) { |
| 1790 | doc = node->doc; |
| 1791 | cur->doc = doc; |
| 1792 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1793 | cur->ns = ns; |
| 1794 | cur->name = xmlStrdup(name); |
| 1795 | if (value != NULL) { |
| 1796 | xmlChar *buffer; |
| 1797 | xmlNodePtr tmp; |
| 1798 | |
Daniel Veillard | a682b21 | 2001-06-07 19:59:42 +0000 | [diff] [blame] | 1799 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1800 | cur->children = xmlStringGetNodeList(doc, buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1801 | cur->last = NULL; |
| 1802 | tmp = cur->children; |
| 1803 | while (tmp != NULL) { |
| 1804 | tmp->parent = (xmlNodePtr) cur; |
| 1805 | if (tmp->next == NULL) |
| 1806 | cur->last = tmp; |
| 1807 | tmp = tmp->next; |
| 1808 | } |
| 1809 | xmlFree(buffer); |
| 1810 | } |
| 1811 | |
| 1812 | /* |
| 1813 | * Add it at the end to preserve parsing order ... |
| 1814 | */ |
| 1815 | if (node != NULL) { |
| 1816 | if (node->properties == NULL) { |
| 1817 | node->properties = cur; |
| 1818 | } else { |
| 1819 | xmlAttrPtr prev = node->properties; |
| 1820 | |
| 1821 | while (prev->next != NULL) prev = prev->next; |
| 1822 | prev->next = cur; |
| 1823 | cur->prev = prev; |
| 1824 | } |
| 1825 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1826 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1827 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1828 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1829 | return(cur); |
| 1830 | } |
| 1831 | |
| 1832 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1833 | * xmlNewNsPropEatName: |
| 1834 | * @node: the holding node |
| 1835 | * @ns: the namespace |
| 1836 | * @name: the name of the attribute |
| 1837 | * @value: the value of the attribute |
| 1838 | * |
| 1839 | * Create a new property tagged with a namespace and carried by a node. |
| 1840 | * Returns a pointer to the attribute |
| 1841 | */ |
| 1842 | xmlAttrPtr |
| 1843 | xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name, |
| 1844 | const xmlChar *value) { |
| 1845 | xmlAttrPtr cur; |
| 1846 | xmlDocPtr doc = NULL; |
| 1847 | |
| 1848 | if (name == NULL) { |
| 1849 | #ifdef DEBUG_TREE |
| 1850 | xmlGenericError(xmlGenericErrorContext, |
| 1851 | "xmlNewNsPropEatName : name == NULL\n"); |
| 1852 | #endif |
| 1853 | return(NULL); |
| 1854 | } |
| 1855 | |
| 1856 | /* |
| 1857 | * Allocate a new property and fill the fields. |
| 1858 | */ |
| 1859 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1860 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1861 | xmlTreeErrMemory("building attribute"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1862 | xmlFree(name); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1863 | return(NULL); |
| 1864 | } |
| 1865 | memset(cur, 0, sizeof(xmlAttr)); |
| 1866 | cur->type = XML_ATTRIBUTE_NODE; |
| 1867 | |
| 1868 | cur->parent = node; |
| 1869 | if (node != NULL) { |
| 1870 | doc = node->doc; |
| 1871 | cur->doc = doc; |
| 1872 | } |
| 1873 | cur->ns = ns; |
| 1874 | cur->name = name; |
| 1875 | if (value != NULL) { |
| 1876 | xmlChar *buffer; |
| 1877 | xmlNodePtr tmp; |
| 1878 | |
| 1879 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1880 | cur->children = xmlStringGetNodeList(doc, buffer); |
| 1881 | cur->last = NULL; |
| 1882 | tmp = cur->children; |
| 1883 | while (tmp != NULL) { |
| 1884 | tmp->parent = (xmlNodePtr) cur; |
| 1885 | if (tmp->next == NULL) |
| 1886 | cur->last = tmp; |
| 1887 | tmp = tmp->next; |
| 1888 | } |
| 1889 | xmlFree(buffer); |
| 1890 | } |
| 1891 | |
| 1892 | /* |
| 1893 | * Add it at the end to preserve parsing order ... |
| 1894 | */ |
| 1895 | if (node != NULL) { |
| 1896 | if (node->properties == NULL) { |
| 1897 | node->properties = cur; |
| 1898 | } else { |
| 1899 | xmlAttrPtr prev = node->properties; |
| 1900 | |
| 1901 | while (prev->next != NULL) prev = prev->next; |
| 1902 | prev->next = cur; |
| 1903 | cur->prev = prev; |
| 1904 | } |
| 1905 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1906 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1907 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1908 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1909 | return(cur); |
| 1910 | } |
| 1911 | |
| 1912 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1913 | * xmlNewDocProp: |
| 1914 | * @doc: the document |
| 1915 | * @name: the name of the attribute |
| 1916 | * @value: the value of the attribute |
| 1917 | * |
| 1918 | * Create a new property carried by a document. |
| 1919 | * Returns a pointer to the attribute |
| 1920 | */ |
| 1921 | xmlAttrPtr |
| 1922 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
| 1923 | xmlAttrPtr cur; |
| 1924 | |
| 1925 | if (name == NULL) { |
| 1926 | #ifdef DEBUG_TREE |
| 1927 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1928 | "xmlNewDocProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1929 | #endif |
| 1930 | return(NULL); |
| 1931 | } |
| 1932 | |
| 1933 | /* |
| 1934 | * Allocate a new property and fill the fields. |
| 1935 | */ |
| 1936 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1937 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1938 | xmlTreeErrMemory("building attribute"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1939 | return(NULL); |
| 1940 | } |
| 1941 | memset(cur, 0, sizeof(xmlAttr)); |
| 1942 | cur->type = XML_ATTRIBUTE_NODE; |
| 1943 | |
| 1944 | cur->name = xmlStrdup(name); |
| 1945 | cur->doc = doc; |
| 1946 | if (value != NULL) { |
| 1947 | xmlNodePtr tmp; |
| 1948 | |
| 1949 | cur->children = xmlStringGetNodeList(doc, value); |
| 1950 | cur->last = NULL; |
| 1951 | |
| 1952 | tmp = cur->children; |
| 1953 | while (tmp != NULL) { |
| 1954 | tmp->parent = (xmlNodePtr) cur; |
| 1955 | if (tmp->next == NULL) |
| 1956 | cur->last = tmp; |
| 1957 | tmp = tmp->next; |
| 1958 | } |
| 1959 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1960 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1961 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1962 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1963 | return(cur); |
| 1964 | } |
| 1965 | |
| 1966 | /** |
| 1967 | * xmlFreePropList: |
| 1968 | * @cur: the first property in the list |
| 1969 | * |
| 1970 | * Free a property and all its siblings, all the children are freed too. |
| 1971 | */ |
| 1972 | void |
| 1973 | xmlFreePropList(xmlAttrPtr cur) { |
| 1974 | xmlAttrPtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1975 | if (cur == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1976 | while (cur != NULL) { |
| 1977 | next = cur->next; |
| 1978 | xmlFreeProp(cur); |
| 1979 | cur = next; |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | /** |
| 1984 | * xmlFreeProp: |
| 1985 | * @cur: an attribute |
| 1986 | * |
| 1987 | * Free one attribute, all the content is freed too |
| 1988 | */ |
| 1989 | void |
| 1990 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1991 | xmlDictPtr dict = NULL; |
| 1992 | if (cur == NULL) return; |
| 1993 | |
| 1994 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1995 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1996 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1997 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1998 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1999 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2000 | if ((cur->parent != NULL) && (cur->parent->doc != NULL) && |
| 2001 | ((cur->parent->doc->intSubset != NULL) || |
| 2002 | (cur->parent->doc->extSubset != NULL))) { |
| 2003 | if (xmlIsID(cur->parent->doc, cur->parent, cur)) |
| 2004 | xmlRemoveID(cur->parent->doc, cur); |
| 2005 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2006 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2007 | DICT_FREE(cur->name) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2008 | xmlFree(cur); |
| 2009 | } |
| 2010 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2011 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2012 | /** |
| 2013 | * xmlRemoveProp: |
| 2014 | * @cur: an attribute |
| 2015 | * |
| 2016 | * Unlink and free one attribute, all the content is freed too |
| 2017 | * Note this doesn't work for namespace definition attributes |
| 2018 | * |
| 2019 | * Returns 0 if success and -1 in case of error. |
| 2020 | */ |
| 2021 | int |
| 2022 | xmlRemoveProp(xmlAttrPtr cur) { |
| 2023 | xmlAttrPtr tmp; |
| 2024 | if (cur == NULL) { |
| 2025 | #ifdef DEBUG_TREE |
| 2026 | xmlGenericError(xmlGenericErrorContext, |
| 2027 | "xmlRemoveProp : cur == NULL\n"); |
| 2028 | #endif |
| 2029 | return(-1); |
| 2030 | } |
| 2031 | if (cur->parent == NULL) { |
| 2032 | #ifdef DEBUG_TREE |
| 2033 | xmlGenericError(xmlGenericErrorContext, |
| 2034 | "xmlRemoveProp : cur->parent == NULL\n"); |
| 2035 | #endif |
| 2036 | return(-1); |
| 2037 | } |
| 2038 | tmp = cur->parent->properties; |
| 2039 | if (tmp == cur) { |
| 2040 | cur->parent->properties = cur->next; |
| 2041 | xmlFreeProp(cur); |
| 2042 | return(0); |
| 2043 | } |
| 2044 | while (tmp != NULL) { |
| 2045 | if (tmp->next == cur) { |
| 2046 | tmp->next = cur->next; |
| 2047 | if (tmp->next != NULL) |
| 2048 | tmp->next->prev = tmp; |
| 2049 | xmlFreeProp(cur); |
| 2050 | return(0); |
| 2051 | } |
| 2052 | tmp = tmp->next; |
| 2053 | } |
| 2054 | #ifdef DEBUG_TREE |
| 2055 | xmlGenericError(xmlGenericErrorContext, |
| 2056 | "xmlRemoveProp : attribute not owned by its node\n"); |
| 2057 | #endif |
| 2058 | return(-1); |
| 2059 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2060 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2061 | |
| 2062 | /** |
| 2063 | * xmlNewPI: |
| 2064 | * @name: the processing instruction name |
| 2065 | * @content: the PI content |
| 2066 | * |
| 2067 | * Creation of a processing instruction element. |
| 2068 | * Returns a pointer to the new node object. |
| 2069 | */ |
| 2070 | xmlNodePtr |
| 2071 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
| 2072 | xmlNodePtr cur; |
| 2073 | |
| 2074 | if (name == NULL) { |
| 2075 | #ifdef DEBUG_TREE |
| 2076 | xmlGenericError(xmlGenericErrorContext, |
| 2077 | "xmlNewPI : name == NULL\n"); |
| 2078 | #endif |
| 2079 | return(NULL); |
| 2080 | } |
| 2081 | |
| 2082 | /* |
| 2083 | * Allocate a new node and fill the fields. |
| 2084 | */ |
| 2085 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2086 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2087 | xmlTreeErrMemory("building PI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2088 | return(NULL); |
| 2089 | } |
| 2090 | memset(cur, 0, sizeof(xmlNode)); |
| 2091 | cur->type = XML_PI_NODE; |
| 2092 | |
| 2093 | cur->name = xmlStrdup(name); |
| 2094 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2095 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2096 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2097 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2098 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2099 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2100 | return(cur); |
| 2101 | } |
| 2102 | |
| 2103 | /** |
| 2104 | * xmlNewNode: |
| 2105 | * @ns: namespace if any |
| 2106 | * @name: the node name |
| 2107 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2108 | * Creation of a new node element. @ns is optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2109 | * |
| 2110 | * Returns a pointer to the new node object. |
| 2111 | */ |
| 2112 | xmlNodePtr |
| 2113 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 2114 | xmlNodePtr cur; |
| 2115 | |
| 2116 | if (name == NULL) { |
| 2117 | #ifdef DEBUG_TREE |
| 2118 | xmlGenericError(xmlGenericErrorContext, |
| 2119 | "xmlNewNode : name == NULL\n"); |
| 2120 | #endif |
| 2121 | return(NULL); |
| 2122 | } |
| 2123 | |
| 2124 | /* |
| 2125 | * Allocate a new node and fill the fields. |
| 2126 | */ |
| 2127 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2128 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2129 | xmlTreeErrMemory("building node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2130 | return(NULL); |
| 2131 | } |
| 2132 | memset(cur, 0, sizeof(xmlNode)); |
| 2133 | cur->type = XML_ELEMENT_NODE; |
| 2134 | |
| 2135 | cur->name = xmlStrdup(name); |
| 2136 | cur->ns = ns; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2137 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2138 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2139 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2140 | return(cur); |
| 2141 | } |
| 2142 | |
| 2143 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2144 | * xmlNewNodeEatName: |
| 2145 | * @ns: namespace if any |
| 2146 | * @name: the node name |
| 2147 | * |
| 2148 | * Creation of a new node element. @ns is optional (NULL). |
| 2149 | * |
| 2150 | * Returns a pointer to the new node object. |
| 2151 | */ |
| 2152 | xmlNodePtr |
| 2153 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
| 2154 | xmlNodePtr cur; |
| 2155 | |
| 2156 | if (name == NULL) { |
| 2157 | #ifdef DEBUG_TREE |
| 2158 | xmlGenericError(xmlGenericErrorContext, |
| 2159 | "xmlNewNode : name == NULL\n"); |
| 2160 | #endif |
| 2161 | return(NULL); |
| 2162 | } |
| 2163 | |
| 2164 | /* |
| 2165 | * Allocate a new node and fill the fields. |
| 2166 | */ |
| 2167 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2168 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2169 | xmlTreeErrMemory("building node"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2170 | xmlFree(name); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2171 | return(NULL); |
| 2172 | } |
| 2173 | memset(cur, 0, sizeof(xmlNode)); |
| 2174 | cur->type = XML_ELEMENT_NODE; |
| 2175 | |
| 2176 | cur->name = name; |
| 2177 | cur->ns = ns; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2178 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2179 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2180 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2181 | return(cur); |
| 2182 | } |
| 2183 | |
| 2184 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2185 | * xmlNewDocNode: |
| 2186 | * @doc: the document |
| 2187 | * @ns: namespace if any |
| 2188 | * @name: the node name |
| 2189 | * @content: the XML text content if any |
| 2190 | * |
| 2191 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2192 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2193 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2194 | * references, but XML special chars need to be escaped first by using |
| 2195 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2196 | * need entities support. |
| 2197 | * |
| 2198 | * Returns a pointer to the new node object. |
| 2199 | */ |
| 2200 | xmlNodePtr |
| 2201 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2202 | const xmlChar *name, const xmlChar *content) { |
| 2203 | xmlNodePtr cur; |
| 2204 | |
| 2205 | cur = xmlNewNode(ns, name); |
| 2206 | if (cur != NULL) { |
| 2207 | cur->doc = doc; |
| 2208 | if (content != NULL) { |
| 2209 | cur->children = xmlStringGetNodeList(doc, content); |
| 2210 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2211 | } |
| 2212 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2213 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2214 | return(cur); |
| 2215 | } |
| 2216 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2217 | /** |
| 2218 | * xmlNewDocNodeEatName: |
| 2219 | * @doc: the document |
| 2220 | * @ns: namespace if any |
| 2221 | * @name: the node name |
| 2222 | * @content: the XML text content if any |
| 2223 | * |
| 2224 | * Creation of a new node element within a document. @ns and @content |
| 2225 | * are optional (NULL). |
| 2226 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2227 | * references, but XML special chars need to be escaped first by using |
| 2228 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2229 | * need entities support. |
| 2230 | * |
| 2231 | * Returns a pointer to the new node object. |
| 2232 | */ |
| 2233 | xmlNodePtr |
| 2234 | xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns, |
| 2235 | xmlChar *name, const xmlChar *content) { |
| 2236 | xmlNodePtr cur; |
| 2237 | |
| 2238 | cur = xmlNewNodeEatName(ns, name); |
| 2239 | if (cur != NULL) { |
| 2240 | cur->doc = doc; |
| 2241 | if (content != NULL) { |
| 2242 | cur->children = xmlStringGetNodeList(doc, content); |
| 2243 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2244 | } |
| 2245 | } |
| 2246 | return(cur); |
| 2247 | } |
| 2248 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2249 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2250 | /** |
| 2251 | * xmlNewDocRawNode: |
| 2252 | * @doc: the document |
| 2253 | * @ns: namespace if any |
| 2254 | * @name: the node name |
| 2255 | * @content: the text content if any |
| 2256 | * |
| 2257 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2258 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2259 | * |
| 2260 | * Returns a pointer to the new node object. |
| 2261 | */ |
| 2262 | xmlNodePtr |
| 2263 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2264 | const xmlChar *name, const xmlChar *content) { |
| 2265 | xmlNodePtr cur; |
| 2266 | |
| 2267 | cur = xmlNewNode(ns, name); |
| 2268 | if (cur != NULL) { |
| 2269 | cur->doc = doc; |
| 2270 | if (content != NULL) { |
| 2271 | cur->children = xmlNewDocText(doc, content); |
| 2272 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2273 | } |
| 2274 | } |
| 2275 | return(cur); |
| 2276 | } |
| 2277 | |
| 2278 | /** |
| 2279 | * xmlNewDocFragment: |
| 2280 | * @doc: the document owning the fragment |
| 2281 | * |
| 2282 | * Creation of a new Fragment node. |
| 2283 | * Returns a pointer to the new node object. |
| 2284 | */ |
| 2285 | xmlNodePtr |
| 2286 | xmlNewDocFragment(xmlDocPtr doc) { |
| 2287 | xmlNodePtr cur; |
| 2288 | |
| 2289 | /* |
| 2290 | * Allocate a new DocumentFragment node and fill the fields. |
| 2291 | */ |
| 2292 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2293 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2294 | xmlTreeErrMemory("building fragment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2295 | return(NULL); |
| 2296 | } |
| 2297 | memset(cur, 0, sizeof(xmlNode)); |
| 2298 | cur->type = XML_DOCUMENT_FRAG_NODE; |
| 2299 | |
| 2300 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2301 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2302 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2303 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2304 | return(cur); |
| 2305 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2306 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2307 | |
| 2308 | /** |
| 2309 | * xmlNewText: |
| 2310 | * @content: the text content |
| 2311 | * |
| 2312 | * Creation of a new text node. |
| 2313 | * Returns a pointer to the new node object. |
| 2314 | */ |
| 2315 | xmlNodePtr |
| 2316 | xmlNewText(const xmlChar *content) { |
| 2317 | xmlNodePtr cur; |
| 2318 | |
| 2319 | /* |
| 2320 | * Allocate a new node and fill the fields. |
| 2321 | */ |
| 2322 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2323 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2324 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2325 | return(NULL); |
| 2326 | } |
| 2327 | memset(cur, 0, sizeof(xmlNode)); |
| 2328 | cur->type = XML_TEXT_NODE; |
| 2329 | |
| 2330 | cur->name = xmlStringText; |
| 2331 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2332 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2333 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2334 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2335 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2336 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2337 | return(cur); |
| 2338 | } |
| 2339 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2340 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2341 | /** |
| 2342 | * xmlNewTextChild: |
| 2343 | * @parent: the parent node |
| 2344 | * @ns: a namespace if any |
| 2345 | * @name: the name of the child |
| 2346 | * @content: the text content of the child if any. |
| 2347 | * |
| 2348 | * 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] | 2349 | * @ns and @content parameters are optional (NULL). If content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2350 | * a child TEXT node will be created containing the string content. |
| 2351 | * |
| 2352 | * Returns a pointer to the new node object. |
| 2353 | */ |
| 2354 | xmlNodePtr |
| 2355 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2356 | const xmlChar *name, const xmlChar *content) { |
| 2357 | xmlNodePtr cur, prev; |
| 2358 | |
| 2359 | if (parent == NULL) { |
| 2360 | #ifdef DEBUG_TREE |
| 2361 | xmlGenericError(xmlGenericErrorContext, |
| 2362 | "xmlNewTextChild : parent == NULL\n"); |
| 2363 | #endif |
| 2364 | return(NULL); |
| 2365 | } |
| 2366 | |
| 2367 | if (name == NULL) { |
| 2368 | #ifdef DEBUG_TREE |
| 2369 | xmlGenericError(xmlGenericErrorContext, |
| 2370 | "xmlNewTextChild : name == NULL\n"); |
| 2371 | #endif |
| 2372 | return(NULL); |
| 2373 | } |
| 2374 | |
| 2375 | /* |
| 2376 | * Allocate a new node |
| 2377 | */ |
| 2378 | if (ns == NULL) |
| 2379 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 2380 | else |
| 2381 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 2382 | if (cur == NULL) return(NULL); |
| 2383 | |
| 2384 | /* |
| 2385 | * add the new element at the end of the children list. |
| 2386 | */ |
| 2387 | cur->type = XML_ELEMENT_NODE; |
| 2388 | cur->parent = parent; |
| 2389 | cur->doc = parent->doc; |
| 2390 | if (parent->children == NULL) { |
| 2391 | parent->children = cur; |
| 2392 | parent->last = cur; |
| 2393 | } else { |
| 2394 | prev = parent->last; |
| 2395 | prev->next = cur; |
| 2396 | cur->prev = prev; |
| 2397 | parent->last = cur; |
| 2398 | } |
| 2399 | |
| 2400 | return(cur); |
| 2401 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2402 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2403 | |
| 2404 | /** |
| 2405 | * xmlNewCharRef: |
| 2406 | * @doc: the document |
| 2407 | * @name: the char ref string, starting with # or "&# ... ;" |
| 2408 | * |
| 2409 | * Creation of a new character reference node. |
| 2410 | * Returns a pointer to the new node object. |
| 2411 | */ |
| 2412 | xmlNodePtr |
| 2413 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 2414 | xmlNodePtr cur; |
| 2415 | |
| 2416 | /* |
| 2417 | * Allocate a new node and fill the fields. |
| 2418 | */ |
| 2419 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2420 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2421 | xmlTreeErrMemory("building character reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2422 | return(NULL); |
| 2423 | } |
| 2424 | memset(cur, 0, sizeof(xmlNode)); |
| 2425 | cur->type = XML_ENTITY_REF_NODE; |
| 2426 | |
| 2427 | cur->doc = doc; |
| 2428 | if (name[0] == '&') { |
| 2429 | int len; |
| 2430 | name++; |
| 2431 | len = xmlStrlen(name); |
| 2432 | if (name[len - 1] == ';') |
| 2433 | cur->name = xmlStrndup(name, len - 1); |
| 2434 | else |
| 2435 | cur->name = xmlStrndup(name, len); |
| 2436 | } else |
| 2437 | cur->name = xmlStrdup(name); |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2438 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2439 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2440 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2441 | return(cur); |
| 2442 | } |
| 2443 | |
| 2444 | /** |
| 2445 | * xmlNewReference: |
| 2446 | * @doc: the document |
| 2447 | * @name: the reference name, or the reference string with & and ; |
| 2448 | * |
| 2449 | * Creation of a new reference node. |
| 2450 | * Returns a pointer to the new node object. |
| 2451 | */ |
| 2452 | xmlNodePtr |
| 2453 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 2454 | xmlNodePtr cur; |
| 2455 | xmlEntityPtr ent; |
| 2456 | |
| 2457 | /* |
| 2458 | * Allocate a new node and fill the fields. |
| 2459 | */ |
| 2460 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2461 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2462 | xmlTreeErrMemory("building reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2463 | return(NULL); |
| 2464 | } |
| 2465 | memset(cur, 0, sizeof(xmlNode)); |
| 2466 | cur->type = XML_ENTITY_REF_NODE; |
| 2467 | |
| 2468 | cur->doc = doc; |
| 2469 | if (name[0] == '&') { |
| 2470 | int len; |
| 2471 | name++; |
| 2472 | len = xmlStrlen(name); |
| 2473 | if (name[len - 1] == ';') |
| 2474 | cur->name = xmlStrndup(name, len - 1); |
| 2475 | else |
| 2476 | cur->name = xmlStrndup(name, len); |
| 2477 | } else |
| 2478 | cur->name = xmlStrdup(name); |
| 2479 | |
| 2480 | ent = xmlGetDocEntity(doc, cur->name); |
| 2481 | if (ent != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2482 | cur->content = ent->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2483 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2484 | * 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] | 2485 | * updated. Not sure if this is 100% correct. |
| 2486 | * -George |
| 2487 | */ |
| 2488 | cur->children = (xmlNodePtr) ent; |
| 2489 | cur->last = (xmlNodePtr) ent; |
| 2490 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2491 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2492 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2493 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2494 | return(cur); |
| 2495 | } |
| 2496 | |
| 2497 | /** |
| 2498 | * xmlNewDocText: |
| 2499 | * @doc: the document |
| 2500 | * @content: the text content |
| 2501 | * |
| 2502 | * Creation of a new text node within a document. |
| 2503 | * Returns a pointer to the new node object. |
| 2504 | */ |
| 2505 | xmlNodePtr |
| 2506 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 2507 | xmlNodePtr cur; |
| 2508 | |
| 2509 | cur = xmlNewText(content); |
| 2510 | if (cur != NULL) cur->doc = doc; |
| 2511 | return(cur); |
| 2512 | } |
| 2513 | |
| 2514 | /** |
| 2515 | * xmlNewTextLen: |
| 2516 | * @content: the text content |
| 2517 | * @len: the text len. |
| 2518 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2519 | * 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] | 2520 | * Returns a pointer to the new node object. |
| 2521 | */ |
| 2522 | xmlNodePtr |
| 2523 | xmlNewTextLen(const xmlChar *content, int len) { |
| 2524 | xmlNodePtr cur; |
| 2525 | |
| 2526 | /* |
| 2527 | * Allocate a new node and fill the fields. |
| 2528 | */ |
| 2529 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2530 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2531 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2532 | return(NULL); |
| 2533 | } |
| 2534 | memset(cur, 0, sizeof(xmlNode)); |
| 2535 | cur->type = XML_TEXT_NODE; |
| 2536 | |
| 2537 | cur->name = xmlStringText; |
| 2538 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2539 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2540 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2541 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2542 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2543 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2544 | return(cur); |
| 2545 | } |
| 2546 | |
| 2547 | /** |
| 2548 | * xmlNewDocTextLen: |
| 2549 | * @doc: the document |
| 2550 | * @content: the text content |
| 2551 | * @len: the text len. |
| 2552 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2553 | * 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] | 2554 | * text node pertain to a given document. |
| 2555 | * Returns a pointer to the new node object. |
| 2556 | */ |
| 2557 | xmlNodePtr |
| 2558 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2559 | xmlNodePtr cur; |
| 2560 | |
| 2561 | cur = xmlNewTextLen(content, len); |
| 2562 | if (cur != NULL) cur->doc = doc; |
| 2563 | return(cur); |
| 2564 | } |
| 2565 | |
| 2566 | /** |
| 2567 | * xmlNewComment: |
| 2568 | * @content: the comment content |
| 2569 | * |
| 2570 | * Creation of a new node containing a comment. |
| 2571 | * Returns a pointer to the new node object. |
| 2572 | */ |
| 2573 | xmlNodePtr |
| 2574 | xmlNewComment(const xmlChar *content) { |
| 2575 | xmlNodePtr cur; |
| 2576 | |
| 2577 | /* |
| 2578 | * Allocate a new node and fill the fields. |
| 2579 | */ |
| 2580 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2581 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2582 | xmlTreeErrMemory("building comment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2583 | return(NULL); |
| 2584 | } |
| 2585 | memset(cur, 0, sizeof(xmlNode)); |
| 2586 | cur->type = XML_COMMENT_NODE; |
| 2587 | |
| 2588 | cur->name = xmlStringComment; |
| 2589 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2590 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2591 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2592 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2593 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2594 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2595 | return(cur); |
| 2596 | } |
| 2597 | |
| 2598 | /** |
| 2599 | * xmlNewCDataBlock: |
| 2600 | * @doc: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2601 | * @content: the CDATA block content content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2602 | * @len: the length of the block |
| 2603 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2604 | * Creation of a new node containing a CDATA block. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2605 | * Returns a pointer to the new node object. |
| 2606 | */ |
| 2607 | xmlNodePtr |
| 2608 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2609 | xmlNodePtr cur; |
| 2610 | |
| 2611 | /* |
| 2612 | * Allocate a new node and fill the fields. |
| 2613 | */ |
| 2614 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2615 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2616 | xmlTreeErrMemory("building CDATA"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2617 | return(NULL); |
| 2618 | } |
| 2619 | memset(cur, 0, sizeof(xmlNode)); |
| 2620 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2621 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2622 | |
| 2623 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2624 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2625 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2626 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2627 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2628 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2629 | return(cur); |
| 2630 | } |
| 2631 | |
| 2632 | /** |
| 2633 | * xmlNewDocComment: |
| 2634 | * @doc: the document |
| 2635 | * @content: the comment content |
| 2636 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2637 | * Creation of a new node containing a comment within a document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2638 | * Returns a pointer to the new node object. |
| 2639 | */ |
| 2640 | xmlNodePtr |
| 2641 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 2642 | xmlNodePtr cur; |
| 2643 | |
| 2644 | cur = xmlNewComment(content); |
| 2645 | if (cur != NULL) cur->doc = doc; |
| 2646 | return(cur); |
| 2647 | } |
| 2648 | |
| 2649 | /** |
| 2650 | * xmlSetTreeDoc: |
| 2651 | * @tree: the top element |
| 2652 | * @doc: the document |
| 2653 | * |
| 2654 | * update all nodes under the tree to point to the right document |
| 2655 | */ |
| 2656 | void |
| 2657 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2658 | xmlAttrPtr prop; |
| 2659 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2660 | if (tree == NULL) |
| 2661 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2662 | if (tree->doc != doc) { |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2663 | if(tree->type == XML_ELEMENT_NODE) { |
| 2664 | prop = tree->properties; |
| 2665 | while (prop != NULL) { |
| 2666 | prop->doc = doc; |
| 2667 | xmlSetListDoc(prop->children, doc); |
| 2668 | prop = prop->next; |
| 2669 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2670 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2671 | if (tree->children != NULL) |
| 2672 | xmlSetListDoc(tree->children, doc); |
| 2673 | tree->doc = doc; |
| 2674 | } |
| 2675 | } |
| 2676 | |
| 2677 | /** |
| 2678 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2679 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2680 | * @doc: the document |
| 2681 | * |
| 2682 | * update all nodes in the list to point to the right document |
| 2683 | */ |
| 2684 | void |
| 2685 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2686 | xmlNodePtr cur; |
| 2687 | |
| 2688 | if (list == NULL) |
| 2689 | return; |
| 2690 | cur = list; |
| 2691 | while (cur != NULL) { |
| 2692 | if (cur->doc != doc) |
| 2693 | xmlSetTreeDoc(cur, doc); |
| 2694 | cur = cur->next; |
| 2695 | } |
| 2696 | } |
| 2697 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2698 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2699 | /** |
| 2700 | * xmlNewChild: |
| 2701 | * @parent: the parent node |
| 2702 | * @ns: a namespace if any |
| 2703 | * @name: the name of the child |
| 2704 | * @content: the XML content of the child if any. |
| 2705 | * |
| 2706 | * 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] | 2707 | * @ns and @content parameters are optional (NULL). If content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2708 | * a child list containing the TEXTs and ENTITY_REFs node will be created. |
| 2709 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2710 | * references, but XML special chars need to be escaped first by using |
| 2711 | * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities |
| 2712 | * support is not needed. |
| 2713 | * |
| 2714 | * Returns a pointer to the new node object. |
| 2715 | */ |
| 2716 | xmlNodePtr |
| 2717 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2718 | const xmlChar *name, const xmlChar *content) { |
| 2719 | xmlNodePtr cur, prev; |
| 2720 | |
| 2721 | if (parent == NULL) { |
| 2722 | #ifdef DEBUG_TREE |
| 2723 | xmlGenericError(xmlGenericErrorContext, |
| 2724 | "xmlNewChild : parent == NULL\n"); |
| 2725 | #endif |
| 2726 | return(NULL); |
| 2727 | } |
| 2728 | |
| 2729 | if (name == NULL) { |
| 2730 | #ifdef DEBUG_TREE |
| 2731 | xmlGenericError(xmlGenericErrorContext, |
| 2732 | "xmlNewChild : name == NULL\n"); |
| 2733 | #endif |
| 2734 | return(NULL); |
| 2735 | } |
| 2736 | |
| 2737 | /* |
| 2738 | * Allocate a new node |
| 2739 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2740 | if (parent->type == XML_ELEMENT_NODE) { |
| 2741 | if (ns == NULL) |
| 2742 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2743 | else |
| 2744 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2745 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2746 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2747 | if (ns == NULL) |
| 2748 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2749 | else |
| 2750 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2751 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2752 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2753 | } else { |
| 2754 | return(NULL); |
| 2755 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2756 | if (cur == NULL) return(NULL); |
| 2757 | |
| 2758 | /* |
| 2759 | * add the new element at the end of the children list. |
| 2760 | */ |
| 2761 | cur->type = XML_ELEMENT_NODE; |
| 2762 | cur->parent = parent; |
| 2763 | cur->doc = parent->doc; |
| 2764 | if (parent->children == NULL) { |
| 2765 | parent->children = cur; |
| 2766 | parent->last = cur; |
| 2767 | } else { |
| 2768 | prev = parent->last; |
| 2769 | prev->next = cur; |
| 2770 | cur->prev = prev; |
| 2771 | parent->last = cur; |
| 2772 | } |
| 2773 | |
| 2774 | return(cur); |
| 2775 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2776 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2777 | |
| 2778 | /** |
| 2779 | * xmlAddNextSibling: |
| 2780 | * @cur: the child node |
| 2781 | * @elem: the new node |
| 2782 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2783 | * Add a new node @elem as the next sibling of @cur |
| 2784 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2785 | * first unlinked from its existing context. |
| 2786 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2787 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2788 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2789 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2790 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2791 | */ |
| 2792 | xmlNodePtr |
| 2793 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2794 | if (cur == NULL) { |
| 2795 | #ifdef DEBUG_TREE |
| 2796 | xmlGenericError(xmlGenericErrorContext, |
| 2797 | "xmlAddNextSibling : cur == NULL\n"); |
| 2798 | #endif |
| 2799 | return(NULL); |
| 2800 | } |
| 2801 | if (elem == NULL) { |
| 2802 | #ifdef DEBUG_TREE |
| 2803 | xmlGenericError(xmlGenericErrorContext, |
| 2804 | "xmlAddNextSibling : elem == NULL\n"); |
| 2805 | #endif |
| 2806 | return(NULL); |
| 2807 | } |
| 2808 | |
| 2809 | xmlUnlinkNode(elem); |
| 2810 | |
| 2811 | if (elem->type == XML_TEXT_NODE) { |
| 2812 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2813 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2814 | xmlFreeNode(elem); |
| 2815 | return(cur); |
| 2816 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2817 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 2818 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2819 | xmlChar *tmp; |
| 2820 | |
| 2821 | tmp = xmlStrdup(elem->content); |
| 2822 | tmp = xmlStrcat(tmp, cur->next->content); |
| 2823 | xmlNodeSetContent(cur->next, tmp); |
| 2824 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2825 | xmlFreeNode(elem); |
| 2826 | return(cur->next); |
| 2827 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2828 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2829 | /* check if an attribute with the same name exists */ |
| 2830 | xmlAttrPtr attr; |
| 2831 | |
| 2832 | if (elem->ns == NULL) |
| 2833 | attr = xmlHasProp(cur->parent, elem->name); |
| 2834 | else |
| 2835 | attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href); |
| 2836 | if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) { |
| 2837 | /* different instance, destroy it (attributes must be unique) */ |
| 2838 | xmlFreeProp(attr); |
| 2839 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | if (elem->doc != cur->doc) { |
| 2843 | xmlSetTreeDoc(elem, cur->doc); |
| 2844 | } |
| 2845 | elem->parent = cur->parent; |
| 2846 | elem->prev = cur; |
| 2847 | elem->next = cur->next; |
| 2848 | cur->next = elem; |
| 2849 | if (elem->next != NULL) |
| 2850 | elem->next->prev = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2851 | 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] | 2852 | elem->parent->last = elem; |
| 2853 | return(elem); |
| 2854 | } |
| 2855 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2856 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2857 | /** |
| 2858 | * xmlAddPrevSibling: |
| 2859 | * @cur: the child node |
| 2860 | * @elem: the new node |
| 2861 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2862 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2863 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2864 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2865 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2866 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2867 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2868 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2869 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2870 | */ |
| 2871 | xmlNodePtr |
| 2872 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2873 | if (cur == NULL) { |
| 2874 | #ifdef DEBUG_TREE |
| 2875 | xmlGenericError(xmlGenericErrorContext, |
| 2876 | "xmlAddPrevSibling : cur == NULL\n"); |
| 2877 | #endif |
| 2878 | return(NULL); |
| 2879 | } |
| 2880 | if (elem == NULL) { |
| 2881 | #ifdef DEBUG_TREE |
| 2882 | xmlGenericError(xmlGenericErrorContext, |
| 2883 | "xmlAddPrevSibling : elem == NULL\n"); |
| 2884 | #endif |
| 2885 | return(NULL); |
| 2886 | } |
| 2887 | |
| 2888 | xmlUnlinkNode(elem); |
| 2889 | |
| 2890 | if (elem->type == XML_TEXT_NODE) { |
| 2891 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2892 | xmlChar *tmp; |
| 2893 | |
| 2894 | tmp = xmlStrdup(elem->content); |
| 2895 | tmp = xmlStrcat(tmp, cur->content); |
| 2896 | xmlNodeSetContent(cur, tmp); |
| 2897 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2898 | xmlFreeNode(elem); |
| 2899 | return(cur); |
| 2900 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2901 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 2902 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2903 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2904 | xmlFreeNode(elem); |
| 2905 | return(cur->prev); |
| 2906 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2907 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2908 | /* check if an attribute with the same name exists */ |
| 2909 | xmlAttrPtr attr; |
| 2910 | |
| 2911 | if (elem->ns == NULL) |
| 2912 | attr = xmlHasProp(cur->parent, elem->name); |
| 2913 | else |
| 2914 | attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href); |
| 2915 | if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) { |
| 2916 | /* different instance, destroy it (attributes must be unique) */ |
| 2917 | xmlFreeProp(attr); |
| 2918 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | if (elem->doc != cur->doc) { |
| 2922 | xmlSetTreeDoc(elem, cur->doc); |
| 2923 | } |
| 2924 | elem->parent = cur->parent; |
| 2925 | elem->next = cur; |
| 2926 | elem->prev = cur->prev; |
| 2927 | cur->prev = elem; |
| 2928 | if (elem->prev != NULL) |
| 2929 | elem->prev->next = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2930 | if (elem->parent != NULL) { |
| 2931 | if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2932 | if (elem->parent->properties == (xmlAttrPtr) cur) { |
| 2933 | elem->parent->properties = (xmlAttrPtr) elem; |
| 2934 | } |
| 2935 | } else { |
| 2936 | if (elem->parent->children == cur) { |
| 2937 | elem->parent->children = elem; |
| 2938 | } |
| 2939 | } |
| 2940 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2941 | return(elem); |
| 2942 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2943 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2944 | |
| 2945 | /** |
| 2946 | * xmlAddSibling: |
| 2947 | * @cur: the child node |
| 2948 | * @elem: the new node |
| 2949 | * |
| 2950 | * Add a new element @elem to the list of siblings of @cur |
| 2951 | * merging adjacent TEXT nodes (@elem may be freed) |
| 2952 | * If the new element was already inserted in a document it is |
| 2953 | * first unlinked from its existing context. |
| 2954 | * |
| 2955 | * Returns the new element or NULL in case of error. |
| 2956 | */ |
| 2957 | xmlNodePtr |
| 2958 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2959 | xmlNodePtr parent; |
| 2960 | |
| 2961 | if (cur == NULL) { |
| 2962 | #ifdef DEBUG_TREE |
| 2963 | xmlGenericError(xmlGenericErrorContext, |
| 2964 | "xmlAddSibling : cur == NULL\n"); |
| 2965 | #endif |
| 2966 | return(NULL); |
| 2967 | } |
| 2968 | |
| 2969 | if (elem == NULL) { |
| 2970 | #ifdef DEBUG_TREE |
| 2971 | xmlGenericError(xmlGenericErrorContext, |
| 2972 | "xmlAddSibling : elem == NULL\n"); |
| 2973 | #endif |
| 2974 | return(NULL); |
| 2975 | } |
| 2976 | |
| 2977 | /* |
| 2978 | * Constant time is we can rely on the ->parent->last to find |
| 2979 | * the last sibling. |
| 2980 | */ |
| 2981 | if ((cur->parent != NULL) && |
| 2982 | (cur->parent->children != NULL) && |
| 2983 | (cur->parent->last != NULL) && |
| 2984 | (cur->parent->last->next == NULL)) { |
| 2985 | cur = cur->parent->last; |
| 2986 | } else { |
| 2987 | while (cur->next != NULL) cur = cur->next; |
| 2988 | } |
| 2989 | |
| 2990 | xmlUnlinkNode(elem); |
| 2991 | |
| 2992 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2993 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2994 | xmlFreeNode(elem); |
| 2995 | return(cur); |
| 2996 | } |
| 2997 | |
| 2998 | if (elem->doc != cur->doc) { |
| 2999 | xmlSetTreeDoc(elem, cur->doc); |
| 3000 | } |
| 3001 | parent = cur->parent; |
| 3002 | elem->prev = cur; |
| 3003 | elem->next = NULL; |
| 3004 | elem->parent = parent; |
| 3005 | cur->next = elem; |
| 3006 | if (parent != NULL) |
| 3007 | parent->last = elem; |
| 3008 | |
| 3009 | return(elem); |
| 3010 | } |
| 3011 | |
| 3012 | /** |
| 3013 | * xmlAddChildList: |
| 3014 | * @parent: the parent node |
| 3015 | * @cur: the first node in the list |
| 3016 | * |
| 3017 | * Add a list of node at the end of the child list of the parent |
| 3018 | * merging adjacent TEXT nodes (@cur may be freed) |
| 3019 | * |
| 3020 | * Returns the last child or NULL in case of error. |
| 3021 | */ |
| 3022 | xmlNodePtr |
| 3023 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 3024 | xmlNodePtr prev; |
| 3025 | |
| 3026 | if (parent == NULL) { |
| 3027 | #ifdef DEBUG_TREE |
| 3028 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3029 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3030 | #endif |
| 3031 | return(NULL); |
| 3032 | } |
| 3033 | |
| 3034 | if (cur == NULL) { |
| 3035 | #ifdef DEBUG_TREE |
| 3036 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3037 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3038 | #endif |
| 3039 | return(NULL); |
| 3040 | } |
| 3041 | |
| 3042 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 3043 | (cur->doc != parent->doc)) { |
| 3044 | #ifdef DEBUG_TREE |
| 3045 | xmlGenericError(xmlGenericErrorContext, |
| 3046 | "Elements moved to a different document\n"); |
| 3047 | #endif |
| 3048 | } |
| 3049 | |
| 3050 | /* |
| 3051 | * add the first element at the end of the children list. |
| 3052 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3053 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3054 | if (parent->children == NULL) { |
| 3055 | parent->children = cur; |
| 3056 | } else { |
| 3057 | /* |
| 3058 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 3059 | */ |
| 3060 | if ((cur->type == XML_TEXT_NODE) && |
| 3061 | (parent->last->type == XML_TEXT_NODE) && |
| 3062 | (cur->name == parent->last->name)) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3063 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3064 | /* |
| 3065 | * if it's the only child, nothing more to be done. |
| 3066 | */ |
| 3067 | if (cur->next == NULL) { |
| 3068 | xmlFreeNode(cur); |
| 3069 | return(parent->last); |
| 3070 | } |
| 3071 | prev = cur; |
| 3072 | cur = cur->next; |
| 3073 | xmlFreeNode(prev); |
| 3074 | } |
| 3075 | prev = parent->last; |
| 3076 | prev->next = cur; |
| 3077 | cur->prev = prev; |
| 3078 | } |
| 3079 | while (cur->next != NULL) { |
| 3080 | cur->parent = parent; |
| 3081 | if (cur->doc != parent->doc) { |
| 3082 | xmlSetTreeDoc(cur, parent->doc); |
| 3083 | } |
| 3084 | cur = cur->next; |
| 3085 | } |
| 3086 | cur->parent = parent; |
| 3087 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 3088 | parent->last = cur; |
| 3089 | |
| 3090 | return(cur); |
| 3091 | } |
| 3092 | |
| 3093 | /** |
| 3094 | * xmlAddChild: |
| 3095 | * @parent: the parent node |
| 3096 | * @cur: the child node |
| 3097 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3098 | * 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] | 3099 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3100 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 3101 | * If there is an attribute with equal name, it is first destroyed. |
| 3102 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3103 | * Returns the child or NULL in case of error. |
| 3104 | */ |
| 3105 | xmlNodePtr |
| 3106 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 3107 | xmlNodePtr prev; |
| 3108 | |
| 3109 | if (parent == NULL) { |
| 3110 | #ifdef DEBUG_TREE |
| 3111 | xmlGenericError(xmlGenericErrorContext, |
| 3112 | "xmlAddChild : parent == NULL\n"); |
| 3113 | #endif |
| 3114 | return(NULL); |
| 3115 | } |
| 3116 | |
| 3117 | if (cur == NULL) { |
| 3118 | #ifdef DEBUG_TREE |
| 3119 | xmlGenericError(xmlGenericErrorContext, |
| 3120 | "xmlAddChild : child == NULL\n"); |
| 3121 | #endif |
| 3122 | return(NULL); |
| 3123 | } |
| 3124 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3125 | /* |
| 3126 | * 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] | 3127 | * cur is then freed. |
| 3128 | */ |
| 3129 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3130 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3131 | (parent->content != NULL) && |
| 3132 | (parent != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3133 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3134 | xmlFreeNode(cur); |
| 3135 | return(parent); |
| 3136 | } |
| 3137 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3138 | (parent->last->name == cur->name) && |
| 3139 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3140 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3141 | xmlFreeNode(cur); |
| 3142 | return(parent->last); |
| 3143 | } |
| 3144 | } |
| 3145 | |
| 3146 | /* |
| 3147 | * add the new element at the end of the children list. |
| 3148 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3149 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3150 | cur->parent = parent; |
| 3151 | if (cur->doc != parent->doc) { |
| 3152 | xmlSetTreeDoc(cur, parent->doc); |
| 3153 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3154 | /* this check prevents a loop on tree-traversions if a developer |
| 3155 | * tries to add a node to its parent multiple times |
| 3156 | */ |
| 3157 | if (prev == parent) |
| 3158 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3159 | |
| 3160 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3161 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3162 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3163 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3164 | (parent->content != NULL) && |
| 3165 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3166 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3167 | xmlFreeNode(cur); |
| 3168 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3169 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3170 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3171 | if (parent->properties == NULL) { |
| 3172 | parent->properties = (xmlAttrPtr) cur; |
| 3173 | } else { |
| 3174 | /* check if an attribute with the same name exists */ |
| 3175 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3176 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3177 | if (cur->ns == NULL) |
| 3178 | lastattr = xmlHasProp(parent, cur->name); |
| 3179 | else |
| 3180 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
| 3181 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur)) { |
| 3182 | /* different instance, destroy it (attributes must be unique) */ |
| 3183 | xmlFreeProp(lastattr); |
| 3184 | } |
| 3185 | /* find the end */ |
| 3186 | lastattr = parent->properties; |
| 3187 | while (lastattr->next != NULL) { |
| 3188 | lastattr = lastattr->next; |
| 3189 | } |
| 3190 | lastattr->next = (xmlAttrPtr) cur; |
| 3191 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3192 | } |
| 3193 | } else { |
| 3194 | if (parent->children == NULL) { |
| 3195 | parent->children = cur; |
| 3196 | parent->last = cur; |
| 3197 | } else { |
| 3198 | prev = parent->last; |
| 3199 | prev->next = cur; |
| 3200 | cur->prev = prev; |
| 3201 | parent->last = cur; |
| 3202 | } |
| 3203 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3204 | return(cur); |
| 3205 | } |
| 3206 | |
| 3207 | /** |
| 3208 | * xmlGetLastChild: |
| 3209 | * @parent: the parent node |
| 3210 | * |
| 3211 | * Search the last child of a node. |
| 3212 | * Returns the last child or NULL if none. |
| 3213 | */ |
| 3214 | xmlNodePtr |
| 3215 | xmlGetLastChild(xmlNodePtr parent) { |
| 3216 | if (parent == NULL) { |
| 3217 | #ifdef DEBUG_TREE |
| 3218 | xmlGenericError(xmlGenericErrorContext, |
| 3219 | "xmlGetLastChild : parent == NULL\n"); |
| 3220 | #endif |
| 3221 | return(NULL); |
| 3222 | } |
| 3223 | return(parent->last); |
| 3224 | } |
| 3225 | |
| 3226 | /** |
| 3227 | * xmlFreeNodeList: |
| 3228 | * @cur: the first node in the list |
| 3229 | * |
| 3230 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3231 | * the children are freed too. |
| 3232 | */ |
| 3233 | void |
| 3234 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3235 | xmlNodePtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3236 | xmlDictPtr dict = NULL; |
| 3237 | |
| 3238 | if (cur == NULL) return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3239 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3240 | xmlFreeNsList((xmlNsPtr) cur); |
| 3241 | return; |
| 3242 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3243 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3244 | #ifdef LIBXML_DOCB_ENABLED |
| 3245 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3246 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3247 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3248 | xmlFreeDoc((xmlDocPtr) cur); |
| 3249 | return; |
| 3250 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3251 | if (cur->doc != NULL) dict = cur->doc->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3252 | while (cur != NULL) { |
| 3253 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3254 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3255 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3256 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3257 | xmlDeregisterNodeDefaultValue(cur); |
| 3258 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3259 | if ((cur->children != NULL) && |
| 3260 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3261 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3262 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3263 | (cur->type == XML_XINCLUDE_START) || |
| 3264 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3265 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3266 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3267 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3268 | (cur->type != XML_XINCLUDE_START) && |
| 3269 | (cur->type != XML_XINCLUDE_END) && |
| 3270 | (cur->type != XML_ENTITY_REF_NODE)) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3271 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3272 | } |
| 3273 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3274 | (cur->type == XML_XINCLUDE_START) || |
| 3275 | (cur->type == XML_XINCLUDE_END)) && |
| 3276 | (cur->nsDef != NULL)) |
| 3277 | xmlFreeNsList(cur->nsDef); |
| 3278 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3279 | /* |
| 3280 | * When a node is a text node or a comment, it uses a global static |
| 3281 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3282 | * Otherwise the node name might come from the document's |
| 3283 | * dictionnary |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3284 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3285 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3286 | (cur->type != XML_TEXT_NODE) && |
| 3287 | (cur->type != XML_COMMENT_NODE)) |
| 3288 | DICT_FREE(cur->name) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3289 | xmlFree(cur); |
| 3290 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3291 | cur = next; |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | /** |
| 3296 | * xmlFreeNode: |
| 3297 | * @cur: the node |
| 3298 | * |
| 3299 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3300 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3301 | */ |
| 3302 | void |
| 3303 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3304 | xmlDictPtr dict = NULL; |
| 3305 | |
| 3306 | if (cur == NULL) return; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3307 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3308 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3309 | if (cur->type == XML_DTD_NODE) { |
| 3310 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3311 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3312 | } |
| 3313 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3314 | xmlFreeNs((xmlNsPtr) cur); |
| 3315 | return; |
| 3316 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3317 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3318 | xmlFreeProp((xmlAttrPtr) cur); |
| 3319 | return; |
| 3320 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3321 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3322 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3323 | xmlDeregisterNodeDefaultValue(cur); |
| 3324 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3325 | if (cur->doc != NULL) dict = cur->doc->dict; |
| 3326 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3327 | if ((cur->children != NULL) && |
| 3328 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3329 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3330 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3331 | (cur->type == XML_XINCLUDE_START) || |
| 3332 | (cur->type == XML_XINCLUDE_END)) && |
| 3333 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3334 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3335 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3336 | (cur->content != NULL) && |
| 3337 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3338 | (cur->type != XML_XINCLUDE_END) && |
| 3339 | (cur->type != XML_XINCLUDE_START)) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3340 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3343 | /* |
| 3344 | * When a node is a text node or a comment, it uses a global static |
| 3345 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3346 | * Otherwise the node name might come from the document's dictionnary |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3347 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3348 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3349 | (cur->type != XML_TEXT_NODE) && |
| 3350 | (cur->type != XML_COMMENT_NODE)) |
| 3351 | DICT_FREE(cur->name) |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3352 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3353 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3354 | (cur->type == XML_XINCLUDE_START) || |
| 3355 | (cur->type == XML_XINCLUDE_END)) && |
| 3356 | (cur->nsDef != NULL)) |
| 3357 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3358 | xmlFree(cur); |
| 3359 | } |
| 3360 | |
| 3361 | /** |
| 3362 | * xmlUnlinkNode: |
| 3363 | * @cur: the node |
| 3364 | * |
| 3365 | * Unlink a node from it's current context, the node is not freed |
| 3366 | */ |
| 3367 | void |
| 3368 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3369 | if (cur == NULL) { |
| 3370 | #ifdef DEBUG_TREE |
| 3371 | xmlGenericError(xmlGenericErrorContext, |
| 3372 | "xmlUnlinkNode : node == NULL\n"); |
| 3373 | #endif |
| 3374 | return; |
| 3375 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3376 | if (cur->type == XML_DTD_NODE) { |
| 3377 | xmlDocPtr doc; |
| 3378 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3379 | if (doc != NULL) { |
| 3380 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3381 | doc->intSubset = NULL; |
| 3382 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3383 | doc->extSubset = NULL; |
| 3384 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3385 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3386 | if (cur->parent != NULL) { |
| 3387 | xmlNodePtr parent; |
| 3388 | parent = cur->parent; |
| 3389 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3390 | if (parent->properties == (xmlAttrPtr) cur) |
| 3391 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3392 | } else { |
| 3393 | if (parent->children == cur) |
| 3394 | parent->children = cur->next; |
| 3395 | if (parent->last == cur) |
| 3396 | parent->last = cur->prev; |
| 3397 | } |
| 3398 | cur->parent = NULL; |
| 3399 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3400 | if (cur->next != NULL) |
| 3401 | cur->next->prev = cur->prev; |
| 3402 | if (cur->prev != NULL) |
| 3403 | cur->prev->next = cur->next; |
| 3404 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3407 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3408 | /** |
| 3409 | * xmlReplaceNode: |
| 3410 | * @old: the old node |
| 3411 | * @cur: the node |
| 3412 | * |
| 3413 | * 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] | 3414 | * 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] | 3415 | * first unlinked from its existing context. |
| 3416 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3417 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3418 | */ |
| 3419 | xmlNodePtr |
| 3420 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
| 3421 | if (old == NULL) { |
| 3422 | #ifdef DEBUG_TREE |
| 3423 | xmlGenericError(xmlGenericErrorContext, |
| 3424 | "xmlReplaceNode : old == NULL\n"); |
| 3425 | #endif |
| 3426 | return(NULL); |
| 3427 | } |
| 3428 | if (cur == NULL) { |
| 3429 | xmlUnlinkNode(old); |
| 3430 | return(old); |
| 3431 | } |
| 3432 | if (cur == old) { |
| 3433 | return(old); |
| 3434 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3435 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3436 | #ifdef DEBUG_TREE |
| 3437 | xmlGenericError(xmlGenericErrorContext, |
| 3438 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3439 | #endif |
| 3440 | return(old); |
| 3441 | } |
| 3442 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3443 | #ifdef DEBUG_TREE |
| 3444 | xmlGenericError(xmlGenericErrorContext, |
| 3445 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3446 | #endif |
| 3447 | return(old); |
| 3448 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3449 | xmlUnlinkNode(cur); |
| 3450 | cur->doc = old->doc; |
| 3451 | cur->parent = old->parent; |
| 3452 | cur->next = old->next; |
| 3453 | if (cur->next != NULL) |
| 3454 | cur->next->prev = cur; |
| 3455 | cur->prev = old->prev; |
| 3456 | if (cur->prev != NULL) |
| 3457 | cur->prev->next = cur; |
| 3458 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3459 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3460 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3461 | cur->parent->properties = ((xmlAttrPtr) cur); |
| 3462 | } else { |
| 3463 | if (cur->parent->children == old) |
| 3464 | cur->parent->children = cur; |
| 3465 | if (cur->parent->last == old) |
| 3466 | cur->parent->last = cur; |
| 3467 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3468 | } |
| 3469 | old->next = old->prev = NULL; |
| 3470 | old->parent = NULL; |
| 3471 | return(old); |
| 3472 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3473 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3474 | |
| 3475 | /************************************************************************ |
| 3476 | * * |
| 3477 | * Copy operations * |
| 3478 | * * |
| 3479 | ************************************************************************/ |
| 3480 | |
| 3481 | /** |
| 3482 | * xmlCopyNamespace: |
| 3483 | * @cur: the namespace |
| 3484 | * |
| 3485 | * Do a copy of the namespace. |
| 3486 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3487 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3488 | */ |
| 3489 | xmlNsPtr |
| 3490 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3491 | xmlNsPtr ret; |
| 3492 | |
| 3493 | if (cur == NULL) return(NULL); |
| 3494 | switch (cur->type) { |
| 3495 | case XML_LOCAL_NAMESPACE: |
| 3496 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3497 | break; |
| 3498 | default: |
| 3499 | #ifdef DEBUG_TREE |
| 3500 | xmlGenericError(xmlGenericErrorContext, |
| 3501 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3502 | #endif |
| 3503 | return(NULL); |
| 3504 | } |
| 3505 | return(ret); |
| 3506 | } |
| 3507 | |
| 3508 | /** |
| 3509 | * xmlCopyNamespaceList: |
| 3510 | * @cur: the first namespace |
| 3511 | * |
| 3512 | * Do a copy of an namespace list. |
| 3513 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3514 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3515 | */ |
| 3516 | xmlNsPtr |
| 3517 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3518 | xmlNsPtr ret = NULL; |
| 3519 | xmlNsPtr p = NULL,q; |
| 3520 | |
| 3521 | while (cur != NULL) { |
| 3522 | q = xmlCopyNamespace(cur); |
| 3523 | if (p == NULL) { |
| 3524 | ret = p = q; |
| 3525 | } else { |
| 3526 | p->next = q; |
| 3527 | p = q; |
| 3528 | } |
| 3529 | cur = cur->next; |
| 3530 | } |
| 3531 | return(ret); |
| 3532 | } |
| 3533 | |
| 3534 | static xmlNodePtr |
| 3535 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 3536 | /** |
| 3537 | * xmlCopyProp: |
| 3538 | * @target: the element where the attribute will be grafted |
| 3539 | * @cur: the attribute |
| 3540 | * |
| 3541 | * Do a copy of the attribute. |
| 3542 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3543 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3544 | */ |
| 3545 | xmlAttrPtr |
| 3546 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 3547 | xmlAttrPtr ret; |
| 3548 | |
| 3549 | if (cur == NULL) return(NULL); |
| 3550 | if (target != NULL) |
| 3551 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
| 3552 | else if (cur->parent != NULL) |
| 3553 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 3554 | else if (cur->children != NULL) |
| 3555 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 3556 | else |
| 3557 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 3558 | if (ret == NULL) return(NULL); |
| 3559 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3560 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3561 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3562 | xmlNsPtr ns; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3563 | |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3564 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3565 | if (ns == NULL) { |
| 3566 | /* |
| 3567 | * Humm, we are copying an element whose namespace is defined |
| 3568 | * out of the new tree scope. Search it in the original tree |
| 3569 | * and add it at the top of the new tree |
| 3570 | */ |
| 3571 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 3572 | if (ns != NULL) { |
| 3573 | xmlNodePtr root = target; |
| 3574 | xmlNodePtr pred = NULL; |
| 3575 | |
| 3576 | while (root->parent != NULL) { |
| 3577 | pred = root; |
| 3578 | root = root->parent; |
| 3579 | } |
| 3580 | if (root == (xmlNodePtr) target->doc) { |
| 3581 | /* correct possibly cycling above the document elt */ |
| 3582 | root = pred; |
| 3583 | } |
| 3584 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 3585 | } |
| 3586 | } else { |
| 3587 | /* |
| 3588 | * we have to find something appropriate here since |
| 3589 | * we cant be sure, that the namespce we found is identified |
| 3590 | * by the prefix |
| 3591 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3592 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3593 | /* this is the nice case */ |
| 3594 | ret->ns = ns; |
| 3595 | } else { |
| 3596 | /* |
| 3597 | * we are in trouble: we need a new reconcilied namespace. |
| 3598 | * This is expensive |
| 3599 | */ |
| 3600 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 3601 | } |
| 3602 | } |
| 3603 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3604 | } else |
| 3605 | ret->ns = NULL; |
| 3606 | |
| 3607 | if (cur->children != NULL) { |
| 3608 | xmlNodePtr tmp; |
| 3609 | |
| 3610 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 3611 | ret->last = NULL; |
| 3612 | tmp = ret->children; |
| 3613 | while (tmp != NULL) { |
| 3614 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 3615 | if (tmp->next == NULL) |
| 3616 | ret->last = tmp; |
| 3617 | tmp = tmp->next; |
| 3618 | } |
| 3619 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3620 | /* |
| 3621 | * Try to handle IDs |
| 3622 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 3623 | if ((target!= NULL) && (cur!= NULL) && |
| 3624 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3625 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 3626 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 3627 | xmlChar *id; |
| 3628 | |
| 3629 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 3630 | if (id != NULL) { |
| 3631 | xmlAddID(NULL, target->doc, id, ret); |
| 3632 | xmlFree(id); |
| 3633 | } |
| 3634 | } |
| 3635 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3636 | return(ret); |
| 3637 | } |
| 3638 | |
| 3639 | /** |
| 3640 | * xmlCopyPropList: |
| 3641 | * @target: the element where the attributes will be grafted |
| 3642 | * @cur: the first attribute |
| 3643 | * |
| 3644 | * Do a copy of an attribute list. |
| 3645 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3646 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3647 | */ |
| 3648 | xmlAttrPtr |
| 3649 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 3650 | xmlAttrPtr ret = NULL; |
| 3651 | xmlAttrPtr p = NULL,q; |
| 3652 | |
| 3653 | while (cur != NULL) { |
| 3654 | q = xmlCopyProp(target, cur); |
| 3655 | if (p == NULL) { |
| 3656 | ret = p = q; |
| 3657 | } else { |
| 3658 | p->next = q; |
| 3659 | q->prev = p; |
| 3660 | p = q; |
| 3661 | } |
| 3662 | cur = cur->next; |
| 3663 | } |
| 3664 | return(ret); |
| 3665 | } |
| 3666 | |
| 3667 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3668 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3669 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3670 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3671 | * tricky reason: namespaces. Doing a direct copy of a node |
| 3672 | * say RPM:Copyright without changing the namespace pointer to |
| 3673 | * something else can produce stale links. One way to do it is |
| 3674 | * to keep a reference counter but this doesn't work as soon |
| 3675 | * as one move the element or the subtree out of the scope of |
| 3676 | * the existing namespace. The actual solution seems to add |
| 3677 | * a copy of the namespace at the top of the copied tree if |
| 3678 | * not available in the subtree. |
| 3679 | * Hence two functions, the public front-end call the inner ones |
| 3680 | */ |
| 3681 | |
| 3682 | static xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3683 | xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3684 | int recursive) { |
| 3685 | xmlNodePtr ret; |
| 3686 | |
| 3687 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3688 | switch (node->type) { |
| 3689 | case XML_TEXT_NODE: |
| 3690 | case XML_CDATA_SECTION_NODE: |
| 3691 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 3692 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3693 | case XML_ENTITY_REF_NODE: |
| 3694 | case XML_ENTITY_NODE: |
| 3695 | case XML_PI_NODE: |
| 3696 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3697 | case XML_XINCLUDE_START: |
| 3698 | case XML_XINCLUDE_END: |
| 3699 | break; |
| 3700 | case XML_ATTRIBUTE_NODE: |
| 3701 | return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node)); |
| 3702 | case XML_NAMESPACE_DECL: |
| 3703 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
| 3704 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3705 | case XML_DOCUMENT_NODE: |
| 3706 | case XML_HTML_DOCUMENT_NODE: |
| 3707 | #ifdef LIBXML_DOCB_ENABLED |
| 3708 | case XML_DOCB_DOCUMENT_NODE: |
| 3709 | #endif |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3710 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3711 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, recursive)); |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3712 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3713 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3714 | case XML_NOTATION_NODE: |
| 3715 | case XML_DTD_NODE: |
| 3716 | case XML_ELEMENT_DECL: |
| 3717 | case XML_ATTRIBUTE_DECL: |
| 3718 | case XML_ENTITY_DECL: |
| 3719 | return(NULL); |
| 3720 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3721 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3722 | /* |
| 3723 | * Allocate a new node and fill the fields. |
| 3724 | */ |
| 3725 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 3726 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 3727 | xmlTreeErrMemory("copying node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3728 | return(NULL); |
| 3729 | } |
| 3730 | memset(ret, 0, sizeof(xmlNode)); |
| 3731 | ret->type = node->type; |
| 3732 | |
| 3733 | ret->doc = doc; |
| 3734 | ret->parent = parent; |
| 3735 | if (node->name == xmlStringText) |
| 3736 | ret->name = xmlStringText; |
| 3737 | else if (node->name == xmlStringTextNoenc) |
| 3738 | ret->name = xmlStringTextNoenc; |
| 3739 | else if (node->name == xmlStringComment) |
| 3740 | ret->name = xmlStringComment; |
| 3741 | else if (node->name != NULL) |
| 3742 | ret->name = xmlStrdup(node->name); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3743 | if ((node->type != XML_ELEMENT_NODE) && |
| 3744 | (node->content != NULL) && |
| 3745 | (node->type != XML_ENTITY_REF_NODE) && |
| 3746 | (node->type != XML_XINCLUDE_END) && |
| 3747 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3748 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3749 | }else{ |
| 3750 | if (node->type == XML_ELEMENT_NODE) |
| 3751 | ret->content = (void*)(long) node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3752 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3753 | if (parent != NULL) { |
| 3754 | xmlNodePtr tmp; |
| 3755 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3756 | /* |
| 3757 | * this is a tricky part for the node register thing: |
| 3758 | * in case ret does get coalesced in xmlAddChild |
| 3759 | * the deregister-node callback is called; so we register ret now already |
| 3760 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3761 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3762 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 3763 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3764 | tmp = xmlAddChild(parent, ret); |
| 3765 | /* node could have coalesced */ |
| 3766 | if (tmp != ret) |
| 3767 | return(tmp); |
| 3768 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3769 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3770 | if (!recursive) |
| 3771 | goto out; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3772 | if (node->nsDef != NULL) |
| 3773 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 3774 | |
| 3775 | if (node->ns != NULL) { |
| 3776 | xmlNsPtr ns; |
| 3777 | |
| 3778 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 3779 | if (ns == NULL) { |
| 3780 | /* |
| 3781 | * Humm, we are copying an element whose namespace is defined |
| 3782 | * out of the new tree scope. Search it in the original tree |
| 3783 | * and add it at the top of the new tree |
| 3784 | */ |
| 3785 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 3786 | if (ns != NULL) { |
| 3787 | xmlNodePtr root = ret; |
| 3788 | |
| 3789 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 3790 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3791 | } |
| 3792 | } else { |
| 3793 | /* |
| 3794 | * reference the existing namespace definition in our own tree. |
| 3795 | */ |
| 3796 | ret->ns = ns; |
| 3797 | } |
| 3798 | } |
| 3799 | if (node->properties != NULL) |
| 3800 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3801 | if (node->type == XML_ENTITY_REF_NODE) { |
| 3802 | if ((doc == NULL) || (node->doc != doc)) { |
| 3803 | /* |
| 3804 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3805 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3806 | * we cannot keep the reference. Try to find it in the |
| 3807 | * target document. |
| 3808 | */ |
| 3809 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 3810 | } else { |
| 3811 | ret->children = node->children; |
| 3812 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3813 | ret->last = ret->children; |
| 3814 | } else if (node->children != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3815 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3816 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 3817 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3818 | |
| 3819 | out: |
| 3820 | /* if parent != NULL we already registered the node above */ |
| 3821 | if (parent == NULL && xmlRegisterNodeDefaultValue) |
| 3822 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3823 | return(ret); |
| 3824 | } |
| 3825 | |
| 3826 | static xmlNodePtr |
| 3827 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 3828 | xmlNodePtr ret = NULL; |
| 3829 | xmlNodePtr p = NULL,q; |
| 3830 | |
| 3831 | while (node != NULL) { |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3832 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3833 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 3834 | if (doc == NULL) { |
| 3835 | node = node->next; |
| 3836 | continue; |
| 3837 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3838 | if (doc->intSubset == NULL) { |
| 3839 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
| 3840 | q->doc = doc; |
| 3841 | q->parent = parent; |
| 3842 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3843 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3844 | } else { |
| 3845 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3846 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3847 | } |
| 3848 | } else |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3849 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3850 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3851 | if (ret == NULL) { |
| 3852 | q->prev = NULL; |
| 3853 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3854 | } else if (p != q) { |
| 3855 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3856 | p->next = q; |
| 3857 | q->prev = p; |
| 3858 | p = q; |
| 3859 | } |
| 3860 | node = node->next; |
| 3861 | } |
| 3862 | return(ret); |
| 3863 | } |
| 3864 | |
| 3865 | /** |
| 3866 | * xmlCopyNode: |
| 3867 | * @node: the node |
| 3868 | * @recursive: if 1 do a recursive copy. |
| 3869 | * |
| 3870 | * Do a copy of the node. |
| 3871 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3872 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3873 | */ |
| 3874 | xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3875 | xmlCopyNode(const xmlNodePtr node, int recursive) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3876 | xmlNodePtr ret; |
| 3877 | |
| 3878 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 3879 | return(ret); |
| 3880 | } |
| 3881 | |
| 3882 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3883 | * xmlDocCopyNode: |
| 3884 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3885 | * @doc: the document |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3886 | * @recursive: if 1 do a recursive copy. |
| 3887 | * |
| 3888 | * Do a copy of the node to a given document. |
| 3889 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3890 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3891 | */ |
| 3892 | xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3893 | xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int recursive) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3894 | xmlNodePtr ret; |
| 3895 | |
| 3896 | ret = xmlStaticCopyNode(node, doc, NULL, recursive); |
| 3897 | return(ret); |
| 3898 | } |
| 3899 | |
| 3900 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3901 | * xmlCopyNodeList: |
| 3902 | * @node: the first node in the list. |
| 3903 | * |
| 3904 | * Do a recursive copy of the node list. |
| 3905 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3906 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3907 | */ |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3908 | xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3909 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 3910 | return(ret); |
| 3911 | } |
| 3912 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3913 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3914 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3915 | * xmlCopyDtd: |
| 3916 | * @dtd: the dtd |
| 3917 | * |
| 3918 | * Do a copy of the dtd. |
| 3919 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3920 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3921 | */ |
| 3922 | xmlDtdPtr |
| 3923 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 3924 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3925 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3926 | |
| 3927 | if (dtd == NULL) return(NULL); |
| 3928 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 3929 | if (ret == NULL) return(NULL); |
| 3930 | if (dtd->entities != NULL) |
| 3931 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 3932 | (xmlEntitiesTablePtr) dtd->entities); |
| 3933 | if (dtd->notations != NULL) |
| 3934 | ret->notations = (void *) xmlCopyNotationTable( |
| 3935 | (xmlNotationTablePtr) dtd->notations); |
| 3936 | if (dtd->elements != NULL) |
| 3937 | ret->elements = (void *) xmlCopyElementTable( |
| 3938 | (xmlElementTablePtr) dtd->elements); |
| 3939 | if (dtd->attributes != NULL) |
| 3940 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 3941 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3942 | if (dtd->pentities != NULL) |
| 3943 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 3944 | (xmlEntitiesTablePtr) dtd->pentities); |
| 3945 | |
| 3946 | cur = dtd->children; |
| 3947 | while (cur != NULL) { |
| 3948 | q = NULL; |
| 3949 | |
| 3950 | if (cur->type == XML_ENTITY_DECL) { |
| 3951 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 3952 | switch (tmp->etype) { |
| 3953 | case XML_INTERNAL_GENERAL_ENTITY: |
| 3954 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 3955 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 3956 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 3957 | break; |
| 3958 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 3959 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 3960 | q = (xmlNodePtr) |
| 3961 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 3962 | break; |
| 3963 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 3964 | break; |
| 3965 | } |
| 3966 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 3967 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 3968 | q = (xmlNodePtr) |
| 3969 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 3970 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 3971 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
| 3972 | q = (xmlNodePtr) |
| 3973 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 3974 | } else if (cur->type == XML_COMMENT_NODE) { |
| 3975 | q = xmlCopyNode(cur, 0); |
| 3976 | } |
| 3977 | |
| 3978 | if (q == NULL) { |
| 3979 | cur = cur->next; |
| 3980 | continue; |
| 3981 | } |
| 3982 | |
| 3983 | if (p == NULL) |
| 3984 | ret->children = q; |
| 3985 | else |
| 3986 | p->next = q; |
| 3987 | |
| 3988 | q->prev = p; |
| 3989 | q->parent = (xmlNodePtr) ret; |
| 3990 | q->next = NULL; |
| 3991 | ret->last = q; |
| 3992 | p = q; |
| 3993 | cur = cur->next; |
| 3994 | } |
| 3995 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3996 | return(ret); |
| 3997 | } |
| 3998 | |
| 3999 | /** |
| 4000 | * xmlCopyDoc: |
| 4001 | * @doc: the document |
| 4002 | * @recursive: if 1 do a recursive copy. |
| 4003 | * |
| 4004 | * 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] | 4005 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4006 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4007 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4008 | */ |
| 4009 | xmlDocPtr |
| 4010 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 4011 | xmlDocPtr ret; |
| 4012 | |
| 4013 | if (doc == NULL) return(NULL); |
| 4014 | ret = xmlNewDoc(doc->version); |
| 4015 | if (ret == NULL) return(NULL); |
| 4016 | if (doc->name != NULL) |
| 4017 | ret->name = xmlMemStrdup(doc->name); |
| 4018 | if (doc->encoding != NULL) |
| 4019 | ret->encoding = xmlStrdup(doc->encoding); |
| 4020 | ret->charset = doc->charset; |
| 4021 | ret->compression = doc->compression; |
| 4022 | ret->standalone = doc->standalone; |
| 4023 | if (!recursive) return(ret); |
| 4024 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4025 | ret->last = NULL; |
| 4026 | ret->children = NULL; |
| 4027 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4028 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4029 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4030 | ret->intSubset->parent = ret; |
| 4031 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4032 | if (doc->oldNs != NULL) |
| 4033 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 4034 | if (doc->children != NULL) { |
| 4035 | xmlNodePtr tmp; |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4036 | |
| 4037 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 4038 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4039 | ret->last = NULL; |
| 4040 | tmp = ret->children; |
| 4041 | while (tmp != NULL) { |
| 4042 | if (tmp->next == NULL) |
| 4043 | ret->last = tmp; |
| 4044 | tmp = tmp->next; |
| 4045 | } |
| 4046 | } |
| 4047 | return(ret); |
| 4048 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4049 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4050 | |
| 4051 | /************************************************************************ |
| 4052 | * * |
| 4053 | * Content access functions * |
| 4054 | * * |
| 4055 | ************************************************************************/ |
| 4056 | |
| 4057 | /** |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4058 | * xmlGetLineNo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4059 | * @node: valid node |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4060 | * |
| 4061 | * Get line number of node. this requires activation of this option |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4062 | * before invoking the parser by calling xmlLineNumbersDefault(1) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4063 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4064 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4065 | */ |
| 4066 | long |
| 4067 | xmlGetLineNo(xmlNodePtr node) |
| 4068 | { |
| 4069 | long result = -1; |
| 4070 | |
| 4071 | if (!node) |
| 4072 | return result; |
| 4073 | if (node->type == XML_ELEMENT_NODE) |
| 4074 | result = (long) node->content; |
| 4075 | else if ((node->prev != NULL) && |
| 4076 | ((node->prev->type == XML_ELEMENT_NODE) || |
| 4077 | (node->prev->type == XML_TEXT_NODE))) |
| 4078 | result = xmlGetLineNo(node->prev); |
| 4079 | else if ((node->parent != NULL) && |
| 4080 | ((node->parent->type == XML_ELEMENT_NODE) || |
| 4081 | (node->parent->type == XML_TEXT_NODE))) |
| 4082 | result = xmlGetLineNo(node->parent); |
| 4083 | |
| 4084 | return result; |
| 4085 | } |
| 4086 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4087 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4088 | /** |
| 4089 | * xmlGetNodePath: |
| 4090 | * @node: a node |
| 4091 | * |
| 4092 | * Build a structure based Path for the given node |
| 4093 | * |
| 4094 | * Returns the new path or NULL in case of error. The caller must free |
| 4095 | * the returned string |
| 4096 | */ |
| 4097 | xmlChar * |
| 4098 | xmlGetNodePath(xmlNodePtr node) |
| 4099 | { |
| 4100 | xmlNodePtr cur, tmp, next; |
| 4101 | xmlChar *buffer = NULL, *temp; |
| 4102 | size_t buf_len; |
| 4103 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4104 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4105 | const char *name; |
| 4106 | char nametemp[100]; |
| 4107 | int occur = 0; |
| 4108 | |
| 4109 | if (node == NULL) |
| 4110 | return (NULL); |
| 4111 | |
| 4112 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4113 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4114 | if (buffer == NULL) { |
| 4115 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4116 | return (NULL); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4117 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4118 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4119 | if (buf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4120 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4121 | xmlFree(buffer); |
| 4122 | return (NULL); |
| 4123 | } |
| 4124 | |
| 4125 | buffer[0] = 0; |
| 4126 | cur = node; |
| 4127 | do { |
| 4128 | name = ""; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4129 | sep = "?"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4130 | occur = 0; |
| 4131 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4132 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4133 | if (buffer[0] == '/') |
| 4134 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4135 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4136 | next = NULL; |
| 4137 | } else if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4138 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4139 | name = (const char *) cur->name; |
| 4140 | if (cur->ns) { |
| 4141 | snprintf(nametemp, sizeof(nametemp) - 1, |
| 4142 | "%s:%s", cur->ns->prefix, cur->name); |
| 4143 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4144 | name = nametemp; |
| 4145 | } |
| 4146 | next = cur->parent; |
| 4147 | |
| 4148 | /* |
| 4149 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4150 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4151 | */ |
| 4152 | tmp = cur->prev; |
| 4153 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4154 | if ((tmp->type == XML_ELEMENT_NODE) && |
| 4155 | (xmlStrEqual(cur->name, tmp->name))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4156 | occur++; |
| 4157 | tmp = tmp->prev; |
| 4158 | } |
| 4159 | if (occur == 0) { |
| 4160 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4161 | while (tmp != NULL && occur == 0) { |
| 4162 | if ((tmp->type == XML_ELEMENT_NODE) && |
| 4163 | (xmlStrEqual(cur->name, tmp->name))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4164 | occur++; |
| 4165 | tmp = tmp->next; |
| 4166 | } |
| 4167 | if (occur != 0) |
| 4168 | occur = 1; |
| 4169 | } else |
| 4170 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4171 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4172 | sep = "/"; |
| 4173 | name = "comment()"; |
| 4174 | next = cur->parent; |
| 4175 | |
| 4176 | /* |
| 4177 | * Thumbler index computation |
| 4178 | */ |
| 4179 | tmp = cur->prev; |
| 4180 | while (tmp != NULL) { |
| 4181 | if (tmp->type == XML_COMMENT_NODE) |
| 4182 | occur++; |
| 4183 | tmp = tmp->prev; |
| 4184 | } |
| 4185 | if (occur == 0) { |
| 4186 | tmp = cur->next; |
| 4187 | while (tmp != NULL && occur == 0) { |
| 4188 | if (tmp->type == XML_COMMENT_NODE) |
| 4189 | occur++; |
| 4190 | tmp = tmp->next; |
| 4191 | } |
| 4192 | if (occur != 0) |
| 4193 | occur = 1; |
| 4194 | } else |
| 4195 | occur++; |
| 4196 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4197 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4198 | sep = "/"; |
| 4199 | name = "text()"; |
| 4200 | next = cur->parent; |
| 4201 | |
| 4202 | /* |
| 4203 | * Thumbler index computation |
| 4204 | */ |
| 4205 | tmp = cur->prev; |
| 4206 | while (tmp != NULL) { |
| 4207 | if ((cur->type == XML_TEXT_NODE) || |
| 4208 | (cur->type == XML_CDATA_SECTION_NODE)) |
| 4209 | occur++; |
| 4210 | tmp = tmp->prev; |
| 4211 | } |
| 4212 | if (occur == 0) { |
| 4213 | tmp = cur->next; |
| 4214 | while (tmp != NULL && occur == 0) { |
| 4215 | if ((cur->type == XML_TEXT_NODE) || |
| 4216 | (cur->type == XML_CDATA_SECTION_NODE)) |
| 4217 | occur++; |
| 4218 | tmp = tmp->next; |
| 4219 | } |
| 4220 | if (occur != 0) |
| 4221 | occur = 1; |
| 4222 | } else |
| 4223 | occur++; |
| 4224 | } else if (cur->type == XML_PI_NODE) { |
| 4225 | sep = "/"; |
| 4226 | snprintf(nametemp, sizeof(nametemp) - 1, |
| 4227 | "processing-instruction('%s')", cur->name); |
| 4228 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4229 | name = nametemp; |
| 4230 | |
| 4231 | next = cur->parent; |
| 4232 | |
| 4233 | /* |
| 4234 | * Thumbler index computation |
| 4235 | */ |
| 4236 | tmp = cur->prev; |
| 4237 | while (tmp != NULL) { |
| 4238 | if ((tmp->type == XML_PI_NODE) && |
| 4239 | (xmlStrEqual(cur->name, tmp->name))) |
| 4240 | occur++; |
| 4241 | tmp = tmp->prev; |
| 4242 | } |
| 4243 | if (occur == 0) { |
| 4244 | tmp = cur->next; |
| 4245 | while (tmp != NULL && occur == 0) { |
| 4246 | if ((tmp->type == XML_PI_NODE) && |
| 4247 | (xmlStrEqual(cur->name, tmp->name))) |
| 4248 | occur++; |
| 4249 | tmp = tmp->next; |
| 4250 | } |
| 4251 | if (occur != 0) |
| 4252 | occur = 1; |
| 4253 | } else |
| 4254 | occur++; |
| 4255 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4256 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4257 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4258 | name = (const char *) (((xmlAttrPtr) cur)->name); |
| 4259 | next = ((xmlAttrPtr) cur)->parent; |
| 4260 | } else { |
| 4261 | next = cur->parent; |
| 4262 | } |
| 4263 | |
| 4264 | /* |
| 4265 | * Make sure there is enough room |
| 4266 | */ |
| 4267 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4268 | buf_len = |
| 4269 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4270 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4271 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4272 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4273 | xmlFree(buf); |
| 4274 | xmlFree(buffer); |
| 4275 | return (NULL); |
| 4276 | } |
| 4277 | buffer = temp; |
| 4278 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4279 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4280 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4281 | xmlFree(buf); |
| 4282 | xmlFree(buffer); |
| 4283 | return (NULL); |
| 4284 | } |
| 4285 | buf = temp; |
| 4286 | } |
| 4287 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4288 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4289 | sep, name, (char *) buffer); |
| 4290 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4291 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4292 | sep, name, occur, (char *) buffer); |
| 4293 | snprintf((char *) buffer, buf_len, "%s", buf); |
| 4294 | cur = next; |
| 4295 | } while (cur != NULL); |
| 4296 | xmlFree(buf); |
| 4297 | return (buffer); |
| 4298 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4299 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4300 | |
| 4301 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4302 | * xmlDocGetRootElement: |
| 4303 | * @doc: the document |
| 4304 | * |
| 4305 | * Get the root element of the document (doc->children is a list |
| 4306 | * containing possibly comments, PIs, etc ...). |
| 4307 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4308 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4309 | */ |
| 4310 | xmlNodePtr |
| 4311 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 4312 | xmlNodePtr ret; |
| 4313 | |
| 4314 | if (doc == NULL) return(NULL); |
| 4315 | ret = doc->children; |
| 4316 | while (ret != NULL) { |
| 4317 | if (ret->type == XML_ELEMENT_NODE) |
| 4318 | return(ret); |
| 4319 | ret = ret->next; |
| 4320 | } |
| 4321 | return(ret); |
| 4322 | } |
| 4323 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4324 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4325 | /** |
| 4326 | * xmlDocSetRootElement: |
| 4327 | * @doc: the document |
| 4328 | * @root: the new document root element |
| 4329 | * |
| 4330 | * Set the root element of the document (doc->children is a list |
| 4331 | * containing possibly comments, PIs, etc ...). |
| 4332 | * |
| 4333 | * Returns the old root element if any was found |
| 4334 | */ |
| 4335 | xmlNodePtr |
| 4336 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4337 | xmlNodePtr old = NULL; |
| 4338 | |
| 4339 | if (doc == NULL) return(NULL); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4340 | if (root == NULL) |
| 4341 | return(NULL); |
| 4342 | xmlUnlinkNode(root); |
| 4343 | root->doc = doc; |
| 4344 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4345 | old = doc->children; |
| 4346 | while (old != NULL) { |
| 4347 | if (old->type == XML_ELEMENT_NODE) |
| 4348 | break; |
| 4349 | old = old->next; |
| 4350 | } |
| 4351 | if (old == NULL) { |
| 4352 | if (doc->children == NULL) { |
| 4353 | doc->children = root; |
| 4354 | doc->last = root; |
| 4355 | } else { |
| 4356 | xmlAddSibling(doc->children, root); |
| 4357 | } |
| 4358 | } else { |
| 4359 | xmlReplaceNode(old, root); |
| 4360 | } |
| 4361 | return(old); |
| 4362 | } |
| 4363 | |
| 4364 | /** |
| 4365 | * xmlNodeSetLang: |
| 4366 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4367 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4368 | * |
| 4369 | * Set the language of a node, i.e. the values of the xml:lang |
| 4370 | * attribute. |
| 4371 | */ |
| 4372 | void |
| 4373 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4374 | xmlNsPtr ns; |
| 4375 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4376 | if (cur == NULL) return; |
| 4377 | switch(cur->type) { |
| 4378 | case XML_TEXT_NODE: |
| 4379 | case XML_CDATA_SECTION_NODE: |
| 4380 | case XML_COMMENT_NODE: |
| 4381 | case XML_DOCUMENT_NODE: |
| 4382 | case XML_DOCUMENT_TYPE_NODE: |
| 4383 | case XML_DOCUMENT_FRAG_NODE: |
| 4384 | case XML_NOTATION_NODE: |
| 4385 | case XML_HTML_DOCUMENT_NODE: |
| 4386 | case XML_DTD_NODE: |
| 4387 | case XML_ELEMENT_DECL: |
| 4388 | case XML_ATTRIBUTE_DECL: |
| 4389 | case XML_ENTITY_DECL: |
| 4390 | case XML_PI_NODE: |
| 4391 | case XML_ENTITY_REF_NODE: |
| 4392 | case XML_ENTITY_NODE: |
| 4393 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4394 | #ifdef LIBXML_DOCB_ENABLED |
| 4395 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4396 | #endif |
| 4397 | case XML_XINCLUDE_START: |
| 4398 | case XML_XINCLUDE_END: |
| 4399 | return; |
| 4400 | case XML_ELEMENT_NODE: |
| 4401 | case XML_ATTRIBUTE_NODE: |
| 4402 | break; |
| 4403 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4404 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4405 | if (ns == NULL) |
| 4406 | return; |
| 4407 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4408 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4409 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4410 | |
| 4411 | /** |
| 4412 | * xmlNodeGetLang: |
| 4413 | * @cur: the node being checked |
| 4414 | * |
| 4415 | * Searches the language of a node, i.e. the values of the xml:lang |
| 4416 | * attribute or the one carried by the nearest ancestor. |
| 4417 | * |
| 4418 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4419 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4420 | */ |
| 4421 | xmlChar * |
| 4422 | xmlNodeGetLang(xmlNodePtr cur) { |
| 4423 | xmlChar *lang; |
| 4424 | |
| 4425 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 4426 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4427 | if (lang != NULL) |
| 4428 | return(lang); |
| 4429 | cur = cur->parent; |
| 4430 | } |
| 4431 | return(NULL); |
| 4432 | } |
| 4433 | |
| 4434 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4435 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4436 | /** |
| 4437 | * xmlNodeSetSpacePreserve: |
| 4438 | * @cur: the node being changed |
| 4439 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 4440 | * |
| 4441 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 4442 | * value of the xml:space attribute. |
| 4443 | */ |
| 4444 | void |
| 4445 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4446 | xmlNsPtr ns; |
| 4447 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4448 | if (cur == NULL) return; |
| 4449 | switch(cur->type) { |
| 4450 | case XML_TEXT_NODE: |
| 4451 | case XML_CDATA_SECTION_NODE: |
| 4452 | case XML_COMMENT_NODE: |
| 4453 | case XML_DOCUMENT_NODE: |
| 4454 | case XML_DOCUMENT_TYPE_NODE: |
| 4455 | case XML_DOCUMENT_FRAG_NODE: |
| 4456 | case XML_NOTATION_NODE: |
| 4457 | case XML_HTML_DOCUMENT_NODE: |
| 4458 | case XML_DTD_NODE: |
| 4459 | case XML_ELEMENT_DECL: |
| 4460 | case XML_ATTRIBUTE_DECL: |
| 4461 | case XML_ENTITY_DECL: |
| 4462 | case XML_PI_NODE: |
| 4463 | case XML_ENTITY_REF_NODE: |
| 4464 | case XML_ENTITY_NODE: |
| 4465 | case XML_NAMESPACE_DECL: |
| 4466 | case XML_XINCLUDE_START: |
| 4467 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4468 | #ifdef LIBXML_DOCB_ENABLED |
| 4469 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4470 | #endif |
| 4471 | return; |
| 4472 | case XML_ELEMENT_NODE: |
| 4473 | case XML_ATTRIBUTE_NODE: |
| 4474 | break; |
| 4475 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4476 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4477 | if (ns == NULL) |
| 4478 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4479 | switch (val) { |
| 4480 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4481 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4482 | break; |
| 4483 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4484 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4485 | break; |
| 4486 | } |
| 4487 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4488 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4489 | |
| 4490 | /** |
| 4491 | * xmlNodeGetSpacePreserve: |
| 4492 | * @cur: the node being checked |
| 4493 | * |
| 4494 | * Searches the space preserving behaviour of a node, i.e. the values |
| 4495 | * of the xml:space attribute or the one carried by the nearest |
| 4496 | * ancestor. |
| 4497 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4498 | * 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] | 4499 | */ |
| 4500 | int |
| 4501 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 4502 | xmlChar *space; |
| 4503 | |
| 4504 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4505 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4506 | if (space != NULL) { |
| 4507 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 4508 | xmlFree(space); |
| 4509 | return(1); |
| 4510 | } |
| 4511 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 4512 | xmlFree(space); |
| 4513 | return(0); |
| 4514 | } |
| 4515 | xmlFree(space); |
| 4516 | } |
| 4517 | cur = cur->parent; |
| 4518 | } |
| 4519 | return(-1); |
| 4520 | } |
| 4521 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4522 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4523 | /** |
| 4524 | * xmlNodeSetName: |
| 4525 | * @cur: the node being changed |
| 4526 | * @name: the new tag name |
| 4527 | * |
| 4528 | * Set (or reset) the name of a node. |
| 4529 | */ |
| 4530 | void |
| 4531 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
| 4532 | if (cur == NULL) return; |
| 4533 | if (name == NULL) return; |
| 4534 | switch(cur->type) { |
| 4535 | case XML_TEXT_NODE: |
| 4536 | case XML_CDATA_SECTION_NODE: |
| 4537 | case XML_COMMENT_NODE: |
| 4538 | case XML_DOCUMENT_TYPE_NODE: |
| 4539 | case XML_DOCUMENT_FRAG_NODE: |
| 4540 | case XML_NOTATION_NODE: |
| 4541 | case XML_HTML_DOCUMENT_NODE: |
| 4542 | case XML_NAMESPACE_DECL: |
| 4543 | case XML_XINCLUDE_START: |
| 4544 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4545 | #ifdef LIBXML_DOCB_ENABLED |
| 4546 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4547 | #endif |
| 4548 | return; |
| 4549 | case XML_ELEMENT_NODE: |
| 4550 | case XML_ATTRIBUTE_NODE: |
| 4551 | case XML_PI_NODE: |
| 4552 | case XML_ENTITY_REF_NODE: |
| 4553 | case XML_ENTITY_NODE: |
| 4554 | case XML_DTD_NODE: |
| 4555 | case XML_DOCUMENT_NODE: |
| 4556 | case XML_ELEMENT_DECL: |
| 4557 | case XML_ATTRIBUTE_DECL: |
| 4558 | case XML_ENTITY_DECL: |
| 4559 | break; |
| 4560 | } |
| 4561 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 4562 | cur->name = xmlStrdup(name); |
| 4563 | } |
| 4564 | |
| 4565 | /** |
| 4566 | * xmlNodeSetBase: |
| 4567 | * @cur: the node being changed |
| 4568 | * @uri: the new base URI |
| 4569 | * |
| 4570 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 4571 | * xml:base attribute. |
| 4572 | */ |
| 4573 | void |
Daniel Veillard | f85ce8e | 2003-09-22 10:24:45 +0000 | [diff] [blame] | 4574 | xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4575 | xmlNsPtr ns; |
| 4576 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4577 | if (cur == NULL) return; |
| 4578 | switch(cur->type) { |
| 4579 | case XML_TEXT_NODE: |
| 4580 | case XML_CDATA_SECTION_NODE: |
| 4581 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4582 | case XML_DOCUMENT_TYPE_NODE: |
| 4583 | case XML_DOCUMENT_FRAG_NODE: |
| 4584 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4585 | case XML_DTD_NODE: |
| 4586 | case XML_ELEMENT_DECL: |
| 4587 | case XML_ATTRIBUTE_DECL: |
| 4588 | case XML_ENTITY_DECL: |
| 4589 | case XML_PI_NODE: |
| 4590 | case XML_ENTITY_REF_NODE: |
| 4591 | case XML_ENTITY_NODE: |
| 4592 | case XML_NAMESPACE_DECL: |
| 4593 | case XML_XINCLUDE_START: |
| 4594 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4595 | return; |
| 4596 | case XML_ELEMENT_NODE: |
| 4597 | case XML_ATTRIBUTE_NODE: |
| 4598 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 4599 | case XML_DOCUMENT_NODE: |
| 4600 | #ifdef LIBXML_DOCB_ENABLED |
| 4601 | case XML_DOCB_DOCUMENT_NODE: |
| 4602 | #endif |
| 4603 | case XML_HTML_DOCUMENT_NODE: { |
| 4604 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 4605 | |
| 4606 | if (doc->URL != NULL) |
| 4607 | xmlFree((xmlChar *) doc->URL); |
| 4608 | if (uri == NULL) |
| 4609 | doc->URL = NULL; |
| 4610 | else |
| 4611 | doc->URL = xmlStrdup(uri); |
| 4612 | return; |
| 4613 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4614 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4615 | |
| 4616 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4617 | if (ns == NULL) |
| 4618 | return; |
| 4619 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4620 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4621 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4622 | |
| 4623 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4624 | * xmlNodeGetBase: |
| 4625 | * @doc: the document the node pertains to |
| 4626 | * @cur: the node being checked |
| 4627 | * |
| 4628 | * Searches for the BASE URL. The code should work on both XML |
| 4629 | * and HTML document even if base mechanisms are completely different. |
| 4630 | * It returns the base as defined in RFC 2396 sections |
| 4631 | * 5.1.1. Base URI within Document Content |
| 4632 | * and |
| 4633 | * 5.1.2. Base URI from the Encapsulating Entity |
| 4634 | * However it does not return the document base (5.1.3), use |
| 4635 | * xmlDocumentGetBase() for this |
| 4636 | * |
| 4637 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4638 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4639 | */ |
| 4640 | xmlChar * |
| 4641 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4642 | xmlChar *oldbase = NULL; |
| 4643 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4644 | |
| 4645 | if ((cur == NULL) && (doc == NULL)) |
| 4646 | return(NULL); |
| 4647 | if (doc == NULL) doc = cur->doc; |
| 4648 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 4649 | cur = doc->children; |
| 4650 | while ((cur != NULL) && (cur->name != NULL)) { |
| 4651 | if (cur->type != XML_ELEMENT_NODE) { |
| 4652 | cur = cur->next; |
| 4653 | continue; |
| 4654 | } |
| 4655 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 4656 | cur = cur->children; |
| 4657 | continue; |
| 4658 | } |
| 4659 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 4660 | cur = cur->children; |
| 4661 | continue; |
| 4662 | } |
| 4663 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 4664 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 4665 | } |
| 4666 | cur = cur->next; |
| 4667 | } |
| 4668 | return(NULL); |
| 4669 | } |
| 4670 | while (cur != NULL) { |
| 4671 | if (cur->type == XML_ENTITY_DECL) { |
| 4672 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 4673 | return(xmlStrdup(ent->URI)); |
| 4674 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4675 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4676 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4677 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4678 | if (oldbase != NULL) { |
| 4679 | newbase = xmlBuildURI(oldbase, base); |
| 4680 | if (newbase != NULL) { |
| 4681 | xmlFree(oldbase); |
| 4682 | xmlFree(base); |
| 4683 | oldbase = newbase; |
| 4684 | } else { |
| 4685 | xmlFree(oldbase); |
| 4686 | xmlFree(base); |
| 4687 | return(NULL); |
| 4688 | } |
| 4689 | } else { |
| 4690 | oldbase = base; |
| 4691 | } |
| 4692 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 4693 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 4694 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 4695 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4696 | } |
| 4697 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4698 | cur = cur->parent; |
| 4699 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4700 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 4701 | if (oldbase == NULL) |
| 4702 | return(xmlStrdup(doc->URL)); |
| 4703 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 4704 | xmlFree(oldbase); |
| 4705 | return(newbase); |
| 4706 | } |
| 4707 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | /** |
| 4711 | * xmlNodeGetContent: |
| 4712 | * @cur: the node being read |
| 4713 | * |
| 4714 | * Read the value of a node, this can be either the text carried |
| 4715 | * directly by this node if it's a TEXT node or the aggregate string |
| 4716 | * 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] | 4717 | * Entity references are substituted. |
| 4718 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4719 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4720 | */ |
| 4721 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4722 | xmlNodeGetContent(xmlNodePtr cur) |
| 4723 | { |
| 4724 | if (cur == NULL) |
| 4725 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4726 | switch (cur->type) { |
| 4727 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4728 | case XML_ELEMENT_NODE:{ |
| 4729 | xmlNodePtr tmp = cur; |
| 4730 | xmlBufferPtr buffer; |
| 4731 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4732 | |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 4733 | buffer = xmlBufferCreateSize(64); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4734 | if (buffer == NULL) |
| 4735 | return (NULL); |
| 4736 | while (tmp != NULL) { |
| 4737 | switch (tmp->type) { |
| 4738 | case XML_CDATA_SECTION_NODE: |
| 4739 | case XML_TEXT_NODE: |
| 4740 | if (tmp->content != NULL) |
| 4741 | xmlBufferCat(buffer, tmp->content); |
| 4742 | break; |
| 4743 | case XML_ENTITY_REF_NODE:{ |
| 4744 | /* recursive substitution of entity references */ |
| 4745 | xmlChar *cont = xmlNodeGetContent(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4746 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4747 | if (cont) { |
| 4748 | xmlBufferCat(buffer, |
| 4749 | (const xmlChar *) cont); |
| 4750 | xmlFree(cont); |
| 4751 | } |
| 4752 | break; |
| 4753 | } |
| 4754 | default: |
| 4755 | break; |
| 4756 | } |
| 4757 | /* |
| 4758 | * Skip to next node |
| 4759 | */ |
| 4760 | if (tmp->children != NULL) { |
| 4761 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4762 | tmp = tmp->children; |
| 4763 | continue; |
| 4764 | } |
| 4765 | } |
| 4766 | if (tmp == cur) |
| 4767 | break; |
Daniel Veillard | 6c83120 | 2001-03-07 15:57:53 +0000 | [diff] [blame] | 4768 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4769 | if (tmp->next != NULL) { |
| 4770 | tmp = tmp->next; |
| 4771 | continue; |
| 4772 | } |
| 4773 | |
| 4774 | do { |
| 4775 | tmp = tmp->parent; |
| 4776 | if (tmp == NULL) |
| 4777 | break; |
| 4778 | if (tmp == cur) { |
| 4779 | tmp = NULL; |
| 4780 | break; |
| 4781 | } |
| 4782 | if (tmp->next != NULL) { |
| 4783 | tmp = tmp->next; |
| 4784 | break; |
| 4785 | } |
| 4786 | } while (tmp != NULL); |
| 4787 | } |
| 4788 | ret = buffer->content; |
| 4789 | buffer->content = NULL; |
| 4790 | xmlBufferFree(buffer); |
| 4791 | return (ret); |
| 4792 | } |
| 4793 | case XML_ATTRIBUTE_NODE:{ |
| 4794 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 4795 | |
| 4796 | if (attr->parent != NULL) |
| 4797 | return (xmlNodeListGetString |
| 4798 | (attr->parent->doc, attr->children, 1)); |
| 4799 | else |
| 4800 | return (xmlNodeListGetString(NULL, attr->children, 1)); |
| 4801 | break; |
| 4802 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4803 | case XML_COMMENT_NODE: |
| 4804 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4805 | if (cur->content != NULL) |
| 4806 | return (xmlStrdup(cur->content)); |
| 4807 | return (NULL); |
| 4808 | case XML_ENTITY_REF_NODE:{ |
| 4809 | xmlEntityPtr ent; |
| 4810 | xmlNodePtr tmp; |
| 4811 | xmlBufferPtr buffer; |
| 4812 | xmlChar *ret; |
| 4813 | |
| 4814 | /* lookup entity declaration */ |
| 4815 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 4816 | if (ent == NULL) |
| 4817 | return (NULL); |
| 4818 | |
| 4819 | buffer = xmlBufferCreate(); |
| 4820 | if (buffer == NULL) |
| 4821 | return (NULL); |
| 4822 | |
| 4823 | /* an entity content can be any "well balanced chunk", |
| 4824 | * i.e. the result of the content [43] production: |
| 4825 | * http://www.w3.org/TR/REC-xml#NT-content |
| 4826 | * -> we iterate through child nodes and recursive call |
| 4827 | * xmlNodeGetContent() which handles all possible node types */ |
| 4828 | tmp = ent->children; |
| 4829 | while (tmp) { |
| 4830 | xmlChar *cont = xmlNodeGetContent(tmp); |
| 4831 | |
| 4832 | if (cont) { |
| 4833 | xmlBufferCat(buffer, (const xmlChar *) cont); |
| 4834 | xmlFree(cont); |
| 4835 | } |
| 4836 | tmp = tmp->next; |
| 4837 | } |
| 4838 | |
| 4839 | ret = buffer->content; |
| 4840 | buffer->content = NULL; |
| 4841 | xmlBufferFree(buffer); |
| 4842 | return (ret); |
| 4843 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4844 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4845 | case XML_DOCUMENT_TYPE_NODE: |
| 4846 | case XML_NOTATION_NODE: |
| 4847 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4848 | case XML_XINCLUDE_START: |
| 4849 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4850 | return (NULL); |
| 4851 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4852 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4853 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4854 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4855 | case XML_HTML_DOCUMENT_NODE: { |
| 4856 | xmlChar *tmp; |
| 4857 | xmlChar *res = NULL; |
| 4858 | |
| 4859 | cur = cur->children; |
| 4860 | while (cur!= NULL) { |
| 4861 | if ((cur->type == XML_ELEMENT_NODE) || |
| 4862 | (cur->type == XML_TEXT_NODE) || |
| 4863 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4864 | tmp = xmlNodeGetContent(cur); |
| 4865 | if (tmp != NULL) { |
| 4866 | if (res == NULL) |
| 4867 | res = tmp; |
| 4868 | else { |
| 4869 | res = xmlStrcat(res, tmp); |
| 4870 | xmlFree(tmp); |
| 4871 | } |
| 4872 | } |
| 4873 | } |
| 4874 | cur = cur->next; |
| 4875 | } |
| 4876 | return(res); |
| 4877 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4878 | case XML_NAMESPACE_DECL: { |
| 4879 | xmlChar *tmp; |
| 4880 | |
| 4881 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 4882 | return (tmp); |
| 4883 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4884 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4885 | /* TODO !!! */ |
| 4886 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4887 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4888 | /* TODO !!! */ |
| 4889 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4890 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4891 | /* TODO !!! */ |
| 4892 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4893 | case XML_CDATA_SECTION_NODE: |
| 4894 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4895 | if (cur->content != NULL) |
| 4896 | return (xmlStrdup(cur->content)); |
| 4897 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4898 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4899 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4900 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4901 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4902 | /** |
| 4903 | * xmlNodeSetContent: |
| 4904 | * @cur: the node being modified |
| 4905 | * @content: the new value of the content |
| 4906 | * |
| 4907 | * Replace the content of a node. |
| 4908 | */ |
| 4909 | void |
| 4910 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 4911 | if (cur == NULL) { |
| 4912 | #ifdef DEBUG_TREE |
| 4913 | xmlGenericError(xmlGenericErrorContext, |
| 4914 | "xmlNodeSetContent : node == NULL\n"); |
| 4915 | #endif |
| 4916 | return; |
| 4917 | } |
| 4918 | switch (cur->type) { |
| 4919 | case XML_DOCUMENT_FRAG_NODE: |
| 4920 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 4921 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4922 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 4923 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 4924 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 4925 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4926 | case XML_TEXT_NODE: |
| 4927 | case XML_CDATA_SECTION_NODE: |
| 4928 | case XML_ENTITY_REF_NODE: |
| 4929 | case XML_ENTITY_NODE: |
| 4930 | case XML_PI_NODE: |
| 4931 | case XML_COMMENT_NODE: |
| 4932 | if (cur->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4933 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4934 | } |
| 4935 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 4936 | cur->last = cur->children = NULL; |
| 4937 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4938 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4939 | } else |
| 4940 | cur->content = NULL; |
| 4941 | break; |
| 4942 | case XML_DOCUMENT_NODE: |
| 4943 | case XML_HTML_DOCUMENT_NODE: |
| 4944 | case XML_DOCUMENT_TYPE_NODE: |
| 4945 | case XML_XINCLUDE_START: |
| 4946 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4947 | #ifdef LIBXML_DOCB_ENABLED |
| 4948 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4949 | #endif |
| 4950 | break; |
| 4951 | case XML_NOTATION_NODE: |
| 4952 | break; |
| 4953 | case XML_DTD_NODE: |
| 4954 | break; |
| 4955 | case XML_NAMESPACE_DECL: |
| 4956 | break; |
| 4957 | case XML_ELEMENT_DECL: |
| 4958 | /* TODO !!! */ |
| 4959 | break; |
| 4960 | case XML_ATTRIBUTE_DECL: |
| 4961 | /* TODO !!! */ |
| 4962 | break; |
| 4963 | case XML_ENTITY_DECL: |
| 4964 | /* TODO !!! */ |
| 4965 | break; |
| 4966 | } |
| 4967 | } |
| 4968 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4969 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4970 | /** |
| 4971 | * xmlNodeSetContentLen: |
| 4972 | * @cur: the node being modified |
| 4973 | * @content: the new value of the content |
| 4974 | * @len: the size of @content |
| 4975 | * |
| 4976 | * Replace the content of a node. |
| 4977 | */ |
| 4978 | void |
| 4979 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 4980 | if (cur == NULL) { |
| 4981 | #ifdef DEBUG_TREE |
| 4982 | xmlGenericError(xmlGenericErrorContext, |
| 4983 | "xmlNodeSetContentLen : node == NULL\n"); |
| 4984 | #endif |
| 4985 | return; |
| 4986 | } |
| 4987 | switch (cur->type) { |
| 4988 | case XML_DOCUMENT_FRAG_NODE: |
| 4989 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 4990 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4991 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 4992 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 4993 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 4994 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4995 | case XML_TEXT_NODE: |
| 4996 | case XML_CDATA_SECTION_NODE: |
| 4997 | case XML_ENTITY_REF_NODE: |
| 4998 | case XML_ENTITY_NODE: |
| 4999 | case XML_PI_NODE: |
| 5000 | case XML_COMMENT_NODE: |
| 5001 | case XML_NOTATION_NODE: |
| 5002 | if (cur->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5003 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5004 | } |
| 5005 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5006 | cur->children = cur->last = NULL; |
| 5007 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5008 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5009 | } else |
| 5010 | cur->content = NULL; |
| 5011 | break; |
| 5012 | case XML_DOCUMENT_NODE: |
| 5013 | case XML_DTD_NODE: |
| 5014 | case XML_HTML_DOCUMENT_NODE: |
| 5015 | case XML_DOCUMENT_TYPE_NODE: |
| 5016 | case XML_NAMESPACE_DECL: |
| 5017 | case XML_XINCLUDE_START: |
| 5018 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5019 | #ifdef LIBXML_DOCB_ENABLED |
| 5020 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5021 | #endif |
| 5022 | break; |
| 5023 | case XML_ELEMENT_DECL: |
| 5024 | /* TODO !!! */ |
| 5025 | break; |
| 5026 | case XML_ATTRIBUTE_DECL: |
| 5027 | /* TODO !!! */ |
| 5028 | break; |
| 5029 | case XML_ENTITY_DECL: |
| 5030 | /* TODO !!! */ |
| 5031 | break; |
| 5032 | } |
| 5033 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5034 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5035 | |
| 5036 | /** |
| 5037 | * xmlNodeAddContentLen: |
| 5038 | * @cur: the node being modified |
| 5039 | * @content: extra content |
| 5040 | * @len: the size of @content |
| 5041 | * |
| 5042 | * Append the extra substring to the node content. |
| 5043 | */ |
| 5044 | void |
| 5045 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5046 | if (cur == NULL) { |
| 5047 | #ifdef DEBUG_TREE |
| 5048 | xmlGenericError(xmlGenericErrorContext, |
| 5049 | "xmlNodeAddContentLen : node == NULL\n"); |
| 5050 | #endif |
| 5051 | return; |
| 5052 | } |
| 5053 | if (len <= 0) return; |
| 5054 | switch (cur->type) { |
| 5055 | case XML_DOCUMENT_FRAG_NODE: |
| 5056 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5057 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5058 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 5059 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5060 | newNode = xmlNewTextLen(content, len); |
| 5061 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5062 | tmp = xmlAddChild(cur, newNode); |
| 5063 | if (tmp != newNode) |
| 5064 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5065 | if ((last != NULL) && (last->next == newNode)) { |
| 5066 | xmlTextMerge(last, newNode); |
| 5067 | } |
| 5068 | } |
| 5069 | break; |
| 5070 | } |
| 5071 | case XML_ATTRIBUTE_NODE: |
| 5072 | break; |
| 5073 | case XML_TEXT_NODE: |
| 5074 | case XML_CDATA_SECTION_NODE: |
| 5075 | case XML_ENTITY_REF_NODE: |
| 5076 | case XML_ENTITY_NODE: |
| 5077 | case XML_PI_NODE: |
| 5078 | case XML_COMMENT_NODE: |
| 5079 | case XML_NOTATION_NODE: |
| 5080 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5081 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5082 | } |
| 5083 | case XML_DOCUMENT_NODE: |
| 5084 | case XML_DTD_NODE: |
| 5085 | case XML_HTML_DOCUMENT_NODE: |
| 5086 | case XML_DOCUMENT_TYPE_NODE: |
| 5087 | case XML_NAMESPACE_DECL: |
| 5088 | case XML_XINCLUDE_START: |
| 5089 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5090 | #ifdef LIBXML_DOCB_ENABLED |
| 5091 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5092 | #endif |
| 5093 | break; |
| 5094 | case XML_ELEMENT_DECL: |
| 5095 | case XML_ATTRIBUTE_DECL: |
| 5096 | case XML_ENTITY_DECL: |
| 5097 | break; |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | /** |
| 5102 | * xmlNodeAddContent: |
| 5103 | * @cur: the node being modified |
| 5104 | * @content: extra content |
| 5105 | * |
| 5106 | * Append the extra substring to the node content. |
| 5107 | */ |
| 5108 | void |
| 5109 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5110 | int len; |
| 5111 | |
| 5112 | if (cur == NULL) { |
| 5113 | #ifdef DEBUG_TREE |
| 5114 | xmlGenericError(xmlGenericErrorContext, |
| 5115 | "xmlNodeAddContent : node == NULL\n"); |
| 5116 | #endif |
| 5117 | return; |
| 5118 | } |
| 5119 | if (content == NULL) return; |
| 5120 | len = xmlStrlen(content); |
| 5121 | xmlNodeAddContentLen(cur, content, len); |
| 5122 | } |
| 5123 | |
| 5124 | /** |
| 5125 | * xmlTextMerge: |
| 5126 | * @first: the first text node |
| 5127 | * @second: the second text node being merged |
| 5128 | * |
| 5129 | * Merge two text nodes into one |
| 5130 | * Returns the first text node augmented |
| 5131 | */ |
| 5132 | xmlNodePtr |
| 5133 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5134 | if (first == NULL) return(second); |
| 5135 | if (second == NULL) return(first); |
| 5136 | if (first->type != XML_TEXT_NODE) return(first); |
| 5137 | if (second->type != XML_TEXT_NODE) return(first); |
| 5138 | if (second->name != first->name) |
| 5139 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5140 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5141 | xmlUnlinkNode(second); |
| 5142 | xmlFreeNode(second); |
| 5143 | return(first); |
| 5144 | } |
| 5145 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5146 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5147 | /** |
| 5148 | * xmlGetNsList: |
| 5149 | * @doc: the document |
| 5150 | * @node: the current node |
| 5151 | * |
| 5152 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5153 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5154 | * that need to be freed by the caller or NULL if no |
| 5155 | * namespace if defined |
| 5156 | */ |
| 5157 | xmlNsPtr * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5158 | xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) |
| 5159 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5160 | xmlNsPtr cur; |
| 5161 | xmlNsPtr *ret = NULL; |
| 5162 | int nbns = 0; |
| 5163 | int maxns = 10; |
| 5164 | int i; |
| 5165 | |
| 5166 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5167 | if (node->type == XML_ELEMENT_NODE) { |
| 5168 | cur = node->nsDef; |
| 5169 | while (cur != NULL) { |
| 5170 | if (ret == NULL) { |
| 5171 | ret = |
| 5172 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5173 | sizeof(xmlNsPtr)); |
| 5174 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5175 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5176 | return (NULL); |
| 5177 | } |
| 5178 | ret[nbns] = NULL; |
| 5179 | } |
| 5180 | for (i = 0; i < nbns; i++) { |
| 5181 | if ((cur->prefix == ret[i]->prefix) || |
| 5182 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5183 | break; |
| 5184 | } |
| 5185 | if (i >= nbns) { |
| 5186 | if (nbns >= maxns) { |
| 5187 | maxns *= 2; |
| 5188 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5189 | (maxns + |
| 5190 | 1) * |
| 5191 | sizeof(xmlNsPtr)); |
| 5192 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5193 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5194 | return (NULL); |
| 5195 | } |
| 5196 | } |
| 5197 | ret[nbns++] = cur; |
| 5198 | ret[nbns] = NULL; |
| 5199 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5200 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5201 | cur = cur->next; |
| 5202 | } |
| 5203 | } |
| 5204 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5205 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5206 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5207 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5208 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5209 | |
| 5210 | /** |
| 5211 | * xmlSearchNs: |
| 5212 | * @doc: the document |
| 5213 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5214 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5215 | * |
| 5216 | * Search a Ns registered under a given name space for a document. |
| 5217 | * recurse on the parents until it finds the defined namespace |
| 5218 | * or return NULL otherwise. |
| 5219 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5220 | * We don't allow to cross entities boundaries. If you don't declare |
| 5221 | * the namespace within those you will be in troubles !!! A warning |
| 5222 | * is generated to cover this case. |
| 5223 | * |
| 5224 | * Returns the namespace pointer or NULL. |
| 5225 | */ |
| 5226 | xmlNsPtr |
| 5227 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
| 5228 | xmlNsPtr cur; |
| 5229 | |
| 5230 | if (node == NULL) return(NULL); |
| 5231 | if ((nameSpace != NULL) && |
| 5232 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5233 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5234 | /* |
| 5235 | * The XML-1.0 namespace is normally held on the root |
| 5236 | * element. In this case exceptionally create it on the |
| 5237 | * node element. |
| 5238 | */ |
| 5239 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5240 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5241 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5242 | return(NULL); |
| 5243 | } |
| 5244 | memset(cur, 0, sizeof(xmlNs)); |
| 5245 | cur->type = XML_LOCAL_NAMESPACE; |
| 5246 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5247 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5248 | cur->next = node->nsDef; |
| 5249 | node->nsDef = cur; |
| 5250 | return(cur); |
| 5251 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5252 | if (doc->oldNs == NULL) { |
| 5253 | /* |
| 5254 | * Allocate a new Namespace and fill the fields. |
| 5255 | */ |
| 5256 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5257 | if (doc->oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5258 | xmlTreeErrMemory("searching namespace"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5259 | return(NULL); |
| 5260 | } |
| 5261 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5262 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
| 5263 | |
| 5264 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5265 | doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5266 | } |
| 5267 | return(doc->oldNs); |
| 5268 | } |
| 5269 | while (node != NULL) { |
| 5270 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5271 | (node->type == XML_ENTITY_NODE) || |
| 5272 | (node->type == XML_ENTITY_DECL)) |
| 5273 | return(NULL); |
| 5274 | if (node->type == XML_ELEMENT_NODE) { |
| 5275 | cur = node->nsDef; |
| 5276 | while (cur != NULL) { |
| 5277 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5278 | (cur->href != NULL)) |
| 5279 | return(cur); |
| 5280 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5281 | (cur->href != NULL) && |
| 5282 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5283 | return(cur); |
| 5284 | cur = cur->next; |
| 5285 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5286 | cur = node->ns; |
| 5287 | if (cur != NULL) { |
| 5288 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5289 | (cur->href != NULL)) |
| 5290 | return(cur); |
| 5291 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5292 | (cur->href != NULL) && |
| 5293 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5294 | return(cur); |
| 5295 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5296 | } |
| 5297 | node = node->parent; |
| 5298 | } |
| 5299 | return(NULL); |
| 5300 | } |
| 5301 | |
| 5302 | /** |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5303 | * xmlNsInScope: |
| 5304 | * @doc: the document |
| 5305 | * @node: the current node |
| 5306 | * @ancestor: the ancestor carrying the namespace |
| 5307 | * @prefix: the namespace prefix |
| 5308 | * |
| 5309 | * Verify that the given namespace held on @ancestor is still in scope |
| 5310 | * on node. |
| 5311 | * |
| 5312 | * Returns 1 if true, 0 if false and -1 in case of error. |
| 5313 | */ |
| 5314 | static int |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5315 | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 5316 | xmlNodePtr ancestor, const xmlChar * prefix) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5317 | { |
| 5318 | xmlNsPtr tst; |
| 5319 | |
| 5320 | while ((node != NULL) && (node != ancestor)) { |
| 5321 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5322 | (node->type == XML_ENTITY_NODE) || |
| 5323 | (node->type == XML_ENTITY_DECL)) |
| 5324 | return (-1); |
| 5325 | if (node->type == XML_ELEMENT_NODE) { |
| 5326 | tst = node->nsDef; |
| 5327 | while (tst != NULL) { |
| 5328 | if ((tst->prefix == NULL) |
| 5329 | && (prefix == NULL)) |
| 5330 | return (0); |
| 5331 | if ((tst->prefix != NULL) |
| 5332 | && (prefix != NULL) |
| 5333 | && (xmlStrEqual(tst->prefix, prefix))) |
| 5334 | return (0); |
| 5335 | tst = tst->next; |
| 5336 | } |
| 5337 | } |
| 5338 | node = node->parent; |
| 5339 | } |
| 5340 | if (node != ancestor) |
| 5341 | return (-1); |
| 5342 | return (1); |
| 5343 | } |
| 5344 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5345 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5346 | * xmlSearchNsByHref: |
| 5347 | * @doc: the document |
| 5348 | * @node: the current node |
| 5349 | * @href: the namespace value |
| 5350 | * |
| 5351 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 5352 | * the defined namespace or return NULL otherwise. |
| 5353 | * Returns the namespace pointer or NULL. |
| 5354 | */ |
| 5355 | xmlNsPtr |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5356 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) |
| 5357 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5358 | xmlNsPtr cur; |
| 5359 | xmlNodePtr orig = node; |
| 5360 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5361 | if ((node == NULL) || (href == NULL)) |
| 5362 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5363 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5364 | /* |
| 5365 | * Only the document can hold the XML spec namespace. |
| 5366 | */ |
| 5367 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5368 | /* |
| 5369 | * The XML-1.0 namespace is normally held on the root |
| 5370 | * element. In this case exceptionally create it on the |
| 5371 | * node element. |
| 5372 | */ |
| 5373 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5374 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5375 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5376 | return (NULL); |
| 5377 | } |
| 5378 | memset(cur, 0, sizeof(xmlNs)); |
| 5379 | cur->type = XML_LOCAL_NAMESPACE; |
| 5380 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5381 | cur->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5382 | cur->next = node->nsDef; |
| 5383 | node->nsDef = cur; |
| 5384 | return (cur); |
| 5385 | } |
| 5386 | if (doc->oldNs == NULL) { |
| 5387 | /* |
| 5388 | * Allocate a new Namespace and fill the fields. |
| 5389 | */ |
| 5390 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5391 | if (doc->oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5392 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5393 | return (NULL); |
| 5394 | } |
| 5395 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5396 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5397 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5398 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5399 | doc->oldNs->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5400 | } |
| 5401 | return (doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5402 | } |
| 5403 | while (node != NULL) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5404 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5405 | (node->type == XML_ENTITY_NODE) || |
| 5406 | (node->type == XML_ENTITY_DECL)) |
| 5407 | return (NULL); |
| 5408 | if (node->type == XML_ELEMENT_NODE) { |
| 5409 | cur = node->nsDef; |
| 5410 | while (cur != NULL) { |
| 5411 | if ((cur->href != NULL) && (href != NULL) && |
| 5412 | (xmlStrEqual(cur->href, href))) { |
| 5413 | if (xmlNsInScope(doc, orig, node, cur->href) == 1) |
| 5414 | return (cur); |
| 5415 | } |
| 5416 | cur = cur->next; |
| 5417 | } |
| 5418 | cur = node->ns; |
| 5419 | if (cur != NULL) { |
| 5420 | if ((cur->href != NULL) && (href != NULL) && |
| 5421 | (xmlStrEqual(cur->href, href))) { |
| 5422 | if (xmlNsInScope(doc, orig, node, cur->href) == 1) |
| 5423 | return (cur); |
| 5424 | } |
| 5425 | } |
| 5426 | } |
| 5427 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5428 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5429 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
| 5432 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5433 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5434 | * @doc: the document |
| 5435 | * @tree: a node expected to hold the new namespace |
| 5436 | * @ns: the original namespace |
| 5437 | * |
| 5438 | * This function tries to locate a namespace definition in a tree |
| 5439 | * ancestors, or create a new namespace definition node similar to |
| 5440 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 5441 | * null (default namespace) or reused within the subtree defined by |
| 5442 | * @tree or on one of its ancestors then a new prefix is generated. |
| 5443 | * Returns the (new) namespace definition or NULL in case of error |
| 5444 | */ |
| 5445 | xmlNsPtr |
| 5446 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 5447 | xmlNsPtr def; |
| 5448 | xmlChar prefix[50]; |
| 5449 | int counter = 1; |
| 5450 | |
| 5451 | if (tree == NULL) { |
| 5452 | #ifdef DEBUG_TREE |
| 5453 | xmlGenericError(xmlGenericErrorContext, |
| 5454 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 5455 | #endif |
| 5456 | return(NULL); |
| 5457 | } |
| 5458 | if (ns == NULL) { |
| 5459 | #ifdef DEBUG_TREE |
| 5460 | xmlGenericError(xmlGenericErrorContext, |
| 5461 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 5462 | #endif |
| 5463 | return(NULL); |
| 5464 | } |
| 5465 | /* |
| 5466 | * Search an existing namespace definition inherited. |
| 5467 | */ |
| 5468 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 5469 | if (def != NULL) |
| 5470 | return(def); |
| 5471 | |
| 5472 | /* |
| 5473 | * Find a close prefix which is not already in use. |
| 5474 | * Let's strip namespace prefixes longer than 20 chars ! |
| 5475 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5476 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5477 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5478 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5479 | snprintf((char *) prefix, sizeof(prefix), "%.20s", ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5480 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5481 | def = xmlSearchNs(doc, tree, prefix); |
| 5482 | while (def != NULL) { |
| 5483 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5484 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5485 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5486 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5487 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5488 | def = xmlSearchNs(doc, tree, prefix); |
| 5489 | } |
| 5490 | |
| 5491 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5492 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5493 | */ |
| 5494 | def = xmlNewNs(tree, ns->href, prefix); |
| 5495 | return(def); |
| 5496 | } |
| 5497 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5498 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5499 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5500 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5501 | * @doc: the document |
| 5502 | * @tree: a node defining the subtree to reconciliate |
| 5503 | * |
| 5504 | * This function checks that all the namespaces declared within the given |
| 5505 | * tree are properly declared. This is needed for example after Copy or Cut |
| 5506 | * and then paste operations. The subtree may still hold pointers to |
| 5507 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5508 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5509 | * the new environment. If not possible the new namespaces are redeclared |
| 5510 | * on @tree at the top of the given subtree. |
| 5511 | * Returns the number of namespace declarations created or -1 in case of error. |
| 5512 | */ |
| 5513 | int |
| 5514 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 5515 | xmlNsPtr *oldNs = NULL; |
| 5516 | xmlNsPtr *newNs = NULL; |
| 5517 | int sizeCache = 0; |
| 5518 | int nbCache = 0; |
| 5519 | |
| 5520 | xmlNsPtr n; |
| 5521 | xmlNodePtr node = tree; |
| 5522 | xmlAttrPtr attr; |
| 5523 | int ret = 0, i; |
| 5524 | |
| 5525 | while (node != NULL) { |
| 5526 | /* |
| 5527 | * Reconciliate the node namespace |
| 5528 | */ |
| 5529 | if (node->ns != NULL) { |
| 5530 | /* |
| 5531 | * initialize the cache if needed |
| 5532 | */ |
| 5533 | if (sizeCache == 0) { |
| 5534 | sizeCache = 10; |
| 5535 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5536 | sizeof(xmlNsPtr)); |
| 5537 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5538 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5539 | return(-1); |
| 5540 | } |
| 5541 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5542 | sizeof(xmlNsPtr)); |
| 5543 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5544 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5545 | xmlFree(oldNs); |
| 5546 | return(-1); |
| 5547 | } |
| 5548 | } |
| 5549 | for (i = 0;i < nbCache;i++) { |
| 5550 | if (oldNs[i] == node->ns) { |
| 5551 | node->ns = newNs[i]; |
| 5552 | break; |
| 5553 | } |
| 5554 | } |
| 5555 | if (i == nbCache) { |
| 5556 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5557 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5558 | */ |
| 5559 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 5560 | if (n != NULL) { /* :-( what if else ??? */ |
| 5561 | /* |
| 5562 | * check if we need to grow the cache buffers. |
| 5563 | */ |
| 5564 | if (sizeCache <= nbCache) { |
| 5565 | sizeCache *= 2; |
| 5566 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5567 | sizeof(xmlNsPtr)); |
| 5568 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5569 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5570 | xmlFree(newNs); |
| 5571 | return(-1); |
| 5572 | } |
| 5573 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5574 | sizeof(xmlNsPtr)); |
| 5575 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5576 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5577 | xmlFree(oldNs); |
| 5578 | return(-1); |
| 5579 | } |
| 5580 | } |
| 5581 | newNs[nbCache] = n; |
| 5582 | oldNs[nbCache++] = node->ns; |
| 5583 | node->ns = n; |
| 5584 | } |
| 5585 | } |
| 5586 | } |
| 5587 | /* |
| 5588 | * now check for namespace hold by attributes on the node. |
| 5589 | */ |
| 5590 | attr = node->properties; |
| 5591 | while (attr != NULL) { |
| 5592 | if (attr->ns != NULL) { |
| 5593 | /* |
| 5594 | * initialize the cache if needed |
| 5595 | */ |
| 5596 | if (sizeCache == 0) { |
| 5597 | sizeCache = 10; |
| 5598 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5599 | sizeof(xmlNsPtr)); |
| 5600 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5601 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5602 | return(-1); |
| 5603 | } |
| 5604 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5605 | sizeof(xmlNsPtr)); |
| 5606 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5607 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5608 | xmlFree(oldNs); |
| 5609 | return(-1); |
| 5610 | } |
| 5611 | } |
| 5612 | for (i = 0;i < nbCache;i++) { |
| 5613 | if (oldNs[i] == attr->ns) { |
Daniel Veillard | ce66ce1 | 2002-10-28 19:01:59 +0000 | [diff] [blame] | 5614 | attr->ns = newNs[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5615 | break; |
| 5616 | } |
| 5617 | } |
| 5618 | if (i == nbCache) { |
| 5619 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5620 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5621 | */ |
| 5622 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 5623 | if (n != NULL) { /* :-( what if else ??? */ |
| 5624 | /* |
| 5625 | * check if we need to grow the cache buffers. |
| 5626 | */ |
| 5627 | if (sizeCache <= nbCache) { |
| 5628 | sizeCache *= 2; |
| 5629 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5630 | sizeof(xmlNsPtr)); |
| 5631 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5632 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5633 | xmlFree(newNs); |
| 5634 | return(-1); |
| 5635 | } |
| 5636 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5637 | sizeof(xmlNsPtr)); |
| 5638 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5639 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5640 | xmlFree(oldNs); |
| 5641 | return(-1); |
| 5642 | } |
| 5643 | } |
| 5644 | newNs[nbCache] = n; |
| 5645 | oldNs[nbCache++] = attr->ns; |
| 5646 | attr->ns = n; |
| 5647 | } |
| 5648 | } |
| 5649 | } |
| 5650 | attr = attr->next; |
| 5651 | } |
| 5652 | |
| 5653 | /* |
| 5654 | * Browse the full subtree, deep first |
| 5655 | */ |
| 5656 | if (node->children != NULL) { |
| 5657 | /* deep first */ |
| 5658 | node = node->children; |
| 5659 | } else if ((node != tree) && (node->next != NULL)) { |
| 5660 | /* then siblings */ |
| 5661 | node = node->next; |
| 5662 | } else if (node != tree) { |
| 5663 | /* go up to parents->next if needed */ |
| 5664 | while (node != tree) { |
| 5665 | if (node->parent != NULL) |
| 5666 | node = node->parent; |
| 5667 | if ((node != tree) && (node->next != NULL)) { |
| 5668 | node = node->next; |
| 5669 | break; |
| 5670 | } |
| 5671 | if (node->parent == NULL) { |
| 5672 | node = NULL; |
| 5673 | break; |
| 5674 | } |
| 5675 | } |
| 5676 | /* exit condition */ |
| 5677 | if (node == tree) |
| 5678 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 5679 | } else |
| 5680 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5681 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5682 | if (oldNs != NULL) |
| 5683 | xmlFree(oldNs); |
| 5684 | if (newNs != NULL) |
| 5685 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5686 | return(ret); |
| 5687 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5688 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5689 | |
| 5690 | /** |
| 5691 | * xmlHasProp: |
| 5692 | * @node: the node |
| 5693 | * @name: the attribute name |
| 5694 | * |
| 5695 | * Search an attribute associated to a node |
| 5696 | * This function also looks in DTD attribute declaration for #FIXED or |
| 5697 | * default declaration values unless DTD use has been turned off. |
| 5698 | * |
| 5699 | * Returns the attribute or the attribute declaration or NULL if |
| 5700 | * neither was found. |
| 5701 | */ |
| 5702 | xmlAttrPtr |
| 5703 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 5704 | xmlAttrPtr prop; |
| 5705 | xmlDocPtr doc; |
| 5706 | |
| 5707 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 5708 | /* |
| 5709 | * Check on the properties attached to the node |
| 5710 | */ |
| 5711 | prop = node->properties; |
| 5712 | while (prop != NULL) { |
| 5713 | if (xmlStrEqual(prop->name, name)) { |
| 5714 | return(prop); |
| 5715 | } |
| 5716 | prop = prop->next; |
| 5717 | } |
| 5718 | if (!xmlCheckDTD) return(NULL); |
| 5719 | |
| 5720 | /* |
| 5721 | * Check if there is a default declaration in the internal |
| 5722 | * or external subsets |
| 5723 | */ |
| 5724 | doc = node->doc; |
| 5725 | if (doc != NULL) { |
| 5726 | xmlAttributePtr attrDecl; |
| 5727 | if (doc->intSubset != NULL) { |
| 5728 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5729 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5730 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5731 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5732 | /* return attribute declaration only if a default value is given |
| 5733 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5734 | return((xmlAttrPtr) attrDecl); |
| 5735 | } |
| 5736 | } |
| 5737 | return(NULL); |
| 5738 | } |
| 5739 | |
| 5740 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5741 | * xmlHasNsProp: |
| 5742 | * @node: the node |
| 5743 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5744 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5745 | * |
| 5746 | * Search for an attribute associated to a node |
| 5747 | * This attribute has to be anchored in the namespace specified. |
| 5748 | * This does the entity substitution. |
| 5749 | * This function looks in DTD attribute declaration for #FIXED or |
| 5750 | * default declaration values unless DTD use has been turned off. |
| 5751 | * |
| 5752 | * Returns the attribute or the attribute declaration or NULL |
| 5753 | * if neither was found. |
| 5754 | */ |
| 5755 | xmlAttrPtr |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5756 | xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5757 | xmlAttrPtr prop; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5758 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5759 | xmlDocPtr doc; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5760 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5761 | |
| 5762 | if (node == NULL) |
| 5763 | return(NULL); |
| 5764 | |
| 5765 | prop = node->properties; |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5766 | if (nameSpace == NULL) |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5767 | return(xmlHasProp(node, name)); |
| 5768 | while (prop != NULL) { |
| 5769 | /* |
| 5770 | * One need to have |
| 5771 | * - same attribute names |
| 5772 | * - and the attribute carrying that namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5773 | */ |
| 5774 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | e3c81b5 | 2001-06-17 14:50:34 +0000 | [diff] [blame] | 5775 | ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, nameSpace)))) { |
| 5776 | return(prop); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5777 | } |
| 5778 | prop = prop->next; |
| 5779 | } |
| 5780 | if (!xmlCheckDTD) return(NULL); |
| 5781 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5782 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5783 | /* |
| 5784 | * Check if there is a default declaration in the internal |
| 5785 | * or external subsets |
| 5786 | */ |
| 5787 | doc = node->doc; |
| 5788 | if (doc != NULL) { |
| 5789 | if (doc->intSubset != NULL) { |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5790 | xmlAttributePtr attrDecl = NULL; |
| 5791 | xmlNsPtr *nsList, *cur; |
| 5792 | xmlChar *ename; |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5793 | |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5794 | nsList = xmlGetNsList(node->doc, node); |
| 5795 | if (nsList == NULL) |
| 5796 | return(NULL); |
| 5797 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 5798 | ename = xmlStrdup(node->ns->prefix); |
| 5799 | ename = xmlStrcat(ename, BAD_CAST ":"); |
| 5800 | ename = xmlStrcat(ename, node->name); |
| 5801 | } else { |
| 5802 | ename = xmlStrdup(node->name); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5803 | } |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5804 | if (ename == NULL) { |
| 5805 | xmlFree(nsList); |
| 5806 | return(NULL); |
| 5807 | } |
| 5808 | |
| 5809 | cur = nsList; |
| 5810 | while (*cur != NULL) { |
| 5811 | if (xmlStrEqual((*cur)->href, nameSpace)) { |
| 5812 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename, |
| 5813 | name, (*cur)->prefix); |
| 5814 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5815 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename, |
| 5816 | name, (*cur)->prefix); |
| 5817 | } |
| 5818 | cur++; |
| 5819 | } |
| 5820 | xmlFree(nsList); |
| 5821 | xmlFree(ename); |
| 5822 | return((xmlAttrPtr) attrDecl); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5823 | } |
| 5824 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5825 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5826 | return(NULL); |
| 5827 | } |
| 5828 | |
| 5829 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5830 | * xmlGetProp: |
| 5831 | * @node: the node |
| 5832 | * @name: the attribute name |
| 5833 | * |
| 5834 | * Search and get the value of an attribute associated to a node |
| 5835 | * This does the entity substitution. |
| 5836 | * This function looks in DTD attribute declaration for #FIXED or |
| 5837 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 5838 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5839 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 5840 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5841 | * |
| 5842 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5843 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5844 | */ |
| 5845 | xmlChar * |
| 5846 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
| 5847 | xmlAttrPtr prop; |
| 5848 | xmlDocPtr doc; |
| 5849 | |
| 5850 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 5851 | /* |
| 5852 | * Check on the properties attached to the node |
| 5853 | */ |
| 5854 | prop = node->properties; |
| 5855 | while (prop != NULL) { |
| 5856 | if (xmlStrEqual(prop->name, name)) { |
| 5857 | xmlChar *ret; |
| 5858 | |
| 5859 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 5860 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 5861 | return(ret); |
| 5862 | } |
| 5863 | prop = prop->next; |
| 5864 | } |
| 5865 | if (!xmlCheckDTD) return(NULL); |
| 5866 | |
| 5867 | /* |
| 5868 | * Check if there is a default declaration in the internal |
| 5869 | * or external subsets |
| 5870 | */ |
| 5871 | doc = node->doc; |
| 5872 | if (doc != NULL) { |
| 5873 | xmlAttributePtr attrDecl; |
| 5874 | if (doc->intSubset != NULL) { |
| 5875 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5876 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5877 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5878 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5879 | /* return attribute declaration only if a default value is given |
| 5880 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5881 | return(xmlStrdup(attrDecl->defaultValue)); |
| 5882 | } |
| 5883 | } |
| 5884 | return(NULL); |
| 5885 | } |
| 5886 | |
| 5887 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5888 | * xmlGetNoNsProp: |
| 5889 | * @node: the node |
| 5890 | * @name: the attribute name |
| 5891 | * |
| 5892 | * Search and get the value of an attribute associated to a node |
| 5893 | * This does the entity substitution. |
| 5894 | * This function looks in DTD attribute declaration for #FIXED or |
| 5895 | * default declaration values unless DTD use has been turned off. |
| 5896 | * This function is similar to xmlGetProp except it will accept only |
| 5897 | * an attribute in no namespace. |
| 5898 | * |
| 5899 | * Returns the attribute value or NULL if not found. |
| 5900 | * It's up to the caller to free the memory with xmlFree(). |
| 5901 | */ |
| 5902 | xmlChar * |
| 5903 | xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) { |
| 5904 | xmlAttrPtr prop; |
| 5905 | xmlDocPtr doc; |
| 5906 | |
| 5907 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 5908 | /* |
| 5909 | * Check on the properties attached to the node |
| 5910 | */ |
| 5911 | prop = node->properties; |
| 5912 | while (prop != NULL) { |
| 5913 | if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) { |
| 5914 | xmlChar *ret; |
| 5915 | |
| 5916 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 5917 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 5918 | return(ret); |
| 5919 | } |
| 5920 | prop = prop->next; |
| 5921 | } |
| 5922 | if (!xmlCheckDTD) return(NULL); |
| 5923 | |
| 5924 | /* |
| 5925 | * Check if there is a default declaration in the internal |
| 5926 | * or external subsets |
| 5927 | */ |
| 5928 | doc = node->doc; |
| 5929 | if (doc != NULL) { |
| 5930 | xmlAttributePtr attrDecl; |
| 5931 | if (doc->intSubset != NULL) { |
| 5932 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5933 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5934 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5935 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5936 | /* return attribute declaration only if a default value is given |
| 5937 | (that includes #FIXED declarations) */ |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5938 | return(xmlStrdup(attrDecl->defaultValue)); |
| 5939 | } |
| 5940 | } |
| 5941 | return(NULL); |
| 5942 | } |
| 5943 | |
| 5944 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5945 | * xmlGetNsProp: |
| 5946 | * @node: the node |
| 5947 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5948 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5949 | * |
| 5950 | * Search and get the value of an attribute associated to a node |
| 5951 | * This attribute has to be anchored in the namespace specified. |
| 5952 | * This does the entity substitution. |
| 5953 | * This function looks in DTD attribute declaration for #FIXED or |
| 5954 | * default declaration values unless DTD use has been turned off. |
| 5955 | * |
| 5956 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5957 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5958 | */ |
| 5959 | xmlChar * |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5960 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5961 | xmlAttrPtr prop; |
| 5962 | xmlDocPtr doc; |
| 5963 | xmlNsPtr ns; |
| 5964 | |
| 5965 | if (node == NULL) |
| 5966 | return(NULL); |
| 5967 | |
| 5968 | prop = node->properties; |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5969 | if (nameSpace == NULL) |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5970 | return(xmlGetNoNsProp(node, name)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5971 | while (prop != NULL) { |
| 5972 | /* |
| 5973 | * One need to have |
| 5974 | * - same attribute names |
| 5975 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5976 | */ |
| 5977 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | e8fc08e | 2001-06-07 19:35:47 +0000 | [diff] [blame] | 5978 | ((prop->ns != NULL) && |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5979 | (xmlStrEqual(prop->ns->href, nameSpace)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5980 | xmlChar *ret; |
| 5981 | |
| 5982 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 5983 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 5984 | return(ret); |
| 5985 | } |
| 5986 | prop = prop->next; |
| 5987 | } |
| 5988 | if (!xmlCheckDTD) return(NULL); |
| 5989 | |
| 5990 | /* |
| 5991 | * Check if there is a default declaration in the internal |
| 5992 | * or external subsets |
| 5993 | */ |
| 5994 | doc = node->doc; |
| 5995 | if (doc != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5996 | if (doc->intSubset != NULL) { |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 5997 | xmlAttributePtr attrDecl; |
| 5998 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5999 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6000 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6001 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 6002 | |
| 6003 | if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) { |
| 6004 | /* |
| 6005 | * The DTD declaration only allows a prefix search |
| 6006 | */ |
| 6007 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6008 | if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6009 | return(xmlStrdup(attrDecl->defaultValue)); |
| 6010 | } |
| 6011 | } |
| 6012 | } |
| 6013 | return(NULL); |
| 6014 | } |
| 6015 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6016 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6017 | /** |
| 6018 | * xmlSetProp: |
| 6019 | * @node: the node |
| 6020 | * @name: the attribute name |
| 6021 | * @value: the attribute value |
| 6022 | * |
| 6023 | * Set (or reset) an attribute carried by a node. |
| 6024 | * Returns the attribute pointer. |
| 6025 | */ |
| 6026 | xmlAttrPtr |
| 6027 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6028 | xmlAttrPtr prop; |
| 6029 | xmlDocPtr doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6030 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6031 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6032 | return(NULL); |
| 6033 | doc = node->doc; |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6034 | prop = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6035 | while (prop != NULL) { |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6036 | if ((xmlStrEqual(prop->name, name)) && |
| 6037 | (prop->ns == NULL)){ |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6038 | xmlNodePtr oldprop = prop->children; |
| 6039 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6040 | prop->children = NULL; |
| 6041 | prop->last = NULL; |
| 6042 | if (value != NULL) { |
| 6043 | xmlChar *buffer; |
| 6044 | xmlNodePtr tmp; |
| 6045 | |
| 6046 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6047 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 6048 | prop->last = NULL; |
| 6049 | prop->doc = doc; |
| 6050 | tmp = prop->children; |
| 6051 | while (tmp != NULL) { |
| 6052 | tmp->parent = (xmlNodePtr) prop; |
| 6053 | tmp->doc = doc; |
| 6054 | if (tmp->next == NULL) |
| 6055 | prop->last = tmp; |
| 6056 | tmp = tmp->next; |
| 6057 | } |
| 6058 | xmlFree(buffer); |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6059 | } |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6060 | if (oldprop != NULL) |
| 6061 | xmlFreeNodeList(oldprop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6062 | return(prop); |
| 6063 | } |
| 6064 | prop = prop->next; |
| 6065 | } |
| 6066 | prop = xmlNewProp(node, name, value); |
| 6067 | return(prop); |
| 6068 | } |
| 6069 | |
| 6070 | /** |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6071 | * xmlUnsetProp: |
| 6072 | * @node: the node |
| 6073 | * @name: the attribute name |
| 6074 | * |
| 6075 | * Remove an attribute carried by a node. |
| 6076 | * Returns 0 if successful, -1 if not found |
| 6077 | */ |
| 6078 | int |
| 6079 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 6080 | xmlAttrPtr prop, prev = NULL;; |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6081 | |
| 6082 | if ((node == NULL) || (name == NULL)) |
| 6083 | return(-1); |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 6084 | prop = node->properties; |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6085 | while (prop != NULL) { |
| 6086 | if ((xmlStrEqual(prop->name, name)) && |
| 6087 | (prop->ns == NULL)) { |
| 6088 | if (prev == NULL) |
| 6089 | node->properties = prop->next; |
| 6090 | else |
| 6091 | prev->next = prop->next; |
| 6092 | xmlFreeProp(prop); |
| 6093 | return(0); |
| 6094 | } |
| 6095 | prev = prop; |
| 6096 | prop = prop->next; |
| 6097 | } |
| 6098 | return(-1); |
| 6099 | } |
| 6100 | |
| 6101 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6102 | * xmlSetNsProp: |
| 6103 | * @node: the node |
| 6104 | * @ns: the namespace definition |
| 6105 | * @name: the attribute name |
| 6106 | * @value: the attribute value |
| 6107 | * |
| 6108 | * Set (or reset) an attribute carried by a node. |
| 6109 | * The ns structure must be in scope, this is not checked. |
| 6110 | * |
| 6111 | * Returns the attribute pointer. |
| 6112 | */ |
| 6113 | xmlAttrPtr |
| 6114 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 6115 | const xmlChar *value) { |
| 6116 | xmlAttrPtr prop; |
| 6117 | |
| 6118 | if ((node == NULL) || (name == NULL)) |
| 6119 | return(NULL); |
| 6120 | |
| 6121 | if (ns == NULL) |
| 6122 | return(xmlSetProp(node, name, value)); |
| 6123 | if (ns->href == NULL) |
| 6124 | return(NULL); |
| 6125 | prop = node->properties; |
| 6126 | |
| 6127 | while (prop != NULL) { |
| 6128 | /* |
| 6129 | * One need to have |
| 6130 | * - same attribute names |
| 6131 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6132 | */ |
| 6133 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | a57c26e | 2002-08-01 12:52:24 +0000 | [diff] [blame] | 6134 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6135 | if (prop->children != NULL) |
| 6136 | xmlFreeNodeList(prop->children); |
| 6137 | prop->children = NULL; |
| 6138 | prop->last = NULL; |
| 6139 | prop->ns = ns; |
| 6140 | if (value != NULL) { |
| 6141 | xmlChar *buffer; |
| 6142 | xmlNodePtr tmp; |
| 6143 | |
| 6144 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6145 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 6146 | prop->last = NULL; |
| 6147 | tmp = prop->children; |
| 6148 | while (tmp != NULL) { |
| 6149 | tmp->parent = (xmlNodePtr) prop; |
| 6150 | if (tmp->next == NULL) |
| 6151 | prop->last = tmp; |
| 6152 | tmp = tmp->next; |
| 6153 | } |
| 6154 | xmlFree(buffer); |
| 6155 | } |
| 6156 | return(prop); |
| 6157 | } |
| 6158 | prop = prop->next; |
| 6159 | } |
| 6160 | prop = xmlNewNsProp(node, ns, name, value); |
| 6161 | return(prop); |
| 6162 | } |
| 6163 | |
| 6164 | /** |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6165 | * xmlUnsetNsProp: |
| 6166 | * @node: the node |
| 6167 | * @ns: the namespace definition |
| 6168 | * @name: the attribute name |
| 6169 | * |
| 6170 | * Remove an attribute carried by a node. |
| 6171 | * Returns 0 if successful, -1 if not found |
| 6172 | */ |
| 6173 | int |
| 6174 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
| 6175 | xmlAttrPtr prop = node->properties, prev = NULL;; |
| 6176 | |
| 6177 | if ((node == NULL) || (name == NULL)) |
| 6178 | return(-1); |
| 6179 | if (ns == NULL) |
| 6180 | return(xmlUnsetProp(node, name)); |
| 6181 | if (ns->href == NULL) |
| 6182 | return(-1); |
| 6183 | while (prop != NULL) { |
| 6184 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | 0bf2900 | 2002-08-01 12:54:11 +0000 | [diff] [blame] | 6185 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6186 | if (prev == NULL) |
| 6187 | node->properties = prop->next; |
| 6188 | else |
| 6189 | prev->next = prop->next; |
| 6190 | xmlFreeProp(prop); |
| 6191 | return(0); |
| 6192 | } |
| 6193 | prev = prop; |
| 6194 | prop = prop->next; |
| 6195 | } |
| 6196 | return(-1); |
| 6197 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6198 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6199 | |
| 6200 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6201 | * xmlNodeIsText: |
| 6202 | * @node: the node |
| 6203 | * |
| 6204 | * Is this node a Text node ? |
| 6205 | * Returns 1 yes, 0 no |
| 6206 | */ |
| 6207 | int |
| 6208 | xmlNodeIsText(xmlNodePtr node) { |
| 6209 | if (node == NULL) return(0); |
| 6210 | |
| 6211 | if (node->type == XML_TEXT_NODE) return(1); |
| 6212 | return(0); |
| 6213 | } |
| 6214 | |
| 6215 | /** |
| 6216 | * xmlIsBlankNode: |
| 6217 | * @node: the node |
| 6218 | * |
| 6219 | * Checks whether this node is an empty or whitespace only |
| 6220 | * (and possibly ignorable) text-node. |
| 6221 | * |
| 6222 | * Returns 1 yes, 0 no |
| 6223 | */ |
| 6224 | int |
| 6225 | xmlIsBlankNode(xmlNodePtr node) { |
| 6226 | const xmlChar *cur; |
| 6227 | if (node == NULL) return(0); |
| 6228 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6229 | if ((node->type != XML_TEXT_NODE) && |
| 6230 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6231 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6232 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6233 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6234 | while (*cur != 0) { |
| 6235 | if (!IS_BLANK(*cur)) return(0); |
| 6236 | cur++; |
| 6237 | } |
| 6238 | |
| 6239 | return(1); |
| 6240 | } |
| 6241 | |
| 6242 | /** |
| 6243 | * xmlTextConcat: |
| 6244 | * @node: the node |
| 6245 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6246 | * @len: @content length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6247 | * |
| 6248 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6249 | * |
| 6250 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6251 | */ |
| 6252 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6253 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6254 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6255 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6256 | |
| 6257 | if ((node->type != XML_TEXT_NODE) && |
| 6258 | (node->type != XML_CDATA_SECTION_NODE)) { |
| 6259 | #ifdef DEBUG_TREE |
| 6260 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6261 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6262 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6263 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6264 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6265 | node->content = xmlStrncat(node->content, content, len); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6266 | if (node->content == NULL) |
| 6267 | return(-1); |
| 6268 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6269 | } |
| 6270 | |
| 6271 | /************************************************************************ |
| 6272 | * * |
| 6273 | * Output : to a FILE or in memory * |
| 6274 | * * |
| 6275 | ************************************************************************/ |
| 6276 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6277 | /** |
| 6278 | * xmlBufferCreate: |
| 6279 | * |
| 6280 | * routine to create an XML buffer. |
| 6281 | * returns the new structure. |
| 6282 | */ |
| 6283 | xmlBufferPtr |
| 6284 | xmlBufferCreate(void) { |
| 6285 | xmlBufferPtr ret; |
| 6286 | |
| 6287 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6288 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6289 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6290 | return(NULL); |
| 6291 | } |
| 6292 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 6293 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6294 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6295 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6296 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6297 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6298 | xmlFree(ret); |
| 6299 | return(NULL); |
| 6300 | } |
| 6301 | ret->content[0] = 0; |
| 6302 | return(ret); |
| 6303 | } |
| 6304 | |
| 6305 | /** |
| 6306 | * xmlBufferCreateSize: |
| 6307 | * @size: initial size of buffer |
| 6308 | * |
| 6309 | * routine to create an XML buffer. |
| 6310 | * returns the new structure. |
| 6311 | */ |
| 6312 | xmlBufferPtr |
| 6313 | xmlBufferCreateSize(size_t size) { |
| 6314 | xmlBufferPtr ret; |
| 6315 | |
| 6316 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6317 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6318 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6319 | return(NULL); |
| 6320 | } |
| 6321 | ret->use = 0; |
| 6322 | ret->alloc = xmlBufferAllocScheme; |
| 6323 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 6324 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6325 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6326 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6327 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6328 | xmlFree(ret); |
| 6329 | return(NULL); |
| 6330 | } |
| 6331 | ret->content[0] = 0; |
| 6332 | } else |
| 6333 | ret->content = NULL; |
| 6334 | return(ret); |
| 6335 | } |
| 6336 | |
| 6337 | /** |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6338 | * xmlBufferCreateStatic: |
| 6339 | * @mem: the memory area |
| 6340 | * @size: the size in byte |
| 6341 | * |
| 6342 | * routine to create an XML buffer from an immutable memory area, |
| 6343 | * The are won't be modified nor copied, and is expected to be |
| 6344 | * present until the end of the buffer lifetime. |
| 6345 | * |
| 6346 | * returns the new structure. |
| 6347 | */ |
| 6348 | xmlBufferPtr |
| 6349 | xmlBufferCreateStatic(void *mem, size_t size) { |
| 6350 | xmlBufferPtr ret; |
| 6351 | |
| 6352 | if ((mem == NULL) || (size == 0)) |
| 6353 | return(NULL); |
| 6354 | |
| 6355 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6356 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6357 | xmlTreeErrMemory("creating buffer"); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6358 | return(NULL); |
| 6359 | } |
| 6360 | ret->use = size; |
| 6361 | ret->size = size; |
| 6362 | ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; |
| 6363 | ret->content = (xmlChar *) mem; |
| 6364 | return(ret); |
| 6365 | } |
| 6366 | |
| 6367 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6368 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6369 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6370 | * @scheme: allocation scheme to use |
| 6371 | * |
| 6372 | * Sets the allocation scheme for this buffer |
| 6373 | */ |
| 6374 | void |
| 6375 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 6376 | xmlBufferAllocationScheme scheme) { |
| 6377 | if (buf == NULL) { |
| 6378 | #ifdef DEBUG_BUFFER |
| 6379 | xmlGenericError(xmlGenericErrorContext, |
| 6380 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 6381 | #endif |
| 6382 | return; |
| 6383 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6384 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6385 | |
| 6386 | buf->alloc = scheme; |
| 6387 | } |
| 6388 | |
| 6389 | /** |
| 6390 | * xmlBufferFree: |
| 6391 | * @buf: the buffer to free |
| 6392 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 6393 | * Frees an XML buffer. It frees both the content and the structure which |
| 6394 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6395 | */ |
| 6396 | void |
| 6397 | xmlBufferFree(xmlBufferPtr buf) { |
| 6398 | if (buf == NULL) { |
| 6399 | #ifdef DEBUG_BUFFER |
| 6400 | xmlGenericError(xmlGenericErrorContext, |
| 6401 | "xmlBufferFree: buf == NULL\n"); |
| 6402 | #endif |
| 6403 | return; |
| 6404 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6405 | |
| 6406 | if ((buf->content != NULL) && |
| 6407 | (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6408 | xmlFree(buf->content); |
| 6409 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6410 | xmlFree(buf); |
| 6411 | } |
| 6412 | |
| 6413 | /** |
| 6414 | * xmlBufferEmpty: |
| 6415 | * @buf: the buffer |
| 6416 | * |
| 6417 | * empty a buffer. |
| 6418 | */ |
| 6419 | void |
| 6420 | xmlBufferEmpty(xmlBufferPtr buf) { |
| 6421 | if (buf->content == NULL) return; |
| 6422 | buf->use = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6423 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 6424 | buf->content = BAD_CAST ""; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6425 | } else { |
| 6426 | memset(buf->content, 0, buf->size); |
| 6427 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6428 | } |
| 6429 | |
| 6430 | /** |
| 6431 | * xmlBufferShrink: |
| 6432 | * @buf: the buffer to dump |
| 6433 | * @len: the number of xmlChar to remove |
| 6434 | * |
| 6435 | * Remove the beginning of an XML buffer. |
| 6436 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6437 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6438 | */ |
| 6439 | int |
| 6440 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
| 6441 | if (len == 0) return(0); |
| 6442 | if (len > buf->use) return(-1); |
| 6443 | |
| 6444 | buf->use -= len; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6445 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
| 6446 | buf->content += len; |
| 6447 | } else { |
| 6448 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
| 6449 | buf->content[buf->use] = 0; |
| 6450 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6451 | return(len); |
| 6452 | } |
| 6453 | |
| 6454 | /** |
| 6455 | * xmlBufferGrow: |
| 6456 | * @buf: the buffer |
| 6457 | * @len: the minimum free size to allocate |
| 6458 | * |
| 6459 | * Grow the available space of an XML buffer. |
| 6460 | * |
| 6461 | * Returns the new available space or -1 in case of error |
| 6462 | */ |
| 6463 | int |
| 6464 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 6465 | int size; |
| 6466 | xmlChar *newbuf; |
| 6467 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6468 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6469 | if (len + buf->use < buf->size) return(0); |
| 6470 | |
| 6471 | size = buf->use + len + 100; |
| 6472 | |
| 6473 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6474 | if (newbuf == NULL) { |
| 6475 | xmlTreeErrMemory("growing buffer"); |
| 6476 | return(-1); |
| 6477 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6478 | buf->content = newbuf; |
| 6479 | buf->size = size; |
| 6480 | return(buf->size - buf->use); |
| 6481 | } |
| 6482 | |
| 6483 | /** |
| 6484 | * xmlBufferDump: |
| 6485 | * @file: the file output |
| 6486 | * @buf: the buffer to dump |
| 6487 | * |
| 6488 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6489 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6490 | */ |
| 6491 | int |
| 6492 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 6493 | int ret; |
| 6494 | |
| 6495 | if (buf == NULL) { |
| 6496 | #ifdef DEBUG_BUFFER |
| 6497 | xmlGenericError(xmlGenericErrorContext, |
| 6498 | "xmlBufferDump: buf == NULL\n"); |
| 6499 | #endif |
| 6500 | return(0); |
| 6501 | } |
| 6502 | if (buf->content == NULL) { |
| 6503 | #ifdef DEBUG_BUFFER |
| 6504 | xmlGenericError(xmlGenericErrorContext, |
| 6505 | "xmlBufferDump: buf->content == NULL\n"); |
| 6506 | #endif |
| 6507 | return(0); |
| 6508 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 6509 | if (file == NULL) |
| 6510 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6511 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 6512 | return(ret); |
| 6513 | } |
| 6514 | |
| 6515 | /** |
| 6516 | * xmlBufferContent: |
| 6517 | * @buf: the buffer |
| 6518 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6519 | * Function to extract the content of a buffer |
| 6520 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6521 | * Returns the internal content |
| 6522 | */ |
| 6523 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6524 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6525 | xmlBufferContent(const xmlBufferPtr buf) |
| 6526 | { |
| 6527 | if(!buf) |
| 6528 | return NULL; |
| 6529 | |
| 6530 | return buf->content; |
| 6531 | } |
| 6532 | |
| 6533 | /** |
| 6534 | * xmlBufferLength: |
| 6535 | * @buf: the buffer |
| 6536 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6537 | * Function to get the length of a buffer |
| 6538 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6539 | * Returns the length of data in the internal content |
| 6540 | */ |
| 6541 | |
| 6542 | int |
| 6543 | xmlBufferLength(const xmlBufferPtr buf) |
| 6544 | { |
| 6545 | if(!buf) |
| 6546 | return 0; |
| 6547 | |
| 6548 | return buf->use; |
| 6549 | } |
| 6550 | |
| 6551 | /** |
| 6552 | * xmlBufferResize: |
| 6553 | * @buf: the buffer to resize |
| 6554 | * @size: the desired size |
| 6555 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6556 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6557 | * |
| 6558 | * Returns 0 in case of problems, 1 otherwise |
| 6559 | */ |
| 6560 | int |
| 6561 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 6562 | { |
| 6563 | unsigned int newSize; |
| 6564 | xmlChar* rebuf = NULL; |
| 6565 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6566 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
| 6567 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6568 | /*take care of empty case*/ |
| 6569 | newSize = (buf->size ? buf->size*2 : size); |
| 6570 | |
| 6571 | /* Don't resize if we don't have to */ |
| 6572 | if (size < buf->size) |
| 6573 | return 1; |
| 6574 | |
| 6575 | /* figure out new size */ |
| 6576 | switch (buf->alloc){ |
| 6577 | case XML_BUFFER_ALLOC_DOUBLEIT: |
| 6578 | while (size > newSize) newSize *= 2; |
| 6579 | break; |
| 6580 | case XML_BUFFER_ALLOC_EXACT: |
| 6581 | newSize = size+10; |
| 6582 | break; |
| 6583 | default: |
| 6584 | newSize = size+10; |
| 6585 | break; |
| 6586 | } |
| 6587 | |
| 6588 | if (buf->content == NULL) |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6589 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6590 | else if (buf->size - buf->use < 100) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6591 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 6592 | newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6593 | } else { |
| 6594 | /* |
| 6595 | * if we are reallocating a buffer far from being full, it's |
| 6596 | * better to make a new allocation and copy only the used range |
| 6597 | * and free the old one. |
| 6598 | */ |
| 6599 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
| 6600 | if (rebuf != NULL) { |
| 6601 | memcpy(rebuf, buf->content, buf->use); |
| 6602 | xmlFree(buf->content); |
| 6603 | } |
Daniel Veillard | e598408 | 2003-08-19 22:21:13 +0000 | [diff] [blame] | 6604 | rebuf[buf->use] = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6605 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6606 | if (rebuf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6607 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6608 | return 0; |
| 6609 | } |
| 6610 | buf->content = rebuf; |
| 6611 | buf->size = newSize; |
| 6612 | |
| 6613 | return 1; |
| 6614 | } |
| 6615 | |
| 6616 | /** |
| 6617 | * xmlBufferAdd: |
| 6618 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6619 | * @str: the #xmlChar string |
| 6620 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6621 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6622 | * 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] | 6623 | * str is recomputed. |
| 6624 | */ |
| 6625 | void |
| 6626 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6627 | unsigned int needSize; |
| 6628 | |
| 6629 | if (str == NULL) { |
| 6630 | #ifdef DEBUG_BUFFER |
| 6631 | xmlGenericError(xmlGenericErrorContext, |
| 6632 | "xmlBufferAdd: str == NULL\n"); |
| 6633 | #endif |
| 6634 | return; |
| 6635 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6636 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6637 | if (len < -1) { |
| 6638 | #ifdef DEBUG_BUFFER |
| 6639 | xmlGenericError(xmlGenericErrorContext, |
| 6640 | "xmlBufferAdd: len < 0\n"); |
| 6641 | #endif |
| 6642 | return; |
| 6643 | } |
| 6644 | if (len == 0) return; |
| 6645 | |
| 6646 | if (len < 0) |
| 6647 | len = xmlStrlen(str); |
| 6648 | |
| 6649 | if (len <= 0) return; |
| 6650 | |
| 6651 | needSize = buf->use + len + 2; |
| 6652 | if (needSize > buf->size){ |
| 6653 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6654 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6655 | return; |
| 6656 | } |
| 6657 | } |
| 6658 | |
| 6659 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 6660 | buf->use += len; |
| 6661 | buf->content[buf->use] = 0; |
| 6662 | } |
| 6663 | |
| 6664 | /** |
| 6665 | * xmlBufferAddHead: |
| 6666 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6667 | * @str: the #xmlChar string |
| 6668 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6669 | * |
| 6670 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6671 | * if len == -1, the length of @str is recomputed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6672 | */ |
| 6673 | void |
| 6674 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6675 | unsigned int needSize; |
| 6676 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6677 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6678 | if (str == NULL) { |
| 6679 | #ifdef DEBUG_BUFFER |
| 6680 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6681 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6682 | #endif |
| 6683 | return; |
| 6684 | } |
| 6685 | if (len < -1) { |
| 6686 | #ifdef DEBUG_BUFFER |
| 6687 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6688 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6689 | #endif |
| 6690 | return; |
| 6691 | } |
| 6692 | if (len == 0) return; |
| 6693 | |
| 6694 | if (len < 0) |
| 6695 | len = xmlStrlen(str); |
| 6696 | |
| 6697 | if (len <= 0) return; |
| 6698 | |
| 6699 | needSize = buf->use + len + 2; |
| 6700 | if (needSize > buf->size){ |
| 6701 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6702 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6703 | return; |
| 6704 | } |
| 6705 | } |
| 6706 | |
| 6707 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 6708 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 6709 | buf->use += len; |
| 6710 | buf->content[buf->use] = 0; |
| 6711 | } |
| 6712 | |
| 6713 | /** |
| 6714 | * xmlBufferCat: |
| 6715 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6716 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6717 | * |
| 6718 | * Append a zero terminated string to an XML buffer. |
| 6719 | */ |
| 6720 | void |
| 6721 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6722 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6723 | if (str != NULL) |
| 6724 | xmlBufferAdd(buf, str, -1); |
| 6725 | } |
| 6726 | |
| 6727 | /** |
| 6728 | * xmlBufferCCat: |
| 6729 | * @buf: the buffer to dump |
| 6730 | * @str: the C char string |
| 6731 | * |
| 6732 | * Append a zero terminated C string to an XML buffer. |
| 6733 | */ |
| 6734 | void |
| 6735 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 6736 | const char *cur; |
| 6737 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6738 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6739 | if (str == NULL) { |
| 6740 | #ifdef DEBUG_BUFFER |
| 6741 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6742 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6743 | #endif |
| 6744 | return; |
| 6745 | } |
| 6746 | for (cur = str;*cur != 0;cur++) { |
| 6747 | if (buf->use + 10 >= buf->size) { |
| 6748 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6749 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6750 | return; |
| 6751 | } |
| 6752 | } |
| 6753 | buf->content[buf->use++] = *cur; |
| 6754 | } |
| 6755 | buf->content[buf->use] = 0; |
| 6756 | } |
| 6757 | |
| 6758 | /** |
| 6759 | * xmlBufferWriteCHAR: |
| 6760 | * @buf: the XML buffer |
| 6761 | * @string: the string to add |
| 6762 | * |
| 6763 | * routine which manages and grows an output buffer. This one adds |
| 6764 | * xmlChars at the end of the buffer. |
| 6765 | */ |
| 6766 | void |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6767 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
| 6768 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6769 | xmlBufferCat(buf, string); |
| 6770 | } |
| 6771 | |
| 6772 | /** |
| 6773 | * xmlBufferWriteChar: |
| 6774 | * @buf: the XML buffer output |
| 6775 | * @string: the string to add |
| 6776 | * |
| 6777 | * routine which manage and grows an output buffer. This one add |
| 6778 | * C chars at the end of the array. |
| 6779 | */ |
| 6780 | void |
| 6781 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6782 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6783 | xmlBufferCCat(buf, string); |
| 6784 | } |
| 6785 | |
| 6786 | |
| 6787 | /** |
| 6788 | * xmlBufferWriteQuotedString: |
| 6789 | * @buf: the XML buffer output |
| 6790 | * @string: the string to add |
| 6791 | * |
| 6792 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6793 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6794 | * quote or double-quotes internally |
| 6795 | */ |
| 6796 | void |
| 6797 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6798 | const xmlChar *cur, *base; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6799 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6800 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 6801 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6802 | #ifdef DEBUG_BUFFER |
| 6803 | xmlGenericError(xmlGenericErrorContext, |
| 6804 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 6805 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6806 | xmlBufferCCat(buf, "\""); |
| 6807 | base = cur = string; |
| 6808 | while(*cur != 0){ |
| 6809 | if(*cur == '"'){ |
| 6810 | if (base != cur) |
| 6811 | xmlBufferAdd(buf, base, cur - base); |
| 6812 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 6813 | cur++; |
| 6814 | base = cur; |
| 6815 | } |
| 6816 | else { |
| 6817 | cur++; |
| 6818 | } |
| 6819 | } |
| 6820 | if (base != cur) |
| 6821 | xmlBufferAdd(buf, base, cur - base); |
| 6822 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6823 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6824 | else{ |
| 6825 | xmlBufferCCat(buf, "\'"); |
| 6826 | xmlBufferCat(buf, string); |
| 6827 | xmlBufferCCat(buf, "\'"); |
| 6828 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6829 | } else { |
| 6830 | xmlBufferCCat(buf, "\""); |
| 6831 | xmlBufferCat(buf, string); |
| 6832 | xmlBufferCCat(buf, "\""); |
| 6833 | } |
| 6834 | } |
| 6835 | |
| 6836 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 6837 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6838 | /************************************************************************ |
| 6839 | * * |
Daniel Veillard | e2238d5 | 2003-10-09 13:14:55 +0000 | [diff] [blame] | 6840 | * Output error handlers * |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6841 | * * |
| 6842 | ************************************************************************/ |
| 6843 | /** |
| 6844 | * xmlSaveErrMemory: |
| 6845 | * @extra: extra informations |
| 6846 | * |
| 6847 | * Handle an out of memory condition |
| 6848 | */ |
| 6849 | static void |
| 6850 | xmlSaveErrMemory(const char *extra) |
| 6851 | { |
| 6852 | __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 6853 | } |
| 6854 | |
| 6855 | /** |
| 6856 | * xmlSaveErr: |
| 6857 | * @code: the error number |
| 6858 | * @node: the location of the error. |
| 6859 | * @extra: extra informations |
| 6860 | * |
| 6861 | * Handle an out of memory condition |
| 6862 | */ |
| 6863 | static void |
| 6864 | xmlSaveErr(int code, xmlNodePtr node, const char *extra) |
| 6865 | { |
| 6866 | const char *msg = NULL; |
| 6867 | |
| 6868 | switch(code) { |
| 6869 | case XML_SAVE_NOT_UTF8: |
| 6870 | msg = "string is not in UTF-8"; |
| 6871 | break; |
| 6872 | case XML_SAVE_CHAR_INVALID: |
| 6873 | msg = "invalid character value"; |
| 6874 | break; |
| 6875 | case XML_SAVE_UNKNOWN_ENCODING: |
| 6876 | msg = "unknown encoding %s"; |
| 6877 | break; |
Daniel Veillard | e2238d5 | 2003-10-09 13:14:55 +0000 | [diff] [blame] | 6878 | case XML_SAVE_NO_DOCTYPE: |
| 6879 | msg = "document has no DOCTYPE"; |
| 6880 | break; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6881 | default: |
| 6882 | msg = "unexpected error number"; |
| 6883 | } |
Daniel Veillard | e2238d5 | 2003-10-09 13:14:55 +0000 | [diff] [blame] | 6884 | __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6885 | } |
| 6886 | /************************************************************************ |
| 6887 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6888 | * Dumping XML tree content to a simple buffer * |
| 6889 | * * |
| 6890 | ************************************************************************/ |
| 6891 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6892 | /** |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6893 | * xmlAttrSerializeContent: |
| 6894 | * @buf: the XML buffer output |
| 6895 | * @doc: the document |
| 6896 | * @attr: the attribute pointer |
| 6897 | * |
| 6898 | * Serialize the attribute in the buffer |
| 6899 | */ |
| 6900 | static void |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6901 | xmlAttrSerializeContent(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr attr) |
| 6902 | { |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6903 | const xmlChar *cur, *base; |
| 6904 | xmlNodePtr children; |
| 6905 | |
| 6906 | children = attr->children; |
| 6907 | while (children != NULL) { |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6908 | switch (children->type) { |
| 6909 | case XML_TEXT_NODE: |
| 6910 | base = cur = children->content; |
| 6911 | while (*cur != 0) { |
| 6912 | if (*cur == '\n') { |
| 6913 | if (base != cur) |
| 6914 | xmlBufferAdd(buf, base, cur - base); |
| 6915 | xmlBufferAdd(buf, BAD_CAST " ", 5); |
| 6916 | cur++; |
| 6917 | base = cur; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 6918 | } else if (*cur == '\r') { |
| 6919 | if (base != cur) |
| 6920 | xmlBufferAdd(buf, base, cur - base); |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 6921 | xmlBufferAdd(buf, BAD_CAST " ", 5); |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 6922 | cur++; |
| 6923 | base = cur; |
| 6924 | } else if (*cur == '\t') { |
| 6925 | if (base != cur) |
| 6926 | xmlBufferAdd(buf, base, cur - base); |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 6927 | xmlBufferAdd(buf, BAD_CAST "	", 4); |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 6928 | cur++; |
| 6929 | base = cur; |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6930 | #if 0 |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6931 | } else if (*cur == '\'') { |
| 6932 | if (base != cur) |
| 6933 | xmlBufferAdd(buf, base, cur - base); |
| 6934 | xmlBufferAdd(buf, BAD_CAST "'", 6); |
| 6935 | cur++; |
| 6936 | base = cur; |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6937 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6938 | } else if (*cur == '"') { |
| 6939 | if (base != cur) |
| 6940 | xmlBufferAdd(buf, base, cur - base); |
| 6941 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 6942 | cur++; |
| 6943 | base = cur; |
| 6944 | } else if (*cur == '<') { |
| 6945 | if (base != cur) |
| 6946 | xmlBufferAdd(buf, base, cur - base); |
| 6947 | xmlBufferAdd(buf, BAD_CAST "<", 4); |
| 6948 | cur++; |
| 6949 | base = cur; |
| 6950 | } else if (*cur == '>') { |
| 6951 | if (base != cur) |
| 6952 | xmlBufferAdd(buf, base, cur - base); |
| 6953 | xmlBufferAdd(buf, BAD_CAST ">", 4); |
| 6954 | cur++; |
| 6955 | base = cur; |
| 6956 | } else if (*cur == '&') { |
| 6957 | if (base != cur) |
| 6958 | xmlBufferAdd(buf, base, cur - base); |
| 6959 | xmlBufferAdd(buf, BAD_CAST "&", 5); |
| 6960 | cur++; |
| 6961 | base = cur; |
| 6962 | } else if ((*cur >= 0x80) && ((doc == NULL) || |
| 6963 | (doc->encoding == |
| 6964 | NULL))) { |
| 6965 | /* |
| 6966 | * We assume we have UTF-8 content. |
| 6967 | */ |
| 6968 | char tmp[10]; |
| 6969 | int val = 0, l = 1; |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 6970 | |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6971 | if (base != cur) |
| 6972 | xmlBufferAdd(buf, base, cur - base); |
| 6973 | if (*cur < 0xC0) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6974 | xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, |
| 6975 | NULL); |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 6976 | if (doc != NULL) |
| 6977 | doc->encoding = |
| 6978 | xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 6979 | snprintf(tmp, sizeof(tmp), "&#%d;", *cur); |
| 6980 | tmp[sizeof(tmp) - 1] = 0; |
| 6981 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 6982 | cur++; |
| 6983 | base = cur; |
| 6984 | continue; |
| 6985 | } else if (*cur < 0xE0) { |
| 6986 | val = (cur[0]) & 0x1F; |
| 6987 | val <<= 6; |
| 6988 | val |= (cur[1]) & 0x3F; |
| 6989 | l = 2; |
| 6990 | } else if (*cur < 0xF0) { |
| 6991 | val = (cur[0]) & 0x0F; |
| 6992 | val <<= 6; |
| 6993 | val |= (cur[1]) & 0x3F; |
| 6994 | val <<= 6; |
| 6995 | val |= (cur[2]) & 0x3F; |
| 6996 | l = 3; |
| 6997 | } else if (*cur < 0xF8) { |
| 6998 | val = (cur[0]) & 0x07; |
| 6999 | val <<= 6; |
| 7000 | val |= (cur[1]) & 0x3F; |
| 7001 | val <<= 6; |
| 7002 | val |= (cur[2]) & 0x3F; |
| 7003 | val <<= 6; |
| 7004 | val |= (cur[3]) & 0x3F; |
| 7005 | l = 4; |
| 7006 | } |
| 7007 | if ((l == 1) || (!IS_CHAR(val))) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7008 | xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, |
| 7009 | NULL); |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7010 | if (doc != NULL) |
| 7011 | doc->encoding = |
| 7012 | xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 7013 | snprintf(tmp, sizeof(tmp), "&#%d;", *cur); |
| 7014 | tmp[sizeof(tmp) - 1] = 0; |
| 7015 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 7016 | cur++; |
| 7017 | base = cur; |
| 7018 | continue; |
| 7019 | } |
| 7020 | /* |
| 7021 | * We could do multiple things here. Just save |
| 7022 | * as a char ref |
| 7023 | */ |
| 7024 | snprintf(tmp, sizeof(tmp), "&#x%X;", val); |
| 7025 | tmp[sizeof(tmp) - 1] = 0; |
| 7026 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 7027 | cur += l; |
| 7028 | base = cur; |
| 7029 | } else { |
| 7030 | cur++; |
| 7031 | } |
| 7032 | } |
| 7033 | if (base != cur) |
| 7034 | xmlBufferAdd(buf, base, cur - base); |
| 7035 | break; |
| 7036 | case XML_ENTITY_REF_NODE: |
| 7037 | xmlBufferAdd(buf, BAD_CAST "&", 1); |
| 7038 | xmlBufferAdd(buf, children->name, |
| 7039 | xmlStrlen(children->name)); |
| 7040 | xmlBufferAdd(buf, BAD_CAST ";", 1); |
| 7041 | break; |
| 7042 | default: |
| 7043 | /* should not happen unless we have a badly built tree */ |
| 7044 | break; |
| 7045 | } |
| 7046 | children = children->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7047 | } |
| 7048 | } |
| 7049 | |
| 7050 | /** |
| 7051 | * xmlNodeDump: |
| 7052 | * @buf: the XML buffer output |
| 7053 | * @doc: the document |
| 7054 | * @cur: the current node |
| 7055 | * @level: the imbrication level for indenting |
| 7056 | * @format: is formatting allowed |
| 7057 | * |
| 7058 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7059 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7060 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7061 | * |
| 7062 | * 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] | 7063 | */ |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7064 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7065 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7066 | int format) |
| 7067 | { |
| 7068 | unsigned int use; |
| 7069 | int ret; |
| 7070 | xmlOutputBufferPtr outbuf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7071 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7072 | xmlInitParser(); |
| 7073 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7074 | if (cur == NULL) { |
| 7075 | #ifdef DEBUG_TREE |
| 7076 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7077 | "xmlNodeDump : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7078 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7079 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7080 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7081 | if (buf == NULL) { |
| 7082 | #ifdef DEBUG_TREE |
| 7083 | xmlGenericError(xmlGenericErrorContext, |
| 7084 | "xmlNodeDump : buf == NULL\n"); |
| 7085 | #endif |
| 7086 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7087 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7088 | outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); |
| 7089 | if (outbuf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7090 | xmlSaveErrMemory("creating buffer"); |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7091 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7092 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7093 | memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer)); |
| 7094 | outbuf->buffer = buf; |
| 7095 | outbuf->encoder = NULL; |
| 7096 | outbuf->writecallback = NULL; |
| 7097 | outbuf->closecallback = NULL; |
| 7098 | outbuf->context = NULL; |
| 7099 | outbuf->written = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7100 | |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7101 | use = buf->use; |
| 7102 | xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL); |
| 7103 | xmlFree(outbuf); |
| 7104 | ret = buf->use - use; |
| 7105 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7106 | } |
| 7107 | |
| 7108 | /** |
| 7109 | * xmlElemDump: |
| 7110 | * @f: the FILE * for the output |
| 7111 | * @doc: the document |
| 7112 | * @cur: the current node |
| 7113 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7114 | * Dump an XML/HTML node, recursive behaviour, children are printed too. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7115 | */ |
| 7116 | void |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7117 | xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur) |
| 7118 | { |
| 7119 | xmlOutputBufferPtr outbuf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7120 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7121 | xmlInitParser(); |
| 7122 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7123 | if (cur == NULL) { |
| 7124 | #ifdef DEBUG_TREE |
| 7125 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7126 | "xmlElemDump : cur == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7127 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7128 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7129 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7130 | #ifdef DEBUG_TREE |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7131 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7132 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7133 | "xmlElemDump : doc == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7134 | } |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7135 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7136 | |
| 7137 | outbuf = xmlOutputBufferCreateFile(f, NULL); |
| 7138 | if (outbuf == NULL) |
| 7139 | return; |
| 7140 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7141 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7142 | htmlNodeDumpOutput(outbuf, doc, cur, NULL); |
| 7143 | #else |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7144 | xmlSaveErr(XML_ERR_INTERNAL_ERROR, "HTML support not compiled in\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7145 | #endif /* LIBXML_HTML_ENABLED */ |
| 7146 | } else |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7147 | xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL); |
| 7148 | xmlOutputBufferClose(outbuf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7149 | } |
| 7150 | |
| 7151 | /************************************************************************ |
| 7152 | * * |
| 7153 | * Dumping XML tree content to an I/O output buffer * |
| 7154 | * * |
| 7155 | ************************************************************************/ |
| 7156 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 7157 | #ifdef LIBXML_HTML_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7158 | static void |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7159 | xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 7160 | int level, int format, const char *encoding); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 7161 | #endif |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7162 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7163 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 7164 | int level, int format, const char *encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7165 | static void |
| 7166 | xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7167 | xmlNodePtr cur, int level, int format, const char *encoding); |
| 7168 | |
Daniel Veillard | 5ecaf7f | 2003-01-09 13:19:33 +0000 | [diff] [blame] | 7169 | void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur); |
| 7170 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7171 | /** |
| 7172 | * xmlNsDumpOutput: |
| 7173 | * @buf: the XML buffer output |
| 7174 | * @cur: a namespace |
| 7175 | * |
| 7176 | * Dump a local Namespace definition. |
| 7177 | * Should be called in the context of attributes dumps. |
| 7178 | */ |
| 7179 | static void |
| 7180 | xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 7181 | if (cur == NULL) { |
| 7182 | #ifdef DEBUG_TREE |
| 7183 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7184 | "xmlNsDumpOutput : Ns == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7185 | #endif |
| 7186 | return; |
| 7187 | } |
| 7188 | if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 7189 | if (xmlStrEqual(cur->prefix, BAD_CAST "xml")) |
| 7190 | return; |
| 7191 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7192 | /* Within the context of an element attributes */ |
| 7193 | if (cur->prefix != NULL) { |
| 7194 | xmlOutputBufferWriteString(buf, " xmlns:"); |
| 7195 | xmlOutputBufferWriteString(buf, (const char *)cur->prefix); |
| 7196 | } else |
| 7197 | xmlOutputBufferWriteString(buf, " xmlns"); |
| 7198 | xmlOutputBufferWriteString(buf, "="); |
| 7199 | xmlBufferWriteQuotedString(buf->buffer, cur->href); |
| 7200 | } |
| 7201 | } |
| 7202 | |
| 7203 | /** |
| 7204 | * xmlNsListDumpOutput: |
| 7205 | * @buf: the XML buffer output |
| 7206 | * @cur: the first namespace |
| 7207 | * |
| 7208 | * Dump a list of local Namespace definitions. |
| 7209 | * Should be called in the context of attributes dumps. |
| 7210 | */ |
Daniel Veillard | 5ecaf7f | 2003-01-09 13:19:33 +0000 | [diff] [blame] | 7211 | void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7212 | xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 7213 | while (cur != NULL) { |
| 7214 | xmlNsDumpOutput(buf, cur); |
| 7215 | cur = cur->next; |
| 7216 | } |
| 7217 | } |
| 7218 | |
| 7219 | /** |
| 7220 | * xmlDtdDumpOutput: |
| 7221 | * @buf: the XML buffer output |
| 7222 | * @doc: the document |
| 7223 | * @encoding: an optional encoding string |
| 7224 | * |
| 7225 | * Dump the XML document DTD, if any. |
| 7226 | */ |
| 7227 | static void |
| 7228 | xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) { |
| 7229 | if (dtd == NULL) { |
| 7230 | #ifdef DEBUG_TREE |
| 7231 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7232 | "xmlDtdDumpOutput : no internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7233 | #endif |
| 7234 | return; |
| 7235 | } |
| 7236 | xmlOutputBufferWriteString(buf, "<!DOCTYPE "); |
| 7237 | xmlOutputBufferWriteString(buf, (const char *)dtd->name); |
| 7238 | if (dtd->ExternalID != NULL) { |
| 7239 | xmlOutputBufferWriteString(buf, " PUBLIC "); |
| 7240 | xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID); |
| 7241 | xmlOutputBufferWriteString(buf, " "); |
| 7242 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 7243 | } else if (dtd->SystemID != NULL) { |
| 7244 | xmlOutputBufferWriteString(buf, " SYSTEM "); |
| 7245 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 7246 | } |
| 7247 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 7248 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 7249 | xmlOutputBufferWriteString(buf, ">"); |
| 7250 | return; |
| 7251 | } |
| 7252 | xmlOutputBufferWriteString(buf, " [\n"); |
| 7253 | xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding); |
| 7254 | xmlOutputBufferWriteString(buf, "]>"); |
| 7255 | } |
| 7256 | |
| 7257 | /** |
| 7258 | * xmlAttrDumpOutput: |
| 7259 | * @buf: the XML buffer output |
| 7260 | * @doc: the document |
| 7261 | * @cur: the attribute pointer |
| 7262 | * @encoding: an optional encoding string |
| 7263 | * |
| 7264 | * Dump an XML attribute |
| 7265 | */ |
| 7266 | static void |
| 7267 | xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 7268 | const char *encoding ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7269 | if (cur == NULL) { |
| 7270 | #ifdef DEBUG_TREE |
| 7271 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7272 | "xmlAttrDumpOutput : property == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7273 | #endif |
| 7274 | return; |
| 7275 | } |
| 7276 | xmlOutputBufferWriteString(buf, " "); |
| 7277 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7278 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7279 | xmlOutputBufferWriteString(buf, ":"); |
| 7280 | } |
| 7281 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 7282 | xmlOutputBufferWriteString(buf, "=\""); |
| 7283 | xmlAttrSerializeContent(buf->buffer, doc, cur); |
| 7284 | xmlOutputBufferWriteString(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7285 | } |
| 7286 | |
| 7287 | /** |
| 7288 | * xmlAttrListDumpOutput: |
| 7289 | * @buf: the XML buffer output |
| 7290 | * @doc: the document |
| 7291 | * @cur: the first attribute pointer |
| 7292 | * @encoding: an optional encoding string |
| 7293 | * |
| 7294 | * Dump a list of XML attributes |
| 7295 | */ |
| 7296 | static void |
| 7297 | xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7298 | xmlAttrPtr cur, const char *encoding) { |
| 7299 | if (cur == NULL) { |
| 7300 | #ifdef DEBUG_TREE |
| 7301 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7302 | "xmlAttrListDumpOutput : property == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7303 | #endif |
| 7304 | return; |
| 7305 | } |
| 7306 | while (cur != NULL) { |
| 7307 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 7308 | cur = cur->next; |
| 7309 | } |
| 7310 | } |
| 7311 | |
| 7312 | |
| 7313 | |
| 7314 | /** |
| 7315 | * xmlNodeListDumpOutput: |
| 7316 | * @buf: the XML buffer output |
| 7317 | * @doc: the document |
| 7318 | * @cur: the first node |
| 7319 | * @level: the imbrication level for indenting |
| 7320 | * @format: is formatting allowed |
| 7321 | * @encoding: an optional encoding string |
| 7322 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7323 | * Dump an XML node list, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7324 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7325 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7326 | */ |
| 7327 | static void |
| 7328 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7329 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 7330 | int i; |
| 7331 | |
| 7332 | if (cur == NULL) { |
| 7333 | #ifdef DEBUG_TREE |
| 7334 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7335 | "xmlNodeListDumpOutput : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7336 | #endif |
| 7337 | return; |
| 7338 | } |
| 7339 | while (cur != NULL) { |
| 7340 | if ((format) && (xmlIndentTreeOutput) && |
| 7341 | (cur->type == XML_ELEMENT_NODE)) |
| 7342 | for (i = 0;i < level;i++) |
Aleksey Sanin | 2300256 | 2002-05-24 07:18:40 +0000 | [diff] [blame] | 7343 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7344 | xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7345 | if (format) { |
| 7346 | xmlOutputBufferWriteString(buf, "\n"); |
| 7347 | } |
| 7348 | cur = cur->next; |
| 7349 | } |
| 7350 | } |
| 7351 | |
| 7352 | /** |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7353 | * xmlNodeDumpOutputInternal: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7354 | * @buf: the XML buffer output |
| 7355 | * @doc: the document |
| 7356 | * @cur: the current node |
| 7357 | * @level: the imbrication level for indenting |
| 7358 | * @format: is formatting allowed |
| 7359 | * @encoding: an optional encoding string |
| 7360 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7361 | * Dump an XML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7362 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7363 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7364 | */ |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7365 | static void |
| 7366 | xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7367 | xmlNodePtr cur, int level, int format, const char *encoding) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7368 | int i; |
| 7369 | xmlNodePtr tmp; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 7370 | xmlChar *start, *end; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7371 | |
| 7372 | if (cur == NULL) { |
| 7373 | #ifdef DEBUG_TREE |
| 7374 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7375 | "xmlNodeDumpOutput : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7376 | #endif |
| 7377 | return; |
| 7378 | } |
| 7379 | if (cur->type == XML_XINCLUDE_START) |
| 7380 | return; |
| 7381 | if (cur->type == XML_XINCLUDE_END) |
| 7382 | return; |
| 7383 | if (cur->type == XML_DTD_NODE) { |
| 7384 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 7385 | return; |
| 7386 | } |
| 7387 | if (cur->type == XML_ELEMENT_DECL) { |
| 7388 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 7389 | return; |
| 7390 | } |
| 7391 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 7392 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 7393 | return; |
| 7394 | } |
| 7395 | if (cur->type == XML_ENTITY_DECL) { |
| 7396 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 7397 | return; |
| 7398 | } |
| 7399 | if (cur->type == XML_TEXT_NODE) { |
| 7400 | if (cur->content != NULL) { |
| 7401 | if ((cur->name == xmlStringText) || |
| 7402 | (cur->name != xmlStringTextNoenc)) { |
| 7403 | xmlChar *buffer; |
| 7404 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7405 | if (encoding == NULL) |
| 7406 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7407 | else |
| 7408 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7409 | if (buffer != NULL) { |
| 7410 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7411 | xmlFree(buffer); |
| 7412 | } |
| 7413 | } else { |
| 7414 | /* |
| 7415 | * Disable escaping, needed for XSLT |
| 7416 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7417 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7418 | } |
| 7419 | } |
| 7420 | |
| 7421 | return; |
| 7422 | } |
| 7423 | if (cur->type == XML_PI_NODE) { |
| 7424 | if (cur->content != NULL) { |
| 7425 | xmlOutputBufferWriteString(buf, "<?"); |
| 7426 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7427 | if (cur->content != NULL) { |
| 7428 | xmlOutputBufferWriteString(buf, " "); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7429 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7430 | } |
| 7431 | xmlOutputBufferWriteString(buf, "?>"); |
| 7432 | } else { |
| 7433 | xmlOutputBufferWriteString(buf, "<?"); |
| 7434 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7435 | xmlOutputBufferWriteString(buf, "?>"); |
| 7436 | } |
| 7437 | return; |
| 7438 | } |
| 7439 | if (cur->type == XML_COMMENT_NODE) { |
| 7440 | if (cur->content != NULL) { |
| 7441 | xmlOutputBufferWriteString(buf, "<!--"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7442 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7443 | xmlOutputBufferWriteString(buf, "-->"); |
| 7444 | } |
| 7445 | return; |
| 7446 | } |
| 7447 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 7448 | xmlOutputBufferWriteString(buf, "&"); |
| 7449 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7450 | xmlOutputBufferWriteString(buf, ";"); |
| 7451 | return; |
| 7452 | } |
| 7453 | if (cur->type == XML_CDATA_SECTION_NODE) { |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 7454 | start = end = cur->content; |
| 7455 | while (*end != '\0') { |
| 7456 | if ((*end == ']') && (*(end + 1) == ']') && (*(end + 2) == '>')) { |
| 7457 | end = end + 2; |
| 7458 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 7459 | xmlOutputBufferWrite(buf, end - start, (const char *)start); |
| 7460 | xmlOutputBufferWriteString(buf, "]]>"); |
| 7461 | start = end; |
| 7462 | } |
| 7463 | end++; |
| 7464 | } |
| 7465 | if (start != end) { |
| 7466 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 7467 | xmlOutputBufferWriteString(buf, (const char *)start); |
| 7468 | xmlOutputBufferWriteString(buf, "]]>"); |
| 7469 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7470 | return; |
| 7471 | } |
Daniel Veillard | 7b72ee5 | 2003-02-27 23:24:53 +0000 | [diff] [blame] | 7472 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 7473 | xmlAttrDumpOutput(buf,doc, (xmlAttrPtr) cur,encoding); |
Daniel Veillard | 7b72ee5 | 2003-02-27 23:24:53 +0000 | [diff] [blame] | 7474 | return; |
| 7475 | } |
Daniel Veillard | 6aa2f60 | 2003-02-10 00:01:56 +0000 | [diff] [blame] | 7476 | if (cur->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7477 | xmlNsDumpOutput(buf, (xmlNsPtr) cur); |
Daniel Veillard | 6aa2f60 | 2003-02-10 00:01:56 +0000 | [diff] [blame] | 7478 | return; |
| 7479 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7480 | |
| 7481 | if (format == 1) { |
| 7482 | tmp = cur->children; |
| 7483 | while (tmp != NULL) { |
| 7484 | if ((tmp->type == XML_TEXT_NODE) || |
| 7485 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 7486 | format = 0; |
| 7487 | break; |
| 7488 | } |
| 7489 | tmp = tmp->next; |
| 7490 | } |
| 7491 | } |
| 7492 | xmlOutputBufferWriteString(buf, "<"); |
| 7493 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7494 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7495 | xmlOutputBufferWriteString(buf, ":"); |
| 7496 | } |
| 7497 | |
| 7498 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7499 | if (cur->nsDef) |
| 7500 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 7501 | if (cur->properties != NULL) |
| 7502 | xmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 7503 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 7504 | if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) && |
| 7505 | (cur->children == NULL) && (!xmlSaveNoEmptyTags)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7506 | xmlOutputBufferWriteString(buf, "/>"); |
| 7507 | return; |
| 7508 | } |
| 7509 | xmlOutputBufferWriteString(buf, ">"); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 7510 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7511 | xmlChar *buffer; |
| 7512 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7513 | if (encoding == NULL) |
| 7514 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7515 | else |
| 7516 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7517 | if (buffer != NULL) { |
| 7518 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7519 | xmlFree(buffer); |
| 7520 | } |
| 7521 | } |
| 7522 | if (cur->children != NULL) { |
| 7523 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 7524 | xmlNodeListDumpOutput(buf, doc, cur->children, |
| 7525 | (level >= 0?level+1:-1), format, encoding); |
| 7526 | if ((xmlIndentTreeOutput) && (format)) |
| 7527 | for (i = 0;i < level;i++) |
Aleksey Sanin | 2300256 | 2002-05-24 07:18:40 +0000 | [diff] [blame] | 7528 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7529 | } |
| 7530 | xmlOutputBufferWriteString(buf, "</"); |
| 7531 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7532 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7533 | xmlOutputBufferWriteString(buf, ":"); |
| 7534 | } |
| 7535 | |
| 7536 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7537 | xmlOutputBufferWriteString(buf, ">"); |
| 7538 | } |
| 7539 | |
| 7540 | /** |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7541 | * xmlNodeDumpOutput: |
| 7542 | * @buf: the XML buffer output |
| 7543 | * @doc: the document |
| 7544 | * @cur: the current node |
| 7545 | * @level: the imbrication level for indenting |
| 7546 | * @format: is formatting allowed |
| 7547 | * @encoding: an optional encoding string |
| 7548 | * |
| 7549 | * Dump an XML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7550 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7551 | * or xmlKeepBlanksDefault(0) was called |
| 7552 | */ |
| 7553 | void |
| 7554 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7555 | int level, int format, const char *encoding) |
| 7556 | { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7557 | #ifdef LIBXML_HTML_ENABLED |
| 7558 | xmlDtdPtr dtd; |
| 7559 | int is_xhtml = 0; |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7560 | #endif |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7561 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7562 | xmlInitParser(); |
| 7563 | |
| 7564 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7565 | dtd = xmlGetIntSubset(doc); |
| 7566 | if (dtd != NULL) { |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7567 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 7568 | if (is_xhtml < 0) |
| 7569 | is_xhtml = 0; |
| 7570 | if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) && |
| 7571 | (cur->type == XML_ELEMENT_NODE) && |
| 7572 | (xmlStrEqual(cur->name, BAD_CAST "html"))) { |
| 7573 | if (encoding != NULL) |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 7574 | htmlSetMetaEncoding((htmlDocPtr) doc, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7575 | (const xmlChar *) encoding); |
| 7576 | else |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 7577 | htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8"); |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7578 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7579 | } |
| 7580 | |
| 7581 | if (is_xhtml) |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7582 | xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7583 | else |
| 7584 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7585 | xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7586 | } |
| 7587 | |
| 7588 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7589 | * xmlDocContentDumpOutput: |
| 7590 | * @buf: the XML buffer output |
| 7591 | * @cur: the document |
| 7592 | * @encoding: an optional encoding string |
| 7593 | * @format: should formatting spaces been added |
| 7594 | * |
| 7595 | * Dump an XML document. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7596 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7597 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7598 | */ |
| 7599 | static void |
| 7600 | xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 7601 | const char *encoding, int format) { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7602 | #ifdef LIBXML_HTML_ENABLED |
| 7603 | xmlDtdPtr dtd; |
| 7604 | int is_xhtml = 0; |
| 7605 | #endif |
| 7606 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7607 | xmlInitParser(); |
| 7608 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7609 | xmlOutputBufferWriteString(buf, "<?xml version="); |
| 7610 | if (cur->version != NULL) |
| 7611 | xmlBufferWriteQuotedString(buf->buffer, cur->version); |
| 7612 | else |
| 7613 | xmlOutputBufferWriteString(buf, "\"1.0\""); |
| 7614 | if (encoding == NULL) { |
| 7615 | if (cur->encoding != NULL) |
| 7616 | encoding = (const char *) cur->encoding; |
| 7617 | else if (cur->charset != XML_CHAR_ENCODING_UTF8) |
| 7618 | encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset); |
| 7619 | } |
| 7620 | if (encoding != NULL) { |
| 7621 | xmlOutputBufferWriteString(buf, " encoding="); |
| 7622 | xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding); |
| 7623 | } |
| 7624 | switch (cur->standalone) { |
| 7625 | case 0: |
| 7626 | xmlOutputBufferWriteString(buf, " standalone=\"no\""); |
| 7627 | break; |
| 7628 | case 1: |
| 7629 | xmlOutputBufferWriteString(buf, " standalone=\"yes\""); |
| 7630 | break; |
| 7631 | } |
| 7632 | xmlOutputBufferWriteString(buf, "?>\n"); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7633 | |
| 7634 | #ifdef LIBXML_HTML_ENABLED |
| 7635 | dtd = xmlGetIntSubset(cur); |
| 7636 | if (dtd != NULL) { |
| 7637 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 7638 | if (is_xhtml < 0) is_xhtml = 0; |
| 7639 | } |
| 7640 | if (is_xhtml) { |
| 7641 | if (encoding != NULL) |
| 7642 | htmlSetMetaEncoding(cur, (const xmlChar *) encoding); |
| 7643 | else |
| 7644 | htmlSetMetaEncoding(cur, BAD_CAST "UTF-8"); |
| 7645 | } |
| 7646 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7647 | if (cur->children != NULL) { |
| 7648 | xmlNodePtr child = cur->children; |
| 7649 | |
| 7650 | while (child != NULL) { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7651 | #ifdef LIBXML_HTML_ENABLED |
| 7652 | if (is_xhtml) |
| 7653 | xhtmlNodeDumpOutput(buf, cur, child, 0, format, encoding); |
| 7654 | else |
| 7655 | #endif |
| 7656 | xmlNodeDumpOutputInternal(buf, cur, child, 0, format, encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7657 | xmlOutputBufferWriteString(buf, "\n"); |
| 7658 | child = child->next; |
| 7659 | } |
| 7660 | } |
| 7661 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7662 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7663 | |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7664 | #ifdef LIBXML_HTML_ENABLED |
| 7665 | /************************************************************************ |
| 7666 | * * |
| 7667 | * Functions specific to XHTML serialization * |
| 7668 | * * |
| 7669 | ************************************************************************/ |
| 7670 | |
| 7671 | #define XHTML_STRICT_PUBLIC_ID BAD_CAST \ |
| 7672 | "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 7673 | #define XHTML_STRICT_SYSTEM_ID BAD_CAST \ |
| 7674 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" |
| 7675 | #define XHTML_FRAME_PUBLIC_ID BAD_CAST \ |
| 7676 | "-//W3C//DTD XHTML 1.0 Frameset//EN" |
| 7677 | #define XHTML_FRAME_SYSTEM_ID BAD_CAST \ |
| 7678 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd" |
| 7679 | #define XHTML_TRANS_PUBLIC_ID BAD_CAST \ |
| 7680 | "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 7681 | #define XHTML_TRANS_SYSTEM_ID BAD_CAST \ |
| 7682 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" |
| 7683 | |
| 7684 | #define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml" |
| 7685 | /** |
| 7686 | * xmlIsXHTML: |
| 7687 | * @systemID: the system identifier |
| 7688 | * @publicID: the public identifier |
| 7689 | * |
| 7690 | * Try to find if the document correspond to an XHTML DTD |
| 7691 | * |
| 7692 | * Returns 1 if true, 0 if not and -1 in case of error |
| 7693 | */ |
| 7694 | int |
| 7695 | xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) { |
| 7696 | if ((systemID == NULL) && (publicID == NULL)) |
| 7697 | return(-1); |
| 7698 | if (publicID != NULL) { |
| 7699 | if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1); |
| 7700 | if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1); |
| 7701 | if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1); |
| 7702 | } |
| 7703 | if (systemID != NULL) { |
| 7704 | if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1); |
| 7705 | if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1); |
| 7706 | if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1); |
| 7707 | } |
| 7708 | return(0); |
| 7709 | } |
| 7710 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7711 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7712 | /** |
| 7713 | * xhtmlIsEmpty: |
| 7714 | * @node: the node |
| 7715 | * |
| 7716 | * Check if a node is an empty xhtml node |
| 7717 | * |
| 7718 | * Returns 1 if the node is an empty node, 0 if not and -1 in case of error |
| 7719 | */ |
| 7720 | static int |
| 7721 | xhtmlIsEmpty(xmlNodePtr node) { |
| 7722 | if (node == NULL) |
| 7723 | return(-1); |
| 7724 | if (node->type != XML_ELEMENT_NODE) |
| 7725 | return(0); |
| 7726 | if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME))) |
| 7727 | return(0); |
| 7728 | if (node->children != NULL) |
| 7729 | return(0); |
| 7730 | switch (node->name[0]) { |
| 7731 | case 'a': |
| 7732 | if (xmlStrEqual(node->name, BAD_CAST "area")) |
| 7733 | return(1); |
| 7734 | return(0); |
| 7735 | case 'b': |
| 7736 | if (xmlStrEqual(node->name, BAD_CAST "br")) |
| 7737 | return(1); |
| 7738 | if (xmlStrEqual(node->name, BAD_CAST "base")) |
| 7739 | return(1); |
| 7740 | if (xmlStrEqual(node->name, BAD_CAST "basefont")) |
| 7741 | return(1); |
| 7742 | return(0); |
| 7743 | case 'c': |
| 7744 | if (xmlStrEqual(node->name, BAD_CAST "col")) |
| 7745 | return(1); |
| 7746 | return(0); |
| 7747 | case 'f': |
| 7748 | if (xmlStrEqual(node->name, BAD_CAST "frame")) |
| 7749 | return(1); |
| 7750 | return(0); |
| 7751 | case 'h': |
| 7752 | if (xmlStrEqual(node->name, BAD_CAST "hr")) |
| 7753 | return(1); |
| 7754 | return(0); |
| 7755 | case 'i': |
| 7756 | if (xmlStrEqual(node->name, BAD_CAST "img")) |
| 7757 | return(1); |
| 7758 | if (xmlStrEqual(node->name, BAD_CAST "input")) |
| 7759 | return(1); |
| 7760 | if (xmlStrEqual(node->name, BAD_CAST "isindex")) |
| 7761 | return(1); |
| 7762 | return(0); |
| 7763 | case 'l': |
| 7764 | if (xmlStrEqual(node->name, BAD_CAST "link")) |
| 7765 | return(1); |
| 7766 | return(0); |
| 7767 | case 'm': |
| 7768 | if (xmlStrEqual(node->name, BAD_CAST "meta")) |
| 7769 | return(1); |
| 7770 | return(0); |
| 7771 | case 'p': |
| 7772 | if (xmlStrEqual(node->name, BAD_CAST "param")) |
| 7773 | return(1); |
| 7774 | return(0); |
| 7775 | } |
| 7776 | return(0); |
| 7777 | } |
| 7778 | |
| 7779 | /** |
| 7780 | * xhtmlAttrListDumpOutput: |
| 7781 | * @buf: the XML buffer output |
| 7782 | * @doc: the document |
| 7783 | * @cur: the first attribute pointer |
| 7784 | * @encoding: an optional encoding string |
| 7785 | * |
| 7786 | * Dump a list of XML attributes |
| 7787 | */ |
| 7788 | static void |
| 7789 | xhtmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7790 | xmlAttrPtr cur, const char *encoding) { |
| 7791 | xmlAttrPtr xml_lang = NULL; |
| 7792 | xmlAttrPtr lang = NULL; |
| 7793 | xmlAttrPtr name = NULL; |
| 7794 | xmlAttrPtr id = NULL; |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7795 | xmlNodePtr parent; |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7796 | |
| 7797 | if (cur == NULL) { |
| 7798 | #ifdef DEBUG_TREE |
| 7799 | xmlGenericError(xmlGenericErrorContext, |
| 7800 | "xmlAttrListDumpOutput : property == NULL\n"); |
| 7801 | #endif |
| 7802 | return; |
| 7803 | } |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7804 | parent = cur->parent; |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7805 | while (cur != NULL) { |
| 7806 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id"))) |
| 7807 | id = cur; |
| 7808 | else |
| 7809 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name"))) |
| 7810 | name = cur; |
| 7811 | else |
| 7812 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang"))) |
| 7813 | lang = cur; |
| 7814 | else |
| 7815 | if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) && |
| 7816 | (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml"))) |
| 7817 | xml_lang = cur; |
| 7818 | else if ((cur->ns == NULL) && |
| 7819 | ((cur->children == NULL) || |
| 7820 | (cur->children->content == NULL) || |
| 7821 | (cur->children->content[0] == 0)) && |
| 7822 | (htmlIsBooleanAttr(cur->name))) { |
| 7823 | if (cur->children != NULL) |
| 7824 | xmlFreeNode(cur->children); |
| 7825 | cur->children = xmlNewText(cur->name); |
| 7826 | if (cur->children != NULL) |
| 7827 | cur->children->parent = (xmlNodePtr) cur; |
| 7828 | } |
| 7829 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 7830 | cur = cur->next; |
| 7831 | } |
| 7832 | /* |
| 7833 | * C.8 |
| 7834 | */ |
| 7835 | if ((name != NULL) && (id == NULL)) { |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7836 | if ((parent != NULL) && (parent->name != NULL) && |
| 7837 | ((xmlStrEqual(parent->name, BAD_CAST "a")) || |
| 7838 | (xmlStrEqual(parent->name, BAD_CAST "p")) || |
| 7839 | (xmlStrEqual(parent->name, BAD_CAST "div")) || |
| 7840 | (xmlStrEqual(parent->name, BAD_CAST "img")) || |
| 7841 | (xmlStrEqual(parent->name, BAD_CAST "map")) || |
| 7842 | (xmlStrEqual(parent->name, BAD_CAST "applet")) || |
| 7843 | (xmlStrEqual(parent->name, BAD_CAST "form")) || |
| 7844 | (xmlStrEqual(parent->name, BAD_CAST "frame")) || |
| 7845 | (xmlStrEqual(parent->name, BAD_CAST "iframe")))) { |
| 7846 | xmlOutputBufferWriteString(buf, " id=\""); |
| 7847 | xmlAttrSerializeContent(buf->buffer, doc, name); |
| 7848 | xmlOutputBufferWriteString(buf, "\""); |
| 7849 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7850 | } |
| 7851 | /* |
| 7852 | * C.7. |
| 7853 | */ |
| 7854 | if ((lang != NULL) && (xml_lang == NULL)) { |
| 7855 | xmlOutputBufferWriteString(buf, " xml:lang=\""); |
| 7856 | xmlAttrSerializeContent(buf->buffer, doc, lang); |
| 7857 | xmlOutputBufferWriteString(buf, "\""); |
| 7858 | } else |
| 7859 | if ((xml_lang != NULL) && (lang == NULL)) { |
| 7860 | xmlOutputBufferWriteString(buf, " lang=\""); |
| 7861 | xmlAttrSerializeContent(buf->buffer, doc, xml_lang); |
| 7862 | xmlOutputBufferWriteString(buf, "\""); |
| 7863 | } |
| 7864 | } |
| 7865 | |
| 7866 | /** |
| 7867 | * xhtmlNodeListDumpOutput: |
| 7868 | * @buf: the XML buffer output |
| 7869 | * @doc: the XHTML document |
| 7870 | * @cur: the first node |
| 7871 | * @level: the imbrication level for indenting |
| 7872 | * @format: is formatting allowed |
| 7873 | * @encoding: an optional encoding string |
| 7874 | * |
| 7875 | * Dump an XML node list, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7876 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7877 | * or xmlKeepBlanksDefault(0) was called |
| 7878 | */ |
| 7879 | static void |
| 7880 | xhtmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7881 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 7882 | int i; |
| 7883 | |
| 7884 | if (cur == NULL) { |
| 7885 | #ifdef DEBUG_TREE |
| 7886 | xmlGenericError(xmlGenericErrorContext, |
| 7887 | "xhtmlNodeListDumpOutput : node == NULL\n"); |
| 7888 | #endif |
| 7889 | return; |
| 7890 | } |
| 7891 | while (cur != NULL) { |
| 7892 | if ((format) && (xmlIndentTreeOutput) && |
| 7893 | (cur->type == XML_ELEMENT_NODE)) |
| 7894 | for (i = 0;i < level;i++) |
| 7895 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
| 7896 | xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
| 7897 | if (format) { |
| 7898 | xmlOutputBufferWriteString(buf, "\n"); |
| 7899 | } |
| 7900 | cur = cur->next; |
| 7901 | } |
| 7902 | } |
| 7903 | |
| 7904 | /** |
| 7905 | * xhtmlNodeDumpOutput: |
| 7906 | * @buf: the XML buffer output |
| 7907 | * @doc: the XHTML document |
| 7908 | * @cur: the current node |
| 7909 | * @level: the imbrication level for indenting |
| 7910 | * @format: is formatting allowed |
| 7911 | * @encoding: an optional encoding string |
| 7912 | * |
| 7913 | * Dump an XHTML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7914 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7915 | * or xmlKeepBlanksDefault(0) was called |
| 7916 | */ |
| 7917 | static void |
| 7918 | xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 7919 | int level, int format, const char *encoding) { |
| 7920 | int i; |
| 7921 | xmlNodePtr tmp; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 7922 | xmlChar *start, *end; |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7923 | |
| 7924 | if (cur == NULL) { |
| 7925 | #ifdef DEBUG_TREE |
| 7926 | xmlGenericError(xmlGenericErrorContext, |
| 7927 | "xmlNodeDumpOutput : node == NULL\n"); |
| 7928 | #endif |
| 7929 | return; |
| 7930 | } |
| 7931 | if (cur->type == XML_XINCLUDE_START) |
| 7932 | return; |
| 7933 | if (cur->type == XML_XINCLUDE_END) |
| 7934 | return; |
| 7935 | if (cur->type == XML_DTD_NODE) { |
| 7936 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 7937 | return; |
| 7938 | } |
| 7939 | if (cur->type == XML_ELEMENT_DECL) { |
| 7940 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 7941 | return; |
| 7942 | } |
| 7943 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 7944 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 7945 | return; |
| 7946 | } |
| 7947 | if (cur->type == XML_ENTITY_DECL) { |
| 7948 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 7949 | return; |
| 7950 | } |
| 7951 | if (cur->type == XML_TEXT_NODE) { |
| 7952 | if (cur->content != NULL) { |
| 7953 | if ((cur->name == xmlStringText) || |
| 7954 | (cur->name != xmlStringTextNoenc)) { |
| 7955 | xmlChar *buffer; |
| 7956 | |
| 7957 | if (encoding == NULL) |
| 7958 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7959 | else |
| 7960 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 7961 | if (buffer != NULL) { |
| 7962 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7963 | xmlFree(buffer); |
| 7964 | } |
| 7965 | } else { |
| 7966 | /* |
| 7967 | * Disable escaping, needed for XSLT |
| 7968 | */ |
| 7969 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
| 7970 | } |
| 7971 | } |
| 7972 | |
| 7973 | return; |
| 7974 | } |
| 7975 | if (cur->type == XML_PI_NODE) { |
| 7976 | if (cur->content != NULL) { |
| 7977 | xmlOutputBufferWriteString(buf, "<?"); |
| 7978 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7979 | if (cur->content != NULL) { |
| 7980 | xmlOutputBufferWriteString(buf, " "); |
| 7981 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 7982 | } |
| 7983 | xmlOutputBufferWriteString(buf, "?>"); |
| 7984 | } else { |
| 7985 | xmlOutputBufferWriteString(buf, "<?"); |
| 7986 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7987 | xmlOutputBufferWriteString(buf, "?>"); |
| 7988 | } |
| 7989 | return; |
| 7990 | } |
| 7991 | if (cur->type == XML_COMMENT_NODE) { |
| 7992 | if (cur->content != NULL) { |
| 7993 | xmlOutputBufferWriteString(buf, "<!--"); |
| 7994 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 7995 | xmlOutputBufferWriteString(buf, "-->"); |
| 7996 | } |
| 7997 | return; |
| 7998 | } |
| 7999 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 8000 | xmlOutputBufferWriteString(buf, "&"); |
| 8001 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 8002 | xmlOutputBufferWriteString(buf, ";"); |
| 8003 | return; |
| 8004 | } |
| 8005 | if (cur->type == XML_CDATA_SECTION_NODE) { |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 8006 | start = end = cur->content; |
| 8007 | while (*end != '\0') { |
| 8008 | if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') { |
| 8009 | end = end + 2; |
| 8010 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 8011 | xmlOutputBufferWrite(buf, end - start, (const char *)start); |
| 8012 | xmlOutputBufferWriteString(buf, "]]>"); |
| 8013 | start = end; |
| 8014 | } |
| 8015 | end++; |
| 8016 | } |
| 8017 | if (start != end) { |
| 8018 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 8019 | xmlOutputBufferWriteString(buf, (const char *)start); |
| 8020 | xmlOutputBufferWriteString(buf, "]]>"); |
| 8021 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 8022 | return; |
| 8023 | } |
| 8024 | |
| 8025 | if (format == 1) { |
| 8026 | tmp = cur->children; |
| 8027 | while (tmp != NULL) { |
| 8028 | if ((tmp->type == XML_TEXT_NODE) || |
| 8029 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 8030 | format = 0; |
| 8031 | break; |
| 8032 | } |
| 8033 | tmp = tmp->next; |
| 8034 | } |
| 8035 | } |
| 8036 | xmlOutputBufferWriteString(buf, "<"); |
| 8037 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 8038 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 8039 | xmlOutputBufferWriteString(buf, ":"); |
| 8040 | } |
| 8041 | |
| 8042 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 8043 | if (cur->nsDef) |
| 8044 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 8045 | if ((xmlStrEqual(cur->name, BAD_CAST "html") && |
| 8046 | (cur->ns == NULL) && (cur->nsDef == NULL))) { |
| 8047 | /* |
| 8048 | * 3.1.1. Strictly Conforming Documents A.3.1.1 3/ |
| 8049 | */ |
| 8050 | xmlOutputBufferWriteString(buf, |
| 8051 | " xmlns=\"http://www.w3.org/1999/xhtml\""); |
| 8052 | } |
| 8053 | if (cur->properties != NULL) |
| 8054 | xhtmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 8055 | |
| 8056 | if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) { |
| 8057 | if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) && |
| 8058 | (xhtmlIsEmpty(cur) == 1)) { |
| 8059 | /* |
| 8060 | * C.2. Empty Elements |
| 8061 | */ |
| 8062 | xmlOutputBufferWriteString(buf, " />"); |
| 8063 | } else { |
| 8064 | /* |
| 8065 | * C.3. Element Minimization and Empty Element Content |
| 8066 | */ |
| 8067 | xmlOutputBufferWriteString(buf, "></"); |
| 8068 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 8069 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 8070 | xmlOutputBufferWriteString(buf, ":"); |
| 8071 | } |
| 8072 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 8073 | xmlOutputBufferWriteString(buf, ">"); |
| 8074 | } |
| 8075 | return; |
| 8076 | } |
| 8077 | xmlOutputBufferWriteString(buf, ">"); |
| 8078 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
| 8079 | xmlChar *buffer; |
| 8080 | |
| 8081 | if (encoding == NULL) |
| 8082 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 8083 | else |
| 8084 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 8085 | if (buffer != NULL) { |
| 8086 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 8087 | xmlFree(buffer); |
| 8088 | } |
| 8089 | } |
| 8090 | |
| 8091 | /* |
| 8092 | * 4.8. Script and Style elements |
| 8093 | */ |
| 8094 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8095 | ((xmlStrEqual(cur->name, BAD_CAST "script")) || |
| 8096 | (xmlStrEqual(cur->name, BAD_CAST "style"))) && |
| 8097 | ((cur->ns == NULL) || |
| 8098 | (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) { |
| 8099 | xmlNodePtr child = cur->children; |
| 8100 | |
| 8101 | while (child != NULL) { |
| 8102 | if ((child->type == XML_TEXT_NODE) || |
| 8103 | (child->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 64b3528 | 2002-12-04 15:10:40 +0000 | [diff] [blame] | 8104 | /* |
| 8105 | * Apparently CDATA escaping for style just break on IE, |
| 8106 | * mozilla and galeon, so ... |
| 8107 | */ |
| 8108 | if (xmlStrEqual(cur->name, BAD_CAST "style") && |
| 8109 | (xmlStrchr(child->content, '<') == NULL) && |
| 8110 | (xmlStrchr(child->content, '>') == NULL) && |
| 8111 | (xmlStrchr(child->content, '&') == NULL)) { |
| 8112 | xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding); |
| 8113 | } else { |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 8114 | start = end = child->content; |
| 8115 | while (*end != '\0') { |
| 8116 | if (*end == ']' && |
| 8117 | *(end + 1) == ']' && |
| 8118 | *(end + 2) == '>') { |
| 8119 | end = end + 2; |
| 8120 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 8121 | xmlOutputBufferWrite(buf, end - start, |
| 8122 | (const char *)start); |
| 8123 | xmlOutputBufferWriteString(buf, "]]>"); |
| 8124 | start = end; |
| 8125 | } |
| 8126 | end++; |
| 8127 | } |
| 8128 | if (start != end) { |
| 8129 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 8130 | xmlOutputBufferWriteString(buf, (const char *)start); |
| 8131 | xmlOutputBufferWriteString(buf, "]]>"); |
| 8132 | } |
Daniel Veillard | 64b3528 | 2002-12-04 15:10:40 +0000 | [diff] [blame] | 8133 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 8134 | } else { |
| 8135 | xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding); |
| 8136 | } |
| 8137 | child = child->next; |
| 8138 | } |
| 8139 | } else if (cur->children != NULL) { |
| 8140 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 8141 | xhtmlNodeListDumpOutput(buf, doc, cur->children, |
| 8142 | (level >= 0?level+1:-1), format, encoding); |
| 8143 | if ((xmlIndentTreeOutput) && (format)) |
| 8144 | for (i = 0;i < level;i++) |
| 8145 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
| 8146 | } |
| 8147 | xmlOutputBufferWriteString(buf, "</"); |
| 8148 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 8149 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 8150 | xmlOutputBufferWriteString(buf, ":"); |
| 8151 | } |
| 8152 | |
| 8153 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 8154 | xmlOutputBufferWriteString(buf, ">"); |
| 8155 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 8156 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 8157 | #endif |
| 8158 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 8159 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8160 | /************************************************************************ |
| 8161 | * * |
| 8162 | * Saving functions front-ends * |
| 8163 | * * |
| 8164 | ************************************************************************/ |
| 8165 | |
| 8166 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 8167 | * xmlDocDumpFormatMemoryEnc: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8168 | * @out_doc: Document to generate XML text from |
| 8169 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 8170 | * @doc_txt_len: Length of the generated XML text |
| 8171 | * @txt_encoding: Character encoding to use when generating XML text |
| 8172 | * @format: should formatting spaces been added |
| 8173 | * |
| 8174 | * Dump the current DOM tree into memory using the character encoding specified |
| 8175 | * 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] | 8176 | * allocated memory with xmlFree(). |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8177 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 8178 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8179 | */ |
| 8180 | |
| 8181 | void |
| 8182 | xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8183 | int * doc_txt_len, const char * txt_encoding, |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8184 | int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8185 | int dummy = 0; |
| 8186 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8187 | xmlOutputBufferPtr out_buff = NULL; |
| 8188 | xmlCharEncodingHandlerPtr conv_hdlr = NULL; |
| 8189 | |
| 8190 | if (doc_txt_len == NULL) { |
| 8191 | doc_txt_len = &dummy; /* Continue, caller just won't get length */ |
| 8192 | } |
| 8193 | |
| 8194 | if (doc_txt_ptr == NULL) { |
| 8195 | *doc_txt_len = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8196 | return; |
| 8197 | } |
| 8198 | |
| 8199 | *doc_txt_ptr = NULL; |
| 8200 | *doc_txt_len = 0; |
| 8201 | |
| 8202 | if (out_doc == NULL) { |
| 8203 | /* No document, no output */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8204 | return; |
| 8205 | } |
| 8206 | |
| 8207 | /* |
| 8208 | * Validate the encoding value, if provided. |
| 8209 | * This logic is copied from xmlSaveFileEnc. |
| 8210 | */ |
| 8211 | |
| 8212 | if (txt_encoding == NULL) |
| 8213 | txt_encoding = (const char *) out_doc->encoding; |
| 8214 | if (txt_encoding != NULL) { |
Daniel Veillard | e132611 | 2003-06-05 09:32:20 +0000 | [diff] [blame] | 8215 | conv_hdlr = xmlFindCharEncodingHandler(txt_encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8216 | if ( conv_hdlr == NULL ) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 8217 | xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc, |
| 8218 | txt_encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8219 | return; |
| 8220 | } |
| 8221 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8222 | |
| 8223 | if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 8224 | xmlSaveErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8225 | return; |
| 8226 | } |
| 8227 | |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8228 | xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8229 | xmlOutputBufferFlush(out_buff); |
| 8230 | if (out_buff->conv != NULL) { |
| 8231 | *doc_txt_len = out_buff->conv->use; |
| 8232 | *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len); |
| 8233 | } else { |
| 8234 | *doc_txt_len = out_buff->buffer->use; |
| 8235 | *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len); |
| 8236 | } |
| 8237 | (void)xmlOutputBufferClose(out_buff); |
| 8238 | |
| 8239 | if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) { |
| 8240 | *doc_txt_len = 0; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 8241 | xmlSaveErrMemory("creating output"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8242 | } |
| 8243 | |
| 8244 | return; |
| 8245 | } |
| 8246 | |
| 8247 | /** |
| 8248 | * xmlDocDumpMemory: |
| 8249 | * @cur: the document |
| 8250 | * @mem: OUT: the memory pointer |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 8251 | * @size: OUT: the memory length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8252 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8253 | * 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] | 8254 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8255 | */ |
| 8256 | void |
| 8257 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
| 8258 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0); |
| 8259 | } |
| 8260 | |
| 8261 | /** |
| 8262 | * xmlDocDumpFormatMemory: |
| 8263 | * @cur: the document |
| 8264 | * @mem: OUT: the memory pointer |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 8265 | * @size: OUT: the memory length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8266 | * @format: should formatting spaces been added |
| 8267 | * |
| 8268 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8269 | * 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] | 8270 | * It's up to the caller to free the memory with xmlFree(). |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8271 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 8272 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8273 | */ |
| 8274 | void |
| 8275 | xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) { |
| 8276 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format); |
| 8277 | } |
| 8278 | |
| 8279 | /** |
| 8280 | * xmlDocDumpMemoryEnc: |
| 8281 | * @out_doc: Document to generate XML text from |
| 8282 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 8283 | * @doc_txt_len: Length of the generated XML text |
| 8284 | * @txt_encoding: Character encoding to use when generating XML text |
| 8285 | * |
| 8286 | * Dump the current DOM tree into memory using the character encoding specified |
| 8287 | * 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] | 8288 | * allocated memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8289 | */ |
| 8290 | |
| 8291 | void |
| 8292 | xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 8293 | int * doc_txt_len, const char * txt_encoding) { |
| 8294 | xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len, |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8295 | txt_encoding, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8296 | } |
| 8297 | |
| 8298 | /** |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8299 | * xmlDocFormatDump: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8300 | * @f: the FILE* |
| 8301 | * @cur: the document |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8302 | * @format: should formatting spaces been added |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8303 | * |
| 8304 | * Dump an XML document to an open FILE. |
| 8305 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8306 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8307 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8308 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8309 | */ |
| 8310 | int |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8311 | xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8312 | xmlOutputBufferPtr buf; |
| 8313 | const char * encoding; |
| 8314 | xmlCharEncodingHandlerPtr handler = NULL; |
| 8315 | int ret; |
| 8316 | |
| 8317 | if (cur == NULL) { |
| 8318 | #ifdef DEBUG_TREE |
| 8319 | xmlGenericError(xmlGenericErrorContext, |
| 8320 | "xmlDocDump : document == NULL\n"); |
| 8321 | #endif |
| 8322 | return(-1); |
| 8323 | } |
| 8324 | encoding = (const char *) cur->encoding; |
| 8325 | |
| 8326 | if (encoding != NULL) { |
Daniel Veillard | e132611 | 2003-06-05 09:32:20 +0000 | [diff] [blame] | 8327 | handler = xmlFindCharEncodingHandler(encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8328 | if (handler == NULL) { |
| 8329 | xmlFree((char *) cur->encoding); |
| 8330 | cur->encoding = NULL; |
| 8331 | } |
| 8332 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8333 | buf = xmlOutputBufferCreateFile(f, handler); |
| 8334 | if (buf == NULL) return(-1); |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8335 | xmlDocContentDumpOutput(buf, cur, NULL, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8336 | |
| 8337 | ret = xmlOutputBufferClose(buf); |
| 8338 | return(ret); |
| 8339 | } |
| 8340 | |
| 8341 | /** |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8342 | * xmlDocDump: |
| 8343 | * @f: the FILE* |
| 8344 | * @cur: the document |
| 8345 | * |
| 8346 | * Dump an XML document to an open FILE. |
| 8347 | * |
| 8348 | * returns: the number of bytes written or -1 in case of failure. |
| 8349 | */ |
| 8350 | int |
| 8351 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 8352 | return(xmlDocFormatDump (f, cur, 0)); |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8353 | } |
| 8354 | |
| 8355 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8356 | * xmlSaveFileTo: |
| 8357 | * @buf: an output I/O buffer |
| 8358 | * @cur: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8359 | * @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] | 8360 | * |
| 8361 | * Dump an XML document to an I/O buffer. |
| 8362 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8363 | * returns: the number of bytes written or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8364 | */ |
| 8365 | int |
CET 2001 Daniel Veillard | 5a37bde | 2001-11-01 14:31:22 +0000 | [diff] [blame] | 8366 | xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8367 | int ret; |
| 8368 | |
| 8369 | if (buf == NULL) return(0); |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8370 | xmlDocContentDumpOutput(buf, cur, encoding, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8371 | ret = xmlOutputBufferClose(buf); |
| 8372 | return(ret); |
| 8373 | } |
| 8374 | |
| 8375 | /** |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8376 | * xmlSaveFormatFileTo: |
| 8377 | * @buf: an output I/O buffer |
| 8378 | * @cur: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8379 | * @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] | 8380 | * @format: should formatting spaces been added |
| 8381 | * |
| 8382 | * Dump an XML document to an I/O buffer. |
| 8383 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8384 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8385 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8386 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8387 | */ |
| 8388 | int |
CET 2001 Daniel Veillard | 5a37bde | 2001-11-01 14:31:22 +0000 | [diff] [blame] | 8389 | xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) { |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8390 | int ret; |
| 8391 | |
| 8392 | if (buf == NULL) return(0); |
| 8393 | xmlDocContentDumpOutput(buf, cur, encoding, format); |
| 8394 | ret = xmlOutputBufferClose(buf); |
| 8395 | return(ret); |
| 8396 | } |
| 8397 | |
| 8398 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 8399 | * xmlSaveFormatFileEnc: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8400 | * @filename: the filename or URL to output |
| 8401 | * @cur: the document being saved |
| 8402 | * @encoding: the name of the encoding to use or NULL. |
| 8403 | * @format: should formatting spaces be added. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8404 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 8405 | * Dump an XML document to a file or an URL. |
| 8406 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8407 | * Returns the number of bytes written or -1 in case of error. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8408 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8409 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8410 | */ |
| 8411 | int |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8412 | xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur, |
| 8413 | const char * encoding, int format ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8414 | xmlOutputBufferPtr buf; |
| 8415 | xmlCharEncodingHandlerPtr handler = NULL; |
| 8416 | int ret; |
| 8417 | |
Daniel Veillard | 4dbe77a | 2003-01-14 00:17:42 +0000 | [diff] [blame] | 8418 | if (cur == NULL) |
| 8419 | return(-1); |
| 8420 | |
Daniel Veillard | fb25a51 | 2002-01-13 20:32:08 +0000 | [diff] [blame] | 8421 | if (encoding == NULL) |
| 8422 | encoding = (const char *) cur->encoding; |
| 8423 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8424 | if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8425 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8426 | handler = xmlFindCharEncodingHandler(encoding); |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 8427 | if (handler == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8428 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8429 | } |
| 8430 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8431 | #ifdef HAVE_ZLIB_H |
| 8432 | if (cur->compression < 0) cur->compression = xmlCompressMode; |
| 8433 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8434 | /* |
| 8435 | * save the content to a temp buffer. |
| 8436 | */ |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8437 | buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8438 | if (buf == NULL) return(-1); |
| 8439 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8440 | xmlDocContentDumpOutput(buf, cur, encoding, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8441 | |
| 8442 | ret = xmlOutputBufferClose(buf); |
| 8443 | return(ret); |
| 8444 | } |
| 8445 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8446 | |
| 8447 | /** |
| 8448 | * xmlSaveFileEnc: |
| 8449 | * @filename: the filename (or URL) |
| 8450 | * @cur: the document |
| 8451 | * @encoding: the name of an encoding (or NULL) |
| 8452 | * |
| 8453 | * Dump an XML document, converting it to the given encoding |
| 8454 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8455 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8456 | */ |
| 8457 | int |
| 8458 | xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 8459 | return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) ); |
| 8460 | } |
| 8461 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8462 | /** |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8463 | * xmlSaveFormatFile: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8464 | * @filename: the filename (or URL) |
| 8465 | * @cur: the document |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8466 | * @format: should formatting spaces been added |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8467 | * |
| 8468 | * Dump an XML document to a file. Will use compression if |
| 8469 | * compiled in and enabled. If @filename is "-" the stdout file is |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8470 | * 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] | 8471 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8472 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8473 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8474 | * returns: the number of bytes written or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8475 | */ |
| 8476 | int |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8477 | xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8478 | return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8479 | } |
| 8480 | |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8481 | /** |
| 8482 | * xmlSaveFile: |
| 8483 | * @filename: the filename (or URL) |
| 8484 | * @cur: the document |
| 8485 | * |
| 8486 | * Dump an XML document to a file. Will use compression if |
| 8487 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 8488 | * used. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8489 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8490 | */ |
| 8491 | int |
| 8492 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8493 | return(xmlSaveFormatFileEnc(filename, cur, NULL, 0)); |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8494 | } |
| 8495 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 8496 | #endif /* LIBXML_OUTPUT_ENABLED */ |
| 8497 | |
| 8498 | /** |
| 8499 | * xmlGetDocCompressMode: |
| 8500 | * @doc: the document |
| 8501 | * |
| 8502 | * get the compression ratio for a document, ZLIB based |
| 8503 | * Returns 0 (uncompressed) to 9 (max compression) |
| 8504 | */ |
| 8505 | int |
| 8506 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 8507 | if (doc == NULL) return(-1); |
| 8508 | return(doc->compression); |
| 8509 | } |
| 8510 | |
| 8511 | /** |
| 8512 | * xmlSetDocCompressMode: |
| 8513 | * @doc: the document |
| 8514 | * @mode: the compression ratio |
| 8515 | * |
| 8516 | * set the compression ratio for a document, ZLIB based |
| 8517 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 8518 | */ |
| 8519 | void |
| 8520 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 8521 | if (doc == NULL) return; |
| 8522 | if (mode < 0) doc->compression = 0; |
| 8523 | else if (mode > 9) doc->compression = 9; |
| 8524 | else doc->compression = mode; |
| 8525 | } |
| 8526 | |
| 8527 | /** |
| 8528 | * xmlGetCompressMode: |
| 8529 | * |
| 8530 | * get the default compression mode used, ZLIB based. |
| 8531 | * Returns 0 (uncompressed) to 9 (max compression) |
| 8532 | */ |
| 8533 | int |
| 8534 | xmlGetCompressMode(void) |
| 8535 | { |
| 8536 | return (xmlCompressMode); |
| 8537 | } |
| 8538 | |
| 8539 | /** |
| 8540 | * xmlSetCompressMode: |
| 8541 | * @mode: the compression ratio |
| 8542 | * |
| 8543 | * set the default compression mode used, ZLIB based |
| 8544 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 8545 | */ |
| 8546 | void |
| 8547 | xmlSetCompressMode(int mode) { |
| 8548 | if (mode < 0) xmlCompressMode = 0; |
| 8549 | else if (mode > 9) xmlCompressMode = 9; |
| 8550 | else xmlCompressMode = mode; |
| 8551 | } |
| 8552 | |