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) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 352 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 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) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 364 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 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 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 380 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 381 | return(1); |
| 382 | cur += l; |
| 383 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 384 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 385 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 386 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 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) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 421 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 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) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 447 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 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 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 463 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 464 | return(1); |
| 465 | cur += l; |
| 466 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 467 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 468 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 469 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 470 | cur += l; |
| 471 | c = CUR_SCHAR(cur, l); |
| 472 | } |
| 473 | if (c == ':') { |
| 474 | cur += l; |
| 475 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 476 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 477 | return(1); |
| 478 | cur += l; |
| 479 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 480 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 481 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 482 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 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) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 517 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 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) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 529 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 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 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 545 | if ((!IS_LETTER(c)) && (c != '_') && (c != ':')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 546 | return(1); |
| 547 | cur += l; |
| 548 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 549 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 550 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 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) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 584 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 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) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 598 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 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 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 614 | if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 615 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c))) |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 616 | return(1); |
| 617 | cur += l; |
| 618 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 619 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 620 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 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 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2110 | * Returns a pointer to the new node object. Uses xmlStrdup() to make |
| 2111 | * copy of @name. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2112 | */ |
| 2113 | xmlNodePtr |
| 2114 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 2115 | xmlNodePtr cur; |
| 2116 | |
| 2117 | if (name == NULL) { |
| 2118 | #ifdef DEBUG_TREE |
| 2119 | xmlGenericError(xmlGenericErrorContext, |
| 2120 | "xmlNewNode : name == NULL\n"); |
| 2121 | #endif |
| 2122 | return(NULL); |
| 2123 | } |
| 2124 | |
| 2125 | /* |
| 2126 | * Allocate a new node and fill the fields. |
| 2127 | */ |
| 2128 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2129 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2130 | xmlTreeErrMemory("building node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2131 | return(NULL); |
| 2132 | } |
| 2133 | memset(cur, 0, sizeof(xmlNode)); |
| 2134 | cur->type = XML_ELEMENT_NODE; |
| 2135 | |
| 2136 | cur->name = xmlStrdup(name); |
| 2137 | cur->ns = ns; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2138 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2139 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2140 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2141 | return(cur); |
| 2142 | } |
| 2143 | |
| 2144 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2145 | * xmlNewNodeEatName: |
| 2146 | * @ns: namespace if any |
| 2147 | * @name: the node name |
| 2148 | * |
| 2149 | * Creation of a new node element. @ns is optional (NULL). |
| 2150 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2151 | * Returns a pointer to the new node object, with pointer @name as |
| 2152 | * new node's name. Use xmlNewNode() if a copy of @name string is |
| 2153 | * is needed as new node's name. |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2154 | */ |
| 2155 | xmlNodePtr |
| 2156 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
| 2157 | xmlNodePtr cur; |
| 2158 | |
| 2159 | if (name == NULL) { |
| 2160 | #ifdef DEBUG_TREE |
| 2161 | xmlGenericError(xmlGenericErrorContext, |
| 2162 | "xmlNewNode : name == NULL\n"); |
| 2163 | #endif |
| 2164 | return(NULL); |
| 2165 | } |
| 2166 | |
| 2167 | /* |
| 2168 | * Allocate a new node and fill the fields. |
| 2169 | */ |
| 2170 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2171 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2172 | xmlTreeErrMemory("building node"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2173 | xmlFree(name); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2174 | return(NULL); |
| 2175 | } |
| 2176 | memset(cur, 0, sizeof(xmlNode)); |
| 2177 | cur->type = XML_ELEMENT_NODE; |
| 2178 | |
| 2179 | cur->name = name; |
| 2180 | cur->ns = ns; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2181 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2182 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2183 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2184 | return(cur); |
| 2185 | } |
| 2186 | |
| 2187 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2188 | * xmlNewDocNode: |
| 2189 | * @doc: the document |
| 2190 | * @ns: namespace if any |
| 2191 | * @name: the node name |
| 2192 | * @content: the XML text content if any |
| 2193 | * |
| 2194 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2195 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2196 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2197 | * references, but XML special chars need to be escaped first by using |
| 2198 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2199 | * need entities support. |
| 2200 | * |
| 2201 | * Returns a pointer to the new node object. |
| 2202 | */ |
| 2203 | xmlNodePtr |
| 2204 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2205 | const xmlChar *name, const xmlChar *content) { |
| 2206 | xmlNodePtr cur; |
| 2207 | |
| 2208 | cur = xmlNewNode(ns, name); |
| 2209 | if (cur != NULL) { |
| 2210 | cur->doc = doc; |
| 2211 | if (content != NULL) { |
| 2212 | cur->children = xmlStringGetNodeList(doc, content); |
| 2213 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2214 | } |
| 2215 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2216 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2217 | return(cur); |
| 2218 | } |
| 2219 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2220 | /** |
| 2221 | * xmlNewDocNodeEatName: |
| 2222 | * @doc: the document |
| 2223 | * @ns: namespace if any |
| 2224 | * @name: the node name |
| 2225 | * @content: the XML text content if any |
| 2226 | * |
| 2227 | * Creation of a new node element within a document. @ns and @content |
| 2228 | * are optional (NULL). |
| 2229 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2230 | * references, but XML special chars need to be escaped first by using |
| 2231 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2232 | * need entities support. |
| 2233 | * |
| 2234 | * Returns a pointer to the new node object. |
| 2235 | */ |
| 2236 | xmlNodePtr |
| 2237 | xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns, |
| 2238 | xmlChar *name, const xmlChar *content) { |
| 2239 | xmlNodePtr cur; |
| 2240 | |
| 2241 | cur = xmlNewNodeEatName(ns, name); |
| 2242 | if (cur != NULL) { |
| 2243 | cur->doc = doc; |
| 2244 | if (content != NULL) { |
| 2245 | cur->children = xmlStringGetNodeList(doc, content); |
| 2246 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2247 | } |
| 2248 | } |
| 2249 | return(cur); |
| 2250 | } |
| 2251 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2252 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2253 | /** |
| 2254 | * xmlNewDocRawNode: |
| 2255 | * @doc: the document |
| 2256 | * @ns: namespace if any |
| 2257 | * @name: the node name |
| 2258 | * @content: the text content if any |
| 2259 | * |
| 2260 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2261 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2262 | * |
| 2263 | * Returns a pointer to the new node object. |
| 2264 | */ |
| 2265 | xmlNodePtr |
| 2266 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2267 | const xmlChar *name, const xmlChar *content) { |
| 2268 | xmlNodePtr cur; |
| 2269 | |
| 2270 | cur = xmlNewNode(ns, name); |
| 2271 | if (cur != NULL) { |
| 2272 | cur->doc = doc; |
| 2273 | if (content != NULL) { |
| 2274 | cur->children = xmlNewDocText(doc, content); |
| 2275 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2276 | } |
| 2277 | } |
| 2278 | return(cur); |
| 2279 | } |
| 2280 | |
| 2281 | /** |
| 2282 | * xmlNewDocFragment: |
| 2283 | * @doc: the document owning the fragment |
| 2284 | * |
| 2285 | * Creation of a new Fragment node. |
| 2286 | * Returns a pointer to the new node object. |
| 2287 | */ |
| 2288 | xmlNodePtr |
| 2289 | xmlNewDocFragment(xmlDocPtr doc) { |
| 2290 | xmlNodePtr cur; |
| 2291 | |
| 2292 | /* |
| 2293 | * Allocate a new DocumentFragment node and fill the fields. |
| 2294 | */ |
| 2295 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2296 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2297 | xmlTreeErrMemory("building fragment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2298 | return(NULL); |
| 2299 | } |
| 2300 | memset(cur, 0, sizeof(xmlNode)); |
| 2301 | cur->type = XML_DOCUMENT_FRAG_NODE; |
| 2302 | |
| 2303 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2304 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2305 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2306 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2307 | return(cur); |
| 2308 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2309 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2310 | |
| 2311 | /** |
| 2312 | * xmlNewText: |
| 2313 | * @content: the text content |
| 2314 | * |
| 2315 | * Creation of a new text node. |
| 2316 | * Returns a pointer to the new node object. |
| 2317 | */ |
| 2318 | xmlNodePtr |
| 2319 | xmlNewText(const xmlChar *content) { |
| 2320 | xmlNodePtr cur; |
| 2321 | |
| 2322 | /* |
| 2323 | * Allocate a new node and fill the fields. |
| 2324 | */ |
| 2325 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2326 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2327 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2328 | return(NULL); |
| 2329 | } |
| 2330 | memset(cur, 0, sizeof(xmlNode)); |
| 2331 | cur->type = XML_TEXT_NODE; |
| 2332 | |
| 2333 | cur->name = xmlStringText; |
| 2334 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2335 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2336 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2337 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2338 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2339 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2340 | return(cur); |
| 2341 | } |
| 2342 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2343 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2344 | /** |
| 2345 | * xmlNewTextChild: |
| 2346 | * @parent: the parent node |
| 2347 | * @ns: a namespace if any |
| 2348 | * @name: the name of the child |
| 2349 | * @content: the text content of the child if any. |
| 2350 | * |
| 2351 | * Creation of a new child element, added at the end of @parent children list. |
MST 2003 John Fleck | e1f7049 | 2003-12-20 23:43:28 +0000 | [diff] [blame] | 2352 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2353 | * created element inherits the namespace of @parent. If @content is non NULL, |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2354 | * a child TEXT node will be created containing the string @content. |
MST 2003 John Fleck | 8b03bc5 | 2003-12-17 03:45:01 +0000 | [diff] [blame] | 2355 | * NOTE: Use xmlNewChild() if @content will contain entities that need to be |
| 2356 | * preserved. Use this function, xmlNewTextChild(), if you need to ensure that |
| 2357 | * reserved XML chars that might appear in @content, such as the ampersand, |
| 2358 | * greater-than or less-than signs, are automatically replaced by their XML |
| 2359 | * escaped entity representations. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2360 | * |
| 2361 | * Returns a pointer to the new node object. |
| 2362 | */ |
| 2363 | xmlNodePtr |
| 2364 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2365 | const xmlChar *name, const xmlChar *content) { |
| 2366 | xmlNodePtr cur, prev; |
| 2367 | |
| 2368 | if (parent == NULL) { |
| 2369 | #ifdef DEBUG_TREE |
| 2370 | xmlGenericError(xmlGenericErrorContext, |
| 2371 | "xmlNewTextChild : parent == NULL\n"); |
| 2372 | #endif |
| 2373 | return(NULL); |
| 2374 | } |
| 2375 | |
| 2376 | if (name == NULL) { |
| 2377 | #ifdef DEBUG_TREE |
| 2378 | xmlGenericError(xmlGenericErrorContext, |
| 2379 | "xmlNewTextChild : name == NULL\n"); |
| 2380 | #endif |
| 2381 | return(NULL); |
| 2382 | } |
| 2383 | |
| 2384 | /* |
| 2385 | * Allocate a new node |
| 2386 | */ |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 2387 | if (parent->type == XML_ELEMENT_NODE) { |
| 2388 | if (ns == NULL) |
| 2389 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 2390 | else |
| 2391 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 2392 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2393 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2394 | if (ns == NULL) |
| 2395 | cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content); |
| 2396 | else |
| 2397 | cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content); |
| 2398 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2399 | cur = xmlNewDocRawNode( parent->doc, ns, name, content); |
| 2400 | } else { |
| 2401 | return(NULL); |
| 2402 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2403 | if (cur == NULL) return(NULL); |
| 2404 | |
| 2405 | /* |
| 2406 | * add the new element at the end of the children list. |
| 2407 | */ |
| 2408 | cur->type = XML_ELEMENT_NODE; |
| 2409 | cur->parent = parent; |
| 2410 | cur->doc = parent->doc; |
| 2411 | if (parent->children == NULL) { |
| 2412 | parent->children = cur; |
| 2413 | parent->last = cur; |
| 2414 | } else { |
| 2415 | prev = parent->last; |
| 2416 | prev->next = cur; |
| 2417 | cur->prev = prev; |
| 2418 | parent->last = cur; |
| 2419 | } |
| 2420 | |
| 2421 | return(cur); |
| 2422 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2423 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2424 | |
| 2425 | /** |
| 2426 | * xmlNewCharRef: |
| 2427 | * @doc: the document |
| 2428 | * @name: the char ref string, starting with # or "&# ... ;" |
| 2429 | * |
| 2430 | * Creation of a new character reference node. |
| 2431 | * Returns a pointer to the new node object. |
| 2432 | */ |
| 2433 | xmlNodePtr |
| 2434 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 2435 | xmlNodePtr cur; |
| 2436 | |
| 2437 | /* |
| 2438 | * Allocate a new node and fill the fields. |
| 2439 | */ |
| 2440 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2441 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2442 | xmlTreeErrMemory("building character reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2443 | return(NULL); |
| 2444 | } |
| 2445 | memset(cur, 0, sizeof(xmlNode)); |
| 2446 | cur->type = XML_ENTITY_REF_NODE; |
| 2447 | |
| 2448 | cur->doc = doc; |
| 2449 | if (name[0] == '&') { |
| 2450 | int len; |
| 2451 | name++; |
| 2452 | len = xmlStrlen(name); |
| 2453 | if (name[len - 1] == ';') |
| 2454 | cur->name = xmlStrndup(name, len - 1); |
| 2455 | else |
| 2456 | cur->name = xmlStrndup(name, len); |
| 2457 | } else |
| 2458 | cur->name = xmlStrdup(name); |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2459 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2460 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2461 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2462 | return(cur); |
| 2463 | } |
| 2464 | |
| 2465 | /** |
| 2466 | * xmlNewReference: |
| 2467 | * @doc: the document |
| 2468 | * @name: the reference name, or the reference string with & and ; |
| 2469 | * |
| 2470 | * Creation of a new reference node. |
| 2471 | * Returns a pointer to the new node object. |
| 2472 | */ |
| 2473 | xmlNodePtr |
| 2474 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 2475 | xmlNodePtr cur; |
| 2476 | xmlEntityPtr ent; |
| 2477 | |
| 2478 | /* |
| 2479 | * Allocate a new node and fill the fields. |
| 2480 | */ |
| 2481 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2482 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2483 | xmlTreeErrMemory("building reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2484 | return(NULL); |
| 2485 | } |
| 2486 | memset(cur, 0, sizeof(xmlNode)); |
| 2487 | cur->type = XML_ENTITY_REF_NODE; |
| 2488 | |
| 2489 | cur->doc = doc; |
| 2490 | if (name[0] == '&') { |
| 2491 | int len; |
| 2492 | name++; |
| 2493 | len = xmlStrlen(name); |
| 2494 | if (name[len - 1] == ';') |
| 2495 | cur->name = xmlStrndup(name, len - 1); |
| 2496 | else |
| 2497 | cur->name = xmlStrndup(name, len); |
| 2498 | } else |
| 2499 | cur->name = xmlStrdup(name); |
| 2500 | |
| 2501 | ent = xmlGetDocEntity(doc, cur->name); |
| 2502 | if (ent != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2503 | cur->content = ent->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2504 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2505 | * 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] | 2506 | * updated. Not sure if this is 100% correct. |
| 2507 | * -George |
| 2508 | */ |
| 2509 | cur->children = (xmlNodePtr) ent; |
| 2510 | cur->last = (xmlNodePtr) ent; |
| 2511 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2512 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2513 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2514 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2515 | return(cur); |
| 2516 | } |
| 2517 | |
| 2518 | /** |
| 2519 | * xmlNewDocText: |
| 2520 | * @doc: the document |
| 2521 | * @content: the text content |
| 2522 | * |
| 2523 | * Creation of a new text node within a document. |
| 2524 | * Returns a pointer to the new node object. |
| 2525 | */ |
| 2526 | xmlNodePtr |
| 2527 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 2528 | xmlNodePtr cur; |
| 2529 | |
| 2530 | cur = xmlNewText(content); |
| 2531 | if (cur != NULL) cur->doc = doc; |
| 2532 | return(cur); |
| 2533 | } |
| 2534 | |
| 2535 | /** |
| 2536 | * xmlNewTextLen: |
| 2537 | * @content: the text content |
| 2538 | * @len: the text len. |
| 2539 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2540 | * 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] | 2541 | * Returns a pointer to the new node object. |
| 2542 | */ |
| 2543 | xmlNodePtr |
| 2544 | xmlNewTextLen(const xmlChar *content, int len) { |
| 2545 | xmlNodePtr cur; |
| 2546 | |
| 2547 | /* |
| 2548 | * Allocate a new node and fill the fields. |
| 2549 | */ |
| 2550 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2551 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2552 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2553 | return(NULL); |
| 2554 | } |
| 2555 | memset(cur, 0, sizeof(xmlNode)); |
| 2556 | cur->type = XML_TEXT_NODE; |
| 2557 | |
| 2558 | cur->name = xmlStringText; |
| 2559 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2560 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2561 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2562 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2563 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2564 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2565 | return(cur); |
| 2566 | } |
| 2567 | |
| 2568 | /** |
| 2569 | * xmlNewDocTextLen: |
| 2570 | * @doc: the document |
| 2571 | * @content: the text content |
| 2572 | * @len: the text len. |
| 2573 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2574 | * 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] | 2575 | * text node pertain to a given document. |
| 2576 | * Returns a pointer to the new node object. |
| 2577 | */ |
| 2578 | xmlNodePtr |
| 2579 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2580 | xmlNodePtr cur; |
| 2581 | |
| 2582 | cur = xmlNewTextLen(content, len); |
| 2583 | if (cur != NULL) cur->doc = doc; |
| 2584 | return(cur); |
| 2585 | } |
| 2586 | |
| 2587 | /** |
| 2588 | * xmlNewComment: |
| 2589 | * @content: the comment content |
| 2590 | * |
| 2591 | * Creation of a new node containing a comment. |
| 2592 | * Returns a pointer to the new node object. |
| 2593 | */ |
| 2594 | xmlNodePtr |
| 2595 | xmlNewComment(const xmlChar *content) { |
| 2596 | xmlNodePtr cur; |
| 2597 | |
| 2598 | /* |
| 2599 | * Allocate a new node and fill the fields. |
| 2600 | */ |
| 2601 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2602 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2603 | xmlTreeErrMemory("building comment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2604 | return(NULL); |
| 2605 | } |
| 2606 | memset(cur, 0, sizeof(xmlNode)); |
| 2607 | cur->type = XML_COMMENT_NODE; |
| 2608 | |
| 2609 | cur->name = xmlStringComment; |
| 2610 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2611 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2612 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2613 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2614 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2615 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2616 | return(cur); |
| 2617 | } |
| 2618 | |
| 2619 | /** |
| 2620 | * xmlNewCDataBlock: |
| 2621 | * @doc: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2622 | * @content: the CDATA block content content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2623 | * @len: the length of the block |
| 2624 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2625 | * Creation of a new node containing a CDATA block. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2626 | * Returns a pointer to the new node object. |
| 2627 | */ |
| 2628 | xmlNodePtr |
| 2629 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2630 | xmlNodePtr cur; |
| 2631 | |
| 2632 | /* |
| 2633 | * Allocate a new node and fill the fields. |
| 2634 | */ |
| 2635 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2636 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2637 | xmlTreeErrMemory("building CDATA"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2638 | return(NULL); |
| 2639 | } |
| 2640 | memset(cur, 0, sizeof(xmlNode)); |
| 2641 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2642 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2643 | |
| 2644 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2645 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2646 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2647 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2648 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2649 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2650 | return(cur); |
| 2651 | } |
| 2652 | |
| 2653 | /** |
| 2654 | * xmlNewDocComment: |
| 2655 | * @doc: the document |
| 2656 | * @content: the comment content |
| 2657 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2658 | * Creation of a new node containing a comment within a document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2659 | * Returns a pointer to the new node object. |
| 2660 | */ |
| 2661 | xmlNodePtr |
| 2662 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 2663 | xmlNodePtr cur; |
| 2664 | |
| 2665 | cur = xmlNewComment(content); |
| 2666 | if (cur != NULL) cur->doc = doc; |
| 2667 | return(cur); |
| 2668 | } |
| 2669 | |
| 2670 | /** |
| 2671 | * xmlSetTreeDoc: |
| 2672 | * @tree: the top element |
| 2673 | * @doc: the document |
| 2674 | * |
| 2675 | * update all nodes under the tree to point to the right document |
| 2676 | */ |
| 2677 | void |
| 2678 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2679 | xmlAttrPtr prop; |
| 2680 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2681 | if (tree == NULL) |
| 2682 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2683 | if (tree->doc != doc) { |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2684 | if(tree->type == XML_ELEMENT_NODE) { |
| 2685 | prop = tree->properties; |
| 2686 | while (prop != NULL) { |
| 2687 | prop->doc = doc; |
| 2688 | xmlSetListDoc(prop->children, doc); |
| 2689 | prop = prop->next; |
| 2690 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2691 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2692 | if (tree->children != NULL) |
| 2693 | xmlSetListDoc(tree->children, doc); |
| 2694 | tree->doc = doc; |
| 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | /** |
| 2699 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2700 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2701 | * @doc: the document |
| 2702 | * |
| 2703 | * update all nodes in the list to point to the right document |
| 2704 | */ |
| 2705 | void |
| 2706 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2707 | xmlNodePtr cur; |
| 2708 | |
| 2709 | if (list == NULL) |
| 2710 | return; |
| 2711 | cur = list; |
| 2712 | while (cur != NULL) { |
| 2713 | if (cur->doc != doc) |
| 2714 | xmlSetTreeDoc(cur, doc); |
| 2715 | cur = cur->next; |
| 2716 | } |
| 2717 | } |
| 2718 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2719 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2720 | /** |
| 2721 | * xmlNewChild: |
| 2722 | * @parent: the parent node |
| 2723 | * @ns: a namespace if any |
| 2724 | * @name: the name of the child |
| 2725 | * @content: the XML content of the child if any. |
| 2726 | * |
| 2727 | * Creation of a new child element, added at the end of @parent children list. |
MST 2003 John Fleck | e1f7049 | 2003-12-20 23:43:28 +0000 | [diff] [blame] | 2728 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2729 | * created element inherits the namespace of @parent. If @content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2730 | * a child list containing the TEXTs and ENTITY_REFs node will be created. |
MST 2003 John Fleck | 8b03bc5 | 2003-12-17 03:45:01 +0000 | [diff] [blame] | 2731 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 2732 | * references. XML special chars must be escaped first by using |
| 2733 | * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2734 | * |
| 2735 | * Returns a pointer to the new node object. |
| 2736 | */ |
| 2737 | xmlNodePtr |
| 2738 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2739 | const xmlChar *name, const xmlChar *content) { |
| 2740 | xmlNodePtr cur, prev; |
| 2741 | |
| 2742 | if (parent == NULL) { |
| 2743 | #ifdef DEBUG_TREE |
| 2744 | xmlGenericError(xmlGenericErrorContext, |
| 2745 | "xmlNewChild : parent == NULL\n"); |
| 2746 | #endif |
| 2747 | return(NULL); |
| 2748 | } |
| 2749 | |
| 2750 | if (name == NULL) { |
| 2751 | #ifdef DEBUG_TREE |
| 2752 | xmlGenericError(xmlGenericErrorContext, |
| 2753 | "xmlNewChild : name == NULL\n"); |
| 2754 | #endif |
| 2755 | return(NULL); |
| 2756 | } |
| 2757 | |
| 2758 | /* |
| 2759 | * Allocate a new node |
| 2760 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2761 | if (parent->type == XML_ELEMENT_NODE) { |
| 2762 | if (ns == NULL) |
| 2763 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2764 | else |
| 2765 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2766 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2767 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2768 | if (ns == NULL) |
| 2769 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2770 | else |
| 2771 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2772 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2773 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2774 | } else { |
| 2775 | return(NULL); |
| 2776 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2777 | if (cur == NULL) return(NULL); |
| 2778 | |
| 2779 | /* |
| 2780 | * add the new element at the end of the children list. |
| 2781 | */ |
| 2782 | cur->type = XML_ELEMENT_NODE; |
| 2783 | cur->parent = parent; |
| 2784 | cur->doc = parent->doc; |
| 2785 | if (parent->children == NULL) { |
| 2786 | parent->children = cur; |
| 2787 | parent->last = cur; |
| 2788 | } else { |
| 2789 | prev = parent->last; |
| 2790 | prev->next = cur; |
| 2791 | cur->prev = prev; |
| 2792 | parent->last = cur; |
| 2793 | } |
| 2794 | |
| 2795 | return(cur); |
| 2796 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2797 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2798 | |
| 2799 | /** |
| 2800 | * xmlAddNextSibling: |
| 2801 | * @cur: the child node |
| 2802 | * @elem: the new node |
| 2803 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2804 | * Add a new node @elem as the next sibling of @cur |
| 2805 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2806 | * first unlinked from its existing context. |
| 2807 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2808 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2809 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2810 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2811 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2812 | */ |
| 2813 | xmlNodePtr |
| 2814 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2815 | if (cur == NULL) { |
| 2816 | #ifdef DEBUG_TREE |
| 2817 | xmlGenericError(xmlGenericErrorContext, |
| 2818 | "xmlAddNextSibling : cur == NULL\n"); |
| 2819 | #endif |
| 2820 | return(NULL); |
| 2821 | } |
| 2822 | if (elem == NULL) { |
| 2823 | #ifdef DEBUG_TREE |
| 2824 | xmlGenericError(xmlGenericErrorContext, |
| 2825 | "xmlAddNextSibling : elem == NULL\n"); |
| 2826 | #endif |
| 2827 | return(NULL); |
| 2828 | } |
| 2829 | |
| 2830 | xmlUnlinkNode(elem); |
| 2831 | |
| 2832 | if (elem->type == XML_TEXT_NODE) { |
| 2833 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2834 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2835 | xmlFreeNode(elem); |
| 2836 | return(cur); |
| 2837 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2838 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 2839 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2840 | xmlChar *tmp; |
| 2841 | |
| 2842 | tmp = xmlStrdup(elem->content); |
| 2843 | tmp = xmlStrcat(tmp, cur->next->content); |
| 2844 | xmlNodeSetContent(cur->next, tmp); |
| 2845 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2846 | xmlFreeNode(elem); |
| 2847 | return(cur->next); |
| 2848 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2849 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2850 | /* check if an attribute with the same name exists */ |
| 2851 | xmlAttrPtr attr; |
| 2852 | |
| 2853 | if (elem->ns == NULL) |
| 2854 | attr = xmlHasProp(cur->parent, elem->name); |
| 2855 | else |
| 2856 | attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href); |
| 2857 | if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) { |
| 2858 | /* different instance, destroy it (attributes must be unique) */ |
| 2859 | xmlFreeProp(attr); |
| 2860 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | if (elem->doc != cur->doc) { |
| 2864 | xmlSetTreeDoc(elem, cur->doc); |
| 2865 | } |
| 2866 | elem->parent = cur->parent; |
| 2867 | elem->prev = cur; |
| 2868 | elem->next = cur->next; |
| 2869 | cur->next = elem; |
| 2870 | if (elem->next != NULL) |
| 2871 | elem->next->prev = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2872 | 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] | 2873 | elem->parent->last = elem; |
| 2874 | return(elem); |
| 2875 | } |
| 2876 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2877 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2878 | /** |
| 2879 | * xmlAddPrevSibling: |
| 2880 | * @cur: the child node |
| 2881 | * @elem: the new node |
| 2882 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2883 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2884 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2885 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2886 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2887 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2888 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2889 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2890 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2891 | */ |
| 2892 | xmlNodePtr |
| 2893 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2894 | if (cur == NULL) { |
| 2895 | #ifdef DEBUG_TREE |
| 2896 | xmlGenericError(xmlGenericErrorContext, |
| 2897 | "xmlAddPrevSibling : cur == NULL\n"); |
| 2898 | #endif |
| 2899 | return(NULL); |
| 2900 | } |
| 2901 | if (elem == NULL) { |
| 2902 | #ifdef DEBUG_TREE |
| 2903 | xmlGenericError(xmlGenericErrorContext, |
| 2904 | "xmlAddPrevSibling : elem == NULL\n"); |
| 2905 | #endif |
| 2906 | return(NULL); |
| 2907 | } |
| 2908 | |
| 2909 | xmlUnlinkNode(elem); |
| 2910 | |
| 2911 | if (elem->type == XML_TEXT_NODE) { |
| 2912 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2913 | xmlChar *tmp; |
| 2914 | |
| 2915 | tmp = xmlStrdup(elem->content); |
| 2916 | tmp = xmlStrcat(tmp, cur->content); |
| 2917 | xmlNodeSetContent(cur, tmp); |
| 2918 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2919 | xmlFreeNode(elem); |
| 2920 | return(cur); |
| 2921 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2922 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 2923 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2924 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2925 | xmlFreeNode(elem); |
| 2926 | return(cur->prev); |
| 2927 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2928 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2929 | /* check if an attribute with the same name exists */ |
| 2930 | xmlAttrPtr attr; |
| 2931 | |
| 2932 | if (elem->ns == NULL) |
| 2933 | attr = xmlHasProp(cur->parent, elem->name); |
| 2934 | else |
| 2935 | attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href); |
| 2936 | if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) { |
| 2937 | /* different instance, destroy it (attributes must be unique) */ |
| 2938 | xmlFreeProp(attr); |
| 2939 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | if (elem->doc != cur->doc) { |
| 2943 | xmlSetTreeDoc(elem, cur->doc); |
| 2944 | } |
| 2945 | elem->parent = cur->parent; |
| 2946 | elem->next = cur; |
| 2947 | elem->prev = cur->prev; |
| 2948 | cur->prev = elem; |
| 2949 | if (elem->prev != NULL) |
| 2950 | elem->prev->next = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2951 | if (elem->parent != NULL) { |
| 2952 | if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2953 | if (elem->parent->properties == (xmlAttrPtr) cur) { |
| 2954 | elem->parent->properties = (xmlAttrPtr) elem; |
| 2955 | } |
| 2956 | } else { |
| 2957 | if (elem->parent->children == cur) { |
| 2958 | elem->parent->children = elem; |
| 2959 | } |
| 2960 | } |
| 2961 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2962 | return(elem); |
| 2963 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2964 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2965 | |
| 2966 | /** |
| 2967 | * xmlAddSibling: |
| 2968 | * @cur: the child node |
| 2969 | * @elem: the new node |
| 2970 | * |
| 2971 | * Add a new element @elem to the list of siblings of @cur |
| 2972 | * merging adjacent TEXT nodes (@elem may be freed) |
| 2973 | * If the new element was already inserted in a document it is |
| 2974 | * first unlinked from its existing context. |
| 2975 | * |
| 2976 | * Returns the new element or NULL in case of error. |
| 2977 | */ |
| 2978 | xmlNodePtr |
| 2979 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2980 | xmlNodePtr parent; |
| 2981 | |
| 2982 | if (cur == NULL) { |
| 2983 | #ifdef DEBUG_TREE |
| 2984 | xmlGenericError(xmlGenericErrorContext, |
| 2985 | "xmlAddSibling : cur == NULL\n"); |
| 2986 | #endif |
| 2987 | return(NULL); |
| 2988 | } |
| 2989 | |
| 2990 | if (elem == NULL) { |
| 2991 | #ifdef DEBUG_TREE |
| 2992 | xmlGenericError(xmlGenericErrorContext, |
| 2993 | "xmlAddSibling : elem == NULL\n"); |
| 2994 | #endif |
| 2995 | return(NULL); |
| 2996 | } |
| 2997 | |
| 2998 | /* |
| 2999 | * Constant time is we can rely on the ->parent->last to find |
| 3000 | * the last sibling. |
| 3001 | */ |
| 3002 | if ((cur->parent != NULL) && |
| 3003 | (cur->parent->children != NULL) && |
| 3004 | (cur->parent->last != NULL) && |
| 3005 | (cur->parent->last->next == NULL)) { |
| 3006 | cur = cur->parent->last; |
| 3007 | } else { |
| 3008 | while (cur->next != NULL) cur = cur->next; |
| 3009 | } |
| 3010 | |
| 3011 | xmlUnlinkNode(elem); |
| 3012 | |
Daniel Veillard | e22dd5c | 2003-10-29 12:53:27 +0000 | [diff] [blame] | 3013 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) && |
| 3014 | (cur->name == elem->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3015 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3016 | xmlFreeNode(elem); |
| 3017 | return(cur); |
| 3018 | } |
| 3019 | |
| 3020 | if (elem->doc != cur->doc) { |
| 3021 | xmlSetTreeDoc(elem, cur->doc); |
| 3022 | } |
| 3023 | parent = cur->parent; |
| 3024 | elem->prev = cur; |
| 3025 | elem->next = NULL; |
| 3026 | elem->parent = parent; |
| 3027 | cur->next = elem; |
| 3028 | if (parent != NULL) |
| 3029 | parent->last = elem; |
| 3030 | |
| 3031 | return(elem); |
| 3032 | } |
| 3033 | |
| 3034 | /** |
| 3035 | * xmlAddChildList: |
| 3036 | * @parent: the parent node |
| 3037 | * @cur: the first node in the list |
| 3038 | * |
| 3039 | * Add a list of node at the end of the child list of the parent |
| 3040 | * merging adjacent TEXT nodes (@cur may be freed) |
| 3041 | * |
| 3042 | * Returns the last child or NULL in case of error. |
| 3043 | */ |
| 3044 | xmlNodePtr |
| 3045 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 3046 | xmlNodePtr prev; |
| 3047 | |
| 3048 | if (parent == NULL) { |
| 3049 | #ifdef DEBUG_TREE |
| 3050 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3051 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3052 | #endif |
| 3053 | return(NULL); |
| 3054 | } |
| 3055 | |
| 3056 | if (cur == NULL) { |
| 3057 | #ifdef DEBUG_TREE |
| 3058 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3059 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3060 | #endif |
| 3061 | return(NULL); |
| 3062 | } |
| 3063 | |
| 3064 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 3065 | (cur->doc != parent->doc)) { |
| 3066 | #ifdef DEBUG_TREE |
| 3067 | xmlGenericError(xmlGenericErrorContext, |
| 3068 | "Elements moved to a different document\n"); |
| 3069 | #endif |
| 3070 | } |
| 3071 | |
| 3072 | /* |
| 3073 | * add the first element at the end of the children list. |
| 3074 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3075 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3076 | if (parent->children == NULL) { |
| 3077 | parent->children = cur; |
| 3078 | } else { |
| 3079 | /* |
| 3080 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 3081 | */ |
| 3082 | if ((cur->type == XML_TEXT_NODE) && |
| 3083 | (parent->last->type == XML_TEXT_NODE) && |
| 3084 | (cur->name == parent->last->name)) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3085 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3086 | /* |
| 3087 | * if it's the only child, nothing more to be done. |
| 3088 | */ |
| 3089 | if (cur->next == NULL) { |
| 3090 | xmlFreeNode(cur); |
| 3091 | return(parent->last); |
| 3092 | } |
| 3093 | prev = cur; |
| 3094 | cur = cur->next; |
| 3095 | xmlFreeNode(prev); |
| 3096 | } |
| 3097 | prev = parent->last; |
| 3098 | prev->next = cur; |
| 3099 | cur->prev = prev; |
| 3100 | } |
| 3101 | while (cur->next != NULL) { |
| 3102 | cur->parent = parent; |
| 3103 | if (cur->doc != parent->doc) { |
| 3104 | xmlSetTreeDoc(cur, parent->doc); |
| 3105 | } |
| 3106 | cur = cur->next; |
| 3107 | } |
| 3108 | cur->parent = parent; |
| 3109 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 3110 | parent->last = cur; |
| 3111 | |
| 3112 | return(cur); |
| 3113 | } |
| 3114 | |
| 3115 | /** |
| 3116 | * xmlAddChild: |
| 3117 | * @parent: the parent node |
| 3118 | * @cur: the child node |
| 3119 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3120 | * 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] | 3121 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3122 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 3123 | * If there is an attribute with equal name, it is first destroyed. |
| 3124 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3125 | * Returns the child or NULL in case of error. |
| 3126 | */ |
| 3127 | xmlNodePtr |
| 3128 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 3129 | xmlNodePtr prev; |
| 3130 | |
| 3131 | if (parent == NULL) { |
| 3132 | #ifdef DEBUG_TREE |
| 3133 | xmlGenericError(xmlGenericErrorContext, |
| 3134 | "xmlAddChild : parent == NULL\n"); |
| 3135 | #endif |
| 3136 | return(NULL); |
| 3137 | } |
| 3138 | |
| 3139 | if (cur == NULL) { |
| 3140 | #ifdef DEBUG_TREE |
| 3141 | xmlGenericError(xmlGenericErrorContext, |
| 3142 | "xmlAddChild : child == NULL\n"); |
| 3143 | #endif |
| 3144 | return(NULL); |
| 3145 | } |
| 3146 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3147 | /* |
| 3148 | * 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] | 3149 | * cur is then freed. |
| 3150 | */ |
| 3151 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3152 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3153 | (parent->content != NULL) && |
Daniel Veillard | e22dd5c | 2003-10-29 12:53:27 +0000 | [diff] [blame] | 3154 | (parent->name == cur->name) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3155 | (parent != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3156 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3157 | xmlFreeNode(cur); |
| 3158 | return(parent); |
| 3159 | } |
| 3160 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3161 | (parent->last->name == cur->name) && |
| 3162 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3163 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3164 | xmlFreeNode(cur); |
| 3165 | return(parent->last); |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | /* |
| 3170 | * add the new element at the end of the children list. |
| 3171 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3172 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3173 | cur->parent = parent; |
| 3174 | if (cur->doc != parent->doc) { |
| 3175 | xmlSetTreeDoc(cur, parent->doc); |
| 3176 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3177 | /* this check prevents a loop on tree-traversions if a developer |
| 3178 | * tries to add a node to its parent multiple times |
| 3179 | */ |
| 3180 | if (prev == parent) |
| 3181 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3182 | |
| 3183 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3184 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3185 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3186 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3187 | (parent->content != NULL) && |
| 3188 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3189 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3190 | xmlFreeNode(cur); |
| 3191 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3192 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3193 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3194 | if (parent->properties == NULL) { |
| 3195 | parent->properties = (xmlAttrPtr) cur; |
| 3196 | } else { |
| 3197 | /* check if an attribute with the same name exists */ |
| 3198 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3199 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3200 | if (cur->ns == NULL) |
| 3201 | lastattr = xmlHasProp(parent, cur->name); |
| 3202 | else |
| 3203 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
| 3204 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur)) { |
| 3205 | /* different instance, destroy it (attributes must be unique) */ |
| 3206 | xmlFreeProp(lastattr); |
| 3207 | } |
| 3208 | /* find the end */ |
| 3209 | lastattr = parent->properties; |
| 3210 | while (lastattr->next != NULL) { |
| 3211 | lastattr = lastattr->next; |
| 3212 | } |
| 3213 | lastattr->next = (xmlAttrPtr) cur; |
| 3214 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3215 | } |
| 3216 | } else { |
| 3217 | if (parent->children == NULL) { |
| 3218 | parent->children = cur; |
| 3219 | parent->last = cur; |
| 3220 | } else { |
| 3221 | prev = parent->last; |
| 3222 | prev->next = cur; |
| 3223 | cur->prev = prev; |
| 3224 | parent->last = cur; |
| 3225 | } |
| 3226 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3227 | return(cur); |
| 3228 | } |
| 3229 | |
| 3230 | /** |
| 3231 | * xmlGetLastChild: |
| 3232 | * @parent: the parent node |
| 3233 | * |
| 3234 | * Search the last child of a node. |
| 3235 | * Returns the last child or NULL if none. |
| 3236 | */ |
| 3237 | xmlNodePtr |
| 3238 | xmlGetLastChild(xmlNodePtr parent) { |
| 3239 | if (parent == NULL) { |
| 3240 | #ifdef DEBUG_TREE |
| 3241 | xmlGenericError(xmlGenericErrorContext, |
| 3242 | "xmlGetLastChild : parent == NULL\n"); |
| 3243 | #endif |
| 3244 | return(NULL); |
| 3245 | } |
| 3246 | return(parent->last); |
| 3247 | } |
| 3248 | |
| 3249 | /** |
| 3250 | * xmlFreeNodeList: |
| 3251 | * @cur: the first node in the list |
| 3252 | * |
| 3253 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3254 | * the children are freed too. |
| 3255 | */ |
| 3256 | void |
| 3257 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3258 | xmlNodePtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3259 | xmlDictPtr dict = NULL; |
| 3260 | |
| 3261 | if (cur == NULL) return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3262 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3263 | xmlFreeNsList((xmlNsPtr) cur); |
| 3264 | return; |
| 3265 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3266 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3267 | #ifdef LIBXML_DOCB_ENABLED |
| 3268 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3269 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3270 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3271 | xmlFreeDoc((xmlDocPtr) cur); |
| 3272 | return; |
| 3273 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3274 | if (cur->doc != NULL) dict = cur->doc->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3275 | while (cur != NULL) { |
| 3276 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3277 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3278 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3279 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3280 | xmlDeregisterNodeDefaultValue(cur); |
| 3281 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3282 | if ((cur->children != NULL) && |
| 3283 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3284 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3285 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3286 | (cur->type == XML_XINCLUDE_START) || |
| 3287 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3288 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3289 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3290 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3291 | (cur->type != XML_XINCLUDE_START) && |
| 3292 | (cur->type != XML_XINCLUDE_END) && |
| 3293 | (cur->type != XML_ENTITY_REF_NODE)) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3294 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3295 | } |
| 3296 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3297 | (cur->type == XML_XINCLUDE_START) || |
| 3298 | (cur->type == XML_XINCLUDE_END)) && |
| 3299 | (cur->nsDef != NULL)) |
| 3300 | xmlFreeNsList(cur->nsDef); |
| 3301 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3302 | /* |
| 3303 | * When a node is a text node or a comment, it uses a global static |
| 3304 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3305 | * Otherwise the node name might come from the document's |
| 3306 | * dictionnary |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3307 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3308 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3309 | (cur->type != XML_TEXT_NODE) && |
| 3310 | (cur->type != XML_COMMENT_NODE)) |
| 3311 | DICT_FREE(cur->name) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3312 | xmlFree(cur); |
| 3313 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3314 | cur = next; |
| 3315 | } |
| 3316 | } |
| 3317 | |
| 3318 | /** |
| 3319 | * xmlFreeNode: |
| 3320 | * @cur: the node |
| 3321 | * |
| 3322 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3323 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3324 | */ |
| 3325 | void |
| 3326 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3327 | xmlDictPtr dict = NULL; |
| 3328 | |
| 3329 | if (cur == NULL) return; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3330 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3331 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3332 | if (cur->type == XML_DTD_NODE) { |
| 3333 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3334 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3335 | } |
| 3336 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3337 | xmlFreeNs((xmlNsPtr) cur); |
| 3338 | return; |
| 3339 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3340 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3341 | xmlFreeProp((xmlAttrPtr) cur); |
| 3342 | return; |
| 3343 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3344 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3345 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3346 | xmlDeregisterNodeDefaultValue(cur); |
| 3347 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3348 | if (cur->doc != NULL) dict = cur->doc->dict; |
| 3349 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3350 | if ((cur->children != NULL) && |
| 3351 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3352 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3353 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3354 | (cur->type == XML_XINCLUDE_START) || |
| 3355 | (cur->type == XML_XINCLUDE_END)) && |
| 3356 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3357 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3358 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3359 | (cur->content != NULL) && |
| 3360 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3361 | (cur->type != XML_XINCLUDE_END) && |
| 3362 | (cur->type != XML_XINCLUDE_START)) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3363 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3366 | /* |
| 3367 | * When a node is a text node or a comment, it uses a global static |
| 3368 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3369 | * Otherwise the node name might come from the document's dictionnary |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3370 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3371 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3372 | (cur->type != XML_TEXT_NODE) && |
| 3373 | (cur->type != XML_COMMENT_NODE)) |
| 3374 | DICT_FREE(cur->name) |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3375 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3376 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3377 | (cur->type == XML_XINCLUDE_START) || |
| 3378 | (cur->type == XML_XINCLUDE_END)) && |
| 3379 | (cur->nsDef != NULL)) |
| 3380 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3381 | xmlFree(cur); |
| 3382 | } |
| 3383 | |
| 3384 | /** |
| 3385 | * xmlUnlinkNode: |
| 3386 | * @cur: the node |
| 3387 | * |
| 3388 | * Unlink a node from it's current context, the node is not freed |
| 3389 | */ |
| 3390 | void |
| 3391 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3392 | if (cur == NULL) { |
| 3393 | #ifdef DEBUG_TREE |
| 3394 | xmlGenericError(xmlGenericErrorContext, |
| 3395 | "xmlUnlinkNode : node == NULL\n"); |
| 3396 | #endif |
| 3397 | return; |
| 3398 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3399 | if (cur->type == XML_DTD_NODE) { |
| 3400 | xmlDocPtr doc; |
| 3401 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3402 | if (doc != NULL) { |
| 3403 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3404 | doc->intSubset = NULL; |
| 3405 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3406 | doc->extSubset = NULL; |
| 3407 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3408 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3409 | if (cur->parent != NULL) { |
| 3410 | xmlNodePtr parent; |
| 3411 | parent = cur->parent; |
| 3412 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3413 | if (parent->properties == (xmlAttrPtr) cur) |
| 3414 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3415 | } else { |
| 3416 | if (parent->children == cur) |
| 3417 | parent->children = cur->next; |
| 3418 | if (parent->last == cur) |
| 3419 | parent->last = cur->prev; |
| 3420 | } |
| 3421 | cur->parent = NULL; |
| 3422 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3423 | if (cur->next != NULL) |
| 3424 | cur->next->prev = cur->prev; |
| 3425 | if (cur->prev != NULL) |
| 3426 | cur->prev->next = cur->next; |
| 3427 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3430 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3431 | /** |
| 3432 | * xmlReplaceNode: |
| 3433 | * @old: the old node |
| 3434 | * @cur: the node |
| 3435 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 3436 | * Unlink the old node from its current context, prune the new one |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3437 | * 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] | 3438 | * first unlinked from its existing context. |
| 3439 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3440 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3441 | */ |
| 3442 | xmlNodePtr |
| 3443 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
| 3444 | if (old == NULL) { |
| 3445 | #ifdef DEBUG_TREE |
| 3446 | xmlGenericError(xmlGenericErrorContext, |
| 3447 | "xmlReplaceNode : old == NULL\n"); |
| 3448 | #endif |
| 3449 | return(NULL); |
| 3450 | } |
| 3451 | if (cur == NULL) { |
| 3452 | xmlUnlinkNode(old); |
| 3453 | return(old); |
| 3454 | } |
| 3455 | if (cur == old) { |
| 3456 | return(old); |
| 3457 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3458 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3459 | #ifdef DEBUG_TREE |
| 3460 | xmlGenericError(xmlGenericErrorContext, |
| 3461 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3462 | #endif |
| 3463 | return(old); |
| 3464 | } |
| 3465 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3466 | #ifdef DEBUG_TREE |
| 3467 | xmlGenericError(xmlGenericErrorContext, |
| 3468 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3469 | #endif |
| 3470 | return(old); |
| 3471 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3472 | xmlUnlinkNode(cur); |
| 3473 | cur->doc = old->doc; |
| 3474 | cur->parent = old->parent; |
| 3475 | cur->next = old->next; |
| 3476 | if (cur->next != NULL) |
| 3477 | cur->next->prev = cur; |
| 3478 | cur->prev = old->prev; |
| 3479 | if (cur->prev != NULL) |
| 3480 | cur->prev->next = cur; |
| 3481 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3482 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3483 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3484 | cur->parent->properties = ((xmlAttrPtr) cur); |
| 3485 | } else { |
| 3486 | if (cur->parent->children == old) |
| 3487 | cur->parent->children = cur; |
| 3488 | if (cur->parent->last == old) |
| 3489 | cur->parent->last = cur; |
| 3490 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3491 | } |
| 3492 | old->next = old->prev = NULL; |
| 3493 | old->parent = NULL; |
| 3494 | return(old); |
| 3495 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3496 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3497 | |
| 3498 | /************************************************************************ |
| 3499 | * * |
| 3500 | * Copy operations * |
| 3501 | * * |
| 3502 | ************************************************************************/ |
| 3503 | |
| 3504 | /** |
| 3505 | * xmlCopyNamespace: |
| 3506 | * @cur: the namespace |
| 3507 | * |
| 3508 | * Do a copy of the namespace. |
| 3509 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3510 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3511 | */ |
| 3512 | xmlNsPtr |
| 3513 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3514 | xmlNsPtr ret; |
| 3515 | |
| 3516 | if (cur == NULL) return(NULL); |
| 3517 | switch (cur->type) { |
| 3518 | case XML_LOCAL_NAMESPACE: |
| 3519 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3520 | break; |
| 3521 | default: |
| 3522 | #ifdef DEBUG_TREE |
| 3523 | xmlGenericError(xmlGenericErrorContext, |
| 3524 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3525 | #endif |
| 3526 | return(NULL); |
| 3527 | } |
| 3528 | return(ret); |
| 3529 | } |
| 3530 | |
| 3531 | /** |
| 3532 | * xmlCopyNamespaceList: |
| 3533 | * @cur: the first namespace |
| 3534 | * |
| 3535 | * Do a copy of an namespace list. |
| 3536 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3537 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3538 | */ |
| 3539 | xmlNsPtr |
| 3540 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3541 | xmlNsPtr ret = NULL; |
| 3542 | xmlNsPtr p = NULL,q; |
| 3543 | |
| 3544 | while (cur != NULL) { |
| 3545 | q = xmlCopyNamespace(cur); |
| 3546 | if (p == NULL) { |
| 3547 | ret = p = q; |
| 3548 | } else { |
| 3549 | p->next = q; |
| 3550 | p = q; |
| 3551 | } |
| 3552 | cur = cur->next; |
| 3553 | } |
| 3554 | return(ret); |
| 3555 | } |
| 3556 | |
| 3557 | static xmlNodePtr |
| 3558 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 3559 | /** |
| 3560 | * xmlCopyProp: |
| 3561 | * @target: the element where the attribute will be grafted |
| 3562 | * @cur: the attribute |
| 3563 | * |
| 3564 | * Do a copy of the attribute. |
| 3565 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3566 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3567 | */ |
| 3568 | xmlAttrPtr |
| 3569 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 3570 | xmlAttrPtr ret; |
| 3571 | |
| 3572 | if (cur == NULL) return(NULL); |
| 3573 | if (target != NULL) |
| 3574 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
| 3575 | else if (cur->parent != NULL) |
| 3576 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 3577 | else if (cur->children != NULL) |
| 3578 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 3579 | else |
| 3580 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 3581 | if (ret == NULL) return(NULL); |
| 3582 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3583 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3584 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3585 | xmlNsPtr ns; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3586 | |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3587 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3588 | if (ns == NULL) { |
| 3589 | /* |
| 3590 | * Humm, we are copying an element whose namespace is defined |
| 3591 | * out of the new tree scope. Search it in the original tree |
| 3592 | * and add it at the top of the new tree |
| 3593 | */ |
| 3594 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 3595 | if (ns != NULL) { |
| 3596 | xmlNodePtr root = target; |
| 3597 | xmlNodePtr pred = NULL; |
| 3598 | |
| 3599 | while (root->parent != NULL) { |
| 3600 | pred = root; |
| 3601 | root = root->parent; |
| 3602 | } |
| 3603 | if (root == (xmlNodePtr) target->doc) { |
| 3604 | /* correct possibly cycling above the document elt */ |
| 3605 | root = pred; |
| 3606 | } |
| 3607 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 3608 | } |
| 3609 | } else { |
| 3610 | /* |
| 3611 | * we have to find something appropriate here since |
| 3612 | * we cant be sure, that the namespce we found is identified |
| 3613 | * by the prefix |
| 3614 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3615 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3616 | /* this is the nice case */ |
| 3617 | ret->ns = ns; |
| 3618 | } else { |
| 3619 | /* |
| 3620 | * we are in trouble: we need a new reconcilied namespace. |
| 3621 | * This is expensive |
| 3622 | */ |
| 3623 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 3624 | } |
| 3625 | } |
| 3626 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3627 | } else |
| 3628 | ret->ns = NULL; |
| 3629 | |
| 3630 | if (cur->children != NULL) { |
| 3631 | xmlNodePtr tmp; |
| 3632 | |
| 3633 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 3634 | ret->last = NULL; |
| 3635 | tmp = ret->children; |
| 3636 | while (tmp != NULL) { |
| 3637 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 3638 | if (tmp->next == NULL) |
| 3639 | ret->last = tmp; |
| 3640 | tmp = tmp->next; |
| 3641 | } |
| 3642 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3643 | /* |
| 3644 | * Try to handle IDs |
| 3645 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 3646 | if ((target!= NULL) && (cur!= NULL) && |
| 3647 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3648 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 3649 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 3650 | xmlChar *id; |
| 3651 | |
| 3652 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 3653 | if (id != NULL) { |
| 3654 | xmlAddID(NULL, target->doc, id, ret); |
| 3655 | xmlFree(id); |
| 3656 | } |
| 3657 | } |
| 3658 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3659 | return(ret); |
| 3660 | } |
| 3661 | |
| 3662 | /** |
| 3663 | * xmlCopyPropList: |
| 3664 | * @target: the element where the attributes will be grafted |
| 3665 | * @cur: the first attribute |
| 3666 | * |
| 3667 | * Do a copy of an attribute list. |
| 3668 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3669 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3670 | */ |
| 3671 | xmlAttrPtr |
| 3672 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 3673 | xmlAttrPtr ret = NULL; |
| 3674 | xmlAttrPtr p = NULL,q; |
| 3675 | |
| 3676 | while (cur != NULL) { |
| 3677 | q = xmlCopyProp(target, cur); |
| 3678 | if (p == NULL) { |
| 3679 | ret = p = q; |
| 3680 | } else { |
| 3681 | p->next = q; |
| 3682 | q->prev = p; |
| 3683 | p = q; |
| 3684 | } |
| 3685 | cur = cur->next; |
| 3686 | } |
| 3687 | return(ret); |
| 3688 | } |
| 3689 | |
| 3690 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3691 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3692 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3693 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3694 | * tricky reason: namespaces. Doing a direct copy of a node |
| 3695 | * say RPM:Copyright without changing the namespace pointer to |
| 3696 | * something else can produce stale links. One way to do it is |
| 3697 | * to keep a reference counter but this doesn't work as soon |
| 3698 | * as one move the element or the subtree out of the scope of |
| 3699 | * the existing namespace. The actual solution seems to add |
| 3700 | * a copy of the namespace at the top of the copied tree if |
| 3701 | * not available in the subtree. |
| 3702 | * Hence two functions, the public front-end call the inner ones |
| 3703 | */ |
| 3704 | |
| 3705 | static xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3706 | xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3707 | int recursive) { |
| 3708 | xmlNodePtr ret; |
| 3709 | |
| 3710 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3711 | switch (node->type) { |
| 3712 | case XML_TEXT_NODE: |
| 3713 | case XML_CDATA_SECTION_NODE: |
| 3714 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 3715 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3716 | case XML_ENTITY_REF_NODE: |
| 3717 | case XML_ENTITY_NODE: |
| 3718 | case XML_PI_NODE: |
| 3719 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3720 | case XML_XINCLUDE_START: |
| 3721 | case XML_XINCLUDE_END: |
| 3722 | break; |
| 3723 | case XML_ATTRIBUTE_NODE: |
| 3724 | return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node)); |
| 3725 | case XML_NAMESPACE_DECL: |
| 3726 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
| 3727 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3728 | case XML_DOCUMENT_NODE: |
| 3729 | case XML_HTML_DOCUMENT_NODE: |
| 3730 | #ifdef LIBXML_DOCB_ENABLED |
| 3731 | case XML_DOCB_DOCUMENT_NODE: |
| 3732 | #endif |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3733 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3734 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, recursive)); |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3735 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3736 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3737 | case XML_NOTATION_NODE: |
| 3738 | case XML_DTD_NODE: |
| 3739 | case XML_ELEMENT_DECL: |
| 3740 | case XML_ATTRIBUTE_DECL: |
| 3741 | case XML_ENTITY_DECL: |
| 3742 | return(NULL); |
| 3743 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3744 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3745 | /* |
| 3746 | * Allocate a new node and fill the fields. |
| 3747 | */ |
| 3748 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 3749 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 3750 | xmlTreeErrMemory("copying node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3751 | return(NULL); |
| 3752 | } |
| 3753 | memset(ret, 0, sizeof(xmlNode)); |
| 3754 | ret->type = node->type; |
| 3755 | |
| 3756 | ret->doc = doc; |
| 3757 | ret->parent = parent; |
| 3758 | if (node->name == xmlStringText) |
| 3759 | ret->name = xmlStringText; |
| 3760 | else if (node->name == xmlStringTextNoenc) |
| 3761 | ret->name = xmlStringTextNoenc; |
| 3762 | else if (node->name == xmlStringComment) |
| 3763 | ret->name = xmlStringComment; |
| 3764 | else if (node->name != NULL) |
| 3765 | ret->name = xmlStrdup(node->name); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3766 | if ((node->type != XML_ELEMENT_NODE) && |
| 3767 | (node->content != NULL) && |
| 3768 | (node->type != XML_ENTITY_REF_NODE) && |
| 3769 | (node->type != XML_XINCLUDE_END) && |
| 3770 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3771 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3772 | }else{ |
| 3773 | if (node->type == XML_ELEMENT_NODE) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 3774 | ret->line = node->line; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3775 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3776 | if (parent != NULL) { |
| 3777 | xmlNodePtr tmp; |
| 3778 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3779 | /* |
| 3780 | * this is a tricky part for the node register thing: |
| 3781 | * in case ret does get coalesced in xmlAddChild |
| 3782 | * the deregister-node callback is called; so we register ret now already |
| 3783 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3784 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3785 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 3786 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3787 | tmp = xmlAddChild(parent, ret); |
| 3788 | /* node could have coalesced */ |
| 3789 | if (tmp != ret) |
| 3790 | return(tmp); |
| 3791 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3792 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3793 | if (!recursive) |
| 3794 | goto out; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3795 | if (node->nsDef != NULL) |
| 3796 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 3797 | |
| 3798 | if (node->ns != NULL) { |
| 3799 | xmlNsPtr ns; |
| 3800 | |
| 3801 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 3802 | if (ns == NULL) { |
| 3803 | /* |
| 3804 | * Humm, we are copying an element whose namespace is defined |
| 3805 | * out of the new tree scope. Search it in the original tree |
| 3806 | * and add it at the top of the new tree |
| 3807 | */ |
| 3808 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 3809 | if (ns != NULL) { |
| 3810 | xmlNodePtr root = ret; |
| 3811 | |
| 3812 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 3813 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3814 | } |
| 3815 | } else { |
| 3816 | /* |
| 3817 | * reference the existing namespace definition in our own tree. |
| 3818 | */ |
| 3819 | ret->ns = ns; |
| 3820 | } |
| 3821 | } |
| 3822 | if (node->properties != NULL) |
| 3823 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3824 | if (node->type == XML_ENTITY_REF_NODE) { |
| 3825 | if ((doc == NULL) || (node->doc != doc)) { |
| 3826 | /* |
| 3827 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3828 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3829 | * we cannot keep the reference. Try to find it in the |
| 3830 | * target document. |
| 3831 | */ |
| 3832 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 3833 | } else { |
| 3834 | ret->children = node->children; |
| 3835 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3836 | ret->last = ret->children; |
| 3837 | } else if (node->children != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3838 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3839 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 3840 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3841 | |
| 3842 | out: |
| 3843 | /* if parent != NULL we already registered the node above */ |
| 3844 | if (parent == NULL && xmlRegisterNodeDefaultValue) |
| 3845 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3846 | return(ret); |
| 3847 | } |
| 3848 | |
| 3849 | static xmlNodePtr |
| 3850 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 3851 | xmlNodePtr ret = NULL; |
| 3852 | xmlNodePtr p = NULL,q; |
| 3853 | |
| 3854 | while (node != NULL) { |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3855 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3856 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 3857 | if (doc == NULL) { |
| 3858 | node = node->next; |
| 3859 | continue; |
| 3860 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3861 | if (doc->intSubset == NULL) { |
| 3862 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
| 3863 | q->doc = doc; |
| 3864 | q->parent = parent; |
| 3865 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3866 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3867 | } else { |
| 3868 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3869 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3870 | } |
| 3871 | } else |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3872 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3873 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3874 | if (ret == NULL) { |
| 3875 | q->prev = NULL; |
| 3876 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3877 | } else if (p != q) { |
| 3878 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3879 | p->next = q; |
| 3880 | q->prev = p; |
| 3881 | p = q; |
| 3882 | } |
| 3883 | node = node->next; |
| 3884 | } |
| 3885 | return(ret); |
| 3886 | } |
| 3887 | |
| 3888 | /** |
| 3889 | * xmlCopyNode: |
| 3890 | * @node: the node |
| 3891 | * @recursive: if 1 do a recursive copy. |
| 3892 | * |
| 3893 | * Do a copy of the node. |
| 3894 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3895 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3896 | */ |
| 3897 | xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3898 | xmlCopyNode(const xmlNodePtr node, int recursive) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3899 | xmlNodePtr ret; |
| 3900 | |
| 3901 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 3902 | return(ret); |
| 3903 | } |
| 3904 | |
| 3905 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3906 | * xmlDocCopyNode: |
| 3907 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3908 | * @doc: the document |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3909 | * @recursive: if 1 do a recursive copy. |
| 3910 | * |
| 3911 | * Do a copy of the node to a given document. |
| 3912 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3913 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3914 | */ |
| 3915 | xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3916 | xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int recursive) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3917 | xmlNodePtr ret; |
| 3918 | |
| 3919 | ret = xmlStaticCopyNode(node, doc, NULL, recursive); |
| 3920 | return(ret); |
| 3921 | } |
| 3922 | |
| 3923 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3924 | * xmlCopyNodeList: |
| 3925 | * @node: the first node in the list. |
| 3926 | * |
| 3927 | * Do a recursive copy of the node list. |
| 3928 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3929 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3930 | */ |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3931 | xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3932 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 3933 | return(ret); |
| 3934 | } |
| 3935 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3936 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3937 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3938 | * xmlCopyDtd: |
| 3939 | * @dtd: the dtd |
| 3940 | * |
| 3941 | * Do a copy of the dtd. |
| 3942 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3943 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3944 | */ |
| 3945 | xmlDtdPtr |
| 3946 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 3947 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3948 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3949 | |
| 3950 | if (dtd == NULL) return(NULL); |
| 3951 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 3952 | if (ret == NULL) return(NULL); |
| 3953 | if (dtd->entities != NULL) |
| 3954 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 3955 | (xmlEntitiesTablePtr) dtd->entities); |
| 3956 | if (dtd->notations != NULL) |
| 3957 | ret->notations = (void *) xmlCopyNotationTable( |
| 3958 | (xmlNotationTablePtr) dtd->notations); |
| 3959 | if (dtd->elements != NULL) |
| 3960 | ret->elements = (void *) xmlCopyElementTable( |
| 3961 | (xmlElementTablePtr) dtd->elements); |
| 3962 | if (dtd->attributes != NULL) |
| 3963 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 3964 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3965 | if (dtd->pentities != NULL) |
| 3966 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 3967 | (xmlEntitiesTablePtr) dtd->pentities); |
| 3968 | |
| 3969 | cur = dtd->children; |
| 3970 | while (cur != NULL) { |
| 3971 | q = NULL; |
| 3972 | |
| 3973 | if (cur->type == XML_ENTITY_DECL) { |
| 3974 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 3975 | switch (tmp->etype) { |
| 3976 | case XML_INTERNAL_GENERAL_ENTITY: |
| 3977 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 3978 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 3979 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 3980 | break; |
| 3981 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 3982 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 3983 | q = (xmlNodePtr) |
| 3984 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 3985 | break; |
| 3986 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 3987 | break; |
| 3988 | } |
| 3989 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 3990 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 3991 | q = (xmlNodePtr) |
| 3992 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 3993 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 3994 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
| 3995 | q = (xmlNodePtr) |
| 3996 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 3997 | } else if (cur->type == XML_COMMENT_NODE) { |
| 3998 | q = xmlCopyNode(cur, 0); |
| 3999 | } |
| 4000 | |
| 4001 | if (q == NULL) { |
| 4002 | cur = cur->next; |
| 4003 | continue; |
| 4004 | } |
| 4005 | |
| 4006 | if (p == NULL) |
| 4007 | ret->children = q; |
| 4008 | else |
| 4009 | p->next = q; |
| 4010 | |
| 4011 | q->prev = p; |
| 4012 | q->parent = (xmlNodePtr) ret; |
| 4013 | q->next = NULL; |
| 4014 | ret->last = q; |
| 4015 | p = q; |
| 4016 | cur = cur->next; |
| 4017 | } |
| 4018 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4019 | return(ret); |
| 4020 | } |
| 4021 | |
| 4022 | /** |
| 4023 | * xmlCopyDoc: |
| 4024 | * @doc: the document |
| 4025 | * @recursive: if 1 do a recursive copy. |
| 4026 | * |
| 4027 | * 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] | 4028 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4029 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4030 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4031 | */ |
| 4032 | xmlDocPtr |
| 4033 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 4034 | xmlDocPtr ret; |
| 4035 | |
| 4036 | if (doc == NULL) return(NULL); |
| 4037 | ret = xmlNewDoc(doc->version); |
| 4038 | if (ret == NULL) return(NULL); |
| 4039 | if (doc->name != NULL) |
| 4040 | ret->name = xmlMemStrdup(doc->name); |
| 4041 | if (doc->encoding != NULL) |
| 4042 | ret->encoding = xmlStrdup(doc->encoding); |
| 4043 | ret->charset = doc->charset; |
| 4044 | ret->compression = doc->compression; |
| 4045 | ret->standalone = doc->standalone; |
| 4046 | if (!recursive) return(ret); |
| 4047 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4048 | ret->last = NULL; |
| 4049 | ret->children = NULL; |
| 4050 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4051 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4052 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4053 | ret->intSubset->parent = ret; |
| 4054 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4055 | if (doc->oldNs != NULL) |
| 4056 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 4057 | if (doc->children != NULL) { |
| 4058 | xmlNodePtr tmp; |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4059 | |
| 4060 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 4061 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4062 | ret->last = NULL; |
| 4063 | tmp = ret->children; |
| 4064 | while (tmp != NULL) { |
| 4065 | if (tmp->next == NULL) |
| 4066 | ret->last = tmp; |
| 4067 | tmp = tmp->next; |
| 4068 | } |
| 4069 | } |
| 4070 | return(ret); |
| 4071 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4072 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4073 | |
| 4074 | /************************************************************************ |
| 4075 | * * |
| 4076 | * Content access functions * |
| 4077 | * * |
| 4078 | ************************************************************************/ |
| 4079 | |
| 4080 | /** |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4081 | * xmlGetLineNo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4082 | * @node: valid node |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4083 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 4084 | * Get line number of @node. This requires activation of this option |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4085 | * before invoking the parser by calling xmlLineNumbersDefault(1) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4086 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4087 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4088 | */ |
| 4089 | long |
| 4090 | xmlGetLineNo(xmlNodePtr node) |
| 4091 | { |
| 4092 | long result = -1; |
| 4093 | |
| 4094 | if (!node) |
| 4095 | return result; |
| 4096 | if (node->type == XML_ELEMENT_NODE) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 4097 | result = (long) node->line; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4098 | else if ((node->prev != NULL) && |
| 4099 | ((node->prev->type == XML_ELEMENT_NODE) || |
| 4100 | (node->prev->type == XML_TEXT_NODE))) |
| 4101 | result = xmlGetLineNo(node->prev); |
| 4102 | else if ((node->parent != NULL) && |
| 4103 | ((node->parent->type == XML_ELEMENT_NODE) || |
| 4104 | (node->parent->type == XML_TEXT_NODE))) |
| 4105 | result = xmlGetLineNo(node->parent); |
| 4106 | |
| 4107 | return result; |
| 4108 | } |
| 4109 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4110 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4111 | /** |
| 4112 | * xmlGetNodePath: |
| 4113 | * @node: a node |
| 4114 | * |
| 4115 | * Build a structure based Path for the given node |
| 4116 | * |
| 4117 | * Returns the new path or NULL in case of error. The caller must free |
| 4118 | * the returned string |
| 4119 | */ |
| 4120 | xmlChar * |
| 4121 | xmlGetNodePath(xmlNodePtr node) |
| 4122 | { |
| 4123 | xmlNodePtr cur, tmp, next; |
| 4124 | xmlChar *buffer = NULL, *temp; |
| 4125 | size_t buf_len; |
| 4126 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4127 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4128 | const char *name; |
| 4129 | char nametemp[100]; |
| 4130 | int occur = 0; |
| 4131 | |
| 4132 | if (node == NULL) |
| 4133 | return (NULL); |
| 4134 | |
| 4135 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4136 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4137 | if (buffer == NULL) { |
| 4138 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4139 | return (NULL); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4140 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4141 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4142 | if (buf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4143 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4144 | xmlFree(buffer); |
| 4145 | return (NULL); |
| 4146 | } |
| 4147 | |
| 4148 | buffer[0] = 0; |
| 4149 | cur = node; |
| 4150 | do { |
| 4151 | name = ""; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4152 | sep = "?"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4153 | occur = 0; |
| 4154 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4155 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4156 | if (buffer[0] == '/') |
| 4157 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4158 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4159 | next = NULL; |
| 4160 | } else if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4161 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4162 | name = (const char *) cur->name; |
| 4163 | if (cur->ns) { |
William M. Brack | 84d83e3 | 2003-12-23 03:45:17 +0000 | [diff] [blame] | 4164 | if (cur->ns->prefix != NULL) |
| 4165 | snprintf(nametemp, sizeof(nametemp) - 1, |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4166 | "%s:%s", cur->ns->prefix, cur->name); |
William M. Brack | 84d83e3 | 2003-12-23 03:45:17 +0000 | [diff] [blame] | 4167 | else |
| 4168 | snprintf(nametemp, sizeof(nametemp) - 1, |
| 4169 | "%s", cur->name); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4170 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4171 | name = nametemp; |
| 4172 | } |
| 4173 | next = cur->parent; |
| 4174 | |
| 4175 | /* |
| 4176 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4177 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4178 | */ |
| 4179 | tmp = cur->prev; |
| 4180 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4181 | if ((tmp->type == XML_ELEMENT_NODE) && |
| 4182 | (xmlStrEqual(cur->name, tmp->name))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4183 | occur++; |
| 4184 | tmp = tmp->prev; |
| 4185 | } |
| 4186 | if (occur == 0) { |
| 4187 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4188 | while (tmp != NULL && occur == 0) { |
| 4189 | if ((tmp->type == XML_ELEMENT_NODE) && |
| 4190 | (xmlStrEqual(cur->name, tmp->name))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4191 | occur++; |
| 4192 | tmp = tmp->next; |
| 4193 | } |
| 4194 | if (occur != 0) |
| 4195 | occur = 1; |
| 4196 | } else |
| 4197 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4198 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4199 | sep = "/"; |
| 4200 | name = "comment()"; |
| 4201 | next = cur->parent; |
| 4202 | |
| 4203 | /* |
| 4204 | * Thumbler index computation |
| 4205 | */ |
| 4206 | tmp = cur->prev; |
| 4207 | while (tmp != NULL) { |
| 4208 | if (tmp->type == XML_COMMENT_NODE) |
| 4209 | occur++; |
| 4210 | tmp = tmp->prev; |
| 4211 | } |
| 4212 | if (occur == 0) { |
| 4213 | tmp = cur->next; |
| 4214 | while (tmp != NULL && occur == 0) { |
| 4215 | if (tmp->type == XML_COMMENT_NODE) |
| 4216 | occur++; |
| 4217 | tmp = tmp->next; |
| 4218 | } |
| 4219 | if (occur != 0) |
| 4220 | occur = 1; |
| 4221 | } else |
| 4222 | occur++; |
| 4223 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4224 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4225 | sep = "/"; |
| 4226 | name = "text()"; |
| 4227 | next = cur->parent; |
| 4228 | |
| 4229 | /* |
| 4230 | * Thumbler index computation |
| 4231 | */ |
| 4232 | tmp = cur->prev; |
| 4233 | while (tmp != NULL) { |
| 4234 | if ((cur->type == XML_TEXT_NODE) || |
| 4235 | (cur->type == XML_CDATA_SECTION_NODE)) |
| 4236 | occur++; |
| 4237 | tmp = tmp->prev; |
| 4238 | } |
| 4239 | if (occur == 0) { |
| 4240 | tmp = cur->next; |
| 4241 | while (tmp != NULL && occur == 0) { |
| 4242 | if ((cur->type == XML_TEXT_NODE) || |
| 4243 | (cur->type == XML_CDATA_SECTION_NODE)) |
| 4244 | occur++; |
| 4245 | tmp = tmp->next; |
| 4246 | } |
| 4247 | if (occur != 0) |
| 4248 | occur = 1; |
| 4249 | } else |
| 4250 | occur++; |
| 4251 | } else if (cur->type == XML_PI_NODE) { |
| 4252 | sep = "/"; |
| 4253 | snprintf(nametemp, sizeof(nametemp) - 1, |
| 4254 | "processing-instruction('%s')", cur->name); |
| 4255 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4256 | name = nametemp; |
| 4257 | |
| 4258 | next = cur->parent; |
| 4259 | |
| 4260 | /* |
| 4261 | * Thumbler index computation |
| 4262 | */ |
| 4263 | tmp = cur->prev; |
| 4264 | while (tmp != NULL) { |
| 4265 | if ((tmp->type == XML_PI_NODE) && |
| 4266 | (xmlStrEqual(cur->name, tmp->name))) |
| 4267 | occur++; |
| 4268 | tmp = tmp->prev; |
| 4269 | } |
| 4270 | if (occur == 0) { |
| 4271 | tmp = cur->next; |
| 4272 | while (tmp != NULL && occur == 0) { |
| 4273 | if ((tmp->type == XML_PI_NODE) && |
| 4274 | (xmlStrEqual(cur->name, tmp->name))) |
| 4275 | occur++; |
| 4276 | tmp = tmp->next; |
| 4277 | } |
| 4278 | if (occur != 0) |
| 4279 | occur = 1; |
| 4280 | } else |
| 4281 | occur++; |
| 4282 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4283 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4284 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4285 | name = (const char *) (((xmlAttrPtr) cur)->name); |
| 4286 | next = ((xmlAttrPtr) cur)->parent; |
| 4287 | } else { |
| 4288 | next = cur->parent; |
| 4289 | } |
| 4290 | |
| 4291 | /* |
| 4292 | * Make sure there is enough room |
| 4293 | */ |
| 4294 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4295 | buf_len = |
| 4296 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4297 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4298 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4299 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4300 | xmlFree(buf); |
| 4301 | xmlFree(buffer); |
| 4302 | return (NULL); |
| 4303 | } |
| 4304 | buffer = temp; |
| 4305 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4306 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4307 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4308 | xmlFree(buf); |
| 4309 | xmlFree(buffer); |
| 4310 | return (NULL); |
| 4311 | } |
| 4312 | buf = temp; |
| 4313 | } |
| 4314 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4315 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4316 | sep, name, (char *) buffer); |
| 4317 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4318 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4319 | sep, name, occur, (char *) buffer); |
| 4320 | snprintf((char *) buffer, buf_len, "%s", buf); |
| 4321 | cur = next; |
| 4322 | } while (cur != NULL); |
| 4323 | xmlFree(buf); |
| 4324 | return (buffer); |
| 4325 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4326 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4327 | |
| 4328 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4329 | * xmlDocGetRootElement: |
| 4330 | * @doc: the document |
| 4331 | * |
| 4332 | * Get the root element of the document (doc->children is a list |
| 4333 | * containing possibly comments, PIs, etc ...). |
| 4334 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4335 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4336 | */ |
| 4337 | xmlNodePtr |
| 4338 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 4339 | xmlNodePtr ret; |
| 4340 | |
| 4341 | if (doc == NULL) return(NULL); |
| 4342 | ret = doc->children; |
| 4343 | while (ret != NULL) { |
| 4344 | if (ret->type == XML_ELEMENT_NODE) |
| 4345 | return(ret); |
| 4346 | ret = ret->next; |
| 4347 | } |
| 4348 | return(ret); |
| 4349 | } |
| 4350 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4351 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4352 | /** |
| 4353 | * xmlDocSetRootElement: |
| 4354 | * @doc: the document |
| 4355 | * @root: the new document root element |
| 4356 | * |
| 4357 | * Set the root element of the document (doc->children is a list |
| 4358 | * containing possibly comments, PIs, etc ...). |
| 4359 | * |
| 4360 | * Returns the old root element if any was found |
| 4361 | */ |
| 4362 | xmlNodePtr |
| 4363 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4364 | xmlNodePtr old = NULL; |
| 4365 | |
| 4366 | if (doc == NULL) return(NULL); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4367 | if (root == NULL) |
| 4368 | return(NULL); |
| 4369 | xmlUnlinkNode(root); |
| 4370 | root->doc = doc; |
| 4371 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4372 | old = doc->children; |
| 4373 | while (old != NULL) { |
| 4374 | if (old->type == XML_ELEMENT_NODE) |
| 4375 | break; |
| 4376 | old = old->next; |
| 4377 | } |
| 4378 | if (old == NULL) { |
| 4379 | if (doc->children == NULL) { |
| 4380 | doc->children = root; |
| 4381 | doc->last = root; |
| 4382 | } else { |
| 4383 | xmlAddSibling(doc->children, root); |
| 4384 | } |
| 4385 | } else { |
| 4386 | xmlReplaceNode(old, root); |
| 4387 | } |
| 4388 | return(old); |
| 4389 | } |
| 4390 | |
| 4391 | /** |
| 4392 | * xmlNodeSetLang: |
| 4393 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4394 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4395 | * |
| 4396 | * Set the language of a node, i.e. the values of the xml:lang |
| 4397 | * attribute. |
| 4398 | */ |
| 4399 | void |
| 4400 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4401 | xmlNsPtr ns; |
| 4402 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4403 | if (cur == NULL) return; |
| 4404 | switch(cur->type) { |
| 4405 | case XML_TEXT_NODE: |
| 4406 | case XML_CDATA_SECTION_NODE: |
| 4407 | case XML_COMMENT_NODE: |
| 4408 | case XML_DOCUMENT_NODE: |
| 4409 | case XML_DOCUMENT_TYPE_NODE: |
| 4410 | case XML_DOCUMENT_FRAG_NODE: |
| 4411 | case XML_NOTATION_NODE: |
| 4412 | case XML_HTML_DOCUMENT_NODE: |
| 4413 | case XML_DTD_NODE: |
| 4414 | case XML_ELEMENT_DECL: |
| 4415 | case XML_ATTRIBUTE_DECL: |
| 4416 | case XML_ENTITY_DECL: |
| 4417 | case XML_PI_NODE: |
| 4418 | case XML_ENTITY_REF_NODE: |
| 4419 | case XML_ENTITY_NODE: |
| 4420 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4421 | #ifdef LIBXML_DOCB_ENABLED |
| 4422 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4423 | #endif |
| 4424 | case XML_XINCLUDE_START: |
| 4425 | case XML_XINCLUDE_END: |
| 4426 | return; |
| 4427 | case XML_ELEMENT_NODE: |
| 4428 | case XML_ATTRIBUTE_NODE: |
| 4429 | break; |
| 4430 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4431 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4432 | if (ns == NULL) |
| 4433 | return; |
| 4434 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4435 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4436 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4437 | |
| 4438 | /** |
| 4439 | * xmlNodeGetLang: |
| 4440 | * @cur: the node being checked |
| 4441 | * |
| 4442 | * Searches the language of a node, i.e. the values of the xml:lang |
| 4443 | * attribute or the one carried by the nearest ancestor. |
| 4444 | * |
| 4445 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4446 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4447 | */ |
| 4448 | xmlChar * |
| 4449 | xmlNodeGetLang(xmlNodePtr cur) { |
| 4450 | xmlChar *lang; |
| 4451 | |
| 4452 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 4453 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4454 | if (lang != NULL) |
| 4455 | return(lang); |
| 4456 | cur = cur->parent; |
| 4457 | } |
| 4458 | return(NULL); |
| 4459 | } |
| 4460 | |
| 4461 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4462 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4463 | /** |
| 4464 | * xmlNodeSetSpacePreserve: |
| 4465 | * @cur: the node being changed |
| 4466 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 4467 | * |
| 4468 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 4469 | * value of the xml:space attribute. |
| 4470 | */ |
| 4471 | void |
| 4472 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4473 | xmlNsPtr ns; |
| 4474 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4475 | if (cur == NULL) return; |
| 4476 | switch(cur->type) { |
| 4477 | case XML_TEXT_NODE: |
| 4478 | case XML_CDATA_SECTION_NODE: |
| 4479 | case XML_COMMENT_NODE: |
| 4480 | case XML_DOCUMENT_NODE: |
| 4481 | case XML_DOCUMENT_TYPE_NODE: |
| 4482 | case XML_DOCUMENT_FRAG_NODE: |
| 4483 | case XML_NOTATION_NODE: |
| 4484 | case XML_HTML_DOCUMENT_NODE: |
| 4485 | case XML_DTD_NODE: |
| 4486 | case XML_ELEMENT_DECL: |
| 4487 | case XML_ATTRIBUTE_DECL: |
| 4488 | case XML_ENTITY_DECL: |
| 4489 | case XML_PI_NODE: |
| 4490 | case XML_ENTITY_REF_NODE: |
| 4491 | case XML_ENTITY_NODE: |
| 4492 | case XML_NAMESPACE_DECL: |
| 4493 | case XML_XINCLUDE_START: |
| 4494 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4495 | #ifdef LIBXML_DOCB_ENABLED |
| 4496 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4497 | #endif |
| 4498 | return; |
| 4499 | case XML_ELEMENT_NODE: |
| 4500 | case XML_ATTRIBUTE_NODE: |
| 4501 | break; |
| 4502 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4503 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4504 | if (ns == NULL) |
| 4505 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4506 | switch (val) { |
| 4507 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4508 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4509 | break; |
| 4510 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4511 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4512 | break; |
| 4513 | } |
| 4514 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4515 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4516 | |
| 4517 | /** |
| 4518 | * xmlNodeGetSpacePreserve: |
| 4519 | * @cur: the node being checked |
| 4520 | * |
| 4521 | * Searches the space preserving behaviour of a node, i.e. the values |
| 4522 | * of the xml:space attribute or the one carried by the nearest |
| 4523 | * ancestor. |
| 4524 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4525 | * 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] | 4526 | */ |
| 4527 | int |
| 4528 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 4529 | xmlChar *space; |
| 4530 | |
| 4531 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4532 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4533 | if (space != NULL) { |
| 4534 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 4535 | xmlFree(space); |
| 4536 | return(1); |
| 4537 | } |
| 4538 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 4539 | xmlFree(space); |
| 4540 | return(0); |
| 4541 | } |
| 4542 | xmlFree(space); |
| 4543 | } |
| 4544 | cur = cur->parent; |
| 4545 | } |
| 4546 | return(-1); |
| 4547 | } |
| 4548 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4549 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4550 | /** |
| 4551 | * xmlNodeSetName: |
| 4552 | * @cur: the node being changed |
| 4553 | * @name: the new tag name |
| 4554 | * |
| 4555 | * Set (or reset) the name of a node. |
| 4556 | */ |
| 4557 | void |
| 4558 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
| 4559 | if (cur == NULL) return; |
| 4560 | if (name == NULL) return; |
| 4561 | switch(cur->type) { |
| 4562 | case XML_TEXT_NODE: |
| 4563 | case XML_CDATA_SECTION_NODE: |
| 4564 | case XML_COMMENT_NODE: |
| 4565 | case XML_DOCUMENT_TYPE_NODE: |
| 4566 | case XML_DOCUMENT_FRAG_NODE: |
| 4567 | case XML_NOTATION_NODE: |
| 4568 | case XML_HTML_DOCUMENT_NODE: |
| 4569 | case XML_NAMESPACE_DECL: |
| 4570 | case XML_XINCLUDE_START: |
| 4571 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4572 | #ifdef LIBXML_DOCB_ENABLED |
| 4573 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4574 | #endif |
| 4575 | return; |
| 4576 | case XML_ELEMENT_NODE: |
| 4577 | case XML_ATTRIBUTE_NODE: |
| 4578 | case XML_PI_NODE: |
| 4579 | case XML_ENTITY_REF_NODE: |
| 4580 | case XML_ENTITY_NODE: |
| 4581 | case XML_DTD_NODE: |
| 4582 | case XML_DOCUMENT_NODE: |
| 4583 | case XML_ELEMENT_DECL: |
| 4584 | case XML_ATTRIBUTE_DECL: |
| 4585 | case XML_ENTITY_DECL: |
| 4586 | break; |
| 4587 | } |
| 4588 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 4589 | cur->name = xmlStrdup(name); |
| 4590 | } |
| 4591 | |
| 4592 | /** |
| 4593 | * xmlNodeSetBase: |
| 4594 | * @cur: the node being changed |
| 4595 | * @uri: the new base URI |
| 4596 | * |
| 4597 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 4598 | * xml:base attribute. |
| 4599 | */ |
| 4600 | void |
Daniel Veillard | f85ce8e | 2003-09-22 10:24:45 +0000 | [diff] [blame] | 4601 | xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4602 | xmlNsPtr ns; |
| 4603 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4604 | if (cur == NULL) return; |
| 4605 | switch(cur->type) { |
| 4606 | case XML_TEXT_NODE: |
| 4607 | case XML_CDATA_SECTION_NODE: |
| 4608 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4609 | case XML_DOCUMENT_TYPE_NODE: |
| 4610 | case XML_DOCUMENT_FRAG_NODE: |
| 4611 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4612 | case XML_DTD_NODE: |
| 4613 | case XML_ELEMENT_DECL: |
| 4614 | case XML_ATTRIBUTE_DECL: |
| 4615 | case XML_ENTITY_DECL: |
| 4616 | case XML_PI_NODE: |
| 4617 | case XML_ENTITY_REF_NODE: |
| 4618 | case XML_ENTITY_NODE: |
| 4619 | case XML_NAMESPACE_DECL: |
| 4620 | case XML_XINCLUDE_START: |
| 4621 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4622 | return; |
| 4623 | case XML_ELEMENT_NODE: |
| 4624 | case XML_ATTRIBUTE_NODE: |
| 4625 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 4626 | case XML_DOCUMENT_NODE: |
| 4627 | #ifdef LIBXML_DOCB_ENABLED |
| 4628 | case XML_DOCB_DOCUMENT_NODE: |
| 4629 | #endif |
| 4630 | case XML_HTML_DOCUMENT_NODE: { |
| 4631 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 4632 | |
| 4633 | if (doc->URL != NULL) |
| 4634 | xmlFree((xmlChar *) doc->URL); |
| 4635 | if (uri == NULL) |
| 4636 | doc->URL = NULL; |
| 4637 | else |
| 4638 | doc->URL = xmlStrdup(uri); |
| 4639 | return; |
| 4640 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4641 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4642 | |
| 4643 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4644 | if (ns == NULL) |
| 4645 | return; |
| 4646 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4647 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4648 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4649 | |
| 4650 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4651 | * xmlNodeGetBase: |
| 4652 | * @doc: the document the node pertains to |
| 4653 | * @cur: the node being checked |
| 4654 | * |
| 4655 | * Searches for the BASE URL. The code should work on both XML |
| 4656 | * and HTML document even if base mechanisms are completely different. |
| 4657 | * It returns the base as defined in RFC 2396 sections |
| 4658 | * 5.1.1. Base URI within Document Content |
| 4659 | * and |
| 4660 | * 5.1.2. Base URI from the Encapsulating Entity |
| 4661 | * However it does not return the document base (5.1.3), use |
| 4662 | * xmlDocumentGetBase() for this |
| 4663 | * |
| 4664 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4665 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4666 | */ |
| 4667 | xmlChar * |
| 4668 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4669 | xmlChar *oldbase = NULL; |
| 4670 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4671 | |
| 4672 | if ((cur == NULL) && (doc == NULL)) |
| 4673 | return(NULL); |
| 4674 | if (doc == NULL) doc = cur->doc; |
| 4675 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 4676 | cur = doc->children; |
| 4677 | while ((cur != NULL) && (cur->name != NULL)) { |
| 4678 | if (cur->type != XML_ELEMENT_NODE) { |
| 4679 | cur = cur->next; |
| 4680 | continue; |
| 4681 | } |
| 4682 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 4683 | cur = cur->children; |
| 4684 | continue; |
| 4685 | } |
| 4686 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 4687 | cur = cur->children; |
| 4688 | continue; |
| 4689 | } |
| 4690 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 4691 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 4692 | } |
| 4693 | cur = cur->next; |
| 4694 | } |
| 4695 | return(NULL); |
| 4696 | } |
| 4697 | while (cur != NULL) { |
| 4698 | if (cur->type == XML_ENTITY_DECL) { |
| 4699 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 4700 | return(xmlStrdup(ent->URI)); |
| 4701 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4702 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4703 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4704 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4705 | if (oldbase != NULL) { |
| 4706 | newbase = xmlBuildURI(oldbase, base); |
| 4707 | if (newbase != NULL) { |
| 4708 | xmlFree(oldbase); |
| 4709 | xmlFree(base); |
| 4710 | oldbase = newbase; |
| 4711 | } else { |
| 4712 | xmlFree(oldbase); |
| 4713 | xmlFree(base); |
| 4714 | return(NULL); |
| 4715 | } |
| 4716 | } else { |
| 4717 | oldbase = base; |
| 4718 | } |
| 4719 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 4720 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 4721 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 4722 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4723 | } |
| 4724 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4725 | cur = cur->parent; |
| 4726 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4727 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 4728 | if (oldbase == NULL) |
| 4729 | return(xmlStrdup(doc->URL)); |
| 4730 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 4731 | xmlFree(oldbase); |
| 4732 | return(newbase); |
| 4733 | } |
| 4734 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | /** |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 4738 | * xmlNodeBufGetContent: |
| 4739 | * @buffer: a buffer |
| 4740 | * @cur: the node being read |
| 4741 | * |
| 4742 | * Read the value of a node @cur, this can be either the text carried |
| 4743 | * directly by this node if it's a TEXT node or the aggregate string |
| 4744 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 4745 | * Entity references are substituted. |
| 4746 | * Fills up the buffer @buffer with this value |
| 4747 | * |
| 4748 | * Returns 0 in case of success and -1 in case of error. |
| 4749 | */ |
| 4750 | int |
| 4751 | xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur) |
| 4752 | { |
| 4753 | if ((cur == NULL) || (buffer == NULL)) return(-1); |
| 4754 | switch (cur->type) { |
| 4755 | case XML_CDATA_SECTION_NODE: |
| 4756 | case XML_TEXT_NODE: |
| 4757 | xmlBufferCat(buffer, cur->content); |
| 4758 | break; |
| 4759 | case XML_DOCUMENT_FRAG_NODE: |
| 4760 | case XML_ELEMENT_NODE:{ |
| 4761 | xmlNodePtr tmp = cur; |
| 4762 | |
| 4763 | while (tmp != NULL) { |
| 4764 | switch (tmp->type) { |
| 4765 | case XML_CDATA_SECTION_NODE: |
| 4766 | case XML_TEXT_NODE: |
| 4767 | if (tmp->content != NULL) |
| 4768 | xmlBufferCat(buffer, tmp->content); |
| 4769 | break; |
| 4770 | case XML_ENTITY_REF_NODE: |
| 4771 | xmlNodeBufGetContent(buffer, tmp->children); |
| 4772 | break; |
| 4773 | default: |
| 4774 | break; |
| 4775 | } |
| 4776 | /* |
| 4777 | * Skip to next node |
| 4778 | */ |
| 4779 | if (tmp->children != NULL) { |
| 4780 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4781 | tmp = tmp->children; |
| 4782 | continue; |
| 4783 | } |
| 4784 | } |
| 4785 | if (tmp == cur) |
| 4786 | break; |
| 4787 | |
| 4788 | if (tmp->next != NULL) { |
| 4789 | tmp = tmp->next; |
| 4790 | continue; |
| 4791 | } |
| 4792 | |
| 4793 | do { |
| 4794 | tmp = tmp->parent; |
| 4795 | if (tmp == NULL) |
| 4796 | break; |
| 4797 | if (tmp == cur) { |
| 4798 | tmp = NULL; |
| 4799 | break; |
| 4800 | } |
| 4801 | if (tmp->next != NULL) { |
| 4802 | tmp = tmp->next; |
| 4803 | break; |
| 4804 | } |
| 4805 | } while (tmp != NULL); |
| 4806 | } |
| 4807 | break; |
| 4808 | } |
| 4809 | case XML_ATTRIBUTE_NODE:{ |
| 4810 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 4811 | xmlNodePtr tmp = attr->children; |
| 4812 | |
| 4813 | while (tmp != NULL) { |
| 4814 | if (tmp->type == XML_TEXT_NODE) |
| 4815 | xmlBufferCat(buffer, tmp->content); |
| 4816 | else |
| 4817 | xmlNodeBufGetContent(buffer, tmp); |
| 4818 | tmp = tmp->next; |
| 4819 | } |
| 4820 | break; |
| 4821 | } |
| 4822 | case XML_COMMENT_NODE: |
| 4823 | case XML_PI_NODE: |
| 4824 | xmlBufferCat(buffer, cur->content); |
| 4825 | break; |
| 4826 | case XML_ENTITY_REF_NODE:{ |
| 4827 | xmlEntityPtr ent; |
| 4828 | xmlNodePtr tmp; |
| 4829 | |
| 4830 | /* lookup entity declaration */ |
| 4831 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 4832 | if (ent == NULL) |
| 4833 | return(-1); |
| 4834 | |
| 4835 | /* an entity content can be any "well balanced chunk", |
| 4836 | * i.e. the result of the content [43] production: |
| 4837 | * http://www.w3.org/TR/REC-xml#NT-content |
| 4838 | * -> we iterate through child nodes and recursive call |
| 4839 | * xmlNodeGetContent() which handles all possible node types */ |
| 4840 | tmp = ent->children; |
| 4841 | while (tmp) { |
| 4842 | xmlNodeBufGetContent(buffer, tmp); |
| 4843 | tmp = tmp->next; |
| 4844 | } |
| 4845 | break; |
| 4846 | } |
| 4847 | case XML_ENTITY_NODE: |
| 4848 | case XML_DOCUMENT_TYPE_NODE: |
| 4849 | case XML_NOTATION_NODE: |
| 4850 | case XML_DTD_NODE: |
| 4851 | case XML_XINCLUDE_START: |
| 4852 | case XML_XINCLUDE_END: |
| 4853 | break; |
| 4854 | case XML_DOCUMENT_NODE: |
| 4855 | #ifdef LIBXML_DOCB_ENABLED |
| 4856 | case XML_DOCB_DOCUMENT_NODE: |
| 4857 | #endif |
| 4858 | case XML_HTML_DOCUMENT_NODE: |
| 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 | xmlNodeBufGetContent(buffer, cur); |
| 4865 | } |
| 4866 | cur = cur->next; |
| 4867 | } |
| 4868 | break; |
| 4869 | case XML_NAMESPACE_DECL: |
| 4870 | xmlBufferCat(buffer, ((xmlNsPtr) cur)->href); |
| 4871 | break; |
| 4872 | case XML_ELEMENT_DECL: |
| 4873 | case XML_ATTRIBUTE_DECL: |
| 4874 | case XML_ENTITY_DECL: |
| 4875 | break; |
| 4876 | } |
| 4877 | return(0); |
| 4878 | } |
| 4879 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4880 | * xmlNodeGetContent: |
| 4881 | * @cur: the node being read |
| 4882 | * |
| 4883 | * Read the value of a node, this can be either the text carried |
| 4884 | * directly by this node if it's a TEXT node or the aggregate string |
| 4885 | * 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] | 4886 | * Entity references are substituted. |
| 4887 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4888 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4889 | */ |
| 4890 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4891 | xmlNodeGetContent(xmlNodePtr cur) |
| 4892 | { |
| 4893 | if (cur == NULL) |
| 4894 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4895 | switch (cur->type) { |
| 4896 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4897 | case XML_ELEMENT_NODE:{ |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4898 | xmlBufferPtr buffer; |
| 4899 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4900 | |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 4901 | buffer = xmlBufferCreateSize(64); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4902 | if (buffer == NULL) |
| 4903 | return (NULL); |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 4904 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4905 | ret = buffer->content; |
| 4906 | buffer->content = NULL; |
| 4907 | xmlBufferFree(buffer); |
| 4908 | return (ret); |
| 4909 | } |
| 4910 | case XML_ATTRIBUTE_NODE:{ |
| 4911 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 4912 | |
| 4913 | if (attr->parent != NULL) |
| 4914 | return (xmlNodeListGetString |
| 4915 | (attr->parent->doc, attr->children, 1)); |
| 4916 | else |
| 4917 | return (xmlNodeListGetString(NULL, attr->children, 1)); |
| 4918 | break; |
| 4919 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4920 | case XML_COMMENT_NODE: |
| 4921 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4922 | if (cur->content != NULL) |
| 4923 | return (xmlStrdup(cur->content)); |
| 4924 | return (NULL); |
| 4925 | case XML_ENTITY_REF_NODE:{ |
| 4926 | xmlEntityPtr ent; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4927 | xmlBufferPtr buffer; |
| 4928 | xmlChar *ret; |
| 4929 | |
| 4930 | /* lookup entity declaration */ |
| 4931 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 4932 | if (ent == NULL) |
| 4933 | return (NULL); |
| 4934 | |
| 4935 | buffer = xmlBufferCreate(); |
| 4936 | if (buffer == NULL) |
| 4937 | return (NULL); |
| 4938 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 4939 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4940 | |
| 4941 | ret = buffer->content; |
| 4942 | buffer->content = NULL; |
| 4943 | xmlBufferFree(buffer); |
| 4944 | return (ret); |
| 4945 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4946 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4947 | case XML_DOCUMENT_TYPE_NODE: |
| 4948 | case XML_NOTATION_NODE: |
| 4949 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4950 | case XML_XINCLUDE_START: |
| 4951 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4952 | return (NULL); |
| 4953 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4954 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4955 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4956 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4957 | case XML_HTML_DOCUMENT_NODE: { |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 4958 | xmlBufferPtr buffer; |
| 4959 | xmlChar *ret; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4960 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 4961 | buffer = xmlBufferCreate(); |
| 4962 | if (buffer == NULL) |
| 4963 | return (NULL); |
| 4964 | |
| 4965 | xmlNodeBufGetContent(buffer, (xmlNodePtr) cur); |
| 4966 | |
| 4967 | ret = buffer->content; |
| 4968 | buffer->content = NULL; |
| 4969 | xmlBufferFree(buffer); |
| 4970 | return (ret); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4971 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4972 | case XML_NAMESPACE_DECL: { |
| 4973 | xmlChar *tmp; |
| 4974 | |
| 4975 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 4976 | return (tmp); |
| 4977 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4978 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4979 | /* TODO !!! */ |
| 4980 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4981 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4982 | /* TODO !!! */ |
| 4983 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4984 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4985 | /* TODO !!! */ |
| 4986 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4987 | case XML_CDATA_SECTION_NODE: |
| 4988 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4989 | if (cur->content != NULL) |
| 4990 | return (xmlStrdup(cur->content)); |
| 4991 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4992 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4993 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4994 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4995 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4996 | /** |
| 4997 | * xmlNodeSetContent: |
| 4998 | * @cur: the node being modified |
| 4999 | * @content: the new value of the content |
| 5000 | * |
| 5001 | * Replace the content of a node. |
| 5002 | */ |
| 5003 | void |
| 5004 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 5005 | if (cur == NULL) { |
| 5006 | #ifdef DEBUG_TREE |
| 5007 | xmlGenericError(xmlGenericErrorContext, |
| 5008 | "xmlNodeSetContent : node == NULL\n"); |
| 5009 | #endif |
| 5010 | return; |
| 5011 | } |
| 5012 | switch (cur->type) { |
| 5013 | case XML_DOCUMENT_FRAG_NODE: |
| 5014 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5015 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5016 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5017 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 5018 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5019 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5020 | case XML_TEXT_NODE: |
| 5021 | case XML_CDATA_SECTION_NODE: |
| 5022 | case XML_ENTITY_REF_NODE: |
| 5023 | case XML_ENTITY_NODE: |
| 5024 | case XML_PI_NODE: |
| 5025 | case XML_COMMENT_NODE: |
| 5026 | if (cur->content != NULL) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5027 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5028 | xmlDictOwns(cur->doc->dict, cur->content))) |
| 5029 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5030 | } |
| 5031 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5032 | cur->last = cur->children = NULL; |
| 5033 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5034 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5035 | } else |
| 5036 | cur->content = NULL; |
| 5037 | break; |
| 5038 | case XML_DOCUMENT_NODE: |
| 5039 | case XML_HTML_DOCUMENT_NODE: |
| 5040 | case XML_DOCUMENT_TYPE_NODE: |
| 5041 | case XML_XINCLUDE_START: |
| 5042 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5043 | #ifdef LIBXML_DOCB_ENABLED |
| 5044 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5045 | #endif |
| 5046 | break; |
| 5047 | case XML_NOTATION_NODE: |
| 5048 | break; |
| 5049 | case XML_DTD_NODE: |
| 5050 | break; |
| 5051 | case XML_NAMESPACE_DECL: |
| 5052 | break; |
| 5053 | case XML_ELEMENT_DECL: |
| 5054 | /* TODO !!! */ |
| 5055 | break; |
| 5056 | case XML_ATTRIBUTE_DECL: |
| 5057 | /* TODO !!! */ |
| 5058 | break; |
| 5059 | case XML_ENTITY_DECL: |
| 5060 | /* TODO !!! */ |
| 5061 | break; |
| 5062 | } |
| 5063 | } |
| 5064 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5065 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5066 | /** |
| 5067 | * xmlNodeSetContentLen: |
| 5068 | * @cur: the node being modified |
| 5069 | * @content: the new value of the content |
| 5070 | * @len: the size of @content |
| 5071 | * |
| 5072 | * Replace the content of a node. |
| 5073 | */ |
| 5074 | void |
| 5075 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5076 | if (cur == NULL) { |
| 5077 | #ifdef DEBUG_TREE |
| 5078 | xmlGenericError(xmlGenericErrorContext, |
| 5079 | "xmlNodeSetContentLen : node == NULL\n"); |
| 5080 | #endif |
| 5081 | return; |
| 5082 | } |
| 5083 | switch (cur->type) { |
| 5084 | case XML_DOCUMENT_FRAG_NODE: |
| 5085 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5086 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5087 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5088 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 5089 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5090 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5091 | case XML_TEXT_NODE: |
| 5092 | case XML_CDATA_SECTION_NODE: |
| 5093 | case XML_ENTITY_REF_NODE: |
| 5094 | case XML_ENTITY_NODE: |
| 5095 | case XML_PI_NODE: |
| 5096 | case XML_COMMENT_NODE: |
| 5097 | case XML_NOTATION_NODE: |
| 5098 | if (cur->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5099 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5100 | } |
| 5101 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5102 | cur->children = cur->last = NULL; |
| 5103 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5104 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5105 | } else |
| 5106 | cur->content = NULL; |
| 5107 | break; |
| 5108 | case XML_DOCUMENT_NODE: |
| 5109 | case XML_DTD_NODE: |
| 5110 | case XML_HTML_DOCUMENT_NODE: |
| 5111 | case XML_DOCUMENT_TYPE_NODE: |
| 5112 | case XML_NAMESPACE_DECL: |
| 5113 | case XML_XINCLUDE_START: |
| 5114 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5115 | #ifdef LIBXML_DOCB_ENABLED |
| 5116 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5117 | #endif |
| 5118 | break; |
| 5119 | case XML_ELEMENT_DECL: |
| 5120 | /* TODO !!! */ |
| 5121 | break; |
| 5122 | case XML_ATTRIBUTE_DECL: |
| 5123 | /* TODO !!! */ |
| 5124 | break; |
| 5125 | case XML_ENTITY_DECL: |
| 5126 | /* TODO !!! */ |
| 5127 | break; |
| 5128 | } |
| 5129 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5130 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5131 | |
| 5132 | /** |
| 5133 | * xmlNodeAddContentLen: |
| 5134 | * @cur: the node being modified |
| 5135 | * @content: extra content |
| 5136 | * @len: the size of @content |
| 5137 | * |
| 5138 | * Append the extra substring to the node content. |
| 5139 | */ |
| 5140 | void |
| 5141 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5142 | if (cur == NULL) { |
| 5143 | #ifdef DEBUG_TREE |
| 5144 | xmlGenericError(xmlGenericErrorContext, |
| 5145 | "xmlNodeAddContentLen : node == NULL\n"); |
| 5146 | #endif |
| 5147 | return; |
| 5148 | } |
| 5149 | if (len <= 0) return; |
| 5150 | switch (cur->type) { |
| 5151 | case XML_DOCUMENT_FRAG_NODE: |
| 5152 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5153 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5154 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 5155 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5156 | newNode = xmlNewTextLen(content, len); |
| 5157 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5158 | tmp = xmlAddChild(cur, newNode); |
| 5159 | if (tmp != newNode) |
| 5160 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5161 | if ((last != NULL) && (last->next == newNode)) { |
| 5162 | xmlTextMerge(last, newNode); |
| 5163 | } |
| 5164 | } |
| 5165 | break; |
| 5166 | } |
| 5167 | case XML_ATTRIBUTE_NODE: |
| 5168 | break; |
| 5169 | case XML_TEXT_NODE: |
| 5170 | case XML_CDATA_SECTION_NODE: |
| 5171 | case XML_ENTITY_REF_NODE: |
| 5172 | case XML_ENTITY_NODE: |
| 5173 | case XML_PI_NODE: |
| 5174 | case XML_COMMENT_NODE: |
| 5175 | case XML_NOTATION_NODE: |
| 5176 | if (content != NULL) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5177 | if ((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5178 | xmlDictOwns(cur->doc->dict, cur->content)) { |
| 5179 | cur->content = |
| 5180 | xmlStrncatNew(cur->content, content, len); |
| 5181 | break; |
| 5182 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5183 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5184 | } |
| 5185 | case XML_DOCUMENT_NODE: |
| 5186 | case XML_DTD_NODE: |
| 5187 | case XML_HTML_DOCUMENT_NODE: |
| 5188 | case XML_DOCUMENT_TYPE_NODE: |
| 5189 | case XML_NAMESPACE_DECL: |
| 5190 | case XML_XINCLUDE_START: |
| 5191 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5192 | #ifdef LIBXML_DOCB_ENABLED |
| 5193 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5194 | #endif |
| 5195 | break; |
| 5196 | case XML_ELEMENT_DECL: |
| 5197 | case XML_ATTRIBUTE_DECL: |
| 5198 | case XML_ENTITY_DECL: |
| 5199 | break; |
| 5200 | } |
| 5201 | } |
| 5202 | |
| 5203 | /** |
| 5204 | * xmlNodeAddContent: |
| 5205 | * @cur: the node being modified |
| 5206 | * @content: extra content |
| 5207 | * |
| 5208 | * Append the extra substring to the node content. |
| 5209 | */ |
| 5210 | void |
| 5211 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5212 | int len; |
| 5213 | |
| 5214 | if (cur == NULL) { |
| 5215 | #ifdef DEBUG_TREE |
| 5216 | xmlGenericError(xmlGenericErrorContext, |
| 5217 | "xmlNodeAddContent : node == NULL\n"); |
| 5218 | #endif |
| 5219 | return; |
| 5220 | } |
| 5221 | if (content == NULL) return; |
| 5222 | len = xmlStrlen(content); |
| 5223 | xmlNodeAddContentLen(cur, content, len); |
| 5224 | } |
| 5225 | |
| 5226 | /** |
| 5227 | * xmlTextMerge: |
| 5228 | * @first: the first text node |
| 5229 | * @second: the second text node being merged |
| 5230 | * |
| 5231 | * Merge two text nodes into one |
| 5232 | * Returns the first text node augmented |
| 5233 | */ |
| 5234 | xmlNodePtr |
| 5235 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5236 | if (first == NULL) return(second); |
| 5237 | if (second == NULL) return(first); |
| 5238 | if (first->type != XML_TEXT_NODE) return(first); |
| 5239 | if (second->type != XML_TEXT_NODE) return(first); |
| 5240 | if (second->name != first->name) |
| 5241 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5242 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5243 | xmlUnlinkNode(second); |
| 5244 | xmlFreeNode(second); |
| 5245 | return(first); |
| 5246 | } |
| 5247 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5248 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5249 | /** |
| 5250 | * xmlGetNsList: |
| 5251 | * @doc: the document |
| 5252 | * @node: the current node |
| 5253 | * |
| 5254 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5255 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5256 | * that need to be freed by the caller or NULL if no |
| 5257 | * namespace if defined |
| 5258 | */ |
| 5259 | xmlNsPtr * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5260 | xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) |
| 5261 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5262 | xmlNsPtr cur; |
| 5263 | xmlNsPtr *ret = NULL; |
| 5264 | int nbns = 0; |
| 5265 | int maxns = 10; |
| 5266 | int i; |
| 5267 | |
| 5268 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5269 | if (node->type == XML_ELEMENT_NODE) { |
| 5270 | cur = node->nsDef; |
| 5271 | while (cur != NULL) { |
| 5272 | if (ret == NULL) { |
| 5273 | ret = |
| 5274 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5275 | sizeof(xmlNsPtr)); |
| 5276 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5277 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5278 | return (NULL); |
| 5279 | } |
| 5280 | ret[nbns] = NULL; |
| 5281 | } |
| 5282 | for (i = 0; i < nbns; i++) { |
| 5283 | if ((cur->prefix == ret[i]->prefix) || |
| 5284 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5285 | break; |
| 5286 | } |
| 5287 | if (i >= nbns) { |
| 5288 | if (nbns >= maxns) { |
| 5289 | maxns *= 2; |
| 5290 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5291 | (maxns + |
| 5292 | 1) * |
| 5293 | sizeof(xmlNsPtr)); |
| 5294 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5295 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5296 | return (NULL); |
| 5297 | } |
| 5298 | } |
| 5299 | ret[nbns++] = cur; |
| 5300 | ret[nbns] = NULL; |
| 5301 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5302 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5303 | cur = cur->next; |
| 5304 | } |
| 5305 | } |
| 5306 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5307 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5308 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5309 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5310 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5311 | |
| 5312 | /** |
| 5313 | * xmlSearchNs: |
| 5314 | * @doc: the document |
| 5315 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5316 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5317 | * |
| 5318 | * Search a Ns registered under a given name space for a document. |
| 5319 | * recurse on the parents until it finds the defined namespace |
| 5320 | * or return NULL otherwise. |
| 5321 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5322 | * We don't allow to cross entities boundaries. If you don't declare |
| 5323 | * the namespace within those you will be in troubles !!! A warning |
| 5324 | * is generated to cover this case. |
| 5325 | * |
| 5326 | * Returns the namespace pointer or NULL. |
| 5327 | */ |
| 5328 | xmlNsPtr |
| 5329 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5330 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5331 | xmlNsPtr cur; |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5332 | xmlNodePtr orig = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5333 | |
| 5334 | if (node == NULL) return(NULL); |
| 5335 | if ((nameSpace != NULL) && |
| 5336 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5337 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5338 | /* |
| 5339 | * The XML-1.0 namespace is normally held on the root |
| 5340 | * element. In this case exceptionally create it on the |
| 5341 | * node element. |
| 5342 | */ |
| 5343 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5344 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5345 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5346 | return(NULL); |
| 5347 | } |
| 5348 | memset(cur, 0, sizeof(xmlNs)); |
| 5349 | cur->type = XML_LOCAL_NAMESPACE; |
| 5350 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5351 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5352 | cur->next = node->nsDef; |
| 5353 | node->nsDef = cur; |
| 5354 | return(cur); |
| 5355 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5356 | if (doc->oldNs == NULL) { |
| 5357 | /* |
| 5358 | * Allocate a new Namespace and fill the fields. |
| 5359 | */ |
| 5360 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5361 | if (doc->oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5362 | xmlTreeErrMemory("searching namespace"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5363 | return(NULL); |
| 5364 | } |
| 5365 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5366 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
| 5367 | |
| 5368 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5369 | doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5370 | } |
| 5371 | return(doc->oldNs); |
| 5372 | } |
| 5373 | while (node != NULL) { |
| 5374 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5375 | (node->type == XML_ENTITY_NODE) || |
| 5376 | (node->type == XML_ENTITY_DECL)) |
| 5377 | return(NULL); |
| 5378 | if (node->type == XML_ELEMENT_NODE) { |
| 5379 | cur = node->nsDef; |
| 5380 | while (cur != NULL) { |
| 5381 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5382 | (cur->href != NULL)) |
| 5383 | return(cur); |
| 5384 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5385 | (cur->href != NULL) && |
| 5386 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5387 | return(cur); |
| 5388 | cur = cur->next; |
| 5389 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5390 | if (orig != node) { |
| 5391 | cur = node->ns; |
| 5392 | if (cur != NULL) { |
| 5393 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5394 | (cur->href != NULL)) |
| 5395 | return(cur); |
| 5396 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5397 | (cur->href != NULL) && |
| 5398 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5399 | return(cur); |
| 5400 | } |
| 5401 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5402 | } |
| 5403 | node = node->parent; |
| 5404 | } |
| 5405 | return(NULL); |
| 5406 | } |
| 5407 | |
| 5408 | /** |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5409 | * xmlNsInScope: |
| 5410 | * @doc: the document |
| 5411 | * @node: the current node |
| 5412 | * @ancestor: the ancestor carrying the namespace |
| 5413 | * @prefix: the namespace prefix |
| 5414 | * |
| 5415 | * Verify that the given namespace held on @ancestor is still in scope |
| 5416 | * on node. |
| 5417 | * |
| 5418 | * Returns 1 if true, 0 if false and -1 in case of error. |
| 5419 | */ |
| 5420 | static int |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5421 | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 5422 | xmlNodePtr ancestor, const xmlChar * prefix) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5423 | { |
| 5424 | xmlNsPtr tst; |
| 5425 | |
| 5426 | while ((node != NULL) && (node != ancestor)) { |
| 5427 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5428 | (node->type == XML_ENTITY_NODE) || |
| 5429 | (node->type == XML_ENTITY_DECL)) |
| 5430 | return (-1); |
| 5431 | if (node->type == XML_ELEMENT_NODE) { |
| 5432 | tst = node->nsDef; |
| 5433 | while (tst != NULL) { |
| 5434 | if ((tst->prefix == NULL) |
| 5435 | && (prefix == NULL)) |
| 5436 | return (0); |
| 5437 | if ((tst->prefix != NULL) |
| 5438 | && (prefix != NULL) |
| 5439 | && (xmlStrEqual(tst->prefix, prefix))) |
| 5440 | return (0); |
| 5441 | tst = tst->next; |
| 5442 | } |
| 5443 | } |
| 5444 | node = node->parent; |
| 5445 | } |
| 5446 | if (node != ancestor) |
| 5447 | return (-1); |
| 5448 | return (1); |
| 5449 | } |
| 5450 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5451 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5452 | * xmlSearchNsByHref: |
| 5453 | * @doc: the document |
| 5454 | * @node: the current node |
| 5455 | * @href: the namespace value |
| 5456 | * |
| 5457 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 5458 | * the defined namespace or return NULL otherwise. |
| 5459 | * Returns the namespace pointer or NULL. |
| 5460 | */ |
| 5461 | xmlNsPtr |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5462 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) |
| 5463 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5464 | xmlNsPtr cur; |
| 5465 | xmlNodePtr orig = node; |
| 5466 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5467 | if ((node == NULL) || (href == NULL)) |
| 5468 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5469 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5470 | /* |
| 5471 | * Only the document can hold the XML spec namespace. |
| 5472 | */ |
| 5473 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5474 | /* |
| 5475 | * The XML-1.0 namespace is normally held on the root |
| 5476 | * element. In this case exceptionally create it on the |
| 5477 | * node element. |
| 5478 | */ |
| 5479 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5480 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5481 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5482 | return (NULL); |
| 5483 | } |
| 5484 | memset(cur, 0, sizeof(xmlNs)); |
| 5485 | cur->type = XML_LOCAL_NAMESPACE; |
| 5486 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5487 | cur->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5488 | cur->next = node->nsDef; |
| 5489 | node->nsDef = cur; |
| 5490 | return (cur); |
| 5491 | } |
| 5492 | if (doc->oldNs == NULL) { |
| 5493 | /* |
| 5494 | * Allocate a new Namespace and fill the fields. |
| 5495 | */ |
| 5496 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5497 | if (doc->oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5498 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5499 | return (NULL); |
| 5500 | } |
| 5501 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5502 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5503 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5504 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5505 | doc->oldNs->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5506 | } |
| 5507 | return (doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5508 | } |
| 5509 | while (node != NULL) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5510 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5511 | (node->type == XML_ENTITY_NODE) || |
| 5512 | (node->type == XML_ENTITY_DECL)) |
| 5513 | return (NULL); |
| 5514 | if (node->type == XML_ELEMENT_NODE) { |
| 5515 | cur = node->nsDef; |
| 5516 | while (cur != NULL) { |
| 5517 | if ((cur->href != NULL) && (href != NULL) && |
| 5518 | (xmlStrEqual(cur->href, href))) { |
| 5519 | if (xmlNsInScope(doc, orig, node, cur->href) == 1) |
| 5520 | return (cur); |
| 5521 | } |
| 5522 | cur = cur->next; |
| 5523 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5524 | if (orig != node) { |
| 5525 | cur = node->ns; |
| 5526 | if (cur != NULL) { |
| 5527 | if ((cur->href != NULL) && (href != NULL) && |
| 5528 | (xmlStrEqual(cur->href, href))) { |
| 5529 | if (xmlNsInScope(doc, orig, node, cur->href) == 1) |
| 5530 | return (cur); |
| 5531 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5532 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5533 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5534 | } |
| 5535 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5536 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5537 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5541 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5542 | * @doc: the document |
| 5543 | * @tree: a node expected to hold the new namespace |
| 5544 | * @ns: the original namespace |
| 5545 | * |
| 5546 | * This function tries to locate a namespace definition in a tree |
| 5547 | * ancestors, or create a new namespace definition node similar to |
| 5548 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 5549 | * null (default namespace) or reused within the subtree defined by |
| 5550 | * @tree or on one of its ancestors then a new prefix is generated. |
| 5551 | * Returns the (new) namespace definition or NULL in case of error |
| 5552 | */ |
| 5553 | xmlNsPtr |
| 5554 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 5555 | xmlNsPtr def; |
| 5556 | xmlChar prefix[50]; |
| 5557 | int counter = 1; |
| 5558 | |
| 5559 | if (tree == NULL) { |
| 5560 | #ifdef DEBUG_TREE |
| 5561 | xmlGenericError(xmlGenericErrorContext, |
| 5562 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 5563 | #endif |
| 5564 | return(NULL); |
| 5565 | } |
| 5566 | if (ns == NULL) { |
| 5567 | #ifdef DEBUG_TREE |
| 5568 | xmlGenericError(xmlGenericErrorContext, |
| 5569 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 5570 | #endif |
| 5571 | return(NULL); |
| 5572 | } |
| 5573 | /* |
| 5574 | * Search an existing namespace definition inherited. |
| 5575 | */ |
| 5576 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 5577 | if (def != NULL) |
| 5578 | return(def); |
| 5579 | |
| 5580 | /* |
| 5581 | * Find a close prefix which is not already in use. |
| 5582 | * Let's strip namespace prefixes longer than 20 chars ! |
| 5583 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5584 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5585 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5586 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5587 | snprintf((char *) prefix, sizeof(prefix), "%.20s", ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5588 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5589 | def = xmlSearchNs(doc, tree, prefix); |
| 5590 | while (def != NULL) { |
| 5591 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5592 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5593 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5594 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5595 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5596 | def = xmlSearchNs(doc, tree, prefix); |
| 5597 | } |
| 5598 | |
| 5599 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5600 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5601 | */ |
| 5602 | def = xmlNewNs(tree, ns->href, prefix); |
| 5603 | return(def); |
| 5604 | } |
| 5605 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5606 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5607 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5608 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5609 | * @doc: the document |
| 5610 | * @tree: a node defining the subtree to reconciliate |
| 5611 | * |
| 5612 | * This function checks that all the namespaces declared within the given |
| 5613 | * tree are properly declared. This is needed for example after Copy or Cut |
| 5614 | * and then paste operations. The subtree may still hold pointers to |
| 5615 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5616 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5617 | * the new environment. If not possible the new namespaces are redeclared |
| 5618 | * on @tree at the top of the given subtree. |
| 5619 | * Returns the number of namespace declarations created or -1 in case of error. |
| 5620 | */ |
| 5621 | int |
| 5622 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 5623 | xmlNsPtr *oldNs = NULL; |
| 5624 | xmlNsPtr *newNs = NULL; |
| 5625 | int sizeCache = 0; |
| 5626 | int nbCache = 0; |
| 5627 | |
| 5628 | xmlNsPtr n; |
| 5629 | xmlNodePtr node = tree; |
| 5630 | xmlAttrPtr attr; |
| 5631 | int ret = 0, i; |
| 5632 | |
| 5633 | while (node != NULL) { |
| 5634 | /* |
| 5635 | * Reconciliate the node namespace |
| 5636 | */ |
| 5637 | if (node->ns != NULL) { |
| 5638 | /* |
| 5639 | * initialize the cache if needed |
| 5640 | */ |
| 5641 | if (sizeCache == 0) { |
| 5642 | sizeCache = 10; |
| 5643 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5644 | sizeof(xmlNsPtr)); |
| 5645 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5646 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5647 | return(-1); |
| 5648 | } |
| 5649 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5650 | sizeof(xmlNsPtr)); |
| 5651 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5652 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5653 | xmlFree(oldNs); |
| 5654 | return(-1); |
| 5655 | } |
| 5656 | } |
| 5657 | for (i = 0;i < nbCache;i++) { |
| 5658 | if (oldNs[i] == node->ns) { |
| 5659 | node->ns = newNs[i]; |
| 5660 | break; |
| 5661 | } |
| 5662 | } |
| 5663 | if (i == nbCache) { |
| 5664 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5665 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5666 | */ |
| 5667 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 5668 | if (n != NULL) { /* :-( what if else ??? */ |
| 5669 | /* |
| 5670 | * check if we need to grow the cache buffers. |
| 5671 | */ |
| 5672 | if (sizeCache <= nbCache) { |
| 5673 | sizeCache *= 2; |
| 5674 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5675 | sizeof(xmlNsPtr)); |
| 5676 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5677 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5678 | xmlFree(newNs); |
| 5679 | return(-1); |
| 5680 | } |
| 5681 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5682 | sizeof(xmlNsPtr)); |
| 5683 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5684 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5685 | xmlFree(oldNs); |
| 5686 | return(-1); |
| 5687 | } |
| 5688 | } |
| 5689 | newNs[nbCache] = n; |
| 5690 | oldNs[nbCache++] = node->ns; |
| 5691 | node->ns = n; |
| 5692 | } |
| 5693 | } |
| 5694 | } |
| 5695 | /* |
| 5696 | * now check for namespace hold by attributes on the node. |
| 5697 | */ |
| 5698 | attr = node->properties; |
| 5699 | while (attr != NULL) { |
| 5700 | if (attr->ns != NULL) { |
| 5701 | /* |
| 5702 | * initialize the cache if needed |
| 5703 | */ |
| 5704 | if (sizeCache == 0) { |
| 5705 | sizeCache = 10; |
| 5706 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5707 | sizeof(xmlNsPtr)); |
| 5708 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5709 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5710 | return(-1); |
| 5711 | } |
| 5712 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5713 | sizeof(xmlNsPtr)); |
| 5714 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5715 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5716 | xmlFree(oldNs); |
| 5717 | return(-1); |
| 5718 | } |
| 5719 | } |
| 5720 | for (i = 0;i < nbCache;i++) { |
| 5721 | if (oldNs[i] == attr->ns) { |
Daniel Veillard | ce66ce1 | 2002-10-28 19:01:59 +0000 | [diff] [blame] | 5722 | attr->ns = newNs[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5723 | break; |
| 5724 | } |
| 5725 | } |
| 5726 | if (i == nbCache) { |
| 5727 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5728 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5729 | */ |
| 5730 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 5731 | if (n != NULL) { /* :-( what if else ??? */ |
| 5732 | /* |
| 5733 | * check if we need to grow the cache buffers. |
| 5734 | */ |
| 5735 | if (sizeCache <= nbCache) { |
| 5736 | sizeCache *= 2; |
| 5737 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5738 | sizeof(xmlNsPtr)); |
| 5739 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5740 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5741 | xmlFree(newNs); |
| 5742 | return(-1); |
| 5743 | } |
| 5744 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5745 | sizeof(xmlNsPtr)); |
| 5746 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5747 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5748 | xmlFree(oldNs); |
| 5749 | return(-1); |
| 5750 | } |
| 5751 | } |
| 5752 | newNs[nbCache] = n; |
| 5753 | oldNs[nbCache++] = attr->ns; |
| 5754 | attr->ns = n; |
| 5755 | } |
| 5756 | } |
| 5757 | } |
| 5758 | attr = attr->next; |
| 5759 | } |
| 5760 | |
| 5761 | /* |
| 5762 | * Browse the full subtree, deep first |
| 5763 | */ |
| 5764 | if (node->children != NULL) { |
| 5765 | /* deep first */ |
| 5766 | node = node->children; |
| 5767 | } else if ((node != tree) && (node->next != NULL)) { |
| 5768 | /* then siblings */ |
| 5769 | node = node->next; |
| 5770 | } else if (node != tree) { |
| 5771 | /* go up to parents->next if needed */ |
| 5772 | while (node != tree) { |
| 5773 | if (node->parent != NULL) |
| 5774 | node = node->parent; |
| 5775 | if ((node != tree) && (node->next != NULL)) { |
| 5776 | node = node->next; |
| 5777 | break; |
| 5778 | } |
| 5779 | if (node->parent == NULL) { |
| 5780 | node = NULL; |
| 5781 | break; |
| 5782 | } |
| 5783 | } |
| 5784 | /* exit condition */ |
| 5785 | if (node == tree) |
| 5786 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 5787 | } else |
| 5788 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5789 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5790 | if (oldNs != NULL) |
| 5791 | xmlFree(oldNs); |
| 5792 | if (newNs != NULL) |
| 5793 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5794 | return(ret); |
| 5795 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5796 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5797 | |
| 5798 | /** |
| 5799 | * xmlHasProp: |
| 5800 | * @node: the node |
| 5801 | * @name: the attribute name |
| 5802 | * |
| 5803 | * Search an attribute associated to a node |
| 5804 | * This function also looks in DTD attribute declaration for #FIXED or |
| 5805 | * default declaration values unless DTD use has been turned off. |
| 5806 | * |
| 5807 | * Returns the attribute or the attribute declaration or NULL if |
| 5808 | * neither was found. |
| 5809 | */ |
| 5810 | xmlAttrPtr |
| 5811 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 5812 | xmlAttrPtr prop; |
| 5813 | xmlDocPtr doc; |
| 5814 | |
| 5815 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 5816 | /* |
| 5817 | * Check on the properties attached to the node |
| 5818 | */ |
| 5819 | prop = node->properties; |
| 5820 | while (prop != NULL) { |
| 5821 | if (xmlStrEqual(prop->name, name)) { |
| 5822 | return(prop); |
| 5823 | } |
| 5824 | prop = prop->next; |
| 5825 | } |
| 5826 | if (!xmlCheckDTD) return(NULL); |
| 5827 | |
| 5828 | /* |
| 5829 | * Check if there is a default declaration in the internal |
| 5830 | * or external subsets |
| 5831 | */ |
| 5832 | doc = node->doc; |
| 5833 | if (doc != NULL) { |
| 5834 | xmlAttributePtr attrDecl; |
| 5835 | if (doc->intSubset != NULL) { |
| 5836 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5837 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5838 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5839 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5840 | /* return attribute declaration only if a default value is given |
| 5841 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5842 | return((xmlAttrPtr) attrDecl); |
| 5843 | } |
| 5844 | } |
| 5845 | return(NULL); |
| 5846 | } |
| 5847 | |
| 5848 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5849 | * xmlHasNsProp: |
| 5850 | * @node: the node |
| 5851 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5852 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5853 | * |
| 5854 | * Search for an attribute associated to a node |
| 5855 | * This attribute has to be anchored in the namespace specified. |
| 5856 | * This does the entity substitution. |
| 5857 | * This function looks in DTD attribute declaration for #FIXED or |
| 5858 | * default declaration values unless DTD use has been turned off. |
| 5859 | * |
| 5860 | * Returns the attribute or the attribute declaration or NULL |
| 5861 | * if neither was found. |
| 5862 | */ |
| 5863 | xmlAttrPtr |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5864 | xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5865 | xmlAttrPtr prop; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5866 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5867 | xmlDocPtr doc; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5868 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5869 | |
| 5870 | if (node == NULL) |
| 5871 | return(NULL); |
| 5872 | |
| 5873 | prop = node->properties; |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 5874 | if (nameSpace == NULL) |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5875 | return(xmlHasProp(node, name)); |
| 5876 | while (prop != NULL) { |
| 5877 | /* |
| 5878 | * One need to have |
| 5879 | * - same attribute names |
| 5880 | * - and the attribute carrying that namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5881 | */ |
| 5882 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | e3c81b5 | 2001-06-17 14:50:34 +0000 | [diff] [blame] | 5883 | ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, nameSpace)))) { |
| 5884 | return(prop); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5885 | } |
| 5886 | prop = prop->next; |
| 5887 | } |
| 5888 | if (!xmlCheckDTD) return(NULL); |
| 5889 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5890 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5891 | /* |
| 5892 | * Check if there is a default declaration in the internal |
| 5893 | * or external subsets |
| 5894 | */ |
| 5895 | doc = node->doc; |
| 5896 | if (doc != NULL) { |
| 5897 | if (doc->intSubset != NULL) { |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5898 | xmlAttributePtr attrDecl = NULL; |
| 5899 | xmlNsPtr *nsList, *cur; |
| 5900 | xmlChar *ename; |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5901 | |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5902 | nsList = xmlGetNsList(node->doc, node); |
| 5903 | if (nsList == NULL) |
| 5904 | return(NULL); |
| 5905 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 5906 | ename = xmlStrdup(node->ns->prefix); |
| 5907 | ename = xmlStrcat(ename, BAD_CAST ":"); |
| 5908 | ename = xmlStrcat(ename, node->name); |
| 5909 | } else { |
| 5910 | ename = xmlStrdup(node->name); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5911 | } |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 5912 | if (ename == NULL) { |
| 5913 | xmlFree(nsList); |
| 5914 | return(NULL); |
| 5915 | } |
| 5916 | |
| 5917 | cur = nsList; |
| 5918 | while (*cur != NULL) { |
| 5919 | if (xmlStrEqual((*cur)->href, nameSpace)) { |
| 5920 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename, |
| 5921 | name, (*cur)->prefix); |
| 5922 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5923 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename, |
| 5924 | name, (*cur)->prefix); |
| 5925 | } |
| 5926 | cur++; |
| 5927 | } |
| 5928 | xmlFree(nsList); |
| 5929 | xmlFree(ename); |
| 5930 | return((xmlAttrPtr) attrDecl); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5931 | } |
| 5932 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5933 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 5934 | return(NULL); |
| 5935 | } |
| 5936 | |
| 5937 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5938 | * xmlGetProp: |
| 5939 | * @node: the node |
| 5940 | * @name: the attribute name |
| 5941 | * |
| 5942 | * Search and get the value of an attribute associated to a node |
| 5943 | * This does the entity substitution. |
| 5944 | * This function looks in DTD attribute declaration for #FIXED or |
| 5945 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 5946 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5947 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 5948 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5949 | * |
| 5950 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5951 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5952 | */ |
| 5953 | xmlChar * |
| 5954 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
| 5955 | xmlAttrPtr prop; |
| 5956 | xmlDocPtr doc; |
| 5957 | |
| 5958 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 5959 | /* |
| 5960 | * Check on the properties attached to the node |
| 5961 | */ |
| 5962 | prop = node->properties; |
| 5963 | while (prop != NULL) { |
| 5964 | if (xmlStrEqual(prop->name, name)) { |
| 5965 | xmlChar *ret; |
| 5966 | |
| 5967 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 5968 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 5969 | return(ret); |
| 5970 | } |
| 5971 | prop = prop->next; |
| 5972 | } |
| 5973 | if (!xmlCheckDTD) return(NULL); |
| 5974 | |
| 5975 | /* |
| 5976 | * Check if there is a default declaration in the internal |
| 5977 | * or external subsets |
| 5978 | */ |
| 5979 | doc = node->doc; |
| 5980 | if (doc != NULL) { |
| 5981 | xmlAttributePtr attrDecl; |
| 5982 | if (doc->intSubset != NULL) { |
| 5983 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5984 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5985 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5986 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5987 | /* return attribute declaration only if a default value is given |
| 5988 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5989 | return(xmlStrdup(attrDecl->defaultValue)); |
| 5990 | } |
| 5991 | } |
| 5992 | return(NULL); |
| 5993 | } |
| 5994 | |
| 5995 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5996 | * xmlGetNoNsProp: |
| 5997 | * @node: the node |
| 5998 | * @name: the attribute name |
| 5999 | * |
| 6000 | * Search and get the value of an attribute associated to a node |
| 6001 | * This does the entity substitution. |
| 6002 | * This function looks in DTD attribute declaration for #FIXED or |
| 6003 | * default declaration values unless DTD use has been turned off. |
| 6004 | * This function is similar to xmlGetProp except it will accept only |
| 6005 | * an attribute in no namespace. |
| 6006 | * |
| 6007 | * Returns the attribute value or NULL if not found. |
| 6008 | * It's up to the caller to free the memory with xmlFree(). |
| 6009 | */ |
| 6010 | xmlChar * |
| 6011 | xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) { |
| 6012 | xmlAttrPtr prop; |
| 6013 | xmlDocPtr doc; |
| 6014 | |
| 6015 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 6016 | /* |
| 6017 | * Check on the properties attached to the node |
| 6018 | */ |
| 6019 | prop = node->properties; |
| 6020 | while (prop != NULL) { |
| 6021 | if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) { |
| 6022 | xmlChar *ret; |
| 6023 | |
| 6024 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 6025 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 6026 | return(ret); |
| 6027 | } |
| 6028 | prop = prop->next; |
| 6029 | } |
| 6030 | if (!xmlCheckDTD) return(NULL); |
| 6031 | |
| 6032 | /* |
| 6033 | * Check if there is a default declaration in the internal |
| 6034 | * or external subsets |
| 6035 | */ |
| 6036 | doc = node->doc; |
| 6037 | if (doc != NULL) { |
| 6038 | xmlAttributePtr attrDecl; |
| 6039 | if (doc->intSubset != NULL) { |
| 6040 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6041 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6042 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6043 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6044 | /* return attribute declaration only if a default value is given |
| 6045 | (that includes #FIXED declarations) */ |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6046 | return(xmlStrdup(attrDecl->defaultValue)); |
| 6047 | } |
| 6048 | } |
| 6049 | return(NULL); |
| 6050 | } |
| 6051 | |
| 6052 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6053 | * xmlGetNsProp: |
| 6054 | * @node: the node |
| 6055 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6056 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6057 | * |
| 6058 | * Search and get the value of an attribute associated to a node |
| 6059 | * This attribute has to be anchored in the namespace specified. |
| 6060 | * This does the entity substitution. |
| 6061 | * This function looks in DTD attribute declaration for #FIXED or |
| 6062 | * default declaration values unless DTD use has been turned off. |
| 6063 | * |
| 6064 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6065 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6066 | */ |
| 6067 | xmlChar * |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6068 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6069 | xmlAttrPtr prop; |
| 6070 | xmlDocPtr doc; |
| 6071 | xmlNsPtr ns; |
| 6072 | |
| 6073 | if (node == NULL) |
| 6074 | return(NULL); |
| 6075 | |
| 6076 | prop = node->properties; |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6077 | if (nameSpace == NULL) |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6078 | return(xmlGetNoNsProp(node, name)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6079 | while (prop != NULL) { |
| 6080 | /* |
| 6081 | * One need to have |
| 6082 | * - same attribute names |
| 6083 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6084 | */ |
| 6085 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | e8fc08e | 2001-06-07 19:35:47 +0000 | [diff] [blame] | 6086 | ((prop->ns != NULL) && |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6087 | (xmlStrEqual(prop->ns->href, nameSpace)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6088 | xmlChar *ret; |
| 6089 | |
| 6090 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 6091 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 6092 | return(ret); |
| 6093 | } |
| 6094 | prop = prop->next; |
| 6095 | } |
| 6096 | if (!xmlCheckDTD) return(NULL); |
| 6097 | |
| 6098 | /* |
| 6099 | * Check if there is a default declaration in the internal |
| 6100 | * or external subsets |
| 6101 | */ |
| 6102 | doc = node->doc; |
| 6103 | if (doc != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6104 | if (doc->intSubset != NULL) { |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 6105 | xmlAttributePtr attrDecl; |
| 6106 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6107 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6108 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6109 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 6110 | |
| 6111 | if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) { |
| 6112 | /* |
| 6113 | * The DTD declaration only allows a prefix search |
| 6114 | */ |
| 6115 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6116 | if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6117 | return(xmlStrdup(attrDecl->defaultValue)); |
| 6118 | } |
| 6119 | } |
| 6120 | } |
| 6121 | return(NULL); |
| 6122 | } |
| 6123 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6124 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6125 | /** |
| 6126 | * xmlSetProp: |
| 6127 | * @node: the node |
| 6128 | * @name: the attribute name |
| 6129 | * @value: the attribute value |
| 6130 | * |
| 6131 | * Set (or reset) an attribute carried by a node. |
| 6132 | * Returns the attribute pointer. |
| 6133 | */ |
| 6134 | xmlAttrPtr |
| 6135 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6136 | xmlAttrPtr prop; |
| 6137 | xmlDocPtr doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6138 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6139 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6140 | return(NULL); |
| 6141 | doc = node->doc; |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6142 | prop = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6143 | while (prop != NULL) { |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6144 | if ((xmlStrEqual(prop->name, name)) && |
| 6145 | (prop->ns == NULL)){ |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6146 | xmlNodePtr oldprop = prop->children; |
| 6147 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6148 | prop->children = NULL; |
| 6149 | prop->last = NULL; |
| 6150 | if (value != NULL) { |
| 6151 | xmlChar *buffer; |
| 6152 | xmlNodePtr tmp; |
| 6153 | |
| 6154 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6155 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 6156 | prop->last = NULL; |
| 6157 | prop->doc = doc; |
| 6158 | tmp = prop->children; |
| 6159 | while (tmp != NULL) { |
| 6160 | tmp->parent = (xmlNodePtr) prop; |
| 6161 | tmp->doc = doc; |
| 6162 | if (tmp->next == NULL) |
| 6163 | prop->last = tmp; |
| 6164 | tmp = tmp->next; |
| 6165 | } |
| 6166 | xmlFree(buffer); |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6167 | } |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6168 | if (oldprop != NULL) |
| 6169 | xmlFreeNodeList(oldprop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6170 | return(prop); |
| 6171 | } |
| 6172 | prop = prop->next; |
| 6173 | } |
| 6174 | prop = xmlNewProp(node, name, value); |
| 6175 | return(prop); |
| 6176 | } |
| 6177 | |
| 6178 | /** |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6179 | * xmlUnsetProp: |
| 6180 | * @node: the node |
| 6181 | * @name: the attribute name |
| 6182 | * |
| 6183 | * Remove an attribute carried by a node. |
| 6184 | * Returns 0 if successful, -1 if not found |
| 6185 | */ |
| 6186 | int |
| 6187 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 6188 | xmlAttrPtr prop, prev = NULL;; |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6189 | |
| 6190 | if ((node == NULL) || (name == NULL)) |
| 6191 | return(-1); |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 6192 | prop = node->properties; |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6193 | while (prop != NULL) { |
| 6194 | if ((xmlStrEqual(prop->name, name)) && |
| 6195 | (prop->ns == NULL)) { |
| 6196 | if (prev == NULL) |
| 6197 | node->properties = prop->next; |
| 6198 | else |
| 6199 | prev->next = prop->next; |
| 6200 | xmlFreeProp(prop); |
| 6201 | return(0); |
| 6202 | } |
| 6203 | prev = prop; |
| 6204 | prop = prop->next; |
| 6205 | } |
| 6206 | return(-1); |
| 6207 | } |
| 6208 | |
| 6209 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6210 | * xmlSetNsProp: |
| 6211 | * @node: the node |
| 6212 | * @ns: the namespace definition |
| 6213 | * @name: the attribute name |
| 6214 | * @value: the attribute value |
| 6215 | * |
| 6216 | * Set (or reset) an attribute carried by a node. |
| 6217 | * The ns structure must be in scope, this is not checked. |
| 6218 | * |
| 6219 | * Returns the attribute pointer. |
| 6220 | */ |
| 6221 | xmlAttrPtr |
| 6222 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 6223 | const xmlChar *value) { |
| 6224 | xmlAttrPtr prop; |
| 6225 | |
| 6226 | if ((node == NULL) || (name == NULL)) |
| 6227 | return(NULL); |
| 6228 | |
| 6229 | if (ns == NULL) |
| 6230 | return(xmlSetProp(node, name, value)); |
| 6231 | if (ns->href == NULL) |
| 6232 | return(NULL); |
| 6233 | prop = node->properties; |
| 6234 | |
| 6235 | while (prop != NULL) { |
| 6236 | /* |
| 6237 | * One need to have |
| 6238 | * - same attribute names |
| 6239 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6240 | */ |
| 6241 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | a57c26e | 2002-08-01 12:52:24 +0000 | [diff] [blame] | 6242 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6243 | if (prop->children != NULL) |
| 6244 | xmlFreeNodeList(prop->children); |
| 6245 | prop->children = NULL; |
| 6246 | prop->last = NULL; |
| 6247 | prop->ns = ns; |
| 6248 | if (value != NULL) { |
| 6249 | xmlChar *buffer; |
| 6250 | xmlNodePtr tmp; |
| 6251 | |
| 6252 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6253 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 6254 | prop->last = NULL; |
| 6255 | tmp = prop->children; |
| 6256 | while (tmp != NULL) { |
| 6257 | tmp->parent = (xmlNodePtr) prop; |
| 6258 | if (tmp->next == NULL) |
| 6259 | prop->last = tmp; |
| 6260 | tmp = tmp->next; |
| 6261 | } |
| 6262 | xmlFree(buffer); |
| 6263 | } |
| 6264 | return(prop); |
| 6265 | } |
| 6266 | prop = prop->next; |
| 6267 | } |
| 6268 | prop = xmlNewNsProp(node, ns, name, value); |
| 6269 | return(prop); |
| 6270 | } |
| 6271 | |
| 6272 | /** |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6273 | * xmlUnsetNsProp: |
| 6274 | * @node: the node |
| 6275 | * @ns: the namespace definition |
| 6276 | * @name: the attribute name |
| 6277 | * |
| 6278 | * Remove an attribute carried by a node. |
| 6279 | * Returns 0 if successful, -1 if not found |
| 6280 | */ |
| 6281 | int |
| 6282 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
| 6283 | xmlAttrPtr prop = node->properties, prev = NULL;; |
| 6284 | |
| 6285 | if ((node == NULL) || (name == NULL)) |
| 6286 | return(-1); |
| 6287 | if (ns == NULL) |
| 6288 | return(xmlUnsetProp(node, name)); |
| 6289 | if (ns->href == NULL) |
| 6290 | return(-1); |
| 6291 | while (prop != NULL) { |
| 6292 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | 0bf2900 | 2002-08-01 12:54:11 +0000 | [diff] [blame] | 6293 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6294 | if (prev == NULL) |
| 6295 | node->properties = prop->next; |
| 6296 | else |
| 6297 | prev->next = prop->next; |
| 6298 | xmlFreeProp(prop); |
| 6299 | return(0); |
| 6300 | } |
| 6301 | prev = prop; |
| 6302 | prop = prop->next; |
| 6303 | } |
| 6304 | return(-1); |
| 6305 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6306 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6307 | |
| 6308 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6309 | * xmlNodeIsText: |
| 6310 | * @node: the node |
| 6311 | * |
| 6312 | * Is this node a Text node ? |
| 6313 | * Returns 1 yes, 0 no |
| 6314 | */ |
| 6315 | int |
| 6316 | xmlNodeIsText(xmlNodePtr node) { |
| 6317 | if (node == NULL) return(0); |
| 6318 | |
| 6319 | if (node->type == XML_TEXT_NODE) return(1); |
| 6320 | return(0); |
| 6321 | } |
| 6322 | |
| 6323 | /** |
| 6324 | * xmlIsBlankNode: |
| 6325 | * @node: the node |
| 6326 | * |
| 6327 | * Checks whether this node is an empty or whitespace only |
| 6328 | * (and possibly ignorable) text-node. |
| 6329 | * |
| 6330 | * Returns 1 yes, 0 no |
| 6331 | */ |
| 6332 | int |
| 6333 | xmlIsBlankNode(xmlNodePtr node) { |
| 6334 | const xmlChar *cur; |
| 6335 | if (node == NULL) return(0); |
| 6336 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6337 | if ((node->type != XML_TEXT_NODE) && |
| 6338 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6339 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6340 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6341 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6342 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6343 | if (!IS_BLANK_CH(*cur)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6344 | cur++; |
| 6345 | } |
| 6346 | |
| 6347 | return(1); |
| 6348 | } |
| 6349 | |
| 6350 | /** |
| 6351 | * xmlTextConcat: |
| 6352 | * @node: the node |
| 6353 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6354 | * @len: @content length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6355 | * |
| 6356 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6357 | * |
| 6358 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6359 | */ |
| 6360 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6361 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6362 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6363 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6364 | |
| 6365 | if ((node->type != XML_TEXT_NODE) && |
| 6366 | (node->type != XML_CDATA_SECTION_NODE)) { |
| 6367 | #ifdef DEBUG_TREE |
| 6368 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6369 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6370 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6371 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6372 | } |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6373 | /* need to check if content is currently in the dictionary */ |
| 6374 | if ((node->doc != NULL) && (node->doc->dict != NULL) && |
| 6375 | xmlDictOwns(node->doc->dict, node->content)) { |
| 6376 | node->content = xmlStrncatNew(node->content, content, len); |
| 6377 | } else { |
| 6378 | node->content = xmlStrncat(node->content, content, len); |
| 6379 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6380 | if (node->content == NULL) |
| 6381 | return(-1); |
| 6382 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6383 | } |
| 6384 | |
| 6385 | /************************************************************************ |
| 6386 | * * |
| 6387 | * Output : to a FILE or in memory * |
| 6388 | * * |
| 6389 | ************************************************************************/ |
| 6390 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6391 | /** |
| 6392 | * xmlBufferCreate: |
| 6393 | * |
| 6394 | * routine to create an XML buffer. |
| 6395 | * returns the new structure. |
| 6396 | */ |
| 6397 | xmlBufferPtr |
| 6398 | xmlBufferCreate(void) { |
| 6399 | xmlBufferPtr ret; |
| 6400 | |
| 6401 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6402 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6403 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6404 | return(NULL); |
| 6405 | } |
| 6406 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 6407 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6408 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6409 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6410 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6411 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6412 | xmlFree(ret); |
| 6413 | return(NULL); |
| 6414 | } |
| 6415 | ret->content[0] = 0; |
| 6416 | return(ret); |
| 6417 | } |
| 6418 | |
| 6419 | /** |
| 6420 | * xmlBufferCreateSize: |
| 6421 | * @size: initial size of buffer |
| 6422 | * |
| 6423 | * routine to create an XML buffer. |
| 6424 | * returns the new structure. |
| 6425 | */ |
| 6426 | xmlBufferPtr |
| 6427 | xmlBufferCreateSize(size_t size) { |
| 6428 | xmlBufferPtr ret; |
| 6429 | |
| 6430 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6431 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6432 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6433 | return(NULL); |
| 6434 | } |
| 6435 | ret->use = 0; |
| 6436 | ret->alloc = xmlBufferAllocScheme; |
| 6437 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 6438 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6439 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6440 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6441 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6442 | xmlFree(ret); |
| 6443 | return(NULL); |
| 6444 | } |
| 6445 | ret->content[0] = 0; |
| 6446 | } else |
| 6447 | ret->content = NULL; |
| 6448 | return(ret); |
| 6449 | } |
| 6450 | |
| 6451 | /** |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6452 | * xmlBufferCreateStatic: |
| 6453 | * @mem: the memory area |
| 6454 | * @size: the size in byte |
| 6455 | * |
MST 2003 John Fleck | a0e7e93 | 2003-12-19 03:13:47 +0000 | [diff] [blame] | 6456 | * routine to create an XML buffer from an immutable memory area. |
| 6457 | * The area won't be modified nor copied, and is expected to be |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6458 | * present until the end of the buffer lifetime. |
| 6459 | * |
| 6460 | * returns the new structure. |
| 6461 | */ |
| 6462 | xmlBufferPtr |
| 6463 | xmlBufferCreateStatic(void *mem, size_t size) { |
| 6464 | xmlBufferPtr ret; |
| 6465 | |
| 6466 | if ((mem == NULL) || (size == 0)) |
| 6467 | return(NULL); |
| 6468 | |
| 6469 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6470 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6471 | xmlTreeErrMemory("creating buffer"); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6472 | return(NULL); |
| 6473 | } |
| 6474 | ret->use = size; |
| 6475 | ret->size = size; |
| 6476 | ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; |
| 6477 | ret->content = (xmlChar *) mem; |
| 6478 | return(ret); |
| 6479 | } |
| 6480 | |
| 6481 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6482 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6483 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6484 | * @scheme: allocation scheme to use |
| 6485 | * |
| 6486 | * Sets the allocation scheme for this buffer |
| 6487 | */ |
| 6488 | void |
| 6489 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 6490 | xmlBufferAllocationScheme scheme) { |
| 6491 | if (buf == NULL) { |
| 6492 | #ifdef DEBUG_BUFFER |
| 6493 | xmlGenericError(xmlGenericErrorContext, |
| 6494 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 6495 | #endif |
| 6496 | return; |
| 6497 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6498 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6499 | |
| 6500 | buf->alloc = scheme; |
| 6501 | } |
| 6502 | |
| 6503 | /** |
| 6504 | * xmlBufferFree: |
| 6505 | * @buf: the buffer to free |
| 6506 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 6507 | * Frees an XML buffer. It frees both the content and the structure which |
| 6508 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6509 | */ |
| 6510 | void |
| 6511 | xmlBufferFree(xmlBufferPtr buf) { |
| 6512 | if (buf == NULL) { |
| 6513 | #ifdef DEBUG_BUFFER |
| 6514 | xmlGenericError(xmlGenericErrorContext, |
| 6515 | "xmlBufferFree: buf == NULL\n"); |
| 6516 | #endif |
| 6517 | return; |
| 6518 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6519 | |
| 6520 | if ((buf->content != NULL) && |
| 6521 | (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6522 | xmlFree(buf->content); |
| 6523 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6524 | xmlFree(buf); |
| 6525 | } |
| 6526 | |
| 6527 | /** |
| 6528 | * xmlBufferEmpty: |
| 6529 | * @buf: the buffer |
| 6530 | * |
| 6531 | * empty a buffer. |
| 6532 | */ |
| 6533 | void |
| 6534 | xmlBufferEmpty(xmlBufferPtr buf) { |
| 6535 | if (buf->content == NULL) return; |
| 6536 | buf->use = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6537 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 6538 | buf->content = BAD_CAST ""; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6539 | } else { |
| 6540 | memset(buf->content, 0, buf->size); |
| 6541 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6542 | } |
| 6543 | |
| 6544 | /** |
| 6545 | * xmlBufferShrink: |
| 6546 | * @buf: the buffer to dump |
| 6547 | * @len: the number of xmlChar to remove |
| 6548 | * |
| 6549 | * Remove the beginning of an XML buffer. |
| 6550 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6551 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6552 | */ |
| 6553 | int |
| 6554 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
| 6555 | if (len == 0) return(0); |
| 6556 | if (len > buf->use) return(-1); |
| 6557 | |
| 6558 | buf->use -= len; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6559 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
| 6560 | buf->content += len; |
| 6561 | } else { |
| 6562 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
| 6563 | buf->content[buf->use] = 0; |
| 6564 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6565 | return(len); |
| 6566 | } |
| 6567 | |
| 6568 | /** |
| 6569 | * xmlBufferGrow: |
| 6570 | * @buf: the buffer |
| 6571 | * @len: the minimum free size to allocate |
| 6572 | * |
| 6573 | * Grow the available space of an XML buffer. |
| 6574 | * |
| 6575 | * Returns the new available space or -1 in case of error |
| 6576 | */ |
| 6577 | int |
| 6578 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 6579 | int size; |
| 6580 | xmlChar *newbuf; |
| 6581 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6582 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6583 | if (len + buf->use < buf->size) return(0); |
| 6584 | |
| 6585 | size = buf->use + len + 100; |
| 6586 | |
| 6587 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6588 | if (newbuf == NULL) { |
| 6589 | xmlTreeErrMemory("growing buffer"); |
| 6590 | return(-1); |
| 6591 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6592 | buf->content = newbuf; |
| 6593 | buf->size = size; |
| 6594 | return(buf->size - buf->use); |
| 6595 | } |
| 6596 | |
| 6597 | /** |
| 6598 | * xmlBufferDump: |
| 6599 | * @file: the file output |
| 6600 | * @buf: the buffer to dump |
| 6601 | * |
| 6602 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6603 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6604 | */ |
| 6605 | int |
| 6606 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 6607 | int ret; |
| 6608 | |
| 6609 | if (buf == NULL) { |
| 6610 | #ifdef DEBUG_BUFFER |
| 6611 | xmlGenericError(xmlGenericErrorContext, |
| 6612 | "xmlBufferDump: buf == NULL\n"); |
| 6613 | #endif |
| 6614 | return(0); |
| 6615 | } |
| 6616 | if (buf->content == NULL) { |
| 6617 | #ifdef DEBUG_BUFFER |
| 6618 | xmlGenericError(xmlGenericErrorContext, |
| 6619 | "xmlBufferDump: buf->content == NULL\n"); |
| 6620 | #endif |
| 6621 | return(0); |
| 6622 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 6623 | if (file == NULL) |
| 6624 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6625 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 6626 | return(ret); |
| 6627 | } |
| 6628 | |
| 6629 | /** |
| 6630 | * xmlBufferContent: |
| 6631 | * @buf: the buffer |
| 6632 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6633 | * Function to extract the content of a buffer |
| 6634 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6635 | * Returns the internal content |
| 6636 | */ |
| 6637 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6638 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6639 | xmlBufferContent(const xmlBufferPtr buf) |
| 6640 | { |
| 6641 | if(!buf) |
| 6642 | return NULL; |
| 6643 | |
| 6644 | return buf->content; |
| 6645 | } |
| 6646 | |
| 6647 | /** |
| 6648 | * xmlBufferLength: |
| 6649 | * @buf: the buffer |
| 6650 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6651 | * Function to get the length of a buffer |
| 6652 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6653 | * Returns the length of data in the internal content |
| 6654 | */ |
| 6655 | |
| 6656 | int |
| 6657 | xmlBufferLength(const xmlBufferPtr buf) |
| 6658 | { |
| 6659 | if(!buf) |
| 6660 | return 0; |
| 6661 | |
| 6662 | return buf->use; |
| 6663 | } |
| 6664 | |
| 6665 | /** |
| 6666 | * xmlBufferResize: |
| 6667 | * @buf: the buffer to resize |
| 6668 | * @size: the desired size |
| 6669 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6670 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6671 | * |
| 6672 | * Returns 0 in case of problems, 1 otherwise |
| 6673 | */ |
| 6674 | int |
| 6675 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 6676 | { |
| 6677 | unsigned int newSize; |
| 6678 | xmlChar* rebuf = NULL; |
| 6679 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6680 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
| 6681 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6682 | /*take care of empty case*/ |
| 6683 | newSize = (buf->size ? buf->size*2 : size); |
| 6684 | |
| 6685 | /* Don't resize if we don't have to */ |
| 6686 | if (size < buf->size) |
| 6687 | return 1; |
| 6688 | |
| 6689 | /* figure out new size */ |
| 6690 | switch (buf->alloc){ |
| 6691 | case XML_BUFFER_ALLOC_DOUBLEIT: |
| 6692 | while (size > newSize) newSize *= 2; |
| 6693 | break; |
| 6694 | case XML_BUFFER_ALLOC_EXACT: |
| 6695 | newSize = size+10; |
| 6696 | break; |
| 6697 | default: |
| 6698 | newSize = size+10; |
| 6699 | break; |
| 6700 | } |
| 6701 | |
| 6702 | if (buf->content == NULL) |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6703 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6704 | else if (buf->size - buf->use < 100) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6705 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 6706 | newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6707 | } else { |
| 6708 | /* |
| 6709 | * if we are reallocating a buffer far from being full, it's |
| 6710 | * better to make a new allocation and copy only the used range |
| 6711 | * and free the old one. |
| 6712 | */ |
| 6713 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
| 6714 | if (rebuf != NULL) { |
| 6715 | memcpy(rebuf, buf->content, buf->use); |
| 6716 | xmlFree(buf->content); |
| 6717 | } |
Daniel Veillard | e598408 | 2003-08-19 22:21:13 +0000 | [diff] [blame] | 6718 | rebuf[buf->use] = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6719 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6720 | if (rebuf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6721 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6722 | return 0; |
| 6723 | } |
| 6724 | buf->content = rebuf; |
| 6725 | buf->size = newSize; |
| 6726 | |
| 6727 | return 1; |
| 6728 | } |
| 6729 | |
| 6730 | /** |
| 6731 | * xmlBufferAdd: |
| 6732 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6733 | * @str: the #xmlChar string |
| 6734 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6735 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6736 | * 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] | 6737 | * str is recomputed. |
| 6738 | */ |
| 6739 | void |
| 6740 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6741 | unsigned int needSize; |
| 6742 | |
| 6743 | if (str == NULL) { |
| 6744 | #ifdef DEBUG_BUFFER |
| 6745 | xmlGenericError(xmlGenericErrorContext, |
| 6746 | "xmlBufferAdd: str == NULL\n"); |
| 6747 | #endif |
| 6748 | return; |
| 6749 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6750 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6751 | if (len < -1) { |
| 6752 | #ifdef DEBUG_BUFFER |
| 6753 | xmlGenericError(xmlGenericErrorContext, |
| 6754 | "xmlBufferAdd: len < 0\n"); |
| 6755 | #endif |
| 6756 | return; |
| 6757 | } |
| 6758 | if (len == 0) return; |
| 6759 | |
| 6760 | if (len < 0) |
| 6761 | len = xmlStrlen(str); |
| 6762 | |
| 6763 | if (len <= 0) return; |
| 6764 | |
| 6765 | needSize = buf->use + len + 2; |
| 6766 | if (needSize > buf->size){ |
| 6767 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6768 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6769 | return; |
| 6770 | } |
| 6771 | } |
| 6772 | |
| 6773 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 6774 | buf->use += len; |
| 6775 | buf->content[buf->use] = 0; |
| 6776 | } |
| 6777 | |
| 6778 | /** |
| 6779 | * xmlBufferAddHead: |
| 6780 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6781 | * @str: the #xmlChar string |
| 6782 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6783 | * |
| 6784 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6785 | * if len == -1, the length of @str is recomputed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6786 | */ |
| 6787 | void |
| 6788 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6789 | unsigned int needSize; |
| 6790 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6791 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6792 | if (str == NULL) { |
| 6793 | #ifdef DEBUG_BUFFER |
| 6794 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6795 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6796 | #endif |
| 6797 | return; |
| 6798 | } |
| 6799 | if (len < -1) { |
| 6800 | #ifdef DEBUG_BUFFER |
| 6801 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6802 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6803 | #endif |
| 6804 | return; |
| 6805 | } |
| 6806 | if (len == 0) return; |
| 6807 | |
| 6808 | if (len < 0) |
| 6809 | len = xmlStrlen(str); |
| 6810 | |
| 6811 | if (len <= 0) return; |
| 6812 | |
| 6813 | needSize = buf->use + len + 2; |
| 6814 | if (needSize > buf->size){ |
| 6815 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6816 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6817 | return; |
| 6818 | } |
| 6819 | } |
| 6820 | |
| 6821 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 6822 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 6823 | buf->use += len; |
| 6824 | buf->content[buf->use] = 0; |
| 6825 | } |
| 6826 | |
| 6827 | /** |
| 6828 | * xmlBufferCat: |
| 6829 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6830 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6831 | * |
| 6832 | * Append a zero terminated string to an XML buffer. |
| 6833 | */ |
| 6834 | void |
| 6835 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6836 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6837 | if (str != NULL) |
| 6838 | xmlBufferAdd(buf, str, -1); |
| 6839 | } |
| 6840 | |
| 6841 | /** |
| 6842 | * xmlBufferCCat: |
| 6843 | * @buf: the buffer to dump |
| 6844 | * @str: the C char string |
| 6845 | * |
| 6846 | * Append a zero terminated C string to an XML buffer. |
| 6847 | */ |
| 6848 | void |
| 6849 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 6850 | const char *cur; |
| 6851 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6852 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6853 | if (str == NULL) { |
| 6854 | #ifdef DEBUG_BUFFER |
| 6855 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6856 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6857 | #endif |
| 6858 | return; |
| 6859 | } |
| 6860 | for (cur = str;*cur != 0;cur++) { |
| 6861 | if (buf->use + 10 >= buf->size) { |
| 6862 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6863 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6864 | return; |
| 6865 | } |
| 6866 | } |
| 6867 | buf->content[buf->use++] = *cur; |
| 6868 | } |
| 6869 | buf->content[buf->use] = 0; |
| 6870 | } |
| 6871 | |
| 6872 | /** |
| 6873 | * xmlBufferWriteCHAR: |
| 6874 | * @buf: the XML buffer |
| 6875 | * @string: the string to add |
| 6876 | * |
| 6877 | * routine which manages and grows an output buffer. This one adds |
| 6878 | * xmlChars at the end of the buffer. |
| 6879 | */ |
| 6880 | void |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6881 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
| 6882 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6883 | xmlBufferCat(buf, string); |
| 6884 | } |
| 6885 | |
| 6886 | /** |
| 6887 | * xmlBufferWriteChar: |
| 6888 | * @buf: the XML buffer output |
| 6889 | * @string: the string to add |
| 6890 | * |
| 6891 | * routine which manage and grows an output buffer. This one add |
| 6892 | * C chars at the end of the array. |
| 6893 | */ |
| 6894 | void |
| 6895 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6896 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6897 | xmlBufferCCat(buf, string); |
| 6898 | } |
| 6899 | |
| 6900 | |
| 6901 | /** |
| 6902 | * xmlBufferWriteQuotedString: |
| 6903 | * @buf: the XML buffer output |
| 6904 | * @string: the string to add |
| 6905 | * |
| 6906 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6907 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6908 | * quote or double-quotes internally |
| 6909 | */ |
| 6910 | void |
| 6911 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6912 | const xmlChar *cur, *base; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6913 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6914 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 6915 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6916 | #ifdef DEBUG_BUFFER |
| 6917 | xmlGenericError(xmlGenericErrorContext, |
| 6918 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 6919 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6920 | xmlBufferCCat(buf, "\""); |
| 6921 | base = cur = string; |
| 6922 | while(*cur != 0){ |
| 6923 | if(*cur == '"'){ |
| 6924 | if (base != cur) |
| 6925 | xmlBufferAdd(buf, base, cur - base); |
| 6926 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 6927 | cur++; |
| 6928 | base = cur; |
| 6929 | } |
| 6930 | else { |
| 6931 | cur++; |
| 6932 | } |
| 6933 | } |
| 6934 | if (base != cur) |
| 6935 | xmlBufferAdd(buf, base, cur - base); |
| 6936 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6937 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 6938 | else{ |
| 6939 | xmlBufferCCat(buf, "\'"); |
| 6940 | xmlBufferCat(buf, string); |
| 6941 | xmlBufferCCat(buf, "\'"); |
| 6942 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6943 | } else { |
| 6944 | xmlBufferCCat(buf, "\""); |
| 6945 | xmlBufferCat(buf, string); |
| 6946 | xmlBufferCCat(buf, "\""); |
| 6947 | } |
| 6948 | } |
| 6949 | |
| 6950 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 6951 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6952 | /************************************************************************ |
| 6953 | * * |
Daniel Veillard | e2238d5 | 2003-10-09 13:14:55 +0000 | [diff] [blame] | 6954 | * Output error handlers * |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6955 | * * |
| 6956 | ************************************************************************/ |
| 6957 | /** |
| 6958 | * xmlSaveErrMemory: |
| 6959 | * @extra: extra informations |
| 6960 | * |
| 6961 | * Handle an out of memory condition |
| 6962 | */ |
| 6963 | static void |
| 6964 | xmlSaveErrMemory(const char *extra) |
| 6965 | { |
| 6966 | __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 6967 | } |
| 6968 | |
| 6969 | /** |
| 6970 | * xmlSaveErr: |
| 6971 | * @code: the error number |
| 6972 | * @node: the location of the error. |
| 6973 | * @extra: extra informations |
| 6974 | * |
| 6975 | * Handle an out of memory condition |
| 6976 | */ |
| 6977 | static void |
| 6978 | xmlSaveErr(int code, xmlNodePtr node, const char *extra) |
| 6979 | { |
| 6980 | const char *msg = NULL; |
| 6981 | |
| 6982 | switch(code) { |
| 6983 | case XML_SAVE_NOT_UTF8: |
| 6984 | msg = "string is not in UTF-8"; |
| 6985 | break; |
| 6986 | case XML_SAVE_CHAR_INVALID: |
| 6987 | msg = "invalid character value"; |
| 6988 | break; |
| 6989 | case XML_SAVE_UNKNOWN_ENCODING: |
| 6990 | msg = "unknown encoding %s"; |
| 6991 | break; |
Daniel Veillard | e2238d5 | 2003-10-09 13:14:55 +0000 | [diff] [blame] | 6992 | case XML_SAVE_NO_DOCTYPE: |
| 6993 | msg = "document has no DOCTYPE"; |
| 6994 | break; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6995 | default: |
| 6996 | msg = "unexpected error number"; |
| 6997 | } |
Daniel Veillard | e2238d5 | 2003-10-09 13:14:55 +0000 | [diff] [blame] | 6998 | __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6999 | } |
| 7000 | /************************************************************************ |
| 7001 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7002 | * Dumping XML tree content to a simple buffer * |
| 7003 | * * |
| 7004 | ************************************************************************/ |
William M. Brack | a9c612c | 2004-02-01 10:04:05 +0000 | [diff] [blame] | 7005 | /** |
| 7006 | * xmlAttrSerializeTxtContent: |
| 7007 | * @buf: the XML buffer output |
| 7008 | * @doc: the document |
| 7009 | * @attr: the attribute node |
| 7010 | * @value: the text content |
| 7011 | * |
| 7012 | * Serialize text attribute values to an xml simple buffer |
| 7013 | */ |
| 7014 | void |
| 7015 | xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc, |
| 7016 | xmlAttrPtr attr, const xmlChar *string) { |
| 7017 | xmlChar *base, *cur; |
| 7018 | |
Daniel Veillard | cb35f01 | 2004-02-20 08:18:58 +0000 | [diff] [blame] | 7019 | if (string == NULL) return; |
William M. Brack | a9c612c | 2004-02-01 10:04:05 +0000 | [diff] [blame] | 7020 | base = cur = (xmlChar *)string; |
| 7021 | while (*cur != 0) { |
| 7022 | if (*cur == '\n') { |
| 7023 | if (base != cur) |
| 7024 | xmlBufferAdd(buf, base, cur - base); |
| 7025 | xmlBufferAdd(buf, BAD_CAST " ", 5); |
| 7026 | cur++; |
| 7027 | base = cur; |
| 7028 | } else if (*cur == '\r') { |
| 7029 | if (base != cur) |
| 7030 | xmlBufferAdd(buf, base, cur - base); |
| 7031 | xmlBufferAdd(buf, BAD_CAST " ", 5); |
| 7032 | cur++; |
| 7033 | base = cur; |
| 7034 | } else if (*cur == '\t') { |
| 7035 | if (base != cur) |
| 7036 | xmlBufferAdd(buf, base, cur - base); |
| 7037 | xmlBufferAdd(buf, BAD_CAST "	", 4); |
| 7038 | cur++; |
| 7039 | base = cur; |
| 7040 | #if 0 |
| 7041 | base = cur = children->content; |
| 7042 | } else if (*cur == '\'') { |
| 7043 | if (base != cur) |
| 7044 | xmlBufferAdd(buf, base, cur - base); |
| 7045 | xmlBufferAdd(buf, BAD_CAST "'", 6); |
| 7046 | cur++; |
| 7047 | base = cur; |
| 7048 | #endif |
| 7049 | } else if (*cur == '"') { |
| 7050 | if (base != cur) |
| 7051 | xmlBufferAdd(buf, base, cur - base); |
| 7052 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 7053 | cur++; |
| 7054 | base = cur; |
| 7055 | } else if (*cur == '<') { |
| 7056 | if (base != cur) |
| 7057 | xmlBufferAdd(buf, base, cur - base); |
| 7058 | xmlBufferAdd(buf, BAD_CAST "<", 4); |
| 7059 | cur++; |
| 7060 | base = cur; |
| 7061 | } else if (*cur == '>') { |
| 7062 | if (base != cur) |
| 7063 | xmlBufferAdd(buf, base, cur - base); |
| 7064 | xmlBufferAdd(buf, BAD_CAST ">", 4); |
| 7065 | cur++; |
| 7066 | base = cur; |
| 7067 | } else if (*cur == '&') { |
| 7068 | if (base != cur) |
| 7069 | xmlBufferAdd(buf, base, cur - base); |
| 7070 | xmlBufferAdd(buf, BAD_CAST "&", 5); |
| 7071 | cur++; |
| 7072 | base = cur; |
| 7073 | } else if ((*cur >= 0x80) && ((doc == NULL) || |
| 7074 | (doc->encoding == NULL))) { |
| 7075 | /* |
| 7076 | * We assume we have UTF-8 content. |
| 7077 | */ |
| 7078 | char tmp[10]; |
| 7079 | int val = 0, l = 1; |
| 7080 | |
| 7081 | if (base != cur) |
| 7082 | xmlBufferAdd(buf, base, cur - base); |
| 7083 | if (*cur < 0xC0) { |
| 7084 | xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL); |
| 7085 | if (doc != NULL) |
| 7086 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 7087 | snprintf(tmp, sizeof(tmp), "&#%d;", *cur); |
| 7088 | tmp[sizeof(tmp) - 1] = 0; |
| 7089 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 7090 | cur++; |
| 7091 | base = cur; |
| 7092 | continue; |
| 7093 | } else if (*cur < 0xE0) { |
| 7094 | val = (cur[0]) & 0x1F; |
| 7095 | val <<= 6; |
| 7096 | val |= (cur[1]) & 0x3F; |
| 7097 | l = 2; |
| 7098 | } else if (*cur < 0xF0) { |
| 7099 | val = (cur[0]) & 0x0F; |
| 7100 | val <<= 6; |
| 7101 | val |= (cur[1]) & 0x3F; |
| 7102 | val <<= 6; |
| 7103 | val |= (cur[2]) & 0x3F; |
| 7104 | l = 3; |
| 7105 | } else if (*cur < 0xF8) { |
| 7106 | val = (cur[0]) & 0x07; |
| 7107 | val <<= 6; |
| 7108 | val |= (cur[1]) & 0x3F; |
| 7109 | val <<= 6; |
| 7110 | val |= (cur[2]) & 0x3F; |
| 7111 | val <<= 6; |
| 7112 | val |= (cur[3]) & 0x3F; |
| 7113 | l = 4; |
| 7114 | } |
| 7115 | if ((l == 1) || (!IS_CHAR(val))) { |
| 7116 | xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL); |
| 7117 | if (doc != NULL) |
| 7118 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 7119 | snprintf(tmp, sizeof(tmp), "&#%d;", *cur); |
| 7120 | tmp[sizeof(tmp) - 1] = 0; |
| 7121 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 7122 | cur++; |
| 7123 | base = cur; |
| 7124 | continue; |
| 7125 | } |
| 7126 | /* |
| 7127 | * We could do multiple things here. Just save |
| 7128 | * as a char ref |
| 7129 | */ |
| 7130 | snprintf(tmp, sizeof(tmp), "&#x%X;", val); |
| 7131 | tmp[sizeof(tmp) - 1] = 0; |
| 7132 | xmlBufferAdd(buf, (xmlChar *) tmp, -1); |
| 7133 | cur += l; |
| 7134 | base = cur; |
| 7135 | } else { |
| 7136 | cur++; |
| 7137 | } |
| 7138 | } |
| 7139 | if (base != cur) |
| 7140 | xmlBufferAdd(buf, base, cur - base); |
| 7141 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7142 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7143 | /** |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 7144 | * xmlAttrSerializeContent: |
| 7145 | * @buf: the XML buffer output |
| 7146 | * @doc: the document |
| 7147 | * @attr: the attribute pointer |
| 7148 | * |
| 7149 | * Serialize the attribute in the buffer |
| 7150 | */ |
| 7151 | static void |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7152 | xmlAttrSerializeContent(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr attr) |
| 7153 | { |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 7154 | xmlNodePtr children; |
| 7155 | |
| 7156 | children = attr->children; |
| 7157 | while (children != NULL) { |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7158 | switch (children->type) { |
| 7159 | case XML_TEXT_NODE: |
William M. Brack | a9c612c | 2004-02-01 10:04:05 +0000 | [diff] [blame] | 7160 | xmlAttrSerializeTxtContent(buf, doc, attr, children->content); |
| 7161 | break; |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7162 | case XML_ENTITY_REF_NODE: |
| 7163 | xmlBufferAdd(buf, BAD_CAST "&", 1); |
| 7164 | xmlBufferAdd(buf, children->name, |
| 7165 | xmlStrlen(children->name)); |
| 7166 | xmlBufferAdd(buf, BAD_CAST ";", 1); |
| 7167 | break; |
| 7168 | default: |
| 7169 | /* should not happen unless we have a badly built tree */ |
| 7170 | break; |
| 7171 | } |
| 7172 | children = children->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7173 | } |
| 7174 | } |
| 7175 | |
| 7176 | /** |
| 7177 | * xmlNodeDump: |
| 7178 | * @buf: the XML buffer output |
| 7179 | * @doc: the document |
| 7180 | * @cur: the current node |
| 7181 | * @level: the imbrication level for indenting |
| 7182 | * @format: is formatting allowed |
| 7183 | * |
| 7184 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7185 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7186 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7187 | * |
| 7188 | * 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] | 7189 | */ |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7190 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7191 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7192 | int format) |
| 7193 | { |
| 7194 | unsigned int use; |
| 7195 | int ret; |
| 7196 | xmlOutputBufferPtr outbuf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7197 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7198 | xmlInitParser(); |
| 7199 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7200 | if (cur == NULL) { |
| 7201 | #ifdef DEBUG_TREE |
| 7202 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7203 | "xmlNodeDump : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7204 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7205 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7206 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7207 | if (buf == NULL) { |
| 7208 | #ifdef DEBUG_TREE |
| 7209 | xmlGenericError(xmlGenericErrorContext, |
| 7210 | "xmlNodeDump : buf == NULL\n"); |
| 7211 | #endif |
| 7212 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7213 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7214 | outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); |
| 7215 | if (outbuf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7216 | xmlSaveErrMemory("creating buffer"); |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7217 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7218 | } |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7219 | memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer)); |
| 7220 | outbuf->buffer = buf; |
| 7221 | outbuf->encoder = NULL; |
| 7222 | outbuf->writecallback = NULL; |
| 7223 | outbuf->closecallback = NULL; |
| 7224 | outbuf->context = NULL; |
| 7225 | outbuf->written = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7226 | |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7227 | use = buf->use; |
| 7228 | xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL); |
| 7229 | xmlFree(outbuf); |
| 7230 | ret = buf->use - use; |
| 7231 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7232 | } |
| 7233 | |
| 7234 | /** |
| 7235 | * xmlElemDump: |
| 7236 | * @f: the FILE * for the output |
| 7237 | * @doc: the document |
| 7238 | * @cur: the current node |
| 7239 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7240 | * Dump an XML/HTML node, recursive behaviour, children are printed too. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7241 | */ |
| 7242 | void |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7243 | xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur) |
| 7244 | { |
| 7245 | xmlOutputBufferPtr outbuf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7246 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7247 | xmlInitParser(); |
| 7248 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7249 | if (cur == NULL) { |
| 7250 | #ifdef DEBUG_TREE |
| 7251 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7252 | "xmlElemDump : cur == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7253 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7254 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7255 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7256 | #ifdef DEBUG_TREE |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7257 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7258 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7259 | "xmlElemDump : doc == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7260 | } |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7261 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7262 | |
| 7263 | outbuf = xmlOutputBufferCreateFile(f, NULL); |
| 7264 | if (outbuf == NULL) |
| 7265 | return; |
| 7266 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7267 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7268 | htmlNodeDumpOutput(outbuf, doc, cur, NULL); |
| 7269 | #else |
Daniel Veillard | 2189b59 | 2003-10-21 00:08:42 +0000 | [diff] [blame] | 7270 | xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7271 | #endif /* LIBXML_HTML_ENABLED */ |
| 7272 | } else |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7273 | xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL); |
| 7274 | xmlOutputBufferClose(outbuf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7275 | } |
| 7276 | |
| 7277 | /************************************************************************ |
| 7278 | * * |
| 7279 | * Dumping XML tree content to an I/O output buffer * |
| 7280 | * * |
| 7281 | ************************************************************************/ |
| 7282 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 7283 | #ifdef LIBXML_HTML_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7284 | static void |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7285 | xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 7286 | int level, int format, const char *encoding); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 7287 | #endif |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7288 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7289 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 7290 | int level, int format, const char *encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7291 | static void |
| 7292 | xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7293 | xmlNodePtr cur, int level, int format, const char *encoding); |
| 7294 | |
Daniel Veillard | 5ecaf7f | 2003-01-09 13:19:33 +0000 | [diff] [blame] | 7295 | void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur); |
| 7296 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7297 | /** |
| 7298 | * xmlNsDumpOutput: |
| 7299 | * @buf: the XML buffer output |
| 7300 | * @cur: a namespace |
| 7301 | * |
| 7302 | * Dump a local Namespace definition. |
| 7303 | * Should be called in the context of attributes dumps. |
| 7304 | */ |
| 7305 | static void |
| 7306 | xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 7307 | if (cur == NULL) { |
| 7308 | #ifdef DEBUG_TREE |
| 7309 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7310 | "xmlNsDumpOutput : Ns == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7311 | #endif |
| 7312 | return; |
| 7313 | } |
| 7314 | if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 7315 | if (xmlStrEqual(cur->prefix, BAD_CAST "xml")) |
| 7316 | return; |
| 7317 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7318 | /* Within the context of an element attributes */ |
| 7319 | if (cur->prefix != NULL) { |
| 7320 | xmlOutputBufferWriteString(buf, " xmlns:"); |
| 7321 | xmlOutputBufferWriteString(buf, (const char *)cur->prefix); |
| 7322 | } else |
| 7323 | xmlOutputBufferWriteString(buf, " xmlns"); |
| 7324 | xmlOutputBufferWriteString(buf, "="); |
| 7325 | xmlBufferWriteQuotedString(buf->buffer, cur->href); |
| 7326 | } |
| 7327 | } |
| 7328 | |
| 7329 | /** |
| 7330 | * xmlNsListDumpOutput: |
| 7331 | * @buf: the XML buffer output |
| 7332 | * @cur: the first namespace |
| 7333 | * |
| 7334 | * Dump a list of local Namespace definitions. |
| 7335 | * Should be called in the context of attributes dumps. |
| 7336 | */ |
Daniel Veillard | 5ecaf7f | 2003-01-09 13:19:33 +0000 | [diff] [blame] | 7337 | void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7338 | xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 7339 | while (cur != NULL) { |
| 7340 | xmlNsDumpOutput(buf, cur); |
| 7341 | cur = cur->next; |
| 7342 | } |
| 7343 | } |
| 7344 | |
| 7345 | /** |
| 7346 | * xmlDtdDumpOutput: |
| 7347 | * @buf: the XML buffer output |
| 7348 | * @doc: the document |
| 7349 | * @encoding: an optional encoding string |
| 7350 | * |
| 7351 | * Dump the XML document DTD, if any. |
| 7352 | */ |
| 7353 | static void |
| 7354 | xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) { |
| 7355 | if (dtd == NULL) { |
| 7356 | #ifdef DEBUG_TREE |
| 7357 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7358 | "xmlDtdDumpOutput : no internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7359 | #endif |
| 7360 | return; |
| 7361 | } |
| 7362 | xmlOutputBufferWriteString(buf, "<!DOCTYPE "); |
| 7363 | xmlOutputBufferWriteString(buf, (const char *)dtd->name); |
| 7364 | if (dtd->ExternalID != NULL) { |
| 7365 | xmlOutputBufferWriteString(buf, " PUBLIC "); |
| 7366 | xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID); |
| 7367 | xmlOutputBufferWriteString(buf, " "); |
| 7368 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 7369 | } else if (dtd->SystemID != NULL) { |
| 7370 | xmlOutputBufferWriteString(buf, " SYSTEM "); |
| 7371 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 7372 | } |
| 7373 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
William M. Brack | 6f108a1 | 2004-02-11 00:14:52 +0000 | [diff] [blame] | 7374 | (dtd->attributes == NULL) && (dtd->notations == NULL) && |
| 7375 | (dtd->pentities == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7376 | xmlOutputBufferWriteString(buf, ">"); |
| 7377 | return; |
| 7378 | } |
| 7379 | xmlOutputBufferWriteString(buf, " [\n"); |
| 7380 | xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding); |
| 7381 | xmlOutputBufferWriteString(buf, "]>"); |
| 7382 | } |
| 7383 | |
| 7384 | /** |
| 7385 | * xmlAttrDumpOutput: |
| 7386 | * @buf: the XML buffer output |
| 7387 | * @doc: the document |
| 7388 | * @cur: the attribute pointer |
| 7389 | * @encoding: an optional encoding string |
| 7390 | * |
| 7391 | * Dump an XML attribute |
| 7392 | */ |
| 7393 | static void |
| 7394 | xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 7395 | const char *encoding ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7396 | if (cur == NULL) { |
| 7397 | #ifdef DEBUG_TREE |
| 7398 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7399 | "xmlAttrDumpOutput : property == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7400 | #endif |
| 7401 | return; |
| 7402 | } |
| 7403 | xmlOutputBufferWriteString(buf, " "); |
| 7404 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7405 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7406 | xmlOutputBufferWriteString(buf, ":"); |
| 7407 | } |
| 7408 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | a6d0538 | 2002-02-13 13:07:41 +0000 | [diff] [blame] | 7409 | xmlOutputBufferWriteString(buf, "=\""); |
| 7410 | xmlAttrSerializeContent(buf->buffer, doc, cur); |
| 7411 | xmlOutputBufferWriteString(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7412 | } |
| 7413 | |
| 7414 | /** |
| 7415 | * xmlAttrListDumpOutput: |
| 7416 | * @buf: the XML buffer output |
| 7417 | * @doc: the document |
| 7418 | * @cur: the first attribute pointer |
| 7419 | * @encoding: an optional encoding string |
| 7420 | * |
| 7421 | * Dump a list of XML attributes |
| 7422 | */ |
| 7423 | static void |
| 7424 | xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7425 | xmlAttrPtr cur, const char *encoding) { |
| 7426 | if (cur == NULL) { |
| 7427 | #ifdef DEBUG_TREE |
| 7428 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7429 | "xmlAttrListDumpOutput : property == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7430 | #endif |
| 7431 | return; |
| 7432 | } |
| 7433 | while (cur != NULL) { |
| 7434 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 7435 | cur = cur->next; |
| 7436 | } |
| 7437 | } |
| 7438 | |
| 7439 | |
| 7440 | |
| 7441 | /** |
| 7442 | * xmlNodeListDumpOutput: |
| 7443 | * @buf: the XML buffer output |
| 7444 | * @doc: the document |
| 7445 | * @cur: the first node |
| 7446 | * @level: the imbrication level for indenting |
| 7447 | * @format: is formatting allowed |
| 7448 | * @encoding: an optional encoding string |
| 7449 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7450 | * Dump an XML node list, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7451 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7452 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7453 | */ |
| 7454 | static void |
| 7455 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7456 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 7457 | int i; |
| 7458 | |
| 7459 | if (cur == NULL) { |
| 7460 | #ifdef DEBUG_TREE |
| 7461 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7462 | "xmlNodeListDumpOutput : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7463 | #endif |
| 7464 | return; |
| 7465 | } |
| 7466 | while (cur != NULL) { |
| 7467 | if ((format) && (xmlIndentTreeOutput) && |
| 7468 | (cur->type == XML_ELEMENT_NODE)) |
| 7469 | for (i = 0;i < level;i++) |
Aleksey Sanin | 2300256 | 2002-05-24 07:18:40 +0000 | [diff] [blame] | 7470 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7471 | xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7472 | if (format) { |
| 7473 | xmlOutputBufferWriteString(buf, "\n"); |
| 7474 | } |
| 7475 | cur = cur->next; |
| 7476 | } |
| 7477 | } |
| 7478 | |
| 7479 | /** |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7480 | * xmlNodeDumpOutputInternal: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7481 | * @buf: the XML buffer output |
| 7482 | * @doc: the document |
| 7483 | * @cur: the current node |
| 7484 | * @level: the imbrication level for indenting |
| 7485 | * @format: is formatting allowed |
| 7486 | * @encoding: an optional encoding string |
| 7487 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7488 | * Dump an XML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7489 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7490 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7491 | */ |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7492 | static void |
| 7493 | xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7494 | xmlNodePtr cur, int level, int format, const char *encoding) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7495 | int i; |
| 7496 | xmlNodePtr tmp; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 7497 | xmlChar *start, *end; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7498 | |
| 7499 | if (cur == NULL) { |
| 7500 | #ifdef DEBUG_TREE |
| 7501 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7502 | "xmlNodeDumpOutput : node == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7503 | #endif |
| 7504 | return; |
| 7505 | } |
| 7506 | if (cur->type == XML_XINCLUDE_START) |
| 7507 | return; |
| 7508 | if (cur->type == XML_XINCLUDE_END) |
| 7509 | return; |
| 7510 | if (cur->type == XML_DTD_NODE) { |
| 7511 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 7512 | return; |
| 7513 | } |
Daniel Veillard | cec50a6 | 2003-10-28 13:26:51 +0000 | [diff] [blame] | 7514 | if (cur->type == XML_DOCUMENT_FRAG_NODE) { |
| 7515 | xmlNodeListDumpOutput(buf, doc, cur->children, level, format, encoding); |
| 7516 | return; |
| 7517 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7518 | if (cur->type == XML_ELEMENT_DECL) { |
| 7519 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 7520 | return; |
| 7521 | } |
| 7522 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 7523 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 7524 | return; |
| 7525 | } |
| 7526 | if (cur->type == XML_ENTITY_DECL) { |
| 7527 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 7528 | return; |
| 7529 | } |
| 7530 | if (cur->type == XML_TEXT_NODE) { |
| 7531 | if (cur->content != NULL) { |
| 7532 | if ((cur->name == xmlStringText) || |
| 7533 | (cur->name != xmlStringTextNoenc)) { |
| 7534 | xmlChar *buffer; |
| 7535 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7536 | if (encoding == NULL) |
| 7537 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7538 | else |
| 7539 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7540 | if (buffer != NULL) { |
| 7541 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7542 | xmlFree(buffer); |
| 7543 | } |
| 7544 | } else { |
| 7545 | /* |
| 7546 | * Disable escaping, needed for XSLT |
| 7547 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7548 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7549 | } |
| 7550 | } |
| 7551 | |
| 7552 | return; |
| 7553 | } |
| 7554 | if (cur->type == XML_PI_NODE) { |
| 7555 | if (cur->content != NULL) { |
| 7556 | xmlOutputBufferWriteString(buf, "<?"); |
| 7557 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7558 | if (cur->content != NULL) { |
| 7559 | xmlOutputBufferWriteString(buf, " "); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7560 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7561 | } |
| 7562 | xmlOutputBufferWriteString(buf, "?>"); |
| 7563 | } else { |
| 7564 | xmlOutputBufferWriteString(buf, "<?"); |
| 7565 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7566 | xmlOutputBufferWriteString(buf, "?>"); |
| 7567 | } |
| 7568 | return; |
| 7569 | } |
| 7570 | if (cur->type == XML_COMMENT_NODE) { |
| 7571 | if (cur->content != NULL) { |
| 7572 | xmlOutputBufferWriteString(buf, "<!--"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7573 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7574 | xmlOutputBufferWriteString(buf, "-->"); |
| 7575 | } |
| 7576 | return; |
| 7577 | } |
| 7578 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 7579 | xmlOutputBufferWriteString(buf, "&"); |
| 7580 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7581 | xmlOutputBufferWriteString(buf, ";"); |
| 7582 | return; |
| 7583 | } |
| 7584 | if (cur->type == XML_CDATA_SECTION_NODE) { |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 7585 | start = end = cur->content; |
| 7586 | while (*end != '\0') { |
| 7587 | if ((*end == ']') && (*(end + 1) == ']') && (*(end + 2) == '>')) { |
| 7588 | end = end + 2; |
| 7589 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 7590 | xmlOutputBufferWrite(buf, end - start, (const char *)start); |
| 7591 | xmlOutputBufferWriteString(buf, "]]>"); |
| 7592 | start = end; |
| 7593 | } |
| 7594 | end++; |
| 7595 | } |
| 7596 | if (start != end) { |
| 7597 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 7598 | xmlOutputBufferWriteString(buf, (const char *)start); |
| 7599 | xmlOutputBufferWriteString(buf, "]]>"); |
| 7600 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7601 | return; |
| 7602 | } |
Daniel Veillard | 7b72ee5 | 2003-02-27 23:24:53 +0000 | [diff] [blame] | 7603 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 7604 | xmlAttrDumpOutput(buf,doc, (xmlAttrPtr) cur,encoding); |
Daniel Veillard | 7b72ee5 | 2003-02-27 23:24:53 +0000 | [diff] [blame] | 7605 | return; |
| 7606 | } |
Daniel Veillard | 6aa2f60 | 2003-02-10 00:01:56 +0000 | [diff] [blame] | 7607 | if (cur->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7608 | xmlNsDumpOutput(buf, (xmlNsPtr) cur); |
Daniel Veillard | 6aa2f60 | 2003-02-10 00:01:56 +0000 | [diff] [blame] | 7609 | return; |
| 7610 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7611 | |
| 7612 | if (format == 1) { |
| 7613 | tmp = cur->children; |
| 7614 | while (tmp != NULL) { |
William M. Brack | 9ca682f | 2003-10-19 10:01:59 +0000 | [diff] [blame] | 7615 | if ((tmp->type == XML_TEXT_NODE) || |
| 7616 | (tmp->type == XML_CDATA_SECTION_NODE) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7617 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 7618 | format = 0; |
| 7619 | break; |
| 7620 | } |
| 7621 | tmp = tmp->next; |
| 7622 | } |
| 7623 | } |
| 7624 | xmlOutputBufferWriteString(buf, "<"); |
| 7625 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7626 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7627 | xmlOutputBufferWriteString(buf, ":"); |
| 7628 | } |
| 7629 | |
| 7630 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7631 | if (cur->nsDef) |
| 7632 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 7633 | if (cur->properties != NULL) |
| 7634 | xmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 7635 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 7636 | if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) && |
| 7637 | (cur->children == NULL) && (!xmlSaveNoEmptyTags)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7638 | xmlOutputBufferWriteString(buf, "/>"); |
| 7639 | return; |
| 7640 | } |
| 7641 | xmlOutputBufferWriteString(buf, ">"); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 7642 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7643 | xmlChar *buffer; |
| 7644 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7645 | if (encoding == NULL) |
| 7646 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 7647 | else |
| 7648 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7649 | if (buffer != NULL) { |
| 7650 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 7651 | xmlFree(buffer); |
| 7652 | } |
| 7653 | } |
| 7654 | if (cur->children != NULL) { |
| 7655 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 7656 | xmlNodeListDumpOutput(buf, doc, cur->children, |
| 7657 | (level >= 0?level+1:-1), format, encoding); |
| 7658 | if ((xmlIndentTreeOutput) && (format)) |
| 7659 | for (i = 0;i < level;i++) |
Aleksey Sanin | 2300256 | 2002-05-24 07:18:40 +0000 | [diff] [blame] | 7660 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7661 | } |
| 7662 | xmlOutputBufferWriteString(buf, "</"); |
| 7663 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 7664 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 7665 | xmlOutputBufferWriteString(buf, ":"); |
| 7666 | } |
| 7667 | |
| 7668 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 7669 | xmlOutputBufferWriteString(buf, ">"); |
| 7670 | } |
| 7671 | |
| 7672 | /** |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7673 | * xmlNodeDumpOutput: |
| 7674 | * @buf: the XML buffer output |
| 7675 | * @doc: the document |
| 7676 | * @cur: the current node |
| 7677 | * @level: the imbrication level for indenting |
| 7678 | * @format: is formatting allowed |
| 7679 | * @encoding: an optional encoding string |
| 7680 | * |
| 7681 | * Dump an XML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7682 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7683 | * or xmlKeepBlanksDefault(0) was called |
| 7684 | */ |
| 7685 | void |
| 7686 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7687 | int level, int format, const char *encoding) |
| 7688 | { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7689 | #ifdef LIBXML_HTML_ENABLED |
| 7690 | xmlDtdPtr dtd; |
| 7691 | int is_xhtml = 0; |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7692 | #endif |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7693 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7694 | xmlInitParser(); |
| 7695 | |
| 7696 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7697 | dtd = xmlGetIntSubset(doc); |
| 7698 | if (dtd != NULL) { |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7699 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 7700 | if (is_xhtml < 0) |
| 7701 | is_xhtml = 0; |
| 7702 | if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) && |
| 7703 | (cur->type == XML_ELEMENT_NODE) && |
| 7704 | (xmlStrEqual(cur->name, BAD_CAST "html"))) { |
| 7705 | if (encoding != NULL) |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 7706 | htmlSetMetaEncoding((htmlDocPtr) doc, |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7707 | (const xmlChar *) encoding); |
| 7708 | else |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 7709 | htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8"); |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7710 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7711 | } |
| 7712 | |
| 7713 | if (is_xhtml) |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7714 | xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7715 | else |
| 7716 | #endif |
Daniel Veillard | ebc4ca9 | 2002-11-27 11:43:05 +0000 | [diff] [blame] | 7717 | xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7718 | } |
| 7719 | |
| 7720 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7721 | * xmlDocContentDumpOutput: |
| 7722 | * @buf: the XML buffer output |
| 7723 | * @cur: the document |
| 7724 | * @encoding: an optional encoding string |
| 7725 | * @format: should formatting spaces been added |
| 7726 | * |
| 7727 | * Dump an XML document. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 7728 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 7729 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7730 | */ |
| 7731 | static void |
| 7732 | xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 7733 | const char *encoding, int format) { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7734 | #ifdef LIBXML_HTML_ENABLED |
| 7735 | xmlDtdPtr dtd; |
| 7736 | int is_xhtml = 0; |
| 7737 | #endif |
Daniel Veillard | 2f6ff81 | 2003-12-08 12:11:14 +0000 | [diff] [blame] | 7738 | const xmlChar *oldenc = cur->encoding; |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7739 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 7740 | xmlInitParser(); |
| 7741 | |
Daniel Veillard | 2f6ff81 | 2003-12-08 12:11:14 +0000 | [diff] [blame] | 7742 | if (encoding != NULL) |
| 7743 | cur->encoding = BAD_CAST encoding; |
| 7744 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7745 | xmlOutputBufferWriteString(buf, "<?xml version="); |
| 7746 | if (cur->version != NULL) |
| 7747 | xmlBufferWriteQuotedString(buf->buffer, cur->version); |
| 7748 | else |
| 7749 | xmlOutputBufferWriteString(buf, "\"1.0\""); |
| 7750 | if (encoding == NULL) { |
| 7751 | if (cur->encoding != NULL) |
| 7752 | encoding = (const char *) cur->encoding; |
| 7753 | else if (cur->charset != XML_CHAR_ENCODING_UTF8) |
| 7754 | encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset); |
| 7755 | } |
| 7756 | if (encoding != NULL) { |
| 7757 | xmlOutputBufferWriteString(buf, " encoding="); |
| 7758 | xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding); |
| 7759 | } |
| 7760 | switch (cur->standalone) { |
| 7761 | case 0: |
| 7762 | xmlOutputBufferWriteString(buf, " standalone=\"no\""); |
| 7763 | break; |
| 7764 | case 1: |
| 7765 | xmlOutputBufferWriteString(buf, " standalone=\"yes\""); |
| 7766 | break; |
| 7767 | } |
| 7768 | xmlOutputBufferWriteString(buf, "?>\n"); |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7769 | |
| 7770 | #ifdef LIBXML_HTML_ENABLED |
| 7771 | dtd = xmlGetIntSubset(cur); |
| 7772 | if (dtd != NULL) { |
| 7773 | is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID); |
| 7774 | if (is_xhtml < 0) is_xhtml = 0; |
| 7775 | } |
| 7776 | if (is_xhtml) { |
| 7777 | if (encoding != NULL) |
| 7778 | htmlSetMetaEncoding(cur, (const xmlChar *) encoding); |
| 7779 | else |
| 7780 | htmlSetMetaEncoding(cur, BAD_CAST "UTF-8"); |
| 7781 | } |
| 7782 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7783 | if (cur->children != NULL) { |
| 7784 | xmlNodePtr child = cur->children; |
| 7785 | |
| 7786 | while (child != NULL) { |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7787 | #ifdef LIBXML_HTML_ENABLED |
| 7788 | if (is_xhtml) |
| 7789 | xhtmlNodeDumpOutput(buf, cur, child, 0, format, encoding); |
| 7790 | else |
| 7791 | #endif |
| 7792 | xmlNodeDumpOutputInternal(buf, cur, child, 0, format, encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7793 | xmlOutputBufferWriteString(buf, "\n"); |
| 7794 | child = child->next; |
| 7795 | } |
| 7796 | } |
Daniel Veillard | 2f6ff81 | 2003-12-08 12:11:14 +0000 | [diff] [blame] | 7797 | if (encoding != NULL) |
| 7798 | cur->encoding = oldenc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7799 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7800 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7801 | |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7802 | #ifdef LIBXML_HTML_ENABLED |
| 7803 | /************************************************************************ |
| 7804 | * * |
| 7805 | * Functions specific to XHTML serialization * |
| 7806 | * * |
| 7807 | ************************************************************************/ |
| 7808 | |
| 7809 | #define XHTML_STRICT_PUBLIC_ID BAD_CAST \ |
| 7810 | "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 7811 | #define XHTML_STRICT_SYSTEM_ID BAD_CAST \ |
| 7812 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" |
| 7813 | #define XHTML_FRAME_PUBLIC_ID BAD_CAST \ |
| 7814 | "-//W3C//DTD XHTML 1.0 Frameset//EN" |
| 7815 | #define XHTML_FRAME_SYSTEM_ID BAD_CAST \ |
| 7816 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd" |
| 7817 | #define XHTML_TRANS_PUBLIC_ID BAD_CAST \ |
| 7818 | "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 7819 | #define XHTML_TRANS_SYSTEM_ID BAD_CAST \ |
| 7820 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" |
| 7821 | |
| 7822 | #define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml" |
| 7823 | /** |
| 7824 | * xmlIsXHTML: |
| 7825 | * @systemID: the system identifier |
| 7826 | * @publicID: the public identifier |
| 7827 | * |
| 7828 | * Try to find if the document correspond to an XHTML DTD |
| 7829 | * |
| 7830 | * Returns 1 if true, 0 if not and -1 in case of error |
| 7831 | */ |
| 7832 | int |
| 7833 | xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) { |
| 7834 | if ((systemID == NULL) && (publicID == NULL)) |
| 7835 | return(-1); |
| 7836 | if (publicID != NULL) { |
| 7837 | if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1); |
| 7838 | if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1); |
| 7839 | if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1); |
| 7840 | } |
| 7841 | if (systemID != NULL) { |
| 7842 | if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1); |
| 7843 | if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1); |
| 7844 | if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1); |
| 7845 | } |
| 7846 | return(0); |
| 7847 | } |
| 7848 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7849 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7850 | /** |
| 7851 | * xhtmlIsEmpty: |
| 7852 | * @node: the node |
| 7853 | * |
| 7854 | * Check if a node is an empty xhtml node |
| 7855 | * |
| 7856 | * Returns 1 if the node is an empty node, 0 if not and -1 in case of error |
| 7857 | */ |
| 7858 | static int |
| 7859 | xhtmlIsEmpty(xmlNodePtr node) { |
| 7860 | if (node == NULL) |
| 7861 | return(-1); |
| 7862 | if (node->type != XML_ELEMENT_NODE) |
| 7863 | return(0); |
| 7864 | if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME))) |
| 7865 | return(0); |
| 7866 | if (node->children != NULL) |
| 7867 | return(0); |
| 7868 | switch (node->name[0]) { |
| 7869 | case 'a': |
| 7870 | if (xmlStrEqual(node->name, BAD_CAST "area")) |
| 7871 | return(1); |
| 7872 | return(0); |
| 7873 | case 'b': |
| 7874 | if (xmlStrEqual(node->name, BAD_CAST "br")) |
| 7875 | return(1); |
| 7876 | if (xmlStrEqual(node->name, BAD_CAST "base")) |
| 7877 | return(1); |
| 7878 | if (xmlStrEqual(node->name, BAD_CAST "basefont")) |
| 7879 | return(1); |
| 7880 | return(0); |
| 7881 | case 'c': |
| 7882 | if (xmlStrEqual(node->name, BAD_CAST "col")) |
| 7883 | return(1); |
| 7884 | return(0); |
| 7885 | case 'f': |
| 7886 | if (xmlStrEqual(node->name, BAD_CAST "frame")) |
| 7887 | return(1); |
| 7888 | return(0); |
| 7889 | case 'h': |
| 7890 | if (xmlStrEqual(node->name, BAD_CAST "hr")) |
| 7891 | return(1); |
| 7892 | return(0); |
| 7893 | case 'i': |
| 7894 | if (xmlStrEqual(node->name, BAD_CAST "img")) |
| 7895 | return(1); |
| 7896 | if (xmlStrEqual(node->name, BAD_CAST "input")) |
| 7897 | return(1); |
| 7898 | if (xmlStrEqual(node->name, BAD_CAST "isindex")) |
| 7899 | return(1); |
| 7900 | return(0); |
| 7901 | case 'l': |
| 7902 | if (xmlStrEqual(node->name, BAD_CAST "link")) |
| 7903 | return(1); |
| 7904 | return(0); |
| 7905 | case 'm': |
| 7906 | if (xmlStrEqual(node->name, BAD_CAST "meta")) |
| 7907 | return(1); |
| 7908 | return(0); |
| 7909 | case 'p': |
| 7910 | if (xmlStrEqual(node->name, BAD_CAST "param")) |
| 7911 | return(1); |
| 7912 | return(0); |
| 7913 | } |
| 7914 | return(0); |
| 7915 | } |
| 7916 | |
| 7917 | /** |
| 7918 | * xhtmlAttrListDumpOutput: |
| 7919 | * @buf: the XML buffer output |
| 7920 | * @doc: the document |
| 7921 | * @cur: the first attribute pointer |
| 7922 | * @encoding: an optional encoding string |
| 7923 | * |
| 7924 | * Dump a list of XML attributes |
| 7925 | */ |
| 7926 | static void |
| 7927 | xhtmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 7928 | xmlAttrPtr cur, const char *encoding) { |
| 7929 | xmlAttrPtr xml_lang = NULL; |
| 7930 | xmlAttrPtr lang = NULL; |
| 7931 | xmlAttrPtr name = NULL; |
| 7932 | xmlAttrPtr id = NULL; |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7933 | xmlNodePtr parent; |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7934 | |
| 7935 | if (cur == NULL) { |
| 7936 | #ifdef DEBUG_TREE |
| 7937 | xmlGenericError(xmlGenericErrorContext, |
| 7938 | "xmlAttrListDumpOutput : property == NULL\n"); |
| 7939 | #endif |
| 7940 | return; |
| 7941 | } |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7942 | parent = cur->parent; |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7943 | while (cur != NULL) { |
| 7944 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id"))) |
| 7945 | id = cur; |
| 7946 | else |
| 7947 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name"))) |
| 7948 | name = cur; |
| 7949 | else |
| 7950 | if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang"))) |
| 7951 | lang = cur; |
| 7952 | else |
| 7953 | if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) && |
| 7954 | (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml"))) |
| 7955 | xml_lang = cur; |
| 7956 | else if ((cur->ns == NULL) && |
| 7957 | ((cur->children == NULL) || |
| 7958 | (cur->children->content == NULL) || |
| 7959 | (cur->children->content[0] == 0)) && |
| 7960 | (htmlIsBooleanAttr(cur->name))) { |
| 7961 | if (cur->children != NULL) |
| 7962 | xmlFreeNode(cur->children); |
| 7963 | cur->children = xmlNewText(cur->name); |
| 7964 | if (cur->children != NULL) |
| 7965 | cur->children->parent = (xmlNodePtr) cur; |
| 7966 | } |
| 7967 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 7968 | cur = cur->next; |
| 7969 | } |
| 7970 | /* |
| 7971 | * C.8 |
| 7972 | */ |
| 7973 | if ((name != NULL) && (id == NULL)) { |
Daniel Veillard | fd7ce5f | 2003-02-10 16:12:39 +0000 | [diff] [blame] | 7974 | if ((parent != NULL) && (parent->name != NULL) && |
| 7975 | ((xmlStrEqual(parent->name, BAD_CAST "a")) || |
| 7976 | (xmlStrEqual(parent->name, BAD_CAST "p")) || |
| 7977 | (xmlStrEqual(parent->name, BAD_CAST "div")) || |
| 7978 | (xmlStrEqual(parent->name, BAD_CAST "img")) || |
| 7979 | (xmlStrEqual(parent->name, BAD_CAST "map")) || |
| 7980 | (xmlStrEqual(parent->name, BAD_CAST "applet")) || |
| 7981 | (xmlStrEqual(parent->name, BAD_CAST "form")) || |
| 7982 | (xmlStrEqual(parent->name, BAD_CAST "frame")) || |
| 7983 | (xmlStrEqual(parent->name, BAD_CAST "iframe")))) { |
| 7984 | xmlOutputBufferWriteString(buf, " id=\""); |
| 7985 | xmlAttrSerializeContent(buf->buffer, doc, name); |
| 7986 | xmlOutputBufferWriteString(buf, "\""); |
| 7987 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 7988 | } |
| 7989 | /* |
| 7990 | * C.7. |
| 7991 | */ |
| 7992 | if ((lang != NULL) && (xml_lang == NULL)) { |
| 7993 | xmlOutputBufferWriteString(buf, " xml:lang=\""); |
| 7994 | xmlAttrSerializeContent(buf->buffer, doc, lang); |
| 7995 | xmlOutputBufferWriteString(buf, "\""); |
| 7996 | } else |
| 7997 | if ((xml_lang != NULL) && (lang == NULL)) { |
| 7998 | xmlOutputBufferWriteString(buf, " lang=\""); |
| 7999 | xmlAttrSerializeContent(buf->buffer, doc, xml_lang); |
| 8000 | xmlOutputBufferWriteString(buf, "\""); |
| 8001 | } |
| 8002 | } |
| 8003 | |
| 8004 | /** |
| 8005 | * xhtmlNodeListDumpOutput: |
| 8006 | * @buf: the XML buffer output |
| 8007 | * @doc: the XHTML document |
| 8008 | * @cur: the first node |
| 8009 | * @level: the imbrication level for indenting |
| 8010 | * @format: is formatting allowed |
| 8011 | * @encoding: an optional encoding string |
| 8012 | * |
| 8013 | * Dump an XML node list, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8014 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 8015 | * or xmlKeepBlanksDefault(0) was called |
| 8016 | */ |
| 8017 | static void |
| 8018 | xhtmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 8019 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 8020 | int i; |
| 8021 | |
| 8022 | if (cur == NULL) { |
| 8023 | #ifdef DEBUG_TREE |
| 8024 | xmlGenericError(xmlGenericErrorContext, |
| 8025 | "xhtmlNodeListDumpOutput : node == NULL\n"); |
| 8026 | #endif |
| 8027 | return; |
| 8028 | } |
| 8029 | while (cur != NULL) { |
| 8030 | if ((format) && (xmlIndentTreeOutput) && |
| 8031 | (cur->type == XML_ELEMENT_NODE)) |
| 8032 | for (i = 0;i < level;i++) |
| 8033 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
| 8034 | xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
| 8035 | if (format) { |
| 8036 | xmlOutputBufferWriteString(buf, "\n"); |
| 8037 | } |
| 8038 | cur = cur->next; |
| 8039 | } |
| 8040 | } |
| 8041 | |
| 8042 | /** |
| 8043 | * xhtmlNodeDumpOutput: |
| 8044 | * @buf: the XML buffer output |
| 8045 | * @doc: the XHTML document |
| 8046 | * @cur: the current node |
| 8047 | * @level: the imbrication level for indenting |
| 8048 | * @format: is formatting allowed |
| 8049 | * @encoding: an optional encoding string |
| 8050 | * |
| 8051 | * Dump an XHTML node, recursive behaviour, children are printed too. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8052 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 8053 | * or xmlKeepBlanksDefault(0) was called |
| 8054 | */ |
| 8055 | static void |
| 8056 | xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 8057 | int level, int format, const char *encoding) { |
| 8058 | int i; |
| 8059 | xmlNodePtr tmp; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 8060 | xmlChar *start, *end; |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 8061 | |
| 8062 | if (cur == NULL) { |
| 8063 | #ifdef DEBUG_TREE |
| 8064 | xmlGenericError(xmlGenericErrorContext, |
| 8065 | "xmlNodeDumpOutput : node == NULL\n"); |
| 8066 | #endif |
| 8067 | return; |
| 8068 | } |
| 8069 | if (cur->type == XML_XINCLUDE_START) |
| 8070 | return; |
| 8071 | if (cur->type == XML_XINCLUDE_END) |
| 8072 | return; |
| 8073 | if (cur->type == XML_DTD_NODE) { |
| 8074 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 8075 | return; |
| 8076 | } |
| 8077 | if (cur->type == XML_ELEMENT_DECL) { |
| 8078 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 8079 | return; |
| 8080 | } |
| 8081 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 8082 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 8083 | return; |
| 8084 | } |
| 8085 | if (cur->type == XML_ENTITY_DECL) { |
| 8086 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 8087 | return; |
| 8088 | } |
| 8089 | if (cur->type == XML_TEXT_NODE) { |
| 8090 | if (cur->content != NULL) { |
| 8091 | if ((cur->name == xmlStringText) || |
| 8092 | (cur->name != xmlStringTextNoenc)) { |
| 8093 | xmlChar *buffer; |
| 8094 | |
| 8095 | if (encoding == NULL) |
| 8096 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 8097 | else |
| 8098 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 8099 | if (buffer != NULL) { |
| 8100 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 8101 | xmlFree(buffer); |
| 8102 | } |
| 8103 | } else { |
| 8104 | /* |
| 8105 | * Disable escaping, needed for XSLT |
| 8106 | */ |
| 8107 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
| 8108 | } |
| 8109 | } |
| 8110 | |
| 8111 | return; |
| 8112 | } |
| 8113 | if (cur->type == XML_PI_NODE) { |
| 8114 | if (cur->content != NULL) { |
| 8115 | xmlOutputBufferWriteString(buf, "<?"); |
| 8116 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 8117 | if (cur->content != NULL) { |
| 8118 | xmlOutputBufferWriteString(buf, " "); |
| 8119 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 8120 | } |
| 8121 | xmlOutputBufferWriteString(buf, "?>"); |
| 8122 | } else { |
| 8123 | xmlOutputBufferWriteString(buf, "<?"); |
| 8124 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 8125 | xmlOutputBufferWriteString(buf, "?>"); |
| 8126 | } |
| 8127 | return; |
| 8128 | } |
| 8129 | if (cur->type == XML_COMMENT_NODE) { |
| 8130 | if (cur->content != NULL) { |
| 8131 | xmlOutputBufferWriteString(buf, "<!--"); |
| 8132 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 8133 | xmlOutputBufferWriteString(buf, "-->"); |
| 8134 | } |
| 8135 | return; |
| 8136 | } |
| 8137 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 8138 | xmlOutputBufferWriteString(buf, "&"); |
| 8139 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 8140 | xmlOutputBufferWriteString(buf, ";"); |
| 8141 | return; |
| 8142 | } |
| 8143 | if (cur->type == XML_CDATA_SECTION_NODE) { |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 8144 | start = end = cur->content; |
| 8145 | while (*end != '\0') { |
| 8146 | if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') { |
| 8147 | end = end + 2; |
| 8148 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 8149 | xmlOutputBufferWrite(buf, end - start, (const char *)start); |
| 8150 | xmlOutputBufferWriteString(buf, "]]>"); |
| 8151 | start = end; |
| 8152 | } |
| 8153 | end++; |
| 8154 | } |
| 8155 | if (start != end) { |
| 8156 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 8157 | xmlOutputBufferWriteString(buf, (const char *)start); |
| 8158 | xmlOutputBufferWriteString(buf, "]]>"); |
| 8159 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 8160 | return; |
| 8161 | } |
| 8162 | |
| 8163 | if (format == 1) { |
| 8164 | tmp = cur->children; |
| 8165 | while (tmp != NULL) { |
| 8166 | if ((tmp->type == XML_TEXT_NODE) || |
| 8167 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 8168 | format = 0; |
| 8169 | break; |
| 8170 | } |
| 8171 | tmp = tmp->next; |
| 8172 | } |
| 8173 | } |
| 8174 | xmlOutputBufferWriteString(buf, "<"); |
| 8175 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 8176 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 8177 | xmlOutputBufferWriteString(buf, ":"); |
| 8178 | } |
| 8179 | |
| 8180 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 8181 | if (cur->nsDef) |
| 8182 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 8183 | if ((xmlStrEqual(cur->name, BAD_CAST "html") && |
| 8184 | (cur->ns == NULL) && (cur->nsDef == NULL))) { |
| 8185 | /* |
| 8186 | * 3.1.1. Strictly Conforming Documents A.3.1.1 3/ |
| 8187 | */ |
| 8188 | xmlOutputBufferWriteString(buf, |
| 8189 | " xmlns=\"http://www.w3.org/1999/xhtml\""); |
| 8190 | } |
| 8191 | if (cur->properties != NULL) |
| 8192 | xhtmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 8193 | |
| 8194 | if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) { |
| 8195 | if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) && |
| 8196 | (xhtmlIsEmpty(cur) == 1)) { |
| 8197 | /* |
| 8198 | * C.2. Empty Elements |
| 8199 | */ |
| 8200 | xmlOutputBufferWriteString(buf, " />"); |
| 8201 | } else { |
| 8202 | /* |
| 8203 | * C.3. Element Minimization and Empty Element Content |
| 8204 | */ |
| 8205 | xmlOutputBufferWriteString(buf, "></"); |
| 8206 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 8207 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 8208 | xmlOutputBufferWriteString(buf, ":"); |
| 8209 | } |
| 8210 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 8211 | xmlOutputBufferWriteString(buf, ">"); |
| 8212 | } |
| 8213 | return; |
| 8214 | } |
| 8215 | xmlOutputBufferWriteString(buf, ">"); |
| 8216 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
| 8217 | xmlChar *buffer; |
| 8218 | |
| 8219 | if (encoding == NULL) |
| 8220 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 8221 | else |
| 8222 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 8223 | if (buffer != NULL) { |
| 8224 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 8225 | xmlFree(buffer); |
| 8226 | } |
| 8227 | } |
| 8228 | |
| 8229 | /* |
| 8230 | * 4.8. Script and Style elements |
| 8231 | */ |
| 8232 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8233 | ((xmlStrEqual(cur->name, BAD_CAST "script")) || |
| 8234 | (xmlStrEqual(cur->name, BAD_CAST "style"))) && |
| 8235 | ((cur->ns == NULL) || |
| 8236 | (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) { |
| 8237 | xmlNodePtr child = cur->children; |
| 8238 | |
| 8239 | while (child != NULL) { |
| 8240 | if ((child->type == XML_TEXT_NODE) || |
| 8241 | (child->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 64b3528 | 2002-12-04 15:10:40 +0000 | [diff] [blame] | 8242 | /* |
| 8243 | * Apparently CDATA escaping for style just break on IE, |
| 8244 | * mozilla and galeon, so ... |
| 8245 | */ |
| 8246 | if (xmlStrEqual(cur->name, BAD_CAST "style") && |
| 8247 | (xmlStrchr(child->content, '<') == NULL) && |
| 8248 | (xmlStrchr(child->content, '>') == NULL) && |
| 8249 | (xmlStrchr(child->content, '&') == NULL)) { |
| 8250 | xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding); |
| 8251 | } else { |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 8252 | start = end = child->content; |
| 8253 | while (*end != '\0') { |
| 8254 | if (*end == ']' && |
| 8255 | *(end + 1) == ']' && |
| 8256 | *(end + 2) == '>') { |
| 8257 | end = end + 2; |
| 8258 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 8259 | xmlOutputBufferWrite(buf, end - start, |
| 8260 | (const char *)start); |
| 8261 | xmlOutputBufferWriteString(buf, "]]>"); |
| 8262 | start = end; |
| 8263 | } |
| 8264 | end++; |
| 8265 | } |
| 8266 | if (start != end) { |
| 8267 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 8268 | xmlOutputBufferWriteString(buf, (const char *)start); |
| 8269 | xmlOutputBufferWriteString(buf, "]]>"); |
| 8270 | } |
Daniel Veillard | 64b3528 | 2002-12-04 15:10:40 +0000 | [diff] [blame] | 8271 | } |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 8272 | } else { |
| 8273 | xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding); |
| 8274 | } |
| 8275 | child = child->next; |
| 8276 | } |
| 8277 | } else if (cur->children != NULL) { |
| 8278 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 8279 | xhtmlNodeListDumpOutput(buf, doc, cur->children, |
| 8280 | (level >= 0?level+1:-1), format, encoding); |
| 8281 | if ((xmlIndentTreeOutput) && (format)) |
| 8282 | for (i = 0;i < level;i++) |
| 8283 | xmlOutputBufferWriteString(buf, xmlTreeIndentString); |
| 8284 | } |
| 8285 | xmlOutputBufferWriteString(buf, "</"); |
| 8286 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 8287 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 8288 | xmlOutputBufferWriteString(buf, ":"); |
| 8289 | } |
| 8290 | |
| 8291 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 8292 | xmlOutputBufferWriteString(buf, ">"); |
| 8293 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 8294 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 8295 | #endif |
| 8296 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 8297 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8298 | /************************************************************************ |
| 8299 | * * |
| 8300 | * Saving functions front-ends * |
| 8301 | * * |
| 8302 | ************************************************************************/ |
| 8303 | |
| 8304 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 8305 | * xmlDocDumpFormatMemoryEnc: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8306 | * @out_doc: Document to generate XML text from |
| 8307 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 8308 | * @doc_txt_len: Length of the generated XML text |
| 8309 | * @txt_encoding: Character encoding to use when generating XML text |
| 8310 | * @format: should formatting spaces been added |
| 8311 | * |
| 8312 | * Dump the current DOM tree into memory using the character encoding specified |
| 8313 | * 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] | 8314 | * allocated memory with xmlFree(). |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8315 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 8316 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8317 | */ |
| 8318 | |
| 8319 | void |
| 8320 | xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8321 | int * doc_txt_len, const char * txt_encoding, |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8322 | int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8323 | int dummy = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8324 | xmlOutputBufferPtr out_buff = NULL; |
| 8325 | xmlCharEncodingHandlerPtr conv_hdlr = NULL; |
| 8326 | |
| 8327 | if (doc_txt_len == NULL) { |
| 8328 | doc_txt_len = &dummy; /* Continue, caller just won't get length */ |
| 8329 | } |
| 8330 | |
| 8331 | if (doc_txt_ptr == NULL) { |
| 8332 | *doc_txt_len = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8333 | return; |
| 8334 | } |
| 8335 | |
| 8336 | *doc_txt_ptr = NULL; |
| 8337 | *doc_txt_len = 0; |
| 8338 | |
| 8339 | if (out_doc == NULL) { |
| 8340 | /* No document, no output */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8341 | return; |
| 8342 | } |
| 8343 | |
| 8344 | /* |
| 8345 | * Validate the encoding value, if provided. |
| 8346 | * This logic is copied from xmlSaveFileEnc. |
| 8347 | */ |
| 8348 | |
| 8349 | if (txt_encoding == NULL) |
| 8350 | txt_encoding = (const char *) out_doc->encoding; |
| 8351 | if (txt_encoding != NULL) { |
Daniel Veillard | 3671190 | 2004-02-11 13:25:26 +0000 | [diff] [blame] | 8352 | conv_hdlr = xmlFindCharEncodingHandler(txt_encoding); |
| 8353 | if ( conv_hdlr == NULL ) { |
| 8354 | xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc, |
| 8355 | txt_encoding); |
| 8356 | return; |
| 8357 | } |
| 8358 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8359 | |
| 8360 | if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 8361 | xmlSaveErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8362 | return; |
| 8363 | } |
| 8364 | |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8365 | xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8366 | xmlOutputBufferFlush(out_buff); |
| 8367 | if (out_buff->conv != NULL) { |
| 8368 | *doc_txt_len = out_buff->conv->use; |
| 8369 | *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len); |
| 8370 | } else { |
| 8371 | *doc_txt_len = out_buff->buffer->use; |
| 8372 | *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len); |
| 8373 | } |
| 8374 | (void)xmlOutputBufferClose(out_buff); |
| 8375 | |
| 8376 | if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) { |
| 8377 | *doc_txt_len = 0; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 8378 | xmlSaveErrMemory("creating output"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8379 | } |
| 8380 | |
| 8381 | return; |
| 8382 | } |
| 8383 | |
| 8384 | /** |
| 8385 | * xmlDocDumpMemory: |
| 8386 | * @cur: the document |
| 8387 | * @mem: OUT: the memory pointer |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 8388 | * @size: OUT: the memory length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8389 | * |
Daniel Veillard | 49138f1 | 2004-02-19 12:58:36 +0000 | [diff] [blame] | 8390 | * Dump an XML document in memory and return the #xmlChar * and it's size |
| 8391 | * in bytes. It's up to the caller to free the memory with xmlFree(). |
| 8392 | * The resulting byte array is zero terminated, though the last 0 is not |
| 8393 | * included in the returned size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8394 | */ |
| 8395 | void |
| 8396 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
| 8397 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0); |
| 8398 | } |
| 8399 | |
| 8400 | /** |
| 8401 | * xmlDocDumpFormatMemory: |
| 8402 | * @cur: the document |
| 8403 | * @mem: OUT: the memory pointer |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 8404 | * @size: OUT: the memory length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8405 | * @format: should formatting spaces been added |
| 8406 | * |
| 8407 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8408 | * 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] | 8409 | * It's up to the caller to free the memory with xmlFree(). |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8410 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 8411 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8412 | */ |
| 8413 | void |
| 8414 | xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) { |
| 8415 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format); |
| 8416 | } |
| 8417 | |
| 8418 | /** |
| 8419 | * xmlDocDumpMemoryEnc: |
| 8420 | * @out_doc: Document to generate XML text from |
| 8421 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 8422 | * @doc_txt_len: Length of the generated XML text |
| 8423 | * @txt_encoding: Character encoding to use when generating XML text |
| 8424 | * |
| 8425 | * Dump the current DOM tree into memory using the character encoding specified |
| 8426 | * 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] | 8427 | * allocated memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8428 | */ |
| 8429 | |
| 8430 | void |
| 8431 | xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 8432 | int * doc_txt_len, const char * txt_encoding) { |
| 8433 | xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len, |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8434 | txt_encoding, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8435 | } |
| 8436 | |
| 8437 | /** |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8438 | * xmlDocFormatDump: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8439 | * @f: the FILE* |
| 8440 | * @cur: the document |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8441 | * @format: should formatting spaces been added |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8442 | * |
| 8443 | * Dump an XML document to an open FILE. |
| 8444 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8445 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8446 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8447 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8448 | */ |
| 8449 | int |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8450 | xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8451 | xmlOutputBufferPtr buf; |
| 8452 | const char * encoding; |
| 8453 | xmlCharEncodingHandlerPtr handler = NULL; |
| 8454 | int ret; |
| 8455 | |
| 8456 | if (cur == NULL) { |
| 8457 | #ifdef DEBUG_TREE |
| 8458 | xmlGenericError(xmlGenericErrorContext, |
| 8459 | "xmlDocDump : document == NULL\n"); |
| 8460 | #endif |
| 8461 | return(-1); |
| 8462 | } |
| 8463 | encoding = (const char *) cur->encoding; |
| 8464 | |
| 8465 | if (encoding != NULL) { |
Daniel Veillard | e132611 | 2003-06-05 09:32:20 +0000 | [diff] [blame] | 8466 | handler = xmlFindCharEncodingHandler(encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8467 | if (handler == NULL) { |
| 8468 | xmlFree((char *) cur->encoding); |
| 8469 | cur->encoding = NULL; |
| 8470 | } |
| 8471 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8472 | buf = xmlOutputBufferCreateFile(f, handler); |
| 8473 | if (buf == NULL) return(-1); |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8474 | xmlDocContentDumpOutput(buf, cur, NULL, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8475 | |
| 8476 | ret = xmlOutputBufferClose(buf); |
| 8477 | return(ret); |
| 8478 | } |
| 8479 | |
| 8480 | /** |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8481 | * xmlDocDump: |
| 8482 | * @f: the FILE* |
| 8483 | * @cur: the document |
| 8484 | * |
| 8485 | * Dump an XML document to an open FILE. |
| 8486 | * |
| 8487 | * returns: the number of bytes written or -1 in case of failure. |
| 8488 | */ |
| 8489 | int |
| 8490 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 8491 | return(xmlDocFormatDump (f, cur, 0)); |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 8492 | } |
| 8493 | |
| 8494 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8495 | * xmlSaveFileTo: |
| 8496 | * @buf: an output I/O buffer |
| 8497 | * @cur: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8498 | * @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] | 8499 | * |
| 8500 | * Dump an XML document to an I/O buffer. |
| 8501 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8502 | * returns: the number of bytes written or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8503 | */ |
| 8504 | int |
CET 2001 Daniel Veillard | 5a37bde | 2001-11-01 14:31:22 +0000 | [diff] [blame] | 8505 | xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8506 | int ret; |
| 8507 | |
| 8508 | if (buf == NULL) return(0); |
Daniel Veillard | 1731d6a | 2001-04-10 16:38:06 +0000 | [diff] [blame] | 8509 | xmlDocContentDumpOutput(buf, cur, encoding, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8510 | ret = xmlOutputBufferClose(buf); |
| 8511 | return(ret); |
| 8512 | } |
| 8513 | |
| 8514 | /** |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8515 | * xmlSaveFormatFileTo: |
| 8516 | * @buf: an output I/O buffer |
| 8517 | * @cur: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8518 | * @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] | 8519 | * @format: should formatting spaces been added |
| 8520 | * |
| 8521 | * Dump an XML document to an I/O buffer. |
| 8522 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8523 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8524 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8525 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8526 | */ |
| 8527 | int |
CET 2001 Daniel Veillard | 5a37bde | 2001-11-01 14:31:22 +0000 | [diff] [blame] | 8528 | xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) { |
Daniel Veillard | eefd449 | 2001-04-28 16:55:50 +0000 | [diff] [blame] | 8529 | int ret; |
| 8530 | |
| 8531 | if (buf == NULL) return(0); |
| 8532 | xmlDocContentDumpOutput(buf, cur, encoding, format); |
| 8533 | ret = xmlOutputBufferClose(buf); |
| 8534 | return(ret); |
| 8535 | } |
| 8536 | |
| 8537 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 8538 | * xmlSaveFormatFileEnc: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8539 | * @filename: the filename or URL to output |
| 8540 | * @cur: the document being saved |
| 8541 | * @encoding: the name of the encoding to use or NULL. |
| 8542 | * @format: should formatting spaces be added. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8543 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 8544 | * Dump an XML document to a file or an URL. |
| 8545 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8546 | * Returns the number of bytes written or -1 in case of error. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 8547 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8548 | * or xmlKeepBlanksDefault(0) was called |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8549 | */ |
| 8550 | int |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8551 | xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur, |
| 8552 | const char * encoding, int format ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8553 | xmlOutputBufferPtr buf; |
| 8554 | xmlCharEncodingHandlerPtr handler = NULL; |
| 8555 | int ret; |
| 8556 | |
Daniel Veillard | 4dbe77a | 2003-01-14 00:17:42 +0000 | [diff] [blame] | 8557 | if (cur == NULL) |
| 8558 | return(-1); |
| 8559 | |
Daniel Veillard | fb25a51 | 2002-01-13 20:32:08 +0000 | [diff] [blame] | 8560 | if (encoding == NULL) |
| 8561 | encoding = (const char *) cur->encoding; |
| 8562 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8563 | if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8564 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8565 | handler = xmlFindCharEncodingHandler(encoding); |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 8566 | if (handler == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8567 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8568 | } |
| 8569 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8570 | #ifdef HAVE_ZLIB_H |
| 8571 | if (cur->compression < 0) cur->compression = xmlCompressMode; |
| 8572 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8573 | /* |
| 8574 | * save the content to a temp buffer. |
| 8575 | */ |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8576 | buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8577 | if (buf == NULL) return(-1); |
| 8578 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8579 | xmlDocContentDumpOutput(buf, cur, encoding, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8580 | |
| 8581 | ret = xmlOutputBufferClose(buf); |
| 8582 | return(ret); |
| 8583 | } |
| 8584 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8585 | |
| 8586 | /** |
| 8587 | * xmlSaveFileEnc: |
| 8588 | * @filename: the filename (or URL) |
| 8589 | * @cur: the document |
| 8590 | * @encoding: the name of an encoding (or NULL) |
| 8591 | * |
| 8592 | * Dump an XML document, converting it to the given encoding |
| 8593 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8594 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8595 | */ |
| 8596 | int |
| 8597 | xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 8598 | return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) ); |
| 8599 | } |
| 8600 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8601 | /** |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8602 | * xmlSaveFormatFile: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8603 | * @filename: the filename (or URL) |
| 8604 | * @cur: the document |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8605 | * @format: should formatting spaces been added |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8606 | * |
| 8607 | * Dump an XML document to a file. Will use compression if |
| 8608 | * compiled in and enabled. If @filename is "-" the stdout file is |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8609 | * 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] | 8610 | * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 |
| 8611 | * or xmlKeepBlanksDefault(0) was called |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8612 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8613 | * returns: the number of bytes written or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8614 | */ |
| 8615 | int |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8616 | xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8617 | return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8618 | } |
| 8619 | |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8620 | /** |
| 8621 | * xmlSaveFile: |
| 8622 | * @filename: the filename (or URL) |
| 8623 | * @cur: the document |
| 8624 | * |
| 8625 | * Dump an XML document to a file. Will use compression if |
| 8626 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 8627 | * used. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 8628 | * returns: the number of bytes written or -1 in case of failure. |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8629 | */ |
| 8630 | int |
| 8631 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 8632 | return(xmlSaveFormatFileEnc(filename, cur, NULL, 0)); |
Daniel Veillard | 67fee94 | 2001-04-26 18:59:03 +0000 | [diff] [blame] | 8633 | } |
| 8634 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 8635 | #endif /* LIBXML_OUTPUT_ENABLED */ |
| 8636 | |
| 8637 | /** |
| 8638 | * xmlGetDocCompressMode: |
| 8639 | * @doc: the document |
| 8640 | * |
| 8641 | * get the compression ratio for a document, ZLIB based |
| 8642 | * Returns 0 (uncompressed) to 9 (max compression) |
| 8643 | */ |
| 8644 | int |
| 8645 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 8646 | if (doc == NULL) return(-1); |
| 8647 | return(doc->compression); |
| 8648 | } |
| 8649 | |
| 8650 | /** |
| 8651 | * xmlSetDocCompressMode: |
| 8652 | * @doc: the document |
| 8653 | * @mode: the compression ratio |
| 8654 | * |
| 8655 | * set the compression ratio for a document, ZLIB based |
| 8656 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 8657 | */ |
| 8658 | void |
| 8659 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 8660 | if (doc == NULL) return; |
| 8661 | if (mode < 0) doc->compression = 0; |
| 8662 | else if (mode > 9) doc->compression = 9; |
| 8663 | else doc->compression = mode; |
| 8664 | } |
| 8665 | |
| 8666 | /** |
| 8667 | * xmlGetCompressMode: |
| 8668 | * |
| 8669 | * get the default compression mode used, ZLIB based. |
| 8670 | * Returns 0 (uncompressed) to 9 (max compression) |
| 8671 | */ |
| 8672 | int |
| 8673 | xmlGetCompressMode(void) |
| 8674 | { |
| 8675 | return (xmlCompressMode); |
| 8676 | } |
| 8677 | |
| 8678 | /** |
| 8679 | * xmlSetCompressMode: |
| 8680 | * @mode: the compression ratio |
| 8681 | * |
| 8682 | * set the default compression mode used, ZLIB based |
| 8683 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 8684 | */ |
| 8685 | void |
| 8686 | xmlSetCompressMode(int mode) { |
| 8687 | if (mode < 0) xmlCompressMode = 0; |
| 8688 | else if (mode > 9) xmlCompressMode = 9; |
| 8689 | else xmlCompressMode = mode; |
| 8690 | } |
| 8691 | |