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 |
William M. Brack | 1d8c9b2 | 2004-12-25 10:14:57 +0000 | [diff] [blame] | 40 | #ifdef LIBXML_DEBUG_ENABLED |
| 41 | #include <libxml/debugXML.h> |
| 42 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 43 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 44 | int __xmlRegisterCallbacks = 0; |
| 45 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 46 | xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns); |
| 47 | |
| 48 | /************************************************************************ |
| 49 | * * |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 50 | * Tree memory error handler * |
| 51 | * * |
| 52 | ************************************************************************/ |
| 53 | /** |
| 54 | * xmlTreeErrMemory: |
| 55 | * @extra: extra informations |
| 56 | * |
| 57 | * Handle an out of memory condition |
| 58 | */ |
| 59 | static void |
| 60 | xmlTreeErrMemory(const char *extra) |
| 61 | { |
| 62 | __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * xmlTreeErr: |
| 67 | * @code: the error number |
| 68 | * @extra: extra informations |
| 69 | * |
| 70 | * Handle an out of memory condition |
| 71 | */ |
| 72 | static void |
| 73 | xmlTreeErr(int code, xmlNodePtr node, const char *extra) |
| 74 | { |
| 75 | const char *msg = NULL; |
| 76 | |
| 77 | switch(code) { |
| 78 | case XML_TREE_INVALID_HEX: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 79 | msg = "invalid hexadecimal character value\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 80 | break; |
| 81 | case XML_TREE_INVALID_DEC: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 82 | msg = "invalid decimal character value\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 83 | break; |
| 84 | case XML_TREE_UNTERMINATED_ENTITY: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 85 | msg = "unterminated entity reference %15s\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 86 | break; |
| 87 | default: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 88 | msg = "unexpected error number\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 89 | } |
| 90 | __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra); |
| 91 | } |
| 92 | |
| 93 | /************************************************************************ |
| 94 | * * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 95 | * A few static variables and macros * |
| 96 | * * |
| 97 | ************************************************************************/ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 98 | /* #undef xmlStringText */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 99 | const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 100 | /* #undef xmlStringTextNoenc */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 101 | const xmlChar xmlStringTextNoenc[] = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 102 | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 103 | /* #undef xmlStringComment */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 104 | const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
| 105 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 106 | static int xmlCompressMode = 0; |
| 107 | static int xmlCheckDTD = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 108 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 109 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
| 110 | xmlNodePtr ulccur = (n)->children; \ |
| 111 | if (ulccur == NULL) { \ |
| 112 | (n)->last = NULL; \ |
| 113 | } else { \ |
| 114 | while (ulccur->next != NULL) { \ |
| 115 | ulccur->parent = (n); \ |
| 116 | ulccur = ulccur->next; \ |
| 117 | } \ |
| 118 | ulccur->parent = (n); \ |
| 119 | (n)->last = ulccur; \ |
| 120 | }} |
| 121 | |
| 122 | /* #define DEBUG_BUFFER */ |
| 123 | /* #define DEBUG_TREE */ |
| 124 | |
| 125 | /************************************************************************ |
| 126 | * * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 127 | * Functions to move to entities.c once the * |
| 128 | * API freeze is smoothen and they can be made public. * |
| 129 | * * |
| 130 | ************************************************************************/ |
| 131 | #include <libxml/hash.h> |
| 132 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 133 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 134 | /** |
| 135 | * xmlGetEntityFromDtd: |
| 136 | * @dtd: A pointer to the DTD to search |
| 137 | * @name: The entity name |
| 138 | * |
| 139 | * Do an entity lookup in the DTD entity hash table and |
| 140 | * return the corresponding entity, if found. |
| 141 | * |
| 142 | * Returns A pointer to the entity structure or NULL if not found. |
| 143 | */ |
| 144 | static xmlEntityPtr |
| 145 | xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 146 | xmlEntitiesTablePtr table; |
| 147 | |
| 148 | if((dtd != NULL) && (dtd->entities != NULL)) { |
| 149 | table = (xmlEntitiesTablePtr) dtd->entities; |
| 150 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 151 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 152 | } |
| 153 | return(NULL); |
| 154 | } |
| 155 | /** |
| 156 | * xmlGetParameterEntityFromDtd: |
| 157 | * @dtd: A pointer to the DTD to search |
| 158 | * @name: The entity name |
| 159 | * |
| 160 | * Do an entity lookup in the DTD pararmeter entity hash table and |
| 161 | * return the corresponding entity, if found. |
| 162 | * |
| 163 | * Returns A pointer to the entity structure or NULL if not found. |
| 164 | */ |
| 165 | static xmlEntityPtr |
| 166 | xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 167 | xmlEntitiesTablePtr table; |
| 168 | |
| 169 | if ((dtd != NULL) && (dtd->pentities != NULL)) { |
| 170 | table = (xmlEntitiesTablePtr) dtd->pentities; |
| 171 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 172 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 173 | } |
| 174 | return(NULL); |
| 175 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 176 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 177 | |
| 178 | /************************************************************************ |
| 179 | * * |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 180 | * QName handling helper * |
| 181 | * * |
| 182 | ************************************************************************/ |
| 183 | |
| 184 | /** |
| 185 | * xmlBuildQName: |
| 186 | * @ncname: the Name |
| 187 | * @prefix: the prefix |
| 188 | * @memory: preallocated memory |
| 189 | * @len: preallocated memory length |
| 190 | * |
| 191 | * Builds the QName @prefix:@ncname in @memory if there is enough space |
| 192 | * and prefix is not NULL nor empty, otherwise allocate a new string. |
| 193 | * If prefix is NULL or empty it returns ncname. |
| 194 | * |
| 195 | * Returns the new string which must be freed by the caller if different from |
| 196 | * @memory and @ncname or NULL in case of error |
| 197 | */ |
| 198 | xmlChar * |
| 199 | xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix, |
| 200 | xmlChar *memory, int len) { |
| 201 | int lenn, lenp; |
| 202 | xmlChar *ret; |
| 203 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 204 | if (ncname == NULL) return(NULL); |
| 205 | if (prefix == NULL) return((xmlChar *) ncname); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 206 | |
| 207 | lenn = strlen((char *) ncname); |
| 208 | lenp = strlen((char *) prefix); |
| 209 | |
| 210 | if ((memory == NULL) || (len < lenn + lenp + 2)) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 211 | ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 212 | if (ret == NULL) { |
| 213 | xmlTreeErrMemory("building QName"); |
| 214 | return(NULL); |
| 215 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 216 | } else { |
| 217 | ret = memory; |
| 218 | } |
| 219 | memcpy(&ret[0], prefix, lenp); |
| 220 | ret[lenp] = ':'; |
| 221 | memcpy(&ret[lenp + 1], ncname, lenn); |
| 222 | ret[lenn + lenp + 1] = 0; |
| 223 | return(ret); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * xmlSplitQName2: |
| 228 | * @name: the full QName |
| 229 | * @prefix: a xmlChar ** |
| 230 | * |
| 231 | * parse an XML qualified name string |
| 232 | * |
| 233 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 234 | * |
| 235 | * [NS 6] Prefix ::= NCName |
| 236 | * |
| 237 | * [NS 7] LocalPart ::= NCName |
| 238 | * |
| 239 | * Returns NULL if not a QName, otherwise the local part, and prefix |
| 240 | * is updated to get the Prefix if any. |
| 241 | */ |
| 242 | |
| 243 | xmlChar * |
| 244 | xmlSplitQName2(const xmlChar *name, xmlChar **prefix) { |
| 245 | int len = 0; |
| 246 | xmlChar *ret = NULL; |
| 247 | |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 248 | if (prefix == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 249 | *prefix = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 250 | if (name == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 251 | |
| 252 | #ifndef XML_XML_NAMESPACE |
| 253 | /* xml: prefix is not really a namespace */ |
| 254 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 255 | (name[2] == 'l') && (name[3] == ':')) |
| 256 | return(NULL); |
| 257 | #endif |
| 258 | |
| 259 | /* nasty but valid */ |
| 260 | if (name[0] == ':') |
| 261 | return(NULL); |
| 262 | |
| 263 | /* |
| 264 | * we are not trying to validate but just to cut, and yes it will |
| 265 | * work even if this is as set of UTF-8 encoded chars |
| 266 | */ |
| 267 | while ((name[len] != 0) && (name[len] != ':')) |
| 268 | len++; |
| 269 | |
| 270 | if (name[len] == 0) |
| 271 | return(NULL); |
| 272 | |
| 273 | *prefix = xmlStrndup(name, len); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 274 | if (*prefix == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 275 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 276 | return(NULL); |
| 277 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 278 | ret = xmlStrdup(&name[len + 1]); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 279 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 280 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 281 | if (*prefix != NULL) { |
| 282 | xmlFree(*prefix); |
| 283 | *prefix = NULL; |
| 284 | } |
| 285 | return(NULL); |
| 286 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 287 | |
| 288 | return(ret); |
| 289 | } |
| 290 | |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 291 | /** |
| 292 | * xmlSplitQName3: |
| 293 | * @name: the full QName |
| 294 | * @len: an int * |
| 295 | * |
| 296 | * parse an XML qualified name string,i |
| 297 | * |
| 298 | * returns NULL if it is not a Qualified Name, otherwise, update len |
| 299 | * with the lenght in byte of the prefix and return a pointer |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 300 | * to the start of the name without the prefix |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 301 | */ |
| 302 | |
| 303 | const xmlChar * |
| 304 | xmlSplitQName3(const xmlChar *name, int *len) { |
| 305 | int l = 0; |
| 306 | |
| 307 | if (name == NULL) return(NULL); |
| 308 | if (len == NULL) return(NULL); |
| 309 | |
| 310 | /* nasty but valid */ |
| 311 | if (name[0] == ':') |
| 312 | return(NULL); |
| 313 | |
| 314 | /* |
| 315 | * we are not trying to validate but just to cut, and yes it will |
| 316 | * work even if this is as set of UTF-8 encoded chars |
| 317 | */ |
| 318 | while ((name[l] != 0) && (name[l] != ':')) |
| 319 | l++; |
| 320 | |
| 321 | if (name[l] == 0) |
| 322 | return(NULL); |
| 323 | |
| 324 | *len = l; |
| 325 | |
| 326 | return(&name[l+1]); |
| 327 | } |
| 328 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 329 | /************************************************************************ |
| 330 | * * |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 331 | * Check Name, NCName and QName strings * |
| 332 | * * |
| 333 | ************************************************************************/ |
| 334 | |
| 335 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l) |
| 336 | |
Daniel Veillard | 6b6d680 | 2005-07-03 21:00:34 +0000 | [diff] [blame] | 337 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_DEBUG_ENABLED) || defined (LIBXML_HTML_ENABLED) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 338 | /** |
| 339 | * xmlValidateNCName: |
| 340 | * @value: the value to check |
| 341 | * @space: allow spaces in front and end of the string |
| 342 | * |
| 343 | * Check that a value conforms to the lexical space of NCName |
| 344 | * |
| 345 | * Returns 0 if this validates, a positive error code number otherwise |
| 346 | * and -1 in case of internal or API error. |
| 347 | */ |
| 348 | int |
| 349 | xmlValidateNCName(const xmlChar *value, int space) { |
| 350 | const xmlChar *cur = value; |
| 351 | int c,l; |
| 352 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 353 | if (value == NULL) |
| 354 | return(-1); |
| 355 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 356 | /* |
| 357 | * First quick algorithm for ASCII range |
| 358 | */ |
| 359 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 360 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 361 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 362 | (*cur == '_')) |
| 363 | cur++; |
| 364 | else |
| 365 | goto try_complex; |
| 366 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 367 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 368 | ((*cur >= '0') && (*cur <= '9')) || |
| 369 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 370 | cur++; |
| 371 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 372 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 373 | if (*cur == 0) |
| 374 | return(0); |
| 375 | |
| 376 | try_complex: |
| 377 | /* |
| 378 | * Second check for chars outside the ASCII range |
| 379 | */ |
| 380 | cur = value; |
| 381 | c = CUR_SCHAR(cur, l); |
| 382 | if (space) { |
| 383 | while (IS_BLANK(c)) { |
| 384 | cur += l; |
| 385 | c = CUR_SCHAR(cur, l); |
| 386 | } |
| 387 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 388 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 389 | return(1); |
| 390 | cur += l; |
| 391 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 392 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 393 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 394 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 395 | cur += l; |
| 396 | c = CUR_SCHAR(cur, l); |
| 397 | } |
| 398 | if (space) { |
| 399 | while (IS_BLANK(c)) { |
| 400 | cur += l; |
| 401 | c = CUR_SCHAR(cur, l); |
| 402 | } |
| 403 | } |
| 404 | if (c != 0) |
| 405 | return(1); |
| 406 | |
| 407 | return(0); |
| 408 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 409 | #endif |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 410 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 411 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 412 | /** |
| 413 | * xmlValidateQName: |
| 414 | * @value: the value to check |
| 415 | * @space: allow spaces in front and end of the string |
| 416 | * |
| 417 | * Check that a value conforms to the lexical space of QName |
| 418 | * |
| 419 | * Returns 0 if this validates, a positive error code number otherwise |
| 420 | * and -1 in case of internal or API error. |
| 421 | */ |
| 422 | int |
| 423 | xmlValidateQName(const xmlChar *value, int space) { |
| 424 | const xmlChar *cur = value; |
| 425 | int c,l; |
| 426 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 427 | if (value == NULL) |
| 428 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 429 | /* |
| 430 | * First quick algorithm for ASCII range |
| 431 | */ |
| 432 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 433 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 434 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 435 | (*cur == '_')) |
| 436 | cur++; |
| 437 | else |
| 438 | goto try_complex; |
| 439 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 440 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 441 | ((*cur >= '0') && (*cur <= '9')) || |
| 442 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 443 | cur++; |
| 444 | if (*cur == ':') { |
| 445 | cur++; |
| 446 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 447 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 448 | (*cur == '_')) |
| 449 | cur++; |
| 450 | else |
| 451 | goto try_complex; |
| 452 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 453 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 454 | ((*cur >= '0') && (*cur <= '9')) || |
| 455 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 456 | cur++; |
| 457 | } |
| 458 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 459 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 460 | if (*cur == 0) |
| 461 | return(0); |
| 462 | |
| 463 | try_complex: |
| 464 | /* |
| 465 | * Second check for chars outside the ASCII range |
| 466 | */ |
| 467 | cur = value; |
| 468 | c = CUR_SCHAR(cur, l); |
| 469 | if (space) { |
| 470 | while (IS_BLANK(c)) { |
| 471 | cur += l; |
| 472 | c = CUR_SCHAR(cur, l); |
| 473 | } |
| 474 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 475 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 476 | return(1); |
| 477 | cur += l; |
| 478 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 479 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 480 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 481 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 482 | cur += l; |
| 483 | c = CUR_SCHAR(cur, l); |
| 484 | } |
| 485 | if (c == ':') { |
| 486 | cur += l; |
| 487 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 488 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 489 | return(1); |
| 490 | cur += l; |
| 491 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 492 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 493 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 494 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 495 | cur += l; |
| 496 | c = CUR_SCHAR(cur, l); |
| 497 | } |
| 498 | } |
| 499 | if (space) { |
| 500 | while (IS_BLANK(c)) { |
| 501 | cur += l; |
| 502 | c = CUR_SCHAR(cur, l); |
| 503 | } |
| 504 | } |
| 505 | if (c != 0) |
| 506 | return(1); |
| 507 | return(0); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * xmlValidateName: |
| 512 | * @value: the value to check |
| 513 | * @space: allow spaces in front and end of the string |
| 514 | * |
| 515 | * Check that a value conforms to the lexical space of Name |
| 516 | * |
| 517 | * Returns 0 if this validates, a positive error code number otherwise |
| 518 | * and -1 in case of internal or API error. |
| 519 | */ |
| 520 | int |
| 521 | xmlValidateName(const xmlChar *value, int space) { |
| 522 | const xmlChar *cur = value; |
| 523 | int c,l; |
| 524 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 525 | if (value == NULL) |
| 526 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 527 | /* |
| 528 | * First quick algorithm for ASCII range |
| 529 | */ |
| 530 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 531 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 532 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 533 | (*cur == '_') || (*cur == ':')) |
| 534 | cur++; |
| 535 | else |
| 536 | goto try_complex; |
| 537 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 538 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 539 | ((*cur >= '0') && (*cur <= '9')) || |
| 540 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 541 | cur++; |
| 542 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 543 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 544 | if (*cur == 0) |
| 545 | return(0); |
| 546 | |
| 547 | try_complex: |
| 548 | /* |
| 549 | * Second check for chars outside the ASCII range |
| 550 | */ |
| 551 | cur = value; |
| 552 | c = CUR_SCHAR(cur, l); |
| 553 | if (space) { |
| 554 | while (IS_BLANK(c)) { |
| 555 | cur += l; |
| 556 | c = CUR_SCHAR(cur, l); |
| 557 | } |
| 558 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 559 | if ((!IS_LETTER(c)) && (c != '_') && (c != ':')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 560 | return(1); |
| 561 | cur += l; |
| 562 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 563 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 564 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 565 | cur += l; |
| 566 | c = CUR_SCHAR(cur, l); |
| 567 | } |
| 568 | if (space) { |
| 569 | while (IS_BLANK(c)) { |
| 570 | cur += l; |
| 571 | c = CUR_SCHAR(cur, l); |
| 572 | } |
| 573 | } |
| 574 | if (c != 0) |
| 575 | return(1); |
| 576 | return(0); |
| 577 | } |
| 578 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 579 | /** |
| 580 | * xmlValidateNMToken: |
| 581 | * @value: the value to check |
| 582 | * @space: allow spaces in front and end of the string |
| 583 | * |
| 584 | * Check that a value conforms to the lexical space of NMToken |
| 585 | * |
| 586 | * Returns 0 if this validates, a positive error code number otherwise |
| 587 | * and -1 in case of internal or API error. |
| 588 | */ |
| 589 | int |
| 590 | xmlValidateNMToken(const xmlChar *value, int space) { |
| 591 | const xmlChar *cur = value; |
| 592 | int c,l; |
| 593 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 594 | if (value == NULL) |
| 595 | return(-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 596 | /* |
| 597 | * First quick algorithm for ASCII range |
| 598 | */ |
| 599 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 600 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 601 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 602 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 603 | ((*cur >= '0') && (*cur <= '9')) || |
| 604 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 605 | cur++; |
| 606 | else |
| 607 | goto try_complex; |
| 608 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 609 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 610 | ((*cur >= '0') && (*cur <= '9')) || |
| 611 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 612 | cur++; |
| 613 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 614 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 615 | if (*cur == 0) |
| 616 | return(0); |
| 617 | |
| 618 | try_complex: |
| 619 | /* |
| 620 | * Second check for chars outside the ASCII range |
| 621 | */ |
| 622 | cur = value; |
| 623 | c = CUR_SCHAR(cur, l); |
| 624 | if (space) { |
| 625 | while (IS_BLANK(c)) { |
| 626 | cur += l; |
| 627 | c = CUR_SCHAR(cur, l); |
| 628 | } |
| 629 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 630 | if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 631 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c))) |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 632 | return(1); |
| 633 | cur += l; |
| 634 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 635 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 636 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 637 | cur += l; |
| 638 | c = CUR_SCHAR(cur, l); |
| 639 | } |
| 640 | if (space) { |
| 641 | while (IS_BLANK(c)) { |
| 642 | cur += l; |
| 643 | c = CUR_SCHAR(cur, l); |
| 644 | } |
| 645 | } |
| 646 | if (c != 0) |
| 647 | return(1); |
| 648 | return(0); |
| 649 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 650 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 651 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 652 | /************************************************************************ |
| 653 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 654 | * Allocation and deallocation of basic structures * |
| 655 | * * |
| 656 | ************************************************************************/ |
| 657 | |
| 658 | /** |
| 659 | * xmlSetBufferAllocationScheme: |
| 660 | * @scheme: allocation method to use |
| 661 | * |
| 662 | * Set the buffer allocation method. Types are |
| 663 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 664 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 665 | * improves performance |
| 666 | */ |
| 667 | void |
| 668 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
| 669 | xmlBufferAllocScheme = scheme; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * xmlGetBufferAllocationScheme: |
| 674 | * |
| 675 | * Types are |
| 676 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 677 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 678 | * improves performance |
| 679 | * |
| 680 | * Returns the current allocation scheme |
| 681 | */ |
| 682 | xmlBufferAllocationScheme |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 683 | xmlGetBufferAllocationScheme(void) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 684 | return(xmlBufferAllocScheme); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /** |
| 688 | * xmlNewNs: |
| 689 | * @node: the element carrying the namespace |
| 690 | * @href: the URI associated |
| 691 | * @prefix: the prefix for the namespace |
| 692 | * |
| 693 | * Creation of a new Namespace. This function will refuse to create |
| 694 | * a namespace with a similar prefix than an existing one present on this |
| 695 | * node. |
| 696 | * We use href==NULL in the case of an element creation where the namespace |
| 697 | * was not defined. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 698 | * Returns a new namespace pointer or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 699 | */ |
| 700 | xmlNsPtr |
| 701 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
| 702 | xmlNsPtr cur; |
| 703 | |
| 704 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 705 | return(NULL); |
| 706 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 707 | if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) |
| 708 | return(NULL); |
| 709 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 710 | /* |
| 711 | * Allocate a new Namespace and fill the fields. |
| 712 | */ |
| 713 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 714 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 715 | xmlTreeErrMemory("building namespace"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 716 | return(NULL); |
| 717 | } |
| 718 | memset(cur, 0, sizeof(xmlNs)); |
| 719 | cur->type = XML_LOCAL_NAMESPACE; |
| 720 | |
| 721 | if (href != NULL) |
| 722 | cur->href = xmlStrdup(href); |
| 723 | if (prefix != NULL) |
| 724 | cur->prefix = xmlStrdup(prefix); |
| 725 | |
| 726 | /* |
| 727 | * Add it at the end to preserve parsing order ... |
| 728 | * and checks for existing use of the prefix |
| 729 | */ |
| 730 | if (node != NULL) { |
| 731 | if (node->nsDef == NULL) { |
| 732 | node->nsDef = cur; |
| 733 | } else { |
| 734 | xmlNsPtr prev = node->nsDef; |
| 735 | |
| 736 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 737 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 738 | xmlFreeNs(cur); |
| 739 | return(NULL); |
| 740 | } |
| 741 | while (prev->next != NULL) { |
| 742 | prev = prev->next; |
| 743 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 744 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 745 | xmlFreeNs(cur); |
| 746 | return(NULL); |
| 747 | } |
| 748 | } |
| 749 | prev->next = cur; |
| 750 | } |
| 751 | } |
| 752 | return(cur); |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * xmlSetNs: |
| 757 | * @node: a node in the document |
| 758 | * @ns: a namespace pointer |
| 759 | * |
| 760 | * Associate a namespace to a node, a posteriori. |
| 761 | */ |
| 762 | void |
| 763 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
| 764 | if (node == NULL) { |
| 765 | #ifdef DEBUG_TREE |
| 766 | xmlGenericError(xmlGenericErrorContext, |
| 767 | "xmlSetNs: node == NULL\n"); |
| 768 | #endif |
| 769 | return; |
| 770 | } |
| 771 | node->ns = ns; |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * xmlFreeNs: |
| 776 | * @cur: the namespace pointer |
| 777 | * |
| 778 | * Free up the structures associated to a namespace |
| 779 | */ |
| 780 | void |
| 781 | xmlFreeNs(xmlNsPtr cur) { |
| 782 | if (cur == NULL) { |
| 783 | #ifdef DEBUG_TREE |
| 784 | xmlGenericError(xmlGenericErrorContext, |
| 785 | "xmlFreeNs : ns == NULL\n"); |
| 786 | #endif |
| 787 | return; |
| 788 | } |
| 789 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 790 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 791 | xmlFree(cur); |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * xmlFreeNsList: |
| 796 | * @cur: the first namespace pointer |
| 797 | * |
| 798 | * Free up all the structures associated to the chained namespaces. |
| 799 | */ |
| 800 | void |
| 801 | xmlFreeNsList(xmlNsPtr cur) { |
| 802 | xmlNsPtr next; |
| 803 | if (cur == NULL) { |
| 804 | #ifdef DEBUG_TREE |
| 805 | xmlGenericError(xmlGenericErrorContext, |
| 806 | "xmlFreeNsList : ns == NULL\n"); |
| 807 | #endif |
| 808 | return; |
| 809 | } |
| 810 | while (cur != NULL) { |
| 811 | next = cur->next; |
| 812 | xmlFreeNs(cur); |
| 813 | cur = next; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * xmlNewDtd: |
| 819 | * @doc: the document pointer |
| 820 | * @name: the DTD name |
| 821 | * @ExternalID: the external ID |
| 822 | * @SystemID: the system ID |
| 823 | * |
| 824 | * Creation of a new DTD for the external subset. To create an |
| 825 | * internal subset, use xmlCreateIntSubset(). |
| 826 | * |
| 827 | * Returns a pointer to the new DTD structure |
| 828 | */ |
| 829 | xmlDtdPtr |
| 830 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 831 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 832 | xmlDtdPtr cur; |
| 833 | |
| 834 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
| 835 | #ifdef DEBUG_TREE |
| 836 | xmlGenericError(xmlGenericErrorContext, |
| 837 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
| 838 | /* !!! */ (char *) name, doc->name, |
| 839 | /* !!! */ (char *)doc->extSubset->name); |
| 840 | #endif |
| 841 | return(NULL); |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Allocate a new DTD and fill the fields. |
| 846 | */ |
| 847 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 848 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 849 | xmlTreeErrMemory("building DTD"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 850 | return(NULL); |
| 851 | } |
| 852 | memset(cur, 0 , sizeof(xmlDtd)); |
| 853 | cur->type = XML_DTD_NODE; |
| 854 | |
| 855 | if (name != NULL) |
| 856 | cur->name = xmlStrdup(name); |
| 857 | if (ExternalID != NULL) |
| 858 | cur->ExternalID = xmlStrdup(ExternalID); |
| 859 | if (SystemID != NULL) |
| 860 | cur->SystemID = xmlStrdup(SystemID); |
| 861 | if (doc != NULL) |
| 862 | doc->extSubset = cur; |
| 863 | cur->doc = doc; |
| 864 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 865 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 866 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 867 | return(cur); |
| 868 | } |
| 869 | |
| 870 | /** |
| 871 | * xmlGetIntSubset: |
| 872 | * @doc: the document pointer |
| 873 | * |
| 874 | * Get the internal subset of a document |
| 875 | * Returns a pointer to the DTD structure or NULL if not found |
| 876 | */ |
| 877 | |
| 878 | xmlDtdPtr |
| 879 | xmlGetIntSubset(xmlDocPtr doc) { |
| 880 | xmlNodePtr cur; |
| 881 | |
| 882 | if (doc == NULL) |
| 883 | return(NULL); |
| 884 | cur = doc->children; |
| 885 | while (cur != NULL) { |
| 886 | if (cur->type == XML_DTD_NODE) |
| 887 | return((xmlDtdPtr) cur); |
| 888 | cur = cur->next; |
| 889 | } |
| 890 | return((xmlDtdPtr) doc->intSubset); |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * xmlCreateIntSubset: |
| 895 | * @doc: the document pointer |
| 896 | * @name: the DTD name |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 897 | * @ExternalID: the external (PUBLIC) ID |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 898 | * @SystemID: the system ID |
| 899 | * |
| 900 | * Create the internal subset of a document |
| 901 | * Returns a pointer to the new DTD structure |
| 902 | */ |
| 903 | xmlDtdPtr |
| 904 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 905 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 906 | xmlDtdPtr cur; |
| 907 | |
| 908 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
| 909 | #ifdef DEBUG_TREE |
| 910 | xmlGenericError(xmlGenericErrorContext, |
| 911 | |
| 912 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 913 | doc->name); |
| 914 | #endif |
| 915 | return(NULL); |
| 916 | } |
| 917 | |
| 918 | /* |
| 919 | * Allocate a new DTD and fill the fields. |
| 920 | */ |
| 921 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 922 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 923 | xmlTreeErrMemory("building internal subset"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 924 | return(NULL); |
| 925 | } |
| 926 | memset(cur, 0, sizeof(xmlDtd)); |
| 927 | cur->type = XML_DTD_NODE; |
| 928 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 929 | if (name != NULL) { |
| 930 | cur->name = xmlStrdup(name); |
| 931 | if (cur->name == NULL) { |
| 932 | xmlTreeErrMemory("building internal subset"); |
| 933 | xmlFree(cur); |
| 934 | return(NULL); |
| 935 | } |
| 936 | } |
| 937 | if (ExternalID != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 938 | cur->ExternalID = xmlStrdup(ExternalID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 939 | if (cur->ExternalID == NULL) { |
| 940 | xmlTreeErrMemory("building internal subset"); |
| 941 | if (cur->name != NULL) |
| 942 | xmlFree((char *)cur->name); |
| 943 | xmlFree(cur); |
| 944 | return(NULL); |
| 945 | } |
| 946 | } |
| 947 | if (SystemID != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 948 | cur->SystemID = xmlStrdup(SystemID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 949 | if (cur->SystemID == NULL) { |
| 950 | xmlTreeErrMemory("building internal subset"); |
| 951 | if (cur->name != NULL) |
| 952 | xmlFree((char *)cur->name); |
| 953 | if (cur->ExternalID != NULL) |
| 954 | xmlFree((char *)cur->ExternalID); |
| 955 | xmlFree(cur); |
| 956 | return(NULL); |
| 957 | } |
| 958 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 959 | if (doc != NULL) { |
| 960 | doc->intSubset = cur; |
| 961 | cur->parent = doc; |
| 962 | cur->doc = doc; |
| 963 | if (doc->children == NULL) { |
| 964 | doc->children = (xmlNodePtr) cur; |
| 965 | doc->last = (xmlNodePtr) cur; |
| 966 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 967 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 968 | xmlNodePtr prev; |
| 969 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 970 | prev = doc->children; |
| 971 | prev->prev = (xmlNodePtr) cur; |
| 972 | cur->next = prev; |
| 973 | doc->children = (xmlNodePtr) cur; |
| 974 | } else { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 975 | xmlNodePtr next; |
| 976 | |
| 977 | next = doc->children; |
| 978 | while ((next != NULL) && (next->type != XML_ELEMENT_NODE)) |
| 979 | next = next->next; |
| 980 | if (next == NULL) { |
| 981 | cur->prev = doc->last; |
| 982 | cur->prev->next = (xmlNodePtr) cur; |
| 983 | cur->next = NULL; |
| 984 | doc->last = (xmlNodePtr) cur; |
| 985 | } else { |
| 986 | cur->next = next; |
| 987 | cur->prev = next->prev; |
| 988 | if (cur->prev == NULL) |
| 989 | doc->children = (xmlNodePtr) cur; |
| 990 | else |
| 991 | cur->prev->next = (xmlNodePtr) cur; |
| 992 | next->prev = (xmlNodePtr) cur; |
| 993 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 997 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 998 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 999 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1000 | return(cur); |
| 1001 | } |
| 1002 | |
| 1003 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1004 | * DICT_FREE: |
| 1005 | * @str: a string |
| 1006 | * |
| 1007 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 1008 | * current scope |
| 1009 | */ |
| 1010 | #define DICT_FREE(str) \ |
| 1011 | if ((str) && ((!dict) || \ |
| 1012 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 1013 | xmlFree((char *)(str)); |
| 1014 | |
| 1015 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1016 | * xmlFreeDtd: |
| 1017 | * @cur: the DTD structure to free up |
| 1018 | * |
| 1019 | * Free a DTD structure. |
| 1020 | */ |
| 1021 | void |
| 1022 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1023 | xmlDictPtr dict = NULL; |
| 1024 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1025 | if (cur == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1026 | return; |
| 1027 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1028 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1029 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1030 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1031 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1032 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1033 | if (cur->children != NULL) { |
| 1034 | xmlNodePtr next, c = cur->children; |
| 1035 | |
| 1036 | /* |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1037 | * Cleanup all nodes which are not part of the specific lists |
| 1038 | * of notations, elements, attributes and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1039 | */ |
| 1040 | while (c != NULL) { |
| 1041 | next = c->next; |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1042 | if ((c->type != XML_NOTATION_NODE) && |
| 1043 | (c->type != XML_ELEMENT_DECL) && |
| 1044 | (c->type != XML_ATTRIBUTE_DECL) && |
| 1045 | (c->type != XML_ENTITY_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1046 | xmlUnlinkNode(c); |
| 1047 | xmlFreeNode(c); |
| 1048 | } |
| 1049 | c = next; |
| 1050 | } |
| 1051 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1052 | DICT_FREE(cur->name) |
| 1053 | DICT_FREE(cur->SystemID) |
| 1054 | DICT_FREE(cur->ExternalID) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1055 | /* TODO !!! */ |
| 1056 | if (cur->notations != NULL) |
| 1057 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
| 1058 | |
| 1059 | if (cur->elements != NULL) |
| 1060 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
| 1061 | if (cur->attributes != NULL) |
| 1062 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
| 1063 | if (cur->entities != NULL) |
| 1064 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 1065 | if (cur->pentities != NULL) |
| 1066 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
| 1067 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1068 | xmlFree(cur); |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * xmlNewDoc: |
| 1073 | * @version: xmlChar string giving the version of XML "1.0" |
| 1074 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1075 | * Creates a new XML document |
| 1076 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1077 | * Returns a new document |
| 1078 | */ |
| 1079 | xmlDocPtr |
| 1080 | xmlNewDoc(const xmlChar *version) { |
| 1081 | xmlDocPtr cur; |
| 1082 | |
| 1083 | if (version == NULL) |
| 1084 | version = (const xmlChar *) "1.0"; |
| 1085 | |
| 1086 | /* |
| 1087 | * Allocate a new document and fill the fields. |
| 1088 | */ |
| 1089 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
| 1090 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1091 | xmlTreeErrMemory("building doc"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1092 | return(NULL); |
| 1093 | } |
| 1094 | memset(cur, 0, sizeof(xmlDoc)); |
| 1095 | cur->type = XML_DOCUMENT_NODE; |
| 1096 | |
| 1097 | cur->version = xmlStrdup(version); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 1098 | if (cur->version == NULL) { |
| 1099 | xmlTreeErrMemory("building doc"); |
| 1100 | xmlFree(cur); |
| 1101 | return(NULL); |
| 1102 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1103 | cur->standalone = -1; |
| 1104 | cur->compression = -1; /* not initialized */ |
| 1105 | cur->doc = cur; |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 1106 | /* |
| 1107 | * The in memory encoding is always UTF8 |
| 1108 | * This field will never change and would |
| 1109 | * be obsolete if not for binary compatibility. |
| 1110 | */ |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 1111 | cur->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1112 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1113 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1114 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1115 | return(cur); |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * xmlFreeDoc: |
| 1120 | * @cur: pointer to the document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1121 | * |
| 1122 | * Free up all the structures used by a document, tree included. |
| 1123 | */ |
| 1124 | void |
| 1125 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1126 | xmlDtdPtr extSubset, intSubset; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1127 | xmlDictPtr dict = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1128 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1129 | if (cur == NULL) { |
| 1130 | #ifdef DEBUG_TREE |
| 1131 | xmlGenericError(xmlGenericErrorContext, |
| 1132 | "xmlFreeDoc : document == NULL\n"); |
| 1133 | #endif |
| 1134 | return; |
| 1135 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1136 | #ifdef LIBXML_DEBUG_RUNTIME |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1137 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1138 | xmlDebugCheckDocument(stderr, cur); |
| 1139 | #endif |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1140 | #endif |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1141 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1142 | if (cur != NULL) dict = cur->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1143 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1144 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1145 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1146 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1147 | /* |
| 1148 | * Do this before freeing the children list to avoid ID lookups |
| 1149 | */ |
| 1150 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
| 1151 | cur->ids = NULL; |
| 1152 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
| 1153 | cur->refs = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1154 | extSubset = cur->extSubset; |
| 1155 | intSubset = cur->intSubset; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 1156 | if (intSubset == extSubset) |
| 1157 | extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1158 | if (extSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1159 | xmlUnlinkNode((xmlNodePtr) cur->extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1160 | cur->extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1161 | xmlFreeDtd(extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1162 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1163 | if (intSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1164 | xmlUnlinkNode((xmlNodePtr) cur->intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1165 | cur->intSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1166 | xmlFreeDtd(intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1170 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1171 | |
| 1172 | DICT_FREE(cur->version) |
| 1173 | DICT_FREE(cur->name) |
| 1174 | DICT_FREE(cur->encoding) |
| 1175 | DICT_FREE(cur->URL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1176 | xmlFree(cur); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1177 | if (dict) xmlDictFree(dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * xmlStringLenGetNodeList: |
| 1182 | * @doc: the document |
| 1183 | * @value: the value of the text |
| 1184 | * @len: the length of the string value |
| 1185 | * |
| 1186 | * Parse the value string and build the node list associated. Should |
| 1187 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1188 | * Returns a pointer to the first child |
| 1189 | */ |
| 1190 | xmlNodePtr |
| 1191 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
| 1192 | xmlNodePtr ret = NULL, last = NULL; |
| 1193 | xmlNodePtr node; |
| 1194 | xmlChar *val; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1195 | const xmlChar *cur = value, *end = cur + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1196 | const xmlChar *q; |
| 1197 | xmlEntityPtr ent; |
| 1198 | |
| 1199 | if (value == NULL) return(NULL); |
| 1200 | |
| 1201 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1202 | while ((cur < end) && (*cur != 0)) { |
| 1203 | if (cur[0] == '&') { |
| 1204 | int charval = 0; |
| 1205 | xmlChar tmp; |
| 1206 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1207 | /* |
| 1208 | * Save the current text. |
| 1209 | */ |
| 1210 | if (cur != q) { |
| 1211 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1212 | xmlNodeAddContentLen(last, q, cur - q); |
| 1213 | } else { |
| 1214 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1215 | if (node == NULL) return(ret); |
| 1216 | if (last == NULL) |
| 1217 | last = ret = node; |
| 1218 | else { |
| 1219 | last->next = node; |
| 1220 | node->prev = last; |
| 1221 | last = node; |
| 1222 | } |
| 1223 | } |
| 1224 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1225 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1226 | if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) { |
| 1227 | cur += 3; |
| 1228 | if (cur < end) |
| 1229 | tmp = *cur; |
| 1230 | else |
| 1231 | tmp = 0; |
| 1232 | while (tmp != ';') { /* Non input consuming loop */ |
| 1233 | if ((tmp >= '0') && (tmp <= '9')) |
| 1234 | charval = charval * 16 + (tmp - '0'); |
| 1235 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1236 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1237 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1238 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1239 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1240 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1241 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1242 | charval = 0; |
| 1243 | break; |
| 1244 | } |
| 1245 | cur++; |
| 1246 | if (cur < end) |
| 1247 | tmp = *cur; |
| 1248 | else |
| 1249 | tmp = 0; |
| 1250 | } |
| 1251 | if (tmp == ';') |
| 1252 | cur++; |
| 1253 | q = cur; |
| 1254 | } else if ((cur + 1 < end) && (cur[1] == '#')) { |
| 1255 | cur += 2; |
| 1256 | if (cur < end) |
| 1257 | tmp = *cur; |
| 1258 | else |
| 1259 | tmp = 0; |
| 1260 | while (tmp != ';') { /* Non input consuming loops */ |
| 1261 | if ((tmp >= '0') && (tmp <= '9')) |
| 1262 | charval = charval * 10 + (tmp - '0'); |
| 1263 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1264 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1265 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1266 | charval = 0; |
| 1267 | break; |
| 1268 | } |
| 1269 | cur++; |
| 1270 | if (cur < end) |
| 1271 | tmp = *cur; |
| 1272 | else |
| 1273 | tmp = 0; |
| 1274 | } |
| 1275 | if (tmp == ';') |
| 1276 | cur++; |
| 1277 | q = cur; |
| 1278 | } else { |
| 1279 | /* |
| 1280 | * Read the entity string |
| 1281 | */ |
| 1282 | cur++; |
| 1283 | q = cur; |
| 1284 | while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++; |
| 1285 | if ((cur >= end) || (*cur == 0)) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1286 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc, |
| 1287 | (const char *) q); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1288 | return(ret); |
| 1289 | } |
| 1290 | if (cur != q) { |
| 1291 | /* |
| 1292 | * Predefined entities don't generate nodes |
| 1293 | */ |
| 1294 | val = xmlStrndup(q, cur - q); |
| 1295 | ent = xmlGetDocEntity(doc, val); |
| 1296 | if ((ent != NULL) && |
| 1297 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1298 | if (last == NULL) { |
| 1299 | node = xmlNewDocText(doc, ent->content); |
| 1300 | last = ret = node; |
| 1301 | } else if (last->type != XML_TEXT_NODE) { |
| 1302 | node = xmlNewDocText(doc, ent->content); |
| 1303 | last = xmlAddNextSibling(last, node); |
| 1304 | } else |
| 1305 | xmlNodeAddContent(last, ent->content); |
| 1306 | |
| 1307 | } else { |
| 1308 | /* |
| 1309 | * Create a new REFERENCE_REF node |
| 1310 | */ |
| 1311 | node = xmlNewReference(doc, val); |
| 1312 | if (node == NULL) { |
| 1313 | if (val != NULL) xmlFree(val); |
| 1314 | return(ret); |
| 1315 | } |
| 1316 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1317 | xmlNodePtr temp; |
| 1318 | |
| 1319 | ent->children = xmlStringGetNodeList(doc, |
| 1320 | (const xmlChar*)node->content); |
| 1321 | ent->owner = 1; |
| 1322 | temp = ent->children; |
| 1323 | while (temp) { |
| 1324 | temp->parent = (xmlNodePtr)ent; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1325 | ent->last = temp; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1326 | temp = temp->next; |
| 1327 | } |
| 1328 | } |
| 1329 | if (last == NULL) { |
| 1330 | last = ret = node; |
| 1331 | } else { |
| 1332 | last = xmlAddNextSibling(last, node); |
| 1333 | } |
| 1334 | } |
| 1335 | xmlFree(val); |
| 1336 | } |
| 1337 | cur++; |
| 1338 | q = cur; |
| 1339 | } |
| 1340 | if (charval != 0) { |
| 1341 | xmlChar buf[10]; |
| 1342 | int l; |
| 1343 | |
| 1344 | l = xmlCopyCharMultiByte(buf, charval); |
| 1345 | buf[l] = 0; |
| 1346 | node = xmlNewDocText(doc, buf); |
| 1347 | if (node != NULL) { |
| 1348 | if (last == NULL) { |
| 1349 | last = ret = node; |
| 1350 | } else { |
| 1351 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1352 | } |
| 1353 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1354 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1355 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1356 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1357 | cur++; |
| 1358 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1359 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1360 | /* |
| 1361 | * Handle the last piece of text. |
| 1362 | */ |
| 1363 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1364 | xmlNodeAddContentLen(last, q, cur - q); |
| 1365 | } else { |
| 1366 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1367 | if (node == NULL) return(ret); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1368 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1369 | last = ret = node; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1370 | } else { |
| 1371 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | return(ret); |
| 1376 | } |
| 1377 | |
| 1378 | /** |
| 1379 | * xmlStringGetNodeList: |
| 1380 | * @doc: the document |
| 1381 | * @value: the value of the attribute |
| 1382 | * |
| 1383 | * Parse the value string and build the node list associated. Should |
| 1384 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1385 | * Returns a pointer to the first child |
| 1386 | */ |
| 1387 | xmlNodePtr |
| 1388 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
| 1389 | xmlNodePtr ret = NULL, last = NULL; |
| 1390 | xmlNodePtr node; |
| 1391 | xmlChar *val; |
| 1392 | const xmlChar *cur = value; |
| 1393 | const xmlChar *q; |
| 1394 | xmlEntityPtr ent; |
| 1395 | |
| 1396 | if (value == NULL) return(NULL); |
| 1397 | |
| 1398 | q = cur; |
| 1399 | while (*cur != 0) { |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1400 | if (cur[0] == '&') { |
| 1401 | int charval = 0; |
| 1402 | xmlChar tmp; |
| 1403 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1404 | /* |
| 1405 | * Save the current text. |
| 1406 | */ |
| 1407 | if (cur != q) { |
| 1408 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1409 | xmlNodeAddContentLen(last, q, cur - q); |
| 1410 | } else { |
| 1411 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1412 | if (node == NULL) return(ret); |
| 1413 | if (last == NULL) |
| 1414 | last = ret = node; |
| 1415 | else { |
| 1416 | last->next = node; |
| 1417 | node->prev = last; |
| 1418 | last = node; |
| 1419 | } |
| 1420 | } |
| 1421 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1422 | q = cur; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1423 | if ((cur[1] == '#') && (cur[2] == 'x')) { |
| 1424 | cur += 3; |
| 1425 | tmp = *cur; |
| 1426 | while (tmp != ';') { /* Non input consuming loop */ |
| 1427 | if ((tmp >= '0') && (tmp <= '9')) |
| 1428 | charval = charval * 16 + (tmp - '0'); |
| 1429 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1430 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1431 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1432 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1433 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1434 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1435 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1436 | charval = 0; |
| 1437 | break; |
| 1438 | } |
| 1439 | cur++; |
| 1440 | tmp = *cur; |
| 1441 | } |
| 1442 | if (tmp == ';') |
| 1443 | cur++; |
| 1444 | q = cur; |
| 1445 | } else if (cur[1] == '#') { |
| 1446 | cur += 2; |
| 1447 | tmp = *cur; |
| 1448 | while (tmp != ';') { /* Non input consuming loops */ |
| 1449 | if ((tmp >= '0') && (tmp <= '9')) |
| 1450 | charval = charval * 10 + (tmp - '0'); |
| 1451 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1452 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1453 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1454 | charval = 0; |
| 1455 | break; |
| 1456 | } |
| 1457 | cur++; |
| 1458 | tmp = *cur; |
| 1459 | } |
| 1460 | if (tmp == ';') |
| 1461 | cur++; |
| 1462 | q = cur; |
| 1463 | } else { |
| 1464 | /* |
| 1465 | * Read the entity string |
| 1466 | */ |
| 1467 | cur++; |
| 1468 | q = cur; |
| 1469 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 1470 | if (*cur == 0) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1471 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, |
| 1472 | (xmlNodePtr) doc, (const char *) q); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1473 | return(ret); |
| 1474 | } |
| 1475 | if (cur != q) { |
| 1476 | /* |
| 1477 | * Predefined entities don't generate nodes |
| 1478 | */ |
| 1479 | val = xmlStrndup(q, cur - q); |
| 1480 | ent = xmlGetDocEntity(doc, val); |
| 1481 | if ((ent != NULL) && |
| 1482 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1483 | if (last == NULL) { |
| 1484 | node = xmlNewDocText(doc, ent->content); |
| 1485 | last = ret = node; |
Daniel Veillard | 6f42c13 | 2002-01-06 23:05:13 +0000 | [diff] [blame] | 1486 | } else if (last->type != XML_TEXT_NODE) { |
| 1487 | node = xmlNewDocText(doc, ent->content); |
| 1488 | last = xmlAddNextSibling(last, node); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1489 | } else |
| 1490 | xmlNodeAddContent(last, ent->content); |
| 1491 | |
| 1492 | } else { |
| 1493 | /* |
| 1494 | * Create a new REFERENCE_REF node |
| 1495 | */ |
| 1496 | node = xmlNewReference(doc, val); |
| 1497 | if (node == NULL) { |
| 1498 | if (val != NULL) xmlFree(val); |
| 1499 | return(ret); |
| 1500 | } |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1501 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1502 | xmlNodePtr temp; |
| 1503 | |
| 1504 | ent->children = xmlStringGetNodeList(doc, |
| 1505 | (const xmlChar*)node->content); |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 1506 | ent->owner = 1; |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1507 | temp = ent->children; |
| 1508 | while (temp) { |
| 1509 | temp->parent = (xmlNodePtr)ent; |
| 1510 | temp = temp->next; |
| 1511 | } |
| 1512 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1513 | if (last == NULL) { |
| 1514 | last = ret = node; |
| 1515 | } else { |
| 1516 | last = xmlAddNextSibling(last, node); |
| 1517 | } |
| 1518 | } |
| 1519 | xmlFree(val); |
| 1520 | } |
| 1521 | cur++; |
| 1522 | q = cur; |
| 1523 | } |
| 1524 | if (charval != 0) { |
| 1525 | xmlChar buf[10]; |
| 1526 | int len; |
| 1527 | |
| 1528 | len = xmlCopyCharMultiByte(buf, charval); |
| 1529 | buf[len] = 0; |
| 1530 | node = xmlNewDocText(doc, buf); |
| 1531 | if (node != NULL) { |
| 1532 | if (last == NULL) { |
| 1533 | last = ret = node; |
| 1534 | } else { |
| 1535 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1536 | } |
| 1537 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1538 | |
| 1539 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1540 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1541 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1542 | cur++; |
| 1543 | } |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 1544 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1545 | /* |
| 1546 | * Handle the last piece of text. |
| 1547 | */ |
| 1548 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1549 | xmlNodeAddContentLen(last, q, cur - q); |
| 1550 | } else { |
| 1551 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1552 | if (node == NULL) return(ret); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1553 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1554 | last = ret = node; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1555 | } else { |
| 1556 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | return(ret); |
| 1561 | } |
| 1562 | |
| 1563 | /** |
| 1564 | * xmlNodeListGetString: |
| 1565 | * @doc: the document |
| 1566 | * @list: a Node list |
| 1567 | * @inLine: should we replace entity contents or show their external form |
| 1568 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1569 | * Build the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1570 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1571 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1572 | * 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] | 1573 | */ |
| 1574 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1575 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1576 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1577 | xmlNodePtr node = list; |
| 1578 | xmlChar *ret = NULL; |
| 1579 | xmlEntityPtr ent; |
| 1580 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1581 | if (list == NULL) |
| 1582 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1583 | |
| 1584 | while (node != NULL) { |
| 1585 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1586 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1587 | if (inLine) { |
| 1588 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1589 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1590 | xmlChar *buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1591 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1592 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
| 1593 | if (buffer != NULL) { |
| 1594 | ret = xmlStrcat(ret, buffer); |
| 1595 | xmlFree(buffer); |
| 1596 | } |
| 1597 | } |
| 1598 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1599 | if (inLine) { |
| 1600 | ent = xmlGetDocEntity(doc, node->name); |
| 1601 | if (ent != NULL) { |
| 1602 | xmlChar *buffer; |
| 1603 | |
| 1604 | /* an entity content can be any "well balanced chunk", |
| 1605 | * i.e. the result of the content [43] production: |
| 1606 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1607 | * So it can contain text, CDATA section or nested |
| 1608 | * entity reference nodes (among others). |
| 1609 | * -> we recursive call xmlNodeListGetString() |
| 1610 | * which handles these types */ |
| 1611 | buffer = xmlNodeListGetString(doc, ent->children, 1); |
| 1612 | if (buffer != NULL) { |
| 1613 | ret = xmlStrcat(ret, buffer); |
| 1614 | xmlFree(buffer); |
| 1615 | } |
| 1616 | } else { |
| 1617 | ret = xmlStrcat(ret, node->content); |
| 1618 | } |
| 1619 | } else { |
| 1620 | xmlChar buf[2]; |
| 1621 | |
| 1622 | buf[0] = '&'; |
| 1623 | buf[1] = 0; |
| 1624 | ret = xmlStrncat(ret, buf, 1); |
| 1625 | ret = xmlStrcat(ret, node->name); |
| 1626 | buf[0] = ';'; |
| 1627 | buf[1] = 0; |
| 1628 | ret = xmlStrncat(ret, buf, 1); |
| 1629 | } |
| 1630 | } |
| 1631 | #if 0 |
| 1632 | else { |
| 1633 | xmlGenericError(xmlGenericErrorContext, |
| 1634 | "xmlGetNodeListString : invalid node type %d\n", |
| 1635 | node->type); |
| 1636 | } |
| 1637 | #endif |
| 1638 | node = node->next; |
| 1639 | } |
| 1640 | return (ret); |
| 1641 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1642 | |
| 1643 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1644 | /** |
| 1645 | * xmlNodeListGetRawString: |
| 1646 | * @doc: the document |
| 1647 | * @list: a Node list |
| 1648 | * @inLine: should we replace entity contents or show their external form |
| 1649 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1650 | * Builds the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1651 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 1652 | * this function doesn't do any character encoding handling. |
| 1653 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1654 | * 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] | 1655 | */ |
| 1656 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1657 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1658 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1659 | xmlNodePtr node = list; |
| 1660 | xmlChar *ret = NULL; |
| 1661 | xmlEntityPtr ent; |
| 1662 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1663 | if (list == NULL) |
| 1664 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1665 | |
| 1666 | while (node != NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1667 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1668 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1669 | if (inLine) { |
| 1670 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1671 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1672 | xmlChar *buffer; |
| 1673 | |
| 1674 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 1675 | if (buffer != NULL) { |
| 1676 | ret = xmlStrcat(ret, buffer); |
| 1677 | xmlFree(buffer); |
| 1678 | } |
| 1679 | } |
| 1680 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1681 | if (inLine) { |
| 1682 | ent = xmlGetDocEntity(doc, node->name); |
| 1683 | if (ent != NULL) { |
| 1684 | xmlChar *buffer; |
| 1685 | |
| 1686 | /* an entity content can be any "well balanced chunk", |
| 1687 | * i.e. the result of the content [43] production: |
| 1688 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1689 | * So it can contain text, CDATA section or nested |
| 1690 | * entity reference nodes (among others). |
| 1691 | * -> we recursive call xmlNodeListGetRawString() |
| 1692 | * which handles these types */ |
| 1693 | buffer = |
| 1694 | xmlNodeListGetRawString(doc, ent->children, 1); |
| 1695 | if (buffer != NULL) { |
| 1696 | ret = xmlStrcat(ret, buffer); |
| 1697 | xmlFree(buffer); |
| 1698 | } |
| 1699 | } else { |
| 1700 | ret = xmlStrcat(ret, node->content); |
| 1701 | } |
| 1702 | } else { |
| 1703 | xmlChar buf[2]; |
| 1704 | |
| 1705 | buf[0] = '&'; |
| 1706 | buf[1] = 0; |
| 1707 | ret = xmlStrncat(ret, buf, 1); |
| 1708 | ret = xmlStrcat(ret, node->name); |
| 1709 | buf[0] = ';'; |
| 1710 | buf[1] = 0; |
| 1711 | ret = xmlStrncat(ret, buf, 1); |
| 1712 | } |
| 1713 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1714 | #if 0 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1715 | else { |
| 1716 | xmlGenericError(xmlGenericErrorContext, |
| 1717 | "xmlGetNodeListString : invalid node type %d\n", |
| 1718 | node->type); |
| 1719 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1720 | #endif |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1721 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1722 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1723 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1724 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1725 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1726 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1727 | static xmlAttrPtr |
| 1728 | xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns, |
| 1729 | const xmlChar * name, const xmlChar * value, |
| 1730 | int eatname) |
| 1731 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1732 | xmlAttrPtr cur; |
| 1733 | xmlDocPtr doc = NULL; |
| 1734 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1735 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1736 | if (eatname == 1) |
| 1737 | xmlFree((xmlChar *) name); |
| 1738 | return (NULL); |
| 1739 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1740 | |
| 1741 | /* |
| 1742 | * Allocate a new property and fill the fields. |
| 1743 | */ |
| 1744 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1745 | if (cur == NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1746 | if (eatname == 1) |
| 1747 | xmlFree((xmlChar *) name); |
| 1748 | xmlTreeErrMemory("building attribute"); |
| 1749 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1750 | } |
| 1751 | memset(cur, 0, sizeof(xmlAttr)); |
| 1752 | cur->type = XML_ATTRIBUTE_NODE; |
| 1753 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1754 | cur->parent = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1755 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1756 | doc = node->doc; |
| 1757 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1758 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1759 | cur->ns = ns; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1760 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1761 | if (eatname == 0) { |
| 1762 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1763 | cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1); |
| 1764 | else |
| 1765 | cur->name = xmlStrdup(name); |
| 1766 | } else |
| 1767 | cur->name = name; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1768 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1769 | if (value != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1770 | xmlChar *buffer; |
| 1771 | xmlNodePtr tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1772 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1773 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1774 | cur->children = xmlStringGetNodeList(doc, buffer); |
| 1775 | cur->last = NULL; |
| 1776 | tmp = cur->children; |
| 1777 | while (tmp != NULL) { |
| 1778 | tmp->parent = (xmlNodePtr) cur; |
| 1779 | if (tmp->next == NULL) |
| 1780 | cur->last = tmp; |
| 1781 | tmp = tmp->next; |
| 1782 | } |
| 1783 | xmlFree(buffer); |
| 1784 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1785 | |
| 1786 | /* |
| 1787 | * Add it at the end to preserve parsing order ... |
| 1788 | */ |
| 1789 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1790 | if (node->properties == NULL) { |
| 1791 | node->properties = cur; |
| 1792 | } else { |
| 1793 | xmlAttrPtr prev = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1794 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1795 | while (prev->next != NULL) |
| 1796 | prev = prev->next; |
| 1797 | prev->next = cur; |
| 1798 | cur->prev = prev; |
| 1799 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1800 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1801 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1802 | if (xmlIsID((node == NULL) ? NULL : node->doc, node, cur) == 1) |
| 1803 | xmlAddID(NULL, node->doc, value, cur); |
| 1804 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1805 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1806 | xmlRegisterNodeDefaultValue((xmlNodePtr) cur); |
| 1807 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1808 | } |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1809 | |
| 1810 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 1811 | defined(LIBXML_SCHEMAS_ENABLED) |
| 1812 | /** |
| 1813 | * xmlNewProp: |
| 1814 | * @node: the holding node |
| 1815 | * @name: the name of the attribute |
| 1816 | * @value: the value of the attribute |
| 1817 | * |
| 1818 | * Create a new property carried by a node. |
| 1819 | * Returns a pointer to the attribute |
| 1820 | */ |
| 1821 | xmlAttrPtr |
| 1822 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 1823 | |
| 1824 | if (name == NULL) { |
| 1825 | #ifdef DEBUG_TREE |
| 1826 | xmlGenericError(xmlGenericErrorContext, |
| 1827 | "xmlNewProp : name == NULL\n"); |
| 1828 | #endif |
| 1829 | return(NULL); |
| 1830 | } |
| 1831 | |
| 1832 | return xmlNewPropInternal(node, NULL, name, value, 0); |
| 1833 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1834 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1835 | |
| 1836 | /** |
| 1837 | * xmlNewNsProp: |
| 1838 | * @node: the holding node |
| 1839 | * @ns: the namespace |
| 1840 | * @name: the name of the attribute |
| 1841 | * @value: the value of the attribute |
| 1842 | * |
| 1843 | * Create a new property tagged with a namespace and carried by a node. |
| 1844 | * Returns a pointer to the attribute |
| 1845 | */ |
| 1846 | xmlAttrPtr |
| 1847 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 1848 | const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1849 | |
| 1850 | if (name == NULL) { |
| 1851 | #ifdef DEBUG_TREE |
| 1852 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1853 | "xmlNewNsProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1854 | #endif |
| 1855 | return(NULL); |
| 1856 | } |
| 1857 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1858 | return xmlNewPropInternal(node, ns, name, value, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1862 | * xmlNewNsPropEatName: |
| 1863 | * @node: the holding node |
| 1864 | * @ns: the namespace |
| 1865 | * @name: the name of the attribute |
| 1866 | * @value: the value of the attribute |
| 1867 | * |
| 1868 | * Create a new property tagged with a namespace and carried by a node. |
| 1869 | * Returns a pointer to the attribute |
| 1870 | */ |
| 1871 | xmlAttrPtr |
| 1872 | xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name, |
| 1873 | const xmlChar *value) { |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1874 | |
| 1875 | if (name == NULL) { |
| 1876 | #ifdef DEBUG_TREE |
| 1877 | xmlGenericError(xmlGenericErrorContext, |
| 1878 | "xmlNewNsPropEatName : name == NULL\n"); |
| 1879 | #endif |
| 1880 | return(NULL); |
| 1881 | } |
| 1882 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1883 | return xmlNewPropInternal(node, ns, name, value, 1); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1887 | * xmlNewDocProp: |
| 1888 | * @doc: the document |
| 1889 | * @name: the name of the attribute |
| 1890 | * @value: the value of the attribute |
| 1891 | * |
| 1892 | * Create a new property carried by a document. |
| 1893 | * Returns a pointer to the attribute |
| 1894 | */ |
| 1895 | xmlAttrPtr |
| 1896 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
| 1897 | xmlAttrPtr cur; |
| 1898 | |
| 1899 | if (name == NULL) { |
| 1900 | #ifdef DEBUG_TREE |
| 1901 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1902 | "xmlNewDocProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1903 | #endif |
| 1904 | return(NULL); |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Allocate a new property and fill the fields. |
| 1909 | */ |
| 1910 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1911 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1912 | xmlTreeErrMemory("building attribute"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1913 | return(NULL); |
| 1914 | } |
| 1915 | memset(cur, 0, sizeof(xmlAttr)); |
| 1916 | cur->type = XML_ATTRIBUTE_NODE; |
| 1917 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 1918 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1919 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 1920 | else |
| 1921 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1922 | cur->doc = doc; |
| 1923 | if (value != NULL) { |
| 1924 | xmlNodePtr tmp; |
| 1925 | |
| 1926 | cur->children = xmlStringGetNodeList(doc, value); |
| 1927 | cur->last = NULL; |
| 1928 | |
| 1929 | tmp = cur->children; |
| 1930 | while (tmp != NULL) { |
| 1931 | tmp->parent = (xmlNodePtr) cur; |
| 1932 | if (tmp->next == NULL) |
| 1933 | cur->last = tmp; |
| 1934 | tmp = tmp->next; |
| 1935 | } |
| 1936 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1937 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1938 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1939 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1940 | return(cur); |
| 1941 | } |
| 1942 | |
| 1943 | /** |
| 1944 | * xmlFreePropList: |
| 1945 | * @cur: the first property in the list |
| 1946 | * |
| 1947 | * Free a property and all its siblings, all the children are freed too. |
| 1948 | */ |
| 1949 | void |
| 1950 | xmlFreePropList(xmlAttrPtr cur) { |
| 1951 | xmlAttrPtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1952 | if (cur == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1953 | while (cur != NULL) { |
| 1954 | next = cur->next; |
| 1955 | xmlFreeProp(cur); |
| 1956 | cur = next; |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | /** |
| 1961 | * xmlFreeProp: |
| 1962 | * @cur: an attribute |
| 1963 | * |
| 1964 | * Free one attribute, all the content is freed too |
| 1965 | */ |
| 1966 | void |
| 1967 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1968 | xmlDictPtr dict = NULL; |
| 1969 | if (cur == NULL) return; |
| 1970 | |
| 1971 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1972 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1973 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1974 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1975 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1976 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | da6f4af | 2005-06-20 17:17:54 +0000 | [diff] [blame] | 1977 | if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) { |
| 1978 | xmlRemoveID(cur->doc, cur); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1979 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1980 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1981 | DICT_FREE(cur->name) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1982 | xmlFree(cur); |
| 1983 | } |
| 1984 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1985 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1986 | /** |
| 1987 | * xmlRemoveProp: |
| 1988 | * @cur: an attribute |
| 1989 | * |
| 1990 | * Unlink and free one attribute, all the content is freed too |
| 1991 | * Note this doesn't work for namespace definition attributes |
| 1992 | * |
| 1993 | * Returns 0 if success and -1 in case of error. |
| 1994 | */ |
| 1995 | int |
| 1996 | xmlRemoveProp(xmlAttrPtr cur) { |
| 1997 | xmlAttrPtr tmp; |
| 1998 | if (cur == NULL) { |
| 1999 | #ifdef DEBUG_TREE |
| 2000 | xmlGenericError(xmlGenericErrorContext, |
| 2001 | "xmlRemoveProp : cur == NULL\n"); |
| 2002 | #endif |
| 2003 | return(-1); |
| 2004 | } |
| 2005 | if (cur->parent == NULL) { |
| 2006 | #ifdef DEBUG_TREE |
| 2007 | xmlGenericError(xmlGenericErrorContext, |
| 2008 | "xmlRemoveProp : cur->parent == NULL\n"); |
| 2009 | #endif |
| 2010 | return(-1); |
| 2011 | } |
| 2012 | tmp = cur->parent->properties; |
| 2013 | if (tmp == cur) { |
| 2014 | cur->parent->properties = cur->next; |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2015 | if (cur->next != NULL) |
| 2016 | cur->next->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2017 | xmlFreeProp(cur); |
| 2018 | return(0); |
| 2019 | } |
| 2020 | while (tmp != NULL) { |
| 2021 | if (tmp->next == cur) { |
| 2022 | tmp->next = cur->next; |
| 2023 | if (tmp->next != NULL) |
| 2024 | tmp->next->prev = tmp; |
| 2025 | xmlFreeProp(cur); |
| 2026 | return(0); |
| 2027 | } |
| 2028 | tmp = tmp->next; |
| 2029 | } |
| 2030 | #ifdef DEBUG_TREE |
| 2031 | xmlGenericError(xmlGenericErrorContext, |
| 2032 | "xmlRemoveProp : attribute not owned by its node\n"); |
| 2033 | #endif |
| 2034 | return(-1); |
| 2035 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2036 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2037 | |
| 2038 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2039 | * xmlNewDocPI: |
| 2040 | * @doc: the target document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2041 | * @name: the processing instruction name |
| 2042 | * @content: the PI content |
| 2043 | * |
| 2044 | * Creation of a processing instruction element. |
| 2045 | * Returns a pointer to the new node object. |
| 2046 | */ |
| 2047 | xmlNodePtr |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2048 | xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2049 | xmlNodePtr cur; |
| 2050 | |
| 2051 | if (name == NULL) { |
| 2052 | #ifdef DEBUG_TREE |
| 2053 | xmlGenericError(xmlGenericErrorContext, |
| 2054 | "xmlNewPI : name == NULL\n"); |
| 2055 | #endif |
| 2056 | return(NULL); |
| 2057 | } |
| 2058 | |
| 2059 | /* |
| 2060 | * Allocate a new node and fill the fields. |
| 2061 | */ |
| 2062 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2063 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2064 | xmlTreeErrMemory("building PI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2065 | return(NULL); |
| 2066 | } |
| 2067 | memset(cur, 0, sizeof(xmlNode)); |
| 2068 | cur->type = XML_PI_NODE; |
| 2069 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2070 | if ((doc != NULL) && (doc->dict != NULL)) |
| 2071 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 2072 | else |
| 2073 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2074 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2075 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2076 | } |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 2077 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2078 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2079 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2080 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2081 | return(cur); |
| 2082 | } |
| 2083 | |
| 2084 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2085 | * xmlNewPI: |
| 2086 | * @name: the processing instruction name |
| 2087 | * @content: the PI content |
| 2088 | * |
| 2089 | * Creation of a processing instruction element. |
| 2090 | * Use xmlDocNewPI preferably to get string interning |
| 2091 | * |
| 2092 | * Returns a pointer to the new node object. |
| 2093 | */ |
| 2094 | xmlNodePtr |
| 2095 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
| 2096 | return(xmlNewDocPI(NULL, name, content)); |
| 2097 | } |
| 2098 | |
| 2099 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2100 | * xmlNewNode: |
| 2101 | * @ns: namespace if any |
| 2102 | * @name: the node name |
| 2103 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2104 | * Creation of a new node element. @ns is optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2105 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2106 | * Returns a pointer to the new node object. Uses xmlStrdup() to make |
| 2107 | * copy of @name. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2108 | */ |
| 2109 | xmlNodePtr |
| 2110 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 2111 | xmlNodePtr cur; |
| 2112 | |
| 2113 | if (name == NULL) { |
| 2114 | #ifdef DEBUG_TREE |
| 2115 | xmlGenericError(xmlGenericErrorContext, |
| 2116 | "xmlNewNode : name == NULL\n"); |
| 2117 | #endif |
| 2118 | return(NULL); |
| 2119 | } |
| 2120 | |
| 2121 | /* |
| 2122 | * Allocate a new node and fill the fields. |
| 2123 | */ |
| 2124 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2125 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2126 | xmlTreeErrMemory("building node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2127 | return(NULL); |
| 2128 | } |
| 2129 | memset(cur, 0, sizeof(xmlNode)); |
| 2130 | cur->type = XML_ELEMENT_NODE; |
| 2131 | |
| 2132 | cur->name = xmlStrdup(name); |
| 2133 | cur->ns = ns; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2134 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2135 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2136 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2137 | return(cur); |
| 2138 | } |
| 2139 | |
| 2140 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2141 | * xmlNewNodeEatName: |
| 2142 | * @ns: namespace if any |
| 2143 | * @name: the node name |
| 2144 | * |
| 2145 | * Creation of a new node element. @ns is optional (NULL). |
| 2146 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2147 | * Returns a pointer to the new node object, with pointer @name as |
| 2148 | * new node's name. Use xmlNewNode() if a copy of @name string is |
| 2149 | * is needed as new node's name. |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2150 | */ |
| 2151 | xmlNodePtr |
| 2152 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
| 2153 | xmlNodePtr cur; |
| 2154 | |
| 2155 | if (name == NULL) { |
| 2156 | #ifdef DEBUG_TREE |
| 2157 | xmlGenericError(xmlGenericErrorContext, |
| 2158 | "xmlNewNode : name == NULL\n"); |
| 2159 | #endif |
| 2160 | return(NULL); |
| 2161 | } |
| 2162 | |
| 2163 | /* |
| 2164 | * Allocate a new node and fill the fields. |
| 2165 | */ |
| 2166 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2167 | if (cur == NULL) { |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 2168 | xmlFree(name); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2169 | xmlTreeErrMemory("building node"); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2170 | return(NULL); |
| 2171 | } |
| 2172 | memset(cur, 0, sizeof(xmlNode)); |
| 2173 | cur->type = XML_ELEMENT_NODE; |
| 2174 | |
| 2175 | cur->name = name; |
| 2176 | cur->ns = ns; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2177 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2178 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2179 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2180 | return(cur); |
| 2181 | } |
| 2182 | |
| 2183 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2184 | * xmlNewDocNode: |
| 2185 | * @doc: the document |
| 2186 | * @ns: namespace if any |
| 2187 | * @name: the node name |
| 2188 | * @content: the XML text content if any |
| 2189 | * |
| 2190 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2191 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2192 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2193 | * references, but XML special chars need to be escaped first by using |
| 2194 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2195 | * need entities support. |
| 2196 | * |
| 2197 | * Returns a pointer to the new node object. |
| 2198 | */ |
| 2199 | xmlNodePtr |
| 2200 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2201 | const xmlChar *name, const xmlChar *content) { |
| 2202 | xmlNodePtr cur; |
| 2203 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2204 | if ((doc != NULL) && (doc->dict != NULL)) |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2205 | cur = xmlNewNodeEatName(ns, (xmlChar *) |
| 2206 | xmlDictLookup(doc->dict, name, -1)); |
| 2207 | else |
| 2208 | cur = xmlNewNode(ns, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2270 | cur = xmlNewDocNode(doc, ns, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2437 | if (name == NULL) |
| 2438 | return(NULL); |
| 2439 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2440 | /* |
| 2441 | * Allocate a new node and fill the fields. |
| 2442 | */ |
| 2443 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2444 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2445 | xmlTreeErrMemory("building character reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2446 | return(NULL); |
| 2447 | } |
| 2448 | memset(cur, 0, sizeof(xmlNode)); |
| 2449 | cur->type = XML_ENTITY_REF_NODE; |
| 2450 | |
| 2451 | cur->doc = doc; |
| 2452 | if (name[0] == '&') { |
| 2453 | int len; |
| 2454 | name++; |
| 2455 | len = xmlStrlen(name); |
| 2456 | if (name[len - 1] == ';') |
| 2457 | cur->name = xmlStrndup(name, len - 1); |
| 2458 | else |
| 2459 | cur->name = xmlStrndup(name, len); |
| 2460 | } else |
| 2461 | cur->name = xmlStrdup(name); |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2462 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2463 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2464 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2465 | return(cur); |
| 2466 | } |
| 2467 | |
| 2468 | /** |
| 2469 | * xmlNewReference: |
| 2470 | * @doc: the document |
| 2471 | * @name: the reference name, or the reference string with & and ; |
| 2472 | * |
| 2473 | * Creation of a new reference node. |
| 2474 | * Returns a pointer to the new node object. |
| 2475 | */ |
| 2476 | xmlNodePtr |
| 2477 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 2478 | xmlNodePtr cur; |
| 2479 | xmlEntityPtr ent; |
| 2480 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2481 | if (name == NULL) |
| 2482 | return(NULL); |
| 2483 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2484 | /* |
| 2485 | * Allocate a new node and fill the fields. |
| 2486 | */ |
| 2487 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2488 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2489 | xmlTreeErrMemory("building reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2490 | return(NULL); |
| 2491 | } |
| 2492 | memset(cur, 0, sizeof(xmlNode)); |
| 2493 | cur->type = XML_ENTITY_REF_NODE; |
| 2494 | |
| 2495 | cur->doc = doc; |
| 2496 | if (name[0] == '&') { |
| 2497 | int len; |
| 2498 | name++; |
| 2499 | len = xmlStrlen(name); |
| 2500 | if (name[len - 1] == ';') |
| 2501 | cur->name = xmlStrndup(name, len - 1); |
| 2502 | else |
| 2503 | cur->name = xmlStrndup(name, len); |
| 2504 | } else |
| 2505 | cur->name = xmlStrdup(name); |
| 2506 | |
| 2507 | ent = xmlGetDocEntity(doc, cur->name); |
| 2508 | if (ent != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2509 | cur->content = ent->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2510 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2511 | * 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] | 2512 | * updated. Not sure if this is 100% correct. |
| 2513 | * -George |
| 2514 | */ |
| 2515 | cur->children = (xmlNodePtr) ent; |
| 2516 | cur->last = (xmlNodePtr) ent; |
| 2517 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2518 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2519 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2520 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2521 | return(cur); |
| 2522 | } |
| 2523 | |
| 2524 | /** |
| 2525 | * xmlNewDocText: |
| 2526 | * @doc: the document |
| 2527 | * @content: the text content |
| 2528 | * |
| 2529 | * Creation of a new text node within a document. |
| 2530 | * Returns a pointer to the new node object. |
| 2531 | */ |
| 2532 | xmlNodePtr |
| 2533 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 2534 | xmlNodePtr cur; |
| 2535 | |
| 2536 | cur = xmlNewText(content); |
| 2537 | if (cur != NULL) cur->doc = doc; |
| 2538 | return(cur); |
| 2539 | } |
| 2540 | |
| 2541 | /** |
| 2542 | * xmlNewTextLen: |
| 2543 | * @content: the text content |
| 2544 | * @len: the text len. |
| 2545 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2546 | * 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] | 2547 | * Returns a pointer to the new node object. |
| 2548 | */ |
| 2549 | xmlNodePtr |
| 2550 | xmlNewTextLen(const xmlChar *content, int len) { |
| 2551 | xmlNodePtr cur; |
| 2552 | |
| 2553 | /* |
| 2554 | * Allocate a new node and fill the fields. |
| 2555 | */ |
| 2556 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2557 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2558 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2559 | return(NULL); |
| 2560 | } |
| 2561 | memset(cur, 0, sizeof(xmlNode)); |
| 2562 | cur->type = XML_TEXT_NODE; |
| 2563 | |
| 2564 | cur->name = xmlStringText; |
| 2565 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2566 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2567 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2568 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2569 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2570 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2571 | return(cur); |
| 2572 | } |
| 2573 | |
| 2574 | /** |
| 2575 | * xmlNewDocTextLen: |
| 2576 | * @doc: the document |
| 2577 | * @content: the text content |
| 2578 | * @len: the text len. |
| 2579 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2580 | * 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] | 2581 | * text node pertain to a given document. |
| 2582 | * Returns a pointer to the new node object. |
| 2583 | */ |
| 2584 | xmlNodePtr |
| 2585 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2586 | xmlNodePtr cur; |
| 2587 | |
| 2588 | cur = xmlNewTextLen(content, len); |
| 2589 | if (cur != NULL) cur->doc = doc; |
| 2590 | return(cur); |
| 2591 | } |
| 2592 | |
| 2593 | /** |
| 2594 | * xmlNewComment: |
| 2595 | * @content: the comment content |
| 2596 | * |
| 2597 | * Creation of a new node containing a comment. |
| 2598 | * Returns a pointer to the new node object. |
| 2599 | */ |
| 2600 | xmlNodePtr |
| 2601 | xmlNewComment(const xmlChar *content) { |
| 2602 | xmlNodePtr cur; |
| 2603 | |
| 2604 | /* |
| 2605 | * Allocate a new node and fill the fields. |
| 2606 | */ |
| 2607 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2608 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2609 | xmlTreeErrMemory("building comment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2610 | return(NULL); |
| 2611 | } |
| 2612 | memset(cur, 0, sizeof(xmlNode)); |
| 2613 | cur->type = XML_COMMENT_NODE; |
| 2614 | |
| 2615 | cur->name = xmlStringComment; |
| 2616 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2617 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2618 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2619 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2620 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2621 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2622 | return(cur); |
| 2623 | } |
| 2624 | |
| 2625 | /** |
| 2626 | * xmlNewCDataBlock: |
| 2627 | * @doc: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2628 | * @content: the CDATA block content content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2629 | * @len: the length of the block |
| 2630 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2631 | * Creation of a new node containing a CDATA block. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2632 | * Returns a pointer to the new node object. |
| 2633 | */ |
| 2634 | xmlNodePtr |
| 2635 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2636 | xmlNodePtr cur; |
| 2637 | |
| 2638 | /* |
| 2639 | * Allocate a new node and fill the fields. |
| 2640 | */ |
| 2641 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2642 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2643 | xmlTreeErrMemory("building CDATA"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2644 | return(NULL); |
| 2645 | } |
| 2646 | memset(cur, 0, sizeof(xmlNode)); |
| 2647 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2648 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2649 | |
| 2650 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2651 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2652 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2653 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2654 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2655 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2656 | return(cur); |
| 2657 | } |
| 2658 | |
| 2659 | /** |
| 2660 | * xmlNewDocComment: |
| 2661 | * @doc: the document |
| 2662 | * @content: the comment content |
| 2663 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2664 | * Creation of a new node containing a comment within a document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2665 | * Returns a pointer to the new node object. |
| 2666 | */ |
| 2667 | xmlNodePtr |
| 2668 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 2669 | xmlNodePtr cur; |
| 2670 | |
| 2671 | cur = xmlNewComment(content); |
| 2672 | if (cur != NULL) cur->doc = doc; |
| 2673 | return(cur); |
| 2674 | } |
| 2675 | |
| 2676 | /** |
| 2677 | * xmlSetTreeDoc: |
| 2678 | * @tree: the top element |
| 2679 | * @doc: the document |
| 2680 | * |
| 2681 | * update all nodes under the tree to point to the right document |
| 2682 | */ |
| 2683 | void |
| 2684 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2685 | xmlAttrPtr prop; |
| 2686 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2687 | if (tree == NULL) |
| 2688 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2689 | if (tree->doc != doc) { |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2690 | if(tree->type == XML_ELEMENT_NODE) { |
| 2691 | prop = tree->properties; |
| 2692 | while (prop != NULL) { |
| 2693 | prop->doc = doc; |
| 2694 | xmlSetListDoc(prop->children, doc); |
| 2695 | prop = prop->next; |
| 2696 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2697 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2698 | if (tree->children != NULL) |
| 2699 | xmlSetListDoc(tree->children, doc); |
| 2700 | tree->doc = doc; |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | /** |
| 2705 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2706 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2707 | * @doc: the document |
| 2708 | * |
| 2709 | * update all nodes in the list to point to the right document |
| 2710 | */ |
| 2711 | void |
| 2712 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2713 | xmlNodePtr cur; |
| 2714 | |
| 2715 | if (list == NULL) |
| 2716 | return; |
| 2717 | cur = list; |
| 2718 | while (cur != NULL) { |
| 2719 | if (cur->doc != doc) |
| 2720 | xmlSetTreeDoc(cur, doc); |
| 2721 | cur = cur->next; |
| 2722 | } |
| 2723 | } |
| 2724 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 2725 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2726 | /** |
| 2727 | * xmlNewChild: |
| 2728 | * @parent: the parent node |
| 2729 | * @ns: a namespace if any |
| 2730 | * @name: the name of the child |
| 2731 | * @content: the XML content of the child if any. |
| 2732 | * |
| 2733 | * 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] | 2734 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2735 | * created element inherits the namespace of @parent. If @content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2736 | * 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] | 2737 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 2738 | * references. XML special chars must be escaped first by using |
| 2739 | * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2740 | * |
| 2741 | * Returns a pointer to the new node object. |
| 2742 | */ |
| 2743 | xmlNodePtr |
| 2744 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2745 | const xmlChar *name, const xmlChar *content) { |
| 2746 | xmlNodePtr cur, prev; |
| 2747 | |
| 2748 | if (parent == NULL) { |
| 2749 | #ifdef DEBUG_TREE |
| 2750 | xmlGenericError(xmlGenericErrorContext, |
| 2751 | "xmlNewChild : parent == NULL\n"); |
| 2752 | #endif |
| 2753 | return(NULL); |
| 2754 | } |
| 2755 | |
| 2756 | if (name == NULL) { |
| 2757 | #ifdef DEBUG_TREE |
| 2758 | xmlGenericError(xmlGenericErrorContext, |
| 2759 | "xmlNewChild : name == NULL\n"); |
| 2760 | #endif |
| 2761 | return(NULL); |
| 2762 | } |
| 2763 | |
| 2764 | /* |
| 2765 | * Allocate a new node |
| 2766 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2767 | if (parent->type == XML_ELEMENT_NODE) { |
| 2768 | if (ns == NULL) |
| 2769 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2770 | else |
| 2771 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2772 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2773 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2774 | if (ns == NULL) |
| 2775 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2776 | else |
| 2777 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2778 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2779 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2780 | } else { |
| 2781 | return(NULL); |
| 2782 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2783 | if (cur == NULL) return(NULL); |
| 2784 | |
| 2785 | /* |
| 2786 | * add the new element at the end of the children list. |
| 2787 | */ |
| 2788 | cur->type = XML_ELEMENT_NODE; |
| 2789 | cur->parent = parent; |
| 2790 | cur->doc = parent->doc; |
| 2791 | if (parent->children == NULL) { |
| 2792 | parent->children = cur; |
| 2793 | parent->last = cur; |
| 2794 | } else { |
| 2795 | prev = parent->last; |
| 2796 | prev->next = cur; |
| 2797 | cur->prev = prev; |
| 2798 | parent->last = cur; |
| 2799 | } |
| 2800 | |
| 2801 | return(cur); |
| 2802 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2803 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2804 | |
| 2805 | /** |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2806 | * xmlAddPropSibling: |
| 2807 | * @prev: the attribute to which @prop is added after |
| 2808 | * @cur: the base attribute passed to calling function |
| 2809 | * @prop: the new attribute |
| 2810 | * |
| 2811 | * Add a new attribute after @prev using @cur as base attribute. |
| 2812 | * When inserting before @cur, @prev is passed as @cur->prev. |
| 2813 | * When inserting after @cur, @prev is passed as @cur. |
| 2814 | * If an existing attribute is found it is detroyed prior to adding @prop. |
| 2815 | * |
| 2816 | * Returns the attribute being inserted or NULL in case of error. |
| 2817 | */ |
| 2818 | static xmlNodePtr |
| 2819 | xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) { |
| 2820 | xmlAttrPtr attr; |
| 2821 | |
| 2822 | if (cur->type != XML_ATTRIBUTE_NODE) |
| 2823 | return(NULL); |
| 2824 | |
| 2825 | /* check if an attribute with the same name exists */ |
| 2826 | if (prop->ns == NULL) |
| 2827 | attr = xmlHasNsProp(cur->parent, prop->name, NULL); |
| 2828 | else |
| 2829 | attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href); |
| 2830 | |
| 2831 | if (prop->doc != cur->doc) { |
| 2832 | xmlSetTreeDoc(prop, cur->doc); |
| 2833 | } |
| 2834 | prop->parent = cur->parent; |
| 2835 | prop->prev = prev; |
| 2836 | if (prev != NULL) { |
| 2837 | prop->next = prev->next; |
| 2838 | prev->next = prop; |
| 2839 | if (prop->next) |
| 2840 | prop->next->prev = prop; |
| 2841 | } else { |
| 2842 | prop->next = cur; |
| 2843 | cur->prev = prop; |
| 2844 | } |
| 2845 | if (prop->prev == NULL && prop->parent != NULL) |
| 2846 | prop->parent->properties = (xmlAttrPtr) prop; |
| 2847 | if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) { |
| 2848 | /* different instance, destroy it (attributes must be unique) */ |
| 2849 | xmlRemoveProp((xmlAttrPtr) attr); |
| 2850 | } |
| 2851 | return prop; |
| 2852 | } |
| 2853 | |
| 2854 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2855 | * xmlAddNextSibling: |
| 2856 | * @cur: the child node |
| 2857 | * @elem: the new node |
| 2858 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2859 | * Add a new node @elem as the next sibling of @cur |
| 2860 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2861 | * first unlinked from its existing context. |
| 2862 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2863 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2864 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2865 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2866 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2867 | */ |
| 2868 | xmlNodePtr |
| 2869 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2870 | if (cur == NULL) { |
| 2871 | #ifdef DEBUG_TREE |
| 2872 | xmlGenericError(xmlGenericErrorContext, |
| 2873 | "xmlAddNextSibling : cur == NULL\n"); |
| 2874 | #endif |
| 2875 | return(NULL); |
| 2876 | } |
| 2877 | if (elem == NULL) { |
| 2878 | #ifdef DEBUG_TREE |
| 2879 | xmlGenericError(xmlGenericErrorContext, |
| 2880 | "xmlAddNextSibling : elem == NULL\n"); |
| 2881 | #endif |
| 2882 | return(NULL); |
| 2883 | } |
| 2884 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2885 | if (cur == elem) { |
| 2886 | #ifdef DEBUG_TREE |
| 2887 | xmlGenericError(xmlGenericErrorContext, |
| 2888 | "xmlAddNextSibling : cur == elem\n"); |
| 2889 | #endif |
| 2890 | return(NULL); |
| 2891 | } |
| 2892 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2893 | xmlUnlinkNode(elem); |
| 2894 | |
| 2895 | if (elem->type == XML_TEXT_NODE) { |
| 2896 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2897 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2898 | xmlFreeNode(elem); |
| 2899 | return(cur); |
| 2900 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2901 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 2902 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2903 | xmlChar *tmp; |
| 2904 | |
| 2905 | tmp = xmlStrdup(elem->content); |
| 2906 | tmp = xmlStrcat(tmp, cur->next->content); |
| 2907 | xmlNodeSetContent(cur->next, tmp); |
| 2908 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2909 | xmlFreeNode(elem); |
| 2910 | return(cur->next); |
| 2911 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2912 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2913 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | if (elem->doc != cur->doc) { |
| 2917 | xmlSetTreeDoc(elem, cur->doc); |
| 2918 | } |
| 2919 | elem->parent = cur->parent; |
| 2920 | elem->prev = cur; |
| 2921 | elem->next = cur->next; |
| 2922 | cur->next = elem; |
| 2923 | if (elem->next != NULL) |
| 2924 | elem->next->prev = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2925 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2926 | elem->parent->last = elem; |
| 2927 | return(elem); |
| 2928 | } |
| 2929 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 2930 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 2931 | defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2932 | /** |
| 2933 | * xmlAddPrevSibling: |
| 2934 | * @cur: the child node |
| 2935 | * @elem: the new node |
| 2936 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2937 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2938 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2939 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2940 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2941 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2942 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2943 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2944 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2945 | */ |
| 2946 | xmlNodePtr |
| 2947 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2948 | if (cur == NULL) { |
| 2949 | #ifdef DEBUG_TREE |
| 2950 | xmlGenericError(xmlGenericErrorContext, |
| 2951 | "xmlAddPrevSibling : cur == NULL\n"); |
| 2952 | #endif |
| 2953 | return(NULL); |
| 2954 | } |
| 2955 | if (elem == NULL) { |
| 2956 | #ifdef DEBUG_TREE |
| 2957 | xmlGenericError(xmlGenericErrorContext, |
| 2958 | "xmlAddPrevSibling : elem == NULL\n"); |
| 2959 | #endif |
| 2960 | return(NULL); |
| 2961 | } |
| 2962 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2963 | if (cur == elem) { |
| 2964 | #ifdef DEBUG_TREE |
| 2965 | xmlGenericError(xmlGenericErrorContext, |
| 2966 | "xmlAddPrevSibling : cur == elem\n"); |
| 2967 | #endif |
| 2968 | return(NULL); |
| 2969 | } |
| 2970 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2971 | xmlUnlinkNode(elem); |
| 2972 | |
| 2973 | if (elem->type == XML_TEXT_NODE) { |
| 2974 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2975 | xmlChar *tmp; |
| 2976 | |
| 2977 | tmp = xmlStrdup(elem->content); |
| 2978 | tmp = xmlStrcat(tmp, cur->content); |
| 2979 | xmlNodeSetContent(cur, tmp); |
| 2980 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2981 | xmlFreeNode(elem); |
| 2982 | return(cur); |
| 2983 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2984 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 2985 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2986 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2987 | xmlFreeNode(elem); |
| 2988 | return(cur->prev); |
| 2989 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2990 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2991 | return xmlAddPropSibling(cur->prev, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | if (elem->doc != cur->doc) { |
| 2995 | xmlSetTreeDoc(elem, cur->doc); |
| 2996 | } |
| 2997 | elem->parent = cur->parent; |
| 2998 | elem->next = cur; |
| 2999 | elem->prev = cur->prev; |
| 3000 | cur->prev = elem; |
| 3001 | if (elem->prev != NULL) |
| 3002 | elem->prev->next = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3003 | if ((elem->parent != NULL) && (elem->parent->children == cur)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3004 | elem->parent->children = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3005 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3006 | return(elem); |
| 3007 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3008 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3009 | |
| 3010 | /** |
| 3011 | * xmlAddSibling: |
| 3012 | * @cur: the child node |
| 3013 | * @elem: the new node |
| 3014 | * |
| 3015 | * Add a new element @elem to the list of siblings of @cur |
| 3016 | * merging adjacent TEXT nodes (@elem may be freed) |
| 3017 | * If the new element was already inserted in a document it is |
| 3018 | * first unlinked from its existing context. |
| 3019 | * |
| 3020 | * Returns the new element or NULL in case of error. |
| 3021 | */ |
| 3022 | xmlNodePtr |
| 3023 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 3024 | xmlNodePtr parent; |
| 3025 | |
| 3026 | if (cur == NULL) { |
| 3027 | #ifdef DEBUG_TREE |
| 3028 | xmlGenericError(xmlGenericErrorContext, |
| 3029 | "xmlAddSibling : cur == NULL\n"); |
| 3030 | #endif |
| 3031 | return(NULL); |
| 3032 | } |
| 3033 | |
| 3034 | if (elem == NULL) { |
| 3035 | #ifdef DEBUG_TREE |
| 3036 | xmlGenericError(xmlGenericErrorContext, |
| 3037 | "xmlAddSibling : elem == NULL\n"); |
| 3038 | #endif |
| 3039 | return(NULL); |
| 3040 | } |
| 3041 | |
| 3042 | /* |
| 3043 | * Constant time is we can rely on the ->parent->last to find |
| 3044 | * the last sibling. |
| 3045 | */ |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3046 | if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3047 | (cur->parent->children != NULL) && |
| 3048 | (cur->parent->last != NULL) && |
| 3049 | (cur->parent->last->next == NULL)) { |
| 3050 | cur = cur->parent->last; |
| 3051 | } else { |
| 3052 | while (cur->next != NULL) cur = cur->next; |
| 3053 | } |
| 3054 | |
| 3055 | xmlUnlinkNode(elem); |
| 3056 | |
Daniel Veillard | e22dd5c | 2003-10-29 12:53:27 +0000 | [diff] [blame] | 3057 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) && |
| 3058 | (cur->name == elem->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3059 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3060 | xmlFreeNode(elem); |
| 3061 | return(cur); |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3062 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 3063 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | if (elem->doc != cur->doc) { |
| 3067 | xmlSetTreeDoc(elem, cur->doc); |
| 3068 | } |
| 3069 | parent = cur->parent; |
| 3070 | elem->prev = cur; |
| 3071 | elem->next = NULL; |
| 3072 | elem->parent = parent; |
| 3073 | cur->next = elem; |
| 3074 | if (parent != NULL) |
| 3075 | parent->last = elem; |
| 3076 | |
| 3077 | return(elem); |
| 3078 | } |
| 3079 | |
| 3080 | /** |
| 3081 | * xmlAddChildList: |
| 3082 | * @parent: the parent node |
| 3083 | * @cur: the first node in the list |
| 3084 | * |
| 3085 | * Add a list of node at the end of the child list of the parent |
| 3086 | * merging adjacent TEXT nodes (@cur may be freed) |
| 3087 | * |
| 3088 | * Returns the last child or NULL in case of error. |
| 3089 | */ |
| 3090 | xmlNodePtr |
| 3091 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 3092 | xmlNodePtr prev; |
| 3093 | |
| 3094 | if (parent == NULL) { |
| 3095 | #ifdef DEBUG_TREE |
| 3096 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3097 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3098 | #endif |
| 3099 | return(NULL); |
| 3100 | } |
| 3101 | |
| 3102 | if (cur == NULL) { |
| 3103 | #ifdef DEBUG_TREE |
| 3104 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3105 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3106 | #endif |
| 3107 | return(NULL); |
| 3108 | } |
| 3109 | |
| 3110 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 3111 | (cur->doc != parent->doc)) { |
| 3112 | #ifdef DEBUG_TREE |
| 3113 | xmlGenericError(xmlGenericErrorContext, |
| 3114 | "Elements moved to a different document\n"); |
| 3115 | #endif |
| 3116 | } |
| 3117 | |
| 3118 | /* |
| 3119 | * add the first element at the end of the children list. |
| 3120 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3121 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3122 | if (parent->children == NULL) { |
| 3123 | parent->children = cur; |
| 3124 | } else { |
| 3125 | /* |
| 3126 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 3127 | */ |
| 3128 | if ((cur->type == XML_TEXT_NODE) && |
| 3129 | (parent->last->type == XML_TEXT_NODE) && |
| 3130 | (cur->name == parent->last->name)) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3131 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3132 | /* |
| 3133 | * if it's the only child, nothing more to be done. |
| 3134 | */ |
| 3135 | if (cur->next == NULL) { |
| 3136 | xmlFreeNode(cur); |
| 3137 | return(parent->last); |
| 3138 | } |
| 3139 | prev = cur; |
| 3140 | cur = cur->next; |
| 3141 | xmlFreeNode(prev); |
| 3142 | } |
| 3143 | prev = parent->last; |
| 3144 | prev->next = cur; |
| 3145 | cur->prev = prev; |
| 3146 | } |
| 3147 | while (cur->next != NULL) { |
| 3148 | cur->parent = parent; |
| 3149 | if (cur->doc != parent->doc) { |
| 3150 | xmlSetTreeDoc(cur, parent->doc); |
| 3151 | } |
| 3152 | cur = cur->next; |
| 3153 | } |
| 3154 | cur->parent = parent; |
| 3155 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 3156 | parent->last = cur; |
| 3157 | |
| 3158 | return(cur); |
| 3159 | } |
| 3160 | |
| 3161 | /** |
| 3162 | * xmlAddChild: |
| 3163 | * @parent: the parent node |
| 3164 | * @cur: the child node |
| 3165 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3166 | * 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] | 3167 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3168 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 3169 | * If there is an attribute with equal name, it is first destroyed. |
| 3170 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3171 | * Returns the child or NULL in case of error. |
| 3172 | */ |
| 3173 | xmlNodePtr |
| 3174 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 3175 | xmlNodePtr prev; |
| 3176 | |
| 3177 | if (parent == NULL) { |
| 3178 | #ifdef DEBUG_TREE |
| 3179 | xmlGenericError(xmlGenericErrorContext, |
| 3180 | "xmlAddChild : parent == NULL\n"); |
| 3181 | #endif |
| 3182 | return(NULL); |
| 3183 | } |
| 3184 | |
| 3185 | if (cur == NULL) { |
| 3186 | #ifdef DEBUG_TREE |
| 3187 | xmlGenericError(xmlGenericErrorContext, |
| 3188 | "xmlAddChild : child == NULL\n"); |
| 3189 | #endif |
| 3190 | return(NULL); |
| 3191 | } |
| 3192 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3193 | if (parent == cur) { |
| 3194 | #ifdef DEBUG_TREE |
| 3195 | xmlGenericError(xmlGenericErrorContext, |
| 3196 | "xmlAddChild : parent == cur\n"); |
| 3197 | #endif |
| 3198 | return(NULL); |
| 3199 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3200 | /* |
| 3201 | * 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] | 3202 | * cur is then freed. |
| 3203 | */ |
| 3204 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3205 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3206 | (parent->content != NULL) && |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3207 | (parent->name == cur->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3208 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3209 | xmlFreeNode(cur); |
| 3210 | return(parent); |
| 3211 | } |
| 3212 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3213 | (parent->last->name == cur->name) && |
| 3214 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3215 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3216 | xmlFreeNode(cur); |
| 3217 | return(parent->last); |
| 3218 | } |
| 3219 | } |
| 3220 | |
| 3221 | /* |
| 3222 | * add the new element at the end of the children list. |
| 3223 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3224 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3225 | cur->parent = parent; |
| 3226 | if (cur->doc != parent->doc) { |
| 3227 | xmlSetTreeDoc(cur, parent->doc); |
| 3228 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3229 | /* this check prevents a loop on tree-traversions if a developer |
| 3230 | * tries to add a node to its parent multiple times |
| 3231 | */ |
| 3232 | if (prev == parent) |
| 3233 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3234 | |
| 3235 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3236 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3237 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3238 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3239 | (parent->content != NULL) && |
| 3240 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3241 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3242 | xmlFreeNode(cur); |
| 3243 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3244 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3245 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3246 | if (parent->type != XML_ELEMENT_NODE) |
| 3247 | return(NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3248 | if (parent->properties == NULL) { |
| 3249 | parent->properties = (xmlAttrPtr) cur; |
| 3250 | } else { |
| 3251 | /* check if an attribute with the same name exists */ |
| 3252 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3253 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3254 | if (cur->ns == NULL) |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3255 | lastattr = xmlHasNsProp(parent, cur->name, NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3256 | else |
| 3257 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3258 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3259 | /* different instance, destroy it (attributes must be unique) */ |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3260 | xmlUnlinkNode((xmlNodePtr) lastattr); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3261 | xmlFreeProp(lastattr); |
| 3262 | } |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3263 | if (lastattr == (xmlAttrPtr) cur) |
| 3264 | return(cur); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3265 | /* find the end */ |
| 3266 | lastattr = parent->properties; |
| 3267 | while (lastattr->next != NULL) { |
| 3268 | lastattr = lastattr->next; |
| 3269 | } |
| 3270 | lastattr->next = (xmlAttrPtr) cur; |
| 3271 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3272 | } |
| 3273 | } else { |
| 3274 | if (parent->children == NULL) { |
| 3275 | parent->children = cur; |
| 3276 | parent->last = cur; |
| 3277 | } else { |
| 3278 | prev = parent->last; |
| 3279 | prev->next = cur; |
| 3280 | cur->prev = prev; |
| 3281 | parent->last = cur; |
| 3282 | } |
| 3283 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3284 | return(cur); |
| 3285 | } |
| 3286 | |
| 3287 | /** |
| 3288 | * xmlGetLastChild: |
| 3289 | * @parent: the parent node |
| 3290 | * |
| 3291 | * Search the last child of a node. |
| 3292 | * Returns the last child or NULL if none. |
| 3293 | */ |
| 3294 | xmlNodePtr |
| 3295 | xmlGetLastChild(xmlNodePtr parent) { |
| 3296 | if (parent == NULL) { |
| 3297 | #ifdef DEBUG_TREE |
| 3298 | xmlGenericError(xmlGenericErrorContext, |
| 3299 | "xmlGetLastChild : parent == NULL\n"); |
| 3300 | #endif |
| 3301 | return(NULL); |
| 3302 | } |
| 3303 | return(parent->last); |
| 3304 | } |
| 3305 | |
| 3306 | /** |
| 3307 | * xmlFreeNodeList: |
| 3308 | * @cur: the first node in the list |
| 3309 | * |
| 3310 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3311 | * the children are freed too. |
| 3312 | */ |
| 3313 | void |
| 3314 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3315 | xmlNodePtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3316 | xmlDictPtr dict = NULL; |
| 3317 | |
| 3318 | if (cur == NULL) return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3319 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3320 | xmlFreeNsList((xmlNsPtr) cur); |
| 3321 | return; |
| 3322 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3323 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3324 | #ifdef LIBXML_DOCB_ENABLED |
| 3325 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3326 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3327 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3328 | xmlFreeDoc((xmlDocPtr) cur); |
| 3329 | return; |
| 3330 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3331 | if (cur->doc != NULL) dict = cur->doc->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3332 | while (cur != NULL) { |
| 3333 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3334 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3335 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3336 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3337 | xmlDeregisterNodeDefaultValue(cur); |
| 3338 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3339 | if ((cur->children != NULL) && |
| 3340 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3341 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3342 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3343 | (cur->type == XML_XINCLUDE_START) || |
| 3344 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3345 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3346 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3347 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3348 | (cur->type != XML_XINCLUDE_START) && |
| 3349 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3350 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3351 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3352 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3353 | } |
| 3354 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3355 | (cur->type == XML_XINCLUDE_START) || |
| 3356 | (cur->type == XML_XINCLUDE_END)) && |
| 3357 | (cur->nsDef != NULL)) |
| 3358 | xmlFreeNsList(cur->nsDef); |
| 3359 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3360 | /* |
| 3361 | * When a node is a text node or a comment, it uses a global static |
| 3362 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3363 | * Otherwise the node name might come from the document's |
| 3364 | * dictionnary |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3365 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3366 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3367 | (cur->type != XML_TEXT_NODE) && |
| 3368 | (cur->type != XML_COMMENT_NODE)) |
| 3369 | DICT_FREE(cur->name) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3370 | xmlFree(cur); |
| 3371 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3372 | cur = next; |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | /** |
| 3377 | * xmlFreeNode: |
| 3378 | * @cur: the node |
| 3379 | * |
| 3380 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3381 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3382 | */ |
| 3383 | void |
| 3384 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3385 | xmlDictPtr dict = NULL; |
| 3386 | |
| 3387 | if (cur == NULL) return; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3388 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3389 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3390 | if (cur->type == XML_DTD_NODE) { |
| 3391 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3392 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3393 | } |
| 3394 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3395 | xmlFreeNs((xmlNsPtr) cur); |
| 3396 | return; |
| 3397 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3398 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3399 | xmlFreeProp((xmlAttrPtr) cur); |
| 3400 | return; |
| 3401 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3402 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3403 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3404 | xmlDeregisterNodeDefaultValue(cur); |
| 3405 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3406 | if (cur->doc != NULL) dict = cur->doc->dict; |
| 3407 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3408 | if ((cur->children != NULL) && |
| 3409 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3410 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3411 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3412 | (cur->type == XML_XINCLUDE_START) || |
| 3413 | (cur->type == XML_XINCLUDE_END)) && |
| 3414 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3415 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3416 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3417 | (cur->content != NULL) && |
| 3418 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3419 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3420 | (cur->type != XML_XINCLUDE_START) && |
| 3421 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3422 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3423 | } |
| 3424 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3425 | /* |
| 3426 | * When a node is a text node or a comment, it uses a global static |
| 3427 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3428 | * Otherwise the node name might come from the document's dictionnary |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3429 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3430 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3431 | (cur->type != XML_TEXT_NODE) && |
| 3432 | (cur->type != XML_COMMENT_NODE)) |
| 3433 | DICT_FREE(cur->name) |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3434 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3435 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3436 | (cur->type == XML_XINCLUDE_START) || |
| 3437 | (cur->type == XML_XINCLUDE_END)) && |
| 3438 | (cur->nsDef != NULL)) |
| 3439 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3440 | xmlFree(cur); |
| 3441 | } |
| 3442 | |
| 3443 | /** |
| 3444 | * xmlUnlinkNode: |
| 3445 | * @cur: the node |
| 3446 | * |
| 3447 | * Unlink a node from it's current context, the node is not freed |
| 3448 | */ |
| 3449 | void |
| 3450 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3451 | if (cur == NULL) { |
| 3452 | #ifdef DEBUG_TREE |
| 3453 | xmlGenericError(xmlGenericErrorContext, |
| 3454 | "xmlUnlinkNode : node == NULL\n"); |
| 3455 | #endif |
| 3456 | return; |
| 3457 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3458 | if (cur->type == XML_DTD_NODE) { |
| 3459 | xmlDocPtr doc; |
| 3460 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3461 | if (doc != NULL) { |
| 3462 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3463 | doc->intSubset = NULL; |
| 3464 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3465 | doc->extSubset = NULL; |
| 3466 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3467 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3468 | if (cur->parent != NULL) { |
| 3469 | xmlNodePtr parent; |
| 3470 | parent = cur->parent; |
| 3471 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3472 | if (parent->properties == (xmlAttrPtr) cur) |
| 3473 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3474 | } else { |
| 3475 | if (parent->children == cur) |
| 3476 | parent->children = cur->next; |
| 3477 | if (parent->last == cur) |
| 3478 | parent->last = cur->prev; |
| 3479 | } |
| 3480 | cur->parent = NULL; |
| 3481 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3482 | if (cur->next != NULL) |
| 3483 | cur->next->prev = cur->prev; |
| 3484 | if (cur->prev != NULL) |
| 3485 | cur->prev->next = cur->next; |
| 3486 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3487 | } |
| 3488 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 3489 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3490 | /** |
| 3491 | * xmlReplaceNode: |
| 3492 | * @old: the old node |
| 3493 | * @cur: the node |
| 3494 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 3495 | * Unlink the old node from its current context, prune the new one |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3496 | * 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] | 3497 | * first unlinked from its existing context. |
| 3498 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3499 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3500 | */ |
| 3501 | xmlNodePtr |
| 3502 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 3503 | if (old == cur) return(NULL); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3504 | if ((old == NULL) || (old->parent == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3505 | #ifdef DEBUG_TREE |
| 3506 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3507 | "xmlReplaceNode : old == NULL or without parent\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3508 | #endif |
| 3509 | return(NULL); |
| 3510 | } |
| 3511 | if (cur == NULL) { |
| 3512 | xmlUnlinkNode(old); |
| 3513 | return(old); |
| 3514 | } |
| 3515 | if (cur == old) { |
| 3516 | return(old); |
| 3517 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3518 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3519 | #ifdef DEBUG_TREE |
| 3520 | xmlGenericError(xmlGenericErrorContext, |
| 3521 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3522 | #endif |
| 3523 | return(old); |
| 3524 | } |
| 3525 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3526 | #ifdef DEBUG_TREE |
| 3527 | xmlGenericError(xmlGenericErrorContext, |
| 3528 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3529 | #endif |
| 3530 | return(old); |
| 3531 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3532 | xmlUnlinkNode(cur); |
Daniel Veillard | 64d7d12 | 2005-05-11 18:03:42 +0000 | [diff] [blame] | 3533 | xmlSetTreeDoc(cur, old->doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3534 | cur->parent = old->parent; |
| 3535 | cur->next = old->next; |
| 3536 | if (cur->next != NULL) |
| 3537 | cur->next->prev = cur; |
| 3538 | cur->prev = old->prev; |
| 3539 | if (cur->prev != NULL) |
| 3540 | cur->prev->next = cur; |
| 3541 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3542 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3543 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3544 | cur->parent->properties = ((xmlAttrPtr) cur); |
| 3545 | } else { |
| 3546 | if (cur->parent->children == old) |
| 3547 | cur->parent->children = cur; |
| 3548 | if (cur->parent->last == old) |
| 3549 | cur->parent->last = cur; |
| 3550 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3551 | } |
| 3552 | old->next = old->prev = NULL; |
| 3553 | old->parent = NULL; |
| 3554 | return(old); |
| 3555 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3556 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3557 | |
| 3558 | /************************************************************************ |
| 3559 | * * |
| 3560 | * Copy operations * |
| 3561 | * * |
| 3562 | ************************************************************************/ |
| 3563 | |
| 3564 | /** |
| 3565 | * xmlCopyNamespace: |
| 3566 | * @cur: the namespace |
| 3567 | * |
| 3568 | * Do a copy of the namespace. |
| 3569 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3570 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3571 | */ |
| 3572 | xmlNsPtr |
| 3573 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3574 | xmlNsPtr ret; |
| 3575 | |
| 3576 | if (cur == NULL) return(NULL); |
| 3577 | switch (cur->type) { |
| 3578 | case XML_LOCAL_NAMESPACE: |
| 3579 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3580 | break; |
| 3581 | default: |
| 3582 | #ifdef DEBUG_TREE |
| 3583 | xmlGenericError(xmlGenericErrorContext, |
| 3584 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3585 | #endif |
| 3586 | return(NULL); |
| 3587 | } |
| 3588 | return(ret); |
| 3589 | } |
| 3590 | |
| 3591 | /** |
| 3592 | * xmlCopyNamespaceList: |
| 3593 | * @cur: the first namespace |
| 3594 | * |
| 3595 | * Do a copy of an namespace list. |
| 3596 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3597 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3598 | */ |
| 3599 | xmlNsPtr |
| 3600 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3601 | xmlNsPtr ret = NULL; |
| 3602 | xmlNsPtr p = NULL,q; |
| 3603 | |
| 3604 | while (cur != NULL) { |
| 3605 | q = xmlCopyNamespace(cur); |
| 3606 | if (p == NULL) { |
| 3607 | ret = p = q; |
| 3608 | } else { |
| 3609 | p->next = q; |
| 3610 | p = q; |
| 3611 | } |
| 3612 | cur = cur->next; |
| 3613 | } |
| 3614 | return(ret); |
| 3615 | } |
| 3616 | |
| 3617 | static xmlNodePtr |
| 3618 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3619 | |
| 3620 | static xmlAttrPtr |
| 3621 | xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3622 | xmlAttrPtr ret; |
| 3623 | |
| 3624 | if (cur == NULL) return(NULL); |
| 3625 | if (target != NULL) |
| 3626 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3627 | else if (doc != NULL) |
| 3628 | ret = xmlNewDocProp(doc, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3629 | else if (cur->parent != NULL) |
| 3630 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 3631 | else if (cur->children != NULL) |
| 3632 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 3633 | else |
| 3634 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 3635 | if (ret == NULL) return(NULL); |
| 3636 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3637 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3638 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3639 | xmlNsPtr ns; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3640 | |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3641 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3642 | if (ns == NULL) { |
| 3643 | /* |
| 3644 | * Humm, we are copying an element whose namespace is defined |
| 3645 | * out of the new tree scope. Search it in the original tree |
| 3646 | * and add it at the top of the new tree |
| 3647 | */ |
| 3648 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 3649 | if (ns != NULL) { |
| 3650 | xmlNodePtr root = target; |
| 3651 | xmlNodePtr pred = NULL; |
| 3652 | |
| 3653 | while (root->parent != NULL) { |
| 3654 | pred = root; |
| 3655 | root = root->parent; |
| 3656 | } |
| 3657 | if (root == (xmlNodePtr) target->doc) { |
| 3658 | /* correct possibly cycling above the document elt */ |
| 3659 | root = pred; |
| 3660 | } |
| 3661 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 3662 | } |
| 3663 | } else { |
| 3664 | /* |
| 3665 | * we have to find something appropriate here since |
| 3666 | * we cant be sure, that the namespce we found is identified |
| 3667 | * by the prefix |
| 3668 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3669 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3670 | /* this is the nice case */ |
| 3671 | ret->ns = ns; |
| 3672 | } else { |
| 3673 | /* |
| 3674 | * we are in trouble: we need a new reconcilied namespace. |
| 3675 | * This is expensive |
| 3676 | */ |
| 3677 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 3678 | } |
| 3679 | } |
| 3680 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3681 | } else |
| 3682 | ret->ns = NULL; |
| 3683 | |
| 3684 | if (cur->children != NULL) { |
| 3685 | xmlNodePtr tmp; |
| 3686 | |
| 3687 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 3688 | ret->last = NULL; |
| 3689 | tmp = ret->children; |
| 3690 | while (tmp != NULL) { |
| 3691 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 3692 | if (tmp->next == NULL) |
| 3693 | ret->last = tmp; |
| 3694 | tmp = tmp->next; |
| 3695 | } |
| 3696 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3697 | /* |
| 3698 | * Try to handle IDs |
| 3699 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 3700 | if ((target!= NULL) && (cur!= NULL) && |
| 3701 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3702 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 3703 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 3704 | xmlChar *id; |
| 3705 | |
| 3706 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 3707 | if (id != NULL) { |
| 3708 | xmlAddID(NULL, target->doc, id, ret); |
| 3709 | xmlFree(id); |
| 3710 | } |
| 3711 | } |
| 3712 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3713 | return(ret); |
| 3714 | } |
| 3715 | |
| 3716 | /** |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3717 | * xmlCopyProp: |
| 3718 | * @target: the element where the attribute will be grafted |
| 3719 | * @cur: the attribute |
| 3720 | * |
| 3721 | * Do a copy of the attribute. |
| 3722 | * |
| 3723 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
| 3724 | */ |
| 3725 | xmlAttrPtr |
| 3726 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 3727 | return xmlCopyPropInternal(NULL, target, cur); |
| 3728 | } |
| 3729 | |
| 3730 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3731 | * xmlCopyPropList: |
| 3732 | * @target: the element where the attributes will be grafted |
| 3733 | * @cur: the first attribute |
| 3734 | * |
| 3735 | * Do a copy of an attribute list. |
| 3736 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3737 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3738 | */ |
| 3739 | xmlAttrPtr |
| 3740 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 3741 | xmlAttrPtr ret = NULL; |
| 3742 | xmlAttrPtr p = NULL,q; |
| 3743 | |
| 3744 | while (cur != NULL) { |
| 3745 | q = xmlCopyProp(target, cur); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 3746 | if (q == NULL) |
| 3747 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3748 | if (p == NULL) { |
| 3749 | ret = p = q; |
| 3750 | } else { |
| 3751 | p->next = q; |
| 3752 | q->prev = p; |
| 3753 | p = q; |
| 3754 | } |
| 3755 | cur = cur->next; |
| 3756 | } |
| 3757 | return(ret); |
| 3758 | } |
| 3759 | |
| 3760 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3761 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3762 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3763 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3764 | * tricky reason: namespaces. Doing a direct copy of a node |
| 3765 | * say RPM:Copyright without changing the namespace pointer to |
| 3766 | * something else can produce stale links. One way to do it is |
| 3767 | * to keep a reference counter but this doesn't work as soon |
| 3768 | * as one move the element or the subtree out of the scope of |
| 3769 | * the existing namespace. The actual solution seems to add |
| 3770 | * a copy of the namespace at the top of the copied tree if |
| 3771 | * not available in the subtree. |
| 3772 | * Hence two functions, the public front-end call the inner ones |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3773 | * The argument "recursive" normally indicates a recursive copy |
| 3774 | * of the node with values 0 (no) and 1 (yes). For XInclude, |
| 3775 | * however, we allow a value of 2 to indicate copy properties and |
| 3776 | * namespace info, but don't recurse on children. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3777 | */ |
| 3778 | |
| 3779 | static xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3780 | xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3781 | int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3782 | xmlNodePtr ret; |
| 3783 | |
| 3784 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3785 | switch (node->type) { |
| 3786 | case XML_TEXT_NODE: |
| 3787 | case XML_CDATA_SECTION_NODE: |
| 3788 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 3789 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3790 | case XML_ENTITY_REF_NODE: |
| 3791 | case XML_ENTITY_NODE: |
| 3792 | case XML_PI_NODE: |
| 3793 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3794 | case XML_XINCLUDE_START: |
| 3795 | case XML_XINCLUDE_END: |
| 3796 | break; |
| 3797 | case XML_ATTRIBUTE_NODE: |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3798 | return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node)); |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3799 | case XML_NAMESPACE_DECL: |
| 3800 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
| 3801 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3802 | case XML_DOCUMENT_NODE: |
| 3803 | case XML_HTML_DOCUMENT_NODE: |
| 3804 | #ifdef LIBXML_DOCB_ENABLED |
| 3805 | case XML_DOCB_DOCUMENT_NODE: |
| 3806 | #endif |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3807 | #ifdef LIBXML_TREE_ENABLED |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3808 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended)); |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3809 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3810 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3811 | case XML_NOTATION_NODE: |
| 3812 | case XML_DTD_NODE: |
| 3813 | case XML_ELEMENT_DECL: |
| 3814 | case XML_ATTRIBUTE_DECL: |
| 3815 | case XML_ENTITY_DECL: |
| 3816 | return(NULL); |
| 3817 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3818 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3819 | /* |
| 3820 | * Allocate a new node and fill the fields. |
| 3821 | */ |
| 3822 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 3823 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 3824 | xmlTreeErrMemory("copying node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3825 | return(NULL); |
| 3826 | } |
| 3827 | memset(ret, 0, sizeof(xmlNode)); |
| 3828 | ret->type = node->type; |
| 3829 | |
| 3830 | ret->doc = doc; |
| 3831 | ret->parent = parent; |
| 3832 | if (node->name == xmlStringText) |
| 3833 | ret->name = xmlStringText; |
| 3834 | else if (node->name == xmlStringTextNoenc) |
| 3835 | ret->name = xmlStringTextNoenc; |
| 3836 | else if (node->name == xmlStringComment) |
| 3837 | ret->name = xmlStringComment; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 3838 | else if (node->name != NULL) { |
| 3839 | if ((doc != NULL) && (doc->dict != NULL)) |
| 3840 | ret->name = xmlDictLookup(doc->dict, node->name, -1); |
| 3841 | else |
| 3842 | ret->name = xmlStrdup(node->name); |
| 3843 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3844 | if ((node->type != XML_ELEMENT_NODE) && |
| 3845 | (node->content != NULL) && |
| 3846 | (node->type != XML_ENTITY_REF_NODE) && |
| 3847 | (node->type != XML_XINCLUDE_END) && |
| 3848 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3849 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3850 | }else{ |
| 3851 | if (node->type == XML_ELEMENT_NODE) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 3852 | ret->line = node->line; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3853 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3854 | if (parent != NULL) { |
| 3855 | xmlNodePtr tmp; |
| 3856 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3857 | /* |
| 3858 | * this is a tricky part for the node register thing: |
| 3859 | * in case ret does get coalesced in xmlAddChild |
| 3860 | * the deregister-node callback is called; so we register ret now already |
| 3861 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3862 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3863 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 3864 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3865 | tmp = xmlAddChild(parent, ret); |
| 3866 | /* node could have coalesced */ |
| 3867 | if (tmp != ret) |
| 3868 | return(tmp); |
| 3869 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3870 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3871 | if (!extended) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3872 | goto out; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3873 | if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3874 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 3875 | |
| 3876 | if (node->ns != NULL) { |
| 3877 | xmlNsPtr ns; |
| 3878 | |
| 3879 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 3880 | if (ns == NULL) { |
| 3881 | /* |
| 3882 | * Humm, we are copying an element whose namespace is defined |
| 3883 | * out of the new tree scope. Search it in the original tree |
| 3884 | * and add it at the top of the new tree |
| 3885 | */ |
| 3886 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 3887 | if (ns != NULL) { |
| 3888 | xmlNodePtr root = ret; |
| 3889 | |
| 3890 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 3891 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3892 | } |
| 3893 | } else { |
| 3894 | /* |
| 3895 | * reference the existing namespace definition in our own tree. |
| 3896 | */ |
| 3897 | ret->ns = ns; |
| 3898 | } |
| 3899 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3900 | if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3901 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3902 | if (node->type == XML_ENTITY_REF_NODE) { |
| 3903 | if ((doc == NULL) || (node->doc != doc)) { |
| 3904 | /* |
| 3905 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3906 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3907 | * we cannot keep the reference. Try to find it in the |
| 3908 | * target document. |
| 3909 | */ |
| 3910 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 3911 | } else { |
| 3912 | ret->children = node->children; |
| 3913 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3914 | ret->last = ret->children; |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3915 | } else if ((node->children != NULL) && (extended != 2)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3916 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3917 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 3918 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3919 | |
| 3920 | out: |
| 3921 | /* if parent != NULL we already registered the node above */ |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 3922 | if ((parent == NULL) && |
| 3923 | ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3924 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3925 | return(ret); |
| 3926 | } |
| 3927 | |
| 3928 | static xmlNodePtr |
| 3929 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 3930 | xmlNodePtr ret = NULL; |
| 3931 | xmlNodePtr p = NULL,q; |
| 3932 | |
| 3933 | while (node != NULL) { |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3934 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3935 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 3936 | if (doc == NULL) { |
| 3937 | node = node->next; |
| 3938 | continue; |
| 3939 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3940 | if (doc->intSubset == NULL) { |
| 3941 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
| 3942 | q->doc = doc; |
| 3943 | q->parent = parent; |
| 3944 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3945 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3946 | } else { |
| 3947 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3948 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3949 | } |
| 3950 | } else |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3951 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3952 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3953 | if (ret == NULL) { |
| 3954 | q->prev = NULL; |
| 3955 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3956 | } else if (p != q) { |
| 3957 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3958 | p->next = q; |
| 3959 | q->prev = p; |
| 3960 | p = q; |
| 3961 | } |
| 3962 | node = node->next; |
| 3963 | } |
| 3964 | return(ret); |
| 3965 | } |
| 3966 | |
| 3967 | /** |
| 3968 | * xmlCopyNode: |
| 3969 | * @node: the node |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3970 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 3971 | * when applicable) |
| 3972 | * if 2 copy properties and namespaces (when applicable) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3973 | * |
| 3974 | * Do a copy of the node. |
| 3975 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3976 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3977 | */ |
| 3978 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3979 | xmlCopyNode(const xmlNodePtr node, int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3980 | xmlNodePtr ret; |
| 3981 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3982 | ret = xmlStaticCopyNode(node, NULL, NULL, extended); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3983 | return(ret); |
| 3984 | } |
| 3985 | |
| 3986 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3987 | * xmlDocCopyNode: |
| 3988 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3989 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3990 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 3991 | * when applicable) |
| 3992 | * if 2 copy properties and namespaces (when applicable) |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3993 | * |
| 3994 | * Do a copy of the node to a given document. |
| 3995 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3996 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3997 | */ |
| 3998 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3999 | xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4000 | xmlNodePtr ret; |
| 4001 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4002 | ret = xmlStaticCopyNode(node, doc, NULL, extended); |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4003 | return(ret); |
| 4004 | } |
| 4005 | |
| 4006 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4007 | * xmlDocCopyNodeList: |
| 4008 | * @doc: the target document |
| 4009 | * @node: the first node in the list. |
| 4010 | * |
| 4011 | * Do a recursive copy of the node list. |
| 4012 | * |
| 4013 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
| 4014 | */ |
| 4015 | xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) { |
| 4016 | xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL); |
| 4017 | return(ret); |
| 4018 | } |
| 4019 | |
| 4020 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4021 | * xmlCopyNodeList: |
| 4022 | * @node: the first node in the list. |
| 4023 | * |
| 4024 | * Do a recursive copy of the node list. |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4025 | * Use xmlDocCopyNodeList() if possible to ensure string interning. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4026 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4027 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4028 | */ |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 4029 | xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4030 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 4031 | return(ret); |
| 4032 | } |
| 4033 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4034 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4035 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4036 | * xmlCopyDtd: |
| 4037 | * @dtd: the dtd |
| 4038 | * |
| 4039 | * Do a copy of the dtd. |
| 4040 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4041 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4042 | */ |
| 4043 | xmlDtdPtr |
| 4044 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 4045 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4046 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4047 | |
| 4048 | if (dtd == NULL) return(NULL); |
| 4049 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 4050 | if (ret == NULL) return(NULL); |
| 4051 | if (dtd->entities != NULL) |
| 4052 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 4053 | (xmlEntitiesTablePtr) dtd->entities); |
| 4054 | if (dtd->notations != NULL) |
| 4055 | ret->notations = (void *) xmlCopyNotationTable( |
| 4056 | (xmlNotationTablePtr) dtd->notations); |
| 4057 | if (dtd->elements != NULL) |
| 4058 | ret->elements = (void *) xmlCopyElementTable( |
| 4059 | (xmlElementTablePtr) dtd->elements); |
| 4060 | if (dtd->attributes != NULL) |
| 4061 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 4062 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4063 | if (dtd->pentities != NULL) |
| 4064 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 4065 | (xmlEntitiesTablePtr) dtd->pentities); |
| 4066 | |
| 4067 | cur = dtd->children; |
| 4068 | while (cur != NULL) { |
| 4069 | q = NULL; |
| 4070 | |
| 4071 | if (cur->type == XML_ENTITY_DECL) { |
| 4072 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 4073 | switch (tmp->etype) { |
| 4074 | case XML_INTERNAL_GENERAL_ENTITY: |
| 4075 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 4076 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 4077 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 4078 | break; |
| 4079 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 4080 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 4081 | q = (xmlNodePtr) |
| 4082 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 4083 | break; |
| 4084 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 4085 | break; |
| 4086 | } |
| 4087 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 4088 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 4089 | q = (xmlNodePtr) |
| 4090 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 4091 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4092 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
| 4093 | q = (xmlNodePtr) |
| 4094 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 4095 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4096 | q = xmlCopyNode(cur, 0); |
| 4097 | } |
| 4098 | |
| 4099 | if (q == NULL) { |
| 4100 | cur = cur->next; |
| 4101 | continue; |
| 4102 | } |
| 4103 | |
| 4104 | if (p == NULL) |
| 4105 | ret->children = q; |
| 4106 | else |
| 4107 | p->next = q; |
| 4108 | |
| 4109 | q->prev = p; |
| 4110 | q->parent = (xmlNodePtr) ret; |
| 4111 | q->next = NULL; |
| 4112 | ret->last = q; |
| 4113 | p = q; |
| 4114 | cur = cur->next; |
| 4115 | } |
| 4116 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4117 | return(ret); |
| 4118 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4119 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4120 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4121 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4122 | /** |
| 4123 | * xmlCopyDoc: |
| 4124 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4125 | * @recursive: if not zero do a recursive copy. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4126 | * |
| 4127 | * 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] | 4128 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4129 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4130 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4131 | */ |
| 4132 | xmlDocPtr |
| 4133 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 4134 | xmlDocPtr ret; |
| 4135 | |
| 4136 | if (doc == NULL) return(NULL); |
| 4137 | ret = xmlNewDoc(doc->version); |
| 4138 | if (ret == NULL) return(NULL); |
| 4139 | if (doc->name != NULL) |
| 4140 | ret->name = xmlMemStrdup(doc->name); |
| 4141 | if (doc->encoding != NULL) |
| 4142 | ret->encoding = xmlStrdup(doc->encoding); |
Daniel Veillard | f59507d | 2005-01-27 17:26:49 +0000 | [diff] [blame] | 4143 | if (doc->URL != NULL) |
| 4144 | ret->URL = xmlStrdup(doc->URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4145 | ret->charset = doc->charset; |
| 4146 | ret->compression = doc->compression; |
| 4147 | ret->standalone = doc->standalone; |
| 4148 | if (!recursive) return(ret); |
| 4149 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4150 | ret->last = NULL; |
| 4151 | ret->children = NULL; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4152 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4153 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4154 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4155 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4156 | ret->intSubset->parent = ret; |
| 4157 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4158 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4159 | if (doc->oldNs != NULL) |
| 4160 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 4161 | if (doc->children != NULL) { |
| 4162 | xmlNodePtr tmp; |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4163 | |
| 4164 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 4165 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4166 | ret->last = NULL; |
| 4167 | tmp = ret->children; |
| 4168 | while (tmp != NULL) { |
| 4169 | if (tmp->next == NULL) |
| 4170 | ret->last = tmp; |
| 4171 | tmp = tmp->next; |
| 4172 | } |
| 4173 | } |
| 4174 | return(ret); |
| 4175 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4176 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4177 | |
| 4178 | /************************************************************************ |
| 4179 | * * |
| 4180 | * Content access functions * |
| 4181 | * * |
| 4182 | ************************************************************************/ |
| 4183 | |
| 4184 | /** |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4185 | * xmlGetLineNo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4186 | * @node: valid node |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4187 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 4188 | * Get line number of @node. This requires activation of this option |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4189 | * before invoking the parser by calling xmlLineNumbersDefault(1) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4190 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4191 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4192 | */ |
| 4193 | long |
| 4194 | xmlGetLineNo(xmlNodePtr node) |
| 4195 | { |
| 4196 | long result = -1; |
| 4197 | |
| 4198 | if (!node) |
| 4199 | return result; |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4200 | if ((node->type == XML_ELEMENT_NODE) || |
| 4201 | (node->type == XML_TEXT_NODE) || |
| 4202 | (node->type == XML_COMMENT_NODE) || |
| 4203 | (node->type == XML_PI_NODE)) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 4204 | result = (long) node->line; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4205 | else if ((node->prev != NULL) && |
| 4206 | ((node->prev->type == XML_ELEMENT_NODE) || |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4207 | (node->prev->type == XML_TEXT_NODE) || |
| 4208 | (node->prev->type == XML_COMMENT_NODE) || |
| 4209 | (node->prev->type == XML_PI_NODE))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4210 | result = xmlGetLineNo(node->prev); |
| 4211 | else if ((node->parent != NULL) && |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4212 | (node->parent->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4213 | result = xmlGetLineNo(node->parent); |
| 4214 | |
| 4215 | return result; |
| 4216 | } |
| 4217 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4218 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4219 | /** |
| 4220 | * xmlGetNodePath: |
| 4221 | * @node: a node |
| 4222 | * |
| 4223 | * Build a structure based Path for the given node |
| 4224 | * |
| 4225 | * Returns the new path or NULL in case of error. The caller must free |
| 4226 | * the returned string |
| 4227 | */ |
| 4228 | xmlChar * |
| 4229 | xmlGetNodePath(xmlNodePtr node) |
| 4230 | { |
| 4231 | xmlNodePtr cur, tmp, next; |
| 4232 | xmlChar *buffer = NULL, *temp; |
| 4233 | size_t buf_len; |
| 4234 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4235 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4236 | const char *name; |
| 4237 | char nametemp[100]; |
| 4238 | int occur = 0; |
| 4239 | |
| 4240 | if (node == NULL) |
| 4241 | return (NULL); |
| 4242 | |
| 4243 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4244 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4245 | if (buffer == NULL) { |
| 4246 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4247 | return (NULL); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4248 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4249 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4250 | if (buf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4251 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4252 | xmlFree(buffer); |
| 4253 | return (NULL); |
| 4254 | } |
| 4255 | |
| 4256 | buffer[0] = 0; |
| 4257 | cur = node; |
| 4258 | do { |
| 4259 | name = ""; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4260 | sep = "?"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4261 | occur = 0; |
| 4262 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4263 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4264 | if (buffer[0] == '/') |
| 4265 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4266 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4267 | next = NULL; |
| 4268 | } else if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4269 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4270 | name = (const char *) cur->name; |
| 4271 | if (cur->ns) { |
William M. Brack | 84d83e3 | 2003-12-23 03:45:17 +0000 | [diff] [blame] | 4272 | if (cur->ns->prefix != NULL) |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4273 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
| 4274 | (char *)cur->ns->prefix, (char *)cur->name); |
William M. Brack | 84d83e3 | 2003-12-23 03:45:17 +0000 | [diff] [blame] | 4275 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4276 | snprintf(nametemp, sizeof(nametemp) - 1, "%s", |
| 4277 | (char *)cur->name); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4278 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4279 | name = nametemp; |
| 4280 | } |
| 4281 | next = cur->parent; |
| 4282 | |
| 4283 | /* |
| 4284 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4285 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4286 | */ |
| 4287 | tmp = cur->prev; |
| 4288 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4289 | if ((tmp->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 0996a16 | 2005-02-05 14:00:10 +0000 | [diff] [blame] | 4290 | (xmlStrEqual(cur->name, tmp->name)) && |
| 4291 | ((tmp->ns == cur->ns) || |
| 4292 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
| 4293 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4294 | occur++; |
| 4295 | tmp = tmp->prev; |
| 4296 | } |
| 4297 | if (occur == 0) { |
| 4298 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4299 | while (tmp != NULL && occur == 0) { |
| 4300 | if ((tmp->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 0996a16 | 2005-02-05 14:00:10 +0000 | [diff] [blame] | 4301 | (xmlStrEqual(cur->name, tmp->name)) && |
| 4302 | ((tmp->ns == cur->ns) || |
| 4303 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
| 4304 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4305 | occur++; |
| 4306 | tmp = tmp->next; |
| 4307 | } |
| 4308 | if (occur != 0) |
| 4309 | occur = 1; |
| 4310 | } else |
| 4311 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4312 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4313 | sep = "/"; |
| 4314 | name = "comment()"; |
| 4315 | next = cur->parent; |
| 4316 | |
| 4317 | /* |
| 4318 | * Thumbler index computation |
| 4319 | */ |
| 4320 | tmp = cur->prev; |
| 4321 | while (tmp != NULL) { |
| 4322 | if (tmp->type == XML_COMMENT_NODE) |
| 4323 | occur++; |
| 4324 | tmp = tmp->prev; |
| 4325 | } |
| 4326 | if (occur == 0) { |
| 4327 | tmp = cur->next; |
| 4328 | while (tmp != NULL && occur == 0) { |
| 4329 | if (tmp->type == XML_COMMENT_NODE) |
| 4330 | occur++; |
| 4331 | tmp = tmp->next; |
| 4332 | } |
| 4333 | if (occur != 0) |
| 4334 | occur = 1; |
| 4335 | } else |
| 4336 | occur++; |
| 4337 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4338 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4339 | sep = "/"; |
| 4340 | name = "text()"; |
| 4341 | next = cur->parent; |
| 4342 | |
| 4343 | /* |
| 4344 | * Thumbler index computation |
| 4345 | */ |
| 4346 | tmp = cur->prev; |
| 4347 | while (tmp != NULL) { |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4348 | if ((tmp->type == XML_TEXT_NODE) || |
| 4349 | (tmp->type == XML_CDATA_SECTION_NODE)) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4350 | occur++; |
| 4351 | tmp = tmp->prev; |
| 4352 | } |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4353 | /* |
| 4354 | * Evaluate if this is the only text- or CDATA-section-node; |
| 4355 | * if yes, then we'll get "text()", otherwise "text()[1]". |
| 4356 | */ |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4357 | if (occur == 0) { |
| 4358 | tmp = cur->next; |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4359 | while (tmp != NULL) { |
| 4360 | if ((tmp->type == XML_TEXT_NODE) || |
| 4361 | (tmp->type == XML_CDATA_SECTION_NODE)) |
| 4362 | { |
| 4363 | occur = 1; |
| 4364 | break; |
| 4365 | } |
| 4366 | tmp = tmp->next; |
| 4367 | } |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4368 | } else |
| 4369 | occur++; |
| 4370 | } else if (cur->type == XML_PI_NODE) { |
| 4371 | sep = "/"; |
| 4372 | snprintf(nametemp, sizeof(nametemp) - 1, |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4373 | "processing-instruction('%s')", (char *)cur->name); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4374 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4375 | name = nametemp; |
| 4376 | |
| 4377 | next = cur->parent; |
| 4378 | |
| 4379 | /* |
| 4380 | * Thumbler index computation |
| 4381 | */ |
| 4382 | tmp = cur->prev; |
| 4383 | while (tmp != NULL) { |
| 4384 | if ((tmp->type == XML_PI_NODE) && |
| 4385 | (xmlStrEqual(cur->name, tmp->name))) |
| 4386 | occur++; |
| 4387 | tmp = tmp->prev; |
| 4388 | } |
| 4389 | if (occur == 0) { |
| 4390 | tmp = cur->next; |
| 4391 | while (tmp != NULL && occur == 0) { |
| 4392 | if ((tmp->type == XML_PI_NODE) && |
| 4393 | (xmlStrEqual(cur->name, tmp->name))) |
| 4394 | occur++; |
| 4395 | tmp = tmp->next; |
| 4396 | } |
| 4397 | if (occur != 0) |
| 4398 | occur = 1; |
| 4399 | } else |
| 4400 | occur++; |
| 4401 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4402 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4403 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4404 | name = (const char *) (((xmlAttrPtr) cur)->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4405 | if (cur->ns) { |
| 4406 | if (cur->ns->prefix != NULL) |
| 4407 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
| 4408 | (char *)cur->ns->prefix, (char *)cur->name); |
| 4409 | else |
| 4410 | snprintf(nametemp, sizeof(nametemp) - 1, "%s", |
| 4411 | (char *)cur->name); |
| 4412 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4413 | name = nametemp; |
| 4414 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4415 | next = ((xmlAttrPtr) cur)->parent; |
| 4416 | } else { |
| 4417 | next = cur->parent; |
| 4418 | } |
| 4419 | |
| 4420 | /* |
| 4421 | * Make sure there is enough room |
| 4422 | */ |
| 4423 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4424 | buf_len = |
| 4425 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4426 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4427 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4428 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4429 | xmlFree(buf); |
| 4430 | xmlFree(buffer); |
| 4431 | return (NULL); |
| 4432 | } |
| 4433 | buffer = temp; |
| 4434 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4435 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4436 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4437 | xmlFree(buf); |
| 4438 | xmlFree(buffer); |
| 4439 | return (NULL); |
| 4440 | } |
| 4441 | buf = temp; |
| 4442 | } |
| 4443 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4444 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4445 | sep, name, (char *) buffer); |
| 4446 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4447 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4448 | sep, name, occur, (char *) buffer); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4449 | snprintf((char *) buffer, buf_len, "%s", (char *)buf); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4450 | cur = next; |
| 4451 | } while (cur != NULL); |
| 4452 | xmlFree(buf); |
| 4453 | return (buffer); |
| 4454 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4455 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4456 | |
| 4457 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4458 | * xmlDocGetRootElement: |
| 4459 | * @doc: the document |
| 4460 | * |
| 4461 | * Get the root element of the document (doc->children is a list |
| 4462 | * containing possibly comments, PIs, etc ...). |
| 4463 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4464 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4465 | */ |
| 4466 | xmlNodePtr |
| 4467 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 4468 | xmlNodePtr ret; |
| 4469 | |
| 4470 | if (doc == NULL) return(NULL); |
| 4471 | ret = doc->children; |
| 4472 | while (ret != NULL) { |
| 4473 | if (ret->type == XML_ELEMENT_NODE) |
| 4474 | return(ret); |
| 4475 | ret = ret->next; |
| 4476 | } |
| 4477 | return(ret); |
| 4478 | } |
| 4479 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4480 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4481 | /** |
| 4482 | * xmlDocSetRootElement: |
| 4483 | * @doc: the document |
| 4484 | * @root: the new document root element |
| 4485 | * |
| 4486 | * Set the root element of the document (doc->children is a list |
| 4487 | * containing possibly comments, PIs, etc ...). |
| 4488 | * |
| 4489 | * Returns the old root element if any was found |
| 4490 | */ |
| 4491 | xmlNodePtr |
| 4492 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4493 | xmlNodePtr old = NULL; |
| 4494 | |
| 4495 | if (doc == NULL) return(NULL); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4496 | if (root == NULL) |
| 4497 | return(NULL); |
| 4498 | xmlUnlinkNode(root); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 4499 | xmlSetTreeDoc(root, doc); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4500 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4501 | old = doc->children; |
| 4502 | while (old != NULL) { |
| 4503 | if (old->type == XML_ELEMENT_NODE) |
| 4504 | break; |
| 4505 | old = old->next; |
| 4506 | } |
| 4507 | if (old == NULL) { |
| 4508 | if (doc->children == NULL) { |
| 4509 | doc->children = root; |
| 4510 | doc->last = root; |
| 4511 | } else { |
| 4512 | xmlAddSibling(doc->children, root); |
| 4513 | } |
| 4514 | } else { |
| 4515 | xmlReplaceNode(old, root); |
| 4516 | } |
| 4517 | return(old); |
| 4518 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4519 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4520 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4521 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4522 | /** |
| 4523 | * xmlNodeSetLang: |
| 4524 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4525 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4526 | * |
| 4527 | * Set the language of a node, i.e. the values of the xml:lang |
| 4528 | * attribute. |
| 4529 | */ |
| 4530 | void |
| 4531 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4532 | xmlNsPtr ns; |
| 4533 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4534 | if (cur == NULL) return; |
| 4535 | switch(cur->type) { |
| 4536 | case XML_TEXT_NODE: |
| 4537 | case XML_CDATA_SECTION_NODE: |
| 4538 | case XML_COMMENT_NODE: |
| 4539 | case XML_DOCUMENT_NODE: |
| 4540 | case XML_DOCUMENT_TYPE_NODE: |
| 4541 | case XML_DOCUMENT_FRAG_NODE: |
| 4542 | case XML_NOTATION_NODE: |
| 4543 | case XML_HTML_DOCUMENT_NODE: |
| 4544 | case XML_DTD_NODE: |
| 4545 | case XML_ELEMENT_DECL: |
| 4546 | case XML_ATTRIBUTE_DECL: |
| 4547 | case XML_ENTITY_DECL: |
| 4548 | case XML_PI_NODE: |
| 4549 | case XML_ENTITY_REF_NODE: |
| 4550 | case XML_ENTITY_NODE: |
| 4551 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4552 | #ifdef LIBXML_DOCB_ENABLED |
| 4553 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4554 | #endif |
| 4555 | case XML_XINCLUDE_START: |
| 4556 | case XML_XINCLUDE_END: |
| 4557 | return; |
| 4558 | case XML_ELEMENT_NODE: |
| 4559 | case XML_ATTRIBUTE_NODE: |
| 4560 | break; |
| 4561 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4562 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4563 | if (ns == NULL) |
| 4564 | return; |
| 4565 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4566 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4567 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4568 | |
| 4569 | /** |
| 4570 | * xmlNodeGetLang: |
| 4571 | * @cur: the node being checked |
| 4572 | * |
| 4573 | * Searches the language of a node, i.e. the values of the xml:lang |
| 4574 | * attribute or the one carried by the nearest ancestor. |
| 4575 | * |
| 4576 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4577 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4578 | */ |
| 4579 | xmlChar * |
| 4580 | xmlNodeGetLang(xmlNodePtr cur) { |
| 4581 | xmlChar *lang; |
| 4582 | |
| 4583 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 4584 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4585 | if (lang != NULL) |
| 4586 | return(lang); |
| 4587 | cur = cur->parent; |
| 4588 | } |
| 4589 | return(NULL); |
| 4590 | } |
| 4591 | |
| 4592 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4593 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4594 | /** |
| 4595 | * xmlNodeSetSpacePreserve: |
| 4596 | * @cur: the node being changed |
| 4597 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 4598 | * |
| 4599 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 4600 | * value of the xml:space attribute. |
| 4601 | */ |
| 4602 | void |
| 4603 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4604 | xmlNsPtr ns; |
| 4605 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4606 | if (cur == NULL) return; |
| 4607 | switch(cur->type) { |
| 4608 | case XML_TEXT_NODE: |
| 4609 | case XML_CDATA_SECTION_NODE: |
| 4610 | case XML_COMMENT_NODE: |
| 4611 | case XML_DOCUMENT_NODE: |
| 4612 | case XML_DOCUMENT_TYPE_NODE: |
| 4613 | case XML_DOCUMENT_FRAG_NODE: |
| 4614 | case XML_NOTATION_NODE: |
| 4615 | case XML_HTML_DOCUMENT_NODE: |
| 4616 | case XML_DTD_NODE: |
| 4617 | case XML_ELEMENT_DECL: |
| 4618 | case XML_ATTRIBUTE_DECL: |
| 4619 | case XML_ENTITY_DECL: |
| 4620 | case XML_PI_NODE: |
| 4621 | case XML_ENTITY_REF_NODE: |
| 4622 | case XML_ENTITY_NODE: |
| 4623 | case XML_NAMESPACE_DECL: |
| 4624 | case XML_XINCLUDE_START: |
| 4625 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4626 | #ifdef LIBXML_DOCB_ENABLED |
| 4627 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4628 | #endif |
| 4629 | return; |
| 4630 | case XML_ELEMENT_NODE: |
| 4631 | case XML_ATTRIBUTE_NODE: |
| 4632 | break; |
| 4633 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4634 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4635 | if (ns == NULL) |
| 4636 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4637 | switch (val) { |
| 4638 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4639 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4640 | break; |
| 4641 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4642 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4643 | break; |
| 4644 | } |
| 4645 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4646 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4647 | |
| 4648 | /** |
| 4649 | * xmlNodeGetSpacePreserve: |
| 4650 | * @cur: the node being checked |
| 4651 | * |
| 4652 | * Searches the space preserving behaviour of a node, i.e. the values |
| 4653 | * of the xml:space attribute or the one carried by the nearest |
| 4654 | * ancestor. |
| 4655 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4656 | * 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] | 4657 | */ |
| 4658 | int |
| 4659 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 4660 | xmlChar *space; |
| 4661 | |
| 4662 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4663 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4664 | if (space != NULL) { |
| 4665 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 4666 | xmlFree(space); |
| 4667 | return(1); |
| 4668 | } |
| 4669 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 4670 | xmlFree(space); |
| 4671 | return(0); |
| 4672 | } |
| 4673 | xmlFree(space); |
| 4674 | } |
| 4675 | cur = cur->parent; |
| 4676 | } |
| 4677 | return(-1); |
| 4678 | } |
| 4679 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4680 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4681 | /** |
| 4682 | * xmlNodeSetName: |
| 4683 | * @cur: the node being changed |
| 4684 | * @name: the new tag name |
| 4685 | * |
| 4686 | * Set (or reset) the name of a node. |
| 4687 | */ |
| 4688 | void |
| 4689 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 4690 | xmlDocPtr doc; |
| 4691 | xmlDictPtr dict; |
| 4692 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4693 | if (cur == NULL) return; |
| 4694 | if (name == NULL) return; |
| 4695 | switch(cur->type) { |
| 4696 | case XML_TEXT_NODE: |
| 4697 | case XML_CDATA_SECTION_NODE: |
| 4698 | case XML_COMMENT_NODE: |
| 4699 | case XML_DOCUMENT_TYPE_NODE: |
| 4700 | case XML_DOCUMENT_FRAG_NODE: |
| 4701 | case XML_NOTATION_NODE: |
| 4702 | case XML_HTML_DOCUMENT_NODE: |
| 4703 | case XML_NAMESPACE_DECL: |
| 4704 | case XML_XINCLUDE_START: |
| 4705 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4706 | #ifdef LIBXML_DOCB_ENABLED |
| 4707 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4708 | #endif |
| 4709 | return; |
| 4710 | case XML_ELEMENT_NODE: |
| 4711 | case XML_ATTRIBUTE_NODE: |
| 4712 | case XML_PI_NODE: |
| 4713 | case XML_ENTITY_REF_NODE: |
| 4714 | case XML_ENTITY_NODE: |
| 4715 | case XML_DTD_NODE: |
| 4716 | case XML_DOCUMENT_NODE: |
| 4717 | case XML_ELEMENT_DECL: |
| 4718 | case XML_ATTRIBUTE_DECL: |
| 4719 | case XML_ENTITY_DECL: |
| 4720 | break; |
| 4721 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 4722 | doc = cur->doc; |
| 4723 | if (doc != NULL) |
| 4724 | dict = doc->dict; |
| 4725 | else |
| 4726 | dict = NULL; |
| 4727 | if (dict != NULL) { |
| 4728 | if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name))) |
| 4729 | xmlFree((xmlChar *) cur->name); |
| 4730 | cur->name = xmlDictLookup(dict, name, -1); |
| 4731 | } else { |
| 4732 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 4733 | cur->name = xmlStrdup(name); |
| 4734 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4735 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4736 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4737 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4738 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4739 | /** |
| 4740 | * xmlNodeSetBase: |
| 4741 | * @cur: the node being changed |
| 4742 | * @uri: the new base URI |
| 4743 | * |
| 4744 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 4745 | * xml:base attribute. |
| 4746 | */ |
| 4747 | void |
Daniel Veillard | f85ce8e | 2003-09-22 10:24:45 +0000 | [diff] [blame] | 4748 | xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4749 | xmlNsPtr ns; |
| 4750 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4751 | if (cur == NULL) return; |
| 4752 | switch(cur->type) { |
| 4753 | case XML_TEXT_NODE: |
| 4754 | case XML_CDATA_SECTION_NODE: |
| 4755 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4756 | case XML_DOCUMENT_TYPE_NODE: |
| 4757 | case XML_DOCUMENT_FRAG_NODE: |
| 4758 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4759 | case XML_DTD_NODE: |
| 4760 | case XML_ELEMENT_DECL: |
| 4761 | case XML_ATTRIBUTE_DECL: |
| 4762 | case XML_ENTITY_DECL: |
| 4763 | case XML_PI_NODE: |
| 4764 | case XML_ENTITY_REF_NODE: |
| 4765 | case XML_ENTITY_NODE: |
| 4766 | case XML_NAMESPACE_DECL: |
| 4767 | case XML_XINCLUDE_START: |
| 4768 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4769 | return; |
| 4770 | case XML_ELEMENT_NODE: |
| 4771 | case XML_ATTRIBUTE_NODE: |
| 4772 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 4773 | case XML_DOCUMENT_NODE: |
| 4774 | #ifdef LIBXML_DOCB_ENABLED |
| 4775 | case XML_DOCB_DOCUMENT_NODE: |
| 4776 | #endif |
| 4777 | case XML_HTML_DOCUMENT_NODE: { |
| 4778 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 4779 | |
| 4780 | if (doc->URL != NULL) |
| 4781 | xmlFree((xmlChar *) doc->URL); |
| 4782 | if (uri == NULL) |
| 4783 | doc->URL = NULL; |
| 4784 | else |
| 4785 | doc->URL = xmlStrdup(uri); |
| 4786 | return; |
| 4787 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4788 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4789 | |
| 4790 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4791 | if (ns == NULL) |
| 4792 | return; |
| 4793 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4794 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4795 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4796 | |
| 4797 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4798 | * xmlNodeGetBase: |
| 4799 | * @doc: the document the node pertains to |
| 4800 | * @cur: the node being checked |
| 4801 | * |
| 4802 | * Searches for the BASE URL. The code should work on both XML |
| 4803 | * and HTML document even if base mechanisms are completely different. |
| 4804 | * It returns the base as defined in RFC 2396 sections |
| 4805 | * 5.1.1. Base URI within Document Content |
| 4806 | * and |
| 4807 | * 5.1.2. Base URI from the Encapsulating Entity |
| 4808 | * However it does not return the document base (5.1.3), use |
| 4809 | * xmlDocumentGetBase() for this |
| 4810 | * |
| 4811 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4812 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4813 | */ |
| 4814 | xmlChar * |
| 4815 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4816 | xmlChar *oldbase = NULL; |
| 4817 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4818 | |
| 4819 | if ((cur == NULL) && (doc == NULL)) |
| 4820 | return(NULL); |
| 4821 | if (doc == NULL) doc = cur->doc; |
| 4822 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 4823 | cur = doc->children; |
| 4824 | while ((cur != NULL) && (cur->name != NULL)) { |
| 4825 | if (cur->type != XML_ELEMENT_NODE) { |
| 4826 | cur = cur->next; |
| 4827 | continue; |
| 4828 | } |
| 4829 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 4830 | cur = cur->children; |
| 4831 | continue; |
| 4832 | } |
| 4833 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 4834 | cur = cur->children; |
| 4835 | continue; |
| 4836 | } |
| 4837 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 4838 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 4839 | } |
| 4840 | cur = cur->next; |
| 4841 | } |
| 4842 | return(NULL); |
| 4843 | } |
| 4844 | while (cur != NULL) { |
| 4845 | if (cur->type == XML_ENTITY_DECL) { |
| 4846 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 4847 | return(xmlStrdup(ent->URI)); |
| 4848 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4849 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4850 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4851 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4852 | if (oldbase != NULL) { |
| 4853 | newbase = xmlBuildURI(oldbase, base); |
| 4854 | if (newbase != NULL) { |
| 4855 | xmlFree(oldbase); |
| 4856 | xmlFree(base); |
| 4857 | oldbase = newbase; |
| 4858 | } else { |
| 4859 | xmlFree(oldbase); |
| 4860 | xmlFree(base); |
| 4861 | return(NULL); |
| 4862 | } |
| 4863 | } else { |
| 4864 | oldbase = base; |
| 4865 | } |
| 4866 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 4867 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 4868 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 4869 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4870 | } |
| 4871 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4872 | cur = cur->parent; |
| 4873 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4874 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 4875 | if (oldbase == NULL) |
| 4876 | return(xmlStrdup(doc->URL)); |
| 4877 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 4878 | xmlFree(oldbase); |
| 4879 | return(newbase); |
| 4880 | } |
| 4881 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | /** |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 4885 | * xmlNodeBufGetContent: |
| 4886 | * @buffer: a buffer |
| 4887 | * @cur: the node being read |
| 4888 | * |
| 4889 | * Read the value of a node @cur, this can be either the text carried |
| 4890 | * directly by this node if it's a TEXT node or the aggregate string |
| 4891 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 4892 | * Entity references are substituted. |
| 4893 | * Fills up the buffer @buffer with this value |
| 4894 | * |
| 4895 | * Returns 0 in case of success and -1 in case of error. |
| 4896 | */ |
| 4897 | int |
| 4898 | xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur) |
| 4899 | { |
| 4900 | if ((cur == NULL) || (buffer == NULL)) return(-1); |
| 4901 | switch (cur->type) { |
| 4902 | case XML_CDATA_SECTION_NODE: |
| 4903 | case XML_TEXT_NODE: |
| 4904 | xmlBufferCat(buffer, cur->content); |
| 4905 | break; |
| 4906 | case XML_DOCUMENT_FRAG_NODE: |
| 4907 | case XML_ELEMENT_NODE:{ |
| 4908 | xmlNodePtr tmp = cur; |
| 4909 | |
| 4910 | while (tmp != NULL) { |
| 4911 | switch (tmp->type) { |
| 4912 | case XML_CDATA_SECTION_NODE: |
| 4913 | case XML_TEXT_NODE: |
| 4914 | if (tmp->content != NULL) |
| 4915 | xmlBufferCat(buffer, tmp->content); |
| 4916 | break; |
| 4917 | case XML_ENTITY_REF_NODE: |
Rob Richards | 77b92ff | 2005-12-20 15:55:14 +0000 | [diff] [blame] | 4918 | xmlNodeBufGetContent(buffer, tmp); |
| 4919 | break; |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 4920 | default: |
| 4921 | break; |
| 4922 | } |
| 4923 | /* |
| 4924 | * Skip to next node |
| 4925 | */ |
| 4926 | if (tmp->children != NULL) { |
| 4927 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4928 | tmp = tmp->children; |
| 4929 | continue; |
| 4930 | } |
| 4931 | } |
| 4932 | if (tmp == cur) |
| 4933 | break; |
| 4934 | |
| 4935 | if (tmp->next != NULL) { |
| 4936 | tmp = tmp->next; |
| 4937 | continue; |
| 4938 | } |
| 4939 | |
| 4940 | do { |
| 4941 | tmp = tmp->parent; |
| 4942 | if (tmp == NULL) |
| 4943 | break; |
| 4944 | if (tmp == cur) { |
| 4945 | tmp = NULL; |
| 4946 | break; |
| 4947 | } |
| 4948 | if (tmp->next != NULL) { |
| 4949 | tmp = tmp->next; |
| 4950 | break; |
| 4951 | } |
| 4952 | } while (tmp != NULL); |
| 4953 | } |
| 4954 | break; |
| 4955 | } |
| 4956 | case XML_ATTRIBUTE_NODE:{ |
| 4957 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 4958 | xmlNodePtr tmp = attr->children; |
| 4959 | |
| 4960 | while (tmp != NULL) { |
| 4961 | if (tmp->type == XML_TEXT_NODE) |
| 4962 | xmlBufferCat(buffer, tmp->content); |
| 4963 | else |
| 4964 | xmlNodeBufGetContent(buffer, tmp); |
| 4965 | tmp = tmp->next; |
| 4966 | } |
| 4967 | break; |
| 4968 | } |
| 4969 | case XML_COMMENT_NODE: |
| 4970 | case XML_PI_NODE: |
| 4971 | xmlBufferCat(buffer, cur->content); |
| 4972 | break; |
| 4973 | case XML_ENTITY_REF_NODE:{ |
| 4974 | xmlEntityPtr ent; |
| 4975 | xmlNodePtr tmp; |
| 4976 | |
| 4977 | /* lookup entity declaration */ |
| 4978 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 4979 | if (ent == NULL) |
| 4980 | return(-1); |
| 4981 | |
| 4982 | /* an entity content can be any "well balanced chunk", |
| 4983 | * i.e. the result of the content [43] production: |
| 4984 | * http://www.w3.org/TR/REC-xml#NT-content |
| 4985 | * -> we iterate through child nodes and recursive call |
| 4986 | * xmlNodeGetContent() which handles all possible node types */ |
| 4987 | tmp = ent->children; |
| 4988 | while (tmp) { |
| 4989 | xmlNodeBufGetContent(buffer, tmp); |
| 4990 | tmp = tmp->next; |
| 4991 | } |
| 4992 | break; |
| 4993 | } |
| 4994 | case XML_ENTITY_NODE: |
| 4995 | case XML_DOCUMENT_TYPE_NODE: |
| 4996 | case XML_NOTATION_NODE: |
| 4997 | case XML_DTD_NODE: |
| 4998 | case XML_XINCLUDE_START: |
| 4999 | case XML_XINCLUDE_END: |
| 5000 | break; |
| 5001 | case XML_DOCUMENT_NODE: |
| 5002 | #ifdef LIBXML_DOCB_ENABLED |
| 5003 | case XML_DOCB_DOCUMENT_NODE: |
| 5004 | #endif |
| 5005 | case XML_HTML_DOCUMENT_NODE: |
| 5006 | cur = cur->children; |
| 5007 | while (cur!= NULL) { |
| 5008 | if ((cur->type == XML_ELEMENT_NODE) || |
| 5009 | (cur->type == XML_TEXT_NODE) || |
| 5010 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 5011 | xmlNodeBufGetContent(buffer, cur); |
| 5012 | } |
| 5013 | cur = cur->next; |
| 5014 | } |
| 5015 | break; |
| 5016 | case XML_NAMESPACE_DECL: |
| 5017 | xmlBufferCat(buffer, ((xmlNsPtr) cur)->href); |
| 5018 | break; |
| 5019 | case XML_ELEMENT_DECL: |
| 5020 | case XML_ATTRIBUTE_DECL: |
| 5021 | case XML_ENTITY_DECL: |
| 5022 | break; |
| 5023 | } |
| 5024 | return(0); |
| 5025 | } |
| 5026 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5027 | * xmlNodeGetContent: |
| 5028 | * @cur: the node being read |
| 5029 | * |
| 5030 | * Read the value of a node, this can be either the text carried |
| 5031 | * directly by this node if it's a TEXT node or the aggregate string |
| 5032 | * 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] | 5033 | * Entity references are substituted. |
| 5034 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5035 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5036 | */ |
| 5037 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5038 | xmlNodeGetContent(xmlNodePtr cur) |
| 5039 | { |
| 5040 | if (cur == NULL) |
| 5041 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5042 | switch (cur->type) { |
| 5043 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5044 | case XML_ELEMENT_NODE:{ |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5045 | xmlBufferPtr buffer; |
| 5046 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5047 | |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 5048 | buffer = xmlBufferCreateSize(64); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5049 | if (buffer == NULL) |
| 5050 | return (NULL); |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5051 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5052 | ret = buffer->content; |
| 5053 | buffer->content = NULL; |
| 5054 | xmlBufferFree(buffer); |
| 5055 | return (ret); |
| 5056 | } |
| 5057 | case XML_ATTRIBUTE_NODE:{ |
| 5058 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 5059 | |
| 5060 | if (attr->parent != NULL) |
| 5061 | return (xmlNodeListGetString |
| 5062 | (attr->parent->doc, attr->children, 1)); |
| 5063 | else |
| 5064 | return (xmlNodeListGetString(NULL, attr->children, 1)); |
| 5065 | break; |
| 5066 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5067 | case XML_COMMENT_NODE: |
| 5068 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5069 | if (cur->content != NULL) |
| 5070 | return (xmlStrdup(cur->content)); |
| 5071 | return (NULL); |
| 5072 | case XML_ENTITY_REF_NODE:{ |
| 5073 | xmlEntityPtr ent; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5074 | xmlBufferPtr buffer; |
| 5075 | xmlChar *ret; |
| 5076 | |
| 5077 | /* lookup entity declaration */ |
| 5078 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5079 | if (ent == NULL) |
| 5080 | return (NULL); |
| 5081 | |
| 5082 | buffer = xmlBufferCreate(); |
| 5083 | if (buffer == NULL) |
| 5084 | return (NULL); |
| 5085 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5086 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5087 | |
| 5088 | ret = buffer->content; |
| 5089 | buffer->content = NULL; |
| 5090 | xmlBufferFree(buffer); |
| 5091 | return (ret); |
| 5092 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5093 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5094 | case XML_DOCUMENT_TYPE_NODE: |
| 5095 | case XML_NOTATION_NODE: |
| 5096 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5097 | case XML_XINCLUDE_START: |
| 5098 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5099 | return (NULL); |
| 5100 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5101 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5102 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5103 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5104 | case XML_HTML_DOCUMENT_NODE: { |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5105 | xmlBufferPtr buffer; |
| 5106 | xmlChar *ret; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5107 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5108 | buffer = xmlBufferCreate(); |
| 5109 | if (buffer == NULL) |
| 5110 | return (NULL); |
| 5111 | |
| 5112 | xmlNodeBufGetContent(buffer, (xmlNodePtr) cur); |
| 5113 | |
| 5114 | ret = buffer->content; |
| 5115 | buffer->content = NULL; |
| 5116 | xmlBufferFree(buffer); |
| 5117 | return (ret); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5118 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 5119 | case XML_NAMESPACE_DECL: { |
| 5120 | xmlChar *tmp; |
| 5121 | |
| 5122 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 5123 | return (tmp); |
| 5124 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5125 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5126 | /* TODO !!! */ |
| 5127 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5128 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5129 | /* TODO !!! */ |
| 5130 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5131 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5132 | /* TODO !!! */ |
| 5133 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5134 | case XML_CDATA_SECTION_NODE: |
| 5135 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5136 | if (cur->content != NULL) |
| 5137 | return (xmlStrdup(cur->content)); |
| 5138 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5139 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5140 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5141 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5142 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5143 | /** |
| 5144 | * xmlNodeSetContent: |
| 5145 | * @cur: the node being modified |
| 5146 | * @content: the new value of the content |
| 5147 | * |
| 5148 | * Replace the content of a node. |
| 5149 | */ |
| 5150 | void |
| 5151 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 5152 | if (cur == NULL) { |
| 5153 | #ifdef DEBUG_TREE |
| 5154 | xmlGenericError(xmlGenericErrorContext, |
| 5155 | "xmlNodeSetContent : node == NULL\n"); |
| 5156 | #endif |
| 5157 | return; |
| 5158 | } |
| 5159 | switch (cur->type) { |
| 5160 | case XML_DOCUMENT_FRAG_NODE: |
| 5161 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5162 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5163 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5164 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 5165 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5166 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5167 | case XML_TEXT_NODE: |
| 5168 | case XML_CDATA_SECTION_NODE: |
| 5169 | case XML_ENTITY_REF_NODE: |
| 5170 | case XML_ENTITY_NODE: |
| 5171 | case XML_PI_NODE: |
| 5172 | case XML_COMMENT_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5173 | if ((cur->content != NULL) && |
| 5174 | (cur->content != (xmlChar *) &(cur->properties))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5175 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
Daniel Veillard | c587bce | 2005-05-10 15:28:08 +0000 | [diff] [blame] | 5176 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5177 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5178 | } |
| 5179 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5180 | cur->last = cur->children = NULL; |
| 5181 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5182 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5183 | } else |
| 5184 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5185 | cur->properties = NULL; |
| 5186 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5187 | break; |
| 5188 | case XML_DOCUMENT_NODE: |
| 5189 | case XML_HTML_DOCUMENT_NODE: |
| 5190 | case XML_DOCUMENT_TYPE_NODE: |
| 5191 | case XML_XINCLUDE_START: |
| 5192 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5193 | #ifdef LIBXML_DOCB_ENABLED |
| 5194 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5195 | #endif |
| 5196 | break; |
| 5197 | case XML_NOTATION_NODE: |
| 5198 | break; |
| 5199 | case XML_DTD_NODE: |
| 5200 | break; |
| 5201 | case XML_NAMESPACE_DECL: |
| 5202 | break; |
| 5203 | case XML_ELEMENT_DECL: |
| 5204 | /* TODO !!! */ |
| 5205 | break; |
| 5206 | case XML_ATTRIBUTE_DECL: |
| 5207 | /* TODO !!! */ |
| 5208 | break; |
| 5209 | case XML_ENTITY_DECL: |
| 5210 | /* TODO !!! */ |
| 5211 | break; |
| 5212 | } |
| 5213 | } |
| 5214 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5215 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5216 | /** |
| 5217 | * xmlNodeSetContentLen: |
| 5218 | * @cur: the node being modified |
| 5219 | * @content: the new value of the content |
| 5220 | * @len: the size of @content |
| 5221 | * |
| 5222 | * Replace the content of a node. |
| 5223 | */ |
| 5224 | void |
| 5225 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5226 | if (cur == NULL) { |
| 5227 | #ifdef DEBUG_TREE |
| 5228 | xmlGenericError(xmlGenericErrorContext, |
| 5229 | "xmlNodeSetContentLen : node == NULL\n"); |
| 5230 | #endif |
| 5231 | return; |
| 5232 | } |
| 5233 | switch (cur->type) { |
| 5234 | case XML_DOCUMENT_FRAG_NODE: |
| 5235 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5236 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5237 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5238 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 5239 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5240 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5241 | case XML_TEXT_NODE: |
| 5242 | case XML_CDATA_SECTION_NODE: |
| 5243 | case XML_ENTITY_REF_NODE: |
| 5244 | case XML_ENTITY_NODE: |
| 5245 | case XML_PI_NODE: |
| 5246 | case XML_COMMENT_NODE: |
| 5247 | case XML_NOTATION_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5248 | if ((cur->content != NULL) && |
| 5249 | (cur->content != (xmlChar *) &(cur->properties))) { |
| 5250 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5251 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
| 5252 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5253 | } |
| 5254 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5255 | cur->children = cur->last = NULL; |
| 5256 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5257 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5258 | } else |
| 5259 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5260 | cur->properties = NULL; |
| 5261 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5262 | break; |
| 5263 | case XML_DOCUMENT_NODE: |
| 5264 | case XML_DTD_NODE: |
| 5265 | case XML_HTML_DOCUMENT_NODE: |
| 5266 | case XML_DOCUMENT_TYPE_NODE: |
| 5267 | case XML_NAMESPACE_DECL: |
| 5268 | case XML_XINCLUDE_START: |
| 5269 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5270 | #ifdef LIBXML_DOCB_ENABLED |
| 5271 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5272 | #endif |
| 5273 | break; |
| 5274 | case XML_ELEMENT_DECL: |
| 5275 | /* TODO !!! */ |
| 5276 | break; |
| 5277 | case XML_ATTRIBUTE_DECL: |
| 5278 | /* TODO !!! */ |
| 5279 | break; |
| 5280 | case XML_ENTITY_DECL: |
| 5281 | /* TODO !!! */ |
| 5282 | break; |
| 5283 | } |
| 5284 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5285 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5286 | |
| 5287 | /** |
| 5288 | * xmlNodeAddContentLen: |
| 5289 | * @cur: the node being modified |
| 5290 | * @content: extra content |
| 5291 | * @len: the size of @content |
| 5292 | * |
| 5293 | * Append the extra substring to the node content. |
| 5294 | */ |
| 5295 | void |
| 5296 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5297 | if (cur == NULL) { |
| 5298 | #ifdef DEBUG_TREE |
| 5299 | xmlGenericError(xmlGenericErrorContext, |
| 5300 | "xmlNodeAddContentLen : node == NULL\n"); |
| 5301 | #endif |
| 5302 | return; |
| 5303 | } |
| 5304 | if (len <= 0) return; |
| 5305 | switch (cur->type) { |
| 5306 | case XML_DOCUMENT_FRAG_NODE: |
| 5307 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5308 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5309 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 5310 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5311 | newNode = xmlNewTextLen(content, len); |
| 5312 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5313 | tmp = xmlAddChild(cur, newNode); |
| 5314 | if (tmp != newNode) |
| 5315 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5316 | if ((last != NULL) && (last->next == newNode)) { |
| 5317 | xmlTextMerge(last, newNode); |
| 5318 | } |
| 5319 | } |
| 5320 | break; |
| 5321 | } |
| 5322 | case XML_ATTRIBUTE_NODE: |
| 5323 | break; |
| 5324 | case XML_TEXT_NODE: |
| 5325 | case XML_CDATA_SECTION_NODE: |
| 5326 | case XML_ENTITY_REF_NODE: |
| 5327 | case XML_ENTITY_NODE: |
| 5328 | case XML_PI_NODE: |
| 5329 | case XML_COMMENT_NODE: |
| 5330 | case XML_NOTATION_NODE: |
| 5331 | if (content != NULL) { |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5332 | if ((cur->content == (xmlChar *) &(cur->properties)) || |
| 5333 | ((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5334 | xmlDictOwns(cur->doc->dict, cur->content))) { |
| 5335 | cur->content = xmlStrncatNew(cur->content, content, len); |
| 5336 | cur->properties = NULL; |
| 5337 | cur->nsDef = NULL; |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5338 | break; |
| 5339 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5340 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5341 | } |
| 5342 | case XML_DOCUMENT_NODE: |
| 5343 | case XML_DTD_NODE: |
| 5344 | case XML_HTML_DOCUMENT_NODE: |
| 5345 | case XML_DOCUMENT_TYPE_NODE: |
| 5346 | case XML_NAMESPACE_DECL: |
| 5347 | case XML_XINCLUDE_START: |
| 5348 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5349 | #ifdef LIBXML_DOCB_ENABLED |
| 5350 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5351 | #endif |
| 5352 | break; |
| 5353 | case XML_ELEMENT_DECL: |
| 5354 | case XML_ATTRIBUTE_DECL: |
| 5355 | case XML_ENTITY_DECL: |
| 5356 | break; |
| 5357 | } |
| 5358 | } |
| 5359 | |
| 5360 | /** |
| 5361 | * xmlNodeAddContent: |
| 5362 | * @cur: the node being modified |
| 5363 | * @content: extra content |
| 5364 | * |
| 5365 | * Append the extra substring to the node content. |
| 5366 | */ |
| 5367 | void |
| 5368 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5369 | int len; |
| 5370 | |
| 5371 | if (cur == NULL) { |
| 5372 | #ifdef DEBUG_TREE |
| 5373 | xmlGenericError(xmlGenericErrorContext, |
| 5374 | "xmlNodeAddContent : node == NULL\n"); |
| 5375 | #endif |
| 5376 | return; |
| 5377 | } |
| 5378 | if (content == NULL) return; |
| 5379 | len = xmlStrlen(content); |
| 5380 | xmlNodeAddContentLen(cur, content, len); |
| 5381 | } |
| 5382 | |
| 5383 | /** |
| 5384 | * xmlTextMerge: |
| 5385 | * @first: the first text node |
| 5386 | * @second: the second text node being merged |
| 5387 | * |
| 5388 | * Merge two text nodes into one |
| 5389 | * Returns the first text node augmented |
| 5390 | */ |
| 5391 | xmlNodePtr |
| 5392 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5393 | if (first == NULL) return(second); |
| 5394 | if (second == NULL) return(first); |
| 5395 | if (first->type != XML_TEXT_NODE) return(first); |
| 5396 | if (second->type != XML_TEXT_NODE) return(first); |
| 5397 | if (second->name != first->name) |
| 5398 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5399 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5400 | xmlUnlinkNode(second); |
| 5401 | xmlFreeNode(second); |
| 5402 | return(first); |
| 5403 | } |
| 5404 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 5405 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5406 | /** |
| 5407 | * xmlGetNsList: |
| 5408 | * @doc: the document |
| 5409 | * @node: the current node |
| 5410 | * |
| 5411 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5412 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5413 | * that need to be freed by the caller or NULL if no |
| 5414 | * namespace if defined |
| 5415 | */ |
| 5416 | xmlNsPtr * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5417 | xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) |
| 5418 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5419 | xmlNsPtr cur; |
| 5420 | xmlNsPtr *ret = NULL; |
| 5421 | int nbns = 0; |
| 5422 | int maxns = 10; |
| 5423 | int i; |
| 5424 | |
| 5425 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5426 | if (node->type == XML_ELEMENT_NODE) { |
| 5427 | cur = node->nsDef; |
| 5428 | while (cur != NULL) { |
| 5429 | if (ret == NULL) { |
| 5430 | ret = |
| 5431 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5432 | sizeof(xmlNsPtr)); |
| 5433 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5434 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5435 | return (NULL); |
| 5436 | } |
| 5437 | ret[nbns] = NULL; |
| 5438 | } |
| 5439 | for (i = 0; i < nbns; i++) { |
| 5440 | if ((cur->prefix == ret[i]->prefix) || |
| 5441 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5442 | break; |
| 5443 | } |
| 5444 | if (i >= nbns) { |
| 5445 | if (nbns >= maxns) { |
| 5446 | maxns *= 2; |
| 5447 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5448 | (maxns + |
| 5449 | 1) * |
| 5450 | sizeof(xmlNsPtr)); |
| 5451 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5452 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5453 | return (NULL); |
| 5454 | } |
| 5455 | } |
| 5456 | ret[nbns++] = cur; |
| 5457 | ret[nbns] = NULL; |
| 5458 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5459 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5460 | cur = cur->next; |
| 5461 | } |
| 5462 | } |
| 5463 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5464 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5465 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5466 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5467 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5468 | |
| 5469 | /** |
| 5470 | * xmlSearchNs: |
| 5471 | * @doc: the document |
| 5472 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5473 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5474 | * |
| 5475 | * Search a Ns registered under a given name space for a document. |
| 5476 | * recurse on the parents until it finds the defined namespace |
| 5477 | * or return NULL otherwise. |
| 5478 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5479 | * We don't allow to cross entities boundaries. If you don't declare |
| 5480 | * the namespace within those you will be in troubles !!! A warning |
| 5481 | * is generated to cover this case. |
| 5482 | * |
| 5483 | * Returns the namespace pointer or NULL. |
| 5484 | */ |
| 5485 | xmlNsPtr |
| 5486 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5487 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5488 | xmlNsPtr cur; |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5489 | xmlNodePtr orig = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5490 | |
| 5491 | if (node == NULL) return(NULL); |
| 5492 | if ((nameSpace != NULL) && |
| 5493 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5494 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5495 | /* |
| 5496 | * The XML-1.0 namespace is normally held on the root |
| 5497 | * element. In this case exceptionally create it on the |
| 5498 | * node element. |
| 5499 | */ |
| 5500 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5501 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5502 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5503 | return(NULL); |
| 5504 | } |
| 5505 | memset(cur, 0, sizeof(xmlNs)); |
| 5506 | cur->type = XML_LOCAL_NAMESPACE; |
| 5507 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5508 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5509 | cur->next = node->nsDef; |
| 5510 | node->nsDef = cur; |
| 5511 | return(cur); |
| 5512 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5513 | if (doc->oldNs == NULL) { |
| 5514 | /* |
| 5515 | * Allocate a new Namespace and fill the fields. |
| 5516 | */ |
| 5517 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5518 | if (doc->oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5519 | xmlTreeErrMemory("searching namespace"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5520 | return(NULL); |
| 5521 | } |
| 5522 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5523 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
| 5524 | |
| 5525 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5526 | doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5527 | } |
| 5528 | return(doc->oldNs); |
| 5529 | } |
| 5530 | while (node != NULL) { |
| 5531 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5532 | (node->type == XML_ENTITY_NODE) || |
| 5533 | (node->type == XML_ENTITY_DECL)) |
| 5534 | return(NULL); |
| 5535 | if (node->type == XML_ELEMENT_NODE) { |
| 5536 | cur = node->nsDef; |
| 5537 | while (cur != NULL) { |
| 5538 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5539 | (cur->href != NULL)) |
| 5540 | return(cur); |
| 5541 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5542 | (cur->href != NULL) && |
| 5543 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5544 | return(cur); |
| 5545 | cur = cur->next; |
| 5546 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5547 | if (orig != node) { |
| 5548 | cur = node->ns; |
| 5549 | if (cur != NULL) { |
| 5550 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5551 | (cur->href != NULL)) |
| 5552 | return(cur); |
| 5553 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5554 | (cur->href != NULL) && |
| 5555 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5556 | return(cur); |
| 5557 | } |
| 5558 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5559 | } |
| 5560 | node = node->parent; |
| 5561 | } |
| 5562 | return(NULL); |
| 5563 | } |
| 5564 | |
| 5565 | /** |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5566 | * xmlNsInScope: |
| 5567 | * @doc: the document |
| 5568 | * @node: the current node |
| 5569 | * @ancestor: the ancestor carrying the namespace |
| 5570 | * @prefix: the namespace prefix |
| 5571 | * |
| 5572 | * Verify that the given namespace held on @ancestor is still in scope |
| 5573 | * on node. |
| 5574 | * |
| 5575 | * Returns 1 if true, 0 if false and -1 in case of error. |
| 5576 | */ |
| 5577 | static int |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5578 | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 5579 | xmlNodePtr ancestor, const xmlChar * prefix) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5580 | { |
| 5581 | xmlNsPtr tst; |
| 5582 | |
| 5583 | while ((node != NULL) && (node != ancestor)) { |
| 5584 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5585 | (node->type == XML_ENTITY_NODE) || |
| 5586 | (node->type == XML_ENTITY_DECL)) |
| 5587 | return (-1); |
| 5588 | if (node->type == XML_ELEMENT_NODE) { |
| 5589 | tst = node->nsDef; |
| 5590 | while (tst != NULL) { |
| 5591 | if ((tst->prefix == NULL) |
| 5592 | && (prefix == NULL)) |
| 5593 | return (0); |
| 5594 | if ((tst->prefix != NULL) |
| 5595 | && (prefix != NULL) |
| 5596 | && (xmlStrEqual(tst->prefix, prefix))) |
| 5597 | return (0); |
| 5598 | tst = tst->next; |
| 5599 | } |
| 5600 | } |
| 5601 | node = node->parent; |
| 5602 | } |
| 5603 | if (node != ancestor) |
| 5604 | return (-1); |
| 5605 | return (1); |
| 5606 | } |
| 5607 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5608 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5609 | * xmlSearchNsByHref: |
| 5610 | * @doc: the document |
| 5611 | * @node: the current node |
| 5612 | * @href: the namespace value |
| 5613 | * |
| 5614 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 5615 | * the defined namespace or return NULL otherwise. |
| 5616 | * Returns the namespace pointer or NULL. |
| 5617 | */ |
| 5618 | xmlNsPtr |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5619 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) |
| 5620 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5621 | xmlNsPtr cur; |
| 5622 | xmlNodePtr orig = node; |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5623 | int is_attr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5624 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5625 | if ((node == NULL) || (href == NULL)) |
| 5626 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5627 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5628 | /* |
| 5629 | * Only the document can hold the XML spec namespace. |
| 5630 | */ |
| 5631 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5632 | /* |
| 5633 | * The XML-1.0 namespace is normally held on the root |
| 5634 | * element. In this case exceptionally create it on the |
| 5635 | * node element. |
| 5636 | */ |
| 5637 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5638 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5639 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5640 | return (NULL); |
| 5641 | } |
| 5642 | memset(cur, 0, sizeof(xmlNs)); |
| 5643 | cur->type = XML_LOCAL_NAMESPACE; |
| 5644 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5645 | cur->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5646 | cur->next = node->nsDef; |
| 5647 | node->nsDef = cur; |
| 5648 | return (cur); |
| 5649 | } |
| 5650 | if (doc->oldNs == NULL) { |
| 5651 | /* |
| 5652 | * Allocate a new Namespace and fill the fields. |
| 5653 | */ |
| 5654 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5655 | if (doc->oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5656 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5657 | return (NULL); |
| 5658 | } |
| 5659 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5660 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5661 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5662 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5663 | doc->oldNs->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5664 | } |
| 5665 | return (doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5666 | } |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5667 | is_attr = (node->type == XML_ATTRIBUTE_NODE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5668 | while (node != NULL) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5669 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5670 | (node->type == XML_ENTITY_NODE) || |
| 5671 | (node->type == XML_ENTITY_DECL)) |
| 5672 | return (NULL); |
| 5673 | if (node->type == XML_ELEMENT_NODE) { |
| 5674 | cur = node->nsDef; |
| 5675 | while (cur != NULL) { |
| 5676 | if ((cur->href != NULL) && (href != NULL) && |
| 5677 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5678 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 5679 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5680 | return (cur); |
| 5681 | } |
| 5682 | cur = cur->next; |
| 5683 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5684 | if (orig != node) { |
| 5685 | cur = node->ns; |
| 5686 | if (cur != NULL) { |
| 5687 | if ((cur->href != NULL) && (href != NULL) && |
| 5688 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5689 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 5690 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5691 | return (cur); |
| 5692 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5693 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5694 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5695 | } |
| 5696 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5697 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5698 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5699 | } |
| 5700 | |
| 5701 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5702 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5703 | * @doc: the document |
| 5704 | * @tree: a node expected to hold the new namespace |
| 5705 | * @ns: the original namespace |
| 5706 | * |
| 5707 | * This function tries to locate a namespace definition in a tree |
| 5708 | * ancestors, or create a new namespace definition node similar to |
| 5709 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 5710 | * null (default namespace) or reused within the subtree defined by |
| 5711 | * @tree or on one of its ancestors then a new prefix is generated. |
| 5712 | * Returns the (new) namespace definition or NULL in case of error |
| 5713 | */ |
| 5714 | xmlNsPtr |
| 5715 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 5716 | xmlNsPtr def; |
| 5717 | xmlChar prefix[50]; |
| 5718 | int counter = 1; |
| 5719 | |
| 5720 | if (tree == NULL) { |
| 5721 | #ifdef DEBUG_TREE |
| 5722 | xmlGenericError(xmlGenericErrorContext, |
| 5723 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 5724 | #endif |
| 5725 | return(NULL); |
| 5726 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5727 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5728 | #ifdef DEBUG_TREE |
| 5729 | xmlGenericError(xmlGenericErrorContext, |
| 5730 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 5731 | #endif |
| 5732 | return(NULL); |
| 5733 | } |
| 5734 | /* |
| 5735 | * Search an existing namespace definition inherited. |
| 5736 | */ |
| 5737 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 5738 | if (def != NULL) |
| 5739 | return(def); |
| 5740 | |
| 5741 | /* |
| 5742 | * Find a close prefix which is not already in use. |
| 5743 | * Let's strip namespace prefixes longer than 20 chars ! |
| 5744 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5745 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5746 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5747 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 5748 | snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5749 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5750 | def = xmlSearchNs(doc, tree, prefix); |
| 5751 | while (def != NULL) { |
| 5752 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5753 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5754 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5755 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 5756 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", |
| 5757 | (char *)ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5758 | def = xmlSearchNs(doc, tree, prefix); |
| 5759 | } |
| 5760 | |
| 5761 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5762 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5763 | */ |
| 5764 | def = xmlNewNs(tree, ns->href, prefix); |
| 5765 | return(def); |
| 5766 | } |
| 5767 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5768 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5769 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5770 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5771 | * @doc: the document |
| 5772 | * @tree: a node defining the subtree to reconciliate |
| 5773 | * |
| 5774 | * This function checks that all the namespaces declared within the given |
| 5775 | * tree are properly declared. This is needed for example after Copy or Cut |
| 5776 | * and then paste operations. The subtree may still hold pointers to |
| 5777 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5778 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5779 | * the new environment. If not possible the new namespaces are redeclared |
| 5780 | * on @tree at the top of the given subtree. |
| 5781 | * Returns the number of namespace declarations created or -1 in case of error. |
| 5782 | */ |
| 5783 | int |
| 5784 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 5785 | xmlNsPtr *oldNs = NULL; |
| 5786 | xmlNsPtr *newNs = NULL; |
| 5787 | int sizeCache = 0; |
| 5788 | int nbCache = 0; |
| 5789 | |
| 5790 | xmlNsPtr n; |
| 5791 | xmlNodePtr node = tree; |
| 5792 | xmlAttrPtr attr; |
| 5793 | int ret = 0, i; |
| 5794 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5795 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); |
| 5796 | if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1); |
| 5797 | if (node->doc != doc) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5798 | while (node != NULL) { |
| 5799 | /* |
| 5800 | * Reconciliate the node namespace |
| 5801 | */ |
| 5802 | if (node->ns != NULL) { |
| 5803 | /* |
| 5804 | * initialize the cache if needed |
| 5805 | */ |
| 5806 | if (sizeCache == 0) { |
| 5807 | sizeCache = 10; |
| 5808 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5809 | sizeof(xmlNsPtr)); |
| 5810 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5811 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5812 | return(-1); |
| 5813 | } |
| 5814 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5815 | sizeof(xmlNsPtr)); |
| 5816 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5817 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5818 | xmlFree(oldNs); |
| 5819 | return(-1); |
| 5820 | } |
| 5821 | } |
| 5822 | for (i = 0;i < nbCache;i++) { |
| 5823 | if (oldNs[i] == node->ns) { |
| 5824 | node->ns = newNs[i]; |
| 5825 | break; |
| 5826 | } |
| 5827 | } |
| 5828 | if (i == nbCache) { |
| 5829 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5830 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5831 | */ |
| 5832 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 5833 | if (n != NULL) { /* :-( what if else ??? */ |
| 5834 | /* |
| 5835 | * check if we need to grow the cache buffers. |
| 5836 | */ |
| 5837 | if (sizeCache <= nbCache) { |
| 5838 | sizeCache *= 2; |
| 5839 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5840 | sizeof(xmlNsPtr)); |
| 5841 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5842 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5843 | xmlFree(newNs); |
| 5844 | return(-1); |
| 5845 | } |
| 5846 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5847 | sizeof(xmlNsPtr)); |
| 5848 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5849 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5850 | xmlFree(oldNs); |
| 5851 | return(-1); |
| 5852 | } |
| 5853 | } |
| 5854 | newNs[nbCache] = n; |
| 5855 | oldNs[nbCache++] = node->ns; |
| 5856 | node->ns = n; |
| 5857 | } |
| 5858 | } |
| 5859 | } |
| 5860 | /* |
| 5861 | * now check for namespace hold by attributes on the node. |
| 5862 | */ |
| 5863 | attr = node->properties; |
| 5864 | while (attr != NULL) { |
| 5865 | if (attr->ns != NULL) { |
| 5866 | /* |
| 5867 | * initialize the cache if needed |
| 5868 | */ |
| 5869 | if (sizeCache == 0) { |
| 5870 | sizeCache = 10; |
| 5871 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5872 | sizeof(xmlNsPtr)); |
| 5873 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5874 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5875 | return(-1); |
| 5876 | } |
| 5877 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5878 | sizeof(xmlNsPtr)); |
| 5879 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5880 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5881 | xmlFree(oldNs); |
| 5882 | return(-1); |
| 5883 | } |
| 5884 | } |
| 5885 | for (i = 0;i < nbCache;i++) { |
| 5886 | if (oldNs[i] == attr->ns) { |
Daniel Veillard | ce66ce1 | 2002-10-28 19:01:59 +0000 | [diff] [blame] | 5887 | attr->ns = newNs[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5888 | break; |
| 5889 | } |
| 5890 | } |
| 5891 | if (i == nbCache) { |
| 5892 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5893 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5894 | */ |
| 5895 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 5896 | if (n != NULL) { /* :-( what if else ??? */ |
| 5897 | /* |
| 5898 | * check if we need to grow the cache buffers. |
| 5899 | */ |
| 5900 | if (sizeCache <= nbCache) { |
| 5901 | sizeCache *= 2; |
| 5902 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5903 | sizeof(xmlNsPtr)); |
| 5904 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5905 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5906 | xmlFree(newNs); |
| 5907 | return(-1); |
| 5908 | } |
| 5909 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5910 | sizeof(xmlNsPtr)); |
| 5911 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5912 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5913 | xmlFree(oldNs); |
| 5914 | return(-1); |
| 5915 | } |
| 5916 | } |
| 5917 | newNs[nbCache] = n; |
| 5918 | oldNs[nbCache++] = attr->ns; |
| 5919 | attr->ns = n; |
| 5920 | } |
| 5921 | } |
| 5922 | } |
| 5923 | attr = attr->next; |
| 5924 | } |
| 5925 | |
| 5926 | /* |
| 5927 | * Browse the full subtree, deep first |
| 5928 | */ |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 5929 | if (node->children != NULL && node->type != XML_ENTITY_REF_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5930 | /* deep first */ |
| 5931 | node = node->children; |
| 5932 | } else if ((node != tree) && (node->next != NULL)) { |
| 5933 | /* then siblings */ |
| 5934 | node = node->next; |
| 5935 | } else if (node != tree) { |
| 5936 | /* go up to parents->next if needed */ |
| 5937 | while (node != tree) { |
| 5938 | if (node->parent != NULL) |
| 5939 | node = node->parent; |
| 5940 | if ((node != tree) && (node->next != NULL)) { |
| 5941 | node = node->next; |
| 5942 | break; |
| 5943 | } |
| 5944 | if (node->parent == NULL) { |
| 5945 | node = NULL; |
| 5946 | break; |
| 5947 | } |
| 5948 | } |
| 5949 | /* exit condition */ |
| 5950 | if (node == tree) |
| 5951 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 5952 | } else |
| 5953 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5954 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5955 | if (oldNs != NULL) |
| 5956 | xmlFree(oldNs); |
| 5957 | if (newNs != NULL) |
| 5958 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5959 | return(ret); |
| 5960 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5961 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5962 | |
| 5963 | /** |
| 5964 | * xmlHasProp: |
| 5965 | * @node: the node |
| 5966 | * @name: the attribute name |
| 5967 | * |
| 5968 | * Search an attribute associated to a node |
| 5969 | * This function also looks in DTD attribute declaration for #FIXED or |
| 5970 | * default declaration values unless DTD use has been turned off. |
| 5971 | * |
| 5972 | * Returns the attribute or the attribute declaration or NULL if |
| 5973 | * neither was found. |
| 5974 | */ |
| 5975 | xmlAttrPtr |
| 5976 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 5977 | xmlAttrPtr prop; |
| 5978 | xmlDocPtr doc; |
| 5979 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5980 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 5981 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5982 | /* |
| 5983 | * Check on the properties attached to the node |
| 5984 | */ |
| 5985 | prop = node->properties; |
| 5986 | while (prop != NULL) { |
| 5987 | if (xmlStrEqual(prop->name, name)) { |
| 5988 | return(prop); |
| 5989 | } |
| 5990 | prop = prop->next; |
| 5991 | } |
| 5992 | if (!xmlCheckDTD) return(NULL); |
| 5993 | |
| 5994 | /* |
| 5995 | * Check if there is a default declaration in the internal |
| 5996 | * or external subsets |
| 5997 | */ |
| 5998 | doc = node->doc; |
| 5999 | if (doc != NULL) { |
| 6000 | xmlAttributePtr attrDecl; |
| 6001 | if (doc->intSubset != NULL) { |
| 6002 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6003 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6004 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6005 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6006 | /* return attribute declaration only if a default value is given |
| 6007 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6008 | return((xmlAttrPtr) attrDecl); |
| 6009 | } |
| 6010 | } |
| 6011 | return(NULL); |
| 6012 | } |
| 6013 | |
| 6014 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6015 | * xmlHasNsProp: |
| 6016 | * @node: the node |
| 6017 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6018 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6019 | * |
| 6020 | * Search for an attribute associated to a node |
| 6021 | * This attribute has to be anchored in the namespace specified. |
| 6022 | * This does the entity substitution. |
| 6023 | * This function looks in DTD attribute declaration for #FIXED or |
| 6024 | * default declaration values unless DTD use has been turned off. |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6025 | * Note that a namespace of NULL indicates to use the default namespace. |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6026 | * |
| 6027 | * Returns the attribute or the attribute declaration or NULL |
| 6028 | * if neither was found. |
| 6029 | */ |
| 6030 | xmlAttrPtr |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6031 | xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6032 | xmlAttrPtr prop; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6033 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6034 | xmlDocPtr doc; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6035 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6036 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6037 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6038 | return(NULL); |
| 6039 | |
| 6040 | prop = node->properties; |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6041 | while (prop != NULL) { |
| 6042 | /* |
| 6043 | * One need to have |
| 6044 | * - same attribute names |
| 6045 | * - and the attribute carrying that namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6046 | */ |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6047 | if (xmlStrEqual(prop->name, name)) { |
| 6048 | if (((prop->ns != NULL) && |
| 6049 | (xmlStrEqual(prop->ns->href, nameSpace))) || |
| 6050 | ((prop->ns == NULL) && (nameSpace == NULL))) { |
| 6051 | return(prop); |
| 6052 | } |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6053 | } |
| 6054 | prop = prop->next; |
| 6055 | } |
| 6056 | if (!xmlCheckDTD) return(NULL); |
| 6057 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6058 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6059 | /* |
| 6060 | * Check if there is a default declaration in the internal |
| 6061 | * or external subsets |
| 6062 | */ |
| 6063 | doc = node->doc; |
| 6064 | if (doc != NULL) { |
| 6065 | if (doc->intSubset != NULL) { |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 6066 | xmlAttributePtr attrDecl = NULL; |
| 6067 | xmlNsPtr *nsList, *cur; |
| 6068 | xmlChar *ename; |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6069 | |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 6070 | nsList = xmlGetNsList(node->doc, node); |
| 6071 | if (nsList == NULL) |
| 6072 | return(NULL); |
| 6073 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 6074 | ename = xmlStrdup(node->ns->prefix); |
| 6075 | ename = xmlStrcat(ename, BAD_CAST ":"); |
| 6076 | ename = xmlStrcat(ename, node->name); |
| 6077 | } else { |
| 6078 | ename = xmlStrdup(node->name); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6079 | } |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 6080 | if (ename == NULL) { |
| 6081 | xmlFree(nsList); |
| 6082 | return(NULL); |
| 6083 | } |
| 6084 | |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6085 | if (nameSpace == NULL) { |
| 6086 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename, |
| 6087 | name, NULL); |
| 6088 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 6089 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename, |
| 6090 | name, NULL); |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 6091 | } |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6092 | } else { |
| 6093 | cur = nsList; |
| 6094 | while (*cur != NULL) { |
| 6095 | if (xmlStrEqual((*cur)->href, nameSpace)) { |
| 6096 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename, |
| 6097 | name, (*cur)->prefix); |
| 6098 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6099 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename, |
| 6100 | name, (*cur)->prefix); |
| 6101 | } |
| 6102 | cur++; |
| 6103 | } |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 6104 | } |
| 6105 | xmlFree(nsList); |
| 6106 | xmlFree(ename); |
| 6107 | return((xmlAttrPtr) attrDecl); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6108 | } |
| 6109 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6110 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6111 | return(NULL); |
| 6112 | } |
| 6113 | |
| 6114 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6115 | * xmlGetProp: |
| 6116 | * @node: the node |
| 6117 | * @name: the attribute name |
| 6118 | * |
| 6119 | * Search and get the value of an attribute associated to a node |
| 6120 | * This does the entity substitution. |
| 6121 | * This function looks in DTD attribute declaration for #FIXED or |
| 6122 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 6123 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6124 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 6125 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6126 | * |
| 6127 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6128 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6129 | */ |
| 6130 | xmlChar * |
| 6131 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
| 6132 | xmlAttrPtr prop; |
| 6133 | xmlDocPtr doc; |
| 6134 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6135 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6136 | return(NULL); |
| 6137 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6138 | /* |
| 6139 | * Check on the properties attached to the node |
| 6140 | */ |
| 6141 | prop = node->properties; |
| 6142 | while (prop != NULL) { |
| 6143 | if (xmlStrEqual(prop->name, name)) { |
| 6144 | xmlChar *ret; |
| 6145 | |
| 6146 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 6147 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 6148 | return(ret); |
| 6149 | } |
| 6150 | prop = prop->next; |
| 6151 | } |
| 6152 | if (!xmlCheckDTD) return(NULL); |
| 6153 | |
| 6154 | /* |
| 6155 | * Check if there is a default declaration in the internal |
| 6156 | * or external subsets |
| 6157 | */ |
| 6158 | doc = node->doc; |
| 6159 | if (doc != NULL) { |
| 6160 | xmlAttributePtr attrDecl; |
| 6161 | if (doc->intSubset != NULL) { |
| 6162 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6163 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6164 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6165 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6166 | /* return attribute declaration only if a default value is given |
| 6167 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6168 | return(xmlStrdup(attrDecl->defaultValue)); |
| 6169 | } |
| 6170 | } |
| 6171 | return(NULL); |
| 6172 | } |
| 6173 | |
| 6174 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6175 | * xmlGetNoNsProp: |
| 6176 | * @node: the node |
| 6177 | * @name: the attribute name |
| 6178 | * |
| 6179 | * Search and get the value of an attribute associated to a node |
| 6180 | * This does the entity substitution. |
| 6181 | * This function looks in DTD attribute declaration for #FIXED or |
| 6182 | * default declaration values unless DTD use has been turned off. |
| 6183 | * This function is similar to xmlGetProp except it will accept only |
| 6184 | * an attribute in no namespace. |
| 6185 | * |
| 6186 | * Returns the attribute value or NULL if not found. |
| 6187 | * It's up to the caller to free the memory with xmlFree(). |
| 6188 | */ |
| 6189 | xmlChar * |
| 6190 | xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) { |
| 6191 | xmlAttrPtr prop; |
| 6192 | xmlDocPtr doc; |
| 6193 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6194 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6195 | return(NULL); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6196 | /* |
| 6197 | * Check on the properties attached to the node |
| 6198 | */ |
| 6199 | prop = node->properties; |
| 6200 | while (prop != NULL) { |
| 6201 | if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) { |
| 6202 | xmlChar *ret; |
| 6203 | |
| 6204 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 6205 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 6206 | return(ret); |
| 6207 | } |
| 6208 | prop = prop->next; |
| 6209 | } |
| 6210 | if (!xmlCheckDTD) return(NULL); |
| 6211 | |
| 6212 | /* |
| 6213 | * Check if there is a default declaration in the internal |
| 6214 | * or external subsets |
| 6215 | */ |
| 6216 | doc = node->doc; |
| 6217 | if (doc != NULL) { |
| 6218 | xmlAttributePtr attrDecl; |
| 6219 | if (doc->intSubset != NULL) { |
| 6220 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6221 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6222 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6223 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6224 | /* return attribute declaration only if a default value is given |
| 6225 | (that includes #FIXED declarations) */ |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6226 | return(xmlStrdup(attrDecl->defaultValue)); |
| 6227 | } |
| 6228 | } |
| 6229 | return(NULL); |
| 6230 | } |
| 6231 | |
| 6232 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6233 | * xmlGetNsProp: |
| 6234 | * @node: the node |
| 6235 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6236 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6237 | * |
| 6238 | * Search and get the value of an attribute associated to a node |
| 6239 | * This attribute has to be anchored in the namespace specified. |
| 6240 | * This does the entity substitution. |
| 6241 | * This function looks in DTD attribute declaration for #FIXED or |
| 6242 | * default declaration values unless DTD use has been turned off. |
| 6243 | * |
| 6244 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6245 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6246 | */ |
| 6247 | xmlChar * |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6248 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6249 | xmlAttrPtr prop; |
| 6250 | xmlDocPtr doc; |
| 6251 | xmlNsPtr ns; |
| 6252 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6253 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6254 | return(NULL); |
| 6255 | |
| 6256 | prop = node->properties; |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6257 | if (nameSpace == NULL) |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6258 | return(xmlGetNoNsProp(node, name)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6259 | while (prop != NULL) { |
| 6260 | /* |
| 6261 | * One need to have |
| 6262 | * - same attribute names |
| 6263 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6264 | */ |
| 6265 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | e8fc08e | 2001-06-07 19:35:47 +0000 | [diff] [blame] | 6266 | ((prop->ns != NULL) && |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6267 | (xmlStrEqual(prop->ns->href, nameSpace)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6268 | xmlChar *ret; |
| 6269 | |
| 6270 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 6271 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 6272 | return(ret); |
| 6273 | } |
| 6274 | prop = prop->next; |
| 6275 | } |
| 6276 | if (!xmlCheckDTD) return(NULL); |
| 6277 | |
| 6278 | /* |
| 6279 | * Check if there is a default declaration in the internal |
| 6280 | * or external subsets |
| 6281 | */ |
| 6282 | doc = node->doc; |
| 6283 | if (doc != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6284 | if (doc->intSubset != NULL) { |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 6285 | xmlAttributePtr attrDecl; |
| 6286 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6287 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6288 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6289 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 6290 | |
| 6291 | if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) { |
| 6292 | /* |
| 6293 | * The DTD declaration only allows a prefix search |
| 6294 | */ |
| 6295 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6296 | if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6297 | return(xmlStrdup(attrDecl->defaultValue)); |
| 6298 | } |
| 6299 | } |
| 6300 | } |
| 6301 | return(NULL); |
| 6302 | } |
| 6303 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6304 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
| 6305 | /** |
| 6306 | * xmlUnsetProp: |
| 6307 | * @node: the node |
| 6308 | * @name: the attribute name |
| 6309 | * |
| 6310 | * Remove an attribute carried by a node. |
| 6311 | * Returns 0 if successful, -1 if not found |
| 6312 | */ |
| 6313 | int |
| 6314 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6315 | xmlAttrPtr prop; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6316 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6317 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6318 | return(-1); |
| 6319 | prop = node->properties; |
| 6320 | while (prop != NULL) { |
| 6321 | if ((xmlStrEqual(prop->name, name)) && |
| 6322 | (prop->ns == NULL)) { |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 6323 | xmlUnlinkNode((xmlNodePtr) prop); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6324 | xmlFreeProp(prop); |
| 6325 | return(0); |
| 6326 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6327 | prop = prop->next; |
| 6328 | } |
| 6329 | return(-1); |
| 6330 | } |
| 6331 | |
| 6332 | /** |
| 6333 | * xmlUnsetNsProp: |
| 6334 | * @node: the node |
| 6335 | * @ns: the namespace definition |
| 6336 | * @name: the attribute name |
| 6337 | * |
| 6338 | * Remove an attribute carried by a node. |
| 6339 | * Returns 0 if successful, -1 if not found |
| 6340 | */ |
| 6341 | int |
| 6342 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6343 | xmlAttrPtr prop; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6344 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6345 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6346 | return(-1); |
Daniel Veillard | 27f2010 | 2004-11-05 11:50:11 +0000 | [diff] [blame] | 6347 | prop = node->properties; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6348 | if (ns == NULL) |
| 6349 | return(xmlUnsetProp(node, name)); |
| 6350 | if (ns->href == NULL) |
| 6351 | return(-1); |
| 6352 | while (prop != NULL) { |
| 6353 | if ((xmlStrEqual(prop->name, name)) && |
| 6354 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 6355 | xmlUnlinkNode((xmlNodePtr) prop); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6356 | xmlFreeProp(prop); |
| 6357 | return(0); |
| 6358 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6359 | prop = prop->next; |
| 6360 | } |
| 6361 | return(-1); |
| 6362 | } |
| 6363 | #endif |
| 6364 | |
| 6365 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6366 | /** |
| 6367 | * xmlSetProp: |
| 6368 | * @node: the node |
| 6369 | * @name: the attribute name |
| 6370 | * @value: the attribute value |
| 6371 | * |
| 6372 | * Set (or reset) an attribute carried by a node. |
| 6373 | * Returns the attribute pointer. |
| 6374 | */ |
| 6375 | xmlAttrPtr |
| 6376 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6377 | xmlAttrPtr prop; |
| 6378 | xmlDocPtr doc; |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6379 | int len; |
| 6380 | const xmlChar *nqname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6381 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6382 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6383 | return(NULL); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6384 | |
| 6385 | /* |
| 6386 | * handle QNames |
| 6387 | */ |
| 6388 | nqname = xmlSplitQName3(name, &len); |
| 6389 | if (nqname != NULL) { |
| 6390 | xmlNsPtr ns; |
| 6391 | xmlChar *prefix = xmlStrndup(name, len); |
| 6392 | ns = xmlSearchNs(node->doc, node, prefix); |
| 6393 | if (prefix != NULL) |
| 6394 | xmlFree(prefix); |
| 6395 | if (ns != NULL) |
| 6396 | return(xmlSetNsProp(node, ns, nqname, value)); |
| 6397 | } |
| 6398 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6399 | doc = node->doc; |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6400 | prop = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6401 | while (prop != NULL) { |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6402 | if ((xmlStrEqual(prop->name, name)) && |
| 6403 | (prop->ns == NULL)){ |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 6404 | int id = 0; |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6405 | xmlNodePtr oldprop = prop->children; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 6406 | if (prop->atype == XML_ATTRIBUTE_ID) { |
| 6407 | id = 1; |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6408 | xmlRemoveID(node->doc, prop); |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 6409 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6410 | prop->children = NULL; |
| 6411 | prop->last = NULL; |
| 6412 | if (value != NULL) { |
| 6413 | xmlChar *buffer; |
| 6414 | xmlNodePtr tmp; |
| 6415 | |
| 6416 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6417 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 6418 | prop->last = NULL; |
| 6419 | prop->doc = doc; |
| 6420 | tmp = prop->children; |
| 6421 | while (tmp != NULL) { |
| 6422 | tmp->parent = (xmlNodePtr) prop; |
| 6423 | tmp->doc = doc; |
| 6424 | if (tmp->next == NULL) |
| 6425 | prop->last = tmp; |
| 6426 | tmp = tmp->next; |
| 6427 | } |
| 6428 | xmlFree(buffer); |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6429 | } |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6430 | if (oldprop != NULL) |
| 6431 | xmlFreeNodeList(oldprop); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6432 | if (id) |
| 6433 | xmlAddID(NULL, node->doc, value, prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6434 | return(prop); |
| 6435 | } |
| 6436 | prop = prop->next; |
| 6437 | } |
| 6438 | prop = xmlNewProp(node, name, value); |
| 6439 | return(prop); |
| 6440 | } |
| 6441 | |
| 6442 | /** |
| 6443 | * xmlSetNsProp: |
| 6444 | * @node: the node |
| 6445 | * @ns: the namespace definition |
| 6446 | * @name: the attribute name |
| 6447 | * @value: the attribute value |
| 6448 | * |
| 6449 | * Set (or reset) an attribute carried by a node. |
| 6450 | * The ns structure must be in scope, this is not checked. |
| 6451 | * |
| 6452 | * Returns the attribute pointer. |
| 6453 | */ |
| 6454 | xmlAttrPtr |
| 6455 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 6456 | const xmlChar *value) { |
| 6457 | xmlAttrPtr prop; |
| 6458 | |
Daniel Veillard | b6b36d3 | 2005-02-09 16:48:53 +0000 | [diff] [blame] | 6459 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6460 | return(NULL); |
| 6461 | |
| 6462 | if (ns == NULL) |
| 6463 | return(xmlSetProp(node, name, value)); |
| 6464 | if (ns->href == NULL) |
| 6465 | return(NULL); |
| 6466 | prop = node->properties; |
| 6467 | |
| 6468 | while (prop != NULL) { |
| 6469 | /* |
| 6470 | * One need to have |
| 6471 | * - same attribute names |
| 6472 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6473 | */ |
| 6474 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | a57c26e | 2002-08-01 12:52:24 +0000 | [diff] [blame] | 6475 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 6476 | int id = 0; |
| 6477 | if (prop->atype == XML_ATTRIBUTE_ID) { |
| 6478 | id = 1; |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6479 | xmlRemoveID(node->doc, prop); |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 6480 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6481 | if (prop->children != NULL) |
| 6482 | xmlFreeNodeList(prop->children); |
| 6483 | prop->children = NULL; |
| 6484 | prop->last = NULL; |
| 6485 | prop->ns = ns; |
| 6486 | if (value != NULL) { |
| 6487 | xmlChar *buffer; |
| 6488 | xmlNodePtr tmp; |
| 6489 | |
| 6490 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6491 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 6492 | prop->last = NULL; |
| 6493 | tmp = prop->children; |
| 6494 | while (tmp != NULL) { |
| 6495 | tmp->parent = (xmlNodePtr) prop; |
| 6496 | if (tmp->next == NULL) |
| 6497 | prop->last = tmp; |
| 6498 | tmp = tmp->next; |
| 6499 | } |
| 6500 | xmlFree(buffer); |
| 6501 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6502 | if (id) |
| 6503 | xmlAddID(NULL, node->doc, value, prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6504 | return(prop); |
| 6505 | } |
| 6506 | prop = prop->next; |
| 6507 | } |
| 6508 | prop = xmlNewNsProp(node, ns, name, value); |
| 6509 | return(prop); |
| 6510 | } |
| 6511 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6512 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6513 | |
| 6514 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6515 | * xmlNodeIsText: |
| 6516 | * @node: the node |
| 6517 | * |
| 6518 | * Is this node a Text node ? |
| 6519 | * Returns 1 yes, 0 no |
| 6520 | */ |
| 6521 | int |
| 6522 | xmlNodeIsText(xmlNodePtr node) { |
| 6523 | if (node == NULL) return(0); |
| 6524 | |
| 6525 | if (node->type == XML_TEXT_NODE) return(1); |
| 6526 | return(0); |
| 6527 | } |
| 6528 | |
| 6529 | /** |
| 6530 | * xmlIsBlankNode: |
| 6531 | * @node: the node |
| 6532 | * |
| 6533 | * Checks whether this node is an empty or whitespace only |
| 6534 | * (and possibly ignorable) text-node. |
| 6535 | * |
| 6536 | * Returns 1 yes, 0 no |
| 6537 | */ |
| 6538 | int |
| 6539 | xmlIsBlankNode(xmlNodePtr node) { |
| 6540 | const xmlChar *cur; |
| 6541 | if (node == NULL) return(0); |
| 6542 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6543 | if ((node->type != XML_TEXT_NODE) && |
| 6544 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6545 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6546 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6547 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6548 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6549 | if (!IS_BLANK_CH(*cur)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6550 | cur++; |
| 6551 | } |
| 6552 | |
| 6553 | return(1); |
| 6554 | } |
| 6555 | |
| 6556 | /** |
| 6557 | * xmlTextConcat: |
| 6558 | * @node: the node |
| 6559 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6560 | * @len: @content length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6561 | * |
| 6562 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6563 | * |
| 6564 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6565 | */ |
| 6566 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6567 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6568 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6569 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6570 | |
| 6571 | if ((node->type != XML_TEXT_NODE) && |
| 6572 | (node->type != XML_CDATA_SECTION_NODE)) { |
| 6573 | #ifdef DEBUG_TREE |
| 6574 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6575 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6576 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6577 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6578 | } |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6579 | /* need to check if content is currently in the dictionary */ |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6580 | if ((node->content == (xmlChar *) &(node->properties)) || |
| 6581 | ((node->doc != NULL) && (node->doc->dict != NULL) && |
| 6582 | xmlDictOwns(node->doc->dict, node->content))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6583 | node->content = xmlStrncatNew(node->content, content, len); |
| 6584 | } else { |
| 6585 | node->content = xmlStrncat(node->content, content, len); |
| 6586 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6587 | node->properties = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6588 | if (node->content == NULL) |
| 6589 | return(-1); |
| 6590 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6591 | } |
| 6592 | |
| 6593 | /************************************************************************ |
| 6594 | * * |
| 6595 | * Output : to a FILE or in memory * |
| 6596 | * * |
| 6597 | ************************************************************************/ |
| 6598 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6599 | /** |
| 6600 | * xmlBufferCreate: |
| 6601 | * |
| 6602 | * routine to create an XML buffer. |
| 6603 | * returns the new structure. |
| 6604 | */ |
| 6605 | xmlBufferPtr |
| 6606 | xmlBufferCreate(void) { |
| 6607 | xmlBufferPtr ret; |
| 6608 | |
| 6609 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6610 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6611 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6612 | return(NULL); |
| 6613 | } |
| 6614 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 6615 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6616 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6617 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6618 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6619 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6620 | xmlFree(ret); |
| 6621 | return(NULL); |
| 6622 | } |
| 6623 | ret->content[0] = 0; |
| 6624 | return(ret); |
| 6625 | } |
| 6626 | |
| 6627 | /** |
| 6628 | * xmlBufferCreateSize: |
| 6629 | * @size: initial size of buffer |
| 6630 | * |
| 6631 | * routine to create an XML buffer. |
| 6632 | * returns the new structure. |
| 6633 | */ |
| 6634 | xmlBufferPtr |
| 6635 | xmlBufferCreateSize(size_t size) { |
| 6636 | xmlBufferPtr ret; |
| 6637 | |
| 6638 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6639 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6640 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6641 | return(NULL); |
| 6642 | } |
| 6643 | ret->use = 0; |
| 6644 | ret->alloc = xmlBufferAllocScheme; |
| 6645 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 6646 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6647 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6648 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6649 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6650 | xmlFree(ret); |
| 6651 | return(NULL); |
| 6652 | } |
| 6653 | ret->content[0] = 0; |
| 6654 | } else |
| 6655 | ret->content = NULL; |
| 6656 | return(ret); |
| 6657 | } |
| 6658 | |
| 6659 | /** |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6660 | * xmlBufferCreateStatic: |
| 6661 | * @mem: the memory area |
| 6662 | * @size: the size in byte |
| 6663 | * |
MST 2003 John Fleck | a0e7e93 | 2003-12-19 03:13:47 +0000 | [diff] [blame] | 6664 | * routine to create an XML buffer from an immutable memory area. |
| 6665 | * 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] | 6666 | * present until the end of the buffer lifetime. |
| 6667 | * |
| 6668 | * returns the new structure. |
| 6669 | */ |
| 6670 | xmlBufferPtr |
| 6671 | xmlBufferCreateStatic(void *mem, size_t size) { |
| 6672 | xmlBufferPtr ret; |
| 6673 | |
| 6674 | if ((mem == NULL) || (size == 0)) |
| 6675 | return(NULL); |
| 6676 | |
| 6677 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6678 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6679 | xmlTreeErrMemory("creating buffer"); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6680 | return(NULL); |
| 6681 | } |
| 6682 | ret->use = size; |
| 6683 | ret->size = size; |
| 6684 | ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; |
| 6685 | ret->content = (xmlChar *) mem; |
| 6686 | return(ret); |
| 6687 | } |
| 6688 | |
| 6689 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6690 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6691 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6692 | * @scheme: allocation scheme to use |
| 6693 | * |
| 6694 | * Sets the allocation scheme for this buffer |
| 6695 | */ |
| 6696 | void |
| 6697 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 6698 | xmlBufferAllocationScheme scheme) { |
| 6699 | if (buf == NULL) { |
| 6700 | #ifdef DEBUG_BUFFER |
| 6701 | xmlGenericError(xmlGenericErrorContext, |
| 6702 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 6703 | #endif |
| 6704 | return; |
| 6705 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6706 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6707 | |
| 6708 | buf->alloc = scheme; |
| 6709 | } |
| 6710 | |
| 6711 | /** |
| 6712 | * xmlBufferFree: |
| 6713 | * @buf: the buffer to free |
| 6714 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 6715 | * Frees an XML buffer. It frees both the content and the structure which |
| 6716 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6717 | */ |
| 6718 | void |
| 6719 | xmlBufferFree(xmlBufferPtr buf) { |
| 6720 | if (buf == NULL) { |
| 6721 | #ifdef DEBUG_BUFFER |
| 6722 | xmlGenericError(xmlGenericErrorContext, |
| 6723 | "xmlBufferFree: buf == NULL\n"); |
| 6724 | #endif |
| 6725 | return; |
| 6726 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6727 | |
| 6728 | if ((buf->content != NULL) && |
| 6729 | (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6730 | xmlFree(buf->content); |
| 6731 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6732 | xmlFree(buf); |
| 6733 | } |
| 6734 | |
| 6735 | /** |
| 6736 | * xmlBufferEmpty: |
| 6737 | * @buf: the buffer |
| 6738 | * |
| 6739 | * empty a buffer. |
| 6740 | */ |
| 6741 | void |
| 6742 | xmlBufferEmpty(xmlBufferPtr buf) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6743 | if (buf == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6744 | if (buf->content == NULL) return; |
| 6745 | buf->use = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6746 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 6747 | buf->content = BAD_CAST ""; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6748 | } else { |
| 6749 | memset(buf->content, 0, buf->size); |
| 6750 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6751 | } |
| 6752 | |
| 6753 | /** |
| 6754 | * xmlBufferShrink: |
| 6755 | * @buf: the buffer to dump |
| 6756 | * @len: the number of xmlChar to remove |
| 6757 | * |
| 6758 | * Remove the beginning of an XML buffer. |
| 6759 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6760 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6761 | */ |
| 6762 | int |
| 6763 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 6764 | if (buf == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6765 | if (len == 0) return(0); |
| 6766 | if (len > buf->use) return(-1); |
| 6767 | |
| 6768 | buf->use -= len; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6769 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
| 6770 | buf->content += len; |
| 6771 | } else { |
| 6772 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
| 6773 | buf->content[buf->use] = 0; |
| 6774 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6775 | return(len); |
| 6776 | } |
| 6777 | |
| 6778 | /** |
| 6779 | * xmlBufferGrow: |
| 6780 | * @buf: the buffer |
| 6781 | * @len: the minimum free size to allocate |
| 6782 | * |
| 6783 | * Grow the available space of an XML buffer. |
| 6784 | * |
| 6785 | * Returns the new available space or -1 in case of error |
| 6786 | */ |
| 6787 | int |
| 6788 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 6789 | int size; |
| 6790 | xmlChar *newbuf; |
| 6791 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 6792 | if (buf == NULL) return(-1); |
| 6793 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6794 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6795 | if (len + buf->use < buf->size) return(0); |
| 6796 | |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 6797 | /* |
| 6798 | * Windows has a BIG problem on realloc timing, so we try to double |
| 6799 | * the buffer size (if that's enough) (bug 146697) |
| 6800 | */ |
| 6801 | #ifdef WIN32 |
| 6802 | if (buf->size > len) |
| 6803 | size = buf->size * 2; |
| 6804 | else |
| 6805 | size = buf->use + len + 100; |
| 6806 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6807 | size = buf->use + len + 100; |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 6808 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6809 | |
| 6810 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6811 | if (newbuf == NULL) { |
| 6812 | xmlTreeErrMemory("growing buffer"); |
| 6813 | return(-1); |
| 6814 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6815 | buf->content = newbuf; |
| 6816 | buf->size = size; |
| 6817 | return(buf->size - buf->use); |
| 6818 | } |
| 6819 | |
| 6820 | /** |
| 6821 | * xmlBufferDump: |
| 6822 | * @file: the file output |
| 6823 | * @buf: the buffer to dump |
| 6824 | * |
| 6825 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6826 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6827 | */ |
| 6828 | int |
| 6829 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 6830 | int ret; |
| 6831 | |
| 6832 | if (buf == NULL) { |
| 6833 | #ifdef DEBUG_BUFFER |
| 6834 | xmlGenericError(xmlGenericErrorContext, |
| 6835 | "xmlBufferDump: buf == NULL\n"); |
| 6836 | #endif |
| 6837 | return(0); |
| 6838 | } |
| 6839 | if (buf->content == NULL) { |
| 6840 | #ifdef DEBUG_BUFFER |
| 6841 | xmlGenericError(xmlGenericErrorContext, |
| 6842 | "xmlBufferDump: buf->content == NULL\n"); |
| 6843 | #endif |
| 6844 | return(0); |
| 6845 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 6846 | if (file == NULL) |
| 6847 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6848 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 6849 | return(ret); |
| 6850 | } |
| 6851 | |
| 6852 | /** |
| 6853 | * xmlBufferContent: |
| 6854 | * @buf: the buffer |
| 6855 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6856 | * Function to extract the content of a buffer |
| 6857 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6858 | * Returns the internal content |
| 6859 | */ |
| 6860 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6861 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6862 | xmlBufferContent(const xmlBufferPtr buf) |
| 6863 | { |
| 6864 | if(!buf) |
| 6865 | return NULL; |
| 6866 | |
| 6867 | return buf->content; |
| 6868 | } |
| 6869 | |
| 6870 | /** |
| 6871 | * xmlBufferLength: |
| 6872 | * @buf: the buffer |
| 6873 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6874 | * Function to get the length of a buffer |
| 6875 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6876 | * Returns the length of data in the internal content |
| 6877 | */ |
| 6878 | |
| 6879 | int |
| 6880 | xmlBufferLength(const xmlBufferPtr buf) |
| 6881 | { |
| 6882 | if(!buf) |
| 6883 | return 0; |
| 6884 | |
| 6885 | return buf->use; |
| 6886 | } |
| 6887 | |
| 6888 | /** |
| 6889 | * xmlBufferResize: |
| 6890 | * @buf: the buffer to resize |
| 6891 | * @size: the desired size |
| 6892 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6893 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6894 | * |
| 6895 | * Returns 0 in case of problems, 1 otherwise |
| 6896 | */ |
| 6897 | int |
| 6898 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 6899 | { |
| 6900 | unsigned int newSize; |
| 6901 | xmlChar* rebuf = NULL; |
| 6902 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6903 | if (buf == NULL) |
| 6904 | return(0); |
| 6905 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6906 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
| 6907 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6908 | /* Don't resize if we don't have to */ |
| 6909 | if (size < buf->size) |
| 6910 | return 1; |
| 6911 | |
| 6912 | /* figure out new size */ |
| 6913 | switch (buf->alloc){ |
| 6914 | case XML_BUFFER_ALLOC_DOUBLEIT: |
Daniel Veillard | bf62949 | 2004-04-20 22:20:59 +0000 | [diff] [blame] | 6915 | /*take care of empty case*/ |
| 6916 | newSize = (buf->size ? buf->size*2 : size + 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6917 | while (size > newSize) newSize *= 2; |
| 6918 | break; |
| 6919 | case XML_BUFFER_ALLOC_EXACT: |
| 6920 | newSize = size+10; |
| 6921 | break; |
| 6922 | default: |
| 6923 | newSize = size+10; |
| 6924 | break; |
| 6925 | } |
| 6926 | |
| 6927 | if (buf->content == NULL) |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6928 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6929 | else if (buf->size - buf->use < 100) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6930 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 6931 | newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6932 | } else { |
| 6933 | /* |
| 6934 | * if we are reallocating a buffer far from being full, it's |
| 6935 | * better to make a new allocation and copy only the used range |
| 6936 | * and free the old one. |
| 6937 | */ |
| 6938 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
| 6939 | if (rebuf != NULL) { |
| 6940 | memcpy(rebuf, buf->content, buf->use); |
| 6941 | xmlFree(buf->content); |
William M. Brack | 42331a9 | 2004-07-29 07:07:16 +0000 | [diff] [blame] | 6942 | rebuf[buf->use] = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6943 | } |
| 6944 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6945 | if (rebuf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6946 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6947 | return 0; |
| 6948 | } |
| 6949 | buf->content = rebuf; |
| 6950 | buf->size = newSize; |
| 6951 | |
| 6952 | return 1; |
| 6953 | } |
| 6954 | |
| 6955 | /** |
| 6956 | * xmlBufferAdd: |
| 6957 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6958 | * @str: the #xmlChar string |
| 6959 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6960 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6961 | * 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] | 6962 | * str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6963 | * |
| 6964 | * Returns 0 successful, a positive error code number otherwise |
| 6965 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6966 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6967 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6968 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6969 | unsigned int needSize; |
| 6970 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6971 | if ((str == NULL) || (buf == NULL)) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6972 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6973 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6974 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6975 | if (len < -1) { |
| 6976 | #ifdef DEBUG_BUFFER |
| 6977 | xmlGenericError(xmlGenericErrorContext, |
| 6978 | "xmlBufferAdd: len < 0\n"); |
| 6979 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6980 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6981 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6982 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6983 | |
| 6984 | if (len < 0) |
| 6985 | len = xmlStrlen(str); |
| 6986 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6987 | if (len <= 0) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6988 | |
| 6989 | needSize = buf->use + len + 2; |
| 6990 | if (needSize > buf->size){ |
| 6991 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6992 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6993 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6994 | } |
| 6995 | } |
| 6996 | |
| 6997 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 6998 | buf->use += len; |
| 6999 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7000 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7001 | } |
| 7002 | |
| 7003 | /** |
| 7004 | * xmlBufferAddHead: |
| 7005 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7006 | * @str: the #xmlChar string |
| 7007 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7008 | * |
| 7009 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7010 | * if len == -1, the length of @str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7011 | * |
| 7012 | * Returns 0 successful, a positive error code number otherwise |
| 7013 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7014 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7015 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7016 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 7017 | unsigned int needSize; |
| 7018 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7019 | if (buf == NULL) |
| 7020 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7021 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7022 | if (str == NULL) { |
| 7023 | #ifdef DEBUG_BUFFER |
| 7024 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7025 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7026 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7027 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7028 | } |
| 7029 | if (len < -1) { |
| 7030 | #ifdef DEBUG_BUFFER |
| 7031 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7032 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7033 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7034 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7035 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7036 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7037 | |
| 7038 | if (len < 0) |
| 7039 | len = xmlStrlen(str); |
| 7040 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7041 | if (len <= 0) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7042 | |
| 7043 | needSize = buf->use + len + 2; |
| 7044 | if (needSize > buf->size){ |
| 7045 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7046 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7047 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7048 | } |
| 7049 | } |
| 7050 | |
| 7051 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 7052 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 7053 | buf->use += len; |
| 7054 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7055 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7056 | } |
| 7057 | |
| 7058 | /** |
| 7059 | * xmlBufferCat: |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7060 | * @buf: the buffer to add to |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7061 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7062 | * |
| 7063 | * Append a zero terminated string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7064 | * |
| 7065 | * Returns 0 successful, a positive error code number otherwise |
| 7066 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7067 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7068 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7069 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7070 | if (buf == NULL) |
| 7071 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7072 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
| 7073 | if (str == NULL) return -1; |
| 7074 | return xmlBufferAdd(buf, str, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7075 | } |
| 7076 | |
| 7077 | /** |
| 7078 | * xmlBufferCCat: |
| 7079 | * @buf: the buffer to dump |
| 7080 | * @str: the C char string |
| 7081 | * |
| 7082 | * Append a zero terminated C string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7083 | * |
| 7084 | * Returns 0 successful, a positive error code number otherwise |
| 7085 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7086 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7087 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7088 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 7089 | const char *cur; |
| 7090 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7091 | if (buf == NULL) |
| 7092 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7093 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7094 | if (str == NULL) { |
| 7095 | #ifdef DEBUG_BUFFER |
| 7096 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7097 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7098 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7099 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7100 | } |
| 7101 | for (cur = str;*cur != 0;cur++) { |
| 7102 | if (buf->use + 10 >= buf->size) { |
| 7103 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7104 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7105 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7106 | } |
| 7107 | } |
| 7108 | buf->content[buf->use++] = *cur; |
| 7109 | } |
| 7110 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7111 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7112 | } |
| 7113 | |
| 7114 | /** |
| 7115 | * xmlBufferWriteCHAR: |
| 7116 | * @buf: the XML buffer |
| 7117 | * @string: the string to add |
| 7118 | * |
| 7119 | * routine which manages and grows an output buffer. This one adds |
| 7120 | * xmlChars at the end of the buffer. |
| 7121 | */ |
| 7122 | void |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7123 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7124 | if (buf == NULL) |
| 7125 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7126 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7127 | xmlBufferCat(buf, string); |
| 7128 | } |
| 7129 | |
| 7130 | /** |
| 7131 | * xmlBufferWriteChar: |
| 7132 | * @buf: the XML buffer output |
| 7133 | * @string: the string to add |
| 7134 | * |
| 7135 | * routine which manage and grows an output buffer. This one add |
| 7136 | * C chars at the end of the array. |
| 7137 | */ |
| 7138 | void |
| 7139 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7140 | if (buf == NULL) |
| 7141 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7142 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7143 | xmlBufferCCat(buf, string); |
| 7144 | } |
| 7145 | |
| 7146 | |
| 7147 | /** |
| 7148 | * xmlBufferWriteQuotedString: |
| 7149 | * @buf: the XML buffer output |
| 7150 | * @string: the string to add |
| 7151 | * |
| 7152 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7153 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7154 | * quote or double-quotes internally |
| 7155 | */ |
| 7156 | void |
| 7157 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7158 | const xmlChar *cur, *base; |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7159 | if (buf == NULL) |
| 7160 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7161 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7162 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 7163 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7164 | #ifdef DEBUG_BUFFER |
| 7165 | xmlGenericError(xmlGenericErrorContext, |
| 7166 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 7167 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7168 | xmlBufferCCat(buf, "\""); |
| 7169 | base = cur = string; |
| 7170 | while(*cur != 0){ |
| 7171 | if(*cur == '"'){ |
| 7172 | if (base != cur) |
| 7173 | xmlBufferAdd(buf, base, cur - base); |
| 7174 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 7175 | cur++; |
| 7176 | base = cur; |
| 7177 | } |
| 7178 | else { |
| 7179 | cur++; |
| 7180 | } |
| 7181 | } |
| 7182 | if (base != cur) |
| 7183 | xmlBufferAdd(buf, base, cur - base); |
| 7184 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7185 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7186 | else{ |
| 7187 | xmlBufferCCat(buf, "\'"); |
| 7188 | xmlBufferCat(buf, string); |
| 7189 | xmlBufferCCat(buf, "\'"); |
| 7190 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7191 | } else { |
| 7192 | xmlBufferCCat(buf, "\""); |
| 7193 | xmlBufferCat(buf, string); |
| 7194 | xmlBufferCCat(buf, "\""); |
| 7195 | } |
| 7196 | } |
| 7197 | |
| 7198 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7199 | /** |
| 7200 | * xmlGetDocCompressMode: |
| 7201 | * @doc: the document |
| 7202 | * |
| 7203 | * get the compression ratio for a document, ZLIB based |
| 7204 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7205 | */ |
| 7206 | int |
| 7207 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 7208 | if (doc == NULL) return(-1); |
| 7209 | return(doc->compression); |
| 7210 | } |
| 7211 | |
| 7212 | /** |
| 7213 | * xmlSetDocCompressMode: |
| 7214 | * @doc: the document |
| 7215 | * @mode: the compression ratio |
| 7216 | * |
| 7217 | * set the compression ratio for a document, ZLIB based |
| 7218 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7219 | */ |
| 7220 | void |
| 7221 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 7222 | if (doc == NULL) return; |
| 7223 | if (mode < 0) doc->compression = 0; |
| 7224 | else if (mode > 9) doc->compression = 9; |
| 7225 | else doc->compression = mode; |
| 7226 | } |
| 7227 | |
| 7228 | /** |
| 7229 | * xmlGetCompressMode: |
| 7230 | * |
| 7231 | * get the default compression mode used, ZLIB based. |
| 7232 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7233 | */ |
| 7234 | int |
| 7235 | xmlGetCompressMode(void) |
| 7236 | { |
| 7237 | return (xmlCompressMode); |
| 7238 | } |
| 7239 | |
| 7240 | /** |
| 7241 | * xmlSetCompressMode: |
| 7242 | * @mode: the compression ratio |
| 7243 | * |
| 7244 | * set the default compression mode used, ZLIB based |
| 7245 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7246 | */ |
| 7247 | void |
| 7248 | xmlSetCompressMode(int mode) { |
| 7249 | if (mode < 0) xmlCompressMode = 0; |
| 7250 | else if (mode > 9) xmlCompressMode = 9; |
| 7251 | else xmlCompressMode = mode; |
| 7252 | } |
| 7253 | |
Kasimier T. Buchcik | 4d9c948 | 2005-06-27 15:04:46 +0000 | [diff] [blame] | 7254 | /* |
| 7255 | * xmlDOMWrapNewCtxt: |
| 7256 | * |
| 7257 | * Allocates and initializes a new DOM-wrapper context. |
| 7258 | * |
| 7259 | * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror. |
| 7260 | */ |
| 7261 | xmlDOMWrapCtxtPtr |
| 7262 | xmlDOMWrapNewCtxt(void) |
| 7263 | { |
| 7264 | xmlDOMWrapCtxtPtr ret; |
| 7265 | |
| 7266 | ret = xmlMalloc(sizeof(xmlDOMWrapCtxt)); |
| 7267 | if (ret == NULL) { |
| 7268 | xmlTreeErrMemory("allocating DOM-wrapper context"); |
| 7269 | return (NULL); |
| 7270 | } |
| 7271 | memset(ret, 0, sizeof(xmlDOMWrapCtxt)); |
| 7272 | return (ret); |
| 7273 | } |
| 7274 | |
| 7275 | /* |
| 7276 | * xmlDOMWrapFreeCtxt: |
| 7277 | * @ctxt: the DOM-wrapper context |
| 7278 | * |
| 7279 | * Frees the DOM-wrapper context. |
| 7280 | */ |
| 7281 | void |
| 7282 | xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt) |
| 7283 | { |
| 7284 | if (ctxt == NULL) |
| 7285 | return; |
| 7286 | xmlFree(ctxt); |
| 7287 | } |
| 7288 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7289 | #define XML_TREE_NSMAP_PARENT -1 |
| 7290 | #define XML_TREE_NSMAP_XML -2 |
| 7291 | #define XML_TREE_NSMAP_DOC -3 |
| 7292 | #define XML_TREE_NSMAP_CUSTOM -4 |
| 7293 | |
| 7294 | typedef struct xmlNsMapItem *xmlNsMapItemPtr; |
| 7295 | struct xmlNsMapItem { |
| 7296 | xmlNsMapItemPtr next; |
| 7297 | xmlNsMapItemPtr prev; |
| 7298 | xmlNsPtr oldNs; /* old ns decl reference */ |
| 7299 | xmlNsPtr newNs; /* new ns decl reference */ |
| 7300 | int shadowDepth; /* Shadowed at this depth */ |
| 7301 | /* |
| 7302 | * depth: |
| 7303 | * >= 0 == @node's ns-decls |
| 7304 | * -1 == @parent's ns-decls |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7305 | * -2 == the doc->oldNs XML ns-decl |
| 7306 | * -3 == the doc->oldNs storage ns-decls |
| 7307 | * -4 == ns-decls provided via custom ns-handling |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7308 | */ |
| 7309 | int depth; |
| 7310 | }; |
| 7311 | |
| 7312 | /* |
| 7313 | * xmlTreeAddNsMapItem: |
| 7314 | * @map: the ns-map |
| 7315 | * @cur: the current map entry to append a new entry to |
| 7316 | * @oldNs: the old ns-struct |
| 7317 | * @newNs: the new ns-struct |
| 7318 | * @depth: depth and ns-kind information |
| 7319 | * |
| 7320 | * Frees the ns-map |
| 7321 | */ |
| 7322 | static xmlNsMapItemPtr |
| 7323 | xmlDOMWrapNSNormAddNsMapItem(xmlNsMapItemPtr *map, |
| 7324 | xmlNsMapItemPtr *cur, |
| 7325 | xmlNsPtr oldNs, |
| 7326 | xmlNsPtr newNs, |
| 7327 | int depth) |
| 7328 | { |
| 7329 | xmlNsMapItemPtr ret; |
| 7330 | |
| 7331 | if ((cur != NULL) && (*cur != NULL) && ((*cur)->next != NULL)) { |
| 7332 | /* |
| 7333 | * Reuse. |
| 7334 | */ |
| 7335 | ret = (*cur)->next; |
| 7336 | *cur = ret; |
| 7337 | } else { |
| 7338 | ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem)); |
| 7339 | if (ret == NULL) { |
| 7340 | xmlTreeErrMemory("allocating namespace map item"); |
| 7341 | return (NULL); |
| 7342 | } |
| 7343 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
| 7344 | if (*map == NULL) { |
| 7345 | /* |
| 7346 | * First ever. |
| 7347 | */ |
| 7348 | *map = ret; |
| 7349 | ret->prev = ret; |
| 7350 | if (cur != NULL) |
| 7351 | *cur = ret; |
| 7352 | } else { |
| 7353 | if (cur) { |
| 7354 | /* |
| 7355 | * Append. |
| 7356 | */ |
| 7357 | (*cur)->next = ret; |
| 7358 | ret->prev = *cur; |
| 7359 | *cur = ret; |
| 7360 | } else { |
| 7361 | /* |
| 7362 | * Set on first position. |
| 7363 | */ |
| 7364 | ret->next = (*map); |
| 7365 | ret->prev = (*map)->prev; |
| 7366 | (*map)->prev = ret; |
| 7367 | *map = ret; |
| 7368 | } |
| 7369 | } |
| 7370 | } |
| 7371 | ret->oldNs = oldNs; |
| 7372 | ret->newNs = newNs; |
| 7373 | ret->shadowDepth = -1; |
| 7374 | ret->depth = depth; |
| 7375 | return (ret); |
| 7376 | } |
| 7377 | |
| 7378 | /* |
| 7379 | * xmlTreeFreeNsMap: |
| 7380 | * @map: the ns-map |
| 7381 | * |
| 7382 | * Frees the ns-map |
| 7383 | */ |
| 7384 | static void |
| 7385 | xmlDOMWrapNSNormFreeNsMap(xmlNsMapItemPtr map) |
| 7386 | { |
| 7387 | xmlNsMapItemPtr mi = map, miprev; |
| 7388 | |
| 7389 | while (mi != NULL) { |
| 7390 | miprev = mi; |
| 7391 | mi = mi->next; |
| 7392 | xmlFree(miprev); |
| 7393 | } |
| 7394 | } |
| 7395 | |
| 7396 | /* |
| 7397 | * xmlTreeEnsureXMLDecl: |
| 7398 | * @doc: the doc |
| 7399 | * |
| 7400 | * Ensures that there is an XML namespace declaration on the doc. |
| 7401 | * |
| 7402 | * Returns the XML ns-struct or NULL on API and internal errors. |
| 7403 | */ |
| 7404 | static xmlNsPtr |
| 7405 | xmlTreeEnsureXMLDecl(xmlDocPtr doc) |
| 7406 | { |
| 7407 | if (doc == NULL) |
| 7408 | return (NULL); |
| 7409 | if (doc->oldNs != NULL) |
| 7410 | return (doc->oldNs); |
| 7411 | { |
| 7412 | xmlNsPtr ns; |
| 7413 | ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 7414 | if (ns == NULL) { |
| 7415 | xmlTreeErrMemory( |
| 7416 | "allocating the XML namespace"); |
| 7417 | return (NULL); |
| 7418 | } |
| 7419 | memset(ns, 0, sizeof(xmlNs)); |
| 7420 | ns->type = XML_LOCAL_NAMESPACE; |
| 7421 | ns->href = xmlStrdup(XML_XML_NAMESPACE); |
| 7422 | ns->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 7423 | doc->oldNs = ns; |
| 7424 | return (ns); |
| 7425 | } |
| 7426 | } |
| 7427 | |
| 7428 | /* |
| 7429 | * xmlDOMWrapStoreNs: |
| 7430 | * @doc: the doc |
| 7431 | * @nsName: the namespace name |
| 7432 | * @prefix: the prefix |
| 7433 | * |
| 7434 | * Creates or reuses an xmlNs struct on doc->oldNs with |
| 7435 | * the given prefix and namespace name. |
| 7436 | * |
| 7437 | * Returns the aquired ns struct or NULL in case of an API |
| 7438 | * or internal error. |
| 7439 | */ |
| 7440 | static xmlNsPtr |
| 7441 | xmlDOMWrapStoreNs(xmlDocPtr doc, |
| 7442 | const xmlChar *nsName, |
| 7443 | const xmlChar *prefix) |
| 7444 | { |
| 7445 | xmlNsPtr ns; |
| 7446 | |
| 7447 | if (doc == NULL) |
| 7448 | return (NULL); |
| 7449 | ns = xmlTreeEnsureXMLDecl(doc); |
| 7450 | if (ns == NULL) |
| 7451 | return (NULL); |
| 7452 | if (ns->next != NULL) { |
| 7453 | /* Reuse. */ |
| 7454 | ns = ns->next; |
| 7455 | while (ns != NULL) { |
| 7456 | if (((ns->prefix == prefix) || |
| 7457 | xmlStrEqual(ns->prefix, prefix)) && |
| 7458 | xmlStrEqual(ns->href, nsName)) { |
| 7459 | return (ns); |
| 7460 | } |
| 7461 | if (ns->next == NULL) |
| 7462 | break; |
| 7463 | ns = ns->next; |
| 7464 | } |
| 7465 | } |
| 7466 | /* Create. */ |
| 7467 | ns->next = xmlNewNs(NULL, nsName, prefix); |
| 7468 | return (ns->next); |
| 7469 | } |
| 7470 | |
| 7471 | /* |
| 7472 | * xmlTreeLookupNsListByPrefix: |
| 7473 | * @nsList: a list of ns-structs |
| 7474 | * @prefix: the searched prefix |
| 7475 | * |
| 7476 | * Searches for a ns-decl with the given prefix in @nsList. |
| 7477 | * |
| 7478 | * Returns the ns-decl if found, NULL if not found and on |
| 7479 | * API errors. |
| 7480 | */ |
| 7481 | static xmlNsPtr |
| 7482 | xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix) |
| 7483 | { |
| 7484 | if (nsList == NULL) |
| 7485 | return (NULL); |
| 7486 | { |
| 7487 | xmlNsPtr ns; |
| 7488 | ns = nsList; |
| 7489 | do { |
| 7490 | if ((prefix == ns->prefix) || |
| 7491 | xmlStrEqual(prefix, ns->prefix)) { |
| 7492 | return (ns); |
| 7493 | } |
| 7494 | ns = ns->next; |
| 7495 | } while (ns != NULL); |
| 7496 | } |
| 7497 | return (NULL); |
| 7498 | } |
| 7499 | |
| 7500 | /* |
| 7501 | * |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7502 | * xmlDOMWrapNSNormGatherInScopeNs: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7503 | * @map: the namespace map |
| 7504 | * @node: the node to start with |
| 7505 | * |
| 7506 | * Puts in-scope namespaces into the ns-map. |
| 7507 | * |
| 7508 | * Returns 0 on success, -1 on API or internal errors. |
| 7509 | */ |
| 7510 | static int |
| 7511 | xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapItemPtr *map, |
| 7512 | xmlNodePtr node) |
| 7513 | { |
| 7514 | xmlNodePtr cur; |
| 7515 | xmlNsPtr ns; |
| 7516 | xmlNsMapItemPtr mi; |
| 7517 | int shadowed; |
| 7518 | |
| 7519 | if ((map == NULL) || (*map != NULL)) |
| 7520 | return (-1); |
| 7521 | /* |
| 7522 | * Get in-scope ns-decls of @parent. |
| 7523 | */ |
| 7524 | cur = node; |
| 7525 | while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) { |
| 7526 | if (cur->type == XML_ELEMENT_NODE) { |
| 7527 | if (cur->nsDef != NULL) { |
| 7528 | ns = cur->nsDef; |
| 7529 | do { |
| 7530 | shadowed = 0; |
| 7531 | if (*map != NULL) { |
| 7532 | /* |
| 7533 | * Skip shadowed prefixes. |
| 7534 | */ |
| 7535 | for (mi = *map; mi != NULL; mi = mi->next) { |
| 7536 | if ((ns->prefix == mi->newNs->prefix) || |
| 7537 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) { |
| 7538 | shadowed = 1; |
| 7539 | break; |
| 7540 | } |
| 7541 | } |
| 7542 | } |
| 7543 | /* |
| 7544 | * Insert mapping. |
| 7545 | */ |
| 7546 | mi = xmlDOMWrapNSNormAddNsMapItem(map, NULL, NULL, |
| 7547 | ns, XML_TREE_NSMAP_PARENT); |
| 7548 | if (mi == NULL) |
| 7549 | return (-1); |
| 7550 | if (shadowed) |
| 7551 | mi->shadowDepth = 0; |
| 7552 | ns = ns->next; |
| 7553 | } while (ns != NULL); |
| 7554 | } |
| 7555 | } |
| 7556 | cur = cur->parent; |
| 7557 | } |
| 7558 | return (0); |
| 7559 | } |
| 7560 | |
| 7561 | /* |
| 7562 | * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict; |
| 7563 | * otherwise copy it, when it was in the source-dict. |
| 7564 | */ |
| 7565 | #define XML_TREE_ADOPT_STR(str) \ |
| 7566 | if (adoptStr && (str != NULL)) { \ |
| 7567 | if (destDoc->dict) { \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7568 | const xmlChar *old = str; \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7569 | str = xmlDictLookup(destDoc->dict, str, -1); \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7570 | if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \ |
| 7571 | (!xmlDictOwns(sourceDoc->dict, old))) \ |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 7572 | xmlFree((char *)old); \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7573 | } else if ((sourceDoc) && (sourceDoc->dict) && \ |
| 7574 | xmlDictOwns(sourceDoc->dict, str)) { \ |
| 7575 | str = BAD_CAST xmlStrdup(str); \ |
| 7576 | } \ |
| 7577 | } |
| 7578 | |
| 7579 | /* |
| 7580 | * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then |
| 7581 | * put it in dest-dict or copy it. |
| 7582 | */ |
| 7583 | #define XML_TREE_ADOPT_STR_2(str) \ |
| 7584 | if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \ |
| 7585 | (sourceDoc->dict != NULL) && \ |
| 7586 | xmlDictOwns(sourceDoc->dict, cur->content)) { \ |
| 7587 | if (destDoc->dict) \ |
| 7588 | cur->content = (xmlChar *) \ |
| 7589 | xmlDictLookup(destDoc->dict, cur->content, -1); \ |
| 7590 | else \ |
| 7591 | cur->content = xmlStrdup(BAD_CAST cur->content); \ |
| 7592 | } |
| 7593 | |
| 7594 | /* |
| 7595 | * xmlDOMWrapNSNormAddNsMapItem2: |
| 7596 | * |
| 7597 | * For internal use. Adds a ns-decl mapping. |
| 7598 | * |
| 7599 | * Returns 0 on success, -1 on internal errors. |
| 7600 | */ |
| 7601 | static int |
| 7602 | xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number, |
| 7603 | xmlNsPtr oldNs, xmlNsPtr newNs) |
| 7604 | { |
| 7605 | if (*list == NULL) { |
| 7606 | *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr)); |
| 7607 | if (*list == NULL) { |
| 7608 | xmlTreeErrMemory("alloc ns map item"); |
| 7609 | return(-1); |
| 7610 | } |
| 7611 | *size = 3; |
| 7612 | *number = 0; |
| 7613 | } else if ((*number) >= (*size)) { |
| 7614 | *size *= 2; |
| 7615 | *list = (xmlNsPtr *) xmlRealloc(*list, |
| 7616 | (*size) * 2 * sizeof(xmlNsPtr)); |
| 7617 | if (*list == NULL) { |
| 7618 | xmlTreeErrMemory("realloc ns map item"); |
| 7619 | return(-1); |
| 7620 | } |
| 7621 | } |
| 7622 | (*list)[2 * (*number)] = oldNs; |
| 7623 | (*list)[2 * (*number) +1] = newNs; |
| 7624 | (*number)++; |
| 7625 | return (0); |
| 7626 | } |
| 7627 | |
| 7628 | /* |
| 7629 | * xmlDOMWrapRemoveNode: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7630 | * @ctxt: a DOM wrapper context |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7631 | * @doc: the doc |
| 7632 | * @node: the node to be removed. |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7633 | * @options: set of options, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7634 | * |
| 7635 | * Unlinks the given node from its owner. |
| 7636 | * This will substitute ns-references to node->nsDef for |
| 7637 | * ns-references to doc->oldNs, thus ensuring the removed |
| 7638 | * branch to be autark wrt ns-references. |
Kasimier T. Buchcik | 017264f | 2005-06-27 13:45:24 +0000 | [diff] [blame] | 7639 | * WARNING: This function is in a experimental state. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7640 | * |
| 7641 | * Returns 0 on success, 1 if the node is not supported, |
| 7642 | * -1 on API and internal errors. |
| 7643 | */ |
| 7644 | int |
| 7645 | xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc, |
| 7646 | xmlNodePtr node, int options ATTRIBUTE_UNUSED) |
| 7647 | { |
| 7648 | xmlNsPtr *list = NULL; |
| 7649 | int sizeList, nbList, i, j; |
| 7650 | xmlNsPtr ns; |
| 7651 | |
| 7652 | if ((node == NULL) || (doc == NULL) || (node->doc != doc)) |
| 7653 | return (-1); |
| 7654 | |
| 7655 | /* TODO: 0 or -1 ? */ |
| 7656 | if (node->parent == NULL) |
| 7657 | return (0); |
| 7658 | |
| 7659 | switch (node->type) { |
| 7660 | case XML_TEXT_NODE: |
| 7661 | case XML_CDATA_SECTION_NODE: |
| 7662 | case XML_ENTITY_REF_NODE: |
| 7663 | case XML_PI_NODE: |
| 7664 | case XML_COMMENT_NODE: |
| 7665 | xmlUnlinkNode(node); |
| 7666 | return (0); |
| 7667 | case XML_ELEMENT_NODE: |
| 7668 | case XML_ATTRIBUTE_NODE: |
| 7669 | break; |
| 7670 | default: |
| 7671 | return (1); |
| 7672 | } |
| 7673 | xmlUnlinkNode(node); |
| 7674 | /* |
| 7675 | * Save out-of-scope ns-references in doc->oldNs. |
| 7676 | */ |
| 7677 | do { |
| 7678 | switch (node->type) { |
| 7679 | case XML_ELEMENT_NODE: |
| 7680 | if ((ctxt == NULL) && (node->nsDef != NULL)) { |
| 7681 | ns = node->nsDef; |
| 7682 | do { |
| 7683 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 7684 | &nbList, ns, ns) == -1) |
| 7685 | goto internal_error; |
| 7686 | ns = ns->next; |
| 7687 | } while (ns != NULL); |
| 7688 | } |
| 7689 | /* No break on purpose. */ |
| 7690 | case XML_ATTRIBUTE_NODE: |
| 7691 | if (node->ns != NULL) { |
| 7692 | /* |
| 7693 | * Find a mapping. |
| 7694 | */ |
| 7695 | if (list != NULL) { |
| 7696 | for (i = 0, j = 0; i < nbList; i++, j += 2) { |
| 7697 | if (node->ns == list[j]) { |
| 7698 | node->ns = list[++j]; |
| 7699 | goto next_node; |
| 7700 | } |
| 7701 | } |
| 7702 | } |
| 7703 | ns = NULL; |
| 7704 | if (ctxt != NULL) { |
| 7705 | /* |
| 7706 | * User defined. |
| 7707 | */ |
| 7708 | } else { |
| 7709 | /* |
| 7710 | * Add to doc's oldNs. |
| 7711 | */ |
| 7712 | ns = xmlDOMWrapStoreNs(doc, node->ns->href, |
| 7713 | node->ns->prefix); |
| 7714 | if (ns == NULL) |
| 7715 | goto internal_error; |
| 7716 | } |
| 7717 | if (ns != NULL) { |
| 7718 | /* |
| 7719 | * Add mapping. |
| 7720 | */ |
| 7721 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 7722 | &nbList, node->ns, ns) == -1) |
| 7723 | goto internal_error; |
| 7724 | } |
| 7725 | node->ns = ns; |
| 7726 | } |
| 7727 | if ((node->type == XML_ELEMENT_NODE) && |
| 7728 | (node->properties != NULL)) { |
| 7729 | node = (xmlNodePtr) node->properties; |
| 7730 | continue; |
| 7731 | } |
| 7732 | break; |
| 7733 | default: |
| 7734 | goto next_sibling; |
| 7735 | } |
| 7736 | next_node: |
| 7737 | if ((node->type == XML_ELEMENT_NODE) && |
| 7738 | (node->children != NULL)) { |
| 7739 | node = node->children; |
| 7740 | continue; |
| 7741 | } |
| 7742 | next_sibling: |
| 7743 | if (node == NULL) |
| 7744 | break; |
| 7745 | if (node->next != NULL) |
| 7746 | node = node->next; |
| 7747 | else { |
| 7748 | node = node->parent; |
| 7749 | goto next_sibling; |
| 7750 | } |
| 7751 | } while (node != NULL); |
| 7752 | |
| 7753 | if (list != NULL) |
| 7754 | xmlFree(list); |
| 7755 | return (0); |
| 7756 | |
| 7757 | internal_error: |
| 7758 | if (list != NULL) |
| 7759 | xmlFree(list); |
| 7760 | return (-1); |
| 7761 | } |
| 7762 | |
| 7763 | /* |
| 7764 | * xmlSearchNsByHrefStrict: |
| 7765 | * @doc: the document |
| 7766 | * @node: the start node |
| 7767 | * @nsName: the searched namespace name |
| 7768 | * @retNs: the resulting ns-decl |
| 7769 | * @prefixed: if the found ns-decl must have a prefix (for attributes) |
| 7770 | * |
| 7771 | * Dynamically searches for a ns-declaration which matches |
| 7772 | * the given @nsName in the ancestor-or-self axis of @node. |
| 7773 | * |
| 7774 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 7775 | * and internal errors. |
| 7776 | */ |
| 7777 | static int |
| 7778 | xmlSearchNsByHrefStrict(xmlDocPtr doc, xmlNodePtr node, const xmlChar* nsName, |
| 7779 | xmlNsPtr *retNs, int prefixed) |
| 7780 | { |
| 7781 | xmlNodePtr cur, prev = NULL, out = NULL; |
| 7782 | xmlNsPtr ns, prevns; |
| 7783 | |
| 7784 | if ((doc == NULL) || (nsName == NULL) || (retNs == NULL)) |
| 7785 | return (-1); |
| 7786 | |
| 7787 | *retNs = NULL; |
| 7788 | if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
| 7789 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 7790 | if (*retNs == NULL) |
| 7791 | return (-1); |
| 7792 | return (1); |
| 7793 | } |
| 7794 | cur = node; |
| 7795 | do { |
| 7796 | if (cur->type == XML_ELEMENT_NODE) { |
| 7797 | if (cur->nsDef != NULL) { |
| 7798 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 7799 | if (prefixed && (ns->prefix == NULL)) |
| 7800 | continue; |
| 7801 | if (prev != NULL) { |
| 7802 | /* |
| 7803 | * Check the last level of ns-decls for a |
| 7804 | * shadowing prefix. |
| 7805 | */ |
| 7806 | prevns = prev->nsDef; |
| 7807 | do { |
| 7808 | if ((prevns->prefix == ns->prefix) || |
| 7809 | ((prevns->prefix != NULL) && |
| 7810 | (ns->prefix != NULL) && |
| 7811 | xmlStrEqual(prevns->prefix, ns->prefix))) { |
| 7812 | /* |
| 7813 | * Shadowed. |
| 7814 | */ |
| 7815 | break; |
| 7816 | } |
| 7817 | prevns = prevns->next; |
| 7818 | } while (prevns != NULL); |
| 7819 | if (prevns != NULL) |
| 7820 | continue; |
| 7821 | } |
| 7822 | /* |
| 7823 | * Ns-name comparison. |
| 7824 | */ |
| 7825 | if ((nsName == ns->href) || |
| 7826 | xmlStrEqual(nsName, ns->href)) { |
| 7827 | /* |
| 7828 | * At this point the prefix can only be shadowed, |
| 7829 | * if we are the the (at least) 3rd level of |
| 7830 | * ns-decls. |
| 7831 | */ |
| 7832 | if (out) { |
| 7833 | int ret; |
| 7834 | |
| 7835 | ret = xmlNsInScope(doc, node, prev, ns->prefix); |
| 7836 | if (ret < 0) |
| 7837 | return (-1); |
| 7838 | /* |
| 7839 | * TODO: Should we try to find a matching ns-name |
| 7840 | * only once? This here keeps on searching. |
| 7841 | * I think we should try further since, there might |
| 7842 | * be an other matching ns-decl with an unshadowed |
| 7843 | * prefix. |
| 7844 | */ |
| 7845 | if (! ret) |
| 7846 | continue; |
| 7847 | } |
| 7848 | *retNs = ns; |
| 7849 | return (1); |
| 7850 | } |
| 7851 | } |
| 7852 | out = prev; |
| 7853 | prev = cur; |
| 7854 | } |
| 7855 | } else if ((node->type == XML_ENTITY_REF_NODE) || |
| 7856 | (node->type == XML_ENTITY_NODE) || |
| 7857 | (node->type == XML_ENTITY_DECL)) |
| 7858 | return (0); |
| 7859 | cur = cur->parent; |
| 7860 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 7861 | return (0); |
| 7862 | } |
| 7863 | |
| 7864 | /* |
| 7865 | * xmlDOMWrapNSNormDeclareNsForced: |
| 7866 | * @doc: the doc |
| 7867 | * @elem: the element-node to declare on |
| 7868 | * @nsName: the namespace-name of the ns-decl |
| 7869 | * @prefix: the preferred prefix of the ns-decl |
| 7870 | * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls |
| 7871 | * |
| 7872 | * Declares a new namespace on @elem. It tries to use the |
| 7873 | * given @prefix; if a ns-decl with the given prefix is already existent |
| 7874 | * on @elem, it will generate an other prefix. |
| 7875 | * |
| 7876 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 7877 | * and internal errors. |
| 7878 | */ |
| 7879 | static xmlNsPtr |
| 7880 | xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc, |
| 7881 | xmlNodePtr elem, |
| 7882 | const xmlChar *nsName, |
| 7883 | const xmlChar *prefix, |
| 7884 | int checkShadow) |
| 7885 | { |
| 7886 | |
| 7887 | xmlNsPtr ret; |
| 7888 | char buf[50]; |
| 7889 | const xmlChar *pref; |
| 7890 | int counter = 0; |
| 7891 | /* |
| 7892 | * Create a ns-decl on @anchor. |
| 7893 | */ |
| 7894 | pref = prefix; |
| 7895 | while (1) { |
| 7896 | /* |
| 7897 | * Lookup whether the prefix is unused in elem's ns-decls. |
| 7898 | */ |
| 7899 | if ((elem->nsDef != NULL) && |
| 7900 | (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL)) |
| 7901 | goto ns_next_prefix; |
| 7902 | if (checkShadow && elem->parent && |
| 7903 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 7904 | /* |
| 7905 | * Does it shadow ancestor ns-decls? |
| 7906 | */ |
| 7907 | if (xmlSearchNs(doc, elem->parent, pref) != NULL) |
| 7908 | goto ns_next_prefix; |
| 7909 | } |
| 7910 | ret = xmlNewNs(NULL, nsName, pref); |
| 7911 | if (ret == NULL) |
| 7912 | return (NULL); |
| 7913 | if (elem->nsDef == NULL) |
| 7914 | elem->nsDef = ret; |
| 7915 | else { |
| 7916 | xmlNsPtr ns2 = elem->nsDef; |
| 7917 | while (ns2->next != NULL) |
| 7918 | ns2 = ns2->next; |
| 7919 | ns2->next = ret; |
| 7920 | } |
| 7921 | return (ret); |
| 7922 | ns_next_prefix: |
| 7923 | counter++; |
| 7924 | if (counter > 1000) |
| 7925 | return (NULL); |
| 7926 | if (prefix == NULL) { |
| 7927 | snprintf((char *) buf, sizeof(buf), |
| 7928 | "default%d", counter); |
| 7929 | } else |
| 7930 | snprintf((char *) buf, sizeof(buf), |
| 7931 | "%.30s%d", (char *)prefix, counter); |
| 7932 | pref = BAD_CAST buf; |
| 7933 | } |
| 7934 | } |
| 7935 | |
| 7936 | /* |
| 7937 | * xmlDOMWrapNSNormAquireNormalizedNs: |
| 7938 | * @doc: the doc |
| 7939 | * @elem: the element-node to declare namespaces on |
| 7940 | * @ns: the ns-struct to use for the search |
| 7941 | * @retNs: the found/created ns-struct |
| 7942 | * @nsMap: the ns-map |
| 7943 | * @topmi: the last ns-map entry |
| 7944 | * @depth: the current tree depth |
| 7945 | * @ancestorsOnly: search in ancestor ns-decls only |
| 7946 | * @prefixed: if the searched ns-decl must have a prefix (for attributes) |
| 7947 | * |
| 7948 | * Searches for a matching ns-name in the ns-decls of @nsMap, if not |
| 7949 | * found it will either declare it on @elem, or store it in doc->oldNs. |
| 7950 | * If a new ns-decl needs to be declared on @elem, it tries to use the |
| 7951 | * @ns->prefix for it, if this prefix is already in use on @elem, it will |
| 7952 | * change the prefix or the new ns-decl. |
| 7953 | * |
| 7954 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 7955 | */ |
| 7956 | static int |
| 7957 | xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc, |
| 7958 | xmlNodePtr elem, |
| 7959 | xmlNsPtr ns, |
| 7960 | xmlNsPtr *retNs, |
| 7961 | xmlNsMapItemPtr *nsMap, |
| 7962 | xmlNsMapItemPtr *topmi, |
| 7963 | int depth, |
| 7964 | int ancestorsOnly, |
| 7965 | int prefixed) |
| 7966 | { |
| 7967 | xmlNsMapItemPtr mi; |
| 7968 | |
| 7969 | if ((doc == NULL) || (ns == NULL) || (retNs == NULL) || |
| 7970 | (nsMap == NULL) || (topmi == NULL)) |
| 7971 | return (-1); |
| 7972 | |
| 7973 | *retNs = NULL; |
| 7974 | /* |
| 7975 | * Handle XML namespace. |
| 7976 | */ |
| 7977 | if ((ns->prefix) && |
| 7978 | (ns->prefix[0] == 'x') && |
| 7979 | (ns->prefix[1] == 'm') && |
| 7980 | (ns->prefix[2] == 'l') && |
| 7981 | (ns->prefix[3] == 0)) { |
| 7982 | /* |
| 7983 | * Insert XML namespace mapping. |
| 7984 | */ |
| 7985 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 7986 | if (*retNs == NULL) |
| 7987 | return (-1); |
| 7988 | return (0); |
| 7989 | } |
| 7990 | /* |
| 7991 | * If the search should be done in ancestors only and no |
| 7992 | * @elem (the first ancestor) was specified, then skip the search. |
| 7993 | */ |
| 7994 | if ((! (ancestorsOnly && (elem == NULL))) && |
| 7995 | (*nsMap != NULL)) { |
| 7996 | |
| 7997 | /* |
| 7998 | * Try to find an equal ns-name in in-scope ns-decls. |
| 7999 | */ |
| 8000 | for (mi = *nsMap; mi != (*topmi)->next; mi = mi->next) { |
| 8001 | |
| 8002 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8003 | /* |
| 8004 | * This should be turned on to gain speed, if one knows |
| 8005 | * that the branch itself was already ns-wellformed and no |
| 8006 | * stale references existed. I.e. it searches in the ancestor |
| 8007 | * axis only. |
| 8008 | */ |
| 8009 | ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) && |
| 8010 | /* Skip shadowed prefixes. */ |
| 8011 | (mi->shadowDepth == -1) && |
| 8012 | /* Skip xmlns="" or xmlns:foo="". */ |
| 8013 | ((mi->newNs->href != NULL) && |
| 8014 | (mi->newNs->href[0] != 0)) && |
| 8015 | /* Ensure a prefix if wanted. */ |
| 8016 | ((! prefixed) || (mi->newNs->prefix != NULL)) && |
| 8017 | /* Equal ns name */ |
| 8018 | ((mi->newNs->href == ns->href) || |
| 8019 | xmlStrEqual(mi->newNs->href, ns->href))) { |
| 8020 | /* Set the mapping. */ |
| 8021 | mi->oldNs = ns; |
| 8022 | *retNs = mi->newNs; |
| 8023 | return (0); |
| 8024 | } |
| 8025 | } |
| 8026 | } |
| 8027 | /* |
| 8028 | * No luck, the namespace is out of scope or shadowed. |
| 8029 | */ |
| 8030 | if (elem == NULL) { |
| 8031 | xmlNsPtr tmpns; |
| 8032 | |
| 8033 | /* |
| 8034 | * Store ns-decls in "oldNs" of the document-node. |
| 8035 | */ |
| 8036 | tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix); |
| 8037 | if (tmpns == NULL) |
| 8038 | return (-1); |
| 8039 | /* |
| 8040 | * Insert mapping. |
| 8041 | */ |
| 8042 | if (xmlDOMWrapNSNormAddNsMapItem(nsMap, NULL, ns, |
| 8043 | tmpns, XML_TREE_NSMAP_DOC) == NULL) { |
| 8044 | xmlFreeNs(tmpns); |
| 8045 | return (-1); |
| 8046 | } |
| 8047 | *retNs = tmpns; |
| 8048 | } else { |
| 8049 | xmlNsPtr tmpns; |
| 8050 | |
| 8051 | tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href, |
| 8052 | ns->prefix, 0); |
| 8053 | if (tmpns == NULL) |
| 8054 | return (-1); |
| 8055 | |
| 8056 | if (*nsMap != NULL) { |
| 8057 | /* |
| 8058 | * Does it shadow ancestor ns-decls? |
| 8059 | */ |
| 8060 | for (mi = *nsMap; mi != (*topmi)->next; mi = mi->next) { |
| 8061 | if ((mi->depth < depth) && |
| 8062 | (mi->shadowDepth == -1) && |
| 8063 | ((ns->prefix == mi->newNs->prefix) || |
| 8064 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8065 | /* |
| 8066 | * Shadows. |
| 8067 | */ |
| 8068 | mi->shadowDepth = depth; |
| 8069 | break; |
| 8070 | } |
| 8071 | } |
| 8072 | } |
| 8073 | if (xmlDOMWrapNSNormAddNsMapItem(nsMap, topmi, ns, |
| 8074 | tmpns, depth) == NULL) { |
| 8075 | xmlFreeNs(tmpns); |
| 8076 | return (-1); |
| 8077 | } |
| 8078 | *retNs = tmpns; |
| 8079 | } |
| 8080 | return (0); |
| 8081 | } |
| 8082 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8083 | typedef enum { |
| 8084 | XML_DOM_RECONNS_REMOVEREDUND = 1<<0 |
| 8085 | } xmlDOMReconcileNSOptions; |
| 8086 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8087 | /* |
| 8088 | * xmlDOMWrapReconcileNamespaces: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8089 | * @ctxt: DOM wrapper context, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8090 | * @elem: the element-node |
| 8091 | * @options: option flags |
| 8092 | * |
| 8093 | * Ensures that ns-references point to ns-decls hold on element-nodes. |
| 8094 | * Ensures that the tree is namespace wellformed by creating additional |
| 8095 | * ns-decls where needed. Note that, since prefixes of already existent |
| 8096 | * ns-decls can be shadowed by this process, it could break QNames in |
| 8097 | * attribute values or element content. |
Kasimier T. Buchcik | 017264f | 2005-06-27 13:45:24 +0000 | [diff] [blame] | 8098 | * WARNING: This function is in a experimental state. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8099 | * |
| 8100 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8101 | */ |
| 8102 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8103 | int |
| 8104 | xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8105 | xmlNodePtr elem, |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8106 | int options) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8107 | { |
| 8108 | int depth = -1, adoptns = 0, parnsdone = 0; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8109 | xmlNsPtr ns, prevns; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8110 | xmlDocPtr doc; |
| 8111 | xmlNodePtr cur, curElem = NULL; |
| 8112 | xmlNsMapItemPtr nsMap = NULL, topmi = NULL, mi; |
| 8113 | /* @ancestorsOnly should be set by an option flag. */ |
| 8114 | int ancestorsOnly = 0; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8115 | int optRemoveDedundantNS = |
| 8116 | ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0; |
| 8117 | xmlNsPtr *listRedund = NULL; |
| 8118 | int sizeRedund = 0, nbRedund = 0, ret, i, j; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8119 | |
| 8120 | if ((elem == NULL) || (elem->doc == NULL) || |
| 8121 | (elem->type != XML_ELEMENT_NODE)) |
| 8122 | return (-1); |
| 8123 | |
| 8124 | doc = elem->doc; |
| 8125 | cur = elem; |
| 8126 | do { |
| 8127 | switch (cur->type) { |
| 8128 | case XML_ELEMENT_NODE: |
| 8129 | adoptns = 1; |
| 8130 | curElem = cur; |
| 8131 | depth++; |
| 8132 | /* |
| 8133 | * Namespace declarations. |
| 8134 | */ |
| 8135 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8136 | prevns = NULL; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8137 | ns = cur->nsDef; |
| 8138 | while (ns != NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8139 | if (! parnsdone) { |
| 8140 | if ((elem->parent) && |
| 8141 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8142 | /* |
| 8143 | * Gather ancestor in-scope ns-decls. |
| 8144 | */ |
| 8145 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8146 | elem->parent) == -1) |
| 8147 | goto internal_error; |
| 8148 | if (nsMap != NULL) |
| 8149 | topmi = nsMap->prev; |
| 8150 | } |
| 8151 | parnsdone = 1; |
| 8152 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8153 | |
| 8154 | /* |
| 8155 | * Lookup the ns ancestor-axis for equal ns-decls in scope. |
| 8156 | */ |
| 8157 | if (optRemoveDedundantNS && nsMap) { |
| 8158 | for (mi = nsMap; mi != topmi->next; mi = mi->next) { |
| 8159 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8160 | (mi->shadowDepth == -1) && |
| 8161 | ((ns->prefix == mi->newNs->prefix) || |
| 8162 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) && |
| 8163 | ((ns->href == mi->newNs->href) || |
| 8164 | xmlStrEqual(ns->href, mi->newNs->href))) |
| 8165 | { |
| 8166 | /* |
| 8167 | * A redundant ns-decl was found. |
| 8168 | * Add it to the list of redundant ns-decls. |
| 8169 | */ |
| 8170 | if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund, |
| 8171 | &sizeRedund, &nbRedund, ns, mi->newNs) == -1) |
| 8172 | goto internal_error; |
| 8173 | /* |
| 8174 | * Remove the ns-decl from the element-node. |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8175 | */ |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8176 | if (prevns) |
| 8177 | prevns->next = ns->next; |
| 8178 | else |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8179 | cur->nsDef = ns->next; |
| 8180 | goto next_ns_decl; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8181 | } |
| 8182 | } |
| 8183 | } |
| 8184 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8185 | /* |
| 8186 | * Skip ns-references handling if the referenced |
| 8187 | * ns-decl is declared on the same element. |
| 8188 | */ |
| 8189 | if ((cur->ns != NULL) && adoptns && (cur->ns == ns)) |
| 8190 | adoptns = 0; |
| 8191 | /* |
| 8192 | * Does it shadow any ns-decl? |
| 8193 | */ |
| 8194 | if (nsMap) { |
| 8195 | for (mi = nsMap; mi != topmi->next; mi = mi->next) { |
| 8196 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8197 | (mi->shadowDepth == -1) && |
| 8198 | ((ns->prefix == mi->newNs->prefix) || |
| 8199 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8200 | |
| 8201 | mi->shadowDepth = depth; |
| 8202 | } |
| 8203 | } |
| 8204 | } |
| 8205 | /* |
| 8206 | * Push mapping. |
| 8207 | */ |
| 8208 | if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi, ns, ns, |
| 8209 | depth) == NULL) |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8210 | goto internal_error; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8211 | |
| 8212 | prevns = ns; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8213 | next_ns_decl: |
| 8214 | ns = ns->next; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8215 | } |
| 8216 | } |
| 8217 | if (! adoptns) |
| 8218 | goto ns_end; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8219 | /* No break on purpose. */ |
| 8220 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8221 | /* No ns, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8222 | if (cur->ns == NULL) |
| 8223 | goto ns_end; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8224 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8225 | if (! parnsdone) { |
| 8226 | if ((elem->parent) && |
| 8227 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8228 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8229 | elem->parent) == -1) |
| 8230 | goto internal_error; |
| 8231 | if (nsMap != NULL) |
| 8232 | topmi = nsMap->prev; |
| 8233 | } |
| 8234 | parnsdone = 1; |
| 8235 | } |
| 8236 | /* |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8237 | * Adjust the reference if this was a redundant ns-decl. |
| 8238 | */ |
| 8239 | if (listRedund) { |
| 8240 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8241 | if (cur->ns == listRedund[j]) { |
| 8242 | cur->ns = listRedund[++j]; |
| 8243 | break; |
| 8244 | } |
| 8245 | } |
| 8246 | } |
| 8247 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8248 | * Adopt ns-references. |
| 8249 | */ |
| 8250 | if (nsMap != NULL) { |
| 8251 | /* |
| 8252 | * Search for a mapping. |
| 8253 | */ |
| 8254 | for (mi = nsMap; mi != topmi->next; mi = mi->next) { |
| 8255 | if ((mi->shadowDepth == -1) && |
| 8256 | (cur->ns == mi->oldNs)) { |
| 8257 | |
| 8258 | cur->ns = mi->newNs; |
| 8259 | goto ns_end; |
| 8260 | } |
| 8261 | } |
| 8262 | } |
| 8263 | /* |
| 8264 | * Aquire a normalized ns-decl and add it to the map. |
| 8265 | */ |
| 8266 | if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem, |
| 8267 | cur->ns, &ns, |
| 8268 | &nsMap, &topmi, depth, |
| 8269 | ancestorsOnly, |
| 8270 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8271 | goto internal_error; |
| 8272 | cur->ns = ns; |
| 8273 | |
| 8274 | ns_end: |
| 8275 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8276 | (cur->properties != NULL)) { |
| 8277 | /* |
| 8278 | * Process attributes. |
| 8279 | */ |
| 8280 | cur = (xmlNodePtr) cur->properties; |
| 8281 | continue; |
| 8282 | } |
| 8283 | break; |
| 8284 | default: |
| 8285 | goto next_sibling; |
| 8286 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8287 | into_content: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8288 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8289 | (cur->children != NULL)) { |
| 8290 | /* |
| 8291 | * Process content of element-nodes only. |
| 8292 | */ |
| 8293 | cur = cur->children; |
| 8294 | continue; |
| 8295 | } |
| 8296 | next_sibling: |
| 8297 | if (cur == elem) |
| 8298 | break; |
| 8299 | if (cur->type == XML_ELEMENT_NODE) { |
| 8300 | if (nsMap != NULL) { |
| 8301 | /* |
| 8302 | * Pop mappings. |
| 8303 | */ |
| 8304 | while ((topmi->depth >= 0) && (topmi->depth >= depth)) |
| 8305 | topmi = topmi->prev; |
| 8306 | /* |
| 8307 | * Unshadow. |
| 8308 | */ |
| 8309 | for (mi = nsMap; mi != topmi->next; mi = mi->next) |
| 8310 | if (mi->shadowDepth >= depth) |
| 8311 | mi->shadowDepth = -1; |
| 8312 | } |
| 8313 | depth--; |
| 8314 | } |
| 8315 | if (cur->next != NULL) |
| 8316 | cur = cur->next; |
| 8317 | else { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8318 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 8319 | cur = cur->parent; |
| 8320 | goto into_content; |
| 8321 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8322 | cur = cur->parent; |
| 8323 | goto next_sibling; |
| 8324 | } |
| 8325 | } while (cur != NULL); |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8326 | |
| 8327 | ret = 0; |
| 8328 | goto exit; |
| 8329 | internal_error: |
| 8330 | ret = -1; |
| 8331 | exit: |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8332 | if (listRedund) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8333 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8334 | xmlFreeNs(listRedund[j]); |
| 8335 | } |
| 8336 | xmlFree(listRedund); |
| 8337 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8338 | if (nsMap != NULL) |
| 8339 | xmlDOMWrapNSNormFreeNsMap(nsMap); |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8340 | return (ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8341 | } |
| 8342 | |
| 8343 | /* |
| 8344 | * xmlDOMWrapAdoptBranch: |
| 8345 | * @ctxt: the optional context for custom processing |
| 8346 | * @sourceDoc: the optional sourceDoc |
| 8347 | * @node: the element-node to start with |
| 8348 | * @destDoc: the destination doc for adoption |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8349 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8350 | * @options: option flags |
| 8351 | * |
| 8352 | * Ensures that ns-references point to @destDoc: either to |
| 8353 | * elements->nsDef entries if @destParent is given, or to |
| 8354 | * @destDoc->oldNs otherwise. |
| 8355 | * If @destParent is given, it ensures that the tree is namespace |
| 8356 | * wellformed by creating additional ns-decls where needed. |
| 8357 | * Note that, since prefixes of already existent ns-decls can be |
| 8358 | * shadowed by this process, it could break QNames in attribute |
| 8359 | * values or element content. |
| 8360 | * |
| 8361 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8362 | */ |
| 8363 | static int |
| 8364 | xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt, |
| 8365 | xmlDocPtr sourceDoc, |
| 8366 | xmlNodePtr node, |
| 8367 | xmlDocPtr destDoc, |
| 8368 | xmlNodePtr destParent, |
| 8369 | int options ATTRIBUTE_UNUSED) |
| 8370 | { |
| 8371 | int ret = 0; |
| 8372 | xmlNodePtr cur, curElem = NULL; |
| 8373 | xmlNsMapItemPtr nsMap = NULL, topmi = NULL, mi; |
| 8374 | xmlNsPtr ns; |
| 8375 | int depth = -1, adoptStr = 1; |
| 8376 | /* gather @parent's ns-decls. */ |
| 8377 | int parnsdone = 0; |
| 8378 | /* @ancestorsOnly should be set per option. */ |
| 8379 | int ancestorsOnly = 0; |
| 8380 | |
| 8381 | /* |
| 8382 | * Optimize string adoption for equal or none dicts. |
| 8383 | */ |
| 8384 | if ((sourceDoc != NULL) && |
| 8385 | (sourceDoc->dict == destDoc->dict)) |
| 8386 | adoptStr = 0; |
| 8387 | else |
| 8388 | adoptStr = 1; |
| 8389 | |
| 8390 | cur = node; |
| 8391 | while (cur != NULL) { |
| 8392 | if (cur->doc != sourceDoc) { |
| 8393 | /* |
| 8394 | * We'll assume XIncluded nodes if the doc differs. |
| 8395 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 8396 | * This here skips XIncluded nodes and tries to handle |
| 8397 | * broken sequences. |
| 8398 | */ |
| 8399 | if (cur->next == NULL) |
| 8400 | goto leave_node; |
| 8401 | do { |
| 8402 | cur = cur->next; |
| 8403 | if ((cur->type == XML_XINCLUDE_END) || |
| 8404 | (cur->doc == node->doc)) |
| 8405 | break; |
| 8406 | } while (cur->next != NULL); |
| 8407 | |
| 8408 | if (cur->doc != node->doc) |
| 8409 | goto leave_node; |
| 8410 | } |
| 8411 | cur->doc = destDoc; |
| 8412 | switch (cur->type) { |
| 8413 | case XML_XINCLUDE_START: |
| 8414 | case XML_XINCLUDE_END: |
| 8415 | /* |
| 8416 | * TODO |
| 8417 | */ |
| 8418 | return (-1); |
| 8419 | case XML_ELEMENT_NODE: |
| 8420 | curElem = cur; |
| 8421 | depth++; |
| 8422 | /* |
| 8423 | * Namespace declarations. |
| 8424 | */ |
| 8425 | if ((ctxt == NULL) && (cur->nsDef != NULL)) { |
| 8426 | if (! parnsdone) { |
| 8427 | if (destParent && (ctxt == NULL)) { |
| 8428 | /* |
| 8429 | * Gather @parent's in-scope ns-decls. |
| 8430 | */ |
| 8431 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8432 | destParent) == -1) |
| 8433 | goto internal_error; |
| 8434 | if (nsMap != NULL) |
| 8435 | topmi = nsMap->prev; |
| 8436 | } |
| 8437 | parnsdone = 1; |
| 8438 | } |
| 8439 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8440 | /* |
| 8441 | * ns->prefix and ns->href seem not to be in the dict. |
| 8442 | * XML_TREE_ADOPT_STR(ns->prefix) |
| 8443 | * XML_TREE_ADOPT_STR(ns->href) |
| 8444 | */ |
| 8445 | /* |
| 8446 | * Does it shadow any ns-decl? |
| 8447 | */ |
| 8448 | if (nsMap) { |
| 8449 | for (mi = nsMap; mi != topmi->next; |
| 8450 | mi = mi->next) { |
| 8451 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8452 | (mi->shadowDepth == -1) && |
| 8453 | ((ns->prefix == mi->newNs->prefix) || |
| 8454 | xmlStrEqual(ns->prefix, |
| 8455 | mi->newNs->prefix))) { |
| 8456 | |
| 8457 | mi->shadowDepth = depth; |
| 8458 | } |
| 8459 | } |
| 8460 | } |
| 8461 | /* |
| 8462 | * Push mapping. |
| 8463 | */ |
| 8464 | if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi, |
| 8465 | ns, ns, depth) == NULL) |
| 8466 | goto internal_error; |
| 8467 | } |
| 8468 | } |
| 8469 | /* No break on purpose. */ |
| 8470 | case XML_ATTRIBUTE_NODE: |
| 8471 | |
| 8472 | if (cur->ns == NULL) |
| 8473 | goto ns_end; |
| 8474 | if (! parnsdone) { |
| 8475 | if (destParent && (ctxt == NULL)) { |
| 8476 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8477 | destParent) == -1) |
| 8478 | goto internal_error; |
| 8479 | if (nsMap != NULL) |
| 8480 | topmi = nsMap->prev; |
| 8481 | } |
| 8482 | parnsdone = 1; |
| 8483 | } |
| 8484 | /* |
| 8485 | * Adopt ns-references. |
| 8486 | */ |
| 8487 | if (nsMap != NULL) { |
| 8488 | /* |
| 8489 | * Search for a mapping. |
| 8490 | */ |
| 8491 | for (mi = nsMap; mi != topmi->next; mi = mi->next) { |
| 8492 | if ((mi->shadowDepth == -1) && |
| 8493 | (cur->ns == mi->oldNs)) { |
| 8494 | |
| 8495 | cur->ns = mi->newNs; |
| 8496 | goto ns_end; |
| 8497 | } |
| 8498 | } |
| 8499 | } |
| 8500 | /* |
| 8501 | * Start searching for an in-scope ns-decl. |
| 8502 | */ |
| 8503 | if (ctxt != NULL) { |
| 8504 | /* |
| 8505 | * User-defined behaviour. |
| 8506 | */ |
| 8507 | #if 0 |
| 8508 | ctxt->aquireNsDecl(ctxt, cur->ns, &ns); |
| 8509 | #endif |
| 8510 | /* |
| 8511 | * Insert mapping if ns is available; it's the users fault |
| 8512 | * if not. |
| 8513 | */ |
| 8514 | if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi, |
| 8515 | ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
| 8516 | goto internal_error; |
| 8517 | cur->ns = ns; |
| 8518 | } else { |
| 8519 | /* |
| 8520 | * Aquire a normalized ns-decl and add it to the map. |
| 8521 | */ |
| 8522 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
| 8523 | /* ns-decls on curElem or on destDoc->oldNs */ |
| 8524 | destParent ? curElem : NULL, |
| 8525 | cur->ns, &ns, |
| 8526 | &nsMap, &topmi, depth, |
| 8527 | ancestorsOnly, |
| 8528 | /* ns-decls must be prefixed for attributes. */ |
| 8529 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8530 | goto internal_error; |
| 8531 | cur->ns = ns; |
| 8532 | } |
| 8533 | ns_end: |
| 8534 | /* |
| 8535 | * Further node properties. |
| 8536 | * TODO: Is this all? |
| 8537 | */ |
| 8538 | XML_TREE_ADOPT_STR(cur->name) |
| 8539 | if (cur->type == XML_ELEMENT_NODE) { |
| 8540 | cur->psvi = NULL; |
| 8541 | cur->line = 0; |
| 8542 | cur->extra = 0; |
| 8543 | /* |
| 8544 | * Walk attributes. |
| 8545 | */ |
| 8546 | if (cur->properties != NULL) { |
| 8547 | /* |
| 8548 | * Process first attribute node. |
| 8549 | */ |
| 8550 | cur = (xmlNodePtr) cur->properties; |
| 8551 | continue; |
| 8552 | } |
| 8553 | } else { |
| 8554 | /* |
| 8555 | * Attributes. |
| 8556 | */ |
| 8557 | if ((sourceDoc != NULL) && |
| 8558 | (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID)) |
| 8559 | xmlRemoveID(sourceDoc, (xmlAttrPtr) cur); |
| 8560 | ((xmlAttrPtr) cur)->atype = 0; |
| 8561 | ((xmlAttrPtr) cur)->psvi = NULL; |
| 8562 | } |
| 8563 | break; |
| 8564 | case XML_TEXT_NODE: |
| 8565 | case XML_CDATA_SECTION_NODE: |
| 8566 | /* |
| 8567 | * This puts the content in the dest dict, only if |
| 8568 | * it was previously in the source dict. |
| 8569 | */ |
| 8570 | XML_TREE_ADOPT_STR_2(cur->content) |
| 8571 | goto leave_node; |
| 8572 | case XML_ENTITY_REF_NODE: |
| 8573 | /* |
| 8574 | * Remove reference to the entitity-node. |
| 8575 | */ |
| 8576 | cur->content = NULL; |
| 8577 | cur->children = NULL; |
| 8578 | cur->last = NULL; |
| 8579 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 8580 | xmlEntityPtr ent; |
| 8581 | /* |
| 8582 | * Assign new entity-node if available. |
| 8583 | */ |
| 8584 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 8585 | if (ent != NULL) { |
| 8586 | cur->content = ent->content; |
| 8587 | cur->children = (xmlNodePtr) ent; |
| 8588 | cur->last = (xmlNodePtr) ent; |
| 8589 | } |
| 8590 | } |
| 8591 | goto leave_node; |
| 8592 | case XML_PI_NODE: |
| 8593 | XML_TREE_ADOPT_STR(cur->name) |
| 8594 | XML_TREE_ADOPT_STR_2(cur->content) |
| 8595 | break; |
| 8596 | case XML_COMMENT_NODE: |
| 8597 | break; |
| 8598 | default: |
| 8599 | goto internal_error; |
| 8600 | } |
| 8601 | /* |
| 8602 | * Walk the tree. |
| 8603 | */ |
| 8604 | if (cur->children != NULL) { |
| 8605 | cur = cur->children; |
| 8606 | continue; |
| 8607 | } |
| 8608 | |
| 8609 | leave_node: |
| 8610 | if (cur == node) |
| 8611 | break; |
| 8612 | if ((cur->type == XML_ELEMENT_NODE) || |
| 8613 | (cur->type == XML_XINCLUDE_START) || |
| 8614 | (cur->type == XML_XINCLUDE_END)) { |
| 8615 | /* |
| 8616 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 8617 | */ |
| 8618 | if (nsMap != NULL) { |
| 8619 | /* |
| 8620 | * Pop mappings. |
| 8621 | */ |
| 8622 | while (topmi->depth >= depth) |
| 8623 | topmi = topmi->prev; |
| 8624 | /* |
| 8625 | * Unshadow. |
| 8626 | */ |
| 8627 | for (mi = nsMap; mi != topmi->next; mi = mi->next) |
| 8628 | if (mi->shadowDepth >= depth) |
| 8629 | mi->shadowDepth = -1; |
| 8630 | } |
| 8631 | depth--; |
| 8632 | } |
| 8633 | if (cur->next != NULL) |
| 8634 | cur = cur->next; |
| 8635 | else { |
| 8636 | cur = cur->parent; |
| 8637 | goto leave_node; |
| 8638 | } |
| 8639 | } |
| 8640 | /* |
| 8641 | * Cleanup. |
| 8642 | */ |
| 8643 | if (nsMap != NULL) |
| 8644 | xmlDOMWrapNSNormFreeNsMap(nsMap); |
| 8645 | return (ret); |
| 8646 | internal_error: |
| 8647 | if (nsMap != NULL) |
| 8648 | xmlDOMWrapNSNormFreeNsMap(nsMap); |
| 8649 | return (-1); |
| 8650 | } |
| 8651 | |
| 8652 | /* |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8653 | * xmlDOMWrapCloneNode: |
| 8654 | * @ctxt: the optional context for custom processing |
| 8655 | * @sourceDoc: the optional sourceDoc |
| 8656 | * @node: the node to start with |
| 8657 | * @resNode: the clone of the given @node |
| 8658 | * @destDoc: the destination doc |
| 8659 | * @destParent: the optional new parent of @node in @destDoc |
| 8660 | * @options: option flags |
| 8661 | * |
| 8662 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 8663 | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
| 8664 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used |
| 8665 | * This is the case when you have an unliked node and just want to move it |
| 8666 | * to the context of |
| 8667 | * |
| 8668 | * If @destParent is given, it ensures that the tree is namespace |
| 8669 | * wellformed by creating additional ns-decls where needed. |
| 8670 | * Note that, since prefixes of already existent ns-decls can be |
| 8671 | * shadowed by this process, it could break QNames in attribute |
| 8672 | * values or element content. |
| 8673 | * TODO: |
| 8674 | * 1) Support dicts |
| 8675 | * Optimize string adoption for equal or none dicts. |
| 8676 | * 2) XInclude |
| 8677 | * WARNING: This function is in a experimental state and should only be currently |
| 8678 | * only be used to test it. |
| 8679 | * |
| 8680 | * Returns 0 if the operation succeeded, |
| 8681 | * 1 if a node of unsupported (or not yet supported) type was given, |
| 8682 | * -1 on API/internal errors. |
| 8683 | */ |
| 8684 | |
| 8685 | int |
| 8686 | xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt, |
| 8687 | xmlDocPtr sourceDoc, |
| 8688 | xmlNodePtr node, |
| 8689 | xmlNodePtr *resNode, |
| 8690 | xmlDocPtr destDoc, |
| 8691 | xmlNodePtr destParent, |
| 8692 | int deep, |
| 8693 | int options ATTRIBUTE_UNUSED) |
| 8694 | { |
| 8695 | int ret = 0; |
| 8696 | xmlNodePtr cur, curElem = NULL; |
| 8697 | xmlNsMapItemPtr nsMap = NULL, topmi = NULL, mi; |
| 8698 | xmlNsPtr ns; |
| 8699 | int depth = -1; |
| 8700 | /* int adoptStr = 1; */ |
| 8701 | /* gather @parent's ns-decls. */ |
| 8702 | int parnsdone = 0; |
| 8703 | /* @ancestorsOnly should be set per option. */ |
| 8704 | int ancestorsOnly = 0; |
| 8705 | xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL; |
| 8706 | xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL; |
| 8707 | |
| 8708 | if ((node == NULL) || (resNode == NULL) || |
| 8709 | (sourceDoc == NULL) || (destDoc == NULL)) |
| 8710 | return(-1); |
| 8711 | /* |
| 8712 | * TODO: Initially we support only element-nodes. |
| 8713 | */ |
| 8714 | if (node->type != XML_ELEMENT_NODE) |
| 8715 | return(1); |
| 8716 | /* |
| 8717 | * Check node->doc sanity. |
| 8718 | */ |
| 8719 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 8720 | (node->doc != sourceDoc)) { |
| 8721 | /* |
| 8722 | * Might be an XIncluded node. |
| 8723 | */ |
| 8724 | return (-1); |
| 8725 | } |
| 8726 | if (sourceDoc == NULL) |
| 8727 | sourceDoc = node->doc; |
| 8728 | if (sourceDoc == destDoc) |
| 8729 | return (-1); |
| 8730 | |
| 8731 | *resNode = NULL; |
| 8732 | |
| 8733 | deep = 1; |
| 8734 | cur = node; |
| 8735 | while (cur != NULL) { |
| 8736 | if (cur->doc != sourceDoc) { |
| 8737 | /* |
| 8738 | * We'll assume XIncluded nodes if the doc differs. |
| 8739 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 8740 | * TODO: This here returns -1 in this case. |
| 8741 | */ |
| 8742 | goto internal_error; |
| 8743 | } |
| 8744 | /* |
| 8745 | * Create a new node. |
| 8746 | */ |
| 8747 | switch (cur->type) { |
| 8748 | case XML_XINCLUDE_START: |
| 8749 | case XML_XINCLUDE_END: |
| 8750 | /* TODO: What to do with XInclude? */ |
| 8751 | goto internal_error; |
| 8752 | break; |
| 8753 | case XML_TEXT_NODE: |
| 8754 | case XML_CDATA_SECTION_NODE: |
| 8755 | case XML_ELEMENT_NODE: |
| 8756 | case XML_DOCUMENT_FRAG_NODE: |
| 8757 | case XML_ENTITY_REF_NODE: |
| 8758 | case XML_ENTITY_NODE: |
| 8759 | case XML_PI_NODE: |
| 8760 | case XML_COMMENT_NODE: |
| 8761 | /* |
| 8762 | * Nodes of xmlNode structure. |
| 8763 | */ |
| 8764 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 8765 | if (clone == NULL) { |
| 8766 | xmlTreeErrMemory("xmlDOMWrapCloneBranch(): allocating a node"); |
| 8767 | goto internal_error; |
| 8768 | } |
| 8769 | memset(clone, 0, sizeof(xmlNode)); |
| 8770 | /* |
| 8771 | * Set hierachical links. |
| 8772 | */ |
| 8773 | if (resultClone != NULL) { |
| 8774 | clone->parent = parentClone; |
| 8775 | if (prevClone) { |
| 8776 | prevClone->next = clone; |
| 8777 | clone->prev = prevClone; |
| 8778 | } else |
| 8779 | parentClone->children = clone; |
| 8780 | } else |
| 8781 | resultClone = clone; |
| 8782 | |
| 8783 | break; |
| 8784 | case XML_ATTRIBUTE_NODE: |
| 8785 | /* |
| 8786 | * Attributes (xmlAttr). |
| 8787 | */ |
| 8788 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr)); |
| 8789 | if (clone == NULL) { |
| 8790 | xmlTreeErrMemory("xmlDOMWrapCloneBranch(): allocating an attr-node"); |
| 8791 | goto internal_error; |
| 8792 | } |
| 8793 | memset(clone, 0, sizeof(xmlAttr)); |
| 8794 | /* |
| 8795 | * Set hierachical links. |
| 8796 | */ |
| 8797 | if (resultClone != NULL) { |
| 8798 | clone->parent = parentClone; |
| 8799 | if (prevClone) { |
| 8800 | prevClone->next = clone; |
| 8801 | clone->prev = prevClone; |
| 8802 | } else |
| 8803 | parentClone->properties = (xmlAttrPtr) clone; |
| 8804 | } else |
| 8805 | resultClone = clone; |
| 8806 | break; |
| 8807 | default: |
| 8808 | /* TODO */ |
| 8809 | goto internal_error; |
| 8810 | } |
| 8811 | |
| 8812 | clone->type = cur->type; |
| 8813 | clone->doc = destDoc; |
| 8814 | |
| 8815 | if (cur->name == xmlStringText) |
| 8816 | clone->name = xmlStringText; |
| 8817 | else if (cur->name == xmlStringTextNoenc) |
| 8818 | /* |
| 8819 | * TODO: xmlStringTextNoenc is never assigned to a node |
| 8820 | * in tree.c. |
| 8821 | */ |
| 8822 | clone->name = xmlStringTextNoenc; |
| 8823 | else if (cur->name == xmlStringComment) |
| 8824 | clone->name = xmlStringComment; |
| 8825 | else if (cur->name != NULL) { |
| 8826 | if ((destDoc != NULL) && (destDoc->dict != NULL)) |
| 8827 | clone->name = xmlDictLookup(destDoc->dict, cur->name, -1); |
| 8828 | else |
| 8829 | clone->name = xmlStrdup(cur->name); |
| 8830 | } |
| 8831 | |
| 8832 | switch (cur->type) { |
| 8833 | case XML_XINCLUDE_START: |
| 8834 | case XML_XINCLUDE_END: |
| 8835 | /* |
| 8836 | * TODO |
| 8837 | */ |
| 8838 | return (-1); |
| 8839 | case XML_ELEMENT_NODE: |
| 8840 | curElem = cur; |
| 8841 | depth++; |
| 8842 | /* |
| 8843 | * Namespace declarations. |
| 8844 | */ |
| 8845 | if ((ctxt == NULL) && (cur->nsDef != NULL)) { |
| 8846 | if (! parnsdone) { |
| 8847 | if (destParent && (ctxt == NULL)) { |
| 8848 | /* |
| 8849 | * Gather @parent's in-scope ns-decls. |
| 8850 | */ |
| 8851 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8852 | destParent) == -1) |
| 8853 | goto internal_error; |
| 8854 | if (nsMap != NULL) |
| 8855 | topmi = nsMap->prev; |
| 8856 | } |
| 8857 | parnsdone = 1; |
| 8858 | } |
| 8859 | /* |
| 8860 | * Clone namespace declarations. |
| 8861 | */ |
| 8862 | cloneNsDefSlot = &(clone->nsDef); |
| 8863 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8864 | /* |
| 8865 | * Create a new xmlNs. |
| 8866 | */ |
| 8867 | cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 8868 | if (cloneNs == NULL) { |
| 8869 | xmlTreeErrMemory("xmlDOMWrapCloneBranch(): " |
| 8870 | "allocating namespace"); |
| 8871 | return(-1); |
| 8872 | } |
| 8873 | memset(cloneNs, 0, sizeof(xmlNs)); |
| 8874 | cloneNs->type = XML_LOCAL_NAMESPACE; |
| 8875 | |
| 8876 | if (ns->href != NULL) |
| 8877 | cloneNs->href = xmlStrdup(ns->href); |
| 8878 | if (ns->prefix != NULL) |
| 8879 | cloneNs->prefix = xmlStrdup(ns->prefix); |
| 8880 | |
| 8881 | *cloneNsDefSlot = cloneNs; |
| 8882 | cloneNsDefSlot = &(cloneNs->next); |
| 8883 | |
| 8884 | /* |
| 8885 | * Does it shadow any ns-decl? |
| 8886 | */ |
| 8887 | if (nsMap) { |
| 8888 | for (mi = nsMap; mi != topmi->next; |
| 8889 | mi = mi->next) { |
| 8890 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8891 | (mi->shadowDepth == -1) && |
| 8892 | ((ns->prefix == mi->newNs->prefix) || |
| 8893 | xmlStrEqual(ns->prefix, |
| 8894 | mi->newNs->prefix))) { |
| 8895 | /* Mark as shadowed at the current depth. */ |
| 8896 | mi->shadowDepth = depth; |
| 8897 | } |
| 8898 | } |
| 8899 | } |
| 8900 | /* |
| 8901 | * Push mapping. |
| 8902 | */ |
| 8903 | if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi, |
| 8904 | ns, cloneNs, depth) == NULL) |
| 8905 | goto internal_error; |
| 8906 | } |
| 8907 | } |
| 8908 | /* cur->ns will be processed further down. */ |
| 8909 | break; |
| 8910 | case XML_ATTRIBUTE_NODE: |
| 8911 | /* IDs will be processed further down. */ |
| 8912 | /* cur->ns will be processed further down. */ |
| 8913 | break; |
| 8914 | case XML_TEXT_NODE: |
| 8915 | case XML_CDATA_SECTION_NODE: |
| 8916 | if (cur->content) |
| 8917 | clone->content = xmlStrdup(cur->content); |
| 8918 | goto leave_node; |
| 8919 | case XML_ENTITY_REF_NODE: |
| 8920 | if (sourceDoc != destDoc) { |
| 8921 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 8922 | xmlEntityPtr ent; |
| 8923 | /* |
| 8924 | * Assign new entity-node if available. |
| 8925 | */ |
| 8926 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 8927 | if (ent != NULL) { |
| 8928 | clone->content = ent->content; |
| 8929 | clone->children = (xmlNodePtr) ent; |
| 8930 | clone->last = (xmlNodePtr) ent; |
| 8931 | } |
| 8932 | } |
| 8933 | } else { |
| 8934 | /* |
| 8935 | * Use the current node's entity declaration and value. |
| 8936 | */ |
| 8937 | clone->content = cur->content; |
| 8938 | clone->children = cur->children; |
| 8939 | clone->last = cur->last; |
| 8940 | } |
| 8941 | goto leave_node; |
| 8942 | case XML_PI_NODE: |
| 8943 | if (cur->content) |
| 8944 | clone->content = xmlStrdup(cur->content); |
| 8945 | goto leave_node; |
| 8946 | case XML_COMMENT_NODE: |
| 8947 | if (cur->content) |
| 8948 | clone->content = xmlStrdup(cur->content); |
| 8949 | goto leave_node; |
| 8950 | default: |
| 8951 | goto internal_error; |
| 8952 | } |
| 8953 | |
| 8954 | if (cur->ns == NULL) |
| 8955 | goto end_ns_reference; |
| 8956 | |
| 8957 | /* handle_ns_reference: */ |
| 8958 | /* |
| 8959 | ** The following will take care of references to ns-decls ******** |
| 8960 | ** and is intended only for element- and attribute-nodes. |
| 8961 | ** |
| 8962 | */ |
| 8963 | if (! parnsdone) { |
| 8964 | if (destParent && (ctxt == NULL)) { |
| 8965 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8966 | destParent) == -1) |
| 8967 | goto internal_error; |
| 8968 | if (nsMap != NULL) |
| 8969 | topmi = nsMap->prev; |
| 8970 | } |
| 8971 | parnsdone = 1; |
| 8972 | } |
| 8973 | /* |
| 8974 | * Adopt ns-references. |
| 8975 | */ |
| 8976 | if (nsMap != NULL) { |
| 8977 | /* |
| 8978 | * Search for a mapping. |
| 8979 | */ |
| 8980 | for (mi = nsMap; mi != topmi->next; mi = mi->next) { |
| 8981 | if ((mi->shadowDepth == -1) && |
| 8982 | (cur->ns == mi->oldNs)) { |
| 8983 | /* |
| 8984 | * This is the nice case: a mapping was found. |
| 8985 | */ |
| 8986 | clone->ns = mi->newNs; |
| 8987 | goto end_ns_reference; |
| 8988 | } |
| 8989 | } |
| 8990 | } |
| 8991 | /* |
| 8992 | * Start searching for an in-scope ns-decl. |
| 8993 | */ |
| 8994 | if (ctxt != NULL) { |
| 8995 | /* |
| 8996 | * User-defined behaviour. |
| 8997 | */ |
| 8998 | #if 0 |
| 8999 | ctxt->aquireNsDecl(ctxt, cur->ns, &ns); |
| 9000 | #endif |
| 9001 | /* |
| 9002 | * Add user's mapping. |
| 9003 | */ |
| 9004 | if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi, |
| 9005 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
| 9006 | goto internal_error; |
| 9007 | clone->ns = ns; |
| 9008 | } else { |
| 9009 | /* |
| 9010 | * Aquire a normalized ns-decl and add it to the map. |
| 9011 | */ |
| 9012 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
| 9013 | /* ns-decls on curElem or on destDoc->oldNs */ |
| 9014 | destParent ? curElem : NULL, |
| 9015 | cur->ns, &ns, |
| 9016 | &nsMap, &topmi, depth, |
| 9017 | ancestorsOnly, |
| 9018 | /* ns-decls must be prefixed for attributes. */ |
| 9019 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 9020 | goto internal_error; |
| 9021 | clone->ns = ns; |
| 9022 | } |
| 9023 | |
| 9024 | end_ns_reference: |
| 9025 | |
| 9026 | /* |
| 9027 | * Some post-processing. |
| 9028 | * |
| 9029 | * Handle ID attributes. |
| 9030 | */ |
| 9031 | if ((clone->type == XML_ATTRIBUTE_NODE) && |
| 9032 | (clone->parent != NULL)) { |
| 9033 | if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) { |
| 9034 | |
| 9035 | xmlChar *idVal; |
| 9036 | |
| 9037 | idVal = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 9038 | if (idVal != NULL) { |
| 9039 | if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) { |
| 9040 | /* TODO: error message. */ |
| 9041 | xmlFree(idVal); |
| 9042 | goto internal_error; |
| 9043 | } |
| 9044 | xmlFree(idVal); |
| 9045 | } |
| 9046 | } |
| 9047 | } |
| 9048 | /* |
| 9049 | ** |
| 9050 | ** The following will traversing the tree ************************ |
| 9051 | ** |
| 9052 | * |
| 9053 | * Walk the element's attributes before descending into child-nodes. |
| 9054 | */ |
| 9055 | if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) { |
| 9056 | prevClone = NULL; |
| 9057 | parentClone = clone; |
| 9058 | cur = (xmlNodePtr) cur->properties; |
| 9059 | continue; |
| 9060 | } |
| 9061 | into_content: |
| 9062 | /* |
| 9063 | * Descend into child-nodes. |
| 9064 | */ |
| 9065 | if (cur->children != NULL) { |
| 9066 | prevClone = NULL; |
| 9067 | parentClone = clone; |
| 9068 | cur = cur->children; |
| 9069 | continue; |
| 9070 | } |
| 9071 | |
| 9072 | leave_node: |
| 9073 | /* |
| 9074 | * At this point we are done with the node, its content |
| 9075 | * and an element-nodes's attribute-nodes. |
| 9076 | */ |
| 9077 | if (cur == node) |
| 9078 | break; |
| 9079 | if ((cur->type == XML_ELEMENT_NODE) || |
| 9080 | (cur->type == XML_XINCLUDE_START) || |
| 9081 | (cur->type == XML_XINCLUDE_END)) { |
| 9082 | /* |
| 9083 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 9084 | */ |
| 9085 | if (nsMap != NULL) { |
| 9086 | /* |
| 9087 | * Pop mappings. |
| 9088 | */ |
| 9089 | while (topmi->depth >= depth) |
| 9090 | topmi = topmi->prev; |
| 9091 | /* |
| 9092 | * Unshadow. |
| 9093 | * TODO: How to optimize this? |
| 9094 | */ |
| 9095 | for (mi = nsMap; mi != topmi->next; mi = mi->next) |
| 9096 | if (mi->shadowDepth >= depth) |
| 9097 | mi->shadowDepth = -1; |
| 9098 | } |
| 9099 | depth--; |
| 9100 | } |
| 9101 | if (cur->next != NULL) { |
| 9102 | prevClone = clone; |
| 9103 | cur = cur->next; |
| 9104 | } else if (cur->type != XML_ATTRIBUTE_NODE) { |
| 9105 | /* |
| 9106 | * Set clone->last. |
| 9107 | */ |
| 9108 | clone->parent->last = clone; |
| 9109 | clone = clone->parent; |
| 9110 | parentClone = clone->parent; |
| 9111 | /* |
| 9112 | * Process parent --> next; |
| 9113 | */ |
| 9114 | cur = cur->parent; |
| 9115 | goto leave_node; |
| 9116 | } else { |
| 9117 | /* This is for attributes only. */ |
| 9118 | clone = clone->parent; |
| 9119 | parentClone = clone->parent; |
| 9120 | /* |
| 9121 | * Process parent-element --> children. |
| 9122 | */ |
| 9123 | cur = cur->parent; |
| 9124 | goto into_content; |
| 9125 | } |
| 9126 | } |
| 9127 | goto exit; |
| 9128 | |
| 9129 | internal_error: |
| 9130 | ret = -1; |
| 9131 | |
| 9132 | exit: |
| 9133 | /* |
| 9134 | * Cleanup. |
| 9135 | */ |
| 9136 | if (nsMap != NULL) |
| 9137 | xmlDOMWrapNSNormFreeNsMap(nsMap); |
| 9138 | /* |
| 9139 | * TODO: Should we try a cleanup of the cloned node in case of a |
| 9140 | * fatal error? |
| 9141 | */ |
| 9142 | *resNode = resultClone; |
| 9143 | return (ret); |
| 9144 | } |
| 9145 | |
| 9146 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9147 | * xmlDOMWrapAdoptAttr: |
| 9148 | * @ctxt: the optional context for custom processing |
| 9149 | * @sourceDoc: the optional source document of attr |
| 9150 | * @attr: the attribute-node to be adopted |
| 9151 | * @destDoc: the destination doc for adoption |
| 9152 | * @destParent: the optional new parent of @attr in @destDoc |
| 9153 | * @options: option flags |
| 9154 | * |
| 9155 | * @attr is adopted by @destDoc. |
| 9156 | * Ensures that ns-references point to @destDoc: either to |
| 9157 | * elements->nsDef entries if @destParent is given, or to |
| 9158 | * @destDoc->oldNs otherwise. |
| 9159 | * |
| 9160 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 9161 | */ |
| 9162 | static int |
| 9163 | xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt, |
| 9164 | xmlDocPtr sourceDoc, |
| 9165 | xmlAttrPtr attr, |
| 9166 | xmlDocPtr destDoc, |
| 9167 | xmlNodePtr destParent, |
| 9168 | int options ATTRIBUTE_UNUSED) |
| 9169 | { |
| 9170 | xmlNodePtr cur; |
| 9171 | int adoptStr = 1; |
| 9172 | |
| 9173 | if ((attr == NULL) || (destDoc == NULL)) |
| 9174 | return (-1); |
| 9175 | |
| 9176 | attr->doc = destDoc; |
| 9177 | if (attr->ns != NULL) { |
| 9178 | xmlNsPtr ns = NULL; |
| 9179 | |
| 9180 | if (ctxt != NULL) { |
| 9181 | /* TODO: User defined. */ |
| 9182 | } |
| 9183 | /* XML Namespace. */ |
| 9184 | if ((attr->ns->prefix[0] == 'x') && (attr->ns->prefix[1] == 'm') && |
| 9185 | (attr->ns->prefix[2] == 'l') && (attr->ns->prefix[3] == 0)) { |
| 9186 | ns = xmlTreeEnsureXMLDecl(destDoc); |
| 9187 | } else if (destParent == NULL) { |
| 9188 | /* |
| 9189 | * Store in @destDoc->oldNs. |
| 9190 | */ |
| 9191 | ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix); |
| 9192 | } else { |
| 9193 | /* |
| 9194 | * Declare on @destParent. |
| 9195 | */ |
| 9196 | if (xmlSearchNsByHrefStrict(destDoc, destParent, attr->ns->href, |
| 9197 | &ns, 1) == -1) |
| 9198 | goto internal_error; |
| 9199 | if (ns == NULL) { |
| 9200 | ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent, |
| 9201 | attr->ns->href, attr->ns->prefix, 1); |
| 9202 | } |
| 9203 | } |
| 9204 | if (ns == NULL) |
| 9205 | goto internal_error; |
| 9206 | attr->ns = ns; |
| 9207 | } |
| 9208 | |
| 9209 | XML_TREE_ADOPT_STR(attr->name); |
| 9210 | attr->atype = 0; |
| 9211 | attr->psvi = NULL; |
| 9212 | /* |
| 9213 | * Walk content. |
| 9214 | */ |
| 9215 | if (attr->children == NULL) |
| 9216 | return (0); |
| 9217 | cur = attr->children; |
| 9218 | while (cur != NULL) { |
| 9219 | cur->doc = destDoc; |
| 9220 | switch (cur->type) { |
| 9221 | case XML_TEXT_NODE: |
| 9222 | case XML_CDATA_SECTION_NODE: |
| 9223 | XML_TREE_ADOPT_STR_2(cur->content) |
| 9224 | break; |
| 9225 | case XML_ENTITY_REF_NODE: |
| 9226 | /* |
| 9227 | * Remove reference to the entitity-node. |
| 9228 | */ |
| 9229 | cur->content = NULL; |
| 9230 | cur->children = NULL; |
| 9231 | cur->last = NULL; |
| 9232 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9233 | xmlEntityPtr ent; |
| 9234 | /* |
| 9235 | * Assign new entity-node if available. |
| 9236 | */ |
| 9237 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9238 | if (ent != NULL) { |
| 9239 | cur->content = ent->content; |
| 9240 | cur->children = (xmlNodePtr) ent; |
| 9241 | cur->last = (xmlNodePtr) ent; |
| 9242 | } |
| 9243 | } |
| 9244 | break; |
| 9245 | default: |
| 9246 | break; |
| 9247 | } |
| 9248 | if (cur->children != NULL) { |
| 9249 | cur = cur->children; |
| 9250 | continue; |
| 9251 | } |
| 9252 | next_sibling: |
| 9253 | if (cur == (xmlNodePtr) attr) |
| 9254 | break; |
| 9255 | if (cur->next != NULL) |
| 9256 | cur = cur->next; |
| 9257 | else { |
| 9258 | cur = cur->parent; |
| 9259 | goto next_sibling; |
| 9260 | } |
| 9261 | } |
| 9262 | return (0); |
| 9263 | internal_error: |
| 9264 | return (-1); |
| 9265 | } |
| 9266 | |
| 9267 | /* |
| 9268 | * xmlDOMWrapAdoptNode: |
| 9269 | * @ctxt: the optional context for custom processing |
| 9270 | * @sourceDoc: the optional sourceDoc |
| 9271 | * @node: the node to start with |
| 9272 | * @destDoc: the destination doc |
Kasimier T. Buchcik | 4d9c948 | 2005-06-27 15:04:46 +0000 | [diff] [blame] | 9273 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9274 | * @options: option flags |
| 9275 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9276 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 9277 | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
| 9278 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used |
| 9279 | * This is the case when you have an unliked node and just want to move it |
| 9280 | * to the context of |
| 9281 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9282 | * If @destParent is given, it ensures that the tree is namespace |
| 9283 | * wellformed by creating additional ns-decls where needed. |
| 9284 | * Note that, since prefixes of already existent ns-decls can be |
| 9285 | * shadowed by this process, it could break QNames in attribute |
| 9286 | * values or element content. |
Kasimier T. Buchcik | 017264f | 2005-06-27 13:45:24 +0000 | [diff] [blame] | 9287 | * WARNING: This function is in a experimental state. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9288 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9289 | * Returns 0 if the operation succeeded, |
| 9290 | * 1 if a node of unsupported type was given, |
| 9291 | * 2 if a node of not yet supported type was given and |
| 9292 | * -1 on API/internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9293 | */ |
| 9294 | int |
| 9295 | xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt, |
| 9296 | xmlDocPtr sourceDoc, |
| 9297 | xmlNodePtr node, |
| 9298 | xmlDocPtr destDoc, |
| 9299 | xmlNodePtr destParent, |
| 9300 | int options) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9301 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9302 | if ((node == NULL) || (destDoc == NULL) || |
| 9303 | ((destParent != NULL) && (destParent->doc != destDoc))) |
| 9304 | return(-1); |
| 9305 | /* |
| 9306 | * Check node->doc sanity. |
| 9307 | */ |
| 9308 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 9309 | (node->doc != sourceDoc)) { |
| 9310 | /* |
| 9311 | * Might be an XIncluded node. |
| 9312 | */ |
| 9313 | return (-1); |
| 9314 | } |
| 9315 | if (sourceDoc == NULL) |
| 9316 | sourceDoc = node->doc; |
| 9317 | if (sourceDoc == destDoc) |
| 9318 | return (-1); |
| 9319 | switch (node->type) { |
| 9320 | case XML_ELEMENT_NODE: |
| 9321 | case XML_ATTRIBUTE_NODE: |
| 9322 | case XML_TEXT_NODE: |
| 9323 | case XML_CDATA_SECTION_NODE: |
| 9324 | case XML_ENTITY_REF_NODE: |
| 9325 | case XML_PI_NODE: |
| 9326 | case XML_COMMENT_NODE: |
| 9327 | break; |
| 9328 | case XML_DOCUMENT_FRAG_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9329 | /* TODO: Support document-fragment-nodes. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9330 | return (2); |
| 9331 | default: |
| 9332 | return (1); |
| 9333 | } |
| 9334 | /* |
| 9335 | * Unlink only if @node was not already added to @destParent. |
| 9336 | */ |
| 9337 | if ((node->parent != NULL) && (destParent != node->parent)) |
| 9338 | xmlUnlinkNode(node); |
| 9339 | |
| 9340 | if (node->type == XML_ELEMENT_NODE) { |
| 9341 | return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node, |
| 9342 | destDoc, destParent, options)); |
| 9343 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
| 9344 | return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc, |
| 9345 | (xmlAttrPtr) node, destDoc, destParent, options)); |
| 9346 | } else { |
| 9347 | xmlNodePtr cur = node; |
| 9348 | int adoptStr = 1; |
| 9349 | |
| 9350 | cur->doc = destDoc; |
| 9351 | /* |
| 9352 | * Optimize string adoption. |
| 9353 | */ |
| 9354 | if ((sourceDoc != NULL) && |
| 9355 | (sourceDoc->dict == destDoc->dict)) |
| 9356 | adoptStr = 0; |
| 9357 | switch (node->type) { |
| 9358 | case XML_TEXT_NODE: |
| 9359 | case XML_CDATA_SECTION_NODE: |
| 9360 | XML_TREE_ADOPT_STR_2(node->content) |
| 9361 | break; |
| 9362 | case XML_ENTITY_REF_NODE: |
| 9363 | /* |
| 9364 | * Remove reference to the entitity-node. |
| 9365 | */ |
| 9366 | node->content = NULL; |
| 9367 | node->children = NULL; |
| 9368 | node->last = NULL; |
| 9369 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9370 | xmlEntityPtr ent; |
| 9371 | /* |
| 9372 | * Assign new entity-node if available. |
| 9373 | */ |
| 9374 | ent = xmlGetDocEntity(destDoc, node->name); |
| 9375 | if (ent != NULL) { |
| 9376 | node->content = ent->content; |
| 9377 | node->children = (xmlNodePtr) ent; |
| 9378 | node->last = (xmlNodePtr) ent; |
| 9379 | } |
| 9380 | } |
| 9381 | XML_TREE_ADOPT_STR(node->name) |
| 9382 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 9383 | case XML_PI_NODE: { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9384 | XML_TREE_ADOPT_STR(node->name) |
| 9385 | XML_TREE_ADOPT_STR_2(node->content) |
| 9386 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 9387 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9388 | default: |
| 9389 | break; |
| 9390 | } |
| 9391 | } |
| 9392 | return (0); |
| 9393 | } |
| 9394 | |
| 9395 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9396 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 9397 | #define bottom_tree |
| 9398 | #include "elfgcchack.h" |