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 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 46 | /************************************************************************ |
| 47 | * * |
| 48 | * Forward declarations * |
| 49 | * * |
| 50 | ************************************************************************/ |
| 51 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 52 | xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns); |
| 53 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 54 | static xmlChar* xmlGetPropNodeValueInternal(xmlAttrPtr prop); |
| 55 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 56 | /************************************************************************ |
| 57 | * * |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 58 | * Tree memory error handler * |
| 59 | * * |
| 60 | ************************************************************************/ |
| 61 | /** |
| 62 | * xmlTreeErrMemory: |
| 63 | * @extra: extra informations |
| 64 | * |
| 65 | * Handle an out of memory condition |
| 66 | */ |
| 67 | static void |
| 68 | xmlTreeErrMemory(const char *extra) |
| 69 | { |
| 70 | __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * xmlTreeErr: |
| 75 | * @code: the error number |
| 76 | * @extra: extra informations |
| 77 | * |
| 78 | * Handle an out of memory condition |
| 79 | */ |
| 80 | static void |
| 81 | xmlTreeErr(int code, xmlNodePtr node, const char *extra) |
| 82 | { |
| 83 | const char *msg = NULL; |
| 84 | |
| 85 | switch(code) { |
| 86 | case XML_TREE_INVALID_HEX: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 87 | msg = "invalid hexadecimal character value\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 88 | break; |
| 89 | case XML_TREE_INVALID_DEC: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 90 | msg = "invalid decimal character value\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 91 | break; |
| 92 | case XML_TREE_UNTERMINATED_ENTITY: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 93 | msg = "unterminated entity reference %15s\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 94 | break; |
| 95 | default: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 96 | msg = "unexpected error number\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 97 | } |
| 98 | __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra); |
| 99 | } |
| 100 | |
| 101 | /************************************************************************ |
| 102 | * * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 103 | * A few static variables and macros * |
| 104 | * * |
| 105 | ************************************************************************/ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 106 | /* #undef xmlStringText */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 107 | const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 108 | /* #undef xmlStringTextNoenc */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 109 | const xmlChar xmlStringTextNoenc[] = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 110 | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 111 | /* #undef xmlStringComment */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 112 | const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
| 113 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 114 | static int xmlCompressMode = 0; |
| 115 | static int xmlCheckDTD = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 116 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 117 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
| 118 | xmlNodePtr ulccur = (n)->children; \ |
| 119 | if (ulccur == NULL) { \ |
| 120 | (n)->last = NULL; \ |
| 121 | } else { \ |
| 122 | while (ulccur->next != NULL) { \ |
| 123 | ulccur->parent = (n); \ |
| 124 | ulccur = ulccur->next; \ |
| 125 | } \ |
| 126 | ulccur->parent = (n); \ |
| 127 | (n)->last = ulccur; \ |
| 128 | }} |
| 129 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 130 | #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \ |
| 131 | (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0)) |
| 132 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 133 | /* #define DEBUG_BUFFER */ |
| 134 | /* #define DEBUG_TREE */ |
| 135 | |
| 136 | /************************************************************************ |
| 137 | * * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 138 | * Functions to move to entities.c once the * |
| 139 | * API freeze is smoothen and they can be made public. * |
| 140 | * * |
| 141 | ************************************************************************/ |
| 142 | #include <libxml/hash.h> |
| 143 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 144 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 145 | /** |
| 146 | * xmlGetEntityFromDtd: |
| 147 | * @dtd: A pointer to the DTD to search |
| 148 | * @name: The entity name |
| 149 | * |
| 150 | * Do an entity lookup in the DTD entity hash table and |
| 151 | * return the corresponding entity, if found. |
| 152 | * |
| 153 | * Returns A pointer to the entity structure or NULL if not found. |
| 154 | */ |
| 155 | static xmlEntityPtr |
| 156 | xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 157 | xmlEntitiesTablePtr table; |
| 158 | |
| 159 | if((dtd != NULL) && (dtd->entities != NULL)) { |
| 160 | table = (xmlEntitiesTablePtr) dtd->entities; |
| 161 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 162 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 163 | } |
| 164 | return(NULL); |
| 165 | } |
| 166 | /** |
| 167 | * xmlGetParameterEntityFromDtd: |
| 168 | * @dtd: A pointer to the DTD to search |
| 169 | * @name: The entity name |
| 170 | * |
| 171 | * Do an entity lookup in the DTD pararmeter entity hash table and |
| 172 | * return the corresponding entity, if found. |
| 173 | * |
| 174 | * Returns A pointer to the entity structure or NULL if not found. |
| 175 | */ |
| 176 | static xmlEntityPtr |
| 177 | xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 178 | xmlEntitiesTablePtr table; |
| 179 | |
| 180 | if ((dtd != NULL) && (dtd->pentities != NULL)) { |
| 181 | table = (xmlEntitiesTablePtr) dtd->pentities; |
| 182 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 183 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 184 | } |
| 185 | return(NULL); |
| 186 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 187 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 188 | |
| 189 | /************************************************************************ |
| 190 | * * |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 191 | * QName handling helper * |
| 192 | * * |
| 193 | ************************************************************************/ |
| 194 | |
| 195 | /** |
| 196 | * xmlBuildQName: |
| 197 | * @ncname: the Name |
| 198 | * @prefix: the prefix |
| 199 | * @memory: preallocated memory |
| 200 | * @len: preallocated memory length |
| 201 | * |
| 202 | * Builds the QName @prefix:@ncname in @memory if there is enough space |
| 203 | * and prefix is not NULL nor empty, otherwise allocate a new string. |
| 204 | * If prefix is NULL or empty it returns ncname. |
| 205 | * |
| 206 | * Returns the new string which must be freed by the caller if different from |
| 207 | * @memory and @ncname or NULL in case of error |
| 208 | */ |
| 209 | xmlChar * |
| 210 | xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix, |
| 211 | xmlChar *memory, int len) { |
| 212 | int lenn, lenp; |
| 213 | xmlChar *ret; |
| 214 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 215 | if (ncname == NULL) return(NULL); |
| 216 | if (prefix == NULL) return((xmlChar *) ncname); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 217 | |
| 218 | lenn = strlen((char *) ncname); |
| 219 | lenp = strlen((char *) prefix); |
| 220 | |
| 221 | if ((memory == NULL) || (len < lenn + lenp + 2)) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 222 | ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 223 | if (ret == NULL) { |
| 224 | xmlTreeErrMemory("building QName"); |
| 225 | return(NULL); |
| 226 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 227 | } else { |
| 228 | ret = memory; |
| 229 | } |
| 230 | memcpy(&ret[0], prefix, lenp); |
| 231 | ret[lenp] = ':'; |
| 232 | memcpy(&ret[lenp + 1], ncname, lenn); |
| 233 | ret[lenn + lenp + 1] = 0; |
| 234 | return(ret); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * xmlSplitQName2: |
| 239 | * @name: the full QName |
| 240 | * @prefix: a xmlChar ** |
| 241 | * |
| 242 | * parse an XML qualified name string |
| 243 | * |
| 244 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 245 | * |
| 246 | * [NS 6] Prefix ::= NCName |
| 247 | * |
| 248 | * [NS 7] LocalPart ::= NCName |
| 249 | * |
| 250 | * Returns NULL if not a QName, otherwise the local part, and prefix |
| 251 | * is updated to get the Prefix if any. |
| 252 | */ |
| 253 | |
| 254 | xmlChar * |
| 255 | xmlSplitQName2(const xmlChar *name, xmlChar **prefix) { |
| 256 | int len = 0; |
| 257 | xmlChar *ret = NULL; |
| 258 | |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 259 | if (prefix == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 260 | *prefix = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 261 | if (name == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 262 | |
| 263 | #ifndef XML_XML_NAMESPACE |
| 264 | /* xml: prefix is not really a namespace */ |
| 265 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 266 | (name[2] == 'l') && (name[3] == ':')) |
| 267 | return(NULL); |
| 268 | #endif |
| 269 | |
| 270 | /* nasty but valid */ |
| 271 | if (name[0] == ':') |
| 272 | return(NULL); |
| 273 | |
| 274 | /* |
| 275 | * we are not trying to validate but just to cut, and yes it will |
| 276 | * work even if this is as set of UTF-8 encoded chars |
| 277 | */ |
| 278 | while ((name[len] != 0) && (name[len] != ':')) |
| 279 | len++; |
| 280 | |
| 281 | if (name[len] == 0) |
| 282 | return(NULL); |
| 283 | |
| 284 | *prefix = xmlStrndup(name, len); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 285 | if (*prefix == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 286 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 287 | return(NULL); |
| 288 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 289 | ret = xmlStrdup(&name[len + 1]); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 290 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 291 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 292 | if (*prefix != NULL) { |
| 293 | xmlFree(*prefix); |
| 294 | *prefix = NULL; |
| 295 | } |
| 296 | return(NULL); |
| 297 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 298 | |
| 299 | return(ret); |
| 300 | } |
| 301 | |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 302 | /** |
| 303 | * xmlSplitQName3: |
| 304 | * @name: the full QName |
| 305 | * @len: an int * |
| 306 | * |
| 307 | * parse an XML qualified name string,i |
| 308 | * |
| 309 | * returns NULL if it is not a Qualified Name, otherwise, update len |
| 310 | * with the lenght in byte of the prefix and return a pointer |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 311 | * to the start of the name without the prefix |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 312 | */ |
| 313 | |
| 314 | const xmlChar * |
| 315 | xmlSplitQName3(const xmlChar *name, int *len) { |
| 316 | int l = 0; |
| 317 | |
| 318 | if (name == NULL) return(NULL); |
| 319 | if (len == NULL) return(NULL); |
| 320 | |
| 321 | /* nasty but valid */ |
| 322 | if (name[0] == ':') |
| 323 | return(NULL); |
| 324 | |
| 325 | /* |
| 326 | * we are not trying to validate but just to cut, and yes it will |
| 327 | * work even if this is as set of UTF-8 encoded chars |
| 328 | */ |
| 329 | while ((name[l] != 0) && (name[l] != ':')) |
| 330 | l++; |
| 331 | |
| 332 | if (name[l] == 0) |
| 333 | return(NULL); |
| 334 | |
| 335 | *len = l; |
| 336 | |
| 337 | return(&name[l+1]); |
| 338 | } |
| 339 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 340 | /************************************************************************ |
| 341 | * * |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 342 | * Check Name, NCName and QName strings * |
| 343 | * * |
| 344 | ************************************************************************/ |
| 345 | |
| 346 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l) |
| 347 | |
Daniel Veillard | 6b6d680 | 2005-07-03 21:00:34 +0000 | [diff] [blame] | 348 | #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] | 349 | /** |
| 350 | * xmlValidateNCName: |
| 351 | * @value: the value to check |
| 352 | * @space: allow spaces in front and end of the string |
| 353 | * |
| 354 | * Check that a value conforms to the lexical space of NCName |
| 355 | * |
| 356 | * Returns 0 if this validates, a positive error code number otherwise |
| 357 | * and -1 in case of internal or API error. |
| 358 | */ |
| 359 | int |
| 360 | xmlValidateNCName(const xmlChar *value, int space) { |
| 361 | const xmlChar *cur = value; |
| 362 | int c,l; |
| 363 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 364 | if (value == NULL) |
| 365 | return(-1); |
| 366 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 367 | /* |
| 368 | * First quick algorithm for ASCII range |
| 369 | */ |
| 370 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 371 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 372 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 373 | (*cur == '_')) |
| 374 | cur++; |
| 375 | else |
| 376 | goto try_complex; |
| 377 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 378 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 379 | ((*cur >= '0') && (*cur <= '9')) || |
| 380 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 381 | cur++; |
| 382 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 383 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 384 | if (*cur == 0) |
| 385 | return(0); |
| 386 | |
| 387 | try_complex: |
| 388 | /* |
| 389 | * Second check for chars outside the ASCII range |
| 390 | */ |
| 391 | cur = value; |
| 392 | c = CUR_SCHAR(cur, l); |
| 393 | if (space) { |
| 394 | while (IS_BLANK(c)) { |
| 395 | cur += l; |
| 396 | c = CUR_SCHAR(cur, l); |
| 397 | } |
| 398 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 399 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 400 | return(1); |
| 401 | cur += l; |
| 402 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 403 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 404 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 405 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 406 | cur += l; |
| 407 | c = CUR_SCHAR(cur, l); |
| 408 | } |
| 409 | if (space) { |
| 410 | while (IS_BLANK(c)) { |
| 411 | cur += l; |
| 412 | c = CUR_SCHAR(cur, l); |
| 413 | } |
| 414 | } |
| 415 | if (c != 0) |
| 416 | return(1); |
| 417 | |
| 418 | return(0); |
| 419 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 420 | #endif |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 421 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 422 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 423 | /** |
| 424 | * xmlValidateQName: |
| 425 | * @value: the value to check |
| 426 | * @space: allow spaces in front and end of the string |
| 427 | * |
| 428 | * Check that a value conforms to the lexical space of QName |
| 429 | * |
| 430 | * Returns 0 if this validates, a positive error code number otherwise |
| 431 | * and -1 in case of internal or API error. |
| 432 | */ |
| 433 | int |
| 434 | xmlValidateQName(const xmlChar *value, int space) { |
| 435 | const xmlChar *cur = value; |
| 436 | int c,l; |
| 437 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 438 | if (value == NULL) |
| 439 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 440 | /* |
| 441 | * First quick algorithm for ASCII range |
| 442 | */ |
| 443 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 444 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 445 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 446 | (*cur == '_')) |
| 447 | cur++; |
| 448 | else |
| 449 | goto try_complex; |
| 450 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 451 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 452 | ((*cur >= '0') && (*cur <= '9')) || |
| 453 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 454 | cur++; |
| 455 | if (*cur == ':') { |
| 456 | cur++; |
| 457 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 458 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 459 | (*cur == '_')) |
| 460 | cur++; |
| 461 | else |
| 462 | goto try_complex; |
| 463 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 464 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 465 | ((*cur >= '0') && (*cur <= '9')) || |
| 466 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 467 | cur++; |
| 468 | } |
| 469 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 470 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 471 | if (*cur == 0) |
| 472 | return(0); |
| 473 | |
| 474 | try_complex: |
| 475 | /* |
| 476 | * Second check for chars outside the ASCII range |
| 477 | */ |
| 478 | cur = value; |
| 479 | c = CUR_SCHAR(cur, l); |
| 480 | if (space) { |
| 481 | while (IS_BLANK(c)) { |
| 482 | cur += l; |
| 483 | c = CUR_SCHAR(cur, l); |
| 484 | } |
| 485 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 486 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 487 | return(1); |
| 488 | cur += l; |
| 489 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 490 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 491 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 492 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 493 | cur += l; |
| 494 | c = CUR_SCHAR(cur, l); |
| 495 | } |
| 496 | if (c == ':') { |
| 497 | cur += l; |
| 498 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 499 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 500 | return(1); |
| 501 | cur += l; |
| 502 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 503 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 504 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 505 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 506 | cur += l; |
| 507 | c = CUR_SCHAR(cur, l); |
| 508 | } |
| 509 | } |
| 510 | if (space) { |
| 511 | while (IS_BLANK(c)) { |
| 512 | cur += l; |
| 513 | c = CUR_SCHAR(cur, l); |
| 514 | } |
| 515 | } |
| 516 | if (c != 0) |
| 517 | return(1); |
| 518 | return(0); |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * xmlValidateName: |
| 523 | * @value: the value to check |
| 524 | * @space: allow spaces in front and end of the string |
| 525 | * |
| 526 | * Check that a value conforms to the lexical space of Name |
| 527 | * |
| 528 | * Returns 0 if this validates, a positive error code number otherwise |
| 529 | * and -1 in case of internal or API error. |
| 530 | */ |
| 531 | int |
| 532 | xmlValidateName(const xmlChar *value, int space) { |
| 533 | const xmlChar *cur = value; |
| 534 | int c,l; |
| 535 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 536 | if (value == NULL) |
| 537 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 538 | /* |
| 539 | * First quick algorithm for ASCII range |
| 540 | */ |
| 541 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 542 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 543 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 544 | (*cur == '_') || (*cur == ':')) |
| 545 | cur++; |
| 546 | else |
| 547 | goto try_complex; |
| 548 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 549 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 550 | ((*cur >= '0') && (*cur <= '9')) || |
| 551 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 552 | cur++; |
| 553 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 554 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 555 | if (*cur == 0) |
| 556 | return(0); |
| 557 | |
| 558 | try_complex: |
| 559 | /* |
| 560 | * Second check for chars outside the ASCII range |
| 561 | */ |
| 562 | cur = value; |
| 563 | c = CUR_SCHAR(cur, l); |
| 564 | if (space) { |
| 565 | while (IS_BLANK(c)) { |
| 566 | cur += l; |
| 567 | c = CUR_SCHAR(cur, l); |
| 568 | } |
| 569 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 570 | if ((!IS_LETTER(c)) && (c != '_') && (c != ':')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 571 | return(1); |
| 572 | cur += l; |
| 573 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 574 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 575 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 576 | cur += l; |
| 577 | c = CUR_SCHAR(cur, l); |
| 578 | } |
| 579 | if (space) { |
| 580 | while (IS_BLANK(c)) { |
| 581 | cur += l; |
| 582 | c = CUR_SCHAR(cur, l); |
| 583 | } |
| 584 | } |
| 585 | if (c != 0) |
| 586 | return(1); |
| 587 | return(0); |
| 588 | } |
| 589 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 590 | /** |
| 591 | * xmlValidateNMToken: |
| 592 | * @value: the value to check |
| 593 | * @space: allow spaces in front and end of the string |
| 594 | * |
| 595 | * Check that a value conforms to the lexical space of NMToken |
| 596 | * |
| 597 | * Returns 0 if this validates, a positive error code number otherwise |
| 598 | * and -1 in case of internal or API error. |
| 599 | */ |
| 600 | int |
| 601 | xmlValidateNMToken(const xmlChar *value, int space) { |
| 602 | const xmlChar *cur = value; |
| 603 | int c,l; |
| 604 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 605 | if (value == NULL) |
| 606 | return(-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 607 | /* |
| 608 | * First quick algorithm for ASCII range |
| 609 | */ |
| 610 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 611 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 612 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 613 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 614 | ((*cur >= '0') && (*cur <= '9')) || |
| 615 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 616 | cur++; |
| 617 | else |
| 618 | goto try_complex; |
| 619 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 620 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 621 | ((*cur >= '0') && (*cur <= '9')) || |
| 622 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 623 | cur++; |
| 624 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 625 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 626 | if (*cur == 0) |
| 627 | return(0); |
| 628 | |
| 629 | try_complex: |
| 630 | /* |
| 631 | * Second check for chars outside the ASCII range |
| 632 | */ |
| 633 | cur = value; |
| 634 | c = CUR_SCHAR(cur, l); |
| 635 | if (space) { |
| 636 | while (IS_BLANK(c)) { |
| 637 | cur += l; |
| 638 | c = CUR_SCHAR(cur, l); |
| 639 | } |
| 640 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 641 | if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 642 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c))) |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 643 | return(1); |
| 644 | cur += l; |
| 645 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 646 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 647 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 648 | cur += l; |
| 649 | c = CUR_SCHAR(cur, l); |
| 650 | } |
| 651 | if (space) { |
| 652 | while (IS_BLANK(c)) { |
| 653 | cur += l; |
| 654 | c = CUR_SCHAR(cur, l); |
| 655 | } |
| 656 | } |
| 657 | if (c != 0) |
| 658 | return(1); |
| 659 | return(0); |
| 660 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 661 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 662 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 663 | /************************************************************************ |
| 664 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 665 | * Allocation and deallocation of basic structures * |
| 666 | * * |
| 667 | ************************************************************************/ |
| 668 | |
| 669 | /** |
| 670 | * xmlSetBufferAllocationScheme: |
| 671 | * @scheme: allocation method to use |
| 672 | * |
| 673 | * Set the buffer allocation method. Types are |
| 674 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 675 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 676 | * improves performance |
| 677 | */ |
| 678 | void |
| 679 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
| 680 | xmlBufferAllocScheme = scheme; |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * xmlGetBufferAllocationScheme: |
| 685 | * |
| 686 | * Types are |
| 687 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 688 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 689 | * improves performance |
| 690 | * |
| 691 | * Returns the current allocation scheme |
| 692 | */ |
| 693 | xmlBufferAllocationScheme |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 694 | xmlGetBufferAllocationScheme(void) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 695 | return(xmlBufferAllocScheme); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /** |
| 699 | * xmlNewNs: |
| 700 | * @node: the element carrying the namespace |
| 701 | * @href: the URI associated |
| 702 | * @prefix: the prefix for the namespace |
| 703 | * |
| 704 | * Creation of a new Namespace. This function will refuse to create |
| 705 | * a namespace with a similar prefix than an existing one present on this |
| 706 | * node. |
| 707 | * We use href==NULL in the case of an element creation where the namespace |
| 708 | * was not defined. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 709 | * Returns a new namespace pointer or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 710 | */ |
| 711 | xmlNsPtr |
| 712 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
| 713 | xmlNsPtr cur; |
| 714 | |
| 715 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 716 | return(NULL); |
| 717 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 718 | if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) |
| 719 | return(NULL); |
| 720 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 721 | /* |
| 722 | * Allocate a new Namespace and fill the fields. |
| 723 | */ |
| 724 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 725 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 726 | xmlTreeErrMemory("building namespace"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 727 | return(NULL); |
| 728 | } |
| 729 | memset(cur, 0, sizeof(xmlNs)); |
| 730 | cur->type = XML_LOCAL_NAMESPACE; |
| 731 | |
| 732 | if (href != NULL) |
| 733 | cur->href = xmlStrdup(href); |
| 734 | if (prefix != NULL) |
| 735 | cur->prefix = xmlStrdup(prefix); |
| 736 | |
| 737 | /* |
| 738 | * Add it at the end to preserve parsing order ... |
| 739 | * and checks for existing use of the prefix |
| 740 | */ |
| 741 | if (node != NULL) { |
| 742 | if (node->nsDef == NULL) { |
| 743 | node->nsDef = cur; |
| 744 | } else { |
| 745 | xmlNsPtr prev = node->nsDef; |
| 746 | |
| 747 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 748 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 749 | xmlFreeNs(cur); |
| 750 | return(NULL); |
| 751 | } |
| 752 | while (prev->next != NULL) { |
| 753 | prev = prev->next; |
| 754 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 755 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 756 | xmlFreeNs(cur); |
| 757 | return(NULL); |
| 758 | } |
| 759 | } |
| 760 | prev->next = cur; |
| 761 | } |
| 762 | } |
| 763 | return(cur); |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * xmlSetNs: |
| 768 | * @node: a node in the document |
| 769 | * @ns: a namespace pointer |
| 770 | * |
| 771 | * Associate a namespace to a node, a posteriori. |
| 772 | */ |
| 773 | void |
| 774 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
| 775 | if (node == NULL) { |
| 776 | #ifdef DEBUG_TREE |
| 777 | xmlGenericError(xmlGenericErrorContext, |
| 778 | "xmlSetNs: node == NULL\n"); |
| 779 | #endif |
| 780 | return; |
| 781 | } |
| 782 | node->ns = ns; |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * xmlFreeNs: |
| 787 | * @cur: the namespace pointer |
| 788 | * |
| 789 | * Free up the structures associated to a namespace |
| 790 | */ |
| 791 | void |
| 792 | xmlFreeNs(xmlNsPtr cur) { |
| 793 | if (cur == NULL) { |
| 794 | #ifdef DEBUG_TREE |
| 795 | xmlGenericError(xmlGenericErrorContext, |
| 796 | "xmlFreeNs : ns == NULL\n"); |
| 797 | #endif |
| 798 | return; |
| 799 | } |
| 800 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 801 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 802 | xmlFree(cur); |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * xmlFreeNsList: |
| 807 | * @cur: the first namespace pointer |
| 808 | * |
| 809 | * Free up all the structures associated to the chained namespaces. |
| 810 | */ |
| 811 | void |
| 812 | xmlFreeNsList(xmlNsPtr cur) { |
| 813 | xmlNsPtr next; |
| 814 | if (cur == NULL) { |
| 815 | #ifdef DEBUG_TREE |
| 816 | xmlGenericError(xmlGenericErrorContext, |
| 817 | "xmlFreeNsList : ns == NULL\n"); |
| 818 | #endif |
| 819 | return; |
| 820 | } |
| 821 | while (cur != NULL) { |
| 822 | next = cur->next; |
| 823 | xmlFreeNs(cur); |
| 824 | cur = next; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * xmlNewDtd: |
| 830 | * @doc: the document pointer |
| 831 | * @name: the DTD name |
| 832 | * @ExternalID: the external ID |
| 833 | * @SystemID: the system ID |
| 834 | * |
| 835 | * Creation of a new DTD for the external subset. To create an |
| 836 | * internal subset, use xmlCreateIntSubset(). |
| 837 | * |
| 838 | * Returns a pointer to the new DTD structure |
| 839 | */ |
| 840 | xmlDtdPtr |
| 841 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 842 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 843 | xmlDtdPtr cur; |
| 844 | |
| 845 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
| 846 | #ifdef DEBUG_TREE |
| 847 | xmlGenericError(xmlGenericErrorContext, |
| 848 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
| 849 | /* !!! */ (char *) name, doc->name, |
| 850 | /* !!! */ (char *)doc->extSubset->name); |
| 851 | #endif |
| 852 | return(NULL); |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * Allocate a new DTD and fill the fields. |
| 857 | */ |
| 858 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 859 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 860 | xmlTreeErrMemory("building DTD"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 861 | return(NULL); |
| 862 | } |
| 863 | memset(cur, 0 , sizeof(xmlDtd)); |
| 864 | cur->type = XML_DTD_NODE; |
| 865 | |
| 866 | if (name != NULL) |
| 867 | cur->name = xmlStrdup(name); |
| 868 | if (ExternalID != NULL) |
| 869 | cur->ExternalID = xmlStrdup(ExternalID); |
| 870 | if (SystemID != NULL) |
| 871 | cur->SystemID = xmlStrdup(SystemID); |
| 872 | if (doc != NULL) |
| 873 | doc->extSubset = cur; |
| 874 | cur->doc = doc; |
| 875 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 876 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 877 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 878 | return(cur); |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * xmlGetIntSubset: |
| 883 | * @doc: the document pointer |
| 884 | * |
| 885 | * Get the internal subset of a document |
| 886 | * Returns a pointer to the DTD structure or NULL if not found |
| 887 | */ |
| 888 | |
| 889 | xmlDtdPtr |
| 890 | xmlGetIntSubset(xmlDocPtr doc) { |
| 891 | xmlNodePtr cur; |
| 892 | |
| 893 | if (doc == NULL) |
| 894 | return(NULL); |
| 895 | cur = doc->children; |
| 896 | while (cur != NULL) { |
| 897 | if (cur->type == XML_DTD_NODE) |
| 898 | return((xmlDtdPtr) cur); |
| 899 | cur = cur->next; |
| 900 | } |
| 901 | return((xmlDtdPtr) doc->intSubset); |
| 902 | } |
| 903 | |
| 904 | /** |
| 905 | * xmlCreateIntSubset: |
| 906 | * @doc: the document pointer |
| 907 | * @name: the DTD name |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 908 | * @ExternalID: the external (PUBLIC) ID |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 909 | * @SystemID: the system ID |
| 910 | * |
| 911 | * Create the internal subset of a document |
| 912 | * Returns a pointer to the new DTD structure |
| 913 | */ |
| 914 | xmlDtdPtr |
| 915 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 916 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 917 | xmlDtdPtr cur; |
| 918 | |
| 919 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
| 920 | #ifdef DEBUG_TREE |
| 921 | xmlGenericError(xmlGenericErrorContext, |
| 922 | |
| 923 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 924 | doc->name); |
| 925 | #endif |
| 926 | return(NULL); |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | * Allocate a new DTD and fill the fields. |
| 931 | */ |
| 932 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 933 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 934 | xmlTreeErrMemory("building internal subset"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 935 | return(NULL); |
| 936 | } |
| 937 | memset(cur, 0, sizeof(xmlDtd)); |
| 938 | cur->type = XML_DTD_NODE; |
| 939 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 940 | if (name != NULL) { |
| 941 | cur->name = xmlStrdup(name); |
| 942 | if (cur->name == NULL) { |
| 943 | xmlTreeErrMemory("building internal subset"); |
| 944 | xmlFree(cur); |
| 945 | return(NULL); |
| 946 | } |
| 947 | } |
| 948 | if (ExternalID != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 949 | cur->ExternalID = xmlStrdup(ExternalID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 950 | if (cur->ExternalID == NULL) { |
| 951 | xmlTreeErrMemory("building internal subset"); |
| 952 | if (cur->name != NULL) |
| 953 | xmlFree((char *)cur->name); |
| 954 | xmlFree(cur); |
| 955 | return(NULL); |
| 956 | } |
| 957 | } |
| 958 | if (SystemID != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 959 | cur->SystemID = xmlStrdup(SystemID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 960 | if (cur->SystemID == NULL) { |
| 961 | xmlTreeErrMemory("building internal subset"); |
| 962 | if (cur->name != NULL) |
| 963 | xmlFree((char *)cur->name); |
| 964 | if (cur->ExternalID != NULL) |
| 965 | xmlFree((char *)cur->ExternalID); |
| 966 | xmlFree(cur); |
| 967 | return(NULL); |
| 968 | } |
| 969 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 970 | if (doc != NULL) { |
| 971 | doc->intSubset = cur; |
| 972 | cur->parent = doc; |
| 973 | cur->doc = doc; |
| 974 | if (doc->children == NULL) { |
| 975 | doc->children = (xmlNodePtr) cur; |
| 976 | doc->last = (xmlNodePtr) cur; |
| 977 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 978 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 979 | xmlNodePtr prev; |
| 980 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 981 | prev = doc->children; |
| 982 | prev->prev = (xmlNodePtr) cur; |
| 983 | cur->next = prev; |
| 984 | doc->children = (xmlNodePtr) cur; |
| 985 | } else { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 986 | xmlNodePtr next; |
| 987 | |
| 988 | next = doc->children; |
| 989 | while ((next != NULL) && (next->type != XML_ELEMENT_NODE)) |
| 990 | next = next->next; |
| 991 | if (next == NULL) { |
| 992 | cur->prev = doc->last; |
| 993 | cur->prev->next = (xmlNodePtr) cur; |
| 994 | cur->next = NULL; |
| 995 | doc->last = (xmlNodePtr) cur; |
| 996 | } else { |
| 997 | cur->next = next; |
| 998 | cur->prev = next->prev; |
| 999 | if (cur->prev == NULL) |
| 1000 | doc->children = (xmlNodePtr) cur; |
| 1001 | else |
| 1002 | cur->prev->next = (xmlNodePtr) cur; |
| 1003 | next->prev = (xmlNodePtr) cur; |
| 1004 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1008 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1009 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1010 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1011 | return(cur); |
| 1012 | } |
| 1013 | |
| 1014 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1015 | * DICT_FREE: |
| 1016 | * @str: a string |
| 1017 | * |
| 1018 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 1019 | * current scope |
| 1020 | */ |
| 1021 | #define DICT_FREE(str) \ |
| 1022 | if ((str) && ((!dict) || \ |
| 1023 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 1024 | xmlFree((char *)(str)); |
| 1025 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 1026 | |
| 1027 | /** |
| 1028 | * DICT_COPY: |
| 1029 | * @str: a string |
| 1030 | * |
| 1031 | * Copy a string using a "dict" dictionnary in the current scope, |
| 1032 | * if availabe. |
| 1033 | */ |
| 1034 | #define DICT_COPY(str, cpy) \ |
| 1035 | if (str) { \ |
| 1036 | if (dict) { \ |
| 1037 | if (xmlDictOwns(dict, (const xmlChar *)(str))) \ |
| 1038 | cpy = (xmlChar *) (str); \ |
| 1039 | else \ |
| 1040 | cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \ |
| 1041 | } else \ |
| 1042 | cpy = xmlStrdup((const xmlChar *)(str)); } |
| 1043 | |
| 1044 | /** |
| 1045 | * DICT_CONST_COPY: |
| 1046 | * @str: a string |
| 1047 | * |
| 1048 | * Copy a string using a "dict" dictionnary in the current scope, |
| 1049 | * if availabe. |
| 1050 | */ |
| 1051 | #define DICT_CONST_COPY(str, cpy) \ |
| 1052 | if (str) { \ |
| 1053 | if (dict) { \ |
| 1054 | if (xmlDictOwns(dict, (const xmlChar *)(str))) \ |
| 1055 | cpy = (const xmlChar *) (str); \ |
| 1056 | else \ |
| 1057 | cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \ |
| 1058 | } else \ |
| 1059 | cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); } |
| 1060 | |
| 1061 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1062 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1063 | * xmlFreeDtd: |
| 1064 | * @cur: the DTD structure to free up |
| 1065 | * |
| 1066 | * Free a DTD structure. |
| 1067 | */ |
| 1068 | void |
| 1069 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1070 | xmlDictPtr dict = NULL; |
| 1071 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1072 | if (cur == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1073 | return; |
| 1074 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1075 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1076 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1077 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1078 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1079 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1080 | if (cur->children != NULL) { |
| 1081 | xmlNodePtr next, c = cur->children; |
| 1082 | |
| 1083 | /* |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1084 | * Cleanup all nodes which are not part of the specific lists |
| 1085 | * of notations, elements, attributes and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1086 | */ |
| 1087 | while (c != NULL) { |
| 1088 | next = c->next; |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1089 | if ((c->type != XML_NOTATION_NODE) && |
| 1090 | (c->type != XML_ELEMENT_DECL) && |
| 1091 | (c->type != XML_ATTRIBUTE_DECL) && |
| 1092 | (c->type != XML_ENTITY_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1093 | xmlUnlinkNode(c); |
| 1094 | xmlFreeNode(c); |
| 1095 | } |
| 1096 | c = next; |
| 1097 | } |
| 1098 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1099 | DICT_FREE(cur->name) |
| 1100 | DICT_FREE(cur->SystemID) |
| 1101 | DICT_FREE(cur->ExternalID) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1102 | /* TODO !!! */ |
| 1103 | if (cur->notations != NULL) |
| 1104 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
| 1105 | |
| 1106 | if (cur->elements != NULL) |
| 1107 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
| 1108 | if (cur->attributes != NULL) |
| 1109 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
| 1110 | if (cur->entities != NULL) |
| 1111 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 1112 | if (cur->pentities != NULL) |
| 1113 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
| 1114 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1115 | xmlFree(cur); |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * xmlNewDoc: |
| 1120 | * @version: xmlChar string giving the version of XML "1.0" |
| 1121 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1122 | * Creates a new XML document |
| 1123 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1124 | * Returns a new document |
| 1125 | */ |
| 1126 | xmlDocPtr |
| 1127 | xmlNewDoc(const xmlChar *version) { |
| 1128 | xmlDocPtr cur; |
| 1129 | |
| 1130 | if (version == NULL) |
| 1131 | version = (const xmlChar *) "1.0"; |
| 1132 | |
| 1133 | /* |
| 1134 | * Allocate a new document and fill the fields. |
| 1135 | */ |
| 1136 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
| 1137 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1138 | xmlTreeErrMemory("building doc"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1139 | return(NULL); |
| 1140 | } |
| 1141 | memset(cur, 0, sizeof(xmlDoc)); |
| 1142 | cur->type = XML_DOCUMENT_NODE; |
| 1143 | |
| 1144 | cur->version = xmlStrdup(version); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 1145 | if (cur->version == NULL) { |
| 1146 | xmlTreeErrMemory("building doc"); |
| 1147 | xmlFree(cur); |
| 1148 | return(NULL); |
| 1149 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1150 | cur->standalone = -1; |
| 1151 | cur->compression = -1; /* not initialized */ |
| 1152 | cur->doc = cur; |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 1153 | /* |
| 1154 | * The in memory encoding is always UTF8 |
| 1155 | * This field will never change and would |
| 1156 | * be obsolete if not for binary compatibility. |
| 1157 | */ |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 1158 | cur->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1159 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1160 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1161 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1162 | return(cur); |
| 1163 | } |
| 1164 | |
| 1165 | /** |
| 1166 | * xmlFreeDoc: |
| 1167 | * @cur: pointer to the document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1168 | * |
| 1169 | * Free up all the structures used by a document, tree included. |
| 1170 | */ |
| 1171 | void |
| 1172 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1173 | xmlDtdPtr extSubset, intSubset; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1174 | xmlDictPtr dict = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1175 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1176 | if (cur == NULL) { |
| 1177 | #ifdef DEBUG_TREE |
| 1178 | xmlGenericError(xmlGenericErrorContext, |
| 1179 | "xmlFreeDoc : document == NULL\n"); |
| 1180 | #endif |
| 1181 | return; |
| 1182 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1183 | #ifdef LIBXML_DEBUG_RUNTIME |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1184 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1185 | xmlDebugCheckDocument(stderr, cur); |
| 1186 | #endif |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1187 | #endif |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1188 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1189 | if (cur != NULL) dict = cur->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1190 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1191 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1192 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1193 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1194 | /* |
| 1195 | * Do this before freeing the children list to avoid ID lookups |
| 1196 | */ |
| 1197 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
| 1198 | cur->ids = NULL; |
| 1199 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
| 1200 | cur->refs = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1201 | extSubset = cur->extSubset; |
| 1202 | intSubset = cur->intSubset; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 1203 | if (intSubset == extSubset) |
| 1204 | extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1205 | if (extSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1206 | xmlUnlinkNode((xmlNodePtr) cur->extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1207 | cur->extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1208 | xmlFreeDtd(extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1209 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1210 | if (intSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1211 | xmlUnlinkNode((xmlNodePtr) cur->intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1212 | cur->intSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1213 | xmlFreeDtd(intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1217 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1218 | |
| 1219 | DICT_FREE(cur->version) |
| 1220 | DICT_FREE(cur->name) |
| 1221 | DICT_FREE(cur->encoding) |
| 1222 | DICT_FREE(cur->URL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1223 | xmlFree(cur); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1224 | if (dict) xmlDictFree(dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | /** |
| 1228 | * xmlStringLenGetNodeList: |
| 1229 | * @doc: the document |
| 1230 | * @value: the value of the text |
| 1231 | * @len: the length of the string value |
| 1232 | * |
| 1233 | * Parse the value string and build the node list associated. Should |
| 1234 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1235 | * Returns a pointer to the first child |
| 1236 | */ |
| 1237 | xmlNodePtr |
| 1238 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
| 1239 | xmlNodePtr ret = NULL, last = NULL; |
| 1240 | xmlNodePtr node; |
| 1241 | xmlChar *val; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1242 | const xmlChar *cur = value, *end = cur + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1243 | const xmlChar *q; |
| 1244 | xmlEntityPtr ent; |
| 1245 | |
| 1246 | if (value == NULL) return(NULL); |
| 1247 | |
| 1248 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1249 | while ((cur < end) && (*cur != 0)) { |
| 1250 | if (cur[0] == '&') { |
| 1251 | int charval = 0; |
| 1252 | xmlChar tmp; |
| 1253 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1254 | /* |
| 1255 | * Save the current text. |
| 1256 | */ |
| 1257 | if (cur != q) { |
| 1258 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1259 | xmlNodeAddContentLen(last, q, cur - q); |
| 1260 | } else { |
| 1261 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1262 | if (node == NULL) return(ret); |
| 1263 | if (last == NULL) |
| 1264 | last = ret = node; |
| 1265 | else { |
| 1266 | last->next = node; |
| 1267 | node->prev = last; |
| 1268 | last = node; |
| 1269 | } |
| 1270 | } |
| 1271 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1272 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1273 | if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) { |
| 1274 | cur += 3; |
| 1275 | if (cur < end) |
| 1276 | tmp = *cur; |
| 1277 | else |
| 1278 | tmp = 0; |
| 1279 | while (tmp != ';') { /* Non input consuming loop */ |
| 1280 | if ((tmp >= '0') && (tmp <= '9')) |
| 1281 | charval = charval * 16 + (tmp - '0'); |
| 1282 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1283 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1284 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1285 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1286 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1287 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1288 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1289 | charval = 0; |
| 1290 | break; |
| 1291 | } |
| 1292 | cur++; |
| 1293 | if (cur < end) |
| 1294 | tmp = *cur; |
| 1295 | else |
| 1296 | tmp = 0; |
| 1297 | } |
| 1298 | if (tmp == ';') |
| 1299 | cur++; |
| 1300 | q = cur; |
| 1301 | } else if ((cur + 1 < end) && (cur[1] == '#')) { |
| 1302 | cur += 2; |
| 1303 | if (cur < end) |
| 1304 | tmp = *cur; |
| 1305 | else |
| 1306 | tmp = 0; |
| 1307 | while (tmp != ';') { /* Non input consuming loops */ |
| 1308 | if ((tmp >= '0') && (tmp <= '9')) |
| 1309 | charval = charval * 10 + (tmp - '0'); |
| 1310 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1311 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1312 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1313 | charval = 0; |
| 1314 | break; |
| 1315 | } |
| 1316 | cur++; |
| 1317 | if (cur < end) |
| 1318 | tmp = *cur; |
| 1319 | else |
| 1320 | tmp = 0; |
| 1321 | } |
| 1322 | if (tmp == ';') |
| 1323 | cur++; |
| 1324 | q = cur; |
| 1325 | } else { |
| 1326 | /* |
| 1327 | * Read the entity string |
| 1328 | */ |
| 1329 | cur++; |
| 1330 | q = cur; |
| 1331 | while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++; |
| 1332 | if ((cur >= end) || (*cur == 0)) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1333 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc, |
| 1334 | (const char *) q); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1335 | return(ret); |
| 1336 | } |
| 1337 | if (cur != q) { |
| 1338 | /* |
| 1339 | * Predefined entities don't generate nodes |
| 1340 | */ |
| 1341 | val = xmlStrndup(q, cur - q); |
| 1342 | ent = xmlGetDocEntity(doc, val); |
| 1343 | if ((ent != NULL) && |
| 1344 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1345 | if (last == NULL) { |
| 1346 | node = xmlNewDocText(doc, ent->content); |
| 1347 | last = ret = node; |
| 1348 | } else if (last->type != XML_TEXT_NODE) { |
| 1349 | node = xmlNewDocText(doc, ent->content); |
| 1350 | last = xmlAddNextSibling(last, node); |
| 1351 | } else |
| 1352 | xmlNodeAddContent(last, ent->content); |
| 1353 | |
| 1354 | } else { |
| 1355 | /* |
| 1356 | * Create a new REFERENCE_REF node |
| 1357 | */ |
| 1358 | node = xmlNewReference(doc, val); |
| 1359 | if (node == NULL) { |
| 1360 | if (val != NULL) xmlFree(val); |
| 1361 | return(ret); |
| 1362 | } |
| 1363 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1364 | xmlNodePtr temp; |
| 1365 | |
| 1366 | ent->children = xmlStringGetNodeList(doc, |
| 1367 | (const xmlChar*)node->content); |
| 1368 | ent->owner = 1; |
| 1369 | temp = ent->children; |
| 1370 | while (temp) { |
| 1371 | temp->parent = (xmlNodePtr)ent; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1372 | ent->last = temp; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1373 | temp = temp->next; |
| 1374 | } |
| 1375 | } |
| 1376 | if (last == NULL) { |
| 1377 | last = ret = node; |
| 1378 | } else { |
| 1379 | last = xmlAddNextSibling(last, node); |
| 1380 | } |
| 1381 | } |
| 1382 | xmlFree(val); |
| 1383 | } |
| 1384 | cur++; |
| 1385 | q = cur; |
| 1386 | } |
| 1387 | if (charval != 0) { |
| 1388 | xmlChar buf[10]; |
| 1389 | int l; |
| 1390 | |
| 1391 | l = xmlCopyCharMultiByte(buf, charval); |
| 1392 | buf[l] = 0; |
| 1393 | node = xmlNewDocText(doc, buf); |
| 1394 | if (node != NULL) { |
| 1395 | if (last == NULL) { |
| 1396 | last = ret = node; |
| 1397 | } else { |
| 1398 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1399 | } |
| 1400 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1401 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1402 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1403 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1404 | cur++; |
| 1405 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1406 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1407 | /* |
| 1408 | * Handle the last piece of text. |
| 1409 | */ |
| 1410 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1411 | xmlNodeAddContentLen(last, q, cur - q); |
| 1412 | } else { |
| 1413 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1414 | if (node == NULL) return(ret); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1415 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1416 | last = ret = node; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1417 | } else { |
| 1418 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | return(ret); |
| 1423 | } |
| 1424 | |
| 1425 | /** |
| 1426 | * xmlStringGetNodeList: |
| 1427 | * @doc: the document |
| 1428 | * @value: the value of the attribute |
| 1429 | * |
| 1430 | * Parse the value string and build the node list associated. Should |
| 1431 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1432 | * Returns a pointer to the first child |
| 1433 | */ |
| 1434 | xmlNodePtr |
| 1435 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
| 1436 | xmlNodePtr ret = NULL, last = NULL; |
| 1437 | xmlNodePtr node; |
| 1438 | xmlChar *val; |
| 1439 | const xmlChar *cur = value; |
| 1440 | const xmlChar *q; |
| 1441 | xmlEntityPtr ent; |
| 1442 | |
| 1443 | if (value == NULL) return(NULL); |
| 1444 | |
| 1445 | q = cur; |
| 1446 | while (*cur != 0) { |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1447 | if (cur[0] == '&') { |
| 1448 | int charval = 0; |
| 1449 | xmlChar tmp; |
| 1450 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1451 | /* |
| 1452 | * Save the current text. |
| 1453 | */ |
| 1454 | if (cur != q) { |
| 1455 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1456 | xmlNodeAddContentLen(last, q, cur - q); |
| 1457 | } else { |
| 1458 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1459 | if (node == NULL) return(ret); |
| 1460 | if (last == NULL) |
| 1461 | last = ret = node; |
| 1462 | else { |
| 1463 | last->next = node; |
| 1464 | node->prev = last; |
| 1465 | last = node; |
| 1466 | } |
| 1467 | } |
| 1468 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1469 | q = cur; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1470 | if ((cur[1] == '#') && (cur[2] == 'x')) { |
| 1471 | cur += 3; |
| 1472 | tmp = *cur; |
| 1473 | while (tmp != ';') { /* Non input consuming loop */ |
| 1474 | if ((tmp >= '0') && (tmp <= '9')) |
| 1475 | charval = charval * 16 + (tmp - '0'); |
| 1476 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1477 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1478 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1479 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1480 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1481 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1482 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1483 | charval = 0; |
| 1484 | break; |
| 1485 | } |
| 1486 | cur++; |
| 1487 | tmp = *cur; |
| 1488 | } |
| 1489 | if (tmp == ';') |
| 1490 | cur++; |
| 1491 | q = cur; |
| 1492 | } else if (cur[1] == '#') { |
| 1493 | cur += 2; |
| 1494 | tmp = *cur; |
| 1495 | while (tmp != ';') { /* Non input consuming loops */ |
| 1496 | if ((tmp >= '0') && (tmp <= '9')) |
| 1497 | charval = charval * 10 + (tmp - '0'); |
| 1498 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1499 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1500 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1501 | charval = 0; |
| 1502 | break; |
| 1503 | } |
| 1504 | cur++; |
| 1505 | tmp = *cur; |
| 1506 | } |
| 1507 | if (tmp == ';') |
| 1508 | cur++; |
| 1509 | q = cur; |
| 1510 | } else { |
| 1511 | /* |
| 1512 | * Read the entity string |
| 1513 | */ |
| 1514 | cur++; |
| 1515 | q = cur; |
| 1516 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 1517 | if (*cur == 0) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1518 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, |
| 1519 | (xmlNodePtr) doc, (const char *) q); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1520 | return(ret); |
| 1521 | } |
| 1522 | if (cur != q) { |
| 1523 | /* |
| 1524 | * Predefined entities don't generate nodes |
| 1525 | */ |
| 1526 | val = xmlStrndup(q, cur - q); |
| 1527 | ent = xmlGetDocEntity(doc, val); |
| 1528 | if ((ent != NULL) && |
| 1529 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1530 | if (last == NULL) { |
| 1531 | node = xmlNewDocText(doc, ent->content); |
| 1532 | last = ret = node; |
Daniel Veillard | 6f42c13 | 2002-01-06 23:05:13 +0000 | [diff] [blame] | 1533 | } else if (last->type != XML_TEXT_NODE) { |
| 1534 | node = xmlNewDocText(doc, ent->content); |
| 1535 | last = xmlAddNextSibling(last, node); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1536 | } else |
| 1537 | xmlNodeAddContent(last, ent->content); |
| 1538 | |
| 1539 | } else { |
| 1540 | /* |
| 1541 | * Create a new REFERENCE_REF node |
| 1542 | */ |
| 1543 | node = xmlNewReference(doc, val); |
| 1544 | if (node == NULL) { |
| 1545 | if (val != NULL) xmlFree(val); |
| 1546 | return(ret); |
| 1547 | } |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1548 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1549 | xmlNodePtr temp; |
| 1550 | |
| 1551 | ent->children = xmlStringGetNodeList(doc, |
| 1552 | (const xmlChar*)node->content); |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 1553 | ent->owner = 1; |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1554 | temp = ent->children; |
| 1555 | while (temp) { |
| 1556 | temp->parent = (xmlNodePtr)ent; |
| 1557 | temp = temp->next; |
| 1558 | } |
| 1559 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1560 | if (last == NULL) { |
| 1561 | last = ret = node; |
| 1562 | } else { |
| 1563 | last = xmlAddNextSibling(last, node); |
| 1564 | } |
| 1565 | } |
| 1566 | xmlFree(val); |
| 1567 | } |
| 1568 | cur++; |
| 1569 | q = cur; |
| 1570 | } |
| 1571 | if (charval != 0) { |
| 1572 | xmlChar buf[10]; |
| 1573 | int len; |
| 1574 | |
| 1575 | len = xmlCopyCharMultiByte(buf, charval); |
| 1576 | buf[len] = 0; |
| 1577 | node = xmlNewDocText(doc, buf); |
| 1578 | if (node != NULL) { |
| 1579 | if (last == NULL) { |
| 1580 | last = ret = node; |
| 1581 | } else { |
| 1582 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1583 | } |
| 1584 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1585 | |
| 1586 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1587 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1588 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1589 | cur++; |
| 1590 | } |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 1591 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1592 | /* |
| 1593 | * Handle the last piece of text. |
| 1594 | */ |
| 1595 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1596 | xmlNodeAddContentLen(last, q, cur - q); |
| 1597 | } else { |
| 1598 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1599 | if (node == NULL) return(ret); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1600 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1601 | last = ret = node; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1602 | } else { |
| 1603 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | return(ret); |
| 1608 | } |
| 1609 | |
| 1610 | /** |
| 1611 | * xmlNodeListGetString: |
| 1612 | * @doc: the document |
| 1613 | * @list: a Node list |
| 1614 | * @inLine: should we replace entity contents or show their external form |
| 1615 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1616 | * Build the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1617 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1618 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1619 | * 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] | 1620 | */ |
| 1621 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1622 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1623 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1624 | xmlNodePtr node = list; |
| 1625 | xmlChar *ret = NULL; |
| 1626 | xmlEntityPtr ent; |
| 1627 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1628 | if (list == NULL) |
| 1629 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1630 | |
| 1631 | while (node != NULL) { |
| 1632 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1633 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1634 | if (inLine) { |
| 1635 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1636 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1637 | xmlChar *buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1638 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1639 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
| 1640 | if (buffer != NULL) { |
| 1641 | ret = xmlStrcat(ret, buffer); |
| 1642 | xmlFree(buffer); |
| 1643 | } |
| 1644 | } |
| 1645 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1646 | if (inLine) { |
| 1647 | ent = xmlGetDocEntity(doc, node->name); |
| 1648 | if (ent != NULL) { |
| 1649 | xmlChar *buffer; |
| 1650 | |
| 1651 | /* an entity content can be any "well balanced chunk", |
| 1652 | * i.e. the result of the content [43] production: |
| 1653 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1654 | * So it can contain text, CDATA section or nested |
| 1655 | * entity reference nodes (among others). |
| 1656 | * -> we recursive call xmlNodeListGetString() |
| 1657 | * which handles these types */ |
| 1658 | buffer = xmlNodeListGetString(doc, ent->children, 1); |
| 1659 | if (buffer != NULL) { |
| 1660 | ret = xmlStrcat(ret, buffer); |
| 1661 | xmlFree(buffer); |
| 1662 | } |
| 1663 | } else { |
| 1664 | ret = xmlStrcat(ret, node->content); |
| 1665 | } |
| 1666 | } else { |
| 1667 | xmlChar buf[2]; |
| 1668 | |
| 1669 | buf[0] = '&'; |
| 1670 | buf[1] = 0; |
| 1671 | ret = xmlStrncat(ret, buf, 1); |
| 1672 | ret = xmlStrcat(ret, node->name); |
| 1673 | buf[0] = ';'; |
| 1674 | buf[1] = 0; |
| 1675 | ret = xmlStrncat(ret, buf, 1); |
| 1676 | } |
| 1677 | } |
| 1678 | #if 0 |
| 1679 | else { |
| 1680 | xmlGenericError(xmlGenericErrorContext, |
| 1681 | "xmlGetNodeListString : invalid node type %d\n", |
| 1682 | node->type); |
| 1683 | } |
| 1684 | #endif |
| 1685 | node = node->next; |
| 1686 | } |
| 1687 | return (ret); |
| 1688 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1689 | |
| 1690 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1691 | /** |
| 1692 | * xmlNodeListGetRawString: |
| 1693 | * @doc: the document |
| 1694 | * @list: a Node list |
| 1695 | * @inLine: should we replace entity contents or show their external form |
| 1696 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1697 | * Builds the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1698 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 1699 | * this function doesn't do any character encoding handling. |
| 1700 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1701 | * 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] | 1702 | */ |
| 1703 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1704 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1705 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1706 | xmlNodePtr node = list; |
| 1707 | xmlChar *ret = NULL; |
| 1708 | xmlEntityPtr ent; |
| 1709 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1710 | if (list == NULL) |
| 1711 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1712 | |
| 1713 | while (node != NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1714 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1715 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1716 | if (inLine) { |
| 1717 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1718 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1719 | xmlChar *buffer; |
| 1720 | |
| 1721 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 1722 | if (buffer != NULL) { |
| 1723 | ret = xmlStrcat(ret, buffer); |
| 1724 | xmlFree(buffer); |
| 1725 | } |
| 1726 | } |
| 1727 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1728 | if (inLine) { |
| 1729 | ent = xmlGetDocEntity(doc, node->name); |
| 1730 | if (ent != NULL) { |
| 1731 | xmlChar *buffer; |
| 1732 | |
| 1733 | /* an entity content can be any "well balanced chunk", |
| 1734 | * i.e. the result of the content [43] production: |
| 1735 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1736 | * So it can contain text, CDATA section or nested |
| 1737 | * entity reference nodes (among others). |
| 1738 | * -> we recursive call xmlNodeListGetRawString() |
| 1739 | * which handles these types */ |
| 1740 | buffer = |
| 1741 | xmlNodeListGetRawString(doc, ent->children, 1); |
| 1742 | if (buffer != NULL) { |
| 1743 | ret = xmlStrcat(ret, buffer); |
| 1744 | xmlFree(buffer); |
| 1745 | } |
| 1746 | } else { |
| 1747 | ret = xmlStrcat(ret, node->content); |
| 1748 | } |
| 1749 | } else { |
| 1750 | xmlChar buf[2]; |
| 1751 | |
| 1752 | buf[0] = '&'; |
| 1753 | buf[1] = 0; |
| 1754 | ret = xmlStrncat(ret, buf, 1); |
| 1755 | ret = xmlStrcat(ret, node->name); |
| 1756 | buf[0] = ';'; |
| 1757 | buf[1] = 0; |
| 1758 | ret = xmlStrncat(ret, buf, 1); |
| 1759 | } |
| 1760 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1761 | #if 0 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1762 | else { |
| 1763 | xmlGenericError(xmlGenericErrorContext, |
| 1764 | "xmlGetNodeListString : invalid node type %d\n", |
| 1765 | node->type); |
| 1766 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1767 | #endif |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1768 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1769 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1770 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1771 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1772 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1773 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1774 | static xmlAttrPtr |
| 1775 | xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns, |
| 1776 | const xmlChar * name, const xmlChar * value, |
| 1777 | int eatname) |
| 1778 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1779 | xmlAttrPtr cur; |
| 1780 | xmlDocPtr doc = NULL; |
| 1781 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1782 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1783 | if (eatname == 1) |
| 1784 | xmlFree((xmlChar *) name); |
| 1785 | return (NULL); |
| 1786 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1787 | |
| 1788 | /* |
| 1789 | * Allocate a new property and fill the fields. |
| 1790 | */ |
| 1791 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1792 | if (cur == NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1793 | if (eatname == 1) |
| 1794 | xmlFree((xmlChar *) name); |
| 1795 | xmlTreeErrMemory("building attribute"); |
| 1796 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1797 | } |
| 1798 | memset(cur, 0, sizeof(xmlAttr)); |
| 1799 | cur->type = XML_ATTRIBUTE_NODE; |
| 1800 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1801 | cur->parent = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1802 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1803 | doc = node->doc; |
| 1804 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1805 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1806 | cur->ns = ns; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1807 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1808 | if (eatname == 0) { |
| 1809 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1810 | cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1); |
| 1811 | else |
| 1812 | cur->name = xmlStrdup(name); |
| 1813 | } else |
| 1814 | cur->name = name; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1815 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1816 | if (value != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1817 | xmlChar *buffer; |
| 1818 | xmlNodePtr tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1819 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1820 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1821 | cur->children = xmlStringGetNodeList(doc, buffer); |
| 1822 | cur->last = NULL; |
| 1823 | tmp = cur->children; |
| 1824 | while (tmp != NULL) { |
| 1825 | tmp->parent = (xmlNodePtr) cur; |
| 1826 | if (tmp->next == NULL) |
| 1827 | cur->last = tmp; |
| 1828 | tmp = tmp->next; |
| 1829 | } |
| 1830 | xmlFree(buffer); |
| 1831 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1832 | |
| 1833 | /* |
| 1834 | * Add it at the end to preserve parsing order ... |
| 1835 | */ |
| 1836 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1837 | if (node->properties == NULL) { |
| 1838 | node->properties = cur; |
| 1839 | } else { |
| 1840 | xmlAttrPtr prev = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1841 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1842 | while (prev->next != NULL) |
| 1843 | prev = prev->next; |
| 1844 | prev->next = cur; |
| 1845 | cur->prev = prev; |
| 1846 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1847 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1848 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1849 | if (xmlIsID((node == NULL) ? NULL : node->doc, node, cur) == 1) |
| 1850 | xmlAddID(NULL, node->doc, value, cur); |
| 1851 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1852 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1853 | xmlRegisterNodeDefaultValue((xmlNodePtr) cur); |
| 1854 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1855 | } |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1856 | |
| 1857 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 1858 | defined(LIBXML_SCHEMAS_ENABLED) |
| 1859 | /** |
| 1860 | * xmlNewProp: |
| 1861 | * @node: the holding node |
| 1862 | * @name: the name of the attribute |
| 1863 | * @value: the value of the attribute |
| 1864 | * |
| 1865 | * Create a new property carried by a node. |
| 1866 | * Returns a pointer to the attribute |
| 1867 | */ |
| 1868 | xmlAttrPtr |
| 1869 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 1870 | |
| 1871 | if (name == NULL) { |
| 1872 | #ifdef DEBUG_TREE |
| 1873 | xmlGenericError(xmlGenericErrorContext, |
| 1874 | "xmlNewProp : name == NULL\n"); |
| 1875 | #endif |
| 1876 | return(NULL); |
| 1877 | } |
| 1878 | |
| 1879 | return xmlNewPropInternal(node, NULL, name, value, 0); |
| 1880 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1881 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1882 | |
| 1883 | /** |
| 1884 | * xmlNewNsProp: |
| 1885 | * @node: the holding node |
| 1886 | * @ns: the namespace |
| 1887 | * @name: the name of the attribute |
| 1888 | * @value: the value of the attribute |
| 1889 | * |
| 1890 | * Create a new property tagged with a namespace and carried by a node. |
| 1891 | * Returns a pointer to the attribute |
| 1892 | */ |
| 1893 | xmlAttrPtr |
| 1894 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 1895 | const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1896 | |
| 1897 | if (name == NULL) { |
| 1898 | #ifdef DEBUG_TREE |
| 1899 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1900 | "xmlNewNsProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1901 | #endif |
| 1902 | return(NULL); |
| 1903 | } |
| 1904 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1905 | return xmlNewPropInternal(node, ns, name, value, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1909 | * xmlNewNsPropEatName: |
| 1910 | * @node: the holding node |
| 1911 | * @ns: the namespace |
| 1912 | * @name: the name of the attribute |
| 1913 | * @value: the value of the attribute |
| 1914 | * |
| 1915 | * Create a new property tagged with a namespace and carried by a node. |
| 1916 | * Returns a pointer to the attribute |
| 1917 | */ |
| 1918 | xmlAttrPtr |
| 1919 | xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name, |
| 1920 | const xmlChar *value) { |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1921 | |
| 1922 | if (name == NULL) { |
| 1923 | #ifdef DEBUG_TREE |
| 1924 | xmlGenericError(xmlGenericErrorContext, |
| 1925 | "xmlNewNsPropEatName : name == NULL\n"); |
| 1926 | #endif |
| 1927 | return(NULL); |
| 1928 | } |
| 1929 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1930 | return xmlNewPropInternal(node, ns, name, value, 1); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1934 | * xmlNewDocProp: |
| 1935 | * @doc: the document |
| 1936 | * @name: the name of the attribute |
| 1937 | * @value: the value of the attribute |
| 1938 | * |
| 1939 | * Create a new property carried by a document. |
| 1940 | * Returns a pointer to the attribute |
| 1941 | */ |
| 1942 | xmlAttrPtr |
| 1943 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
| 1944 | xmlAttrPtr cur; |
| 1945 | |
| 1946 | if (name == NULL) { |
| 1947 | #ifdef DEBUG_TREE |
| 1948 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1949 | "xmlNewDocProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1950 | #endif |
| 1951 | return(NULL); |
| 1952 | } |
| 1953 | |
| 1954 | /* |
| 1955 | * Allocate a new property and fill the fields. |
| 1956 | */ |
| 1957 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1958 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1959 | xmlTreeErrMemory("building attribute"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1960 | return(NULL); |
| 1961 | } |
| 1962 | memset(cur, 0, sizeof(xmlAttr)); |
| 1963 | cur->type = XML_ATTRIBUTE_NODE; |
| 1964 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 1965 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1966 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 1967 | else |
| 1968 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1969 | cur->doc = doc; |
| 1970 | if (value != NULL) { |
| 1971 | xmlNodePtr tmp; |
| 1972 | |
| 1973 | cur->children = xmlStringGetNodeList(doc, value); |
| 1974 | cur->last = NULL; |
| 1975 | |
| 1976 | tmp = cur->children; |
| 1977 | while (tmp != NULL) { |
| 1978 | tmp->parent = (xmlNodePtr) cur; |
| 1979 | if (tmp->next == NULL) |
| 1980 | cur->last = tmp; |
| 1981 | tmp = tmp->next; |
| 1982 | } |
| 1983 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1984 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1985 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1986 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1987 | return(cur); |
| 1988 | } |
| 1989 | |
| 1990 | /** |
| 1991 | * xmlFreePropList: |
| 1992 | * @cur: the first property in the list |
| 1993 | * |
| 1994 | * Free a property and all its siblings, all the children are freed too. |
| 1995 | */ |
| 1996 | void |
| 1997 | xmlFreePropList(xmlAttrPtr cur) { |
| 1998 | xmlAttrPtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1999 | if (cur == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2000 | while (cur != NULL) { |
| 2001 | next = cur->next; |
| 2002 | xmlFreeProp(cur); |
| 2003 | cur = next; |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | /** |
| 2008 | * xmlFreeProp: |
| 2009 | * @cur: an attribute |
| 2010 | * |
| 2011 | * Free one attribute, all the content is freed too |
| 2012 | */ |
| 2013 | void |
| 2014 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2015 | xmlDictPtr dict = NULL; |
| 2016 | if (cur == NULL) return; |
| 2017 | |
| 2018 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2019 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2020 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2021 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 2022 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2023 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | da6f4af | 2005-06-20 17:17:54 +0000 | [diff] [blame] | 2024 | if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) { |
| 2025 | xmlRemoveID(cur->doc, cur); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2026 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2027 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2028 | DICT_FREE(cur->name) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2029 | xmlFree(cur); |
| 2030 | } |
| 2031 | |
| 2032 | /** |
| 2033 | * xmlRemoveProp: |
| 2034 | * @cur: an attribute |
| 2035 | * |
| 2036 | * Unlink and free one attribute, all the content is freed too |
| 2037 | * Note this doesn't work for namespace definition attributes |
| 2038 | * |
| 2039 | * Returns 0 if success and -1 in case of error. |
| 2040 | */ |
| 2041 | int |
| 2042 | xmlRemoveProp(xmlAttrPtr cur) { |
| 2043 | xmlAttrPtr tmp; |
| 2044 | if (cur == NULL) { |
| 2045 | #ifdef DEBUG_TREE |
| 2046 | xmlGenericError(xmlGenericErrorContext, |
| 2047 | "xmlRemoveProp : cur == NULL\n"); |
| 2048 | #endif |
| 2049 | return(-1); |
| 2050 | } |
| 2051 | if (cur->parent == NULL) { |
| 2052 | #ifdef DEBUG_TREE |
| 2053 | xmlGenericError(xmlGenericErrorContext, |
| 2054 | "xmlRemoveProp : cur->parent == NULL\n"); |
| 2055 | #endif |
| 2056 | return(-1); |
| 2057 | } |
| 2058 | tmp = cur->parent->properties; |
| 2059 | if (tmp == cur) { |
| 2060 | cur->parent->properties = cur->next; |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2061 | if (cur->next != NULL) |
| 2062 | cur->next->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2063 | xmlFreeProp(cur); |
| 2064 | return(0); |
| 2065 | } |
| 2066 | while (tmp != NULL) { |
| 2067 | if (tmp->next == cur) { |
| 2068 | tmp->next = cur->next; |
| 2069 | if (tmp->next != NULL) |
| 2070 | tmp->next->prev = tmp; |
| 2071 | xmlFreeProp(cur); |
| 2072 | return(0); |
| 2073 | } |
| 2074 | tmp = tmp->next; |
| 2075 | } |
| 2076 | #ifdef DEBUG_TREE |
| 2077 | xmlGenericError(xmlGenericErrorContext, |
| 2078 | "xmlRemoveProp : attribute not owned by its node\n"); |
| 2079 | #endif |
| 2080 | return(-1); |
| 2081 | } |
| 2082 | |
| 2083 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2084 | * xmlNewDocPI: |
| 2085 | * @doc: the target document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2086 | * @name: the processing instruction name |
| 2087 | * @content: the PI content |
| 2088 | * |
| 2089 | * Creation of a processing instruction element. |
| 2090 | * Returns a pointer to the new node object. |
| 2091 | */ |
| 2092 | xmlNodePtr |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2093 | xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2094 | xmlNodePtr cur; |
| 2095 | |
| 2096 | if (name == NULL) { |
| 2097 | #ifdef DEBUG_TREE |
| 2098 | xmlGenericError(xmlGenericErrorContext, |
| 2099 | "xmlNewPI : name == NULL\n"); |
| 2100 | #endif |
| 2101 | return(NULL); |
| 2102 | } |
| 2103 | |
| 2104 | /* |
| 2105 | * Allocate a new node and fill the fields. |
| 2106 | */ |
| 2107 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2108 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2109 | xmlTreeErrMemory("building PI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2110 | return(NULL); |
| 2111 | } |
| 2112 | memset(cur, 0, sizeof(xmlNode)); |
| 2113 | cur->type = XML_PI_NODE; |
| 2114 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2115 | if ((doc != NULL) && (doc->dict != NULL)) |
| 2116 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 2117 | else |
| 2118 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2119 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2120 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2121 | } |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 2122 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2123 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2124 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2125 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2126 | return(cur); |
| 2127 | } |
| 2128 | |
| 2129 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2130 | * xmlNewPI: |
| 2131 | * @name: the processing instruction name |
| 2132 | * @content: the PI content |
| 2133 | * |
| 2134 | * Creation of a processing instruction element. |
| 2135 | * Use xmlDocNewPI preferably to get string interning |
| 2136 | * |
| 2137 | * Returns a pointer to the new node object. |
| 2138 | */ |
| 2139 | xmlNodePtr |
| 2140 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
| 2141 | return(xmlNewDocPI(NULL, name, content)); |
| 2142 | } |
| 2143 | |
| 2144 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2145 | * xmlNewNode: |
| 2146 | * @ns: namespace if any |
| 2147 | * @name: the node name |
| 2148 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2149 | * Creation of a new node element. @ns is optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2150 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2151 | * Returns a pointer to the new node object. Uses xmlStrdup() to make |
| 2152 | * copy of @name. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2153 | */ |
| 2154 | xmlNodePtr |
| 2155 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 2156 | xmlNodePtr cur; |
| 2157 | |
| 2158 | if (name == NULL) { |
| 2159 | #ifdef DEBUG_TREE |
| 2160 | xmlGenericError(xmlGenericErrorContext, |
| 2161 | "xmlNewNode : name == NULL\n"); |
| 2162 | #endif |
| 2163 | return(NULL); |
| 2164 | } |
| 2165 | |
| 2166 | /* |
| 2167 | * Allocate a new node and fill the fields. |
| 2168 | */ |
| 2169 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2170 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2171 | xmlTreeErrMemory("building node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2172 | return(NULL); |
| 2173 | } |
| 2174 | memset(cur, 0, sizeof(xmlNode)); |
| 2175 | cur->type = XML_ELEMENT_NODE; |
| 2176 | |
| 2177 | cur->name = xmlStrdup(name); |
| 2178 | cur->ns = ns; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2179 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2180 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2181 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2182 | return(cur); |
| 2183 | } |
| 2184 | |
| 2185 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2186 | * xmlNewNodeEatName: |
| 2187 | * @ns: namespace if any |
| 2188 | * @name: the node name |
| 2189 | * |
| 2190 | * Creation of a new node element. @ns is optional (NULL). |
| 2191 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2192 | * Returns a pointer to the new node object, with pointer @name as |
| 2193 | * new node's name. Use xmlNewNode() if a copy of @name string is |
| 2194 | * is needed as new node's name. |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2195 | */ |
| 2196 | xmlNodePtr |
| 2197 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
| 2198 | xmlNodePtr cur; |
| 2199 | |
| 2200 | if (name == NULL) { |
| 2201 | #ifdef DEBUG_TREE |
| 2202 | xmlGenericError(xmlGenericErrorContext, |
| 2203 | "xmlNewNode : name == NULL\n"); |
| 2204 | #endif |
| 2205 | return(NULL); |
| 2206 | } |
| 2207 | |
| 2208 | /* |
| 2209 | * Allocate a new node and fill the fields. |
| 2210 | */ |
| 2211 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2212 | if (cur == NULL) { |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 2213 | xmlFree(name); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2214 | xmlTreeErrMemory("building node"); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2215 | return(NULL); |
| 2216 | } |
| 2217 | memset(cur, 0, sizeof(xmlNode)); |
| 2218 | cur->type = XML_ELEMENT_NODE; |
| 2219 | |
| 2220 | cur->name = name; |
| 2221 | cur->ns = ns; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2222 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2223 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2224 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2225 | return(cur); |
| 2226 | } |
| 2227 | |
| 2228 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2229 | * xmlNewDocNode: |
| 2230 | * @doc: the document |
| 2231 | * @ns: namespace if any |
| 2232 | * @name: the node name |
| 2233 | * @content: the XML text content if any |
| 2234 | * |
| 2235 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2236 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2237 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2238 | * references, but XML special chars need to be escaped first by using |
| 2239 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2240 | * need entities support. |
| 2241 | * |
| 2242 | * Returns a pointer to the new node object. |
| 2243 | */ |
| 2244 | xmlNodePtr |
| 2245 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2246 | const xmlChar *name, const xmlChar *content) { |
| 2247 | xmlNodePtr cur; |
| 2248 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2249 | if ((doc != NULL) && (doc->dict != NULL)) |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2250 | cur = xmlNewNodeEatName(ns, (xmlChar *) |
| 2251 | xmlDictLookup(doc->dict, name, -1)); |
| 2252 | else |
| 2253 | cur = xmlNewNode(ns, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2254 | if (cur != NULL) { |
| 2255 | cur->doc = doc; |
| 2256 | if (content != NULL) { |
| 2257 | cur->children = xmlStringGetNodeList(doc, content); |
| 2258 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2259 | } |
| 2260 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2261 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2262 | return(cur); |
| 2263 | } |
| 2264 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2265 | /** |
| 2266 | * xmlNewDocNodeEatName: |
| 2267 | * @doc: the document |
| 2268 | * @ns: namespace if any |
| 2269 | * @name: the node name |
| 2270 | * @content: the XML text content if any |
| 2271 | * |
| 2272 | * Creation of a new node element within a document. @ns and @content |
| 2273 | * are optional (NULL). |
| 2274 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2275 | * references, but XML special chars need to be escaped first by using |
| 2276 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2277 | * need entities support. |
| 2278 | * |
| 2279 | * Returns a pointer to the new node object. |
| 2280 | */ |
| 2281 | xmlNodePtr |
| 2282 | xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns, |
| 2283 | xmlChar *name, const xmlChar *content) { |
| 2284 | xmlNodePtr cur; |
| 2285 | |
| 2286 | cur = xmlNewNodeEatName(ns, name); |
| 2287 | if (cur != NULL) { |
| 2288 | cur->doc = doc; |
| 2289 | if (content != NULL) { |
| 2290 | cur->children = xmlStringGetNodeList(doc, content); |
| 2291 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2292 | } |
| 2293 | } |
| 2294 | return(cur); |
| 2295 | } |
| 2296 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2297 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2298 | /** |
| 2299 | * xmlNewDocRawNode: |
| 2300 | * @doc: the document |
| 2301 | * @ns: namespace if any |
| 2302 | * @name: the node name |
| 2303 | * @content: the text content if any |
| 2304 | * |
| 2305 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2306 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2307 | * |
| 2308 | * Returns a pointer to the new node object. |
| 2309 | */ |
| 2310 | xmlNodePtr |
| 2311 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2312 | const xmlChar *name, const xmlChar *content) { |
| 2313 | xmlNodePtr cur; |
| 2314 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2315 | cur = xmlNewDocNode(doc, ns, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2316 | if (cur != NULL) { |
| 2317 | cur->doc = doc; |
| 2318 | if (content != NULL) { |
| 2319 | cur->children = xmlNewDocText(doc, content); |
| 2320 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2321 | } |
| 2322 | } |
| 2323 | return(cur); |
| 2324 | } |
| 2325 | |
| 2326 | /** |
| 2327 | * xmlNewDocFragment: |
| 2328 | * @doc: the document owning the fragment |
| 2329 | * |
| 2330 | * Creation of a new Fragment node. |
| 2331 | * Returns a pointer to the new node object. |
| 2332 | */ |
| 2333 | xmlNodePtr |
| 2334 | xmlNewDocFragment(xmlDocPtr doc) { |
| 2335 | xmlNodePtr cur; |
| 2336 | |
| 2337 | /* |
| 2338 | * Allocate a new DocumentFragment node and fill the fields. |
| 2339 | */ |
| 2340 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2341 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2342 | xmlTreeErrMemory("building fragment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2343 | return(NULL); |
| 2344 | } |
| 2345 | memset(cur, 0, sizeof(xmlNode)); |
| 2346 | cur->type = XML_DOCUMENT_FRAG_NODE; |
| 2347 | |
| 2348 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2349 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2350 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2351 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2352 | return(cur); |
| 2353 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2354 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2355 | |
| 2356 | /** |
| 2357 | * xmlNewText: |
| 2358 | * @content: the text content |
| 2359 | * |
| 2360 | * Creation of a new text node. |
| 2361 | * Returns a pointer to the new node object. |
| 2362 | */ |
| 2363 | xmlNodePtr |
| 2364 | xmlNewText(const xmlChar *content) { |
| 2365 | xmlNodePtr cur; |
| 2366 | |
| 2367 | /* |
| 2368 | * Allocate a new node and fill the fields. |
| 2369 | */ |
| 2370 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2371 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2372 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2373 | return(NULL); |
| 2374 | } |
| 2375 | memset(cur, 0, sizeof(xmlNode)); |
| 2376 | cur->type = XML_TEXT_NODE; |
| 2377 | |
| 2378 | cur->name = xmlStringText; |
| 2379 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2380 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2381 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2382 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2383 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2384 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2385 | return(cur); |
| 2386 | } |
| 2387 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2388 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2389 | /** |
| 2390 | * xmlNewTextChild: |
| 2391 | * @parent: the parent node |
| 2392 | * @ns: a namespace if any |
| 2393 | * @name: the name of the child |
| 2394 | * @content: the text content of the child if any. |
| 2395 | * |
| 2396 | * 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] | 2397 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2398 | * 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] | 2399 | * 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] | 2400 | * NOTE: Use xmlNewChild() if @content will contain entities that need to be |
| 2401 | * preserved. Use this function, xmlNewTextChild(), if you need to ensure that |
| 2402 | * reserved XML chars that might appear in @content, such as the ampersand, |
| 2403 | * greater-than or less-than signs, are automatically replaced by their XML |
| 2404 | * escaped entity representations. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2405 | * |
| 2406 | * Returns a pointer to the new node object. |
| 2407 | */ |
| 2408 | xmlNodePtr |
| 2409 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2410 | const xmlChar *name, const xmlChar *content) { |
| 2411 | xmlNodePtr cur, prev; |
| 2412 | |
| 2413 | if (parent == NULL) { |
| 2414 | #ifdef DEBUG_TREE |
| 2415 | xmlGenericError(xmlGenericErrorContext, |
| 2416 | "xmlNewTextChild : parent == NULL\n"); |
| 2417 | #endif |
| 2418 | return(NULL); |
| 2419 | } |
| 2420 | |
| 2421 | if (name == NULL) { |
| 2422 | #ifdef DEBUG_TREE |
| 2423 | xmlGenericError(xmlGenericErrorContext, |
| 2424 | "xmlNewTextChild : name == NULL\n"); |
| 2425 | #endif |
| 2426 | return(NULL); |
| 2427 | } |
| 2428 | |
| 2429 | /* |
| 2430 | * Allocate a new node |
| 2431 | */ |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 2432 | if (parent->type == XML_ELEMENT_NODE) { |
| 2433 | if (ns == NULL) |
| 2434 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 2435 | else |
| 2436 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 2437 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2438 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2439 | if (ns == NULL) |
| 2440 | cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content); |
| 2441 | else |
| 2442 | cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content); |
| 2443 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2444 | cur = xmlNewDocRawNode( parent->doc, ns, name, content); |
| 2445 | } else { |
| 2446 | return(NULL); |
| 2447 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2448 | if (cur == NULL) return(NULL); |
| 2449 | |
| 2450 | /* |
| 2451 | * add the new element at the end of the children list. |
| 2452 | */ |
| 2453 | cur->type = XML_ELEMENT_NODE; |
| 2454 | cur->parent = parent; |
| 2455 | cur->doc = parent->doc; |
| 2456 | if (parent->children == NULL) { |
| 2457 | parent->children = cur; |
| 2458 | parent->last = cur; |
| 2459 | } else { |
| 2460 | prev = parent->last; |
| 2461 | prev->next = cur; |
| 2462 | cur->prev = prev; |
| 2463 | parent->last = cur; |
| 2464 | } |
| 2465 | |
| 2466 | return(cur); |
| 2467 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2468 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2469 | |
| 2470 | /** |
| 2471 | * xmlNewCharRef: |
| 2472 | * @doc: the document |
| 2473 | * @name: the char ref string, starting with # or "&# ... ;" |
| 2474 | * |
| 2475 | * Creation of a new character reference node. |
| 2476 | * Returns a pointer to the new node object. |
| 2477 | */ |
| 2478 | xmlNodePtr |
| 2479 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 2480 | xmlNodePtr cur; |
| 2481 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2482 | if (name == NULL) |
| 2483 | return(NULL); |
| 2484 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2485 | /* |
| 2486 | * Allocate a new node and fill the fields. |
| 2487 | */ |
| 2488 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2489 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2490 | xmlTreeErrMemory("building character reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2491 | return(NULL); |
| 2492 | } |
| 2493 | memset(cur, 0, sizeof(xmlNode)); |
| 2494 | cur->type = XML_ENTITY_REF_NODE; |
| 2495 | |
| 2496 | cur->doc = doc; |
| 2497 | if (name[0] == '&') { |
| 2498 | int len; |
| 2499 | name++; |
| 2500 | len = xmlStrlen(name); |
| 2501 | if (name[len - 1] == ';') |
| 2502 | cur->name = xmlStrndup(name, len - 1); |
| 2503 | else |
| 2504 | cur->name = xmlStrndup(name, len); |
| 2505 | } else |
| 2506 | cur->name = xmlStrdup(name); |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2507 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2508 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2509 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2510 | return(cur); |
| 2511 | } |
| 2512 | |
| 2513 | /** |
| 2514 | * xmlNewReference: |
| 2515 | * @doc: the document |
| 2516 | * @name: the reference name, or the reference string with & and ; |
| 2517 | * |
| 2518 | * Creation of a new reference node. |
| 2519 | * Returns a pointer to the new node object. |
| 2520 | */ |
| 2521 | xmlNodePtr |
| 2522 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 2523 | xmlNodePtr cur; |
| 2524 | xmlEntityPtr ent; |
| 2525 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2526 | if (name == NULL) |
| 2527 | return(NULL); |
| 2528 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2529 | /* |
| 2530 | * Allocate a new node and fill the fields. |
| 2531 | */ |
| 2532 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2533 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2534 | xmlTreeErrMemory("building reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2535 | return(NULL); |
| 2536 | } |
| 2537 | memset(cur, 0, sizeof(xmlNode)); |
| 2538 | cur->type = XML_ENTITY_REF_NODE; |
| 2539 | |
| 2540 | cur->doc = doc; |
| 2541 | if (name[0] == '&') { |
| 2542 | int len; |
| 2543 | name++; |
| 2544 | len = xmlStrlen(name); |
| 2545 | if (name[len - 1] == ';') |
| 2546 | cur->name = xmlStrndup(name, len - 1); |
| 2547 | else |
| 2548 | cur->name = xmlStrndup(name, len); |
| 2549 | } else |
| 2550 | cur->name = xmlStrdup(name); |
| 2551 | |
| 2552 | ent = xmlGetDocEntity(doc, cur->name); |
| 2553 | if (ent != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2554 | cur->content = ent->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2555 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2556 | * 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] | 2557 | * updated. Not sure if this is 100% correct. |
| 2558 | * -George |
| 2559 | */ |
| 2560 | cur->children = (xmlNodePtr) ent; |
| 2561 | cur->last = (xmlNodePtr) ent; |
| 2562 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2563 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2564 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2565 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2566 | return(cur); |
| 2567 | } |
| 2568 | |
| 2569 | /** |
| 2570 | * xmlNewDocText: |
| 2571 | * @doc: the document |
| 2572 | * @content: the text content |
| 2573 | * |
| 2574 | * Creation of a new text node within a document. |
| 2575 | * Returns a pointer to the new node object. |
| 2576 | */ |
| 2577 | xmlNodePtr |
| 2578 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 2579 | xmlNodePtr cur; |
| 2580 | |
| 2581 | cur = xmlNewText(content); |
| 2582 | if (cur != NULL) cur->doc = doc; |
| 2583 | return(cur); |
| 2584 | } |
| 2585 | |
| 2586 | /** |
| 2587 | * xmlNewTextLen: |
| 2588 | * @content: the text content |
| 2589 | * @len: the text len. |
| 2590 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2591 | * 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] | 2592 | * Returns a pointer to the new node object. |
| 2593 | */ |
| 2594 | xmlNodePtr |
| 2595 | xmlNewTextLen(const xmlChar *content, int len) { |
| 2596 | xmlNodePtr cur; |
| 2597 | |
| 2598 | /* |
| 2599 | * Allocate a new node and fill the fields. |
| 2600 | */ |
| 2601 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2602 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2603 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2604 | return(NULL); |
| 2605 | } |
| 2606 | memset(cur, 0, sizeof(xmlNode)); |
| 2607 | cur->type = XML_TEXT_NODE; |
| 2608 | |
| 2609 | cur->name = xmlStringText; |
| 2610 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2611 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2612 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2613 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2614 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2615 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2616 | return(cur); |
| 2617 | } |
| 2618 | |
| 2619 | /** |
| 2620 | * xmlNewDocTextLen: |
| 2621 | * @doc: the document |
| 2622 | * @content: the text content |
| 2623 | * @len: the text len. |
| 2624 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2625 | * 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] | 2626 | * text node pertain to a given document. |
| 2627 | * Returns a pointer to the new node object. |
| 2628 | */ |
| 2629 | xmlNodePtr |
| 2630 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2631 | xmlNodePtr cur; |
| 2632 | |
| 2633 | cur = xmlNewTextLen(content, len); |
| 2634 | if (cur != NULL) cur->doc = doc; |
| 2635 | return(cur); |
| 2636 | } |
| 2637 | |
| 2638 | /** |
| 2639 | * xmlNewComment: |
| 2640 | * @content: the comment content |
| 2641 | * |
| 2642 | * Creation of a new node containing a comment. |
| 2643 | * Returns a pointer to the new node object. |
| 2644 | */ |
| 2645 | xmlNodePtr |
| 2646 | xmlNewComment(const xmlChar *content) { |
| 2647 | xmlNodePtr cur; |
| 2648 | |
| 2649 | /* |
| 2650 | * Allocate a new node and fill the fields. |
| 2651 | */ |
| 2652 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2653 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2654 | xmlTreeErrMemory("building comment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2655 | return(NULL); |
| 2656 | } |
| 2657 | memset(cur, 0, sizeof(xmlNode)); |
| 2658 | cur->type = XML_COMMENT_NODE; |
| 2659 | |
| 2660 | cur->name = xmlStringComment; |
| 2661 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2662 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2663 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2664 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2665 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2666 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2667 | return(cur); |
| 2668 | } |
| 2669 | |
| 2670 | /** |
| 2671 | * xmlNewCDataBlock: |
| 2672 | * @doc: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2673 | * @content: the CDATA block content content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2674 | * @len: the length of the block |
| 2675 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2676 | * Creation of a new node containing a CDATA block. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2677 | * Returns a pointer to the new node object. |
| 2678 | */ |
| 2679 | xmlNodePtr |
| 2680 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2681 | xmlNodePtr cur; |
| 2682 | |
| 2683 | /* |
| 2684 | * Allocate a new node and fill the fields. |
| 2685 | */ |
| 2686 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2687 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2688 | xmlTreeErrMemory("building CDATA"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2689 | return(NULL); |
| 2690 | } |
| 2691 | memset(cur, 0, sizeof(xmlNode)); |
| 2692 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2693 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2694 | |
| 2695 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2696 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2697 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2698 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2699 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2700 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2701 | return(cur); |
| 2702 | } |
| 2703 | |
| 2704 | /** |
| 2705 | * xmlNewDocComment: |
| 2706 | * @doc: the document |
| 2707 | * @content: the comment content |
| 2708 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2709 | * Creation of a new node containing a comment within a document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2710 | * Returns a pointer to the new node object. |
| 2711 | */ |
| 2712 | xmlNodePtr |
| 2713 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 2714 | xmlNodePtr cur; |
| 2715 | |
| 2716 | cur = xmlNewComment(content); |
| 2717 | if (cur != NULL) cur->doc = doc; |
| 2718 | return(cur); |
| 2719 | } |
| 2720 | |
| 2721 | /** |
| 2722 | * xmlSetTreeDoc: |
| 2723 | * @tree: the top element |
| 2724 | * @doc: the document |
| 2725 | * |
| 2726 | * update all nodes under the tree to point to the right document |
| 2727 | */ |
| 2728 | void |
| 2729 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2730 | xmlAttrPtr prop; |
| 2731 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2732 | if (tree == NULL) |
| 2733 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2734 | if (tree->doc != doc) { |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2735 | if(tree->type == XML_ELEMENT_NODE) { |
| 2736 | prop = tree->properties; |
| 2737 | while (prop != NULL) { |
| 2738 | prop->doc = doc; |
| 2739 | xmlSetListDoc(prop->children, doc); |
| 2740 | prop = prop->next; |
| 2741 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2742 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2743 | if (tree->children != NULL) |
| 2744 | xmlSetListDoc(tree->children, doc); |
| 2745 | tree->doc = doc; |
| 2746 | } |
| 2747 | } |
| 2748 | |
| 2749 | /** |
| 2750 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2751 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2752 | * @doc: the document |
| 2753 | * |
| 2754 | * update all nodes in the list to point to the right document |
| 2755 | */ |
| 2756 | void |
| 2757 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2758 | xmlNodePtr cur; |
| 2759 | |
| 2760 | if (list == NULL) |
| 2761 | return; |
| 2762 | cur = list; |
| 2763 | while (cur != NULL) { |
| 2764 | if (cur->doc != doc) |
| 2765 | xmlSetTreeDoc(cur, doc); |
| 2766 | cur = cur->next; |
| 2767 | } |
| 2768 | } |
| 2769 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 2770 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2771 | /** |
| 2772 | * xmlNewChild: |
| 2773 | * @parent: the parent node |
| 2774 | * @ns: a namespace if any |
| 2775 | * @name: the name of the child |
| 2776 | * @content: the XML content of the child if any. |
| 2777 | * |
| 2778 | * 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] | 2779 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2780 | * created element inherits the namespace of @parent. If @content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2781 | * 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] | 2782 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 2783 | * references. XML special chars must be escaped first by using |
| 2784 | * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2785 | * |
| 2786 | * Returns a pointer to the new node object. |
| 2787 | */ |
| 2788 | xmlNodePtr |
| 2789 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2790 | const xmlChar *name, const xmlChar *content) { |
| 2791 | xmlNodePtr cur, prev; |
| 2792 | |
| 2793 | if (parent == NULL) { |
| 2794 | #ifdef DEBUG_TREE |
| 2795 | xmlGenericError(xmlGenericErrorContext, |
| 2796 | "xmlNewChild : parent == NULL\n"); |
| 2797 | #endif |
| 2798 | return(NULL); |
| 2799 | } |
| 2800 | |
| 2801 | if (name == NULL) { |
| 2802 | #ifdef DEBUG_TREE |
| 2803 | xmlGenericError(xmlGenericErrorContext, |
| 2804 | "xmlNewChild : name == NULL\n"); |
| 2805 | #endif |
| 2806 | return(NULL); |
| 2807 | } |
| 2808 | |
| 2809 | /* |
| 2810 | * Allocate a new node |
| 2811 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2812 | if (parent->type == XML_ELEMENT_NODE) { |
| 2813 | if (ns == NULL) |
| 2814 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2815 | else |
| 2816 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2817 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2818 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2819 | if (ns == NULL) |
| 2820 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2821 | else |
| 2822 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2823 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2824 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2825 | } else { |
| 2826 | return(NULL); |
| 2827 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2828 | if (cur == NULL) return(NULL); |
| 2829 | |
| 2830 | /* |
| 2831 | * add the new element at the end of the children list. |
| 2832 | */ |
| 2833 | cur->type = XML_ELEMENT_NODE; |
| 2834 | cur->parent = parent; |
| 2835 | cur->doc = parent->doc; |
| 2836 | if (parent->children == NULL) { |
| 2837 | parent->children = cur; |
| 2838 | parent->last = cur; |
| 2839 | } else { |
| 2840 | prev = parent->last; |
| 2841 | prev->next = cur; |
| 2842 | cur->prev = prev; |
| 2843 | parent->last = cur; |
| 2844 | } |
| 2845 | |
| 2846 | return(cur); |
| 2847 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2848 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2849 | |
| 2850 | /** |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2851 | * xmlAddPropSibling: |
| 2852 | * @prev: the attribute to which @prop is added after |
| 2853 | * @cur: the base attribute passed to calling function |
| 2854 | * @prop: the new attribute |
| 2855 | * |
| 2856 | * Add a new attribute after @prev using @cur as base attribute. |
| 2857 | * When inserting before @cur, @prev is passed as @cur->prev. |
| 2858 | * When inserting after @cur, @prev is passed as @cur. |
| 2859 | * If an existing attribute is found it is detroyed prior to adding @prop. |
| 2860 | * |
| 2861 | * Returns the attribute being inserted or NULL in case of error. |
| 2862 | */ |
| 2863 | static xmlNodePtr |
| 2864 | xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) { |
| 2865 | xmlAttrPtr attr; |
| 2866 | |
| 2867 | if (cur->type != XML_ATTRIBUTE_NODE) |
| 2868 | return(NULL); |
| 2869 | |
| 2870 | /* check if an attribute with the same name exists */ |
| 2871 | if (prop->ns == NULL) |
| 2872 | attr = xmlHasNsProp(cur->parent, prop->name, NULL); |
| 2873 | else |
| 2874 | attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href); |
| 2875 | |
| 2876 | if (prop->doc != cur->doc) { |
| 2877 | xmlSetTreeDoc(prop, cur->doc); |
| 2878 | } |
| 2879 | prop->parent = cur->parent; |
| 2880 | prop->prev = prev; |
| 2881 | if (prev != NULL) { |
| 2882 | prop->next = prev->next; |
| 2883 | prev->next = prop; |
| 2884 | if (prop->next) |
| 2885 | prop->next->prev = prop; |
| 2886 | } else { |
| 2887 | prop->next = cur; |
| 2888 | cur->prev = prop; |
| 2889 | } |
| 2890 | if (prop->prev == NULL && prop->parent != NULL) |
| 2891 | prop->parent->properties = (xmlAttrPtr) prop; |
| 2892 | if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) { |
| 2893 | /* different instance, destroy it (attributes must be unique) */ |
| 2894 | xmlRemoveProp((xmlAttrPtr) attr); |
| 2895 | } |
| 2896 | return prop; |
| 2897 | } |
| 2898 | |
| 2899 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2900 | * xmlAddNextSibling: |
| 2901 | * @cur: the child node |
| 2902 | * @elem: the new node |
| 2903 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2904 | * Add a new node @elem as the next sibling of @cur |
| 2905 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2906 | * first unlinked from its existing context. |
| 2907 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2908 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2909 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2910 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2911 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2912 | */ |
| 2913 | xmlNodePtr |
| 2914 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2915 | if (cur == NULL) { |
| 2916 | #ifdef DEBUG_TREE |
| 2917 | xmlGenericError(xmlGenericErrorContext, |
| 2918 | "xmlAddNextSibling : cur == NULL\n"); |
| 2919 | #endif |
| 2920 | return(NULL); |
| 2921 | } |
| 2922 | if (elem == NULL) { |
| 2923 | #ifdef DEBUG_TREE |
| 2924 | xmlGenericError(xmlGenericErrorContext, |
| 2925 | "xmlAddNextSibling : elem == NULL\n"); |
| 2926 | #endif |
| 2927 | return(NULL); |
| 2928 | } |
| 2929 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2930 | if (cur == elem) { |
| 2931 | #ifdef DEBUG_TREE |
| 2932 | xmlGenericError(xmlGenericErrorContext, |
| 2933 | "xmlAddNextSibling : cur == elem\n"); |
| 2934 | #endif |
| 2935 | return(NULL); |
| 2936 | } |
| 2937 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2938 | xmlUnlinkNode(elem); |
| 2939 | |
| 2940 | if (elem->type == XML_TEXT_NODE) { |
| 2941 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2942 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2943 | xmlFreeNode(elem); |
| 2944 | return(cur); |
| 2945 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2946 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 2947 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2948 | xmlChar *tmp; |
| 2949 | |
| 2950 | tmp = xmlStrdup(elem->content); |
| 2951 | tmp = xmlStrcat(tmp, cur->next->content); |
| 2952 | xmlNodeSetContent(cur->next, tmp); |
| 2953 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2954 | xmlFreeNode(elem); |
| 2955 | return(cur->next); |
| 2956 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2957 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2958 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | if (elem->doc != cur->doc) { |
| 2962 | xmlSetTreeDoc(elem, cur->doc); |
| 2963 | } |
| 2964 | elem->parent = cur->parent; |
| 2965 | elem->prev = cur; |
| 2966 | elem->next = cur->next; |
| 2967 | cur->next = elem; |
| 2968 | if (elem->next != NULL) |
| 2969 | elem->next->prev = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2970 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2971 | elem->parent->last = elem; |
| 2972 | return(elem); |
| 2973 | } |
| 2974 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 2975 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 2976 | defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2977 | /** |
| 2978 | * xmlAddPrevSibling: |
| 2979 | * @cur: the child node |
| 2980 | * @elem: the new node |
| 2981 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2982 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2983 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2984 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2985 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2986 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2987 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2988 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2989 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2990 | */ |
| 2991 | xmlNodePtr |
| 2992 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2993 | if (cur == NULL) { |
| 2994 | #ifdef DEBUG_TREE |
| 2995 | xmlGenericError(xmlGenericErrorContext, |
| 2996 | "xmlAddPrevSibling : cur == NULL\n"); |
| 2997 | #endif |
| 2998 | return(NULL); |
| 2999 | } |
| 3000 | if (elem == NULL) { |
| 3001 | #ifdef DEBUG_TREE |
| 3002 | xmlGenericError(xmlGenericErrorContext, |
| 3003 | "xmlAddPrevSibling : elem == NULL\n"); |
| 3004 | #endif |
| 3005 | return(NULL); |
| 3006 | } |
| 3007 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3008 | if (cur == elem) { |
| 3009 | #ifdef DEBUG_TREE |
| 3010 | xmlGenericError(xmlGenericErrorContext, |
| 3011 | "xmlAddPrevSibling : cur == elem\n"); |
| 3012 | #endif |
| 3013 | return(NULL); |
| 3014 | } |
| 3015 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3016 | xmlUnlinkNode(elem); |
| 3017 | |
| 3018 | if (elem->type == XML_TEXT_NODE) { |
| 3019 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3020 | xmlChar *tmp; |
| 3021 | |
| 3022 | tmp = xmlStrdup(elem->content); |
| 3023 | tmp = xmlStrcat(tmp, cur->content); |
| 3024 | xmlNodeSetContent(cur, tmp); |
| 3025 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3026 | xmlFreeNode(elem); |
| 3027 | return(cur); |
| 3028 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 3029 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 3030 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3031 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3032 | xmlFreeNode(elem); |
| 3033 | return(cur->prev); |
| 3034 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3035 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3036 | return xmlAddPropSibling(cur->prev, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | if (elem->doc != cur->doc) { |
| 3040 | xmlSetTreeDoc(elem, cur->doc); |
| 3041 | } |
| 3042 | elem->parent = cur->parent; |
| 3043 | elem->next = cur; |
| 3044 | elem->prev = cur->prev; |
| 3045 | cur->prev = elem; |
| 3046 | if (elem->prev != NULL) |
| 3047 | elem->prev->next = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3048 | if ((elem->parent != NULL) && (elem->parent->children == cur)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3049 | elem->parent->children = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3050 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3051 | return(elem); |
| 3052 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3053 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3054 | |
| 3055 | /** |
| 3056 | * xmlAddSibling: |
| 3057 | * @cur: the child node |
| 3058 | * @elem: the new node |
| 3059 | * |
| 3060 | * Add a new element @elem to the list of siblings of @cur |
| 3061 | * merging adjacent TEXT nodes (@elem may be freed) |
| 3062 | * If the new element was already inserted in a document it is |
| 3063 | * first unlinked from its existing context. |
| 3064 | * |
| 3065 | * Returns the new element or NULL in case of error. |
| 3066 | */ |
| 3067 | xmlNodePtr |
| 3068 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 3069 | xmlNodePtr parent; |
| 3070 | |
| 3071 | if (cur == NULL) { |
| 3072 | #ifdef DEBUG_TREE |
| 3073 | xmlGenericError(xmlGenericErrorContext, |
| 3074 | "xmlAddSibling : cur == NULL\n"); |
| 3075 | #endif |
| 3076 | return(NULL); |
| 3077 | } |
| 3078 | |
| 3079 | if (elem == NULL) { |
| 3080 | #ifdef DEBUG_TREE |
| 3081 | xmlGenericError(xmlGenericErrorContext, |
| 3082 | "xmlAddSibling : elem == NULL\n"); |
| 3083 | #endif |
| 3084 | return(NULL); |
| 3085 | } |
| 3086 | |
| 3087 | /* |
| 3088 | * Constant time is we can rely on the ->parent->last to find |
| 3089 | * the last sibling. |
| 3090 | */ |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3091 | if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3092 | (cur->parent->children != NULL) && |
| 3093 | (cur->parent->last != NULL) && |
| 3094 | (cur->parent->last->next == NULL)) { |
| 3095 | cur = cur->parent->last; |
| 3096 | } else { |
| 3097 | while (cur->next != NULL) cur = cur->next; |
| 3098 | } |
| 3099 | |
| 3100 | xmlUnlinkNode(elem); |
| 3101 | |
Daniel Veillard | e22dd5c | 2003-10-29 12:53:27 +0000 | [diff] [blame] | 3102 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) && |
| 3103 | (cur->name == elem->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3104 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3105 | xmlFreeNode(elem); |
| 3106 | return(cur); |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3107 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 3108 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | if (elem->doc != cur->doc) { |
| 3112 | xmlSetTreeDoc(elem, cur->doc); |
| 3113 | } |
| 3114 | parent = cur->parent; |
| 3115 | elem->prev = cur; |
| 3116 | elem->next = NULL; |
| 3117 | elem->parent = parent; |
| 3118 | cur->next = elem; |
| 3119 | if (parent != NULL) |
| 3120 | parent->last = elem; |
| 3121 | |
| 3122 | return(elem); |
| 3123 | } |
| 3124 | |
| 3125 | /** |
| 3126 | * xmlAddChildList: |
| 3127 | * @parent: the parent node |
| 3128 | * @cur: the first node in the list |
| 3129 | * |
| 3130 | * Add a list of node at the end of the child list of the parent |
| 3131 | * merging adjacent TEXT nodes (@cur may be freed) |
| 3132 | * |
| 3133 | * Returns the last child or NULL in case of error. |
| 3134 | */ |
| 3135 | xmlNodePtr |
| 3136 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 3137 | xmlNodePtr prev; |
| 3138 | |
| 3139 | if (parent == NULL) { |
| 3140 | #ifdef DEBUG_TREE |
| 3141 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3142 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3143 | #endif |
| 3144 | return(NULL); |
| 3145 | } |
| 3146 | |
| 3147 | if (cur == NULL) { |
| 3148 | #ifdef DEBUG_TREE |
| 3149 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3150 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3151 | #endif |
| 3152 | return(NULL); |
| 3153 | } |
| 3154 | |
| 3155 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 3156 | (cur->doc != parent->doc)) { |
| 3157 | #ifdef DEBUG_TREE |
| 3158 | xmlGenericError(xmlGenericErrorContext, |
| 3159 | "Elements moved to a different document\n"); |
| 3160 | #endif |
| 3161 | } |
| 3162 | |
| 3163 | /* |
| 3164 | * add the first element at the end of the children list. |
| 3165 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3166 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3167 | if (parent->children == NULL) { |
| 3168 | parent->children = cur; |
| 3169 | } else { |
| 3170 | /* |
| 3171 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 3172 | */ |
| 3173 | if ((cur->type == XML_TEXT_NODE) && |
| 3174 | (parent->last->type == XML_TEXT_NODE) && |
| 3175 | (cur->name == parent->last->name)) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3176 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3177 | /* |
| 3178 | * if it's the only child, nothing more to be done. |
| 3179 | */ |
| 3180 | if (cur->next == NULL) { |
| 3181 | xmlFreeNode(cur); |
| 3182 | return(parent->last); |
| 3183 | } |
| 3184 | prev = cur; |
| 3185 | cur = cur->next; |
| 3186 | xmlFreeNode(prev); |
| 3187 | } |
| 3188 | prev = parent->last; |
| 3189 | prev->next = cur; |
| 3190 | cur->prev = prev; |
| 3191 | } |
| 3192 | while (cur->next != NULL) { |
| 3193 | cur->parent = parent; |
| 3194 | if (cur->doc != parent->doc) { |
| 3195 | xmlSetTreeDoc(cur, parent->doc); |
| 3196 | } |
| 3197 | cur = cur->next; |
| 3198 | } |
| 3199 | cur->parent = parent; |
| 3200 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 3201 | parent->last = cur; |
| 3202 | |
| 3203 | return(cur); |
| 3204 | } |
| 3205 | |
| 3206 | /** |
| 3207 | * xmlAddChild: |
| 3208 | * @parent: the parent node |
| 3209 | * @cur: the child node |
| 3210 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3211 | * 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] | 3212 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3213 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 3214 | * If there is an attribute with equal name, it is first destroyed. |
| 3215 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3216 | * Returns the child or NULL in case of error. |
| 3217 | */ |
| 3218 | xmlNodePtr |
| 3219 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 3220 | xmlNodePtr prev; |
| 3221 | |
| 3222 | if (parent == NULL) { |
| 3223 | #ifdef DEBUG_TREE |
| 3224 | xmlGenericError(xmlGenericErrorContext, |
| 3225 | "xmlAddChild : parent == NULL\n"); |
| 3226 | #endif |
| 3227 | return(NULL); |
| 3228 | } |
| 3229 | |
| 3230 | if (cur == NULL) { |
| 3231 | #ifdef DEBUG_TREE |
| 3232 | xmlGenericError(xmlGenericErrorContext, |
| 3233 | "xmlAddChild : child == NULL\n"); |
| 3234 | #endif |
| 3235 | return(NULL); |
| 3236 | } |
| 3237 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3238 | if (parent == cur) { |
| 3239 | #ifdef DEBUG_TREE |
| 3240 | xmlGenericError(xmlGenericErrorContext, |
| 3241 | "xmlAddChild : parent == cur\n"); |
| 3242 | #endif |
| 3243 | return(NULL); |
| 3244 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3245 | /* |
| 3246 | * 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] | 3247 | * cur is then freed. |
| 3248 | */ |
| 3249 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3250 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3251 | (parent->content != NULL) && |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3252 | (parent->name == cur->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3253 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3254 | xmlFreeNode(cur); |
| 3255 | return(parent); |
| 3256 | } |
| 3257 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3258 | (parent->last->name == cur->name) && |
| 3259 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3260 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3261 | xmlFreeNode(cur); |
| 3262 | return(parent->last); |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | /* |
| 3267 | * add the new element at the end of the children list. |
| 3268 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3269 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3270 | cur->parent = parent; |
| 3271 | if (cur->doc != parent->doc) { |
| 3272 | xmlSetTreeDoc(cur, parent->doc); |
| 3273 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3274 | /* this check prevents a loop on tree-traversions if a developer |
| 3275 | * tries to add a node to its parent multiple times |
| 3276 | */ |
| 3277 | if (prev == parent) |
| 3278 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3279 | |
| 3280 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3281 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3282 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3283 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3284 | (parent->content != NULL) && |
| 3285 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3286 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3287 | xmlFreeNode(cur); |
| 3288 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3289 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3290 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3291 | if (parent->type != XML_ELEMENT_NODE) |
| 3292 | return(NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3293 | if (parent->properties == NULL) { |
| 3294 | parent->properties = (xmlAttrPtr) cur; |
| 3295 | } else { |
| 3296 | /* check if an attribute with the same name exists */ |
| 3297 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3298 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3299 | if (cur->ns == NULL) |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3300 | lastattr = xmlHasNsProp(parent, cur->name, NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3301 | else |
| 3302 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3303 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3304 | /* different instance, destroy it (attributes must be unique) */ |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3305 | xmlUnlinkNode((xmlNodePtr) lastattr); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3306 | xmlFreeProp(lastattr); |
| 3307 | } |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3308 | if (lastattr == (xmlAttrPtr) cur) |
| 3309 | return(cur); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3310 | /* find the end */ |
| 3311 | lastattr = parent->properties; |
| 3312 | while (lastattr->next != NULL) { |
| 3313 | lastattr = lastattr->next; |
| 3314 | } |
| 3315 | lastattr->next = (xmlAttrPtr) cur; |
| 3316 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3317 | } |
| 3318 | } else { |
| 3319 | if (parent->children == NULL) { |
| 3320 | parent->children = cur; |
| 3321 | parent->last = cur; |
| 3322 | } else { |
| 3323 | prev = parent->last; |
| 3324 | prev->next = cur; |
| 3325 | cur->prev = prev; |
| 3326 | parent->last = cur; |
| 3327 | } |
| 3328 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3329 | return(cur); |
| 3330 | } |
| 3331 | |
| 3332 | /** |
| 3333 | * xmlGetLastChild: |
| 3334 | * @parent: the parent node |
| 3335 | * |
| 3336 | * Search the last child of a node. |
| 3337 | * Returns the last child or NULL if none. |
| 3338 | */ |
| 3339 | xmlNodePtr |
| 3340 | xmlGetLastChild(xmlNodePtr parent) { |
| 3341 | if (parent == NULL) { |
| 3342 | #ifdef DEBUG_TREE |
| 3343 | xmlGenericError(xmlGenericErrorContext, |
| 3344 | "xmlGetLastChild : parent == NULL\n"); |
| 3345 | #endif |
| 3346 | return(NULL); |
| 3347 | } |
| 3348 | return(parent->last); |
| 3349 | } |
| 3350 | |
| 3351 | /** |
| 3352 | * xmlFreeNodeList: |
| 3353 | * @cur: the first node in the list |
| 3354 | * |
| 3355 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3356 | * the children are freed too. |
| 3357 | */ |
| 3358 | void |
| 3359 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3360 | xmlNodePtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3361 | xmlDictPtr dict = NULL; |
| 3362 | |
| 3363 | if (cur == NULL) return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3364 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3365 | xmlFreeNsList((xmlNsPtr) cur); |
| 3366 | return; |
| 3367 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3368 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3369 | #ifdef LIBXML_DOCB_ENABLED |
| 3370 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3371 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3372 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3373 | xmlFreeDoc((xmlDocPtr) cur); |
| 3374 | return; |
| 3375 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3376 | if (cur->doc != NULL) dict = cur->doc->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3377 | while (cur != NULL) { |
| 3378 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3379 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3380 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3381 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3382 | xmlDeregisterNodeDefaultValue(cur); |
| 3383 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3384 | if ((cur->children != NULL) && |
| 3385 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3386 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3387 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3388 | (cur->type == XML_XINCLUDE_START) || |
| 3389 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3390 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3391 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3392 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3393 | (cur->type != XML_XINCLUDE_START) && |
| 3394 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3395 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3396 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3397 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3398 | } |
| 3399 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3400 | (cur->type == XML_XINCLUDE_START) || |
| 3401 | (cur->type == XML_XINCLUDE_END)) && |
| 3402 | (cur->nsDef != NULL)) |
| 3403 | xmlFreeNsList(cur->nsDef); |
| 3404 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3405 | /* |
| 3406 | * When a node is a text node or a comment, it uses a global static |
| 3407 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3408 | * Otherwise the node name might come from the document's |
| 3409 | * dictionnary |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3410 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3411 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3412 | (cur->type != XML_TEXT_NODE) && |
| 3413 | (cur->type != XML_COMMENT_NODE)) |
| 3414 | DICT_FREE(cur->name) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3415 | xmlFree(cur); |
| 3416 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3417 | cur = next; |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | /** |
| 3422 | * xmlFreeNode: |
| 3423 | * @cur: the node |
| 3424 | * |
| 3425 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3426 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3427 | */ |
| 3428 | void |
| 3429 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3430 | xmlDictPtr dict = NULL; |
| 3431 | |
| 3432 | if (cur == NULL) return; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3433 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3434 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3435 | if (cur->type == XML_DTD_NODE) { |
| 3436 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3437 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3438 | } |
| 3439 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3440 | xmlFreeNs((xmlNsPtr) cur); |
| 3441 | return; |
| 3442 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3443 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3444 | xmlFreeProp((xmlAttrPtr) cur); |
| 3445 | return; |
| 3446 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3447 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3448 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3449 | xmlDeregisterNodeDefaultValue(cur); |
| 3450 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3451 | if (cur->doc != NULL) dict = cur->doc->dict; |
| 3452 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3453 | if ((cur->children != NULL) && |
| 3454 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3455 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3456 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3457 | (cur->type == XML_XINCLUDE_START) || |
| 3458 | (cur->type == XML_XINCLUDE_END)) && |
| 3459 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3460 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3461 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3462 | (cur->content != NULL) && |
| 3463 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3464 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3465 | (cur->type != XML_XINCLUDE_START) && |
| 3466 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3467 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3470 | /* |
| 3471 | * When a node is a text node or a comment, it uses a global static |
| 3472 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3473 | * Otherwise the node name might come from the document's dictionnary |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3474 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3475 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3476 | (cur->type != XML_TEXT_NODE) && |
| 3477 | (cur->type != XML_COMMENT_NODE)) |
| 3478 | DICT_FREE(cur->name) |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3479 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3480 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3481 | (cur->type == XML_XINCLUDE_START) || |
| 3482 | (cur->type == XML_XINCLUDE_END)) && |
| 3483 | (cur->nsDef != NULL)) |
| 3484 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3485 | xmlFree(cur); |
| 3486 | } |
| 3487 | |
| 3488 | /** |
| 3489 | * xmlUnlinkNode: |
| 3490 | * @cur: the node |
| 3491 | * |
| 3492 | * Unlink a node from it's current context, the node is not freed |
| 3493 | */ |
| 3494 | void |
| 3495 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3496 | if (cur == NULL) { |
| 3497 | #ifdef DEBUG_TREE |
| 3498 | xmlGenericError(xmlGenericErrorContext, |
| 3499 | "xmlUnlinkNode : node == NULL\n"); |
| 3500 | #endif |
| 3501 | return; |
| 3502 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3503 | if (cur->type == XML_DTD_NODE) { |
| 3504 | xmlDocPtr doc; |
| 3505 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3506 | if (doc != NULL) { |
| 3507 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3508 | doc->intSubset = NULL; |
| 3509 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3510 | doc->extSubset = NULL; |
| 3511 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3512 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3513 | if (cur->parent != NULL) { |
| 3514 | xmlNodePtr parent; |
| 3515 | parent = cur->parent; |
| 3516 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3517 | if (parent->properties == (xmlAttrPtr) cur) |
| 3518 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3519 | } else { |
| 3520 | if (parent->children == cur) |
| 3521 | parent->children = cur->next; |
| 3522 | if (parent->last == cur) |
| 3523 | parent->last = cur->prev; |
| 3524 | } |
| 3525 | cur->parent = NULL; |
| 3526 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3527 | if (cur->next != NULL) |
| 3528 | cur->next->prev = cur->prev; |
| 3529 | if (cur->prev != NULL) |
| 3530 | cur->prev->next = cur->next; |
| 3531 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 3534 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3535 | /** |
| 3536 | * xmlReplaceNode: |
| 3537 | * @old: the old node |
| 3538 | * @cur: the node |
| 3539 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 3540 | * Unlink the old node from its current context, prune the new one |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3541 | * 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] | 3542 | * first unlinked from its existing context. |
| 3543 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3544 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3545 | */ |
| 3546 | xmlNodePtr |
| 3547 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 3548 | if (old == cur) return(NULL); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3549 | if ((old == NULL) || (old->parent == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3550 | #ifdef DEBUG_TREE |
| 3551 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3552 | "xmlReplaceNode : old == NULL or without parent\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3553 | #endif |
| 3554 | return(NULL); |
| 3555 | } |
| 3556 | if (cur == NULL) { |
| 3557 | xmlUnlinkNode(old); |
| 3558 | return(old); |
| 3559 | } |
| 3560 | if (cur == old) { |
| 3561 | return(old); |
| 3562 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3563 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3564 | #ifdef DEBUG_TREE |
| 3565 | xmlGenericError(xmlGenericErrorContext, |
| 3566 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3567 | #endif |
| 3568 | return(old); |
| 3569 | } |
| 3570 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3571 | #ifdef DEBUG_TREE |
| 3572 | xmlGenericError(xmlGenericErrorContext, |
| 3573 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3574 | #endif |
| 3575 | return(old); |
| 3576 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3577 | xmlUnlinkNode(cur); |
Daniel Veillard | 64d7d12 | 2005-05-11 18:03:42 +0000 | [diff] [blame] | 3578 | xmlSetTreeDoc(cur, old->doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3579 | cur->parent = old->parent; |
| 3580 | cur->next = old->next; |
| 3581 | if (cur->next != NULL) |
| 3582 | cur->next->prev = cur; |
| 3583 | cur->prev = old->prev; |
| 3584 | if (cur->prev != NULL) |
| 3585 | cur->prev->next = cur; |
| 3586 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3587 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3588 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3589 | cur->parent->properties = ((xmlAttrPtr) cur); |
| 3590 | } else { |
| 3591 | if (cur->parent->children == old) |
| 3592 | cur->parent->children = cur; |
| 3593 | if (cur->parent->last == old) |
| 3594 | cur->parent->last = cur; |
| 3595 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3596 | } |
| 3597 | old->next = old->prev = NULL; |
| 3598 | old->parent = NULL; |
| 3599 | return(old); |
| 3600 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3601 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3602 | |
| 3603 | /************************************************************************ |
| 3604 | * * |
| 3605 | * Copy operations * |
| 3606 | * * |
| 3607 | ************************************************************************/ |
| 3608 | |
| 3609 | /** |
| 3610 | * xmlCopyNamespace: |
| 3611 | * @cur: the namespace |
| 3612 | * |
| 3613 | * Do a copy of the namespace. |
| 3614 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3615 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3616 | */ |
| 3617 | xmlNsPtr |
| 3618 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3619 | xmlNsPtr ret; |
| 3620 | |
| 3621 | if (cur == NULL) return(NULL); |
| 3622 | switch (cur->type) { |
| 3623 | case XML_LOCAL_NAMESPACE: |
| 3624 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3625 | break; |
| 3626 | default: |
| 3627 | #ifdef DEBUG_TREE |
| 3628 | xmlGenericError(xmlGenericErrorContext, |
| 3629 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3630 | #endif |
| 3631 | return(NULL); |
| 3632 | } |
| 3633 | return(ret); |
| 3634 | } |
| 3635 | |
| 3636 | /** |
| 3637 | * xmlCopyNamespaceList: |
| 3638 | * @cur: the first namespace |
| 3639 | * |
| 3640 | * Do a copy of an namespace list. |
| 3641 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3642 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3643 | */ |
| 3644 | xmlNsPtr |
| 3645 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3646 | xmlNsPtr ret = NULL; |
| 3647 | xmlNsPtr p = NULL,q; |
| 3648 | |
| 3649 | while (cur != NULL) { |
| 3650 | q = xmlCopyNamespace(cur); |
| 3651 | if (p == NULL) { |
| 3652 | ret = p = q; |
| 3653 | } else { |
| 3654 | p->next = q; |
| 3655 | p = q; |
| 3656 | } |
| 3657 | cur = cur->next; |
| 3658 | } |
| 3659 | return(ret); |
| 3660 | } |
| 3661 | |
| 3662 | static xmlNodePtr |
| 3663 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3664 | |
| 3665 | static xmlAttrPtr |
| 3666 | xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3667 | xmlAttrPtr ret; |
| 3668 | |
| 3669 | if (cur == NULL) return(NULL); |
| 3670 | if (target != NULL) |
| 3671 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3672 | else if (doc != NULL) |
| 3673 | ret = xmlNewDocProp(doc, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3674 | else if (cur->parent != NULL) |
| 3675 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 3676 | else if (cur->children != NULL) |
| 3677 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 3678 | else |
| 3679 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 3680 | if (ret == NULL) return(NULL); |
| 3681 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3682 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3683 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3684 | xmlNsPtr ns; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3685 | |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3686 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3687 | if (ns == NULL) { |
| 3688 | /* |
| 3689 | * Humm, we are copying an element whose namespace is defined |
| 3690 | * out of the new tree scope. Search it in the original tree |
| 3691 | * and add it at the top of the new tree |
| 3692 | */ |
| 3693 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 3694 | if (ns != NULL) { |
| 3695 | xmlNodePtr root = target; |
| 3696 | xmlNodePtr pred = NULL; |
| 3697 | |
| 3698 | while (root->parent != NULL) { |
| 3699 | pred = root; |
| 3700 | root = root->parent; |
| 3701 | } |
| 3702 | if (root == (xmlNodePtr) target->doc) { |
| 3703 | /* correct possibly cycling above the document elt */ |
| 3704 | root = pred; |
| 3705 | } |
| 3706 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 3707 | } |
| 3708 | } else { |
| 3709 | /* |
| 3710 | * we have to find something appropriate here since |
| 3711 | * we cant be sure, that the namespce we found is identified |
| 3712 | * by the prefix |
| 3713 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3714 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3715 | /* this is the nice case */ |
| 3716 | ret->ns = ns; |
| 3717 | } else { |
| 3718 | /* |
| 3719 | * we are in trouble: we need a new reconcilied namespace. |
| 3720 | * This is expensive |
| 3721 | */ |
| 3722 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 3723 | } |
| 3724 | } |
| 3725 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3726 | } else |
| 3727 | ret->ns = NULL; |
| 3728 | |
| 3729 | if (cur->children != NULL) { |
| 3730 | xmlNodePtr tmp; |
| 3731 | |
| 3732 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 3733 | ret->last = NULL; |
| 3734 | tmp = ret->children; |
| 3735 | while (tmp != NULL) { |
| 3736 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 3737 | if (tmp->next == NULL) |
| 3738 | ret->last = tmp; |
| 3739 | tmp = tmp->next; |
| 3740 | } |
| 3741 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3742 | /* |
| 3743 | * Try to handle IDs |
| 3744 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 3745 | if ((target!= NULL) && (cur!= NULL) && |
| 3746 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3747 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 3748 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 3749 | xmlChar *id; |
| 3750 | |
| 3751 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 3752 | if (id != NULL) { |
| 3753 | xmlAddID(NULL, target->doc, id, ret); |
| 3754 | xmlFree(id); |
| 3755 | } |
| 3756 | } |
| 3757 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3758 | return(ret); |
| 3759 | } |
| 3760 | |
| 3761 | /** |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3762 | * xmlCopyProp: |
| 3763 | * @target: the element where the attribute will be grafted |
| 3764 | * @cur: the attribute |
| 3765 | * |
| 3766 | * Do a copy of the attribute. |
| 3767 | * |
| 3768 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
| 3769 | */ |
| 3770 | xmlAttrPtr |
| 3771 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 3772 | return xmlCopyPropInternal(NULL, target, cur); |
| 3773 | } |
| 3774 | |
| 3775 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3776 | * xmlCopyPropList: |
| 3777 | * @target: the element where the attributes will be grafted |
| 3778 | * @cur: the first attribute |
| 3779 | * |
| 3780 | * Do a copy of an attribute list. |
| 3781 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3782 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3783 | */ |
| 3784 | xmlAttrPtr |
| 3785 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 3786 | xmlAttrPtr ret = NULL; |
| 3787 | xmlAttrPtr p = NULL,q; |
| 3788 | |
| 3789 | while (cur != NULL) { |
| 3790 | q = xmlCopyProp(target, cur); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 3791 | if (q == NULL) |
| 3792 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3793 | if (p == NULL) { |
| 3794 | ret = p = q; |
| 3795 | } else { |
| 3796 | p->next = q; |
| 3797 | q->prev = p; |
| 3798 | p = q; |
| 3799 | } |
| 3800 | cur = cur->next; |
| 3801 | } |
| 3802 | return(ret); |
| 3803 | } |
| 3804 | |
| 3805 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3806 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3807 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3808 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3809 | * tricky reason: namespaces. Doing a direct copy of a node |
| 3810 | * say RPM:Copyright without changing the namespace pointer to |
| 3811 | * something else can produce stale links. One way to do it is |
| 3812 | * to keep a reference counter but this doesn't work as soon |
| 3813 | * as one move the element or the subtree out of the scope of |
| 3814 | * the existing namespace. The actual solution seems to add |
| 3815 | * a copy of the namespace at the top of the copied tree if |
| 3816 | * not available in the subtree. |
| 3817 | * Hence two functions, the public front-end call the inner ones |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3818 | * The argument "recursive" normally indicates a recursive copy |
| 3819 | * of the node with values 0 (no) and 1 (yes). For XInclude, |
| 3820 | * however, we allow a value of 2 to indicate copy properties and |
| 3821 | * namespace info, but don't recurse on children. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3822 | */ |
| 3823 | |
| 3824 | static xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3825 | xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3826 | int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3827 | xmlNodePtr ret; |
| 3828 | |
| 3829 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3830 | switch (node->type) { |
| 3831 | case XML_TEXT_NODE: |
| 3832 | case XML_CDATA_SECTION_NODE: |
| 3833 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 3834 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3835 | case XML_ENTITY_REF_NODE: |
| 3836 | case XML_ENTITY_NODE: |
| 3837 | case XML_PI_NODE: |
| 3838 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3839 | case XML_XINCLUDE_START: |
| 3840 | case XML_XINCLUDE_END: |
| 3841 | break; |
| 3842 | case XML_ATTRIBUTE_NODE: |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3843 | return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node)); |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3844 | case XML_NAMESPACE_DECL: |
| 3845 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
| 3846 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3847 | case XML_DOCUMENT_NODE: |
| 3848 | case XML_HTML_DOCUMENT_NODE: |
| 3849 | #ifdef LIBXML_DOCB_ENABLED |
| 3850 | case XML_DOCB_DOCUMENT_NODE: |
| 3851 | #endif |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3852 | #ifdef LIBXML_TREE_ENABLED |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3853 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended)); |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3854 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3855 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3856 | case XML_NOTATION_NODE: |
| 3857 | case XML_DTD_NODE: |
| 3858 | case XML_ELEMENT_DECL: |
| 3859 | case XML_ATTRIBUTE_DECL: |
| 3860 | case XML_ENTITY_DECL: |
| 3861 | return(NULL); |
| 3862 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3863 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3864 | /* |
| 3865 | * Allocate a new node and fill the fields. |
| 3866 | */ |
| 3867 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 3868 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 3869 | xmlTreeErrMemory("copying node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3870 | return(NULL); |
| 3871 | } |
| 3872 | memset(ret, 0, sizeof(xmlNode)); |
| 3873 | ret->type = node->type; |
| 3874 | |
| 3875 | ret->doc = doc; |
| 3876 | ret->parent = parent; |
| 3877 | if (node->name == xmlStringText) |
| 3878 | ret->name = xmlStringText; |
| 3879 | else if (node->name == xmlStringTextNoenc) |
| 3880 | ret->name = xmlStringTextNoenc; |
| 3881 | else if (node->name == xmlStringComment) |
| 3882 | ret->name = xmlStringComment; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 3883 | else if (node->name != NULL) { |
| 3884 | if ((doc != NULL) && (doc->dict != NULL)) |
| 3885 | ret->name = xmlDictLookup(doc->dict, node->name, -1); |
| 3886 | else |
| 3887 | ret->name = xmlStrdup(node->name); |
| 3888 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3889 | if ((node->type != XML_ELEMENT_NODE) && |
| 3890 | (node->content != NULL) && |
| 3891 | (node->type != XML_ENTITY_REF_NODE) && |
| 3892 | (node->type != XML_XINCLUDE_END) && |
| 3893 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3894 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3895 | }else{ |
| 3896 | if (node->type == XML_ELEMENT_NODE) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 3897 | ret->line = node->line; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3898 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3899 | if (parent != NULL) { |
| 3900 | xmlNodePtr tmp; |
| 3901 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3902 | /* |
| 3903 | * this is a tricky part for the node register thing: |
| 3904 | * in case ret does get coalesced in xmlAddChild |
| 3905 | * the deregister-node callback is called; so we register ret now already |
| 3906 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3907 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3908 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 3909 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3910 | tmp = xmlAddChild(parent, ret); |
| 3911 | /* node could have coalesced */ |
| 3912 | if (tmp != ret) |
| 3913 | return(tmp); |
| 3914 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3915 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3916 | if (!extended) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3917 | goto out; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3918 | if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3919 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 3920 | |
| 3921 | if (node->ns != NULL) { |
| 3922 | xmlNsPtr ns; |
| 3923 | |
| 3924 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 3925 | if (ns == NULL) { |
| 3926 | /* |
| 3927 | * Humm, we are copying an element whose namespace is defined |
| 3928 | * out of the new tree scope. Search it in the original tree |
| 3929 | * and add it at the top of the new tree |
| 3930 | */ |
| 3931 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 3932 | if (ns != NULL) { |
| 3933 | xmlNodePtr root = ret; |
| 3934 | |
| 3935 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 3936 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3937 | } |
| 3938 | } else { |
| 3939 | /* |
| 3940 | * reference the existing namespace definition in our own tree. |
| 3941 | */ |
| 3942 | ret->ns = ns; |
| 3943 | } |
| 3944 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3945 | if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3946 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3947 | if (node->type == XML_ENTITY_REF_NODE) { |
| 3948 | if ((doc == NULL) || (node->doc != doc)) { |
| 3949 | /* |
| 3950 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3951 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3952 | * we cannot keep the reference. Try to find it in the |
| 3953 | * target document. |
| 3954 | */ |
| 3955 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 3956 | } else { |
| 3957 | ret->children = node->children; |
| 3958 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3959 | ret->last = ret->children; |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3960 | } else if ((node->children != NULL) && (extended != 2)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3961 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3962 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 3963 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3964 | |
| 3965 | out: |
| 3966 | /* if parent != NULL we already registered the node above */ |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 3967 | if ((parent == NULL) && |
| 3968 | ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3969 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3970 | return(ret); |
| 3971 | } |
| 3972 | |
| 3973 | static xmlNodePtr |
| 3974 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 3975 | xmlNodePtr ret = NULL; |
| 3976 | xmlNodePtr p = NULL,q; |
| 3977 | |
| 3978 | while (node != NULL) { |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3979 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3980 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 3981 | if (doc == NULL) { |
| 3982 | node = node->next; |
| 3983 | continue; |
| 3984 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3985 | if (doc->intSubset == NULL) { |
| 3986 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
| 3987 | q->doc = doc; |
| 3988 | q->parent = parent; |
| 3989 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3990 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3991 | } else { |
| 3992 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3993 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3994 | } |
| 3995 | } else |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3996 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3997 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3998 | if (ret == NULL) { |
| 3999 | q->prev = NULL; |
| 4000 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4001 | } else if (p != q) { |
| 4002 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4003 | p->next = q; |
| 4004 | q->prev = p; |
| 4005 | p = q; |
| 4006 | } |
| 4007 | node = node->next; |
| 4008 | } |
| 4009 | return(ret); |
| 4010 | } |
| 4011 | |
| 4012 | /** |
| 4013 | * xmlCopyNode: |
| 4014 | * @node: the node |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4015 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4016 | * when applicable) |
| 4017 | * if 2 copy properties and namespaces (when applicable) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4018 | * |
| 4019 | * Do a copy of the node. |
| 4020 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4021 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4022 | */ |
| 4023 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4024 | xmlCopyNode(const xmlNodePtr node, int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4025 | xmlNodePtr ret; |
| 4026 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4027 | ret = xmlStaticCopyNode(node, NULL, NULL, extended); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4028 | return(ret); |
| 4029 | } |
| 4030 | |
| 4031 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4032 | * xmlDocCopyNode: |
| 4033 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4034 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4035 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4036 | * when applicable) |
| 4037 | * if 2 copy properties and namespaces (when applicable) |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4038 | * |
| 4039 | * Do a copy of the node to a given document. |
| 4040 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4041 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4042 | */ |
| 4043 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4044 | xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4045 | xmlNodePtr ret; |
| 4046 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4047 | ret = xmlStaticCopyNode(node, doc, NULL, extended); |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4048 | return(ret); |
| 4049 | } |
| 4050 | |
| 4051 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4052 | * xmlDocCopyNodeList: |
| 4053 | * @doc: the target document |
| 4054 | * @node: the first node in the list. |
| 4055 | * |
| 4056 | * Do a recursive copy of the node list. |
| 4057 | * |
| 4058 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
| 4059 | */ |
| 4060 | xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) { |
| 4061 | xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL); |
| 4062 | return(ret); |
| 4063 | } |
| 4064 | |
| 4065 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4066 | * xmlCopyNodeList: |
| 4067 | * @node: the first node in the list. |
| 4068 | * |
| 4069 | * Do a recursive copy of the node list. |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4070 | * Use xmlDocCopyNodeList() if possible to ensure string interning. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4071 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4072 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4073 | */ |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 4074 | xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4075 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 4076 | return(ret); |
| 4077 | } |
| 4078 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4079 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4080 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4081 | * xmlCopyDtd: |
| 4082 | * @dtd: the dtd |
| 4083 | * |
| 4084 | * Do a copy of the dtd. |
| 4085 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4086 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4087 | */ |
| 4088 | xmlDtdPtr |
| 4089 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 4090 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4091 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4092 | |
| 4093 | if (dtd == NULL) return(NULL); |
| 4094 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 4095 | if (ret == NULL) return(NULL); |
| 4096 | if (dtd->entities != NULL) |
| 4097 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 4098 | (xmlEntitiesTablePtr) dtd->entities); |
| 4099 | if (dtd->notations != NULL) |
| 4100 | ret->notations = (void *) xmlCopyNotationTable( |
| 4101 | (xmlNotationTablePtr) dtd->notations); |
| 4102 | if (dtd->elements != NULL) |
| 4103 | ret->elements = (void *) xmlCopyElementTable( |
| 4104 | (xmlElementTablePtr) dtd->elements); |
| 4105 | if (dtd->attributes != NULL) |
| 4106 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 4107 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4108 | if (dtd->pentities != NULL) |
| 4109 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 4110 | (xmlEntitiesTablePtr) dtd->pentities); |
| 4111 | |
| 4112 | cur = dtd->children; |
| 4113 | while (cur != NULL) { |
| 4114 | q = NULL; |
| 4115 | |
| 4116 | if (cur->type == XML_ENTITY_DECL) { |
| 4117 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 4118 | switch (tmp->etype) { |
| 4119 | case XML_INTERNAL_GENERAL_ENTITY: |
| 4120 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 4121 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 4122 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 4123 | break; |
| 4124 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 4125 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 4126 | q = (xmlNodePtr) |
| 4127 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 4128 | break; |
| 4129 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 4130 | break; |
| 4131 | } |
| 4132 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 4133 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 4134 | q = (xmlNodePtr) |
| 4135 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 4136 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4137 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
| 4138 | q = (xmlNodePtr) |
| 4139 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 4140 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4141 | q = xmlCopyNode(cur, 0); |
| 4142 | } |
| 4143 | |
| 4144 | if (q == NULL) { |
| 4145 | cur = cur->next; |
| 4146 | continue; |
| 4147 | } |
| 4148 | |
| 4149 | if (p == NULL) |
| 4150 | ret->children = q; |
| 4151 | else |
| 4152 | p->next = q; |
| 4153 | |
| 4154 | q->prev = p; |
| 4155 | q->parent = (xmlNodePtr) ret; |
| 4156 | q->next = NULL; |
| 4157 | ret->last = q; |
| 4158 | p = q; |
| 4159 | cur = cur->next; |
| 4160 | } |
| 4161 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4162 | return(ret); |
| 4163 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4164 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4165 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4166 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4167 | /** |
| 4168 | * xmlCopyDoc: |
| 4169 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4170 | * @recursive: if not zero do a recursive copy. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4171 | * |
| 4172 | * 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] | 4173 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4174 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4175 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4176 | */ |
| 4177 | xmlDocPtr |
| 4178 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 4179 | xmlDocPtr ret; |
| 4180 | |
| 4181 | if (doc == NULL) return(NULL); |
| 4182 | ret = xmlNewDoc(doc->version); |
| 4183 | if (ret == NULL) return(NULL); |
| 4184 | if (doc->name != NULL) |
| 4185 | ret->name = xmlMemStrdup(doc->name); |
| 4186 | if (doc->encoding != NULL) |
| 4187 | ret->encoding = xmlStrdup(doc->encoding); |
Daniel Veillard | f59507d | 2005-01-27 17:26:49 +0000 | [diff] [blame] | 4188 | if (doc->URL != NULL) |
| 4189 | ret->URL = xmlStrdup(doc->URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4190 | ret->charset = doc->charset; |
| 4191 | ret->compression = doc->compression; |
| 4192 | ret->standalone = doc->standalone; |
| 4193 | if (!recursive) return(ret); |
| 4194 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4195 | ret->last = NULL; |
| 4196 | ret->children = NULL; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4197 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4198 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4199 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4200 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4201 | ret->intSubset->parent = ret; |
| 4202 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4203 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4204 | if (doc->oldNs != NULL) |
| 4205 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 4206 | if (doc->children != NULL) { |
| 4207 | xmlNodePtr tmp; |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4208 | |
| 4209 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 4210 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4211 | ret->last = NULL; |
| 4212 | tmp = ret->children; |
| 4213 | while (tmp != NULL) { |
| 4214 | if (tmp->next == NULL) |
| 4215 | ret->last = tmp; |
| 4216 | tmp = tmp->next; |
| 4217 | } |
| 4218 | } |
| 4219 | return(ret); |
| 4220 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4221 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4222 | |
| 4223 | /************************************************************************ |
| 4224 | * * |
| 4225 | * Content access functions * |
| 4226 | * * |
| 4227 | ************************************************************************/ |
| 4228 | |
| 4229 | /** |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4230 | * xmlGetLineNo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4231 | * @node: valid node |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4232 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 4233 | * Get line number of @node. This requires activation of this option |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4234 | * before invoking the parser by calling xmlLineNumbersDefault(1) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4235 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4236 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4237 | */ |
| 4238 | long |
| 4239 | xmlGetLineNo(xmlNodePtr node) |
| 4240 | { |
| 4241 | long result = -1; |
| 4242 | |
| 4243 | if (!node) |
| 4244 | return result; |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4245 | if ((node->type == XML_ELEMENT_NODE) || |
| 4246 | (node->type == XML_TEXT_NODE) || |
| 4247 | (node->type == XML_COMMENT_NODE) || |
| 4248 | (node->type == XML_PI_NODE)) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 4249 | result = (long) node->line; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4250 | else if ((node->prev != NULL) && |
| 4251 | ((node->prev->type == XML_ELEMENT_NODE) || |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4252 | (node->prev->type == XML_TEXT_NODE) || |
| 4253 | (node->prev->type == XML_COMMENT_NODE) || |
| 4254 | (node->prev->type == XML_PI_NODE))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4255 | result = xmlGetLineNo(node->prev); |
| 4256 | else if ((node->parent != NULL) && |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4257 | (node->parent->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4258 | result = xmlGetLineNo(node->parent); |
| 4259 | |
| 4260 | return result; |
| 4261 | } |
| 4262 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4263 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4264 | /** |
| 4265 | * xmlGetNodePath: |
| 4266 | * @node: a node |
| 4267 | * |
| 4268 | * Build a structure based Path for the given node |
| 4269 | * |
| 4270 | * Returns the new path or NULL in case of error. The caller must free |
| 4271 | * the returned string |
| 4272 | */ |
| 4273 | xmlChar * |
| 4274 | xmlGetNodePath(xmlNodePtr node) |
| 4275 | { |
| 4276 | xmlNodePtr cur, tmp, next; |
| 4277 | xmlChar *buffer = NULL, *temp; |
| 4278 | size_t buf_len; |
| 4279 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4280 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4281 | const char *name; |
| 4282 | char nametemp[100]; |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4283 | int occur = 0, generic; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4284 | |
| 4285 | if (node == NULL) |
| 4286 | return (NULL); |
| 4287 | |
| 4288 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4289 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4290 | if (buffer == NULL) { |
| 4291 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4292 | return (NULL); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4293 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4294 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4295 | if (buf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4296 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4297 | xmlFree(buffer); |
| 4298 | return (NULL); |
| 4299 | } |
| 4300 | |
| 4301 | buffer[0] = 0; |
| 4302 | cur = node; |
| 4303 | do { |
| 4304 | name = ""; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4305 | sep = "?"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4306 | occur = 0; |
| 4307 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4308 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4309 | if (buffer[0] == '/') |
| 4310 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4311 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4312 | next = NULL; |
| 4313 | } else if (cur->type == XML_ELEMENT_NODE) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4314 | generic = 0; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4315 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4316 | name = (const char *) cur->name; |
| 4317 | if (cur->ns) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4318 | if (cur->ns->prefix != NULL) { |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4319 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
| 4320 | (char *)cur->ns->prefix, (char *)cur->name); |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4321 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4322 | name = nametemp; |
| 4323 | } else { |
| 4324 | /* |
| 4325 | * We cannot express named elements in the default |
| 4326 | * namespace, so use "*". |
| 4327 | */ |
| 4328 | generic = 1; |
| 4329 | name = "*"; |
| 4330 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4331 | } |
| 4332 | next = cur->parent; |
| 4333 | |
| 4334 | /* |
| 4335 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4336 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4337 | */ |
| 4338 | tmp = cur->prev; |
| 4339 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4340 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4341 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4342 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4343 | ((tmp->ns == cur->ns) || |
| 4344 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4345 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4346 | occur++; |
| 4347 | tmp = tmp->prev; |
| 4348 | } |
| 4349 | if (occur == 0) { |
| 4350 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4351 | while (tmp != NULL && occur == 0) { |
| 4352 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4353 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4354 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4355 | ((tmp->ns == cur->ns) || |
| 4356 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4357 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4358 | occur++; |
| 4359 | tmp = tmp->next; |
| 4360 | } |
| 4361 | if (occur != 0) |
| 4362 | occur = 1; |
| 4363 | } else |
| 4364 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4365 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4366 | sep = "/"; |
| 4367 | name = "comment()"; |
| 4368 | next = cur->parent; |
| 4369 | |
| 4370 | /* |
| 4371 | * Thumbler index computation |
| 4372 | */ |
| 4373 | tmp = cur->prev; |
| 4374 | while (tmp != NULL) { |
| 4375 | if (tmp->type == XML_COMMENT_NODE) |
| 4376 | occur++; |
| 4377 | tmp = tmp->prev; |
| 4378 | } |
| 4379 | if (occur == 0) { |
| 4380 | tmp = cur->next; |
| 4381 | while (tmp != NULL && occur == 0) { |
| 4382 | if (tmp->type == XML_COMMENT_NODE) |
| 4383 | occur++; |
| 4384 | tmp = tmp->next; |
| 4385 | } |
| 4386 | if (occur != 0) |
| 4387 | occur = 1; |
| 4388 | } else |
| 4389 | occur++; |
| 4390 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4391 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4392 | sep = "/"; |
| 4393 | name = "text()"; |
| 4394 | next = cur->parent; |
| 4395 | |
| 4396 | /* |
| 4397 | * Thumbler index computation |
| 4398 | */ |
| 4399 | tmp = cur->prev; |
| 4400 | while (tmp != NULL) { |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4401 | if ((tmp->type == XML_TEXT_NODE) || |
| 4402 | (tmp->type == XML_CDATA_SECTION_NODE)) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4403 | occur++; |
| 4404 | tmp = tmp->prev; |
| 4405 | } |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4406 | /* |
| 4407 | * Evaluate if this is the only text- or CDATA-section-node; |
| 4408 | * if yes, then we'll get "text()", otherwise "text()[1]". |
| 4409 | */ |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4410 | if (occur == 0) { |
| 4411 | tmp = cur->next; |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4412 | while (tmp != NULL) { |
| 4413 | if ((tmp->type == XML_TEXT_NODE) || |
| 4414 | (tmp->type == XML_CDATA_SECTION_NODE)) |
| 4415 | { |
| 4416 | occur = 1; |
| 4417 | break; |
| 4418 | } |
| 4419 | tmp = tmp->next; |
| 4420 | } |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4421 | } else |
| 4422 | occur++; |
| 4423 | } else if (cur->type == XML_PI_NODE) { |
| 4424 | sep = "/"; |
| 4425 | snprintf(nametemp, sizeof(nametemp) - 1, |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4426 | "processing-instruction('%s')", (char *)cur->name); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4427 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4428 | name = nametemp; |
| 4429 | |
| 4430 | next = cur->parent; |
| 4431 | |
| 4432 | /* |
| 4433 | * Thumbler index computation |
| 4434 | */ |
| 4435 | tmp = cur->prev; |
| 4436 | while (tmp != NULL) { |
| 4437 | if ((tmp->type == XML_PI_NODE) && |
| 4438 | (xmlStrEqual(cur->name, tmp->name))) |
| 4439 | occur++; |
| 4440 | tmp = tmp->prev; |
| 4441 | } |
| 4442 | if (occur == 0) { |
| 4443 | tmp = cur->next; |
| 4444 | while (tmp != NULL && occur == 0) { |
| 4445 | if ((tmp->type == XML_PI_NODE) && |
| 4446 | (xmlStrEqual(cur->name, tmp->name))) |
| 4447 | occur++; |
| 4448 | tmp = tmp->next; |
| 4449 | } |
| 4450 | if (occur != 0) |
| 4451 | occur = 1; |
| 4452 | } else |
| 4453 | occur++; |
| 4454 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4455 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4456 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4457 | name = (const char *) (((xmlAttrPtr) cur)->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4458 | if (cur->ns) { |
| 4459 | if (cur->ns->prefix != NULL) |
| 4460 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
| 4461 | (char *)cur->ns->prefix, (char *)cur->name); |
| 4462 | else |
| 4463 | snprintf(nametemp, sizeof(nametemp) - 1, "%s", |
| 4464 | (char *)cur->name); |
| 4465 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4466 | name = nametemp; |
| 4467 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4468 | next = ((xmlAttrPtr) cur)->parent; |
| 4469 | } else { |
| 4470 | next = cur->parent; |
| 4471 | } |
| 4472 | |
| 4473 | /* |
| 4474 | * Make sure there is enough room |
| 4475 | */ |
| 4476 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4477 | buf_len = |
| 4478 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4479 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4480 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4481 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4482 | xmlFree(buf); |
| 4483 | xmlFree(buffer); |
| 4484 | return (NULL); |
| 4485 | } |
| 4486 | buffer = temp; |
| 4487 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4488 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4489 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4490 | xmlFree(buf); |
| 4491 | xmlFree(buffer); |
| 4492 | return (NULL); |
| 4493 | } |
| 4494 | buf = temp; |
| 4495 | } |
| 4496 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4497 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4498 | sep, name, (char *) buffer); |
| 4499 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4500 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4501 | sep, name, occur, (char *) buffer); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4502 | snprintf((char *) buffer, buf_len, "%s", (char *)buf); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4503 | cur = next; |
| 4504 | } while (cur != NULL); |
| 4505 | xmlFree(buf); |
| 4506 | return (buffer); |
| 4507 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4508 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4509 | |
| 4510 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4511 | * xmlDocGetRootElement: |
| 4512 | * @doc: the document |
| 4513 | * |
| 4514 | * Get the root element of the document (doc->children is a list |
| 4515 | * containing possibly comments, PIs, etc ...). |
| 4516 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4517 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4518 | */ |
| 4519 | xmlNodePtr |
| 4520 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 4521 | xmlNodePtr ret; |
| 4522 | |
| 4523 | if (doc == NULL) return(NULL); |
| 4524 | ret = doc->children; |
| 4525 | while (ret != NULL) { |
| 4526 | if (ret->type == XML_ELEMENT_NODE) |
| 4527 | return(ret); |
| 4528 | ret = ret->next; |
| 4529 | } |
| 4530 | return(ret); |
| 4531 | } |
| 4532 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4533 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4534 | /** |
| 4535 | * xmlDocSetRootElement: |
| 4536 | * @doc: the document |
| 4537 | * @root: the new document root element |
| 4538 | * |
| 4539 | * Set the root element of the document (doc->children is a list |
| 4540 | * containing possibly comments, PIs, etc ...). |
| 4541 | * |
| 4542 | * Returns the old root element if any was found |
| 4543 | */ |
| 4544 | xmlNodePtr |
| 4545 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4546 | xmlNodePtr old = NULL; |
| 4547 | |
| 4548 | if (doc == NULL) return(NULL); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4549 | if (root == NULL) |
| 4550 | return(NULL); |
| 4551 | xmlUnlinkNode(root); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 4552 | xmlSetTreeDoc(root, doc); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4553 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4554 | old = doc->children; |
| 4555 | while (old != NULL) { |
| 4556 | if (old->type == XML_ELEMENT_NODE) |
| 4557 | break; |
| 4558 | old = old->next; |
| 4559 | } |
| 4560 | if (old == NULL) { |
| 4561 | if (doc->children == NULL) { |
| 4562 | doc->children = root; |
| 4563 | doc->last = root; |
| 4564 | } else { |
| 4565 | xmlAddSibling(doc->children, root); |
| 4566 | } |
| 4567 | } else { |
| 4568 | xmlReplaceNode(old, root); |
| 4569 | } |
| 4570 | return(old); |
| 4571 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4572 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4573 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4574 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4575 | /** |
| 4576 | * xmlNodeSetLang: |
| 4577 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4578 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4579 | * |
| 4580 | * Set the language of a node, i.e. the values of the xml:lang |
| 4581 | * attribute. |
| 4582 | */ |
| 4583 | void |
| 4584 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4585 | xmlNsPtr ns; |
| 4586 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4587 | if (cur == NULL) return; |
| 4588 | switch(cur->type) { |
| 4589 | case XML_TEXT_NODE: |
| 4590 | case XML_CDATA_SECTION_NODE: |
| 4591 | case XML_COMMENT_NODE: |
| 4592 | case XML_DOCUMENT_NODE: |
| 4593 | case XML_DOCUMENT_TYPE_NODE: |
| 4594 | case XML_DOCUMENT_FRAG_NODE: |
| 4595 | case XML_NOTATION_NODE: |
| 4596 | case XML_HTML_DOCUMENT_NODE: |
| 4597 | case XML_DTD_NODE: |
| 4598 | case XML_ELEMENT_DECL: |
| 4599 | case XML_ATTRIBUTE_DECL: |
| 4600 | case XML_ENTITY_DECL: |
| 4601 | case XML_PI_NODE: |
| 4602 | case XML_ENTITY_REF_NODE: |
| 4603 | case XML_ENTITY_NODE: |
| 4604 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4605 | #ifdef LIBXML_DOCB_ENABLED |
| 4606 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4607 | #endif |
| 4608 | case XML_XINCLUDE_START: |
| 4609 | case XML_XINCLUDE_END: |
| 4610 | return; |
| 4611 | case XML_ELEMENT_NODE: |
| 4612 | case XML_ATTRIBUTE_NODE: |
| 4613 | break; |
| 4614 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4615 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4616 | if (ns == NULL) |
| 4617 | return; |
| 4618 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4619 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4620 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4621 | |
| 4622 | /** |
| 4623 | * xmlNodeGetLang: |
| 4624 | * @cur: the node being checked |
| 4625 | * |
| 4626 | * Searches the language of a node, i.e. the values of the xml:lang |
| 4627 | * attribute or the one carried by the nearest ancestor. |
| 4628 | * |
| 4629 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4630 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4631 | */ |
| 4632 | xmlChar * |
| 4633 | xmlNodeGetLang(xmlNodePtr cur) { |
| 4634 | xmlChar *lang; |
| 4635 | |
| 4636 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 4637 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4638 | if (lang != NULL) |
| 4639 | return(lang); |
| 4640 | cur = cur->parent; |
| 4641 | } |
| 4642 | return(NULL); |
| 4643 | } |
| 4644 | |
| 4645 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4646 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4647 | /** |
| 4648 | * xmlNodeSetSpacePreserve: |
| 4649 | * @cur: the node being changed |
| 4650 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 4651 | * |
| 4652 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 4653 | * value of the xml:space attribute. |
| 4654 | */ |
| 4655 | void |
| 4656 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4657 | xmlNsPtr ns; |
| 4658 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4659 | if (cur == NULL) return; |
| 4660 | switch(cur->type) { |
| 4661 | case XML_TEXT_NODE: |
| 4662 | case XML_CDATA_SECTION_NODE: |
| 4663 | case XML_COMMENT_NODE: |
| 4664 | case XML_DOCUMENT_NODE: |
| 4665 | case XML_DOCUMENT_TYPE_NODE: |
| 4666 | case XML_DOCUMENT_FRAG_NODE: |
| 4667 | case XML_NOTATION_NODE: |
| 4668 | case XML_HTML_DOCUMENT_NODE: |
| 4669 | case XML_DTD_NODE: |
| 4670 | case XML_ELEMENT_DECL: |
| 4671 | case XML_ATTRIBUTE_DECL: |
| 4672 | case XML_ENTITY_DECL: |
| 4673 | case XML_PI_NODE: |
| 4674 | case XML_ENTITY_REF_NODE: |
| 4675 | case XML_ENTITY_NODE: |
| 4676 | case XML_NAMESPACE_DECL: |
| 4677 | case XML_XINCLUDE_START: |
| 4678 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4679 | #ifdef LIBXML_DOCB_ENABLED |
| 4680 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4681 | #endif |
| 4682 | return; |
| 4683 | case XML_ELEMENT_NODE: |
| 4684 | case XML_ATTRIBUTE_NODE: |
| 4685 | break; |
| 4686 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4687 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4688 | if (ns == NULL) |
| 4689 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4690 | switch (val) { |
| 4691 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4692 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4693 | break; |
| 4694 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4695 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4696 | break; |
| 4697 | } |
| 4698 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4699 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4700 | |
| 4701 | /** |
| 4702 | * xmlNodeGetSpacePreserve: |
| 4703 | * @cur: the node being checked |
| 4704 | * |
| 4705 | * Searches the space preserving behaviour of a node, i.e. the values |
| 4706 | * of the xml:space attribute or the one carried by the nearest |
| 4707 | * ancestor. |
| 4708 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4709 | * 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] | 4710 | */ |
| 4711 | int |
| 4712 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 4713 | xmlChar *space; |
| 4714 | |
| 4715 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4716 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4717 | if (space != NULL) { |
| 4718 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 4719 | xmlFree(space); |
| 4720 | return(1); |
| 4721 | } |
| 4722 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 4723 | xmlFree(space); |
| 4724 | return(0); |
| 4725 | } |
| 4726 | xmlFree(space); |
| 4727 | } |
| 4728 | cur = cur->parent; |
| 4729 | } |
| 4730 | return(-1); |
| 4731 | } |
| 4732 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4733 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4734 | /** |
| 4735 | * xmlNodeSetName: |
| 4736 | * @cur: the node being changed |
| 4737 | * @name: the new tag name |
| 4738 | * |
| 4739 | * Set (or reset) the name of a node. |
| 4740 | */ |
| 4741 | void |
| 4742 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 4743 | xmlDocPtr doc; |
| 4744 | xmlDictPtr dict; |
| 4745 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4746 | if (cur == NULL) return; |
| 4747 | if (name == NULL) return; |
| 4748 | switch(cur->type) { |
| 4749 | case XML_TEXT_NODE: |
| 4750 | case XML_CDATA_SECTION_NODE: |
| 4751 | case XML_COMMENT_NODE: |
| 4752 | case XML_DOCUMENT_TYPE_NODE: |
| 4753 | case XML_DOCUMENT_FRAG_NODE: |
| 4754 | case XML_NOTATION_NODE: |
| 4755 | case XML_HTML_DOCUMENT_NODE: |
| 4756 | case XML_NAMESPACE_DECL: |
| 4757 | case XML_XINCLUDE_START: |
| 4758 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4759 | #ifdef LIBXML_DOCB_ENABLED |
| 4760 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4761 | #endif |
| 4762 | return; |
| 4763 | case XML_ELEMENT_NODE: |
| 4764 | case XML_ATTRIBUTE_NODE: |
| 4765 | case XML_PI_NODE: |
| 4766 | case XML_ENTITY_REF_NODE: |
| 4767 | case XML_ENTITY_NODE: |
| 4768 | case XML_DTD_NODE: |
| 4769 | case XML_DOCUMENT_NODE: |
| 4770 | case XML_ELEMENT_DECL: |
| 4771 | case XML_ATTRIBUTE_DECL: |
| 4772 | case XML_ENTITY_DECL: |
| 4773 | break; |
| 4774 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 4775 | doc = cur->doc; |
| 4776 | if (doc != NULL) |
| 4777 | dict = doc->dict; |
| 4778 | else |
| 4779 | dict = NULL; |
| 4780 | if (dict != NULL) { |
| 4781 | if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name))) |
| 4782 | xmlFree((xmlChar *) cur->name); |
| 4783 | cur->name = xmlDictLookup(dict, name, -1); |
| 4784 | } else { |
| 4785 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 4786 | cur->name = xmlStrdup(name); |
| 4787 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4788 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4789 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4790 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4791 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4792 | /** |
| 4793 | * xmlNodeSetBase: |
| 4794 | * @cur: the node being changed |
| 4795 | * @uri: the new base URI |
| 4796 | * |
| 4797 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 4798 | * xml:base attribute. |
| 4799 | */ |
| 4800 | void |
Daniel Veillard | f85ce8e | 2003-09-22 10:24:45 +0000 | [diff] [blame] | 4801 | xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4802 | xmlNsPtr ns; |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 4803 | const xmlChar* fixed; |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4804 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4805 | if (cur == NULL) return; |
| 4806 | switch(cur->type) { |
| 4807 | case XML_TEXT_NODE: |
| 4808 | case XML_CDATA_SECTION_NODE: |
| 4809 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4810 | case XML_DOCUMENT_TYPE_NODE: |
| 4811 | case XML_DOCUMENT_FRAG_NODE: |
| 4812 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4813 | case XML_DTD_NODE: |
| 4814 | case XML_ELEMENT_DECL: |
| 4815 | case XML_ATTRIBUTE_DECL: |
| 4816 | case XML_ENTITY_DECL: |
| 4817 | case XML_PI_NODE: |
| 4818 | case XML_ENTITY_REF_NODE: |
| 4819 | case XML_ENTITY_NODE: |
| 4820 | case XML_NAMESPACE_DECL: |
| 4821 | case XML_XINCLUDE_START: |
| 4822 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4823 | return; |
| 4824 | case XML_ELEMENT_NODE: |
| 4825 | case XML_ATTRIBUTE_NODE: |
| 4826 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 4827 | case XML_DOCUMENT_NODE: |
| 4828 | #ifdef LIBXML_DOCB_ENABLED |
| 4829 | case XML_DOCB_DOCUMENT_NODE: |
| 4830 | #endif |
| 4831 | case XML_HTML_DOCUMENT_NODE: { |
| 4832 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 4833 | |
| 4834 | if (doc->URL != NULL) |
| 4835 | xmlFree((xmlChar *) doc->URL); |
| 4836 | if (uri == NULL) |
| 4837 | doc->URL = NULL; |
| 4838 | else |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 4839 | doc->URL = xmlPathToURI(uri); |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 4840 | return; |
| 4841 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4842 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4843 | |
| 4844 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4845 | if (ns == NULL) |
| 4846 | return; |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 4847 | fixed = xmlPathToURI(uri); |
| 4848 | if (fixed != NULL) { |
| 4849 | xmlSetNsProp(cur, ns, BAD_CAST "base", fixed); |
| 4850 | xmlFree(fixed); |
| 4851 | } else { |
| 4852 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
| 4853 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4854 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4855 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4856 | |
| 4857 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4858 | * xmlNodeGetBase: |
| 4859 | * @doc: the document the node pertains to |
| 4860 | * @cur: the node being checked |
| 4861 | * |
| 4862 | * Searches for the BASE URL. The code should work on both XML |
| 4863 | * and HTML document even if base mechanisms are completely different. |
| 4864 | * It returns the base as defined in RFC 2396 sections |
| 4865 | * 5.1.1. Base URI within Document Content |
| 4866 | * and |
| 4867 | * 5.1.2. Base URI from the Encapsulating Entity |
| 4868 | * However it does not return the document base (5.1.3), use |
| 4869 | * xmlDocumentGetBase() for this |
| 4870 | * |
| 4871 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4872 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4873 | */ |
| 4874 | xmlChar * |
| 4875 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4876 | xmlChar *oldbase = NULL; |
| 4877 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4878 | |
| 4879 | if ((cur == NULL) && (doc == NULL)) |
| 4880 | return(NULL); |
| 4881 | if (doc == NULL) doc = cur->doc; |
| 4882 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 4883 | cur = doc->children; |
| 4884 | while ((cur != NULL) && (cur->name != NULL)) { |
| 4885 | if (cur->type != XML_ELEMENT_NODE) { |
| 4886 | cur = cur->next; |
| 4887 | continue; |
| 4888 | } |
| 4889 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 4890 | cur = cur->children; |
| 4891 | continue; |
| 4892 | } |
| 4893 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 4894 | cur = cur->children; |
| 4895 | continue; |
| 4896 | } |
| 4897 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 4898 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 4899 | } |
| 4900 | cur = cur->next; |
| 4901 | } |
| 4902 | return(NULL); |
| 4903 | } |
| 4904 | while (cur != NULL) { |
| 4905 | if (cur->type == XML_ENTITY_DECL) { |
| 4906 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 4907 | return(xmlStrdup(ent->URI)); |
| 4908 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4909 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4910 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4911 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4912 | if (oldbase != NULL) { |
| 4913 | newbase = xmlBuildURI(oldbase, base); |
| 4914 | if (newbase != NULL) { |
| 4915 | xmlFree(oldbase); |
| 4916 | xmlFree(base); |
| 4917 | oldbase = newbase; |
| 4918 | } else { |
| 4919 | xmlFree(oldbase); |
| 4920 | xmlFree(base); |
| 4921 | return(NULL); |
| 4922 | } |
| 4923 | } else { |
| 4924 | oldbase = base; |
| 4925 | } |
| 4926 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 4927 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 4928 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 4929 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4930 | } |
| 4931 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4932 | cur = cur->parent; |
| 4933 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4934 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 4935 | if (oldbase == NULL) |
| 4936 | return(xmlStrdup(doc->URL)); |
| 4937 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 4938 | xmlFree(oldbase); |
| 4939 | return(newbase); |
| 4940 | } |
| 4941 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | /** |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 4945 | * xmlNodeBufGetContent: |
| 4946 | * @buffer: a buffer |
| 4947 | * @cur: the node being read |
| 4948 | * |
| 4949 | * Read the value of a node @cur, this can be either the text carried |
| 4950 | * directly by this node if it's a TEXT node or the aggregate string |
| 4951 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 4952 | * Entity references are substituted. |
| 4953 | * Fills up the buffer @buffer with this value |
| 4954 | * |
| 4955 | * Returns 0 in case of success and -1 in case of error. |
| 4956 | */ |
| 4957 | int |
| 4958 | xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur) |
| 4959 | { |
| 4960 | if ((cur == NULL) || (buffer == NULL)) return(-1); |
| 4961 | switch (cur->type) { |
| 4962 | case XML_CDATA_SECTION_NODE: |
| 4963 | case XML_TEXT_NODE: |
| 4964 | xmlBufferCat(buffer, cur->content); |
| 4965 | break; |
| 4966 | case XML_DOCUMENT_FRAG_NODE: |
| 4967 | case XML_ELEMENT_NODE:{ |
| 4968 | xmlNodePtr tmp = cur; |
| 4969 | |
| 4970 | while (tmp != NULL) { |
| 4971 | switch (tmp->type) { |
| 4972 | case XML_CDATA_SECTION_NODE: |
| 4973 | case XML_TEXT_NODE: |
| 4974 | if (tmp->content != NULL) |
| 4975 | xmlBufferCat(buffer, tmp->content); |
| 4976 | break; |
| 4977 | case XML_ENTITY_REF_NODE: |
Rob Richards | 77b92ff | 2005-12-20 15:55:14 +0000 | [diff] [blame] | 4978 | xmlNodeBufGetContent(buffer, tmp); |
| 4979 | break; |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 4980 | default: |
| 4981 | break; |
| 4982 | } |
| 4983 | /* |
| 4984 | * Skip to next node |
| 4985 | */ |
| 4986 | if (tmp->children != NULL) { |
| 4987 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4988 | tmp = tmp->children; |
| 4989 | continue; |
| 4990 | } |
| 4991 | } |
| 4992 | if (tmp == cur) |
| 4993 | break; |
| 4994 | |
| 4995 | if (tmp->next != NULL) { |
| 4996 | tmp = tmp->next; |
| 4997 | continue; |
| 4998 | } |
| 4999 | |
| 5000 | do { |
| 5001 | tmp = tmp->parent; |
| 5002 | if (tmp == NULL) |
| 5003 | break; |
| 5004 | if (tmp == cur) { |
| 5005 | tmp = NULL; |
| 5006 | break; |
| 5007 | } |
| 5008 | if (tmp->next != NULL) { |
| 5009 | tmp = tmp->next; |
| 5010 | break; |
| 5011 | } |
| 5012 | } while (tmp != NULL); |
| 5013 | } |
| 5014 | break; |
| 5015 | } |
| 5016 | case XML_ATTRIBUTE_NODE:{ |
| 5017 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 5018 | xmlNodePtr tmp = attr->children; |
| 5019 | |
| 5020 | while (tmp != NULL) { |
| 5021 | if (tmp->type == XML_TEXT_NODE) |
| 5022 | xmlBufferCat(buffer, tmp->content); |
| 5023 | else |
| 5024 | xmlNodeBufGetContent(buffer, tmp); |
| 5025 | tmp = tmp->next; |
| 5026 | } |
| 5027 | break; |
| 5028 | } |
| 5029 | case XML_COMMENT_NODE: |
| 5030 | case XML_PI_NODE: |
| 5031 | xmlBufferCat(buffer, cur->content); |
| 5032 | break; |
| 5033 | case XML_ENTITY_REF_NODE:{ |
| 5034 | xmlEntityPtr ent; |
| 5035 | xmlNodePtr tmp; |
| 5036 | |
| 5037 | /* lookup entity declaration */ |
| 5038 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5039 | if (ent == NULL) |
| 5040 | return(-1); |
| 5041 | |
| 5042 | /* an entity content can be any "well balanced chunk", |
| 5043 | * i.e. the result of the content [43] production: |
| 5044 | * http://www.w3.org/TR/REC-xml#NT-content |
| 5045 | * -> we iterate through child nodes and recursive call |
| 5046 | * xmlNodeGetContent() which handles all possible node types */ |
| 5047 | tmp = ent->children; |
| 5048 | while (tmp) { |
| 5049 | xmlNodeBufGetContent(buffer, tmp); |
| 5050 | tmp = tmp->next; |
| 5051 | } |
| 5052 | break; |
| 5053 | } |
| 5054 | case XML_ENTITY_NODE: |
| 5055 | case XML_DOCUMENT_TYPE_NODE: |
| 5056 | case XML_NOTATION_NODE: |
| 5057 | case XML_DTD_NODE: |
| 5058 | case XML_XINCLUDE_START: |
| 5059 | case XML_XINCLUDE_END: |
| 5060 | break; |
| 5061 | case XML_DOCUMENT_NODE: |
| 5062 | #ifdef LIBXML_DOCB_ENABLED |
| 5063 | case XML_DOCB_DOCUMENT_NODE: |
| 5064 | #endif |
| 5065 | case XML_HTML_DOCUMENT_NODE: |
| 5066 | cur = cur->children; |
| 5067 | while (cur!= NULL) { |
| 5068 | if ((cur->type == XML_ELEMENT_NODE) || |
| 5069 | (cur->type == XML_TEXT_NODE) || |
| 5070 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 5071 | xmlNodeBufGetContent(buffer, cur); |
| 5072 | } |
| 5073 | cur = cur->next; |
| 5074 | } |
| 5075 | break; |
| 5076 | case XML_NAMESPACE_DECL: |
| 5077 | xmlBufferCat(buffer, ((xmlNsPtr) cur)->href); |
| 5078 | break; |
| 5079 | case XML_ELEMENT_DECL: |
| 5080 | case XML_ATTRIBUTE_DECL: |
| 5081 | case XML_ENTITY_DECL: |
| 5082 | break; |
| 5083 | } |
| 5084 | return(0); |
| 5085 | } |
| 5086 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5087 | * xmlNodeGetContent: |
| 5088 | * @cur: the node being read |
| 5089 | * |
| 5090 | * Read the value of a node, this can be either the text carried |
| 5091 | * directly by this node if it's a TEXT node or the aggregate string |
| 5092 | * 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] | 5093 | * Entity references are substituted. |
| 5094 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5095 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5096 | */ |
| 5097 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5098 | xmlNodeGetContent(xmlNodePtr cur) |
| 5099 | { |
| 5100 | if (cur == NULL) |
| 5101 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5102 | switch (cur->type) { |
| 5103 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5104 | case XML_ELEMENT_NODE:{ |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5105 | xmlBufferPtr buffer; |
| 5106 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5107 | |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 5108 | buffer = xmlBufferCreateSize(64); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5109 | if (buffer == NULL) |
| 5110 | return (NULL); |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5111 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5112 | ret = buffer->content; |
| 5113 | buffer->content = NULL; |
| 5114 | xmlBufferFree(buffer); |
| 5115 | return (ret); |
| 5116 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5117 | case XML_ATTRIBUTE_NODE: |
| 5118 | return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5119 | case XML_COMMENT_NODE: |
| 5120 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5121 | if (cur->content != NULL) |
| 5122 | return (xmlStrdup(cur->content)); |
| 5123 | return (NULL); |
| 5124 | case XML_ENTITY_REF_NODE:{ |
| 5125 | xmlEntityPtr ent; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5126 | xmlBufferPtr buffer; |
| 5127 | xmlChar *ret; |
| 5128 | |
| 5129 | /* lookup entity declaration */ |
| 5130 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5131 | if (ent == NULL) |
| 5132 | return (NULL); |
| 5133 | |
| 5134 | buffer = xmlBufferCreate(); |
| 5135 | if (buffer == NULL) |
| 5136 | return (NULL); |
| 5137 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5138 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5139 | |
| 5140 | ret = buffer->content; |
| 5141 | buffer->content = NULL; |
| 5142 | xmlBufferFree(buffer); |
| 5143 | return (ret); |
| 5144 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5145 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5146 | case XML_DOCUMENT_TYPE_NODE: |
| 5147 | case XML_NOTATION_NODE: |
| 5148 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5149 | case XML_XINCLUDE_START: |
| 5150 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5151 | return (NULL); |
| 5152 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5153 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5154 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5155 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5156 | case XML_HTML_DOCUMENT_NODE: { |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5157 | xmlBufferPtr buffer; |
| 5158 | xmlChar *ret; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5159 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5160 | buffer = xmlBufferCreate(); |
| 5161 | if (buffer == NULL) |
| 5162 | return (NULL); |
| 5163 | |
| 5164 | xmlNodeBufGetContent(buffer, (xmlNodePtr) cur); |
| 5165 | |
| 5166 | ret = buffer->content; |
| 5167 | buffer->content = NULL; |
| 5168 | xmlBufferFree(buffer); |
| 5169 | return (ret); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5170 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 5171 | case XML_NAMESPACE_DECL: { |
| 5172 | xmlChar *tmp; |
| 5173 | |
| 5174 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 5175 | return (tmp); |
| 5176 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5177 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5178 | /* TODO !!! */ |
| 5179 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5180 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5181 | /* TODO !!! */ |
| 5182 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5183 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5184 | /* TODO !!! */ |
| 5185 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5186 | case XML_CDATA_SECTION_NODE: |
| 5187 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5188 | if (cur->content != NULL) |
| 5189 | return (xmlStrdup(cur->content)); |
| 5190 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5191 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5192 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5193 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5194 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5195 | /** |
| 5196 | * xmlNodeSetContent: |
| 5197 | * @cur: the node being modified |
| 5198 | * @content: the new value of the content |
| 5199 | * |
| 5200 | * Replace the content of a node. |
| 5201 | */ |
| 5202 | void |
| 5203 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 5204 | if (cur == NULL) { |
| 5205 | #ifdef DEBUG_TREE |
| 5206 | xmlGenericError(xmlGenericErrorContext, |
| 5207 | "xmlNodeSetContent : node == NULL\n"); |
| 5208 | #endif |
| 5209 | return; |
| 5210 | } |
| 5211 | switch (cur->type) { |
| 5212 | case XML_DOCUMENT_FRAG_NODE: |
| 5213 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5214 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5215 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5216 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 5217 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5218 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5219 | case XML_TEXT_NODE: |
| 5220 | case XML_CDATA_SECTION_NODE: |
| 5221 | case XML_ENTITY_REF_NODE: |
| 5222 | case XML_ENTITY_NODE: |
| 5223 | case XML_PI_NODE: |
| 5224 | case XML_COMMENT_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5225 | if ((cur->content != NULL) && |
| 5226 | (cur->content != (xmlChar *) &(cur->properties))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5227 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
Daniel Veillard | c587bce | 2005-05-10 15:28:08 +0000 | [diff] [blame] | 5228 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5229 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5230 | } |
| 5231 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5232 | cur->last = cur->children = NULL; |
| 5233 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5234 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5235 | } else |
| 5236 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5237 | cur->properties = NULL; |
| 5238 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5239 | break; |
| 5240 | case XML_DOCUMENT_NODE: |
| 5241 | case XML_HTML_DOCUMENT_NODE: |
| 5242 | case XML_DOCUMENT_TYPE_NODE: |
| 5243 | case XML_XINCLUDE_START: |
| 5244 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5245 | #ifdef LIBXML_DOCB_ENABLED |
| 5246 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5247 | #endif |
| 5248 | break; |
| 5249 | case XML_NOTATION_NODE: |
| 5250 | break; |
| 5251 | case XML_DTD_NODE: |
| 5252 | break; |
| 5253 | case XML_NAMESPACE_DECL: |
| 5254 | break; |
| 5255 | case XML_ELEMENT_DECL: |
| 5256 | /* TODO !!! */ |
| 5257 | break; |
| 5258 | case XML_ATTRIBUTE_DECL: |
| 5259 | /* TODO !!! */ |
| 5260 | break; |
| 5261 | case XML_ENTITY_DECL: |
| 5262 | /* TODO !!! */ |
| 5263 | break; |
| 5264 | } |
| 5265 | } |
| 5266 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5267 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5268 | /** |
| 5269 | * xmlNodeSetContentLen: |
| 5270 | * @cur: the node being modified |
| 5271 | * @content: the new value of the content |
| 5272 | * @len: the size of @content |
| 5273 | * |
| 5274 | * Replace the content of a node. |
| 5275 | */ |
| 5276 | void |
| 5277 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5278 | if (cur == NULL) { |
| 5279 | #ifdef DEBUG_TREE |
| 5280 | xmlGenericError(xmlGenericErrorContext, |
| 5281 | "xmlNodeSetContentLen : node == NULL\n"); |
| 5282 | #endif |
| 5283 | return; |
| 5284 | } |
| 5285 | switch (cur->type) { |
| 5286 | case XML_DOCUMENT_FRAG_NODE: |
| 5287 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5288 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5289 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5290 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 5291 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5292 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5293 | case XML_TEXT_NODE: |
| 5294 | case XML_CDATA_SECTION_NODE: |
| 5295 | case XML_ENTITY_REF_NODE: |
| 5296 | case XML_ENTITY_NODE: |
| 5297 | case XML_PI_NODE: |
| 5298 | case XML_COMMENT_NODE: |
| 5299 | case XML_NOTATION_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5300 | if ((cur->content != NULL) && |
| 5301 | (cur->content != (xmlChar *) &(cur->properties))) { |
| 5302 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5303 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
| 5304 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5305 | } |
| 5306 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5307 | cur->children = cur->last = NULL; |
| 5308 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5309 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5310 | } else |
| 5311 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5312 | cur->properties = NULL; |
| 5313 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5314 | break; |
| 5315 | case XML_DOCUMENT_NODE: |
| 5316 | case XML_DTD_NODE: |
| 5317 | case XML_HTML_DOCUMENT_NODE: |
| 5318 | case XML_DOCUMENT_TYPE_NODE: |
| 5319 | case XML_NAMESPACE_DECL: |
| 5320 | case XML_XINCLUDE_START: |
| 5321 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5322 | #ifdef LIBXML_DOCB_ENABLED |
| 5323 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5324 | #endif |
| 5325 | break; |
| 5326 | case XML_ELEMENT_DECL: |
| 5327 | /* TODO !!! */ |
| 5328 | break; |
| 5329 | case XML_ATTRIBUTE_DECL: |
| 5330 | /* TODO !!! */ |
| 5331 | break; |
| 5332 | case XML_ENTITY_DECL: |
| 5333 | /* TODO !!! */ |
| 5334 | break; |
| 5335 | } |
| 5336 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5337 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5338 | |
| 5339 | /** |
| 5340 | * xmlNodeAddContentLen: |
| 5341 | * @cur: the node being modified |
| 5342 | * @content: extra content |
| 5343 | * @len: the size of @content |
| 5344 | * |
| 5345 | * Append the extra substring to the node content. |
| 5346 | */ |
| 5347 | void |
| 5348 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5349 | if (cur == NULL) { |
| 5350 | #ifdef DEBUG_TREE |
| 5351 | xmlGenericError(xmlGenericErrorContext, |
| 5352 | "xmlNodeAddContentLen : node == NULL\n"); |
| 5353 | #endif |
| 5354 | return; |
| 5355 | } |
| 5356 | if (len <= 0) return; |
| 5357 | switch (cur->type) { |
| 5358 | case XML_DOCUMENT_FRAG_NODE: |
| 5359 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5360 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5361 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 5362 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5363 | newNode = xmlNewTextLen(content, len); |
| 5364 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5365 | tmp = xmlAddChild(cur, newNode); |
| 5366 | if (tmp != newNode) |
| 5367 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5368 | if ((last != NULL) && (last->next == newNode)) { |
| 5369 | xmlTextMerge(last, newNode); |
| 5370 | } |
| 5371 | } |
| 5372 | break; |
| 5373 | } |
| 5374 | case XML_ATTRIBUTE_NODE: |
| 5375 | break; |
| 5376 | case XML_TEXT_NODE: |
| 5377 | case XML_CDATA_SECTION_NODE: |
| 5378 | case XML_ENTITY_REF_NODE: |
| 5379 | case XML_ENTITY_NODE: |
| 5380 | case XML_PI_NODE: |
| 5381 | case XML_COMMENT_NODE: |
| 5382 | case XML_NOTATION_NODE: |
| 5383 | if (content != NULL) { |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5384 | if ((cur->content == (xmlChar *) &(cur->properties)) || |
| 5385 | ((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5386 | xmlDictOwns(cur->doc->dict, cur->content))) { |
| 5387 | cur->content = xmlStrncatNew(cur->content, content, len); |
| 5388 | cur->properties = NULL; |
| 5389 | cur->nsDef = NULL; |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5390 | break; |
| 5391 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5392 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5393 | } |
| 5394 | case XML_DOCUMENT_NODE: |
| 5395 | case XML_DTD_NODE: |
| 5396 | case XML_HTML_DOCUMENT_NODE: |
| 5397 | case XML_DOCUMENT_TYPE_NODE: |
| 5398 | case XML_NAMESPACE_DECL: |
| 5399 | case XML_XINCLUDE_START: |
| 5400 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5401 | #ifdef LIBXML_DOCB_ENABLED |
| 5402 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5403 | #endif |
| 5404 | break; |
| 5405 | case XML_ELEMENT_DECL: |
| 5406 | case XML_ATTRIBUTE_DECL: |
| 5407 | case XML_ENTITY_DECL: |
| 5408 | break; |
| 5409 | } |
| 5410 | } |
| 5411 | |
| 5412 | /** |
| 5413 | * xmlNodeAddContent: |
| 5414 | * @cur: the node being modified |
| 5415 | * @content: extra content |
| 5416 | * |
| 5417 | * Append the extra substring to the node content. |
| 5418 | */ |
| 5419 | void |
| 5420 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5421 | int len; |
| 5422 | |
| 5423 | if (cur == NULL) { |
| 5424 | #ifdef DEBUG_TREE |
| 5425 | xmlGenericError(xmlGenericErrorContext, |
| 5426 | "xmlNodeAddContent : node == NULL\n"); |
| 5427 | #endif |
| 5428 | return; |
| 5429 | } |
| 5430 | if (content == NULL) return; |
| 5431 | len = xmlStrlen(content); |
| 5432 | xmlNodeAddContentLen(cur, content, len); |
| 5433 | } |
| 5434 | |
| 5435 | /** |
| 5436 | * xmlTextMerge: |
| 5437 | * @first: the first text node |
| 5438 | * @second: the second text node being merged |
| 5439 | * |
| 5440 | * Merge two text nodes into one |
| 5441 | * Returns the first text node augmented |
| 5442 | */ |
| 5443 | xmlNodePtr |
| 5444 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5445 | if (first == NULL) return(second); |
| 5446 | if (second == NULL) return(first); |
| 5447 | if (first->type != XML_TEXT_NODE) return(first); |
| 5448 | if (second->type != XML_TEXT_NODE) return(first); |
| 5449 | if (second->name != first->name) |
| 5450 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5451 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5452 | xmlUnlinkNode(second); |
| 5453 | xmlFreeNode(second); |
| 5454 | return(first); |
| 5455 | } |
| 5456 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 5457 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5458 | /** |
| 5459 | * xmlGetNsList: |
| 5460 | * @doc: the document |
| 5461 | * @node: the current node |
| 5462 | * |
| 5463 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5464 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5465 | * that need to be freed by the caller or NULL if no |
| 5466 | * namespace if defined |
| 5467 | */ |
| 5468 | xmlNsPtr * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5469 | xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) |
| 5470 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5471 | xmlNsPtr cur; |
| 5472 | xmlNsPtr *ret = NULL; |
| 5473 | int nbns = 0; |
| 5474 | int maxns = 10; |
| 5475 | int i; |
| 5476 | |
| 5477 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5478 | if (node->type == XML_ELEMENT_NODE) { |
| 5479 | cur = node->nsDef; |
| 5480 | while (cur != NULL) { |
| 5481 | if (ret == NULL) { |
| 5482 | ret = |
| 5483 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5484 | sizeof(xmlNsPtr)); |
| 5485 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5486 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5487 | return (NULL); |
| 5488 | } |
| 5489 | ret[nbns] = NULL; |
| 5490 | } |
| 5491 | for (i = 0; i < nbns; i++) { |
| 5492 | if ((cur->prefix == ret[i]->prefix) || |
| 5493 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5494 | break; |
| 5495 | } |
| 5496 | if (i >= nbns) { |
| 5497 | if (nbns >= maxns) { |
| 5498 | maxns *= 2; |
| 5499 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5500 | (maxns + |
| 5501 | 1) * |
| 5502 | sizeof(xmlNsPtr)); |
| 5503 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5504 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5505 | return (NULL); |
| 5506 | } |
| 5507 | } |
| 5508 | ret[nbns++] = cur; |
| 5509 | ret[nbns] = NULL; |
| 5510 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5511 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5512 | cur = cur->next; |
| 5513 | } |
| 5514 | } |
| 5515 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5516 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5517 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5518 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5519 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5520 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5521 | /* |
| 5522 | * xmlTreeEnsureXMLDecl: |
| 5523 | * @doc: the doc |
| 5524 | * |
| 5525 | * Ensures that there is an XML namespace declaration on the doc. |
| 5526 | * |
| 5527 | * Returns the XML ns-struct or NULL on API and internal errors. |
| 5528 | */ |
| 5529 | static xmlNsPtr |
| 5530 | xmlTreeEnsureXMLDecl(xmlDocPtr doc) |
| 5531 | { |
| 5532 | if (doc == NULL) |
| 5533 | return (NULL); |
| 5534 | if (doc->oldNs != NULL) |
| 5535 | return (doc->oldNs); |
| 5536 | { |
| 5537 | xmlNsPtr ns; |
| 5538 | ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5539 | if (ns == NULL) { |
| 5540 | xmlTreeErrMemory( |
| 5541 | "allocating the XML namespace"); |
| 5542 | return (NULL); |
| 5543 | } |
| 5544 | memset(ns, 0, sizeof(xmlNs)); |
| 5545 | ns->type = XML_LOCAL_NAMESPACE; |
| 5546 | ns->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5547 | ns->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5548 | doc->oldNs = ns; |
| 5549 | return (ns); |
| 5550 | } |
| 5551 | } |
| 5552 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5553 | /** |
| 5554 | * xmlSearchNs: |
| 5555 | * @doc: the document |
| 5556 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5557 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5558 | * |
| 5559 | * Search a Ns registered under a given name space for a document. |
| 5560 | * recurse on the parents until it finds the defined namespace |
| 5561 | * or return NULL otherwise. |
| 5562 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5563 | * We don't allow to cross entities boundaries. If you don't declare |
| 5564 | * the namespace within those you will be in troubles !!! A warning |
| 5565 | * is generated to cover this case. |
| 5566 | * |
| 5567 | * Returns the namespace pointer or NULL. |
| 5568 | */ |
| 5569 | xmlNsPtr |
| 5570 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5571 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5572 | xmlNsPtr cur; |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5573 | xmlNodePtr orig = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5574 | |
| 5575 | if (node == NULL) return(NULL); |
| 5576 | if ((nameSpace != NULL) && |
| 5577 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5578 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5579 | /* |
| 5580 | * The XML-1.0 namespace is normally held on the root |
| 5581 | * element. In this case exceptionally create it on the |
| 5582 | * node element. |
| 5583 | */ |
| 5584 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5585 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5586 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5587 | return(NULL); |
| 5588 | } |
| 5589 | memset(cur, 0, sizeof(xmlNs)); |
| 5590 | cur->type = XML_LOCAL_NAMESPACE; |
| 5591 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5592 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5593 | cur->next = node->nsDef; |
| 5594 | node->nsDef = cur; |
| 5595 | return(cur); |
| 5596 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5597 | if (doc == NULL) { |
| 5598 | doc = node->doc; |
| 5599 | if (doc == NULL) |
| 5600 | return(NULL); |
| 5601 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5602 | /* |
| 5603 | * Return the XML namespace declaration held by the doc. |
| 5604 | */ |
| 5605 | if (doc->oldNs == NULL) |
| 5606 | return(xmlTreeEnsureXMLDecl(doc)); |
| 5607 | else |
| 5608 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5609 | } |
| 5610 | while (node != NULL) { |
| 5611 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5612 | (node->type == XML_ENTITY_NODE) || |
| 5613 | (node->type == XML_ENTITY_DECL)) |
| 5614 | return(NULL); |
| 5615 | if (node->type == XML_ELEMENT_NODE) { |
| 5616 | cur = node->nsDef; |
| 5617 | while (cur != NULL) { |
| 5618 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5619 | (cur->href != NULL)) |
| 5620 | return(cur); |
| 5621 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5622 | (cur->href != NULL) && |
| 5623 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5624 | return(cur); |
| 5625 | cur = cur->next; |
| 5626 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5627 | if (orig != node) { |
| 5628 | cur = node->ns; |
| 5629 | if (cur != NULL) { |
| 5630 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5631 | (cur->href != NULL)) |
| 5632 | return(cur); |
| 5633 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5634 | (cur->href != NULL) && |
| 5635 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5636 | return(cur); |
| 5637 | } |
| 5638 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5639 | } |
| 5640 | node = node->parent; |
| 5641 | } |
| 5642 | return(NULL); |
| 5643 | } |
| 5644 | |
| 5645 | /** |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5646 | * xmlNsInScope: |
| 5647 | * @doc: the document |
| 5648 | * @node: the current node |
| 5649 | * @ancestor: the ancestor carrying the namespace |
| 5650 | * @prefix: the namespace prefix |
| 5651 | * |
| 5652 | * Verify that the given namespace held on @ancestor is still in scope |
| 5653 | * on node. |
| 5654 | * |
| 5655 | * Returns 1 if true, 0 if false and -1 in case of error. |
| 5656 | */ |
| 5657 | static int |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5658 | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 5659 | xmlNodePtr ancestor, const xmlChar * prefix) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5660 | { |
| 5661 | xmlNsPtr tst; |
| 5662 | |
| 5663 | while ((node != NULL) && (node != ancestor)) { |
| 5664 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5665 | (node->type == XML_ENTITY_NODE) || |
| 5666 | (node->type == XML_ENTITY_DECL)) |
| 5667 | return (-1); |
| 5668 | if (node->type == XML_ELEMENT_NODE) { |
| 5669 | tst = node->nsDef; |
| 5670 | while (tst != NULL) { |
| 5671 | if ((tst->prefix == NULL) |
| 5672 | && (prefix == NULL)) |
| 5673 | return (0); |
| 5674 | if ((tst->prefix != NULL) |
| 5675 | && (prefix != NULL) |
| 5676 | && (xmlStrEqual(tst->prefix, prefix))) |
| 5677 | return (0); |
| 5678 | tst = tst->next; |
| 5679 | } |
| 5680 | } |
| 5681 | node = node->parent; |
| 5682 | } |
| 5683 | if (node != ancestor) |
| 5684 | return (-1); |
| 5685 | return (1); |
| 5686 | } |
| 5687 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5688 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5689 | * xmlSearchNsByHref: |
| 5690 | * @doc: the document |
| 5691 | * @node: the current node |
| 5692 | * @href: the namespace value |
| 5693 | * |
| 5694 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 5695 | * the defined namespace or return NULL otherwise. |
| 5696 | * Returns the namespace pointer or NULL. |
| 5697 | */ |
| 5698 | xmlNsPtr |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5699 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) |
| 5700 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5701 | xmlNsPtr cur; |
| 5702 | xmlNodePtr orig = node; |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5703 | int is_attr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5704 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5705 | if ((node == NULL) || (href == NULL)) |
| 5706 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5707 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5708 | /* |
| 5709 | * Only the document can hold the XML spec namespace. |
| 5710 | */ |
| 5711 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5712 | /* |
| 5713 | * The XML-1.0 namespace is normally held on the root |
| 5714 | * element. In this case exceptionally create it on the |
| 5715 | * node element. |
| 5716 | */ |
| 5717 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5718 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5719 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5720 | return (NULL); |
| 5721 | } |
| 5722 | memset(cur, 0, sizeof(xmlNs)); |
| 5723 | cur->type = XML_LOCAL_NAMESPACE; |
| 5724 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5725 | cur->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5726 | cur->next = node->nsDef; |
| 5727 | node->nsDef = cur; |
| 5728 | return (cur); |
| 5729 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5730 | if (doc == NULL) { |
| 5731 | doc = node->doc; |
| 5732 | if (doc == NULL) |
| 5733 | return(NULL); |
| 5734 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5735 | /* |
| 5736 | * Return the XML namespace declaration held by the doc. |
| 5737 | */ |
| 5738 | if (doc->oldNs == NULL) |
| 5739 | return(xmlTreeEnsureXMLDecl(doc)); |
| 5740 | else |
| 5741 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5742 | } |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5743 | is_attr = (node->type == XML_ATTRIBUTE_NODE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5744 | while (node != NULL) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5745 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5746 | (node->type == XML_ENTITY_NODE) || |
| 5747 | (node->type == XML_ENTITY_DECL)) |
| 5748 | return (NULL); |
| 5749 | if (node->type == XML_ELEMENT_NODE) { |
| 5750 | cur = node->nsDef; |
| 5751 | while (cur != NULL) { |
| 5752 | if ((cur->href != NULL) && (href != NULL) && |
| 5753 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5754 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 5755 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5756 | return (cur); |
| 5757 | } |
| 5758 | cur = cur->next; |
| 5759 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5760 | if (orig != node) { |
| 5761 | cur = node->ns; |
| 5762 | if (cur != NULL) { |
| 5763 | if ((cur->href != NULL) && (href != NULL) && |
| 5764 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5765 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 5766 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5767 | return (cur); |
| 5768 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5769 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5770 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5771 | } |
| 5772 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5773 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5774 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5775 | } |
| 5776 | |
| 5777 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5778 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5779 | * @doc: the document |
| 5780 | * @tree: a node expected to hold the new namespace |
| 5781 | * @ns: the original namespace |
| 5782 | * |
| 5783 | * This function tries to locate a namespace definition in a tree |
| 5784 | * ancestors, or create a new namespace definition node similar to |
| 5785 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 5786 | * null (default namespace) or reused within the subtree defined by |
| 5787 | * @tree or on one of its ancestors then a new prefix is generated. |
| 5788 | * Returns the (new) namespace definition or NULL in case of error |
| 5789 | */ |
| 5790 | xmlNsPtr |
| 5791 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 5792 | xmlNsPtr def; |
| 5793 | xmlChar prefix[50]; |
| 5794 | int counter = 1; |
| 5795 | |
| 5796 | if (tree == NULL) { |
| 5797 | #ifdef DEBUG_TREE |
| 5798 | xmlGenericError(xmlGenericErrorContext, |
| 5799 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 5800 | #endif |
| 5801 | return(NULL); |
| 5802 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5803 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5804 | #ifdef DEBUG_TREE |
| 5805 | xmlGenericError(xmlGenericErrorContext, |
| 5806 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 5807 | #endif |
| 5808 | return(NULL); |
| 5809 | } |
| 5810 | /* |
| 5811 | * Search an existing namespace definition inherited. |
| 5812 | */ |
| 5813 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 5814 | if (def != NULL) |
| 5815 | return(def); |
| 5816 | |
| 5817 | /* |
| 5818 | * Find a close prefix which is not already in use. |
| 5819 | * Let's strip namespace prefixes longer than 20 chars ! |
| 5820 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5821 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5822 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5823 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 5824 | snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5825 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5826 | def = xmlSearchNs(doc, tree, prefix); |
| 5827 | while (def != NULL) { |
| 5828 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5829 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5830 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5831 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 5832 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", |
| 5833 | (char *)ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5834 | def = xmlSearchNs(doc, tree, prefix); |
| 5835 | } |
| 5836 | |
| 5837 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5838 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5839 | */ |
| 5840 | def = xmlNewNs(tree, ns->href, prefix); |
| 5841 | return(def); |
| 5842 | } |
| 5843 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5844 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5845 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5846 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5847 | * @doc: the document |
| 5848 | * @tree: a node defining the subtree to reconciliate |
| 5849 | * |
| 5850 | * This function checks that all the namespaces declared within the given |
| 5851 | * tree are properly declared. This is needed for example after Copy or Cut |
| 5852 | * and then paste operations. The subtree may still hold pointers to |
| 5853 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5854 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5855 | * the new environment. If not possible the new namespaces are redeclared |
| 5856 | * on @tree at the top of the given subtree. |
| 5857 | * Returns the number of namespace declarations created or -1 in case of error. |
| 5858 | */ |
| 5859 | int |
| 5860 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 5861 | xmlNsPtr *oldNs = NULL; |
| 5862 | xmlNsPtr *newNs = NULL; |
| 5863 | int sizeCache = 0; |
| 5864 | int nbCache = 0; |
| 5865 | |
| 5866 | xmlNsPtr n; |
| 5867 | xmlNodePtr node = tree; |
| 5868 | xmlAttrPtr attr; |
| 5869 | int ret = 0, i; |
| 5870 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5871 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); |
| 5872 | if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1); |
| 5873 | if (node->doc != doc) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5874 | while (node != NULL) { |
| 5875 | /* |
| 5876 | * Reconciliate the node namespace |
| 5877 | */ |
| 5878 | if (node->ns != NULL) { |
| 5879 | /* |
| 5880 | * initialize the cache if needed |
| 5881 | */ |
| 5882 | if (sizeCache == 0) { |
| 5883 | sizeCache = 10; |
| 5884 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5885 | sizeof(xmlNsPtr)); |
| 5886 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5887 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5888 | return(-1); |
| 5889 | } |
| 5890 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5891 | sizeof(xmlNsPtr)); |
| 5892 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5893 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5894 | xmlFree(oldNs); |
| 5895 | return(-1); |
| 5896 | } |
| 5897 | } |
| 5898 | for (i = 0;i < nbCache;i++) { |
| 5899 | if (oldNs[i] == node->ns) { |
| 5900 | node->ns = newNs[i]; |
| 5901 | break; |
| 5902 | } |
| 5903 | } |
| 5904 | if (i == nbCache) { |
| 5905 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5906 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5907 | */ |
| 5908 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 5909 | if (n != NULL) { /* :-( what if else ??? */ |
| 5910 | /* |
| 5911 | * check if we need to grow the cache buffers. |
| 5912 | */ |
| 5913 | if (sizeCache <= nbCache) { |
| 5914 | sizeCache *= 2; |
| 5915 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5916 | sizeof(xmlNsPtr)); |
| 5917 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5918 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5919 | xmlFree(newNs); |
| 5920 | return(-1); |
| 5921 | } |
| 5922 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5923 | sizeof(xmlNsPtr)); |
| 5924 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5925 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5926 | xmlFree(oldNs); |
| 5927 | return(-1); |
| 5928 | } |
| 5929 | } |
| 5930 | newNs[nbCache] = n; |
| 5931 | oldNs[nbCache++] = node->ns; |
| 5932 | node->ns = n; |
| 5933 | } |
| 5934 | } |
| 5935 | } |
| 5936 | /* |
| 5937 | * now check for namespace hold by attributes on the node. |
| 5938 | */ |
| 5939 | attr = node->properties; |
| 5940 | while (attr != NULL) { |
| 5941 | if (attr->ns != NULL) { |
| 5942 | /* |
| 5943 | * initialize the cache if needed |
| 5944 | */ |
| 5945 | if (sizeCache == 0) { |
| 5946 | sizeCache = 10; |
| 5947 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5948 | sizeof(xmlNsPtr)); |
| 5949 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5950 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5951 | return(-1); |
| 5952 | } |
| 5953 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5954 | sizeof(xmlNsPtr)); |
| 5955 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5956 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5957 | xmlFree(oldNs); |
| 5958 | return(-1); |
| 5959 | } |
| 5960 | } |
| 5961 | for (i = 0;i < nbCache;i++) { |
| 5962 | if (oldNs[i] == attr->ns) { |
Daniel Veillard | ce66ce1 | 2002-10-28 19:01:59 +0000 | [diff] [blame] | 5963 | attr->ns = newNs[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5964 | break; |
| 5965 | } |
| 5966 | } |
| 5967 | if (i == nbCache) { |
| 5968 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5969 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5970 | */ |
| 5971 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 5972 | if (n != NULL) { /* :-( what if else ??? */ |
| 5973 | /* |
| 5974 | * check if we need to grow the cache buffers. |
| 5975 | */ |
| 5976 | if (sizeCache <= nbCache) { |
| 5977 | sizeCache *= 2; |
| 5978 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5979 | sizeof(xmlNsPtr)); |
| 5980 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5981 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5982 | xmlFree(newNs); |
| 5983 | return(-1); |
| 5984 | } |
| 5985 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5986 | sizeof(xmlNsPtr)); |
| 5987 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5988 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5989 | xmlFree(oldNs); |
| 5990 | return(-1); |
| 5991 | } |
| 5992 | } |
| 5993 | newNs[nbCache] = n; |
| 5994 | oldNs[nbCache++] = attr->ns; |
| 5995 | attr->ns = n; |
| 5996 | } |
| 5997 | } |
| 5998 | } |
| 5999 | attr = attr->next; |
| 6000 | } |
| 6001 | |
| 6002 | /* |
| 6003 | * Browse the full subtree, deep first |
| 6004 | */ |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 6005 | if (node->children != NULL && node->type != XML_ENTITY_REF_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6006 | /* deep first */ |
| 6007 | node = node->children; |
| 6008 | } else if ((node != tree) && (node->next != NULL)) { |
| 6009 | /* then siblings */ |
| 6010 | node = node->next; |
| 6011 | } else if (node != tree) { |
| 6012 | /* go up to parents->next if needed */ |
| 6013 | while (node != tree) { |
| 6014 | if (node->parent != NULL) |
| 6015 | node = node->parent; |
| 6016 | if ((node != tree) && (node->next != NULL)) { |
| 6017 | node = node->next; |
| 6018 | break; |
| 6019 | } |
| 6020 | if (node->parent == NULL) { |
| 6021 | node = NULL; |
| 6022 | break; |
| 6023 | } |
| 6024 | } |
| 6025 | /* exit condition */ |
| 6026 | if (node == tree) |
| 6027 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 6028 | } else |
| 6029 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6030 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6031 | if (oldNs != NULL) |
| 6032 | xmlFree(oldNs); |
| 6033 | if (newNs != NULL) |
| 6034 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6035 | return(ret); |
| 6036 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6037 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6038 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6039 | static xmlAttrPtr |
| 6040 | xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name, |
| 6041 | const xmlChar *nsName, int useDTD) |
| 6042 | { |
| 6043 | xmlAttrPtr prop; |
| 6044 | |
| 6045 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6046 | return(NULL); |
| 6047 | |
| 6048 | if (node->properties != NULL) { |
| 6049 | prop = node->properties; |
| 6050 | if (nsName == NULL) { |
| 6051 | /* |
| 6052 | * We want the attr to be in no namespace. |
| 6053 | */ |
| 6054 | do { |
| 6055 | if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) { |
| 6056 | return(prop); |
| 6057 | } |
| 6058 | prop = prop->next; |
| 6059 | } while (prop != NULL); |
| 6060 | } else { |
| 6061 | /* |
| 6062 | * We want the attr to be in the specified namespace. |
| 6063 | */ |
| 6064 | do { |
| 6065 | if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) && |
| 6066 | ((prop->ns->href == nsName) || |
| 6067 | xmlStrEqual(prop->ns->href, nsName))) |
| 6068 | { |
| 6069 | return(prop); |
| 6070 | } |
| 6071 | prop = prop->next; |
| 6072 | } while (prop != NULL); |
| 6073 | } |
| 6074 | } |
| 6075 | |
| 6076 | #ifdef LIBXML_TREE_ENABLED |
| 6077 | if (! useDTD) |
| 6078 | return(NULL); |
| 6079 | /* |
| 6080 | * Check if there is a default/fixed attribute declaration in |
| 6081 | * the internal or external subset. |
| 6082 | */ |
| 6083 | if ((node->doc != NULL) && (node->doc->intSubset != NULL)) { |
| 6084 | xmlDocPtr doc = node->doc; |
| 6085 | xmlAttributePtr attrDecl = NULL; |
| 6086 | xmlChar *elemQName, *tmpstr = NULL; |
| 6087 | |
| 6088 | /* |
| 6089 | * We need the QName of the element for the DTD-lookup. |
| 6090 | */ |
| 6091 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 6092 | tmpstr = xmlStrdup(node->ns->prefix); |
| 6093 | tmpstr = xmlStrcat(tmpstr, BAD_CAST ":"); |
| 6094 | tmpstr = xmlStrcat(tmpstr, node->name); |
| 6095 | if (tmpstr == NULL) |
| 6096 | return(NULL); |
| 6097 | elemQName = tmpstr; |
| 6098 | } else |
| 6099 | elemQName = (xmlChar *) node->name; |
| 6100 | if (nsName == NULL) { |
| 6101 | /* |
| 6102 | * The common and nice case: Attr in no namespace. |
| 6103 | */ |
| 6104 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, |
| 6105 | elemQName, name, NULL); |
| 6106 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 6107 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, |
| 6108 | elemQName, name, NULL); |
| 6109 | } |
| 6110 | } else { |
| 6111 | xmlNsPtr *nsList, *cur; |
| 6112 | |
| 6113 | /* |
| 6114 | * The ugly case: Search using the prefixes of in-scope |
| 6115 | * ns-decls corresponding to @nsName. |
| 6116 | */ |
| 6117 | nsList = xmlGetNsList(node->doc, node); |
| 6118 | if (nsList == NULL) { |
| 6119 | if (tmpstr != NULL) |
| 6120 | xmlFree(tmpstr); |
| 6121 | return(NULL); |
| 6122 | } |
| 6123 | cur = nsList; |
| 6124 | while (*cur != NULL) { |
| 6125 | if (xmlStrEqual((*cur)->href, nsName)) { |
| 6126 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName, |
| 6127 | name, (*cur)->prefix); |
| 6128 | if (attrDecl) |
| 6129 | break; |
| 6130 | if (doc->extSubset != NULL) { |
| 6131 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName, |
| 6132 | name, (*cur)->prefix); |
| 6133 | if (attrDecl) |
| 6134 | break; |
| 6135 | } |
| 6136 | } |
| 6137 | cur++; |
| 6138 | } |
| 6139 | xmlFree(nsList); |
| 6140 | } |
| 6141 | if (tmpstr != NULL) |
| 6142 | xmlFree(tmpstr); |
| 6143 | /* |
| 6144 | * Only default/fixed attrs are relevant. |
| 6145 | */ |
| 6146 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6147 | return((xmlAttrPtr) attrDecl); |
| 6148 | } |
| 6149 | #endif /* LIBXML_TREE_ENABLED */ |
| 6150 | return(NULL); |
| 6151 | } |
| 6152 | |
| 6153 | static xmlChar* |
| 6154 | xmlGetPropNodeValueInternal(xmlAttrPtr prop) |
| 6155 | { |
| 6156 | if (prop == NULL) |
| 6157 | return(NULL); |
| 6158 | if (prop->type == XML_ATTRIBUTE_NODE) { |
| 6159 | /* |
| 6160 | * Note that we return at least the empty string. |
| 6161 | * TODO: Do we really always want that? |
| 6162 | */ |
| 6163 | if (prop->children != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 6164 | if ((prop->children->next == NULL) && |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6165 | ((prop->children->type == XML_TEXT_NODE) || |
| 6166 | (prop->children->type == XML_CDATA_SECTION_NODE))) |
| 6167 | { |
| 6168 | /* |
| 6169 | * Optimization for the common case: only 1 text node. |
| 6170 | */ |
| 6171 | return(xmlStrdup(prop->children->content)); |
| 6172 | } else { |
| 6173 | xmlChar *ret; |
| 6174 | |
| 6175 | ret = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 6176 | if (ret != NULL) |
| 6177 | return(ret); |
| 6178 | } |
| 6179 | } |
| 6180 | return(xmlStrdup((xmlChar *)"")); |
| 6181 | } else if (prop->type == XML_ATTRIBUTE_DECL) { |
| 6182 | return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue)); |
| 6183 | } |
| 6184 | return(NULL); |
| 6185 | } |
| 6186 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6187 | /** |
| 6188 | * xmlHasProp: |
| 6189 | * @node: the node |
| 6190 | * @name: the attribute name |
| 6191 | * |
| 6192 | * Search an attribute associated to a node |
| 6193 | * This function also looks in DTD attribute declaration for #FIXED or |
| 6194 | * default declaration values unless DTD use has been turned off. |
| 6195 | * |
| 6196 | * Returns the attribute or the attribute declaration or NULL if |
| 6197 | * neither was found. |
| 6198 | */ |
| 6199 | xmlAttrPtr |
| 6200 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 6201 | xmlAttrPtr prop; |
| 6202 | xmlDocPtr doc; |
| 6203 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6204 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6205 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6206 | /* |
| 6207 | * Check on the properties attached to the node |
| 6208 | */ |
| 6209 | prop = node->properties; |
| 6210 | while (prop != NULL) { |
| 6211 | if (xmlStrEqual(prop->name, name)) { |
| 6212 | return(prop); |
| 6213 | } |
| 6214 | prop = prop->next; |
| 6215 | } |
| 6216 | if (!xmlCheckDTD) return(NULL); |
| 6217 | |
| 6218 | /* |
| 6219 | * Check if there is a default declaration in the internal |
| 6220 | * or external subsets |
| 6221 | */ |
| 6222 | doc = node->doc; |
| 6223 | if (doc != NULL) { |
| 6224 | xmlAttributePtr attrDecl; |
| 6225 | if (doc->intSubset != NULL) { |
| 6226 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6227 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6228 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6229 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6230 | /* return attribute declaration only if a default value is given |
| 6231 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6232 | return((xmlAttrPtr) attrDecl); |
| 6233 | } |
| 6234 | } |
| 6235 | return(NULL); |
| 6236 | } |
| 6237 | |
| 6238 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6239 | * xmlHasNsProp: |
| 6240 | * @node: the node |
| 6241 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6242 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6243 | * |
| 6244 | * Search for an attribute associated to a node |
| 6245 | * This attribute has to be anchored in the namespace specified. |
| 6246 | * This does the entity substitution. |
| 6247 | * This function looks in DTD attribute declaration for #FIXED or |
| 6248 | * default declaration values unless DTD use has been turned off. |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6249 | * Note that a namespace of NULL indicates to use the default namespace. |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6250 | * |
| 6251 | * Returns the attribute or the attribute declaration or NULL |
| 6252 | * if neither was found. |
| 6253 | */ |
| 6254 | xmlAttrPtr |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6255 | xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6256 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6257 | return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD)); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6258 | } |
| 6259 | |
| 6260 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6261 | * xmlGetProp: |
| 6262 | * @node: the node |
| 6263 | * @name: the attribute name |
| 6264 | * |
| 6265 | * Search and get the value of an attribute associated to a node |
| 6266 | * This does the entity substitution. |
| 6267 | * This function looks in DTD attribute declaration for #FIXED or |
| 6268 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 6269 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6270 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 6271 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6272 | * |
| 6273 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6274 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6275 | */ |
| 6276 | xmlChar * |
| 6277 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6278 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6279 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6280 | prop = xmlHasProp(node, name); |
| 6281 | if (prop == NULL) |
| 6282 | return(NULL); |
| 6283 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6284 | } |
| 6285 | |
| 6286 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6287 | * xmlGetNoNsProp: |
| 6288 | * @node: the node |
| 6289 | * @name: the attribute name |
| 6290 | * |
| 6291 | * Search and get the value of an attribute associated to a node |
| 6292 | * This does the entity substitution. |
| 6293 | * This function looks in DTD attribute declaration for #FIXED or |
| 6294 | * default declaration values unless DTD use has been turned off. |
| 6295 | * This function is similar to xmlGetProp except it will accept only |
| 6296 | * an attribute in no namespace. |
| 6297 | * |
| 6298 | * Returns the attribute value or NULL if not found. |
| 6299 | * It's up to the caller to free the memory with xmlFree(). |
| 6300 | */ |
| 6301 | xmlChar * |
| 6302 | xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) { |
| 6303 | xmlAttrPtr prop; |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6304 | |
| 6305 | prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD); |
| 6306 | if (prop == NULL) |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6307 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6308 | return(xmlGetPropNodeValueInternal(prop)); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6309 | } |
| 6310 | |
| 6311 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6312 | * xmlGetNsProp: |
| 6313 | * @node: the node |
| 6314 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6315 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6316 | * |
| 6317 | * Search and get the value of an attribute associated to a node |
| 6318 | * This attribute has to be anchored in the namespace specified. |
| 6319 | * This does the entity substitution. |
| 6320 | * This function looks in DTD attribute declaration for #FIXED or |
| 6321 | * default declaration values unless DTD use has been turned off. |
| 6322 | * |
| 6323 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6324 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6325 | */ |
| 6326 | xmlChar * |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6327 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6328 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6329 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6330 | prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD); |
| 6331 | if (prop == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6332 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6333 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6334 | } |
| 6335 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6336 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
| 6337 | /** |
| 6338 | * xmlUnsetProp: |
| 6339 | * @node: the node |
| 6340 | * @name: the attribute name |
| 6341 | * |
| 6342 | * Remove an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6343 | * This handles only attributes in no namespace. |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6344 | * Returns 0 if successful, -1 if not found |
| 6345 | */ |
| 6346 | int |
| 6347 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6348 | xmlAttrPtr prop; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6349 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6350 | prop = xmlGetPropNodeInternal(node, name, NULL, 0); |
| 6351 | if (prop == NULL) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6352 | return(-1); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6353 | xmlUnlinkNode((xmlNodePtr) prop); |
| 6354 | xmlFreeProp(prop); |
| 6355 | return(0); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6356 | } |
| 6357 | |
| 6358 | /** |
| 6359 | * xmlUnsetNsProp: |
| 6360 | * @node: the node |
| 6361 | * @ns: the namespace definition |
| 6362 | * @name: the attribute name |
| 6363 | * |
| 6364 | * Remove an attribute carried by a node. |
| 6365 | * Returns 0 if successful, -1 if not found |
| 6366 | */ |
| 6367 | int |
| 6368 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6369 | xmlAttrPtr prop; |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6370 | |
| 6371 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6372 | if (prop == NULL) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6373 | return(-1); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6374 | xmlUnlinkNode((xmlNodePtr) prop); |
| 6375 | xmlFreeProp(prop); |
| 6376 | return(0); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6377 | } |
| 6378 | #endif |
| 6379 | |
| 6380 | #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] | 6381 | /** |
| 6382 | * xmlSetProp: |
| 6383 | * @node: the node |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6384 | * @name: the attribute name (a QName) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6385 | * @value: the attribute value |
| 6386 | * |
| 6387 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6388 | * If @name has a prefix, then the corresponding |
| 6389 | * namespace-binding will be used, if in scope; it is an |
| 6390 | * error it there's no such ns-binding for the prefix in |
| 6391 | * scope. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6392 | * Returns the attribute pointer. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6393 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6394 | */ |
| 6395 | xmlAttrPtr |
| 6396 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6397 | int len; |
| 6398 | const xmlChar *nqname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6399 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6400 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6401 | return(NULL); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6402 | |
| 6403 | /* |
| 6404 | * handle QNames |
| 6405 | */ |
| 6406 | nqname = xmlSplitQName3(name, &len); |
| 6407 | if (nqname != NULL) { |
| 6408 | xmlNsPtr ns; |
| 6409 | xmlChar *prefix = xmlStrndup(name, len); |
| 6410 | ns = xmlSearchNs(node->doc, node, prefix); |
| 6411 | if (prefix != NULL) |
| 6412 | xmlFree(prefix); |
| 6413 | if (ns != NULL) |
| 6414 | return(xmlSetNsProp(node, ns, nqname, value)); |
| 6415 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6416 | return(xmlSetNsProp(node, NULL, name, value)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6417 | } |
| 6418 | |
| 6419 | /** |
| 6420 | * xmlSetNsProp: |
| 6421 | * @node: the node |
| 6422 | * @ns: the namespace definition |
| 6423 | * @name: the attribute name |
| 6424 | * @value: the attribute value |
| 6425 | * |
| 6426 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6427 | * The ns structure must be in scope, this is not checked |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6428 | * |
| 6429 | * Returns the attribute pointer. |
| 6430 | */ |
| 6431 | xmlAttrPtr |
| 6432 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6433 | const xmlChar *value) |
| 6434 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6435 | xmlAttrPtr prop; |
| 6436 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6437 | if (ns && (ns->href == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6438 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6439 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6440 | if (prop != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6441 | /* |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6442 | * Modify the attribute's value. |
| 6443 | */ |
| 6444 | if (prop->atype == XML_ATTRIBUTE_ID) { |
| 6445 | xmlRemoveID(node->doc, prop); |
| 6446 | prop->atype = XML_ATTRIBUTE_ID; |
| 6447 | } |
| 6448 | if (prop->children != NULL) |
| 6449 | xmlFreeNodeList(prop->children); |
| 6450 | prop->children = NULL; |
| 6451 | prop->last = NULL; |
| 6452 | prop->ns = ns; |
| 6453 | if (value != NULL) { |
| 6454 | xmlChar *buffer; |
| 6455 | xmlNodePtr tmp; |
| 6456 | |
| 6457 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6458 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6459 | prop->last = NULL; |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6460 | tmp = prop->children; |
| 6461 | while (tmp != NULL) { |
| 6462 | tmp->parent = (xmlNodePtr) prop; |
| 6463 | if (tmp->next == NULL) |
| 6464 | prop->last = tmp; |
| 6465 | tmp = tmp->next; |
| 6466 | } |
| 6467 | xmlFree(buffer); |
| 6468 | } |
| 6469 | if (prop->atype == XML_ATTRIBUTE_ID) |
| 6470 | xmlAddID(NULL, node->doc, value, prop); |
| 6471 | return(prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6472 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6473 | /* |
| 6474 | * No equal attr found; create a new one. |
| 6475 | */ |
| 6476 | return(xmlNewPropInternal(node, ns, name, value, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6477 | } |
| 6478 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6479 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6480 | |
| 6481 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6482 | * xmlNodeIsText: |
| 6483 | * @node: the node |
| 6484 | * |
| 6485 | * Is this node a Text node ? |
| 6486 | * Returns 1 yes, 0 no |
| 6487 | */ |
| 6488 | int |
| 6489 | xmlNodeIsText(xmlNodePtr node) { |
| 6490 | if (node == NULL) return(0); |
| 6491 | |
| 6492 | if (node->type == XML_TEXT_NODE) return(1); |
| 6493 | return(0); |
| 6494 | } |
| 6495 | |
| 6496 | /** |
| 6497 | * xmlIsBlankNode: |
| 6498 | * @node: the node |
| 6499 | * |
| 6500 | * Checks whether this node is an empty or whitespace only |
| 6501 | * (and possibly ignorable) text-node. |
| 6502 | * |
| 6503 | * Returns 1 yes, 0 no |
| 6504 | */ |
| 6505 | int |
| 6506 | xmlIsBlankNode(xmlNodePtr node) { |
| 6507 | const xmlChar *cur; |
| 6508 | if (node == NULL) return(0); |
| 6509 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6510 | if ((node->type != XML_TEXT_NODE) && |
| 6511 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6512 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6513 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6514 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6515 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6516 | if (!IS_BLANK_CH(*cur)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6517 | cur++; |
| 6518 | } |
| 6519 | |
| 6520 | return(1); |
| 6521 | } |
| 6522 | |
| 6523 | /** |
| 6524 | * xmlTextConcat: |
| 6525 | * @node: the node |
| 6526 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6527 | * @len: @content length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6528 | * |
| 6529 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6530 | * |
| 6531 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6532 | */ |
| 6533 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6534 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6535 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6536 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6537 | |
| 6538 | if ((node->type != XML_TEXT_NODE) && |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 6539 | (node->type != XML_CDATA_SECTION_NODE) && |
| 6540 | (node->type != XML_COMMENT_NODE) && |
| 6541 | (node->type != XML_PI_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6542 | #ifdef DEBUG_TREE |
| 6543 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6544 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6545 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6546 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6547 | } |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6548 | /* need to check if content is currently in the dictionary */ |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6549 | if ((node->content == (xmlChar *) &(node->properties)) || |
| 6550 | ((node->doc != NULL) && (node->doc->dict != NULL) && |
| 6551 | xmlDictOwns(node->doc->dict, node->content))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6552 | node->content = xmlStrncatNew(node->content, content, len); |
| 6553 | } else { |
| 6554 | node->content = xmlStrncat(node->content, content, len); |
| 6555 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6556 | node->properties = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6557 | if (node->content == NULL) |
| 6558 | return(-1); |
| 6559 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6560 | } |
| 6561 | |
| 6562 | /************************************************************************ |
| 6563 | * * |
| 6564 | * Output : to a FILE or in memory * |
| 6565 | * * |
| 6566 | ************************************************************************/ |
| 6567 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6568 | /** |
| 6569 | * xmlBufferCreate: |
| 6570 | * |
| 6571 | * routine to create an XML buffer. |
| 6572 | * returns the new structure. |
| 6573 | */ |
| 6574 | xmlBufferPtr |
| 6575 | xmlBufferCreate(void) { |
| 6576 | xmlBufferPtr ret; |
| 6577 | |
| 6578 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6579 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6580 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6581 | return(NULL); |
| 6582 | } |
| 6583 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 6584 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6585 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6586 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6587 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6588 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6589 | xmlFree(ret); |
| 6590 | return(NULL); |
| 6591 | } |
| 6592 | ret->content[0] = 0; |
| 6593 | return(ret); |
| 6594 | } |
| 6595 | |
| 6596 | /** |
| 6597 | * xmlBufferCreateSize: |
| 6598 | * @size: initial size of buffer |
| 6599 | * |
| 6600 | * routine to create an XML buffer. |
| 6601 | * returns the new structure. |
| 6602 | */ |
| 6603 | xmlBufferPtr |
| 6604 | xmlBufferCreateSize(size_t size) { |
| 6605 | xmlBufferPtr ret; |
| 6606 | |
| 6607 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6608 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6609 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6610 | return(NULL); |
| 6611 | } |
| 6612 | ret->use = 0; |
| 6613 | ret->alloc = xmlBufferAllocScheme; |
| 6614 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 6615 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6616 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6617 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6618 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6619 | xmlFree(ret); |
| 6620 | return(NULL); |
| 6621 | } |
| 6622 | ret->content[0] = 0; |
| 6623 | } else |
| 6624 | ret->content = NULL; |
| 6625 | return(ret); |
| 6626 | } |
| 6627 | |
| 6628 | /** |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6629 | * xmlBufferCreateStatic: |
| 6630 | * @mem: the memory area |
| 6631 | * @size: the size in byte |
| 6632 | * |
MST 2003 John Fleck | a0e7e93 | 2003-12-19 03:13:47 +0000 | [diff] [blame] | 6633 | * routine to create an XML buffer from an immutable memory area. |
| 6634 | * 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] | 6635 | * present until the end of the buffer lifetime. |
| 6636 | * |
| 6637 | * returns the new structure. |
| 6638 | */ |
| 6639 | xmlBufferPtr |
| 6640 | xmlBufferCreateStatic(void *mem, size_t size) { |
| 6641 | xmlBufferPtr ret; |
| 6642 | |
| 6643 | if ((mem == NULL) || (size == 0)) |
| 6644 | return(NULL); |
| 6645 | |
| 6646 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6647 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6648 | xmlTreeErrMemory("creating buffer"); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6649 | return(NULL); |
| 6650 | } |
| 6651 | ret->use = size; |
| 6652 | ret->size = size; |
| 6653 | ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; |
| 6654 | ret->content = (xmlChar *) mem; |
| 6655 | return(ret); |
| 6656 | } |
| 6657 | |
| 6658 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6659 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6660 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6661 | * @scheme: allocation scheme to use |
| 6662 | * |
| 6663 | * Sets the allocation scheme for this buffer |
| 6664 | */ |
| 6665 | void |
| 6666 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 6667 | xmlBufferAllocationScheme scheme) { |
| 6668 | if (buf == NULL) { |
| 6669 | #ifdef DEBUG_BUFFER |
| 6670 | xmlGenericError(xmlGenericErrorContext, |
| 6671 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 6672 | #endif |
| 6673 | return; |
| 6674 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6675 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6676 | |
| 6677 | buf->alloc = scheme; |
| 6678 | } |
| 6679 | |
| 6680 | /** |
| 6681 | * xmlBufferFree: |
| 6682 | * @buf: the buffer to free |
| 6683 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 6684 | * Frees an XML buffer. It frees both the content and the structure which |
| 6685 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6686 | */ |
| 6687 | void |
| 6688 | xmlBufferFree(xmlBufferPtr buf) { |
| 6689 | if (buf == NULL) { |
| 6690 | #ifdef DEBUG_BUFFER |
| 6691 | xmlGenericError(xmlGenericErrorContext, |
| 6692 | "xmlBufferFree: buf == NULL\n"); |
| 6693 | #endif |
| 6694 | return; |
| 6695 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6696 | |
| 6697 | if ((buf->content != NULL) && |
| 6698 | (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6699 | xmlFree(buf->content); |
| 6700 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6701 | xmlFree(buf); |
| 6702 | } |
| 6703 | |
| 6704 | /** |
| 6705 | * xmlBufferEmpty: |
| 6706 | * @buf: the buffer |
| 6707 | * |
| 6708 | * empty a buffer. |
| 6709 | */ |
| 6710 | void |
| 6711 | xmlBufferEmpty(xmlBufferPtr buf) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6712 | if (buf == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6713 | if (buf->content == NULL) return; |
| 6714 | buf->use = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6715 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 6716 | buf->content = BAD_CAST ""; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6717 | } else { |
| 6718 | memset(buf->content, 0, buf->size); |
| 6719 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6720 | } |
| 6721 | |
| 6722 | /** |
| 6723 | * xmlBufferShrink: |
| 6724 | * @buf: the buffer to dump |
| 6725 | * @len: the number of xmlChar to remove |
| 6726 | * |
| 6727 | * Remove the beginning of an XML buffer. |
| 6728 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6729 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6730 | */ |
| 6731 | int |
| 6732 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 6733 | if (buf == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6734 | if (len == 0) return(0); |
| 6735 | if (len > buf->use) return(-1); |
| 6736 | |
| 6737 | buf->use -= len; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6738 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
| 6739 | buf->content += len; |
| 6740 | } else { |
| 6741 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
| 6742 | buf->content[buf->use] = 0; |
| 6743 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6744 | return(len); |
| 6745 | } |
| 6746 | |
| 6747 | /** |
| 6748 | * xmlBufferGrow: |
| 6749 | * @buf: the buffer |
| 6750 | * @len: the minimum free size to allocate |
| 6751 | * |
| 6752 | * Grow the available space of an XML buffer. |
| 6753 | * |
| 6754 | * Returns the new available space or -1 in case of error |
| 6755 | */ |
| 6756 | int |
| 6757 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 6758 | int size; |
| 6759 | xmlChar *newbuf; |
| 6760 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 6761 | if (buf == NULL) return(-1); |
| 6762 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6763 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6764 | if (len + buf->use < buf->size) return(0); |
| 6765 | |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 6766 | /* |
| 6767 | * Windows has a BIG problem on realloc timing, so we try to double |
| 6768 | * the buffer size (if that's enough) (bug 146697) |
| 6769 | */ |
| 6770 | #ifdef WIN32 |
| 6771 | if (buf->size > len) |
| 6772 | size = buf->size * 2; |
| 6773 | else |
| 6774 | size = buf->use + len + 100; |
| 6775 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6776 | size = buf->use + len + 100; |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 6777 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6778 | |
| 6779 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6780 | if (newbuf == NULL) { |
| 6781 | xmlTreeErrMemory("growing buffer"); |
| 6782 | return(-1); |
| 6783 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6784 | buf->content = newbuf; |
| 6785 | buf->size = size; |
| 6786 | return(buf->size - buf->use); |
| 6787 | } |
| 6788 | |
| 6789 | /** |
| 6790 | * xmlBufferDump: |
| 6791 | * @file: the file output |
| 6792 | * @buf: the buffer to dump |
| 6793 | * |
| 6794 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6795 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6796 | */ |
| 6797 | int |
| 6798 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 6799 | int ret; |
| 6800 | |
| 6801 | if (buf == NULL) { |
| 6802 | #ifdef DEBUG_BUFFER |
| 6803 | xmlGenericError(xmlGenericErrorContext, |
| 6804 | "xmlBufferDump: buf == NULL\n"); |
| 6805 | #endif |
| 6806 | return(0); |
| 6807 | } |
| 6808 | if (buf->content == NULL) { |
| 6809 | #ifdef DEBUG_BUFFER |
| 6810 | xmlGenericError(xmlGenericErrorContext, |
| 6811 | "xmlBufferDump: buf->content == NULL\n"); |
| 6812 | #endif |
| 6813 | return(0); |
| 6814 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 6815 | if (file == NULL) |
| 6816 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6817 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 6818 | return(ret); |
| 6819 | } |
| 6820 | |
| 6821 | /** |
| 6822 | * xmlBufferContent: |
| 6823 | * @buf: the buffer |
| 6824 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6825 | * Function to extract the content of a buffer |
| 6826 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6827 | * Returns the internal content |
| 6828 | */ |
| 6829 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6830 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6831 | xmlBufferContent(const xmlBufferPtr buf) |
| 6832 | { |
| 6833 | if(!buf) |
| 6834 | return NULL; |
| 6835 | |
| 6836 | return buf->content; |
| 6837 | } |
| 6838 | |
| 6839 | /** |
| 6840 | * xmlBufferLength: |
| 6841 | * @buf: the buffer |
| 6842 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6843 | * Function to get the length of a buffer |
| 6844 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6845 | * Returns the length of data in the internal content |
| 6846 | */ |
| 6847 | |
| 6848 | int |
| 6849 | xmlBufferLength(const xmlBufferPtr buf) |
| 6850 | { |
| 6851 | if(!buf) |
| 6852 | return 0; |
| 6853 | |
| 6854 | return buf->use; |
| 6855 | } |
| 6856 | |
| 6857 | /** |
| 6858 | * xmlBufferResize: |
| 6859 | * @buf: the buffer to resize |
| 6860 | * @size: the desired size |
| 6861 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6862 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6863 | * |
| 6864 | * Returns 0 in case of problems, 1 otherwise |
| 6865 | */ |
| 6866 | int |
| 6867 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 6868 | { |
| 6869 | unsigned int newSize; |
| 6870 | xmlChar* rebuf = NULL; |
| 6871 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6872 | if (buf == NULL) |
| 6873 | return(0); |
| 6874 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6875 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
| 6876 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6877 | /* Don't resize if we don't have to */ |
| 6878 | if (size < buf->size) |
| 6879 | return 1; |
| 6880 | |
| 6881 | /* figure out new size */ |
| 6882 | switch (buf->alloc){ |
| 6883 | case XML_BUFFER_ALLOC_DOUBLEIT: |
Daniel Veillard | bf62949 | 2004-04-20 22:20:59 +0000 | [diff] [blame] | 6884 | /*take care of empty case*/ |
| 6885 | newSize = (buf->size ? buf->size*2 : size + 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6886 | while (size > newSize) newSize *= 2; |
| 6887 | break; |
| 6888 | case XML_BUFFER_ALLOC_EXACT: |
| 6889 | newSize = size+10; |
| 6890 | break; |
| 6891 | default: |
| 6892 | newSize = size+10; |
| 6893 | break; |
| 6894 | } |
| 6895 | |
| 6896 | if (buf->content == NULL) |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6897 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6898 | else if (buf->size - buf->use < 100) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6899 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 6900 | newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6901 | } else { |
| 6902 | /* |
| 6903 | * if we are reallocating a buffer far from being full, it's |
| 6904 | * better to make a new allocation and copy only the used range |
| 6905 | * and free the old one. |
| 6906 | */ |
| 6907 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
| 6908 | if (rebuf != NULL) { |
| 6909 | memcpy(rebuf, buf->content, buf->use); |
| 6910 | xmlFree(buf->content); |
William M. Brack | 42331a9 | 2004-07-29 07:07:16 +0000 | [diff] [blame] | 6911 | rebuf[buf->use] = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6912 | } |
| 6913 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6914 | if (rebuf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6915 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6916 | return 0; |
| 6917 | } |
| 6918 | buf->content = rebuf; |
| 6919 | buf->size = newSize; |
| 6920 | |
| 6921 | return 1; |
| 6922 | } |
| 6923 | |
| 6924 | /** |
| 6925 | * xmlBufferAdd: |
| 6926 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6927 | * @str: the #xmlChar string |
| 6928 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6929 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6930 | * 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] | 6931 | * str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6932 | * |
| 6933 | * Returns 0 successful, a positive error code number otherwise |
| 6934 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6935 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6936 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6937 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6938 | unsigned int needSize; |
| 6939 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6940 | if ((str == NULL) || (buf == NULL)) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6941 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6942 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6943 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6944 | if (len < -1) { |
| 6945 | #ifdef DEBUG_BUFFER |
| 6946 | xmlGenericError(xmlGenericErrorContext, |
| 6947 | "xmlBufferAdd: len < 0\n"); |
| 6948 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6949 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6950 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6951 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6952 | |
| 6953 | if (len < 0) |
| 6954 | len = xmlStrlen(str); |
| 6955 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6956 | if (len <= 0) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6957 | |
| 6958 | needSize = buf->use + len + 2; |
| 6959 | if (needSize > buf->size){ |
| 6960 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6961 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6962 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6963 | } |
| 6964 | } |
| 6965 | |
| 6966 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 6967 | buf->use += len; |
| 6968 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6969 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6970 | } |
| 6971 | |
| 6972 | /** |
| 6973 | * xmlBufferAddHead: |
| 6974 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6975 | * @str: the #xmlChar string |
| 6976 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6977 | * |
| 6978 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6979 | * if len == -1, the length of @str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6980 | * |
| 6981 | * Returns 0 successful, a positive error code number otherwise |
| 6982 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6983 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6984 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6985 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6986 | unsigned int needSize; |
| 6987 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6988 | if (buf == NULL) |
| 6989 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6990 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6991 | if (str == NULL) { |
| 6992 | #ifdef DEBUG_BUFFER |
| 6993 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6994 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6995 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6996 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6997 | } |
| 6998 | if (len < -1) { |
| 6999 | #ifdef DEBUG_BUFFER |
| 7000 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7001 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7002 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7003 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7004 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7005 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7006 | |
| 7007 | if (len < 0) |
| 7008 | len = xmlStrlen(str); |
| 7009 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7010 | if (len <= 0) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7011 | |
| 7012 | needSize = buf->use + len + 2; |
| 7013 | if (needSize > buf->size){ |
| 7014 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7015 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7016 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7017 | } |
| 7018 | } |
| 7019 | |
| 7020 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 7021 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 7022 | buf->use += len; |
| 7023 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7024 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7025 | } |
| 7026 | |
| 7027 | /** |
| 7028 | * xmlBufferCat: |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7029 | * @buf: the buffer to add to |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7030 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7031 | * |
| 7032 | * Append a zero terminated string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7033 | * |
| 7034 | * Returns 0 successful, a positive error code number otherwise |
| 7035 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7036 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7037 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7038 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7039 | if (buf == NULL) |
| 7040 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7041 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
| 7042 | if (str == NULL) return -1; |
| 7043 | return xmlBufferAdd(buf, str, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7044 | } |
| 7045 | |
| 7046 | /** |
| 7047 | * xmlBufferCCat: |
| 7048 | * @buf: the buffer to dump |
| 7049 | * @str: the C char string |
| 7050 | * |
| 7051 | * Append a zero terminated C string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7052 | * |
| 7053 | * Returns 0 successful, a positive error code number otherwise |
| 7054 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7055 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7056 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7057 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 7058 | const char *cur; |
| 7059 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7060 | if (buf == NULL) |
| 7061 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7062 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7063 | if (str == NULL) { |
| 7064 | #ifdef DEBUG_BUFFER |
| 7065 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7066 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7067 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7068 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7069 | } |
| 7070 | for (cur = str;*cur != 0;cur++) { |
| 7071 | if (buf->use + 10 >= buf->size) { |
| 7072 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7073 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7074 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7075 | } |
| 7076 | } |
| 7077 | buf->content[buf->use++] = *cur; |
| 7078 | } |
| 7079 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7080 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7081 | } |
| 7082 | |
| 7083 | /** |
| 7084 | * xmlBufferWriteCHAR: |
| 7085 | * @buf: the XML buffer |
| 7086 | * @string: the string to add |
| 7087 | * |
| 7088 | * routine which manages and grows an output buffer. This one adds |
| 7089 | * xmlChars at the end of the buffer. |
| 7090 | */ |
| 7091 | void |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7092 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7093 | if (buf == NULL) |
| 7094 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7095 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7096 | xmlBufferCat(buf, string); |
| 7097 | } |
| 7098 | |
| 7099 | /** |
| 7100 | * xmlBufferWriteChar: |
| 7101 | * @buf: the XML buffer output |
| 7102 | * @string: the string to add |
| 7103 | * |
| 7104 | * routine which manage and grows an output buffer. This one add |
| 7105 | * C chars at the end of the array. |
| 7106 | */ |
| 7107 | void |
| 7108 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7109 | if (buf == NULL) |
| 7110 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7111 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7112 | xmlBufferCCat(buf, string); |
| 7113 | } |
| 7114 | |
| 7115 | |
| 7116 | /** |
| 7117 | * xmlBufferWriteQuotedString: |
| 7118 | * @buf: the XML buffer output |
| 7119 | * @string: the string to add |
| 7120 | * |
| 7121 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7122 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7123 | * quote or double-quotes internally |
| 7124 | */ |
| 7125 | void |
| 7126 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7127 | const xmlChar *cur, *base; |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7128 | if (buf == NULL) |
| 7129 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7130 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7131 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 7132 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7133 | #ifdef DEBUG_BUFFER |
| 7134 | xmlGenericError(xmlGenericErrorContext, |
| 7135 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 7136 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7137 | xmlBufferCCat(buf, "\""); |
| 7138 | base = cur = string; |
| 7139 | while(*cur != 0){ |
| 7140 | if(*cur == '"'){ |
| 7141 | if (base != cur) |
| 7142 | xmlBufferAdd(buf, base, cur - base); |
| 7143 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 7144 | cur++; |
| 7145 | base = cur; |
| 7146 | } |
| 7147 | else { |
| 7148 | cur++; |
| 7149 | } |
| 7150 | } |
| 7151 | if (base != cur) |
| 7152 | xmlBufferAdd(buf, base, cur - base); |
| 7153 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7154 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7155 | else{ |
| 7156 | xmlBufferCCat(buf, "\'"); |
| 7157 | xmlBufferCat(buf, string); |
| 7158 | xmlBufferCCat(buf, "\'"); |
| 7159 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7160 | } else { |
| 7161 | xmlBufferCCat(buf, "\""); |
| 7162 | xmlBufferCat(buf, string); |
| 7163 | xmlBufferCCat(buf, "\""); |
| 7164 | } |
| 7165 | } |
| 7166 | |
| 7167 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7168 | /** |
| 7169 | * xmlGetDocCompressMode: |
| 7170 | * @doc: the document |
| 7171 | * |
| 7172 | * get the compression ratio for a document, ZLIB based |
| 7173 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7174 | */ |
| 7175 | int |
| 7176 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 7177 | if (doc == NULL) return(-1); |
| 7178 | return(doc->compression); |
| 7179 | } |
| 7180 | |
| 7181 | /** |
| 7182 | * xmlSetDocCompressMode: |
| 7183 | * @doc: the document |
| 7184 | * @mode: the compression ratio |
| 7185 | * |
| 7186 | * set the compression ratio for a document, ZLIB based |
| 7187 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7188 | */ |
| 7189 | void |
| 7190 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 7191 | if (doc == NULL) return; |
| 7192 | if (mode < 0) doc->compression = 0; |
| 7193 | else if (mode > 9) doc->compression = 9; |
| 7194 | else doc->compression = mode; |
| 7195 | } |
| 7196 | |
| 7197 | /** |
| 7198 | * xmlGetCompressMode: |
| 7199 | * |
| 7200 | * get the default compression mode used, ZLIB based. |
| 7201 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7202 | */ |
| 7203 | int |
| 7204 | xmlGetCompressMode(void) |
| 7205 | { |
| 7206 | return (xmlCompressMode); |
| 7207 | } |
| 7208 | |
| 7209 | /** |
| 7210 | * xmlSetCompressMode: |
| 7211 | * @mode: the compression ratio |
| 7212 | * |
| 7213 | * set the default compression mode used, ZLIB based |
| 7214 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7215 | */ |
| 7216 | void |
| 7217 | xmlSetCompressMode(int mode) { |
| 7218 | if (mode < 0) xmlCompressMode = 0; |
| 7219 | else if (mode > 9) xmlCompressMode = 9; |
| 7220 | else xmlCompressMode = mode; |
| 7221 | } |
| 7222 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7223 | #define XML_TREE_NSMAP_PARENT -1 |
| 7224 | #define XML_TREE_NSMAP_XML -2 |
| 7225 | #define XML_TREE_NSMAP_DOC -3 |
| 7226 | #define XML_TREE_NSMAP_CUSTOM -4 |
| 7227 | |
| 7228 | typedef struct xmlNsMapItem *xmlNsMapItemPtr; |
| 7229 | struct xmlNsMapItem { |
| 7230 | xmlNsMapItemPtr next; |
| 7231 | xmlNsMapItemPtr prev; |
| 7232 | xmlNsPtr oldNs; /* old ns decl reference */ |
| 7233 | xmlNsPtr newNs; /* new ns decl reference */ |
| 7234 | int shadowDepth; /* Shadowed at this depth */ |
| 7235 | /* |
| 7236 | * depth: |
| 7237 | * >= 0 == @node's ns-decls |
| 7238 | * -1 == @parent's ns-decls |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7239 | * -2 == the doc->oldNs XML ns-decl |
| 7240 | * -3 == the doc->oldNs storage ns-decls |
| 7241 | * -4 == ns-decls provided via custom ns-handling |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7242 | */ |
| 7243 | int depth; |
| 7244 | }; |
| 7245 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7246 | typedef struct xmlNsMap *xmlNsMapPtr; |
| 7247 | struct xmlNsMap { |
| 7248 | xmlNsMapItemPtr first; |
| 7249 | xmlNsMapItemPtr last; |
| 7250 | xmlNsMapItemPtr pool; |
| 7251 | }; |
| 7252 | |
| 7253 | #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL)) |
| 7254 | #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next) |
| 7255 | #define XML_NSMAP_POP(m, i) \ |
| 7256 | i = (m)->last; \ |
| 7257 | (m)->last = (i)->prev; \ |
| 7258 | if ((m)->last == NULL) \ |
| 7259 | (m)->first = NULL; \ |
| 7260 | else \ |
| 7261 | (m)->last->next = NULL; \ |
| 7262 | (i)->next = (m)->pool; \ |
| 7263 | (m)->pool = i; |
| 7264 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7265 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7266 | * xmlDOMWrapNsMapFree: |
| 7267 | * @map: the ns-map |
| 7268 | * |
| 7269 | * Frees the ns-map |
| 7270 | */ |
| 7271 | static void |
| 7272 | xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap) |
| 7273 | { |
| 7274 | xmlNsMapItemPtr cur, tmp; |
| 7275 | |
| 7276 | if (nsmap == NULL) |
| 7277 | return; |
| 7278 | cur = nsmap->pool; |
| 7279 | while (cur != NULL) { |
| 7280 | tmp = cur; |
| 7281 | cur = cur->next; |
| 7282 | xmlFree(tmp); |
| 7283 | } |
| 7284 | cur = nsmap->first; |
| 7285 | while (cur != NULL) { |
| 7286 | tmp = cur; |
| 7287 | cur = cur->next; |
| 7288 | xmlFree(tmp); |
| 7289 | } |
| 7290 | xmlFree(nsmap); |
| 7291 | } |
| 7292 | |
| 7293 | /* |
| 7294 | * xmlDOMWrapNsMapAddItem: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7295 | * @map: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7296 | * @oldNs: the old ns-struct |
| 7297 | * @newNs: the new ns-struct |
| 7298 | * @depth: depth and ns-kind information |
| 7299 | * |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7300 | * Adds an ns-mapping item. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7301 | */ |
| 7302 | static xmlNsMapItemPtr |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7303 | xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7304 | xmlNsPtr oldNs, xmlNsPtr newNs, int depth) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7305 | { |
| 7306 | xmlNsMapItemPtr ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7307 | xmlNsMapPtr map; |
| 7308 | |
| 7309 | if (nsmap == NULL) |
| 7310 | return(NULL); |
| 7311 | if ((position != -1) && (position != 0)) |
| 7312 | return(NULL); |
| 7313 | map = *nsmap; |
| 7314 | |
| 7315 | if (map == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7316 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7317 | * Create the ns-map. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7318 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7319 | map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap)); |
| 7320 | if (map == NULL) { |
| 7321 | xmlTreeErrMemory("allocating namespace map"); |
| 7322 | return (NULL); |
| 7323 | } |
| 7324 | memset(map, 0, sizeof(struct xmlNsMap)); |
| 7325 | *nsmap = map; |
| 7326 | } |
| 7327 | |
| 7328 | if (map->pool != NULL) { |
| 7329 | /* |
| 7330 | * Reuse an item from the pool. |
| 7331 | */ |
| 7332 | ret = map->pool; |
| 7333 | map->pool = ret->next; |
| 7334 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7335 | } else { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7336 | /* |
| 7337 | * Create a new item. |
| 7338 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7339 | ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem)); |
| 7340 | if (ret == NULL) { |
| 7341 | xmlTreeErrMemory("allocating namespace map item"); |
| 7342 | return (NULL); |
| 7343 | } |
| 7344 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7345 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7346 | |
| 7347 | if (map->first == NULL) { |
| 7348 | /* |
| 7349 | * First ever. |
| 7350 | */ |
| 7351 | map->first = ret; |
| 7352 | map->last = ret; |
| 7353 | } else if (position == -1) { |
| 7354 | /* |
| 7355 | * Append. |
| 7356 | */ |
| 7357 | ret->prev = map->last; |
| 7358 | map->last->next = ret; |
| 7359 | map->last = ret; |
| 7360 | } else if (position == 0) { |
| 7361 | /* |
| 7362 | * Set on first position. |
| 7363 | */ |
| 7364 | map->first->prev = ret; |
| 7365 | ret->next = map->first; |
| 7366 | map->first = ret; |
| 7367 | } else |
| 7368 | return(NULL); |
| 7369 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7370 | ret->oldNs = oldNs; |
| 7371 | ret->newNs = newNs; |
| 7372 | ret->shadowDepth = -1; |
| 7373 | ret->depth = depth; |
| 7374 | return (ret); |
| 7375 | } |
| 7376 | |
| 7377 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7378 | * xmlDOMWrapStoreNs: |
| 7379 | * @doc: the doc |
| 7380 | * @nsName: the namespace name |
| 7381 | * @prefix: the prefix |
| 7382 | * |
| 7383 | * Creates or reuses an xmlNs struct on doc->oldNs with |
| 7384 | * the given prefix and namespace name. |
| 7385 | * |
| 7386 | * Returns the aquired ns struct or NULL in case of an API |
| 7387 | * or internal error. |
| 7388 | */ |
| 7389 | static xmlNsPtr |
| 7390 | xmlDOMWrapStoreNs(xmlDocPtr doc, |
| 7391 | const xmlChar *nsName, |
| 7392 | const xmlChar *prefix) |
| 7393 | { |
| 7394 | xmlNsPtr ns; |
| 7395 | |
| 7396 | if (doc == NULL) |
| 7397 | return (NULL); |
| 7398 | ns = xmlTreeEnsureXMLDecl(doc); |
| 7399 | if (ns == NULL) |
| 7400 | return (NULL); |
| 7401 | if (ns->next != NULL) { |
| 7402 | /* Reuse. */ |
| 7403 | ns = ns->next; |
| 7404 | while (ns != NULL) { |
| 7405 | if (((ns->prefix == prefix) || |
| 7406 | xmlStrEqual(ns->prefix, prefix)) && |
| 7407 | xmlStrEqual(ns->href, nsName)) { |
| 7408 | return (ns); |
| 7409 | } |
| 7410 | if (ns->next == NULL) |
| 7411 | break; |
| 7412 | ns = ns->next; |
| 7413 | } |
| 7414 | } |
| 7415 | /* Create. */ |
| 7416 | ns->next = xmlNewNs(NULL, nsName, prefix); |
| 7417 | return (ns->next); |
| 7418 | } |
| 7419 | |
| 7420 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7421 | * xmlDOMWrapNewCtxt: |
| 7422 | * |
| 7423 | * Allocates and initializes a new DOM-wrapper context. |
| 7424 | * |
| 7425 | * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror. |
| 7426 | */ |
| 7427 | xmlDOMWrapCtxtPtr |
| 7428 | xmlDOMWrapNewCtxt(void) |
| 7429 | { |
| 7430 | xmlDOMWrapCtxtPtr ret; |
| 7431 | |
| 7432 | ret = xmlMalloc(sizeof(xmlDOMWrapCtxt)); |
| 7433 | if (ret == NULL) { |
| 7434 | xmlTreeErrMemory("allocating DOM-wrapper context"); |
| 7435 | return (NULL); |
| 7436 | } |
| 7437 | memset(ret, 0, sizeof(xmlDOMWrapCtxt)); |
| 7438 | return (ret); |
| 7439 | } |
| 7440 | |
| 7441 | /* |
| 7442 | * xmlDOMWrapFreeCtxt: |
| 7443 | * @ctxt: the DOM-wrapper context |
| 7444 | * |
| 7445 | * Frees the DOM-wrapper context. |
| 7446 | */ |
| 7447 | void |
| 7448 | xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt) |
| 7449 | { |
| 7450 | if (ctxt == NULL) |
| 7451 | return; |
| 7452 | if (ctxt->namespaceMap != NULL) |
| 7453 | xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap); |
| 7454 | /* |
| 7455 | * TODO: Store the namespace map in the context. |
| 7456 | */ |
| 7457 | xmlFree(ctxt); |
| 7458 | } |
| 7459 | |
| 7460 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7461 | * xmlTreeLookupNsListByPrefix: |
| 7462 | * @nsList: a list of ns-structs |
| 7463 | * @prefix: the searched prefix |
| 7464 | * |
| 7465 | * Searches for a ns-decl with the given prefix in @nsList. |
| 7466 | * |
| 7467 | * Returns the ns-decl if found, NULL if not found and on |
| 7468 | * API errors. |
| 7469 | */ |
| 7470 | static xmlNsPtr |
| 7471 | xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix) |
| 7472 | { |
| 7473 | if (nsList == NULL) |
| 7474 | return (NULL); |
| 7475 | { |
| 7476 | xmlNsPtr ns; |
| 7477 | ns = nsList; |
| 7478 | do { |
| 7479 | if ((prefix == ns->prefix) || |
| 7480 | xmlStrEqual(prefix, ns->prefix)) { |
| 7481 | return (ns); |
| 7482 | } |
| 7483 | ns = ns->next; |
| 7484 | } while (ns != NULL); |
| 7485 | } |
| 7486 | return (NULL); |
| 7487 | } |
| 7488 | |
| 7489 | /* |
| 7490 | * |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7491 | * xmlDOMWrapNSNormGatherInScopeNs: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7492 | * @map: the namespace map |
| 7493 | * @node: the node to start with |
| 7494 | * |
| 7495 | * Puts in-scope namespaces into the ns-map. |
| 7496 | * |
| 7497 | * Returns 0 on success, -1 on API or internal errors. |
| 7498 | */ |
| 7499 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7500 | xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7501 | xmlNodePtr node) |
| 7502 | { |
| 7503 | xmlNodePtr cur; |
| 7504 | xmlNsPtr ns; |
| 7505 | xmlNsMapItemPtr mi; |
| 7506 | int shadowed; |
| 7507 | |
| 7508 | if ((map == NULL) || (*map != NULL)) |
| 7509 | return (-1); |
| 7510 | /* |
| 7511 | * Get in-scope ns-decls of @parent. |
| 7512 | */ |
| 7513 | cur = node; |
| 7514 | while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) { |
| 7515 | if (cur->type == XML_ELEMENT_NODE) { |
| 7516 | if (cur->nsDef != NULL) { |
| 7517 | ns = cur->nsDef; |
| 7518 | do { |
| 7519 | shadowed = 0; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7520 | if (XML_NSMAP_NOTEMPTY(*map)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7521 | /* |
| 7522 | * Skip shadowed prefixes. |
| 7523 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7524 | XML_NSMAP_FOREACH(*map, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7525 | if ((ns->prefix == mi->newNs->prefix) || |
| 7526 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) { |
| 7527 | shadowed = 1; |
| 7528 | break; |
| 7529 | } |
| 7530 | } |
| 7531 | } |
| 7532 | /* |
| 7533 | * Insert mapping. |
| 7534 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7535 | mi = xmlDOMWrapNsMapAddItem(map, 0, NULL, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7536 | ns, XML_TREE_NSMAP_PARENT); |
| 7537 | if (mi == NULL) |
| 7538 | return (-1); |
| 7539 | if (shadowed) |
| 7540 | mi->shadowDepth = 0; |
| 7541 | ns = ns->next; |
| 7542 | } while (ns != NULL); |
| 7543 | } |
| 7544 | } |
| 7545 | cur = cur->parent; |
| 7546 | } |
| 7547 | return (0); |
| 7548 | } |
| 7549 | |
| 7550 | /* |
| 7551 | * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict; |
| 7552 | * otherwise copy it, when it was in the source-dict. |
| 7553 | */ |
| 7554 | #define XML_TREE_ADOPT_STR(str) \ |
| 7555 | if (adoptStr && (str != NULL)) { \ |
| 7556 | if (destDoc->dict) { \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7557 | const xmlChar *old = str; \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7558 | str = xmlDictLookup(destDoc->dict, str, -1); \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7559 | if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \ |
| 7560 | (!xmlDictOwns(sourceDoc->dict, old))) \ |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 7561 | xmlFree((char *)old); \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7562 | } else if ((sourceDoc) && (sourceDoc->dict) && \ |
| 7563 | xmlDictOwns(sourceDoc->dict, str)) { \ |
| 7564 | str = BAD_CAST xmlStrdup(str); \ |
| 7565 | } \ |
| 7566 | } |
| 7567 | |
| 7568 | /* |
| 7569 | * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then |
| 7570 | * put it in dest-dict or copy it. |
| 7571 | */ |
| 7572 | #define XML_TREE_ADOPT_STR_2(str) \ |
| 7573 | if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \ |
| 7574 | (sourceDoc->dict != NULL) && \ |
| 7575 | xmlDictOwns(sourceDoc->dict, cur->content)) { \ |
| 7576 | if (destDoc->dict) \ |
| 7577 | cur->content = (xmlChar *) \ |
| 7578 | xmlDictLookup(destDoc->dict, cur->content, -1); \ |
| 7579 | else \ |
| 7580 | cur->content = xmlStrdup(BAD_CAST cur->content); \ |
| 7581 | } |
| 7582 | |
| 7583 | /* |
| 7584 | * xmlDOMWrapNSNormAddNsMapItem2: |
| 7585 | * |
| 7586 | * For internal use. Adds a ns-decl mapping. |
| 7587 | * |
| 7588 | * Returns 0 on success, -1 on internal errors. |
| 7589 | */ |
| 7590 | static int |
| 7591 | xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number, |
| 7592 | xmlNsPtr oldNs, xmlNsPtr newNs) |
| 7593 | { |
| 7594 | if (*list == NULL) { |
| 7595 | *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr)); |
| 7596 | if (*list == NULL) { |
| 7597 | xmlTreeErrMemory("alloc ns map item"); |
| 7598 | return(-1); |
| 7599 | } |
| 7600 | *size = 3; |
| 7601 | *number = 0; |
| 7602 | } else if ((*number) >= (*size)) { |
| 7603 | *size *= 2; |
| 7604 | *list = (xmlNsPtr *) xmlRealloc(*list, |
| 7605 | (*size) * 2 * sizeof(xmlNsPtr)); |
| 7606 | if (*list == NULL) { |
| 7607 | xmlTreeErrMemory("realloc ns map item"); |
| 7608 | return(-1); |
| 7609 | } |
| 7610 | } |
| 7611 | (*list)[2 * (*number)] = oldNs; |
| 7612 | (*list)[2 * (*number) +1] = newNs; |
| 7613 | (*number)++; |
| 7614 | return (0); |
| 7615 | } |
| 7616 | |
| 7617 | /* |
| 7618 | * xmlDOMWrapRemoveNode: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7619 | * @ctxt: a DOM wrapper context |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7620 | * @doc: the doc |
| 7621 | * @node: the node to be removed. |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7622 | * @options: set of options, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7623 | * |
| 7624 | * Unlinks the given node from its owner. |
| 7625 | * This will substitute ns-references to node->nsDef for |
| 7626 | * ns-references to doc->oldNs, thus ensuring the removed |
| 7627 | * branch to be autark wrt ns-references. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7628 | * |
| 7629 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7630 | * |
| 7631 | * Returns 0 on success, 1 if the node is not supported, |
| 7632 | * -1 on API and internal errors. |
| 7633 | */ |
| 7634 | int |
| 7635 | xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc, |
| 7636 | xmlNodePtr node, int options ATTRIBUTE_UNUSED) |
| 7637 | { |
| 7638 | xmlNsPtr *list = NULL; |
| 7639 | int sizeList, nbList, i, j; |
| 7640 | xmlNsPtr ns; |
| 7641 | |
| 7642 | if ((node == NULL) || (doc == NULL) || (node->doc != doc)) |
| 7643 | return (-1); |
| 7644 | |
| 7645 | /* TODO: 0 or -1 ? */ |
| 7646 | if (node->parent == NULL) |
| 7647 | return (0); |
| 7648 | |
| 7649 | switch (node->type) { |
| 7650 | case XML_TEXT_NODE: |
| 7651 | case XML_CDATA_SECTION_NODE: |
| 7652 | case XML_ENTITY_REF_NODE: |
| 7653 | case XML_PI_NODE: |
| 7654 | case XML_COMMENT_NODE: |
| 7655 | xmlUnlinkNode(node); |
| 7656 | return (0); |
| 7657 | case XML_ELEMENT_NODE: |
| 7658 | case XML_ATTRIBUTE_NODE: |
| 7659 | break; |
| 7660 | default: |
| 7661 | return (1); |
| 7662 | } |
| 7663 | xmlUnlinkNode(node); |
| 7664 | /* |
| 7665 | * Save out-of-scope ns-references in doc->oldNs. |
| 7666 | */ |
| 7667 | do { |
| 7668 | switch (node->type) { |
| 7669 | case XML_ELEMENT_NODE: |
| 7670 | if ((ctxt == NULL) && (node->nsDef != NULL)) { |
| 7671 | ns = node->nsDef; |
| 7672 | do { |
| 7673 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 7674 | &nbList, ns, ns) == -1) |
| 7675 | goto internal_error; |
| 7676 | ns = ns->next; |
| 7677 | } while (ns != NULL); |
| 7678 | } |
| 7679 | /* No break on purpose. */ |
| 7680 | case XML_ATTRIBUTE_NODE: |
| 7681 | if (node->ns != NULL) { |
| 7682 | /* |
| 7683 | * Find a mapping. |
| 7684 | */ |
| 7685 | if (list != NULL) { |
| 7686 | for (i = 0, j = 0; i < nbList; i++, j += 2) { |
| 7687 | if (node->ns == list[j]) { |
| 7688 | node->ns = list[++j]; |
| 7689 | goto next_node; |
| 7690 | } |
| 7691 | } |
| 7692 | } |
| 7693 | ns = NULL; |
| 7694 | if (ctxt != NULL) { |
| 7695 | /* |
| 7696 | * User defined. |
| 7697 | */ |
| 7698 | } else { |
| 7699 | /* |
| 7700 | * Add to doc's oldNs. |
| 7701 | */ |
| 7702 | ns = xmlDOMWrapStoreNs(doc, node->ns->href, |
| 7703 | node->ns->prefix); |
| 7704 | if (ns == NULL) |
| 7705 | goto internal_error; |
| 7706 | } |
| 7707 | if (ns != NULL) { |
| 7708 | /* |
| 7709 | * Add mapping. |
| 7710 | */ |
| 7711 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 7712 | &nbList, node->ns, ns) == -1) |
| 7713 | goto internal_error; |
| 7714 | } |
| 7715 | node->ns = ns; |
| 7716 | } |
| 7717 | if ((node->type == XML_ELEMENT_NODE) && |
| 7718 | (node->properties != NULL)) { |
| 7719 | node = (xmlNodePtr) node->properties; |
| 7720 | continue; |
| 7721 | } |
| 7722 | break; |
| 7723 | default: |
| 7724 | goto next_sibling; |
| 7725 | } |
| 7726 | next_node: |
| 7727 | if ((node->type == XML_ELEMENT_NODE) && |
| 7728 | (node->children != NULL)) { |
| 7729 | node = node->children; |
| 7730 | continue; |
| 7731 | } |
| 7732 | next_sibling: |
| 7733 | if (node == NULL) |
| 7734 | break; |
| 7735 | if (node->next != NULL) |
| 7736 | node = node->next; |
| 7737 | else { |
| 7738 | node = node->parent; |
| 7739 | goto next_sibling; |
| 7740 | } |
| 7741 | } while (node != NULL); |
| 7742 | |
| 7743 | if (list != NULL) |
| 7744 | xmlFree(list); |
| 7745 | return (0); |
| 7746 | |
| 7747 | internal_error: |
| 7748 | if (list != NULL) |
| 7749 | xmlFree(list); |
| 7750 | return (-1); |
| 7751 | } |
| 7752 | |
| 7753 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7754 | * xmlSearchNsByNamespaceStrict: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7755 | * @doc: the document |
| 7756 | * @node: the start node |
| 7757 | * @nsName: the searched namespace name |
| 7758 | * @retNs: the resulting ns-decl |
| 7759 | * @prefixed: if the found ns-decl must have a prefix (for attributes) |
| 7760 | * |
| 7761 | * Dynamically searches for a ns-declaration which matches |
| 7762 | * the given @nsName in the ancestor-or-self axis of @node. |
| 7763 | * |
| 7764 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 7765 | * and internal errors. |
| 7766 | */ |
| 7767 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7768 | xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node, |
| 7769 | const xmlChar* nsName, |
| 7770 | xmlNsPtr *retNs, int prefixed) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7771 | { |
| 7772 | xmlNodePtr cur, prev = NULL, out = NULL; |
| 7773 | xmlNsPtr ns, prevns; |
| 7774 | |
| 7775 | if ((doc == NULL) || (nsName == NULL) || (retNs == NULL)) |
| 7776 | return (-1); |
| 7777 | |
| 7778 | *retNs = NULL; |
| 7779 | if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
| 7780 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 7781 | if (*retNs == NULL) |
| 7782 | return (-1); |
| 7783 | return (1); |
| 7784 | } |
| 7785 | cur = node; |
| 7786 | do { |
| 7787 | if (cur->type == XML_ELEMENT_NODE) { |
| 7788 | if (cur->nsDef != NULL) { |
| 7789 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 7790 | if (prefixed && (ns->prefix == NULL)) |
| 7791 | continue; |
| 7792 | if (prev != NULL) { |
| 7793 | /* |
| 7794 | * Check the last level of ns-decls for a |
| 7795 | * shadowing prefix. |
| 7796 | */ |
| 7797 | prevns = prev->nsDef; |
| 7798 | do { |
| 7799 | if ((prevns->prefix == ns->prefix) || |
| 7800 | ((prevns->prefix != NULL) && |
| 7801 | (ns->prefix != NULL) && |
| 7802 | xmlStrEqual(prevns->prefix, ns->prefix))) { |
| 7803 | /* |
| 7804 | * Shadowed. |
| 7805 | */ |
| 7806 | break; |
| 7807 | } |
| 7808 | prevns = prevns->next; |
| 7809 | } while (prevns != NULL); |
| 7810 | if (prevns != NULL) |
| 7811 | continue; |
| 7812 | } |
| 7813 | /* |
| 7814 | * Ns-name comparison. |
| 7815 | */ |
| 7816 | if ((nsName == ns->href) || |
| 7817 | xmlStrEqual(nsName, ns->href)) { |
| 7818 | /* |
| 7819 | * At this point the prefix can only be shadowed, |
| 7820 | * if we are the the (at least) 3rd level of |
| 7821 | * ns-decls. |
| 7822 | */ |
| 7823 | if (out) { |
| 7824 | int ret; |
| 7825 | |
| 7826 | ret = xmlNsInScope(doc, node, prev, ns->prefix); |
| 7827 | if (ret < 0) |
| 7828 | return (-1); |
| 7829 | /* |
| 7830 | * TODO: Should we try to find a matching ns-name |
| 7831 | * only once? This here keeps on searching. |
| 7832 | * I think we should try further since, there might |
| 7833 | * be an other matching ns-decl with an unshadowed |
| 7834 | * prefix. |
| 7835 | */ |
| 7836 | if (! ret) |
| 7837 | continue; |
| 7838 | } |
| 7839 | *retNs = ns; |
| 7840 | return (1); |
| 7841 | } |
| 7842 | } |
| 7843 | out = prev; |
| 7844 | prev = cur; |
| 7845 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7846 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 7847 | (cur->type == XML_ENTITY_DECL)) |
| 7848 | return (0); |
| 7849 | cur = cur->parent; |
| 7850 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 7851 | return (0); |
| 7852 | } |
| 7853 | |
| 7854 | /* |
| 7855 | * xmlSearchNsByPrefixStrict: |
| 7856 | * @doc: the document |
| 7857 | * @node: the start node |
| 7858 | * @prefix: the searched namespace prefix |
| 7859 | * @retNs: the resulting ns-decl |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7860 | * |
| 7861 | * Dynamically searches for a ns-declaration which matches |
| 7862 | * the given @nsName in the ancestor-or-self axis of @node. |
| 7863 | * |
| 7864 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 7865 | * and internal errors. |
| 7866 | */ |
| 7867 | static int |
| 7868 | xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node, |
| 7869 | const xmlChar* prefix, |
| 7870 | xmlNsPtr *retNs) |
| 7871 | { |
| 7872 | xmlNodePtr cur; |
| 7873 | xmlNsPtr ns; |
| 7874 | |
| 7875 | if ((doc == NULL) || (node == NULL)) |
| 7876 | return (-1); |
| 7877 | |
| 7878 | if (retNs) |
| 7879 | *retNs = NULL; |
| 7880 | if (IS_STR_XML(prefix)) { |
| 7881 | if (retNs) { |
| 7882 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 7883 | if (*retNs == NULL) |
| 7884 | return (-1); |
| 7885 | } |
| 7886 | return (1); |
| 7887 | } |
| 7888 | cur = node; |
| 7889 | do { |
| 7890 | if (cur->type == XML_ELEMENT_NODE) { |
| 7891 | if (cur->nsDef != NULL) { |
| 7892 | ns = cur->nsDef; |
| 7893 | do { |
| 7894 | if ((prefix == ns->prefix) || |
| 7895 | xmlStrEqual(prefix, ns->prefix)) |
| 7896 | { |
| 7897 | /* |
| 7898 | * Disabled namespaces, e.g. xmlns:abc="". |
| 7899 | */ |
| 7900 | if (ns->href == NULL) |
| 7901 | return(0); |
| 7902 | if (retNs) |
| 7903 | *retNs = ns; |
| 7904 | return (1); |
| 7905 | } |
| 7906 | ns = ns->next; |
| 7907 | } while (ns != NULL); |
| 7908 | } |
| 7909 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 7910 | (cur->type == XML_ENTITY_DECL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7911 | return (0); |
| 7912 | cur = cur->parent; |
| 7913 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 7914 | return (0); |
| 7915 | } |
| 7916 | |
| 7917 | /* |
| 7918 | * xmlDOMWrapNSNormDeclareNsForced: |
| 7919 | * @doc: the doc |
| 7920 | * @elem: the element-node to declare on |
| 7921 | * @nsName: the namespace-name of the ns-decl |
| 7922 | * @prefix: the preferred prefix of the ns-decl |
| 7923 | * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls |
| 7924 | * |
| 7925 | * Declares a new namespace on @elem. It tries to use the |
| 7926 | * given @prefix; if a ns-decl with the given prefix is already existent |
| 7927 | * on @elem, it will generate an other prefix. |
| 7928 | * |
| 7929 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 7930 | * and internal errors. |
| 7931 | */ |
| 7932 | static xmlNsPtr |
| 7933 | xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc, |
| 7934 | xmlNodePtr elem, |
| 7935 | const xmlChar *nsName, |
| 7936 | const xmlChar *prefix, |
| 7937 | int checkShadow) |
| 7938 | { |
| 7939 | |
| 7940 | xmlNsPtr ret; |
| 7941 | char buf[50]; |
| 7942 | const xmlChar *pref; |
| 7943 | int counter = 0; |
| 7944 | /* |
| 7945 | * Create a ns-decl on @anchor. |
| 7946 | */ |
| 7947 | pref = prefix; |
| 7948 | while (1) { |
| 7949 | /* |
| 7950 | * Lookup whether the prefix is unused in elem's ns-decls. |
| 7951 | */ |
| 7952 | if ((elem->nsDef != NULL) && |
| 7953 | (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL)) |
| 7954 | goto ns_next_prefix; |
| 7955 | if (checkShadow && elem->parent && |
| 7956 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 7957 | /* |
| 7958 | * Does it shadow ancestor ns-decls? |
| 7959 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7960 | if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7961 | goto ns_next_prefix; |
| 7962 | } |
| 7963 | ret = xmlNewNs(NULL, nsName, pref); |
| 7964 | if (ret == NULL) |
| 7965 | return (NULL); |
| 7966 | if (elem->nsDef == NULL) |
| 7967 | elem->nsDef = ret; |
| 7968 | else { |
| 7969 | xmlNsPtr ns2 = elem->nsDef; |
| 7970 | while (ns2->next != NULL) |
| 7971 | ns2 = ns2->next; |
| 7972 | ns2->next = ret; |
| 7973 | } |
| 7974 | return (ret); |
| 7975 | ns_next_prefix: |
| 7976 | counter++; |
| 7977 | if (counter > 1000) |
| 7978 | return (NULL); |
| 7979 | if (prefix == NULL) { |
| 7980 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7981 | "ns_%d", counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7982 | } else |
| 7983 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7984 | "%.30s_%d", (char *)prefix, counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7985 | pref = BAD_CAST buf; |
| 7986 | } |
| 7987 | } |
| 7988 | |
| 7989 | /* |
| 7990 | * xmlDOMWrapNSNormAquireNormalizedNs: |
| 7991 | * @doc: the doc |
| 7992 | * @elem: the element-node to declare namespaces on |
| 7993 | * @ns: the ns-struct to use for the search |
| 7994 | * @retNs: the found/created ns-struct |
| 7995 | * @nsMap: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7996 | * @depth: the current tree depth |
| 7997 | * @ancestorsOnly: search in ancestor ns-decls only |
| 7998 | * @prefixed: if the searched ns-decl must have a prefix (for attributes) |
| 7999 | * |
| 8000 | * Searches for a matching ns-name in the ns-decls of @nsMap, if not |
| 8001 | * found it will either declare it on @elem, or store it in doc->oldNs. |
| 8002 | * If a new ns-decl needs to be declared on @elem, it tries to use the |
| 8003 | * @ns->prefix for it, if this prefix is already in use on @elem, it will |
| 8004 | * change the prefix or the new ns-decl. |
| 8005 | * |
| 8006 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8007 | */ |
| 8008 | static int |
| 8009 | xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc, |
| 8010 | xmlNodePtr elem, |
| 8011 | xmlNsPtr ns, |
| 8012 | xmlNsPtr *retNs, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8013 | xmlNsMapPtr *nsMap, |
| 8014 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8015 | int depth, |
| 8016 | int ancestorsOnly, |
| 8017 | int prefixed) |
| 8018 | { |
| 8019 | xmlNsMapItemPtr mi; |
| 8020 | |
| 8021 | if ((doc == NULL) || (ns == NULL) || (retNs == NULL) || |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8022 | (nsMap == NULL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8023 | return (-1); |
| 8024 | |
| 8025 | *retNs = NULL; |
| 8026 | /* |
| 8027 | * Handle XML namespace. |
| 8028 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8029 | if (IS_STR_XML(ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8030 | /* |
| 8031 | * Insert XML namespace mapping. |
| 8032 | */ |
| 8033 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8034 | if (*retNs == NULL) |
| 8035 | return (-1); |
| 8036 | return (0); |
| 8037 | } |
| 8038 | /* |
| 8039 | * If the search should be done in ancestors only and no |
| 8040 | * @elem (the first ancestor) was specified, then skip the search. |
| 8041 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8042 | if ((XML_NSMAP_NOTEMPTY(*nsMap)) && |
| 8043 | (! (ancestorsOnly && (elem == NULL)))) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8044 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8045 | /* |
| 8046 | * Try to find an equal ns-name in in-scope ns-decls. |
| 8047 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8048 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8049 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8050 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8051 | * ancestorsOnly: This should be turned on to gain speed, |
| 8052 | * if one knows that the branch itself was already |
| 8053 | * ns-wellformed and no stale references existed. |
| 8054 | * I.e. it searches in the ancestor axis only. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8055 | */ |
| 8056 | ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) && |
| 8057 | /* Skip shadowed prefixes. */ |
| 8058 | (mi->shadowDepth == -1) && |
| 8059 | /* Skip xmlns="" or xmlns:foo="". */ |
| 8060 | ((mi->newNs->href != NULL) && |
| 8061 | (mi->newNs->href[0] != 0)) && |
| 8062 | /* Ensure a prefix if wanted. */ |
| 8063 | ((! prefixed) || (mi->newNs->prefix != NULL)) && |
| 8064 | /* Equal ns name */ |
| 8065 | ((mi->newNs->href == ns->href) || |
| 8066 | xmlStrEqual(mi->newNs->href, ns->href))) { |
| 8067 | /* Set the mapping. */ |
| 8068 | mi->oldNs = ns; |
| 8069 | *retNs = mi->newNs; |
| 8070 | return (0); |
| 8071 | } |
| 8072 | } |
| 8073 | } |
| 8074 | /* |
| 8075 | * No luck, the namespace is out of scope or shadowed. |
| 8076 | */ |
| 8077 | if (elem == NULL) { |
| 8078 | xmlNsPtr tmpns; |
| 8079 | |
| 8080 | /* |
| 8081 | * Store ns-decls in "oldNs" of the document-node. |
| 8082 | */ |
| 8083 | tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix); |
| 8084 | if (tmpns == NULL) |
| 8085 | return (-1); |
| 8086 | /* |
| 8087 | * Insert mapping. |
| 8088 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8089 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8090 | tmpns, XML_TREE_NSMAP_DOC) == NULL) { |
| 8091 | xmlFreeNs(tmpns); |
| 8092 | return (-1); |
| 8093 | } |
| 8094 | *retNs = tmpns; |
| 8095 | } else { |
| 8096 | xmlNsPtr tmpns; |
| 8097 | |
| 8098 | tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href, |
| 8099 | ns->prefix, 0); |
| 8100 | if (tmpns == NULL) |
| 8101 | return (-1); |
| 8102 | |
| 8103 | if (*nsMap != NULL) { |
| 8104 | /* |
| 8105 | * Does it shadow ancestor ns-decls? |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8106 | */ |
| 8107 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8108 | if ((mi->depth < depth) && |
| 8109 | (mi->shadowDepth == -1) && |
| 8110 | ((ns->prefix == mi->newNs->prefix) || |
| 8111 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8112 | /* |
| 8113 | * Shadows. |
| 8114 | */ |
| 8115 | mi->shadowDepth = depth; |
| 8116 | break; |
| 8117 | } |
| 8118 | } |
| 8119 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8120 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8121 | xmlFreeNs(tmpns); |
| 8122 | return (-1); |
| 8123 | } |
| 8124 | *retNs = tmpns; |
| 8125 | } |
| 8126 | return (0); |
| 8127 | } |
| 8128 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8129 | typedef enum { |
| 8130 | XML_DOM_RECONNS_REMOVEREDUND = 1<<0 |
| 8131 | } xmlDOMReconcileNSOptions; |
| 8132 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8133 | /* |
| 8134 | * xmlDOMWrapReconcileNamespaces: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8135 | * @ctxt: DOM wrapper context, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8136 | * @elem: the element-node |
| 8137 | * @options: option flags |
| 8138 | * |
| 8139 | * Ensures that ns-references point to ns-decls hold on element-nodes. |
| 8140 | * Ensures that the tree is namespace wellformed by creating additional |
| 8141 | * ns-decls where needed. Note that, since prefixes of already existent |
| 8142 | * ns-decls can be shadowed by this process, it could break QNames in |
| 8143 | * attribute values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8144 | * |
| 8145 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8146 | * |
| 8147 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8148 | */ |
| 8149 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8150 | int |
| 8151 | xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8152 | xmlNodePtr elem, |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8153 | int options) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8154 | { |
| 8155 | int depth = -1, adoptns = 0, parnsdone = 0; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8156 | xmlNsPtr ns, prevns; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8157 | xmlDocPtr doc; |
| 8158 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8159 | xmlNsMapPtr nsMap = NULL; |
| 8160 | xmlNsMapItemPtr /* topmi = NULL, */ mi; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8161 | /* @ancestorsOnly should be set by an option flag. */ |
| 8162 | int ancestorsOnly = 0; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8163 | int optRemoveRedundantNS = |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8164 | ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0; |
| 8165 | xmlNsPtr *listRedund = NULL; |
| 8166 | int sizeRedund = 0, nbRedund = 0, ret, i, j; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8167 | |
| 8168 | if ((elem == NULL) || (elem->doc == NULL) || |
| 8169 | (elem->type != XML_ELEMENT_NODE)) |
| 8170 | return (-1); |
| 8171 | |
| 8172 | doc = elem->doc; |
| 8173 | cur = elem; |
| 8174 | do { |
| 8175 | switch (cur->type) { |
| 8176 | case XML_ELEMENT_NODE: |
| 8177 | adoptns = 1; |
| 8178 | curElem = cur; |
| 8179 | depth++; |
| 8180 | /* |
| 8181 | * Namespace declarations. |
| 8182 | */ |
| 8183 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8184 | prevns = NULL; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8185 | ns = cur->nsDef; |
| 8186 | while (ns != NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8187 | if (! parnsdone) { |
| 8188 | if ((elem->parent) && |
| 8189 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8190 | /* |
| 8191 | * Gather ancestor in-scope ns-decls. |
| 8192 | */ |
| 8193 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8194 | elem->parent) == -1) |
| 8195 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8196 | } |
| 8197 | parnsdone = 1; |
| 8198 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8199 | |
| 8200 | /* |
| 8201 | * Lookup the ns ancestor-axis for equal ns-decls in scope. |
| 8202 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8203 | if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8204 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8205 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8206 | (mi->shadowDepth == -1) && |
| 8207 | ((ns->prefix == mi->newNs->prefix) || |
| 8208 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) && |
| 8209 | ((ns->href == mi->newNs->href) || |
| 8210 | xmlStrEqual(ns->href, mi->newNs->href))) |
| 8211 | { |
| 8212 | /* |
| 8213 | * A redundant ns-decl was found. |
| 8214 | * Add it to the list of redundant ns-decls. |
| 8215 | */ |
| 8216 | if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund, |
| 8217 | &sizeRedund, &nbRedund, ns, mi->newNs) == -1) |
| 8218 | goto internal_error; |
| 8219 | /* |
| 8220 | * Remove the ns-decl from the element-node. |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8221 | */ |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8222 | if (prevns) |
| 8223 | prevns->next = ns->next; |
| 8224 | else |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8225 | cur->nsDef = ns->next; |
| 8226 | goto next_ns_decl; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8227 | } |
| 8228 | } |
| 8229 | } |
| 8230 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8231 | /* |
| 8232 | * Skip ns-references handling if the referenced |
| 8233 | * ns-decl is declared on the same element. |
| 8234 | */ |
| 8235 | if ((cur->ns != NULL) && adoptns && (cur->ns == ns)) |
| 8236 | adoptns = 0; |
| 8237 | /* |
| 8238 | * Does it shadow any ns-decl? |
| 8239 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8240 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
| 8241 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8242 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8243 | (mi->shadowDepth == -1) && |
| 8244 | ((ns->prefix == mi->newNs->prefix) || |
| 8245 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8246 | |
| 8247 | mi->shadowDepth = depth; |
| 8248 | } |
| 8249 | } |
| 8250 | } |
| 8251 | /* |
| 8252 | * Push mapping. |
| 8253 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8254 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8255 | depth) == NULL) |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8256 | goto internal_error; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8257 | |
| 8258 | prevns = ns; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8259 | next_ns_decl: |
| 8260 | ns = ns->next; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8261 | } |
| 8262 | } |
| 8263 | if (! adoptns) |
| 8264 | goto ns_end; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8265 | /* No break on purpose. */ |
| 8266 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8267 | /* No ns, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8268 | if (cur->ns == NULL) |
| 8269 | goto ns_end; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8270 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8271 | if (! parnsdone) { |
| 8272 | if ((elem->parent) && |
| 8273 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8274 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8275 | elem->parent) == -1) |
| 8276 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8277 | } |
| 8278 | parnsdone = 1; |
| 8279 | } |
| 8280 | /* |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8281 | * Adjust the reference if this was a redundant ns-decl. |
| 8282 | */ |
| 8283 | if (listRedund) { |
| 8284 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8285 | if (cur->ns == listRedund[j]) { |
| 8286 | cur->ns = listRedund[++j]; |
| 8287 | break; |
| 8288 | } |
| 8289 | } |
| 8290 | } |
| 8291 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8292 | * Adopt ns-references. |
| 8293 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8294 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8295 | /* |
| 8296 | * Search for a mapping. |
| 8297 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8298 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8299 | if ((mi->shadowDepth == -1) && |
| 8300 | (cur->ns == mi->oldNs)) { |
| 8301 | |
| 8302 | cur->ns = mi->newNs; |
| 8303 | goto ns_end; |
| 8304 | } |
| 8305 | } |
| 8306 | } |
| 8307 | /* |
| 8308 | * Aquire a normalized ns-decl and add it to the map. |
| 8309 | */ |
| 8310 | if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem, |
| 8311 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8312 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8313 | ancestorsOnly, |
| 8314 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8315 | goto internal_error; |
| 8316 | cur->ns = ns; |
| 8317 | |
| 8318 | ns_end: |
| 8319 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8320 | (cur->properties != NULL)) { |
| 8321 | /* |
| 8322 | * Process attributes. |
| 8323 | */ |
| 8324 | cur = (xmlNodePtr) cur->properties; |
| 8325 | continue; |
| 8326 | } |
| 8327 | break; |
| 8328 | default: |
| 8329 | goto next_sibling; |
| 8330 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8331 | into_content: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8332 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8333 | (cur->children != NULL)) { |
| 8334 | /* |
| 8335 | * Process content of element-nodes only. |
| 8336 | */ |
| 8337 | cur = cur->children; |
| 8338 | continue; |
| 8339 | } |
| 8340 | next_sibling: |
| 8341 | if (cur == elem) |
| 8342 | break; |
| 8343 | if (cur->type == XML_ELEMENT_NODE) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8344 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8345 | /* |
| 8346 | * Pop mappings. |
| 8347 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8348 | while ((nsMap->last != NULL) && |
| 8349 | (nsMap->last->depth >= depth)) |
| 8350 | { |
| 8351 | XML_NSMAP_POP(nsMap, mi) |
| 8352 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8353 | /* |
| 8354 | * Unshadow. |
| 8355 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8356 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8357 | if (mi->shadowDepth >= depth) |
| 8358 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8359 | } |
| 8360 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8361 | depth--; |
| 8362 | } |
| 8363 | if (cur->next != NULL) |
| 8364 | cur = cur->next; |
| 8365 | else { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8366 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 8367 | cur = cur->parent; |
| 8368 | goto into_content; |
| 8369 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8370 | cur = cur->parent; |
| 8371 | goto next_sibling; |
| 8372 | } |
| 8373 | } while (cur != NULL); |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8374 | |
| 8375 | ret = 0; |
| 8376 | goto exit; |
| 8377 | internal_error: |
| 8378 | ret = -1; |
| 8379 | exit: |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8380 | if (listRedund) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8381 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8382 | xmlFreeNs(listRedund[j]); |
| 8383 | } |
| 8384 | xmlFree(listRedund); |
| 8385 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8386 | if (nsMap != NULL) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8387 | xmlDOMWrapNsMapFree(nsMap); |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8388 | return (ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8389 | } |
| 8390 | |
| 8391 | /* |
| 8392 | * xmlDOMWrapAdoptBranch: |
| 8393 | * @ctxt: the optional context for custom processing |
| 8394 | * @sourceDoc: the optional sourceDoc |
| 8395 | * @node: the element-node to start with |
| 8396 | * @destDoc: the destination doc for adoption |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8397 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8398 | * @options: option flags |
| 8399 | * |
| 8400 | * Ensures that ns-references point to @destDoc: either to |
| 8401 | * elements->nsDef entries if @destParent is given, or to |
| 8402 | * @destDoc->oldNs otherwise. |
| 8403 | * If @destParent is given, it ensures that the tree is namespace |
| 8404 | * wellformed by creating additional ns-decls where needed. |
| 8405 | * Note that, since prefixes of already existent ns-decls can be |
| 8406 | * shadowed by this process, it could break QNames in attribute |
| 8407 | * values or element content. |
| 8408 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8409 | * NOTE: This function was not intensively tested. |
| 8410 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8411 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8412 | */ |
| 8413 | static int |
| 8414 | xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt, |
| 8415 | xmlDocPtr sourceDoc, |
| 8416 | xmlNodePtr node, |
| 8417 | xmlDocPtr destDoc, |
| 8418 | xmlNodePtr destParent, |
| 8419 | int options ATTRIBUTE_UNUSED) |
| 8420 | { |
| 8421 | int ret = 0; |
| 8422 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8423 | xmlNsMapPtr nsMap = NULL; |
| 8424 | xmlNsMapItemPtr mi; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 8425 | xmlNsPtr ns = NULL; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8426 | int depth = -1, adoptStr = 1; |
| 8427 | /* gather @parent's ns-decls. */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8428 | int parnsdone; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8429 | /* @ancestorsOnly should be set per option. */ |
| 8430 | int ancestorsOnly = 0; |
| 8431 | |
| 8432 | /* |
| 8433 | * Optimize string adoption for equal or none dicts. |
| 8434 | */ |
| 8435 | if ((sourceDoc != NULL) && |
| 8436 | (sourceDoc->dict == destDoc->dict)) |
| 8437 | adoptStr = 0; |
| 8438 | else |
| 8439 | adoptStr = 1; |
| 8440 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8441 | /* |
| 8442 | * Get the ns-map from the context if available. |
| 8443 | */ |
| 8444 | if (ctxt) |
| 8445 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 8446 | /* |
| 8447 | * Disable search for ns-decls in the parent-axis of the |
| 8448 | * desination element, if: |
| 8449 | * 1) there's no destination parent |
| 8450 | * 2) custom ns-reference handling is used |
| 8451 | */ |
| 8452 | if ((destParent == NULL) || |
| 8453 | (ctxt && ctxt->getNsForNodeFunc)) |
| 8454 | { |
| 8455 | parnsdone = 1; |
| 8456 | } else |
| 8457 | parnsdone = 0; |
| 8458 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8459 | cur = node; |
| 8460 | while (cur != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8461 | /* |
| 8462 | * Paranoid source-doc sanity check. |
| 8463 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8464 | if (cur->doc != sourceDoc) { |
| 8465 | /* |
| 8466 | * We'll assume XIncluded nodes if the doc differs. |
| 8467 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 8468 | * This here skips XIncluded nodes and tries to handle |
| 8469 | * broken sequences. |
| 8470 | */ |
| 8471 | if (cur->next == NULL) |
| 8472 | goto leave_node; |
| 8473 | do { |
| 8474 | cur = cur->next; |
| 8475 | if ((cur->type == XML_XINCLUDE_END) || |
| 8476 | (cur->doc == node->doc)) |
| 8477 | break; |
| 8478 | } while (cur->next != NULL); |
| 8479 | |
| 8480 | if (cur->doc != node->doc) |
| 8481 | goto leave_node; |
| 8482 | } |
| 8483 | cur->doc = destDoc; |
| 8484 | switch (cur->type) { |
| 8485 | case XML_XINCLUDE_START: |
| 8486 | case XML_XINCLUDE_END: |
| 8487 | /* |
| 8488 | * TODO |
| 8489 | */ |
| 8490 | return (-1); |
| 8491 | case XML_ELEMENT_NODE: |
| 8492 | curElem = cur; |
| 8493 | depth++; |
| 8494 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8495 | * Namespace declarations. |
| 8496 | * - ns->href and ns->prefix are never in the dict, so |
| 8497 | * we need not move the values over to the destination dict. |
| 8498 | * - Note that for custom handling of ns-references, |
| 8499 | * the ns-decls need not be stored in the ns-map, |
| 8500 | * since they won't be referenced by node->ns. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8501 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8502 | if ((cur->nsDef) && |
| 8503 | ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL))) |
| 8504 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8505 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8506 | /* |
| 8507 | * Gather @parent's in-scope ns-decls. |
| 8508 | */ |
| 8509 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8510 | destParent) == -1) |
| 8511 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8512 | parnsdone = 1; |
| 8513 | } |
| 8514 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8515 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8516 | * NOTE: ns->prefix and ns->href are never in the dict. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8517 | * XML_TREE_ADOPT_STR(ns->prefix) |
| 8518 | * XML_TREE_ADOPT_STR(ns->href) |
| 8519 | */ |
| 8520 | /* |
| 8521 | * Does it shadow any ns-decl? |
| 8522 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8523 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8524 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8525 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8526 | (mi->shadowDepth == -1) && |
| 8527 | ((ns->prefix == mi->newNs->prefix) || |
| 8528 | xmlStrEqual(ns->prefix, |
| 8529 | mi->newNs->prefix))) { |
| 8530 | |
| 8531 | mi->shadowDepth = depth; |
| 8532 | } |
| 8533 | } |
| 8534 | } |
| 8535 | /* |
| 8536 | * Push mapping. |
| 8537 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8538 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8539 | ns, ns, depth) == NULL) |
| 8540 | goto internal_error; |
| 8541 | } |
| 8542 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8543 | /* No break on purpose. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8544 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8545 | /* No namespace, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8546 | if (cur->ns == NULL) |
| 8547 | goto ns_end; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8548 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8549 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8550 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8551 | destParent) == -1) |
| 8552 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8553 | parnsdone = 1; |
| 8554 | } |
| 8555 | /* |
| 8556 | * Adopt ns-references. |
| 8557 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8558 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8559 | /* |
| 8560 | * Search for a mapping. |
| 8561 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8562 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8563 | if ((mi->shadowDepth == -1) && |
| 8564 | (cur->ns == mi->oldNs)) { |
| 8565 | |
| 8566 | cur->ns = mi->newNs; |
| 8567 | goto ns_end; |
| 8568 | } |
| 8569 | } |
| 8570 | } |
| 8571 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8572 | * No matching namespace in scope. We need a new one. |
| 8573 | */ |
| 8574 | if ((ctxt) && (ctxt->getNsForNodeFunc)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8575 | /* |
| 8576 | * User-defined behaviour. |
| 8577 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8578 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 8579 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8580 | /* |
| 8581 | * Insert mapping if ns is available; it's the users fault |
| 8582 | * if not. |
| 8583 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8584 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8585 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8586 | goto internal_error; |
| 8587 | cur->ns = ns; |
| 8588 | } else { |
| 8589 | /* |
| 8590 | * Aquire a normalized ns-decl and add it to the map. |
| 8591 | */ |
| 8592 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
| 8593 | /* ns-decls on curElem or on destDoc->oldNs */ |
| 8594 | destParent ? curElem : NULL, |
| 8595 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8596 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8597 | ancestorsOnly, |
| 8598 | /* ns-decls must be prefixed for attributes. */ |
| 8599 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8600 | goto internal_error; |
| 8601 | cur->ns = ns; |
| 8602 | } |
| 8603 | ns_end: |
| 8604 | /* |
| 8605 | * Further node properties. |
| 8606 | * TODO: Is this all? |
| 8607 | */ |
| 8608 | XML_TREE_ADOPT_STR(cur->name) |
| 8609 | if (cur->type == XML_ELEMENT_NODE) { |
| 8610 | cur->psvi = NULL; |
| 8611 | cur->line = 0; |
| 8612 | cur->extra = 0; |
| 8613 | /* |
| 8614 | * Walk attributes. |
| 8615 | */ |
| 8616 | if (cur->properties != NULL) { |
| 8617 | /* |
| 8618 | * Process first attribute node. |
| 8619 | */ |
| 8620 | cur = (xmlNodePtr) cur->properties; |
| 8621 | continue; |
| 8622 | } |
| 8623 | } else { |
| 8624 | /* |
| 8625 | * Attributes. |
| 8626 | */ |
| 8627 | if ((sourceDoc != NULL) && |
| 8628 | (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID)) |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8629 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8630 | xmlRemoveID(sourceDoc, (xmlAttrPtr) cur); |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8631 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8632 | ((xmlAttrPtr) cur)->atype = 0; |
| 8633 | ((xmlAttrPtr) cur)->psvi = NULL; |
| 8634 | } |
| 8635 | break; |
| 8636 | case XML_TEXT_NODE: |
| 8637 | case XML_CDATA_SECTION_NODE: |
| 8638 | /* |
| 8639 | * This puts the content in the dest dict, only if |
| 8640 | * it was previously in the source dict. |
| 8641 | */ |
| 8642 | XML_TREE_ADOPT_STR_2(cur->content) |
| 8643 | goto leave_node; |
| 8644 | case XML_ENTITY_REF_NODE: |
| 8645 | /* |
| 8646 | * Remove reference to the entitity-node. |
| 8647 | */ |
| 8648 | cur->content = NULL; |
| 8649 | cur->children = NULL; |
| 8650 | cur->last = NULL; |
| 8651 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 8652 | xmlEntityPtr ent; |
| 8653 | /* |
| 8654 | * Assign new entity-node if available. |
| 8655 | */ |
| 8656 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 8657 | if (ent != NULL) { |
| 8658 | cur->content = ent->content; |
| 8659 | cur->children = (xmlNodePtr) ent; |
| 8660 | cur->last = (xmlNodePtr) ent; |
| 8661 | } |
| 8662 | } |
| 8663 | goto leave_node; |
| 8664 | case XML_PI_NODE: |
| 8665 | XML_TREE_ADOPT_STR(cur->name) |
| 8666 | XML_TREE_ADOPT_STR_2(cur->content) |
| 8667 | break; |
| 8668 | case XML_COMMENT_NODE: |
| 8669 | break; |
| 8670 | default: |
| 8671 | goto internal_error; |
| 8672 | } |
| 8673 | /* |
| 8674 | * Walk the tree. |
| 8675 | */ |
| 8676 | if (cur->children != NULL) { |
| 8677 | cur = cur->children; |
| 8678 | continue; |
| 8679 | } |
| 8680 | |
| 8681 | leave_node: |
| 8682 | if (cur == node) |
| 8683 | break; |
| 8684 | if ((cur->type == XML_ELEMENT_NODE) || |
| 8685 | (cur->type == XML_XINCLUDE_START) || |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8686 | (cur->type == XML_XINCLUDE_END)) |
| 8687 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8688 | /* |
| 8689 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 8690 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8691 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8692 | /* |
| 8693 | * Pop mappings. |
| 8694 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8695 | while ((nsMap->last != NULL) && |
| 8696 | (nsMap->last->depth >= depth)) |
| 8697 | { |
| 8698 | XML_NSMAP_POP(nsMap, mi) |
| 8699 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8700 | /* |
| 8701 | * Unshadow. |
| 8702 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8703 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8704 | if (mi->shadowDepth >= depth) |
| 8705 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8706 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8707 | } |
| 8708 | depth--; |
| 8709 | } |
| 8710 | if (cur->next != NULL) |
| 8711 | cur = cur->next; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8712 | else if ((cur->type == XML_ATTRIBUTE_NODE) && |
| 8713 | (cur->parent->children != NULL)) |
| 8714 | { |
| 8715 | cur = cur->parent->children; |
| 8716 | } else { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8717 | cur = cur->parent; |
| 8718 | goto leave_node; |
| 8719 | } |
| 8720 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8721 | |
| 8722 | goto exit; |
| 8723 | |
| 8724 | internal_error: |
| 8725 | ret = -1; |
| 8726 | |
| 8727 | exit: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8728 | /* |
| 8729 | * Cleanup. |
| 8730 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8731 | if (nsMap != NULL) { |
| 8732 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 8733 | /* |
| 8734 | * Just cleanup the map but don't free. |
| 8735 | */ |
| 8736 | if (nsMap->first) { |
| 8737 | if (nsMap->pool) |
| 8738 | nsMap->last->next = nsMap->pool; |
| 8739 | nsMap->pool = nsMap->first; |
| 8740 | nsMap->first = NULL; |
| 8741 | } |
| 8742 | } else |
| 8743 | xmlDOMWrapNsMapFree(nsMap); |
| 8744 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8745 | return(ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8746 | } |
| 8747 | |
| 8748 | /* |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8749 | * xmlDOMWrapCloneNode: |
| 8750 | * @ctxt: the optional context for custom processing |
| 8751 | * @sourceDoc: the optional sourceDoc |
| 8752 | * @node: the node to start with |
| 8753 | * @resNode: the clone of the given @node |
| 8754 | * @destDoc: the destination doc |
| 8755 | * @destParent: the optional new parent of @node in @destDoc |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 8756 | * @deep: descend into child if set |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8757 | * @options: option flags |
| 8758 | * |
| 8759 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 8760 | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8761 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used. |
| 8762 | * This is the case when you don't know already where the cloned branch |
| 8763 | * will be added to. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8764 | * |
| 8765 | * If @destParent is given, it ensures that the tree is namespace |
| 8766 | * wellformed by creating additional ns-decls where needed. |
| 8767 | * Note that, since prefixes of already existent ns-decls can be |
| 8768 | * shadowed by this process, it could break QNames in attribute |
| 8769 | * values or element content. |
| 8770 | * TODO: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8771 | * 1) What to do with XInclude? Currently this returns an error for XInclude. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8772 | * |
| 8773 | * Returns 0 if the operation succeeded, |
| 8774 | * 1 if a node of unsupported (or not yet supported) type was given, |
| 8775 | * -1 on API/internal errors. |
| 8776 | */ |
| 8777 | |
| 8778 | int |
| 8779 | xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt, |
| 8780 | xmlDocPtr sourceDoc, |
| 8781 | xmlNodePtr node, |
| 8782 | xmlNodePtr *resNode, |
| 8783 | xmlDocPtr destDoc, |
| 8784 | xmlNodePtr destParent, |
| 8785 | int deep, |
| 8786 | int options ATTRIBUTE_UNUSED) |
| 8787 | { |
| 8788 | int ret = 0; |
| 8789 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8790 | xmlNsMapPtr nsMap = NULL; |
| 8791 | xmlNsMapItemPtr mi; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8792 | xmlNsPtr ns; |
| 8793 | int depth = -1; |
| 8794 | /* int adoptStr = 1; */ |
| 8795 | /* gather @parent's ns-decls. */ |
| 8796 | int parnsdone = 0; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8797 | /* |
| 8798 | * @ancestorsOnly: |
| 8799 | * TODO: @ancestorsOnly should be set per option. |
| 8800 | * |
| 8801 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8802 | int ancestorsOnly = 0; |
| 8803 | xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8804 | xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL; |
| 8805 | xmlDictPtr dict; /* The destination dict */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8806 | |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 8807 | if ((node == NULL) || (resNode == NULL) || (destDoc == NULL)) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8808 | return(-1); |
| 8809 | /* |
| 8810 | * TODO: Initially we support only element-nodes. |
| 8811 | */ |
| 8812 | if (node->type != XML_ELEMENT_NODE) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8813 | return(1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8814 | /* |
| 8815 | * Check node->doc sanity. |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8816 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8817 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 8818 | (node->doc != sourceDoc)) { |
| 8819 | /* |
| 8820 | * Might be an XIncluded node. |
| 8821 | */ |
| 8822 | return (-1); |
| 8823 | } |
| 8824 | if (sourceDoc == NULL) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8825 | sourceDoc = node->doc; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 8826 | if (sourceDoc == NULL) |
| 8827 | return (-1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8828 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8829 | dict = destDoc->dict; |
| 8830 | /* |
| 8831 | * Reuse the namespace map of the context. |
| 8832 | */ |
| 8833 | if (ctxt) |
| 8834 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 8835 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8836 | *resNode = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8837 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8838 | cur = node; |
| 8839 | while (cur != NULL) { |
| 8840 | if (cur->doc != sourceDoc) { |
| 8841 | /* |
| 8842 | * We'll assume XIncluded nodes if the doc differs. |
| 8843 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 8844 | * TODO: This here returns -1 in this case. |
| 8845 | */ |
| 8846 | goto internal_error; |
| 8847 | } |
| 8848 | /* |
| 8849 | * Create a new node. |
| 8850 | */ |
| 8851 | switch (cur->type) { |
| 8852 | case XML_XINCLUDE_START: |
| 8853 | case XML_XINCLUDE_END: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8854 | /* |
| 8855 | * TODO: What to do with XInclude? |
| 8856 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8857 | goto internal_error; |
| 8858 | break; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8859 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8860 | case XML_TEXT_NODE: |
| 8861 | case XML_CDATA_SECTION_NODE: |
| 8862 | case XML_COMMENT_NODE: |
| 8863 | case XML_PI_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8864 | case XML_DOCUMENT_FRAG_NODE: |
| 8865 | case XML_ENTITY_REF_NODE: |
| 8866 | case XML_ENTITY_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8867 | /* |
| 8868 | * Nodes of xmlNode structure. |
| 8869 | */ |
| 8870 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 8871 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8872 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8873 | goto internal_error; |
| 8874 | } |
| 8875 | memset(clone, 0, sizeof(xmlNode)); |
| 8876 | /* |
| 8877 | * Set hierachical links. |
| 8878 | */ |
| 8879 | if (resultClone != NULL) { |
| 8880 | clone->parent = parentClone; |
| 8881 | if (prevClone) { |
| 8882 | prevClone->next = clone; |
| 8883 | clone->prev = prevClone; |
| 8884 | } else |
| 8885 | parentClone->children = clone; |
| 8886 | } else |
| 8887 | resultClone = clone; |
| 8888 | |
| 8889 | break; |
| 8890 | case XML_ATTRIBUTE_NODE: |
| 8891 | /* |
| 8892 | * Attributes (xmlAttr). |
| 8893 | */ |
| 8894 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr)); |
| 8895 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8896 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8897 | goto internal_error; |
| 8898 | } |
| 8899 | memset(clone, 0, sizeof(xmlAttr)); |
| 8900 | /* |
| 8901 | * Set hierachical links. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8902 | * TODO: Change this to add to the end of attributes. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8903 | */ |
| 8904 | if (resultClone != NULL) { |
| 8905 | clone->parent = parentClone; |
| 8906 | if (prevClone) { |
| 8907 | prevClone->next = clone; |
| 8908 | clone->prev = prevClone; |
| 8909 | } else |
| 8910 | parentClone->properties = (xmlAttrPtr) clone; |
| 8911 | } else |
| 8912 | resultClone = clone; |
| 8913 | break; |
| 8914 | default: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8915 | /* |
| 8916 | * TODO QUESTION: Any other nodes expected? |
| 8917 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8918 | goto internal_error; |
| 8919 | } |
| 8920 | |
| 8921 | clone->type = cur->type; |
| 8922 | clone->doc = destDoc; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8923 | |
| 8924 | /* |
| 8925 | * Clone the name of the node if any. |
| 8926 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8927 | if (cur->name == xmlStringText) |
| 8928 | clone->name = xmlStringText; |
| 8929 | else if (cur->name == xmlStringTextNoenc) |
| 8930 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8931 | * NOTE: Although xmlStringTextNoenc is never assigned to a node |
| 8932 | * in tree.c, it might be set in Libxslt via |
| 8933 | * "xsl:disable-output-escaping". |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8934 | */ |
| 8935 | clone->name = xmlStringTextNoenc; |
| 8936 | else if (cur->name == xmlStringComment) |
| 8937 | clone->name = xmlStringComment; |
| 8938 | else if (cur->name != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8939 | DICT_CONST_COPY(cur->name, clone->name); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8940 | } |
| 8941 | |
| 8942 | switch (cur->type) { |
| 8943 | case XML_XINCLUDE_START: |
| 8944 | case XML_XINCLUDE_END: |
| 8945 | /* |
| 8946 | * TODO |
| 8947 | */ |
| 8948 | return (-1); |
| 8949 | case XML_ELEMENT_NODE: |
| 8950 | curElem = cur; |
| 8951 | depth++; |
| 8952 | /* |
| 8953 | * Namespace declarations. |
| 8954 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8955 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8956 | if (! parnsdone) { |
| 8957 | if (destParent && (ctxt == NULL)) { |
| 8958 | /* |
| 8959 | * Gather @parent's in-scope ns-decls. |
| 8960 | */ |
| 8961 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8962 | destParent) == -1) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8963 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8964 | } |
| 8965 | parnsdone = 1; |
| 8966 | } |
| 8967 | /* |
| 8968 | * Clone namespace declarations. |
| 8969 | */ |
| 8970 | cloneNsDefSlot = &(clone->nsDef); |
| 8971 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8972 | /* |
| 8973 | * Create a new xmlNs. |
| 8974 | */ |
| 8975 | cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 8976 | if (cloneNs == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8977 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): " |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8978 | "allocating namespace"); |
| 8979 | return(-1); |
| 8980 | } |
| 8981 | memset(cloneNs, 0, sizeof(xmlNs)); |
| 8982 | cloneNs->type = XML_LOCAL_NAMESPACE; |
| 8983 | |
| 8984 | if (ns->href != NULL) |
| 8985 | cloneNs->href = xmlStrdup(ns->href); |
| 8986 | if (ns->prefix != NULL) |
| 8987 | cloneNs->prefix = xmlStrdup(ns->prefix); |
| 8988 | |
| 8989 | *cloneNsDefSlot = cloneNs; |
| 8990 | cloneNsDefSlot = &(cloneNs->next); |
| 8991 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8992 | /* |
| 8993 | * Note that for custom handling of ns-references, |
| 8994 | * the ns-decls need not be stored in the ns-map, |
| 8995 | * since they won't be referenced by node->ns. |
| 8996 | */ |
| 8997 | if ((ctxt == NULL) || |
| 8998 | (ctxt->getNsForNodeFunc == NULL)) |
| 8999 | { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9000 | /* |
| 9001 | * Does it shadow any ns-decl? |
| 9002 | */ |
| 9003 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
| 9004 | XML_NSMAP_FOREACH(nsMap, mi) { |
| 9005 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 9006 | (mi->shadowDepth == -1) && |
| 9007 | ((ns->prefix == mi->newNs->prefix) || |
| 9008 | xmlStrEqual(ns->prefix, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9009 | mi->newNs->prefix))) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9010 | /* |
| 9011 | * Mark as shadowed at the current |
| 9012 | * depth. |
| 9013 | */ |
| 9014 | mi->shadowDepth = depth; |
| 9015 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9016 | } |
| 9017 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9018 | /* |
| 9019 | * Push mapping. |
| 9020 | */ |
| 9021 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
| 9022 | ns, cloneNs, depth) == NULL) |
| 9023 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9024 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9025 | } |
| 9026 | } |
| 9027 | /* cur->ns will be processed further down. */ |
| 9028 | break; |
| 9029 | case XML_ATTRIBUTE_NODE: |
| 9030 | /* IDs will be processed further down. */ |
| 9031 | /* cur->ns will be processed further down. */ |
| 9032 | break; |
| 9033 | case XML_TEXT_NODE: |
| 9034 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9035 | /* |
| 9036 | * Note that this will also cover the values of attributes. |
| 9037 | */ |
| 9038 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9039 | goto leave_node; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9040 | case XML_ENTITY_NODE: |
| 9041 | /* TODO: What to do here? */ |
| 9042 | goto leave_node; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9043 | case XML_ENTITY_REF_NODE: |
| 9044 | if (sourceDoc != destDoc) { |
| 9045 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9046 | xmlEntityPtr ent; |
| 9047 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9048 | * Different doc: Assign new entity-node if available. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9049 | */ |
| 9050 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9051 | if (ent != NULL) { |
| 9052 | clone->content = ent->content; |
| 9053 | clone->children = (xmlNodePtr) ent; |
| 9054 | clone->last = (xmlNodePtr) ent; |
| 9055 | } |
| 9056 | } |
| 9057 | } else { |
| 9058 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9059 | * Same doc: Use the current node's entity declaration |
| 9060 | * and value. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9061 | */ |
| 9062 | clone->content = cur->content; |
| 9063 | clone->children = cur->children; |
| 9064 | clone->last = cur->last; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9065 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9066 | goto leave_node; |
| 9067 | case XML_PI_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9068 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9069 | goto leave_node; |
| 9070 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9071 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9072 | goto leave_node; |
| 9073 | default: |
| 9074 | goto internal_error; |
| 9075 | } |
| 9076 | |
| 9077 | if (cur->ns == NULL) |
| 9078 | goto end_ns_reference; |
| 9079 | |
| 9080 | /* handle_ns_reference: */ |
| 9081 | /* |
| 9082 | ** The following will take care of references to ns-decls ******** |
| 9083 | ** and is intended only for element- and attribute-nodes. |
| 9084 | ** |
| 9085 | */ |
| 9086 | if (! parnsdone) { |
| 9087 | if (destParent && (ctxt == NULL)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9088 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1) |
| 9089 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9090 | } |
| 9091 | parnsdone = 1; |
| 9092 | } |
| 9093 | /* |
| 9094 | * Adopt ns-references. |
| 9095 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9096 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9097 | /* |
| 9098 | * Search for a mapping. |
| 9099 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9100 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9101 | if ((mi->shadowDepth == -1) && |
| 9102 | (cur->ns == mi->oldNs)) { |
| 9103 | /* |
| 9104 | * This is the nice case: a mapping was found. |
| 9105 | */ |
| 9106 | clone->ns = mi->newNs; |
| 9107 | goto end_ns_reference; |
| 9108 | } |
| 9109 | } |
| 9110 | } |
| 9111 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9112 | * No matching namespace in scope. We need a new one. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9113 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9114 | if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9115 | /* |
| 9116 | * User-defined behaviour. |
| 9117 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9118 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 9119 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9120 | /* |
| 9121 | * Add user's mapping. |
| 9122 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9123 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9124 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
| 9125 | goto internal_error; |
| 9126 | clone->ns = ns; |
| 9127 | } else { |
| 9128 | /* |
| 9129 | * Aquire a normalized ns-decl and add it to the map. |
| 9130 | */ |
| 9131 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
| 9132 | /* ns-decls on curElem or on destDoc->oldNs */ |
| 9133 | destParent ? curElem : NULL, |
| 9134 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9135 | &nsMap, depth, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9136 | /* if we need to search only in the ancestor-axis */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9137 | ancestorsOnly, |
| 9138 | /* ns-decls must be prefixed for attributes. */ |
| 9139 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 9140 | goto internal_error; |
| 9141 | clone->ns = ns; |
| 9142 | } |
| 9143 | |
| 9144 | end_ns_reference: |
| 9145 | |
| 9146 | /* |
| 9147 | * Some post-processing. |
| 9148 | * |
| 9149 | * Handle ID attributes. |
| 9150 | */ |
| 9151 | if ((clone->type == XML_ATTRIBUTE_NODE) && |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9152 | (clone->parent != NULL)) |
| 9153 | { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9154 | if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) { |
| 9155 | |
| 9156 | xmlChar *idVal; |
| 9157 | |
| 9158 | idVal = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 9159 | if (idVal != NULL) { |
| 9160 | if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) { |
| 9161 | /* TODO: error message. */ |
| 9162 | xmlFree(idVal); |
| 9163 | goto internal_error; |
| 9164 | } |
| 9165 | xmlFree(idVal); |
| 9166 | } |
| 9167 | } |
| 9168 | } |
| 9169 | /* |
| 9170 | ** |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9171 | ** The following will traverse the tree ************************** |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9172 | ** |
| 9173 | * |
| 9174 | * Walk the element's attributes before descending into child-nodes. |
| 9175 | */ |
| 9176 | if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) { |
| 9177 | prevClone = NULL; |
| 9178 | parentClone = clone; |
| 9179 | cur = (xmlNodePtr) cur->properties; |
| 9180 | continue; |
| 9181 | } |
| 9182 | into_content: |
| 9183 | /* |
| 9184 | * Descend into child-nodes. |
| 9185 | */ |
| 9186 | if (cur->children != NULL) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9187 | if (deep || (cur->type == XML_ATTRIBUTE_NODE)) { |
| 9188 | prevClone = NULL; |
| 9189 | parentClone = clone; |
| 9190 | cur = cur->children; |
| 9191 | continue; |
| 9192 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9193 | } |
| 9194 | |
| 9195 | leave_node: |
| 9196 | /* |
| 9197 | * At this point we are done with the node, its content |
| 9198 | * and an element-nodes's attribute-nodes. |
| 9199 | */ |
| 9200 | if (cur == node) |
| 9201 | break; |
| 9202 | if ((cur->type == XML_ELEMENT_NODE) || |
| 9203 | (cur->type == XML_XINCLUDE_START) || |
| 9204 | (cur->type == XML_XINCLUDE_END)) { |
| 9205 | /* |
| 9206 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 9207 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9208 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9209 | /* |
| 9210 | * Pop mappings. |
| 9211 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9212 | while ((nsMap->last != NULL) && |
| 9213 | (nsMap->last->depth >= depth)) |
| 9214 | { |
| 9215 | XML_NSMAP_POP(nsMap, mi) |
| 9216 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9217 | /* |
| 9218 | * Unshadow. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9219 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9220 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9221 | if (mi->shadowDepth >= depth) |
| 9222 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9223 | } |
| 9224 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9225 | depth--; |
| 9226 | } |
| 9227 | if (cur->next != NULL) { |
| 9228 | prevClone = clone; |
| 9229 | cur = cur->next; |
| 9230 | } else if (cur->type != XML_ATTRIBUTE_NODE) { |
| 9231 | /* |
| 9232 | * Set clone->last. |
| 9233 | */ |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9234 | if (clone->parent != NULL) |
| 9235 | clone->parent->last = clone; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9236 | clone = clone->parent; |
| 9237 | parentClone = clone->parent; |
| 9238 | /* |
| 9239 | * Process parent --> next; |
| 9240 | */ |
| 9241 | cur = cur->parent; |
| 9242 | goto leave_node; |
| 9243 | } else { |
| 9244 | /* This is for attributes only. */ |
| 9245 | clone = clone->parent; |
| 9246 | parentClone = clone->parent; |
| 9247 | /* |
| 9248 | * Process parent-element --> children. |
| 9249 | */ |
| 9250 | cur = cur->parent; |
| 9251 | goto into_content; |
| 9252 | } |
| 9253 | } |
| 9254 | goto exit; |
| 9255 | |
| 9256 | internal_error: |
| 9257 | ret = -1; |
| 9258 | |
| 9259 | exit: |
| 9260 | /* |
| 9261 | * Cleanup. |
| 9262 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9263 | if (nsMap != NULL) { |
| 9264 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 9265 | /* |
| 9266 | * Just cleanup the map but don't free. |
| 9267 | */ |
| 9268 | if (nsMap->first) { |
| 9269 | if (nsMap->pool) |
| 9270 | nsMap->last->next = nsMap->pool; |
| 9271 | nsMap->pool = nsMap->first; |
| 9272 | nsMap->first = NULL; |
| 9273 | } |
| 9274 | } else |
| 9275 | xmlDOMWrapNsMapFree(nsMap); |
| 9276 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9277 | /* |
| 9278 | * TODO: Should we try a cleanup of the cloned node in case of a |
| 9279 | * fatal error? |
| 9280 | */ |
| 9281 | *resNode = resultClone; |
| 9282 | return (ret); |
| 9283 | } |
| 9284 | |
| 9285 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9286 | * xmlDOMWrapAdoptAttr: |
| 9287 | * @ctxt: the optional context for custom processing |
| 9288 | * @sourceDoc: the optional source document of attr |
| 9289 | * @attr: the attribute-node to be adopted |
| 9290 | * @destDoc: the destination doc for adoption |
| 9291 | * @destParent: the optional new parent of @attr in @destDoc |
| 9292 | * @options: option flags |
| 9293 | * |
| 9294 | * @attr is adopted by @destDoc. |
| 9295 | * Ensures that ns-references point to @destDoc: either to |
| 9296 | * elements->nsDef entries if @destParent is given, or to |
| 9297 | * @destDoc->oldNs otherwise. |
| 9298 | * |
| 9299 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 9300 | */ |
| 9301 | static int |
| 9302 | xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt, |
| 9303 | xmlDocPtr sourceDoc, |
| 9304 | xmlAttrPtr attr, |
| 9305 | xmlDocPtr destDoc, |
| 9306 | xmlNodePtr destParent, |
| 9307 | int options ATTRIBUTE_UNUSED) |
| 9308 | { |
| 9309 | xmlNodePtr cur; |
| 9310 | int adoptStr = 1; |
| 9311 | |
| 9312 | if ((attr == NULL) || (destDoc == NULL)) |
| 9313 | return (-1); |
| 9314 | |
| 9315 | attr->doc = destDoc; |
| 9316 | if (attr->ns != NULL) { |
| 9317 | xmlNsPtr ns = NULL; |
| 9318 | |
| 9319 | if (ctxt != NULL) { |
| 9320 | /* TODO: User defined. */ |
| 9321 | } |
| 9322 | /* XML Namespace. */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9323 | if (IS_STR_XML(attr->ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9324 | ns = xmlTreeEnsureXMLDecl(destDoc); |
| 9325 | } else if (destParent == NULL) { |
| 9326 | /* |
| 9327 | * Store in @destDoc->oldNs. |
| 9328 | */ |
| 9329 | ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix); |
| 9330 | } else { |
| 9331 | /* |
| 9332 | * Declare on @destParent. |
| 9333 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9334 | if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9335 | &ns, 1) == -1) |
| 9336 | goto internal_error; |
| 9337 | if (ns == NULL) { |
| 9338 | ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent, |
| 9339 | attr->ns->href, attr->ns->prefix, 1); |
| 9340 | } |
| 9341 | } |
| 9342 | if (ns == NULL) |
| 9343 | goto internal_error; |
| 9344 | attr->ns = ns; |
| 9345 | } |
| 9346 | |
| 9347 | XML_TREE_ADOPT_STR(attr->name); |
| 9348 | attr->atype = 0; |
| 9349 | attr->psvi = NULL; |
| 9350 | /* |
| 9351 | * Walk content. |
| 9352 | */ |
| 9353 | if (attr->children == NULL) |
| 9354 | return (0); |
| 9355 | cur = attr->children; |
| 9356 | while (cur != NULL) { |
| 9357 | cur->doc = destDoc; |
| 9358 | switch (cur->type) { |
| 9359 | case XML_TEXT_NODE: |
| 9360 | case XML_CDATA_SECTION_NODE: |
| 9361 | XML_TREE_ADOPT_STR_2(cur->content) |
| 9362 | break; |
| 9363 | case XML_ENTITY_REF_NODE: |
| 9364 | /* |
| 9365 | * Remove reference to the entitity-node. |
| 9366 | */ |
| 9367 | cur->content = NULL; |
| 9368 | cur->children = NULL; |
| 9369 | cur->last = NULL; |
| 9370 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9371 | xmlEntityPtr ent; |
| 9372 | /* |
| 9373 | * Assign new entity-node if available. |
| 9374 | */ |
| 9375 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9376 | if (ent != NULL) { |
| 9377 | cur->content = ent->content; |
| 9378 | cur->children = (xmlNodePtr) ent; |
| 9379 | cur->last = (xmlNodePtr) ent; |
| 9380 | } |
| 9381 | } |
| 9382 | break; |
| 9383 | default: |
| 9384 | break; |
| 9385 | } |
| 9386 | if (cur->children != NULL) { |
| 9387 | cur = cur->children; |
| 9388 | continue; |
| 9389 | } |
| 9390 | next_sibling: |
| 9391 | if (cur == (xmlNodePtr) attr) |
| 9392 | break; |
| 9393 | if (cur->next != NULL) |
| 9394 | cur = cur->next; |
| 9395 | else { |
| 9396 | cur = cur->parent; |
| 9397 | goto next_sibling; |
| 9398 | } |
| 9399 | } |
| 9400 | return (0); |
| 9401 | internal_error: |
| 9402 | return (-1); |
| 9403 | } |
| 9404 | |
| 9405 | /* |
| 9406 | * xmlDOMWrapAdoptNode: |
| 9407 | * @ctxt: the optional context for custom processing |
| 9408 | * @sourceDoc: the optional sourceDoc |
| 9409 | * @node: the node to start with |
| 9410 | * @destDoc: the destination doc |
Kasimier T. Buchcik | 4d9c948 | 2005-06-27 15:04:46 +0000 | [diff] [blame] | 9411 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9412 | * @options: option flags |
| 9413 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9414 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 9415 | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
| 9416 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used |
| 9417 | * This is the case when you have an unliked node and just want to move it |
| 9418 | * to the context of |
| 9419 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9420 | * If @destParent is given, it ensures that the tree is namespace |
| 9421 | * wellformed by creating additional ns-decls where needed. |
| 9422 | * Note that, since prefixes of already existent ns-decls can be |
| 9423 | * shadowed by this process, it could break QNames in attribute |
| 9424 | * values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9425 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9426 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9427 | * Returns 0 if the operation succeeded, |
| 9428 | * 1 if a node of unsupported type was given, |
| 9429 | * 2 if a node of not yet supported type was given and |
| 9430 | * -1 on API/internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9431 | */ |
| 9432 | int |
| 9433 | xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt, |
| 9434 | xmlDocPtr sourceDoc, |
| 9435 | xmlNodePtr node, |
| 9436 | xmlDocPtr destDoc, |
| 9437 | xmlNodePtr destParent, |
| 9438 | int options) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9439 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9440 | if ((node == NULL) || (destDoc == NULL) || |
| 9441 | ((destParent != NULL) && (destParent->doc != destDoc))) |
| 9442 | return(-1); |
| 9443 | /* |
| 9444 | * Check node->doc sanity. |
| 9445 | */ |
| 9446 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 9447 | (node->doc != sourceDoc)) { |
| 9448 | /* |
| 9449 | * Might be an XIncluded node. |
| 9450 | */ |
| 9451 | return (-1); |
| 9452 | } |
| 9453 | if (sourceDoc == NULL) |
| 9454 | sourceDoc = node->doc; |
| 9455 | if (sourceDoc == destDoc) |
| 9456 | return (-1); |
| 9457 | switch (node->type) { |
| 9458 | case XML_ELEMENT_NODE: |
| 9459 | case XML_ATTRIBUTE_NODE: |
| 9460 | case XML_TEXT_NODE: |
| 9461 | case XML_CDATA_SECTION_NODE: |
| 9462 | case XML_ENTITY_REF_NODE: |
| 9463 | case XML_PI_NODE: |
| 9464 | case XML_COMMENT_NODE: |
| 9465 | break; |
| 9466 | case XML_DOCUMENT_FRAG_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9467 | /* TODO: Support document-fragment-nodes. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9468 | return (2); |
| 9469 | default: |
| 9470 | return (1); |
| 9471 | } |
| 9472 | /* |
| 9473 | * Unlink only if @node was not already added to @destParent. |
| 9474 | */ |
| 9475 | if ((node->parent != NULL) && (destParent != node->parent)) |
| 9476 | xmlUnlinkNode(node); |
| 9477 | |
| 9478 | if (node->type == XML_ELEMENT_NODE) { |
| 9479 | return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node, |
| 9480 | destDoc, destParent, options)); |
| 9481 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
| 9482 | return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc, |
| 9483 | (xmlAttrPtr) node, destDoc, destParent, options)); |
| 9484 | } else { |
| 9485 | xmlNodePtr cur = node; |
| 9486 | int adoptStr = 1; |
| 9487 | |
| 9488 | cur->doc = destDoc; |
| 9489 | /* |
| 9490 | * Optimize string adoption. |
| 9491 | */ |
| 9492 | if ((sourceDoc != NULL) && |
| 9493 | (sourceDoc->dict == destDoc->dict)) |
| 9494 | adoptStr = 0; |
| 9495 | switch (node->type) { |
| 9496 | case XML_TEXT_NODE: |
| 9497 | case XML_CDATA_SECTION_NODE: |
| 9498 | XML_TREE_ADOPT_STR_2(node->content) |
| 9499 | break; |
| 9500 | case XML_ENTITY_REF_NODE: |
| 9501 | /* |
| 9502 | * Remove reference to the entitity-node. |
| 9503 | */ |
| 9504 | node->content = NULL; |
| 9505 | node->children = NULL; |
| 9506 | node->last = NULL; |
| 9507 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9508 | xmlEntityPtr ent; |
| 9509 | /* |
| 9510 | * Assign new entity-node if available. |
| 9511 | */ |
| 9512 | ent = xmlGetDocEntity(destDoc, node->name); |
| 9513 | if (ent != NULL) { |
| 9514 | node->content = ent->content; |
| 9515 | node->children = (xmlNodePtr) ent; |
| 9516 | node->last = (xmlNodePtr) ent; |
| 9517 | } |
| 9518 | } |
| 9519 | XML_TREE_ADOPT_STR(node->name) |
| 9520 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 9521 | case XML_PI_NODE: { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9522 | XML_TREE_ADOPT_STR(node->name) |
| 9523 | XML_TREE_ADOPT_STR_2(node->content) |
| 9524 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 9525 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9526 | default: |
| 9527 | break; |
| 9528 | } |
| 9529 | } |
| 9530 | return (0); |
| 9531 | } |
| 9532 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 9533 | #define bottom_tree |
| 9534 | #include "elfgcchack.h" |