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; |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame^] | 95 | case XML_TREE_NOT_UTF8: |
| 96 | msg = "string is not in UTF-8\n"; |
| 97 | break; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 98 | default: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 99 | msg = "unexpected error number\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 100 | } |
| 101 | __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra); |
| 102 | } |
| 103 | |
| 104 | /************************************************************************ |
| 105 | * * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 106 | * A few static variables and macros * |
| 107 | * * |
| 108 | ************************************************************************/ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 109 | /* #undef xmlStringText */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 110 | const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 111 | /* #undef xmlStringTextNoenc */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 112 | const xmlChar xmlStringTextNoenc[] = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 113 | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 114 | /* #undef xmlStringComment */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 115 | const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
| 116 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 117 | static int xmlCompressMode = 0; |
| 118 | static int xmlCheckDTD = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 119 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 120 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
| 121 | xmlNodePtr ulccur = (n)->children; \ |
| 122 | if (ulccur == NULL) { \ |
| 123 | (n)->last = NULL; \ |
| 124 | } else { \ |
| 125 | while (ulccur->next != NULL) { \ |
| 126 | ulccur->parent = (n); \ |
| 127 | ulccur = ulccur->next; \ |
| 128 | } \ |
| 129 | ulccur->parent = (n); \ |
| 130 | (n)->last = ulccur; \ |
| 131 | }} |
| 132 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 133 | #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \ |
| 134 | (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0)) |
| 135 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 136 | /* #define DEBUG_BUFFER */ |
| 137 | /* #define DEBUG_TREE */ |
| 138 | |
| 139 | /************************************************************************ |
| 140 | * * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 141 | * Functions to move to entities.c once the * |
| 142 | * API freeze is smoothen and they can be made public. * |
| 143 | * * |
| 144 | ************************************************************************/ |
| 145 | #include <libxml/hash.h> |
| 146 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 147 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 148 | /** |
| 149 | * xmlGetEntityFromDtd: |
| 150 | * @dtd: A pointer to the DTD to search |
| 151 | * @name: The entity name |
| 152 | * |
| 153 | * Do an entity lookup in the DTD entity hash table and |
| 154 | * return the corresponding entity, if found. |
| 155 | * |
| 156 | * Returns A pointer to the entity structure or NULL if not found. |
| 157 | */ |
| 158 | static xmlEntityPtr |
| 159 | xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 160 | xmlEntitiesTablePtr table; |
| 161 | |
| 162 | if((dtd != NULL) && (dtd->entities != NULL)) { |
| 163 | table = (xmlEntitiesTablePtr) dtd->entities; |
| 164 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 165 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 166 | } |
| 167 | return(NULL); |
| 168 | } |
| 169 | /** |
| 170 | * xmlGetParameterEntityFromDtd: |
| 171 | * @dtd: A pointer to the DTD to search |
| 172 | * @name: The entity name |
| 173 | * |
| 174 | * Do an entity lookup in the DTD pararmeter entity hash table and |
| 175 | * return the corresponding entity, if found. |
| 176 | * |
| 177 | * Returns A pointer to the entity structure or NULL if not found. |
| 178 | */ |
| 179 | static xmlEntityPtr |
| 180 | xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 181 | xmlEntitiesTablePtr table; |
| 182 | |
| 183 | if ((dtd != NULL) && (dtd->pentities != NULL)) { |
| 184 | table = (xmlEntitiesTablePtr) dtd->pentities; |
| 185 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 186 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 187 | } |
| 188 | return(NULL); |
| 189 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 190 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 191 | |
| 192 | /************************************************************************ |
| 193 | * * |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 194 | * QName handling helper * |
| 195 | * * |
| 196 | ************************************************************************/ |
| 197 | |
| 198 | /** |
| 199 | * xmlBuildQName: |
| 200 | * @ncname: the Name |
| 201 | * @prefix: the prefix |
| 202 | * @memory: preallocated memory |
| 203 | * @len: preallocated memory length |
| 204 | * |
| 205 | * Builds the QName @prefix:@ncname in @memory if there is enough space |
| 206 | * and prefix is not NULL nor empty, otherwise allocate a new string. |
| 207 | * If prefix is NULL or empty it returns ncname. |
| 208 | * |
| 209 | * Returns the new string which must be freed by the caller if different from |
| 210 | * @memory and @ncname or NULL in case of error |
| 211 | */ |
| 212 | xmlChar * |
| 213 | xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix, |
| 214 | xmlChar *memory, int len) { |
| 215 | int lenn, lenp; |
| 216 | xmlChar *ret; |
| 217 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 218 | if (ncname == NULL) return(NULL); |
| 219 | if (prefix == NULL) return((xmlChar *) ncname); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 220 | |
| 221 | lenn = strlen((char *) ncname); |
| 222 | lenp = strlen((char *) prefix); |
| 223 | |
| 224 | if ((memory == NULL) || (len < lenn + lenp + 2)) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 225 | ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 226 | if (ret == NULL) { |
| 227 | xmlTreeErrMemory("building QName"); |
| 228 | return(NULL); |
| 229 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 230 | } else { |
| 231 | ret = memory; |
| 232 | } |
| 233 | memcpy(&ret[0], prefix, lenp); |
| 234 | ret[lenp] = ':'; |
| 235 | memcpy(&ret[lenp + 1], ncname, lenn); |
| 236 | ret[lenn + lenp + 1] = 0; |
| 237 | return(ret); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * xmlSplitQName2: |
| 242 | * @name: the full QName |
| 243 | * @prefix: a xmlChar ** |
| 244 | * |
| 245 | * parse an XML qualified name string |
| 246 | * |
| 247 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 248 | * |
| 249 | * [NS 6] Prefix ::= NCName |
| 250 | * |
| 251 | * [NS 7] LocalPart ::= NCName |
| 252 | * |
| 253 | * Returns NULL if not a QName, otherwise the local part, and prefix |
| 254 | * is updated to get the Prefix if any. |
| 255 | */ |
| 256 | |
| 257 | xmlChar * |
| 258 | xmlSplitQName2(const xmlChar *name, xmlChar **prefix) { |
| 259 | int len = 0; |
| 260 | xmlChar *ret = NULL; |
| 261 | |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 262 | if (prefix == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 263 | *prefix = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 264 | if (name == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 265 | |
| 266 | #ifndef XML_XML_NAMESPACE |
| 267 | /* xml: prefix is not really a namespace */ |
| 268 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 269 | (name[2] == 'l') && (name[3] == ':')) |
| 270 | return(NULL); |
| 271 | #endif |
| 272 | |
| 273 | /* nasty but valid */ |
| 274 | if (name[0] == ':') |
| 275 | return(NULL); |
| 276 | |
| 277 | /* |
| 278 | * we are not trying to validate but just to cut, and yes it will |
| 279 | * work even if this is as set of UTF-8 encoded chars |
| 280 | */ |
| 281 | while ((name[len] != 0) && (name[len] != ':')) |
| 282 | len++; |
| 283 | |
| 284 | if (name[len] == 0) |
| 285 | return(NULL); |
| 286 | |
| 287 | *prefix = xmlStrndup(name, len); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 288 | if (*prefix == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 289 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 290 | return(NULL); |
| 291 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 292 | ret = xmlStrdup(&name[len + 1]); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 293 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 294 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 295 | if (*prefix != NULL) { |
| 296 | xmlFree(*prefix); |
| 297 | *prefix = NULL; |
| 298 | } |
| 299 | return(NULL); |
| 300 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 301 | |
| 302 | return(ret); |
| 303 | } |
| 304 | |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 305 | /** |
| 306 | * xmlSplitQName3: |
| 307 | * @name: the full QName |
| 308 | * @len: an int * |
| 309 | * |
| 310 | * parse an XML qualified name string,i |
| 311 | * |
| 312 | * returns NULL if it is not a Qualified Name, otherwise, update len |
| 313 | * with the lenght in byte of the prefix and return a pointer |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 314 | * to the start of the name without the prefix |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 315 | */ |
| 316 | |
| 317 | const xmlChar * |
| 318 | xmlSplitQName3(const xmlChar *name, int *len) { |
| 319 | int l = 0; |
| 320 | |
| 321 | if (name == NULL) return(NULL); |
| 322 | if (len == NULL) return(NULL); |
| 323 | |
| 324 | /* nasty but valid */ |
| 325 | if (name[0] == ':') |
| 326 | return(NULL); |
| 327 | |
| 328 | /* |
| 329 | * we are not trying to validate but just to cut, and yes it will |
| 330 | * work even if this is as set of UTF-8 encoded chars |
| 331 | */ |
| 332 | while ((name[l] != 0) && (name[l] != ':')) |
| 333 | l++; |
| 334 | |
| 335 | if (name[l] == 0) |
| 336 | return(NULL); |
| 337 | |
| 338 | *len = l; |
| 339 | |
| 340 | return(&name[l+1]); |
| 341 | } |
| 342 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 343 | /************************************************************************ |
| 344 | * * |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 345 | * Check Name, NCName and QName strings * |
| 346 | * * |
| 347 | ************************************************************************/ |
| 348 | |
| 349 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l) |
| 350 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 351 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_DEBUG_ENABLED) || defined (LIBXML_HTML_ENABLED) || defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 352 | /** |
| 353 | * xmlValidateNCName: |
| 354 | * @value: the value to check |
| 355 | * @space: allow spaces in front and end of the string |
| 356 | * |
| 357 | * Check that a value conforms to the lexical space of NCName |
| 358 | * |
| 359 | * Returns 0 if this validates, a positive error code number otherwise |
| 360 | * and -1 in case of internal or API error. |
| 361 | */ |
| 362 | int |
| 363 | xmlValidateNCName(const xmlChar *value, int space) { |
| 364 | const xmlChar *cur = value; |
| 365 | int c,l; |
| 366 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 367 | if (value == NULL) |
| 368 | return(-1); |
| 369 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 370 | /* |
| 371 | * First quick algorithm for ASCII range |
| 372 | */ |
| 373 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 374 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 375 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 376 | (*cur == '_')) |
| 377 | cur++; |
| 378 | else |
| 379 | goto try_complex; |
| 380 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 381 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 382 | ((*cur >= '0') && (*cur <= '9')) || |
| 383 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 384 | cur++; |
| 385 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 386 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 387 | if (*cur == 0) |
| 388 | return(0); |
| 389 | |
| 390 | try_complex: |
| 391 | /* |
| 392 | * Second check for chars outside the ASCII range |
| 393 | */ |
| 394 | cur = value; |
| 395 | c = CUR_SCHAR(cur, l); |
| 396 | if (space) { |
| 397 | while (IS_BLANK(c)) { |
| 398 | cur += l; |
| 399 | c = CUR_SCHAR(cur, l); |
| 400 | } |
| 401 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 402 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 403 | return(1); |
| 404 | cur += l; |
| 405 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 406 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 407 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 408 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 409 | cur += l; |
| 410 | c = CUR_SCHAR(cur, l); |
| 411 | } |
| 412 | if (space) { |
| 413 | while (IS_BLANK(c)) { |
| 414 | cur += l; |
| 415 | c = CUR_SCHAR(cur, l); |
| 416 | } |
| 417 | } |
| 418 | if (c != 0) |
| 419 | return(1); |
| 420 | |
| 421 | return(0); |
| 422 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 423 | #endif |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 424 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 425 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 426 | /** |
| 427 | * xmlValidateQName: |
| 428 | * @value: the value to check |
| 429 | * @space: allow spaces in front and end of the string |
| 430 | * |
| 431 | * Check that a value conforms to the lexical space of QName |
| 432 | * |
| 433 | * Returns 0 if this validates, a positive error code number otherwise |
| 434 | * and -1 in case of internal or API error. |
| 435 | */ |
| 436 | int |
| 437 | xmlValidateQName(const xmlChar *value, int space) { |
| 438 | const xmlChar *cur = value; |
| 439 | int c,l; |
| 440 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 441 | if (value == NULL) |
| 442 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 443 | /* |
| 444 | * First quick algorithm for ASCII range |
| 445 | */ |
| 446 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 447 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 448 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 449 | (*cur == '_')) |
| 450 | cur++; |
| 451 | else |
| 452 | goto try_complex; |
| 453 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 454 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 455 | ((*cur >= '0') && (*cur <= '9')) || |
| 456 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 457 | cur++; |
| 458 | if (*cur == ':') { |
| 459 | cur++; |
| 460 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 461 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 462 | (*cur == '_')) |
| 463 | cur++; |
| 464 | else |
| 465 | goto try_complex; |
| 466 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 467 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 468 | ((*cur >= '0') && (*cur <= '9')) || |
| 469 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 470 | cur++; |
| 471 | } |
| 472 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 473 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 474 | if (*cur == 0) |
| 475 | return(0); |
| 476 | |
| 477 | try_complex: |
| 478 | /* |
| 479 | * Second check for chars outside the ASCII range |
| 480 | */ |
| 481 | cur = value; |
| 482 | c = CUR_SCHAR(cur, l); |
| 483 | if (space) { |
| 484 | while (IS_BLANK(c)) { |
| 485 | cur += l; |
| 486 | c = CUR_SCHAR(cur, l); |
| 487 | } |
| 488 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 489 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 490 | return(1); |
| 491 | cur += l; |
| 492 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 493 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 494 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 495 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 496 | cur += l; |
| 497 | c = CUR_SCHAR(cur, l); |
| 498 | } |
| 499 | if (c == ':') { |
| 500 | cur += l; |
| 501 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 502 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 503 | return(1); |
| 504 | cur += l; |
| 505 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 506 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 507 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 508 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 509 | cur += l; |
| 510 | c = CUR_SCHAR(cur, l); |
| 511 | } |
| 512 | } |
| 513 | if (space) { |
| 514 | while (IS_BLANK(c)) { |
| 515 | cur += l; |
| 516 | c = CUR_SCHAR(cur, l); |
| 517 | } |
| 518 | } |
| 519 | if (c != 0) |
| 520 | return(1); |
| 521 | return(0); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * xmlValidateName: |
| 526 | * @value: the value to check |
| 527 | * @space: allow spaces in front and end of the string |
| 528 | * |
| 529 | * Check that a value conforms to the lexical space of Name |
| 530 | * |
| 531 | * Returns 0 if this validates, a positive error code number otherwise |
| 532 | * and -1 in case of internal or API error. |
| 533 | */ |
| 534 | int |
| 535 | xmlValidateName(const xmlChar *value, int space) { |
| 536 | const xmlChar *cur = value; |
| 537 | int c,l; |
| 538 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 539 | if (value == NULL) |
| 540 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 541 | /* |
| 542 | * First quick algorithm for ASCII range |
| 543 | */ |
| 544 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 545 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 546 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 547 | (*cur == '_') || (*cur == ':')) |
| 548 | cur++; |
| 549 | else |
| 550 | goto try_complex; |
| 551 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 552 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 553 | ((*cur >= '0') && (*cur <= '9')) || |
| 554 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 555 | cur++; |
| 556 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 557 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 558 | if (*cur == 0) |
| 559 | return(0); |
| 560 | |
| 561 | try_complex: |
| 562 | /* |
| 563 | * Second check for chars outside the ASCII range |
| 564 | */ |
| 565 | cur = value; |
| 566 | c = CUR_SCHAR(cur, l); |
| 567 | if (space) { |
| 568 | while (IS_BLANK(c)) { |
| 569 | cur += l; |
| 570 | c = CUR_SCHAR(cur, l); |
| 571 | } |
| 572 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 573 | if ((!IS_LETTER(c)) && (c != '_') && (c != ':')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 574 | return(1); |
| 575 | cur += l; |
| 576 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 577 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 578 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 579 | cur += l; |
| 580 | c = CUR_SCHAR(cur, l); |
| 581 | } |
| 582 | if (space) { |
| 583 | while (IS_BLANK(c)) { |
| 584 | cur += l; |
| 585 | c = CUR_SCHAR(cur, l); |
| 586 | } |
| 587 | } |
| 588 | if (c != 0) |
| 589 | return(1); |
| 590 | return(0); |
| 591 | } |
| 592 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 593 | /** |
| 594 | * xmlValidateNMToken: |
| 595 | * @value: the value to check |
| 596 | * @space: allow spaces in front and end of the string |
| 597 | * |
| 598 | * Check that a value conforms to the lexical space of NMToken |
| 599 | * |
| 600 | * Returns 0 if this validates, a positive error code number otherwise |
| 601 | * and -1 in case of internal or API error. |
| 602 | */ |
| 603 | int |
| 604 | xmlValidateNMToken(const xmlChar *value, int space) { |
| 605 | const xmlChar *cur = value; |
| 606 | int c,l; |
| 607 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 608 | if (value == NULL) |
| 609 | return(-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 610 | /* |
| 611 | * First quick algorithm for ASCII range |
| 612 | */ |
| 613 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 614 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 615 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 616 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 617 | ((*cur >= '0') && (*cur <= '9')) || |
| 618 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 619 | cur++; |
| 620 | else |
| 621 | goto try_complex; |
| 622 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 623 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 624 | ((*cur >= '0') && (*cur <= '9')) || |
| 625 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 626 | cur++; |
| 627 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 628 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 629 | if (*cur == 0) |
| 630 | return(0); |
| 631 | |
| 632 | try_complex: |
| 633 | /* |
| 634 | * Second check for chars outside the ASCII range |
| 635 | */ |
| 636 | cur = value; |
| 637 | c = CUR_SCHAR(cur, l); |
| 638 | if (space) { |
| 639 | while (IS_BLANK(c)) { |
| 640 | cur += l; |
| 641 | c = CUR_SCHAR(cur, l); |
| 642 | } |
| 643 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 644 | if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 645 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c))) |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 646 | return(1); |
| 647 | cur += l; |
| 648 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 649 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 650 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 651 | cur += l; |
| 652 | c = CUR_SCHAR(cur, l); |
| 653 | } |
| 654 | if (space) { |
| 655 | while (IS_BLANK(c)) { |
| 656 | cur += l; |
| 657 | c = CUR_SCHAR(cur, l); |
| 658 | } |
| 659 | } |
| 660 | if (c != 0) |
| 661 | return(1); |
| 662 | return(0); |
| 663 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 664 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 665 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 666 | /************************************************************************ |
| 667 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 668 | * Allocation and deallocation of basic structures * |
| 669 | * * |
| 670 | ************************************************************************/ |
| 671 | |
| 672 | /** |
| 673 | * xmlSetBufferAllocationScheme: |
| 674 | * @scheme: allocation method to use |
| 675 | * |
| 676 | * Set the buffer allocation method. Types are |
| 677 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 678 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 679 | * improves performance |
| 680 | */ |
| 681 | void |
| 682 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
| 683 | xmlBufferAllocScheme = scheme; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * xmlGetBufferAllocationScheme: |
| 688 | * |
| 689 | * Types are |
| 690 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 691 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 692 | * improves performance |
| 693 | * |
| 694 | * Returns the current allocation scheme |
| 695 | */ |
| 696 | xmlBufferAllocationScheme |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 697 | xmlGetBufferAllocationScheme(void) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 698 | return(xmlBufferAllocScheme); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | /** |
| 702 | * xmlNewNs: |
| 703 | * @node: the element carrying the namespace |
| 704 | * @href: the URI associated |
| 705 | * @prefix: the prefix for the namespace |
| 706 | * |
| 707 | * Creation of a new Namespace. This function will refuse to create |
| 708 | * a namespace with a similar prefix than an existing one present on this |
| 709 | * node. |
| 710 | * We use href==NULL in the case of an element creation where the namespace |
| 711 | * was not defined. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 712 | * Returns a new namespace pointer or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 713 | */ |
| 714 | xmlNsPtr |
| 715 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
| 716 | xmlNsPtr cur; |
| 717 | |
| 718 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 719 | return(NULL); |
| 720 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 721 | if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) |
| 722 | return(NULL); |
| 723 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 724 | /* |
| 725 | * Allocate a new Namespace and fill the fields. |
| 726 | */ |
| 727 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 728 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 729 | xmlTreeErrMemory("building namespace"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 730 | return(NULL); |
| 731 | } |
| 732 | memset(cur, 0, sizeof(xmlNs)); |
| 733 | cur->type = XML_LOCAL_NAMESPACE; |
| 734 | |
| 735 | if (href != NULL) |
| 736 | cur->href = xmlStrdup(href); |
| 737 | if (prefix != NULL) |
| 738 | cur->prefix = xmlStrdup(prefix); |
| 739 | |
| 740 | /* |
| 741 | * Add it at the end to preserve parsing order ... |
| 742 | * and checks for existing use of the prefix |
| 743 | */ |
| 744 | if (node != NULL) { |
| 745 | if (node->nsDef == NULL) { |
| 746 | node->nsDef = cur; |
| 747 | } else { |
| 748 | xmlNsPtr prev = node->nsDef; |
| 749 | |
| 750 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 751 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 752 | xmlFreeNs(cur); |
| 753 | return(NULL); |
| 754 | } |
| 755 | while (prev->next != NULL) { |
| 756 | prev = prev->next; |
| 757 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 758 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 759 | xmlFreeNs(cur); |
| 760 | return(NULL); |
| 761 | } |
| 762 | } |
| 763 | prev->next = cur; |
| 764 | } |
| 765 | } |
| 766 | return(cur); |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * xmlSetNs: |
| 771 | * @node: a node in the document |
| 772 | * @ns: a namespace pointer |
| 773 | * |
| 774 | * Associate a namespace to a node, a posteriori. |
| 775 | */ |
| 776 | void |
| 777 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
| 778 | if (node == NULL) { |
| 779 | #ifdef DEBUG_TREE |
| 780 | xmlGenericError(xmlGenericErrorContext, |
| 781 | "xmlSetNs: node == NULL\n"); |
| 782 | #endif |
| 783 | return; |
| 784 | } |
| 785 | node->ns = ns; |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * xmlFreeNs: |
| 790 | * @cur: the namespace pointer |
| 791 | * |
| 792 | * Free up the structures associated to a namespace |
| 793 | */ |
| 794 | void |
| 795 | xmlFreeNs(xmlNsPtr cur) { |
| 796 | if (cur == NULL) { |
| 797 | #ifdef DEBUG_TREE |
| 798 | xmlGenericError(xmlGenericErrorContext, |
| 799 | "xmlFreeNs : ns == NULL\n"); |
| 800 | #endif |
| 801 | return; |
| 802 | } |
| 803 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 804 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 805 | xmlFree(cur); |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * xmlFreeNsList: |
| 810 | * @cur: the first namespace pointer |
| 811 | * |
| 812 | * Free up all the structures associated to the chained namespaces. |
| 813 | */ |
| 814 | void |
| 815 | xmlFreeNsList(xmlNsPtr cur) { |
| 816 | xmlNsPtr next; |
| 817 | if (cur == NULL) { |
| 818 | #ifdef DEBUG_TREE |
| 819 | xmlGenericError(xmlGenericErrorContext, |
| 820 | "xmlFreeNsList : ns == NULL\n"); |
| 821 | #endif |
| 822 | return; |
| 823 | } |
| 824 | while (cur != NULL) { |
| 825 | next = cur->next; |
| 826 | xmlFreeNs(cur); |
| 827 | cur = next; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * xmlNewDtd: |
| 833 | * @doc: the document pointer |
| 834 | * @name: the DTD name |
| 835 | * @ExternalID: the external ID |
| 836 | * @SystemID: the system ID |
| 837 | * |
| 838 | * Creation of a new DTD for the external subset. To create an |
| 839 | * internal subset, use xmlCreateIntSubset(). |
| 840 | * |
| 841 | * Returns a pointer to the new DTD structure |
| 842 | */ |
| 843 | xmlDtdPtr |
| 844 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 845 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 846 | xmlDtdPtr cur; |
| 847 | |
| 848 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
| 849 | #ifdef DEBUG_TREE |
| 850 | xmlGenericError(xmlGenericErrorContext, |
| 851 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
| 852 | /* !!! */ (char *) name, doc->name, |
| 853 | /* !!! */ (char *)doc->extSubset->name); |
| 854 | #endif |
| 855 | return(NULL); |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * Allocate a new DTD and fill the fields. |
| 860 | */ |
| 861 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 862 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 863 | xmlTreeErrMemory("building DTD"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 864 | return(NULL); |
| 865 | } |
| 866 | memset(cur, 0 , sizeof(xmlDtd)); |
| 867 | cur->type = XML_DTD_NODE; |
| 868 | |
| 869 | if (name != NULL) |
| 870 | cur->name = xmlStrdup(name); |
| 871 | if (ExternalID != NULL) |
| 872 | cur->ExternalID = xmlStrdup(ExternalID); |
| 873 | if (SystemID != NULL) |
| 874 | cur->SystemID = xmlStrdup(SystemID); |
| 875 | if (doc != NULL) |
| 876 | doc->extSubset = cur; |
| 877 | cur->doc = doc; |
| 878 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 879 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 880 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 881 | return(cur); |
| 882 | } |
| 883 | |
| 884 | /** |
| 885 | * xmlGetIntSubset: |
| 886 | * @doc: the document pointer |
| 887 | * |
| 888 | * Get the internal subset of a document |
| 889 | * Returns a pointer to the DTD structure or NULL if not found |
| 890 | */ |
| 891 | |
| 892 | xmlDtdPtr |
| 893 | xmlGetIntSubset(xmlDocPtr doc) { |
| 894 | xmlNodePtr cur; |
| 895 | |
| 896 | if (doc == NULL) |
| 897 | return(NULL); |
| 898 | cur = doc->children; |
| 899 | while (cur != NULL) { |
| 900 | if (cur->type == XML_DTD_NODE) |
| 901 | return((xmlDtdPtr) cur); |
| 902 | cur = cur->next; |
| 903 | } |
| 904 | return((xmlDtdPtr) doc->intSubset); |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * xmlCreateIntSubset: |
| 909 | * @doc: the document pointer |
| 910 | * @name: the DTD name |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 911 | * @ExternalID: the external (PUBLIC) ID |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 912 | * @SystemID: the system ID |
| 913 | * |
| 914 | * Create the internal subset of a document |
| 915 | * Returns a pointer to the new DTD structure |
| 916 | */ |
| 917 | xmlDtdPtr |
| 918 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 919 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 920 | xmlDtdPtr cur; |
| 921 | |
| 922 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
| 923 | #ifdef DEBUG_TREE |
| 924 | xmlGenericError(xmlGenericErrorContext, |
| 925 | |
| 926 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 927 | doc->name); |
| 928 | #endif |
| 929 | return(NULL); |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Allocate a new DTD and fill the fields. |
| 934 | */ |
| 935 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 936 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 937 | xmlTreeErrMemory("building internal subset"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 938 | return(NULL); |
| 939 | } |
| 940 | memset(cur, 0, sizeof(xmlDtd)); |
| 941 | cur->type = XML_DTD_NODE; |
| 942 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 943 | if (name != NULL) { |
| 944 | cur->name = xmlStrdup(name); |
| 945 | if (cur->name == NULL) { |
| 946 | xmlTreeErrMemory("building internal subset"); |
| 947 | xmlFree(cur); |
| 948 | return(NULL); |
| 949 | } |
| 950 | } |
| 951 | if (ExternalID != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 952 | cur->ExternalID = xmlStrdup(ExternalID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 953 | if (cur->ExternalID == NULL) { |
| 954 | xmlTreeErrMemory("building internal subset"); |
| 955 | if (cur->name != NULL) |
| 956 | xmlFree((char *)cur->name); |
| 957 | xmlFree(cur); |
| 958 | return(NULL); |
| 959 | } |
| 960 | } |
| 961 | if (SystemID != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 962 | cur->SystemID = xmlStrdup(SystemID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 963 | if (cur->SystemID == NULL) { |
| 964 | xmlTreeErrMemory("building internal subset"); |
| 965 | if (cur->name != NULL) |
| 966 | xmlFree((char *)cur->name); |
| 967 | if (cur->ExternalID != NULL) |
| 968 | xmlFree((char *)cur->ExternalID); |
| 969 | xmlFree(cur); |
| 970 | return(NULL); |
| 971 | } |
| 972 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 973 | if (doc != NULL) { |
| 974 | doc->intSubset = cur; |
| 975 | cur->parent = doc; |
| 976 | cur->doc = doc; |
| 977 | if (doc->children == NULL) { |
| 978 | doc->children = (xmlNodePtr) cur; |
| 979 | doc->last = (xmlNodePtr) cur; |
| 980 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 981 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 982 | xmlNodePtr prev; |
| 983 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 984 | prev = doc->children; |
| 985 | prev->prev = (xmlNodePtr) cur; |
| 986 | cur->next = prev; |
| 987 | doc->children = (xmlNodePtr) cur; |
| 988 | } else { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 989 | xmlNodePtr next; |
| 990 | |
| 991 | next = doc->children; |
| 992 | while ((next != NULL) && (next->type != XML_ELEMENT_NODE)) |
| 993 | next = next->next; |
| 994 | if (next == NULL) { |
| 995 | cur->prev = doc->last; |
| 996 | cur->prev->next = (xmlNodePtr) cur; |
| 997 | cur->next = NULL; |
| 998 | doc->last = (xmlNodePtr) cur; |
| 999 | } else { |
| 1000 | cur->next = next; |
| 1001 | cur->prev = next->prev; |
| 1002 | if (cur->prev == NULL) |
| 1003 | doc->children = (xmlNodePtr) cur; |
| 1004 | else |
| 1005 | cur->prev->next = (xmlNodePtr) cur; |
| 1006 | next->prev = (xmlNodePtr) cur; |
| 1007 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1011 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1012 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1013 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1014 | return(cur); |
| 1015 | } |
| 1016 | |
| 1017 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1018 | * DICT_FREE: |
| 1019 | * @str: a string |
| 1020 | * |
| 1021 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 1022 | * current scope |
| 1023 | */ |
| 1024 | #define DICT_FREE(str) \ |
| 1025 | if ((str) && ((!dict) || \ |
| 1026 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 1027 | xmlFree((char *)(str)); |
| 1028 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 1029 | |
| 1030 | /** |
| 1031 | * DICT_COPY: |
| 1032 | * @str: a string |
| 1033 | * |
| 1034 | * Copy a string using a "dict" dictionnary in the current scope, |
| 1035 | * if availabe. |
| 1036 | */ |
| 1037 | #define DICT_COPY(str, cpy) \ |
| 1038 | if (str) { \ |
| 1039 | if (dict) { \ |
| 1040 | if (xmlDictOwns(dict, (const xmlChar *)(str))) \ |
| 1041 | cpy = (xmlChar *) (str); \ |
| 1042 | else \ |
| 1043 | cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \ |
| 1044 | } else \ |
| 1045 | cpy = xmlStrdup((const xmlChar *)(str)); } |
| 1046 | |
| 1047 | /** |
| 1048 | * DICT_CONST_COPY: |
| 1049 | * @str: a string |
| 1050 | * |
| 1051 | * Copy a string using a "dict" dictionnary in the current scope, |
| 1052 | * if availabe. |
| 1053 | */ |
| 1054 | #define DICT_CONST_COPY(str, cpy) \ |
| 1055 | if (str) { \ |
| 1056 | if (dict) { \ |
| 1057 | if (xmlDictOwns(dict, (const xmlChar *)(str))) \ |
| 1058 | cpy = (const xmlChar *) (str); \ |
| 1059 | else \ |
| 1060 | cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \ |
| 1061 | } else \ |
| 1062 | cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); } |
| 1063 | |
| 1064 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1065 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1066 | * xmlFreeDtd: |
| 1067 | * @cur: the DTD structure to free up |
| 1068 | * |
| 1069 | * Free a DTD structure. |
| 1070 | */ |
| 1071 | void |
| 1072 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1073 | xmlDictPtr dict = NULL; |
| 1074 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1075 | if (cur == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1076 | return; |
| 1077 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1078 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1079 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1080 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1081 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1082 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1083 | if (cur->children != NULL) { |
| 1084 | xmlNodePtr next, c = cur->children; |
| 1085 | |
| 1086 | /* |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1087 | * Cleanup all nodes which are not part of the specific lists |
| 1088 | * of notations, elements, attributes and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1089 | */ |
| 1090 | while (c != NULL) { |
| 1091 | next = c->next; |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1092 | if ((c->type != XML_NOTATION_NODE) && |
| 1093 | (c->type != XML_ELEMENT_DECL) && |
| 1094 | (c->type != XML_ATTRIBUTE_DECL) && |
| 1095 | (c->type != XML_ENTITY_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1096 | xmlUnlinkNode(c); |
| 1097 | xmlFreeNode(c); |
| 1098 | } |
| 1099 | c = next; |
| 1100 | } |
| 1101 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1102 | DICT_FREE(cur->name) |
| 1103 | DICT_FREE(cur->SystemID) |
| 1104 | DICT_FREE(cur->ExternalID) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1105 | /* TODO !!! */ |
| 1106 | if (cur->notations != NULL) |
| 1107 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
| 1108 | |
| 1109 | if (cur->elements != NULL) |
| 1110 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
| 1111 | if (cur->attributes != NULL) |
| 1112 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
| 1113 | if (cur->entities != NULL) |
| 1114 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 1115 | if (cur->pentities != NULL) |
| 1116 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
| 1117 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1118 | xmlFree(cur); |
| 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * xmlNewDoc: |
| 1123 | * @version: xmlChar string giving the version of XML "1.0" |
| 1124 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1125 | * Creates a new XML document |
| 1126 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1127 | * Returns a new document |
| 1128 | */ |
| 1129 | xmlDocPtr |
| 1130 | xmlNewDoc(const xmlChar *version) { |
| 1131 | xmlDocPtr cur; |
| 1132 | |
| 1133 | if (version == NULL) |
| 1134 | version = (const xmlChar *) "1.0"; |
| 1135 | |
| 1136 | /* |
| 1137 | * Allocate a new document and fill the fields. |
| 1138 | */ |
| 1139 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
| 1140 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1141 | xmlTreeErrMemory("building doc"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1142 | return(NULL); |
| 1143 | } |
| 1144 | memset(cur, 0, sizeof(xmlDoc)); |
| 1145 | cur->type = XML_DOCUMENT_NODE; |
| 1146 | |
| 1147 | cur->version = xmlStrdup(version); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 1148 | if (cur->version == NULL) { |
| 1149 | xmlTreeErrMemory("building doc"); |
| 1150 | xmlFree(cur); |
| 1151 | return(NULL); |
| 1152 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1153 | cur->standalone = -1; |
| 1154 | cur->compression = -1; /* not initialized */ |
| 1155 | cur->doc = cur; |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 1156 | /* |
| 1157 | * The in memory encoding is always UTF8 |
| 1158 | * This field will never change and would |
| 1159 | * be obsolete if not for binary compatibility. |
| 1160 | */ |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 1161 | cur->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1162 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1163 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1164 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1165 | return(cur); |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * xmlFreeDoc: |
| 1170 | * @cur: pointer to the document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1171 | * |
| 1172 | * Free up all the structures used by a document, tree included. |
| 1173 | */ |
| 1174 | void |
| 1175 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1176 | xmlDtdPtr extSubset, intSubset; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1177 | xmlDictPtr dict = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1178 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1179 | if (cur == NULL) { |
| 1180 | #ifdef DEBUG_TREE |
| 1181 | xmlGenericError(xmlGenericErrorContext, |
| 1182 | "xmlFreeDoc : document == NULL\n"); |
| 1183 | #endif |
| 1184 | return; |
| 1185 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1186 | #ifdef LIBXML_DEBUG_RUNTIME |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1187 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1188 | xmlDebugCheckDocument(stderr, cur); |
| 1189 | #endif |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1190 | #endif |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1191 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1192 | if (cur != NULL) dict = cur->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1193 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1194 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1195 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1196 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1197 | /* |
| 1198 | * Do this before freeing the children list to avoid ID lookups |
| 1199 | */ |
| 1200 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
| 1201 | cur->ids = NULL; |
| 1202 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
| 1203 | cur->refs = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1204 | extSubset = cur->extSubset; |
| 1205 | intSubset = cur->intSubset; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 1206 | if (intSubset == extSubset) |
| 1207 | extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1208 | if (extSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1209 | xmlUnlinkNode((xmlNodePtr) cur->extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1210 | cur->extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1211 | xmlFreeDtd(extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1212 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1213 | if (intSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1214 | xmlUnlinkNode((xmlNodePtr) cur->intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1215 | cur->intSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1216 | xmlFreeDtd(intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1220 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1221 | |
| 1222 | DICT_FREE(cur->version) |
| 1223 | DICT_FREE(cur->name) |
| 1224 | DICT_FREE(cur->encoding) |
| 1225 | DICT_FREE(cur->URL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1226 | xmlFree(cur); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1227 | if (dict) xmlDictFree(dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | /** |
| 1231 | * xmlStringLenGetNodeList: |
| 1232 | * @doc: the document |
| 1233 | * @value: the value of the text |
| 1234 | * @len: the length of the string value |
| 1235 | * |
| 1236 | * Parse the value string and build the node list associated. Should |
| 1237 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1238 | * Returns a pointer to the first child |
| 1239 | */ |
| 1240 | xmlNodePtr |
| 1241 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
| 1242 | xmlNodePtr ret = NULL, last = NULL; |
| 1243 | xmlNodePtr node; |
| 1244 | xmlChar *val; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1245 | const xmlChar *cur = value, *end = cur + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1246 | const xmlChar *q; |
| 1247 | xmlEntityPtr ent; |
| 1248 | |
| 1249 | if (value == NULL) return(NULL); |
| 1250 | |
| 1251 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1252 | while ((cur < end) && (*cur != 0)) { |
| 1253 | if (cur[0] == '&') { |
| 1254 | int charval = 0; |
| 1255 | xmlChar tmp; |
| 1256 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1257 | /* |
| 1258 | * Save the current text. |
| 1259 | */ |
| 1260 | if (cur != q) { |
| 1261 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1262 | xmlNodeAddContentLen(last, q, cur - q); |
| 1263 | } else { |
| 1264 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1265 | if (node == NULL) return(ret); |
| 1266 | if (last == NULL) |
| 1267 | last = ret = node; |
| 1268 | else { |
| 1269 | last->next = node; |
| 1270 | node->prev = last; |
| 1271 | last = node; |
| 1272 | } |
| 1273 | } |
| 1274 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1275 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1276 | if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) { |
| 1277 | cur += 3; |
| 1278 | if (cur < end) |
| 1279 | tmp = *cur; |
| 1280 | else |
| 1281 | tmp = 0; |
| 1282 | while (tmp != ';') { /* Non input consuming loop */ |
| 1283 | if ((tmp >= '0') && (tmp <= '9')) |
| 1284 | charval = charval * 16 + (tmp - '0'); |
| 1285 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1286 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1287 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1288 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1289 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1290 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1291 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1292 | charval = 0; |
| 1293 | break; |
| 1294 | } |
| 1295 | cur++; |
| 1296 | if (cur < end) |
| 1297 | tmp = *cur; |
| 1298 | else |
| 1299 | tmp = 0; |
| 1300 | } |
| 1301 | if (tmp == ';') |
| 1302 | cur++; |
| 1303 | q = cur; |
| 1304 | } else if ((cur + 1 < end) && (cur[1] == '#')) { |
| 1305 | cur += 2; |
| 1306 | if (cur < end) |
| 1307 | tmp = *cur; |
| 1308 | else |
| 1309 | tmp = 0; |
| 1310 | while (tmp != ';') { /* Non input consuming loops */ |
| 1311 | if ((tmp >= '0') && (tmp <= '9')) |
| 1312 | charval = charval * 10 + (tmp - '0'); |
| 1313 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1314 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1315 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1316 | charval = 0; |
| 1317 | break; |
| 1318 | } |
| 1319 | cur++; |
| 1320 | if (cur < end) |
| 1321 | tmp = *cur; |
| 1322 | else |
| 1323 | tmp = 0; |
| 1324 | } |
| 1325 | if (tmp == ';') |
| 1326 | cur++; |
| 1327 | q = cur; |
| 1328 | } else { |
| 1329 | /* |
| 1330 | * Read the entity string |
| 1331 | */ |
| 1332 | cur++; |
| 1333 | q = cur; |
| 1334 | while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++; |
| 1335 | if ((cur >= end) || (*cur == 0)) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1336 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc, |
| 1337 | (const char *) q); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1338 | return(ret); |
| 1339 | } |
| 1340 | if (cur != q) { |
| 1341 | /* |
| 1342 | * Predefined entities don't generate nodes |
| 1343 | */ |
| 1344 | val = xmlStrndup(q, cur - q); |
| 1345 | ent = xmlGetDocEntity(doc, val); |
| 1346 | if ((ent != NULL) && |
| 1347 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1348 | if (last == NULL) { |
| 1349 | node = xmlNewDocText(doc, ent->content); |
| 1350 | last = ret = node; |
| 1351 | } else if (last->type != XML_TEXT_NODE) { |
| 1352 | node = xmlNewDocText(doc, ent->content); |
| 1353 | last = xmlAddNextSibling(last, node); |
| 1354 | } else |
| 1355 | xmlNodeAddContent(last, ent->content); |
| 1356 | |
| 1357 | } else { |
| 1358 | /* |
| 1359 | * Create a new REFERENCE_REF node |
| 1360 | */ |
| 1361 | node = xmlNewReference(doc, val); |
| 1362 | if (node == NULL) { |
| 1363 | if (val != NULL) xmlFree(val); |
| 1364 | return(ret); |
| 1365 | } |
| 1366 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1367 | xmlNodePtr temp; |
| 1368 | |
| 1369 | ent->children = xmlStringGetNodeList(doc, |
| 1370 | (const xmlChar*)node->content); |
| 1371 | ent->owner = 1; |
| 1372 | temp = ent->children; |
| 1373 | while (temp) { |
| 1374 | temp->parent = (xmlNodePtr)ent; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1375 | ent->last = temp; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1376 | temp = temp->next; |
| 1377 | } |
| 1378 | } |
| 1379 | if (last == NULL) { |
| 1380 | last = ret = node; |
| 1381 | } else { |
| 1382 | last = xmlAddNextSibling(last, node); |
| 1383 | } |
| 1384 | } |
| 1385 | xmlFree(val); |
| 1386 | } |
| 1387 | cur++; |
| 1388 | q = cur; |
| 1389 | } |
| 1390 | if (charval != 0) { |
| 1391 | xmlChar buf[10]; |
| 1392 | int l; |
| 1393 | |
| 1394 | l = xmlCopyCharMultiByte(buf, charval); |
| 1395 | buf[l] = 0; |
| 1396 | node = xmlNewDocText(doc, buf); |
| 1397 | if (node != NULL) { |
| 1398 | if (last == NULL) { |
| 1399 | last = ret = node; |
| 1400 | } else { |
| 1401 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1402 | } |
| 1403 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1404 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1405 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1406 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1407 | cur++; |
| 1408 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1409 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1410 | /* |
| 1411 | * Handle the last piece of text. |
| 1412 | */ |
| 1413 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1414 | xmlNodeAddContentLen(last, q, cur - q); |
| 1415 | } else { |
| 1416 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1417 | if (node == NULL) return(ret); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1418 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1419 | last = ret = node; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1420 | } else { |
| 1421 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | return(ret); |
| 1426 | } |
| 1427 | |
| 1428 | /** |
| 1429 | * xmlStringGetNodeList: |
| 1430 | * @doc: the document |
| 1431 | * @value: the value of the attribute |
| 1432 | * |
| 1433 | * Parse the value string and build the node list associated. Should |
| 1434 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1435 | * Returns a pointer to the first child |
| 1436 | */ |
| 1437 | xmlNodePtr |
| 1438 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
| 1439 | xmlNodePtr ret = NULL, last = NULL; |
| 1440 | xmlNodePtr node; |
| 1441 | xmlChar *val; |
| 1442 | const xmlChar *cur = value; |
| 1443 | const xmlChar *q; |
| 1444 | xmlEntityPtr ent; |
| 1445 | |
| 1446 | if (value == NULL) return(NULL); |
| 1447 | |
| 1448 | q = cur; |
| 1449 | while (*cur != 0) { |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1450 | if (cur[0] == '&') { |
| 1451 | int charval = 0; |
| 1452 | xmlChar tmp; |
| 1453 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1454 | /* |
| 1455 | * Save the current text. |
| 1456 | */ |
| 1457 | if (cur != q) { |
| 1458 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1459 | xmlNodeAddContentLen(last, q, cur - q); |
| 1460 | } else { |
| 1461 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1462 | if (node == NULL) return(ret); |
| 1463 | if (last == NULL) |
| 1464 | last = ret = node; |
| 1465 | else { |
| 1466 | last->next = node; |
| 1467 | node->prev = last; |
| 1468 | last = node; |
| 1469 | } |
| 1470 | } |
| 1471 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1472 | q = cur; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1473 | if ((cur[1] == '#') && (cur[2] == 'x')) { |
| 1474 | cur += 3; |
| 1475 | tmp = *cur; |
| 1476 | while (tmp != ';') { /* Non input consuming loop */ |
| 1477 | if ((tmp >= '0') && (tmp <= '9')) |
| 1478 | charval = charval * 16 + (tmp - '0'); |
| 1479 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1480 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1481 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1482 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1483 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1484 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1485 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1486 | charval = 0; |
| 1487 | break; |
| 1488 | } |
| 1489 | cur++; |
| 1490 | tmp = *cur; |
| 1491 | } |
| 1492 | if (tmp == ';') |
| 1493 | cur++; |
| 1494 | q = cur; |
| 1495 | } else if (cur[1] == '#') { |
| 1496 | cur += 2; |
| 1497 | tmp = *cur; |
| 1498 | while (tmp != ';') { /* Non input consuming loops */ |
| 1499 | if ((tmp >= '0') && (tmp <= '9')) |
| 1500 | charval = charval * 10 + (tmp - '0'); |
| 1501 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1502 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1503 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1504 | charval = 0; |
| 1505 | break; |
| 1506 | } |
| 1507 | cur++; |
| 1508 | tmp = *cur; |
| 1509 | } |
| 1510 | if (tmp == ';') |
| 1511 | cur++; |
| 1512 | q = cur; |
| 1513 | } else { |
| 1514 | /* |
| 1515 | * Read the entity string |
| 1516 | */ |
| 1517 | cur++; |
| 1518 | q = cur; |
| 1519 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 1520 | if (*cur == 0) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1521 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, |
| 1522 | (xmlNodePtr) doc, (const char *) q); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1523 | return(ret); |
| 1524 | } |
| 1525 | if (cur != q) { |
| 1526 | /* |
| 1527 | * Predefined entities don't generate nodes |
| 1528 | */ |
| 1529 | val = xmlStrndup(q, cur - q); |
| 1530 | ent = xmlGetDocEntity(doc, val); |
| 1531 | if ((ent != NULL) && |
| 1532 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1533 | if (last == NULL) { |
| 1534 | node = xmlNewDocText(doc, ent->content); |
| 1535 | last = ret = node; |
Daniel Veillard | 6f42c13 | 2002-01-06 23:05:13 +0000 | [diff] [blame] | 1536 | } else if (last->type != XML_TEXT_NODE) { |
| 1537 | node = xmlNewDocText(doc, ent->content); |
| 1538 | last = xmlAddNextSibling(last, node); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1539 | } else |
| 1540 | xmlNodeAddContent(last, ent->content); |
| 1541 | |
| 1542 | } else { |
| 1543 | /* |
| 1544 | * Create a new REFERENCE_REF node |
| 1545 | */ |
| 1546 | node = xmlNewReference(doc, val); |
| 1547 | if (node == NULL) { |
| 1548 | if (val != NULL) xmlFree(val); |
| 1549 | return(ret); |
| 1550 | } |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1551 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1552 | xmlNodePtr temp; |
| 1553 | |
| 1554 | ent->children = xmlStringGetNodeList(doc, |
| 1555 | (const xmlChar*)node->content); |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 1556 | ent->owner = 1; |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1557 | temp = ent->children; |
| 1558 | while (temp) { |
| 1559 | temp->parent = (xmlNodePtr)ent; |
| 1560 | temp = temp->next; |
| 1561 | } |
| 1562 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1563 | if (last == NULL) { |
| 1564 | last = ret = node; |
| 1565 | } else { |
| 1566 | last = xmlAddNextSibling(last, node); |
| 1567 | } |
| 1568 | } |
| 1569 | xmlFree(val); |
| 1570 | } |
| 1571 | cur++; |
| 1572 | q = cur; |
| 1573 | } |
| 1574 | if (charval != 0) { |
| 1575 | xmlChar buf[10]; |
| 1576 | int len; |
| 1577 | |
| 1578 | len = xmlCopyCharMultiByte(buf, charval); |
| 1579 | buf[len] = 0; |
| 1580 | node = xmlNewDocText(doc, buf); |
| 1581 | if (node != NULL) { |
| 1582 | if (last == NULL) { |
| 1583 | last = ret = node; |
| 1584 | } else { |
| 1585 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1586 | } |
| 1587 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1588 | |
| 1589 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1590 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1591 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1592 | cur++; |
| 1593 | } |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 1594 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1595 | /* |
| 1596 | * Handle the last piece of text. |
| 1597 | */ |
| 1598 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1599 | xmlNodeAddContentLen(last, q, cur - q); |
| 1600 | } else { |
| 1601 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1602 | if (node == NULL) return(ret); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1603 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1604 | last = ret = node; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1605 | } else { |
| 1606 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | return(ret); |
| 1611 | } |
| 1612 | |
| 1613 | /** |
| 1614 | * xmlNodeListGetString: |
| 1615 | * @doc: the document |
| 1616 | * @list: a Node list |
| 1617 | * @inLine: should we replace entity contents or show their external form |
| 1618 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1619 | * Build the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1620 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1621 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1622 | * 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] | 1623 | */ |
| 1624 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1625 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1626 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1627 | xmlNodePtr node = list; |
| 1628 | xmlChar *ret = NULL; |
| 1629 | xmlEntityPtr ent; |
| 1630 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1631 | if (list == NULL) |
| 1632 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1633 | |
| 1634 | while (node != NULL) { |
| 1635 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1636 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1637 | if (inLine) { |
| 1638 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1639 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1640 | xmlChar *buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1641 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1642 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
| 1643 | if (buffer != NULL) { |
| 1644 | ret = xmlStrcat(ret, buffer); |
| 1645 | xmlFree(buffer); |
| 1646 | } |
| 1647 | } |
| 1648 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1649 | if (inLine) { |
| 1650 | ent = xmlGetDocEntity(doc, node->name); |
| 1651 | if (ent != NULL) { |
| 1652 | xmlChar *buffer; |
| 1653 | |
| 1654 | /* an entity content can be any "well balanced chunk", |
| 1655 | * i.e. the result of the content [43] production: |
| 1656 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1657 | * So it can contain text, CDATA section or nested |
| 1658 | * entity reference nodes (among others). |
| 1659 | * -> we recursive call xmlNodeListGetString() |
| 1660 | * which handles these types */ |
| 1661 | buffer = xmlNodeListGetString(doc, ent->children, 1); |
| 1662 | if (buffer != NULL) { |
| 1663 | ret = xmlStrcat(ret, buffer); |
| 1664 | xmlFree(buffer); |
| 1665 | } |
| 1666 | } else { |
| 1667 | ret = xmlStrcat(ret, node->content); |
| 1668 | } |
| 1669 | } else { |
| 1670 | xmlChar buf[2]; |
| 1671 | |
| 1672 | buf[0] = '&'; |
| 1673 | buf[1] = 0; |
| 1674 | ret = xmlStrncat(ret, buf, 1); |
| 1675 | ret = xmlStrcat(ret, node->name); |
| 1676 | buf[0] = ';'; |
| 1677 | buf[1] = 0; |
| 1678 | ret = xmlStrncat(ret, buf, 1); |
| 1679 | } |
| 1680 | } |
| 1681 | #if 0 |
| 1682 | else { |
| 1683 | xmlGenericError(xmlGenericErrorContext, |
| 1684 | "xmlGetNodeListString : invalid node type %d\n", |
| 1685 | node->type); |
| 1686 | } |
| 1687 | #endif |
| 1688 | node = node->next; |
| 1689 | } |
| 1690 | return (ret); |
| 1691 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1692 | |
| 1693 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1694 | /** |
| 1695 | * xmlNodeListGetRawString: |
| 1696 | * @doc: the document |
| 1697 | * @list: a Node list |
| 1698 | * @inLine: should we replace entity contents or show their external form |
| 1699 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1700 | * Builds the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1701 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 1702 | * this function doesn't do any character encoding handling. |
| 1703 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1704 | * 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] | 1705 | */ |
| 1706 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1707 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1708 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1709 | xmlNodePtr node = list; |
| 1710 | xmlChar *ret = NULL; |
| 1711 | xmlEntityPtr ent; |
| 1712 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1713 | if (list == NULL) |
| 1714 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1715 | |
| 1716 | while (node != NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1717 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1718 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1719 | if (inLine) { |
| 1720 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1721 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1722 | xmlChar *buffer; |
| 1723 | |
| 1724 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 1725 | if (buffer != NULL) { |
| 1726 | ret = xmlStrcat(ret, buffer); |
| 1727 | xmlFree(buffer); |
| 1728 | } |
| 1729 | } |
| 1730 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1731 | if (inLine) { |
| 1732 | ent = xmlGetDocEntity(doc, node->name); |
| 1733 | if (ent != NULL) { |
| 1734 | xmlChar *buffer; |
| 1735 | |
| 1736 | /* an entity content can be any "well balanced chunk", |
| 1737 | * i.e. the result of the content [43] production: |
| 1738 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1739 | * So it can contain text, CDATA section or nested |
| 1740 | * entity reference nodes (among others). |
| 1741 | * -> we recursive call xmlNodeListGetRawString() |
| 1742 | * which handles these types */ |
| 1743 | buffer = |
| 1744 | xmlNodeListGetRawString(doc, ent->children, 1); |
| 1745 | if (buffer != NULL) { |
| 1746 | ret = xmlStrcat(ret, buffer); |
| 1747 | xmlFree(buffer); |
| 1748 | } |
| 1749 | } else { |
| 1750 | ret = xmlStrcat(ret, node->content); |
| 1751 | } |
| 1752 | } else { |
| 1753 | xmlChar buf[2]; |
| 1754 | |
| 1755 | buf[0] = '&'; |
| 1756 | buf[1] = 0; |
| 1757 | ret = xmlStrncat(ret, buf, 1); |
| 1758 | ret = xmlStrcat(ret, node->name); |
| 1759 | buf[0] = ';'; |
| 1760 | buf[1] = 0; |
| 1761 | ret = xmlStrncat(ret, buf, 1); |
| 1762 | } |
| 1763 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1764 | #if 0 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1765 | else { |
| 1766 | xmlGenericError(xmlGenericErrorContext, |
| 1767 | "xmlGetNodeListString : invalid node type %d\n", |
| 1768 | node->type); |
| 1769 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1770 | #endif |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1771 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1772 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1773 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1774 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1775 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1776 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1777 | static xmlAttrPtr |
| 1778 | xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns, |
| 1779 | const xmlChar * name, const xmlChar * value, |
| 1780 | int eatname) |
| 1781 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1782 | xmlAttrPtr cur; |
| 1783 | xmlDocPtr doc = NULL; |
| 1784 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1785 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1786 | if (eatname == 1) |
| 1787 | xmlFree((xmlChar *) name); |
| 1788 | return (NULL); |
| 1789 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1790 | |
| 1791 | /* |
| 1792 | * Allocate a new property and fill the fields. |
| 1793 | */ |
| 1794 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1795 | if (cur == NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1796 | if (eatname == 1) |
| 1797 | xmlFree((xmlChar *) name); |
| 1798 | xmlTreeErrMemory("building attribute"); |
| 1799 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1800 | } |
| 1801 | memset(cur, 0, sizeof(xmlAttr)); |
| 1802 | cur->type = XML_ATTRIBUTE_NODE; |
| 1803 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1804 | cur->parent = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1805 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1806 | doc = node->doc; |
| 1807 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1808 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1809 | cur->ns = ns; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1810 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1811 | if (eatname == 0) { |
| 1812 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1813 | cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1); |
| 1814 | else |
| 1815 | cur->name = xmlStrdup(name); |
| 1816 | } else |
| 1817 | cur->name = name; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1818 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1819 | if (value != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1820 | xmlNodePtr tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1821 | |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame^] | 1822 | if(!xmlCheckUTF8(value)) { |
| 1823 | xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc, |
| 1824 | NULL); |
| 1825 | if (doc != NULL) |
| 1826 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 1827 | } |
| 1828 | cur->children = xmlNewDocText(doc, value); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1829 | cur->last = NULL; |
| 1830 | tmp = cur->children; |
| 1831 | while (tmp != NULL) { |
| 1832 | tmp->parent = (xmlNodePtr) cur; |
| 1833 | if (tmp->next == NULL) |
| 1834 | cur->last = tmp; |
| 1835 | tmp = tmp->next; |
| 1836 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1837 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1838 | |
| 1839 | /* |
| 1840 | * Add it at the end to preserve parsing order ... |
| 1841 | */ |
| 1842 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1843 | if (node->properties == NULL) { |
| 1844 | node->properties = cur; |
| 1845 | } else { |
| 1846 | xmlAttrPtr prev = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1847 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1848 | while (prev->next != NULL) |
| 1849 | prev = prev->next; |
| 1850 | prev->next = cur; |
| 1851 | cur->prev = prev; |
| 1852 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1853 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1854 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1855 | if (xmlIsID((node == NULL) ? NULL : node->doc, node, cur) == 1) |
| 1856 | xmlAddID(NULL, node->doc, value, cur); |
| 1857 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1858 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1859 | xmlRegisterNodeDefaultValue((xmlNodePtr) cur); |
| 1860 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1861 | } |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1862 | |
| 1863 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 1864 | defined(LIBXML_SCHEMAS_ENABLED) |
| 1865 | /** |
| 1866 | * xmlNewProp: |
| 1867 | * @node: the holding node |
| 1868 | * @name: the name of the attribute |
| 1869 | * @value: the value of the attribute |
| 1870 | * |
| 1871 | * Create a new property carried by a node. |
| 1872 | * Returns a pointer to the attribute |
| 1873 | */ |
| 1874 | xmlAttrPtr |
| 1875 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 1876 | |
| 1877 | if (name == NULL) { |
| 1878 | #ifdef DEBUG_TREE |
| 1879 | xmlGenericError(xmlGenericErrorContext, |
| 1880 | "xmlNewProp : name == NULL\n"); |
| 1881 | #endif |
| 1882 | return(NULL); |
| 1883 | } |
| 1884 | |
| 1885 | return xmlNewPropInternal(node, NULL, name, value, 0); |
| 1886 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1887 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1888 | |
| 1889 | /** |
| 1890 | * xmlNewNsProp: |
| 1891 | * @node: the holding node |
| 1892 | * @ns: the namespace |
| 1893 | * @name: the name of the attribute |
| 1894 | * @value: the value of the attribute |
| 1895 | * |
| 1896 | * Create a new property tagged with a namespace and carried by a node. |
| 1897 | * Returns a pointer to the attribute |
| 1898 | */ |
| 1899 | xmlAttrPtr |
| 1900 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 1901 | const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1902 | |
| 1903 | if (name == NULL) { |
| 1904 | #ifdef DEBUG_TREE |
| 1905 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1906 | "xmlNewNsProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1907 | #endif |
| 1908 | return(NULL); |
| 1909 | } |
| 1910 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1911 | return xmlNewPropInternal(node, ns, name, value, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1915 | * xmlNewNsPropEatName: |
| 1916 | * @node: the holding node |
| 1917 | * @ns: the namespace |
| 1918 | * @name: the name of the attribute |
| 1919 | * @value: the value of the attribute |
| 1920 | * |
| 1921 | * Create a new property tagged with a namespace and carried by a node. |
| 1922 | * Returns a pointer to the attribute |
| 1923 | */ |
| 1924 | xmlAttrPtr |
| 1925 | xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name, |
| 1926 | const xmlChar *value) { |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1927 | |
| 1928 | if (name == NULL) { |
| 1929 | #ifdef DEBUG_TREE |
| 1930 | xmlGenericError(xmlGenericErrorContext, |
| 1931 | "xmlNewNsPropEatName : name == NULL\n"); |
| 1932 | #endif |
| 1933 | return(NULL); |
| 1934 | } |
| 1935 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1936 | return xmlNewPropInternal(node, ns, name, value, 1); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1940 | * xmlNewDocProp: |
| 1941 | * @doc: the document |
| 1942 | * @name: the name of the attribute |
| 1943 | * @value: the value of the attribute |
| 1944 | * |
| 1945 | * Create a new property carried by a document. |
| 1946 | * Returns a pointer to the attribute |
| 1947 | */ |
| 1948 | xmlAttrPtr |
| 1949 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
| 1950 | xmlAttrPtr cur; |
| 1951 | |
| 1952 | if (name == NULL) { |
| 1953 | #ifdef DEBUG_TREE |
| 1954 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1955 | "xmlNewDocProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1956 | #endif |
| 1957 | return(NULL); |
| 1958 | } |
| 1959 | |
| 1960 | /* |
| 1961 | * Allocate a new property and fill the fields. |
| 1962 | */ |
| 1963 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1964 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1965 | xmlTreeErrMemory("building attribute"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1966 | return(NULL); |
| 1967 | } |
| 1968 | memset(cur, 0, sizeof(xmlAttr)); |
| 1969 | cur->type = XML_ATTRIBUTE_NODE; |
| 1970 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 1971 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1972 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 1973 | else |
| 1974 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1975 | cur->doc = doc; |
| 1976 | if (value != NULL) { |
| 1977 | xmlNodePtr tmp; |
| 1978 | |
| 1979 | cur->children = xmlStringGetNodeList(doc, value); |
| 1980 | cur->last = NULL; |
| 1981 | |
| 1982 | tmp = cur->children; |
| 1983 | while (tmp != NULL) { |
| 1984 | tmp->parent = (xmlNodePtr) cur; |
| 1985 | if (tmp->next == NULL) |
| 1986 | cur->last = tmp; |
| 1987 | tmp = tmp->next; |
| 1988 | } |
| 1989 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1990 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1991 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1992 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1993 | return(cur); |
| 1994 | } |
| 1995 | |
| 1996 | /** |
| 1997 | * xmlFreePropList: |
| 1998 | * @cur: the first property in the list |
| 1999 | * |
| 2000 | * Free a property and all its siblings, all the children are freed too. |
| 2001 | */ |
| 2002 | void |
| 2003 | xmlFreePropList(xmlAttrPtr cur) { |
| 2004 | xmlAttrPtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2005 | if (cur == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2006 | while (cur != NULL) { |
| 2007 | next = cur->next; |
| 2008 | xmlFreeProp(cur); |
| 2009 | cur = next; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | /** |
| 2014 | * xmlFreeProp: |
| 2015 | * @cur: an attribute |
| 2016 | * |
| 2017 | * Free one attribute, all the content is freed too |
| 2018 | */ |
| 2019 | void |
| 2020 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2021 | xmlDictPtr dict = NULL; |
| 2022 | if (cur == NULL) return; |
| 2023 | |
| 2024 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2025 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2026 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2027 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 2028 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2029 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | da6f4af | 2005-06-20 17:17:54 +0000 | [diff] [blame] | 2030 | if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) { |
| 2031 | xmlRemoveID(cur->doc, cur); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2032 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2033 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2034 | DICT_FREE(cur->name) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2035 | xmlFree(cur); |
| 2036 | } |
| 2037 | |
| 2038 | /** |
| 2039 | * xmlRemoveProp: |
| 2040 | * @cur: an attribute |
| 2041 | * |
| 2042 | * Unlink and free one attribute, all the content is freed too |
| 2043 | * Note this doesn't work for namespace definition attributes |
| 2044 | * |
| 2045 | * Returns 0 if success and -1 in case of error. |
| 2046 | */ |
| 2047 | int |
| 2048 | xmlRemoveProp(xmlAttrPtr cur) { |
| 2049 | xmlAttrPtr tmp; |
| 2050 | if (cur == NULL) { |
| 2051 | #ifdef DEBUG_TREE |
| 2052 | xmlGenericError(xmlGenericErrorContext, |
| 2053 | "xmlRemoveProp : cur == NULL\n"); |
| 2054 | #endif |
| 2055 | return(-1); |
| 2056 | } |
| 2057 | if (cur->parent == NULL) { |
| 2058 | #ifdef DEBUG_TREE |
| 2059 | xmlGenericError(xmlGenericErrorContext, |
| 2060 | "xmlRemoveProp : cur->parent == NULL\n"); |
| 2061 | #endif |
| 2062 | return(-1); |
| 2063 | } |
| 2064 | tmp = cur->parent->properties; |
| 2065 | if (tmp == cur) { |
| 2066 | cur->parent->properties = cur->next; |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2067 | if (cur->next != NULL) |
| 2068 | cur->next->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2069 | xmlFreeProp(cur); |
| 2070 | return(0); |
| 2071 | } |
| 2072 | while (tmp != NULL) { |
| 2073 | if (tmp->next == cur) { |
| 2074 | tmp->next = cur->next; |
| 2075 | if (tmp->next != NULL) |
| 2076 | tmp->next->prev = tmp; |
| 2077 | xmlFreeProp(cur); |
| 2078 | return(0); |
| 2079 | } |
| 2080 | tmp = tmp->next; |
| 2081 | } |
| 2082 | #ifdef DEBUG_TREE |
| 2083 | xmlGenericError(xmlGenericErrorContext, |
| 2084 | "xmlRemoveProp : attribute not owned by its node\n"); |
| 2085 | #endif |
| 2086 | return(-1); |
| 2087 | } |
| 2088 | |
| 2089 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2090 | * xmlNewDocPI: |
| 2091 | * @doc: the target document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2092 | * @name: the processing instruction name |
| 2093 | * @content: the PI content |
| 2094 | * |
| 2095 | * Creation of a processing instruction element. |
| 2096 | * Returns a pointer to the new node object. |
| 2097 | */ |
| 2098 | xmlNodePtr |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2099 | xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2100 | xmlNodePtr cur; |
| 2101 | |
| 2102 | if (name == NULL) { |
| 2103 | #ifdef DEBUG_TREE |
| 2104 | xmlGenericError(xmlGenericErrorContext, |
| 2105 | "xmlNewPI : name == NULL\n"); |
| 2106 | #endif |
| 2107 | return(NULL); |
| 2108 | } |
| 2109 | |
| 2110 | /* |
| 2111 | * Allocate a new node and fill the fields. |
| 2112 | */ |
| 2113 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2114 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2115 | xmlTreeErrMemory("building PI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2116 | return(NULL); |
| 2117 | } |
| 2118 | memset(cur, 0, sizeof(xmlNode)); |
| 2119 | cur->type = XML_PI_NODE; |
| 2120 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2121 | if ((doc != NULL) && (doc->dict != NULL)) |
| 2122 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 2123 | else |
| 2124 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2125 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2126 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2127 | } |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 2128 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2129 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2130 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2131 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2132 | return(cur); |
| 2133 | } |
| 2134 | |
| 2135 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2136 | * xmlNewPI: |
| 2137 | * @name: the processing instruction name |
| 2138 | * @content: the PI content |
| 2139 | * |
| 2140 | * Creation of a processing instruction element. |
| 2141 | * Use xmlDocNewPI preferably to get string interning |
| 2142 | * |
| 2143 | * Returns a pointer to the new node object. |
| 2144 | */ |
| 2145 | xmlNodePtr |
| 2146 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
| 2147 | return(xmlNewDocPI(NULL, name, content)); |
| 2148 | } |
| 2149 | |
| 2150 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2151 | * xmlNewNode: |
| 2152 | * @ns: namespace if any |
| 2153 | * @name: the node name |
| 2154 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2155 | * Creation of a new node element. @ns is optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2156 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2157 | * Returns a pointer to the new node object. Uses xmlStrdup() to make |
| 2158 | * copy of @name. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2159 | */ |
| 2160 | xmlNodePtr |
| 2161 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 2162 | xmlNodePtr cur; |
| 2163 | |
| 2164 | if (name == NULL) { |
| 2165 | #ifdef DEBUG_TREE |
| 2166 | xmlGenericError(xmlGenericErrorContext, |
| 2167 | "xmlNewNode : name == NULL\n"); |
| 2168 | #endif |
| 2169 | return(NULL); |
| 2170 | } |
| 2171 | |
| 2172 | /* |
| 2173 | * Allocate a new node and fill the fields. |
| 2174 | */ |
| 2175 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2176 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2177 | xmlTreeErrMemory("building node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2178 | return(NULL); |
| 2179 | } |
| 2180 | memset(cur, 0, sizeof(xmlNode)); |
| 2181 | cur->type = XML_ELEMENT_NODE; |
| 2182 | |
| 2183 | cur->name = xmlStrdup(name); |
| 2184 | cur->ns = ns; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2185 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2186 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2187 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2188 | return(cur); |
| 2189 | } |
| 2190 | |
| 2191 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2192 | * xmlNewNodeEatName: |
| 2193 | * @ns: namespace if any |
| 2194 | * @name: the node name |
| 2195 | * |
| 2196 | * Creation of a new node element. @ns is optional (NULL). |
| 2197 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2198 | * Returns a pointer to the new node object, with pointer @name as |
| 2199 | * new node's name. Use xmlNewNode() if a copy of @name string is |
| 2200 | * is needed as new node's name. |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2201 | */ |
| 2202 | xmlNodePtr |
| 2203 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
| 2204 | xmlNodePtr cur; |
| 2205 | |
| 2206 | if (name == NULL) { |
| 2207 | #ifdef DEBUG_TREE |
| 2208 | xmlGenericError(xmlGenericErrorContext, |
| 2209 | "xmlNewNode : name == NULL\n"); |
| 2210 | #endif |
| 2211 | return(NULL); |
| 2212 | } |
| 2213 | |
| 2214 | /* |
| 2215 | * Allocate a new node and fill the fields. |
| 2216 | */ |
| 2217 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2218 | if (cur == NULL) { |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 2219 | xmlFree(name); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2220 | xmlTreeErrMemory("building node"); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2221 | return(NULL); |
| 2222 | } |
| 2223 | memset(cur, 0, sizeof(xmlNode)); |
| 2224 | cur->type = XML_ELEMENT_NODE; |
| 2225 | |
| 2226 | cur->name = name; |
| 2227 | cur->ns = ns; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2228 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2229 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2230 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2231 | return(cur); |
| 2232 | } |
| 2233 | |
| 2234 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2235 | * xmlNewDocNode: |
| 2236 | * @doc: the document |
| 2237 | * @ns: namespace if any |
| 2238 | * @name: the node name |
| 2239 | * @content: the XML text content if any |
| 2240 | * |
| 2241 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2242 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2243 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2244 | * references, but XML special chars need to be escaped first by using |
| 2245 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2246 | * need entities support. |
| 2247 | * |
| 2248 | * Returns a pointer to the new node object. |
| 2249 | */ |
| 2250 | xmlNodePtr |
| 2251 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2252 | const xmlChar *name, const xmlChar *content) { |
| 2253 | xmlNodePtr cur; |
| 2254 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2255 | if ((doc != NULL) && (doc->dict != NULL)) |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2256 | cur = xmlNewNodeEatName(ns, (xmlChar *) |
| 2257 | xmlDictLookup(doc->dict, name, -1)); |
| 2258 | else |
| 2259 | cur = xmlNewNode(ns, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2260 | if (cur != NULL) { |
| 2261 | cur->doc = doc; |
| 2262 | if (content != NULL) { |
| 2263 | cur->children = xmlStringGetNodeList(doc, content); |
| 2264 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2265 | } |
| 2266 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2267 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2268 | return(cur); |
| 2269 | } |
| 2270 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2271 | /** |
| 2272 | * xmlNewDocNodeEatName: |
| 2273 | * @doc: the document |
| 2274 | * @ns: namespace if any |
| 2275 | * @name: the node name |
| 2276 | * @content: the XML text content if any |
| 2277 | * |
| 2278 | * Creation of a new node element within a document. @ns and @content |
| 2279 | * are optional (NULL). |
| 2280 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2281 | * references, but XML special chars need to be escaped first by using |
| 2282 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2283 | * need entities support. |
| 2284 | * |
| 2285 | * Returns a pointer to the new node object. |
| 2286 | */ |
| 2287 | xmlNodePtr |
| 2288 | xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns, |
| 2289 | xmlChar *name, const xmlChar *content) { |
| 2290 | xmlNodePtr cur; |
| 2291 | |
| 2292 | cur = xmlNewNodeEatName(ns, name); |
| 2293 | if (cur != NULL) { |
| 2294 | cur->doc = doc; |
| 2295 | if (content != NULL) { |
| 2296 | cur->children = xmlStringGetNodeList(doc, content); |
| 2297 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2298 | } |
| 2299 | } |
| 2300 | return(cur); |
| 2301 | } |
| 2302 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2303 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2304 | /** |
| 2305 | * xmlNewDocRawNode: |
| 2306 | * @doc: the document |
| 2307 | * @ns: namespace if any |
| 2308 | * @name: the node name |
| 2309 | * @content: the text content if any |
| 2310 | * |
| 2311 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2312 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2313 | * |
| 2314 | * Returns a pointer to the new node object. |
| 2315 | */ |
| 2316 | xmlNodePtr |
| 2317 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2318 | const xmlChar *name, const xmlChar *content) { |
| 2319 | xmlNodePtr cur; |
| 2320 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2321 | cur = xmlNewDocNode(doc, ns, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2322 | if (cur != NULL) { |
| 2323 | cur->doc = doc; |
| 2324 | if (content != NULL) { |
| 2325 | cur->children = xmlNewDocText(doc, content); |
| 2326 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2327 | } |
| 2328 | } |
| 2329 | return(cur); |
| 2330 | } |
| 2331 | |
| 2332 | /** |
| 2333 | * xmlNewDocFragment: |
| 2334 | * @doc: the document owning the fragment |
| 2335 | * |
| 2336 | * Creation of a new Fragment node. |
| 2337 | * Returns a pointer to the new node object. |
| 2338 | */ |
| 2339 | xmlNodePtr |
| 2340 | xmlNewDocFragment(xmlDocPtr doc) { |
| 2341 | xmlNodePtr cur; |
| 2342 | |
| 2343 | /* |
| 2344 | * Allocate a new DocumentFragment node and fill the fields. |
| 2345 | */ |
| 2346 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2347 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2348 | xmlTreeErrMemory("building fragment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2349 | return(NULL); |
| 2350 | } |
| 2351 | memset(cur, 0, sizeof(xmlNode)); |
| 2352 | cur->type = XML_DOCUMENT_FRAG_NODE; |
| 2353 | |
| 2354 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2355 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2356 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2357 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2358 | return(cur); |
| 2359 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2360 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2361 | |
| 2362 | /** |
| 2363 | * xmlNewText: |
| 2364 | * @content: the text content |
| 2365 | * |
| 2366 | * Creation of a new text node. |
| 2367 | * Returns a pointer to the new node object. |
| 2368 | */ |
| 2369 | xmlNodePtr |
| 2370 | xmlNewText(const xmlChar *content) { |
| 2371 | xmlNodePtr cur; |
| 2372 | |
| 2373 | /* |
| 2374 | * Allocate a new node and fill the fields. |
| 2375 | */ |
| 2376 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2377 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2378 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2379 | return(NULL); |
| 2380 | } |
| 2381 | memset(cur, 0, sizeof(xmlNode)); |
| 2382 | cur->type = XML_TEXT_NODE; |
| 2383 | |
| 2384 | cur->name = xmlStringText; |
| 2385 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2386 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2387 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2388 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2389 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2390 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2391 | return(cur); |
| 2392 | } |
| 2393 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2394 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2395 | /** |
| 2396 | * xmlNewTextChild: |
| 2397 | * @parent: the parent node |
| 2398 | * @ns: a namespace if any |
| 2399 | * @name: the name of the child |
| 2400 | * @content: the text content of the child if any. |
| 2401 | * |
| 2402 | * 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] | 2403 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2404 | * 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] | 2405 | * 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] | 2406 | * NOTE: Use xmlNewChild() if @content will contain entities that need to be |
| 2407 | * preserved. Use this function, xmlNewTextChild(), if you need to ensure that |
| 2408 | * reserved XML chars that might appear in @content, such as the ampersand, |
| 2409 | * greater-than or less-than signs, are automatically replaced by their XML |
| 2410 | * escaped entity representations. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2411 | * |
| 2412 | * Returns a pointer to the new node object. |
| 2413 | */ |
| 2414 | xmlNodePtr |
| 2415 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2416 | const xmlChar *name, const xmlChar *content) { |
| 2417 | xmlNodePtr cur, prev; |
| 2418 | |
| 2419 | if (parent == NULL) { |
| 2420 | #ifdef DEBUG_TREE |
| 2421 | xmlGenericError(xmlGenericErrorContext, |
| 2422 | "xmlNewTextChild : parent == NULL\n"); |
| 2423 | #endif |
| 2424 | return(NULL); |
| 2425 | } |
| 2426 | |
| 2427 | if (name == NULL) { |
| 2428 | #ifdef DEBUG_TREE |
| 2429 | xmlGenericError(xmlGenericErrorContext, |
| 2430 | "xmlNewTextChild : name == NULL\n"); |
| 2431 | #endif |
| 2432 | return(NULL); |
| 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | * Allocate a new node |
| 2437 | */ |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 2438 | if (parent->type == XML_ELEMENT_NODE) { |
| 2439 | if (ns == NULL) |
| 2440 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 2441 | else |
| 2442 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 2443 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2444 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2445 | if (ns == NULL) |
| 2446 | cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content); |
| 2447 | else |
| 2448 | cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content); |
| 2449 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2450 | cur = xmlNewDocRawNode( parent->doc, ns, name, content); |
| 2451 | } else { |
| 2452 | return(NULL); |
| 2453 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2454 | if (cur == NULL) return(NULL); |
| 2455 | |
| 2456 | /* |
| 2457 | * add the new element at the end of the children list. |
| 2458 | */ |
| 2459 | cur->type = XML_ELEMENT_NODE; |
| 2460 | cur->parent = parent; |
| 2461 | cur->doc = parent->doc; |
| 2462 | if (parent->children == NULL) { |
| 2463 | parent->children = cur; |
| 2464 | parent->last = cur; |
| 2465 | } else { |
| 2466 | prev = parent->last; |
| 2467 | prev->next = cur; |
| 2468 | cur->prev = prev; |
| 2469 | parent->last = cur; |
| 2470 | } |
| 2471 | |
| 2472 | return(cur); |
| 2473 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2474 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2475 | |
| 2476 | /** |
| 2477 | * xmlNewCharRef: |
| 2478 | * @doc: the document |
| 2479 | * @name: the char ref string, starting with # or "&# ... ;" |
| 2480 | * |
| 2481 | * Creation of a new character reference node. |
| 2482 | * Returns a pointer to the new node object. |
| 2483 | */ |
| 2484 | xmlNodePtr |
| 2485 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 2486 | xmlNodePtr cur; |
| 2487 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2488 | if (name == NULL) |
| 2489 | return(NULL); |
| 2490 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2491 | /* |
| 2492 | * Allocate a new node and fill the fields. |
| 2493 | */ |
| 2494 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2495 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2496 | xmlTreeErrMemory("building character reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2497 | return(NULL); |
| 2498 | } |
| 2499 | memset(cur, 0, sizeof(xmlNode)); |
| 2500 | cur->type = XML_ENTITY_REF_NODE; |
| 2501 | |
| 2502 | cur->doc = doc; |
| 2503 | if (name[0] == '&') { |
| 2504 | int len; |
| 2505 | name++; |
| 2506 | len = xmlStrlen(name); |
| 2507 | if (name[len - 1] == ';') |
| 2508 | cur->name = xmlStrndup(name, len - 1); |
| 2509 | else |
| 2510 | cur->name = xmlStrndup(name, len); |
| 2511 | } else |
| 2512 | cur->name = xmlStrdup(name); |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2513 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2514 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2515 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2516 | return(cur); |
| 2517 | } |
| 2518 | |
| 2519 | /** |
| 2520 | * xmlNewReference: |
| 2521 | * @doc: the document |
| 2522 | * @name: the reference name, or the reference string with & and ; |
| 2523 | * |
| 2524 | * Creation of a new reference node. |
| 2525 | * Returns a pointer to the new node object. |
| 2526 | */ |
| 2527 | xmlNodePtr |
| 2528 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 2529 | xmlNodePtr cur; |
| 2530 | xmlEntityPtr ent; |
| 2531 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2532 | if (name == NULL) |
| 2533 | return(NULL); |
| 2534 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2535 | /* |
| 2536 | * Allocate a new node and fill the fields. |
| 2537 | */ |
| 2538 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2539 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2540 | xmlTreeErrMemory("building reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2541 | return(NULL); |
| 2542 | } |
| 2543 | memset(cur, 0, sizeof(xmlNode)); |
| 2544 | cur->type = XML_ENTITY_REF_NODE; |
| 2545 | |
| 2546 | cur->doc = doc; |
| 2547 | if (name[0] == '&') { |
| 2548 | int len; |
| 2549 | name++; |
| 2550 | len = xmlStrlen(name); |
| 2551 | if (name[len - 1] == ';') |
| 2552 | cur->name = xmlStrndup(name, len - 1); |
| 2553 | else |
| 2554 | cur->name = xmlStrndup(name, len); |
| 2555 | } else |
| 2556 | cur->name = xmlStrdup(name); |
| 2557 | |
| 2558 | ent = xmlGetDocEntity(doc, cur->name); |
| 2559 | if (ent != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2560 | cur->content = ent->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2561 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2562 | * 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] | 2563 | * updated. Not sure if this is 100% correct. |
| 2564 | * -George |
| 2565 | */ |
| 2566 | cur->children = (xmlNodePtr) ent; |
| 2567 | cur->last = (xmlNodePtr) ent; |
| 2568 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2569 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2570 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2571 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2572 | return(cur); |
| 2573 | } |
| 2574 | |
| 2575 | /** |
| 2576 | * xmlNewDocText: |
| 2577 | * @doc: the document |
| 2578 | * @content: the text content |
| 2579 | * |
| 2580 | * Creation of a new text node within a document. |
| 2581 | * Returns a pointer to the new node object. |
| 2582 | */ |
| 2583 | xmlNodePtr |
| 2584 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 2585 | xmlNodePtr cur; |
| 2586 | |
| 2587 | cur = xmlNewText(content); |
| 2588 | if (cur != NULL) cur->doc = doc; |
| 2589 | return(cur); |
| 2590 | } |
| 2591 | |
| 2592 | /** |
| 2593 | * xmlNewTextLen: |
| 2594 | * @content: the text content |
| 2595 | * @len: the text len. |
| 2596 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2597 | * 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] | 2598 | * Returns a pointer to the new node object. |
| 2599 | */ |
| 2600 | xmlNodePtr |
| 2601 | xmlNewTextLen(const xmlChar *content, int len) { |
| 2602 | xmlNodePtr cur; |
| 2603 | |
| 2604 | /* |
| 2605 | * Allocate a new node and fill the fields. |
| 2606 | */ |
| 2607 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2608 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2609 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2610 | return(NULL); |
| 2611 | } |
| 2612 | memset(cur, 0, sizeof(xmlNode)); |
| 2613 | cur->type = XML_TEXT_NODE; |
| 2614 | |
| 2615 | cur->name = xmlStringText; |
| 2616 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2617 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2618 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2619 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2620 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2621 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2622 | return(cur); |
| 2623 | } |
| 2624 | |
| 2625 | /** |
| 2626 | * xmlNewDocTextLen: |
| 2627 | * @doc: the document |
| 2628 | * @content: the text content |
| 2629 | * @len: the text len. |
| 2630 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2631 | * 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] | 2632 | * text node pertain to a given document. |
| 2633 | * Returns a pointer to the new node object. |
| 2634 | */ |
| 2635 | xmlNodePtr |
| 2636 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2637 | xmlNodePtr cur; |
| 2638 | |
| 2639 | cur = xmlNewTextLen(content, len); |
| 2640 | if (cur != NULL) cur->doc = doc; |
| 2641 | return(cur); |
| 2642 | } |
| 2643 | |
| 2644 | /** |
| 2645 | * xmlNewComment: |
| 2646 | * @content: the comment content |
| 2647 | * |
| 2648 | * Creation of a new node containing a comment. |
| 2649 | * Returns a pointer to the new node object. |
| 2650 | */ |
| 2651 | xmlNodePtr |
| 2652 | xmlNewComment(const xmlChar *content) { |
| 2653 | xmlNodePtr cur; |
| 2654 | |
| 2655 | /* |
| 2656 | * Allocate a new node and fill the fields. |
| 2657 | */ |
| 2658 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2659 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2660 | xmlTreeErrMemory("building comment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2661 | return(NULL); |
| 2662 | } |
| 2663 | memset(cur, 0, sizeof(xmlNode)); |
| 2664 | cur->type = XML_COMMENT_NODE; |
| 2665 | |
| 2666 | cur->name = xmlStringComment; |
| 2667 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2668 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2669 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2670 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2671 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2672 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2673 | return(cur); |
| 2674 | } |
| 2675 | |
| 2676 | /** |
| 2677 | * xmlNewCDataBlock: |
| 2678 | * @doc: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2679 | * @content: the CDATA block content content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2680 | * @len: the length of the block |
| 2681 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2682 | * Creation of a new node containing a CDATA block. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2683 | * Returns a pointer to the new node object. |
| 2684 | */ |
| 2685 | xmlNodePtr |
| 2686 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2687 | xmlNodePtr cur; |
| 2688 | |
| 2689 | /* |
| 2690 | * Allocate a new node and fill the fields. |
| 2691 | */ |
| 2692 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2693 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2694 | xmlTreeErrMemory("building CDATA"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2695 | return(NULL); |
| 2696 | } |
| 2697 | memset(cur, 0, sizeof(xmlNode)); |
| 2698 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2699 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2700 | |
| 2701 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2702 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2703 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2704 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2705 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2706 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2707 | return(cur); |
| 2708 | } |
| 2709 | |
| 2710 | /** |
| 2711 | * xmlNewDocComment: |
| 2712 | * @doc: the document |
| 2713 | * @content: the comment content |
| 2714 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2715 | * Creation of a new node containing a comment within a document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2716 | * Returns a pointer to the new node object. |
| 2717 | */ |
| 2718 | xmlNodePtr |
| 2719 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 2720 | xmlNodePtr cur; |
| 2721 | |
| 2722 | cur = xmlNewComment(content); |
| 2723 | if (cur != NULL) cur->doc = doc; |
| 2724 | return(cur); |
| 2725 | } |
| 2726 | |
| 2727 | /** |
| 2728 | * xmlSetTreeDoc: |
| 2729 | * @tree: the top element |
| 2730 | * @doc: the document |
| 2731 | * |
| 2732 | * update all nodes under the tree to point to the right document |
| 2733 | */ |
| 2734 | void |
| 2735 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2736 | xmlAttrPtr prop; |
| 2737 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2738 | if (tree == NULL) |
| 2739 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2740 | if (tree->doc != doc) { |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2741 | if(tree->type == XML_ELEMENT_NODE) { |
| 2742 | prop = tree->properties; |
| 2743 | while (prop != NULL) { |
| 2744 | prop->doc = doc; |
| 2745 | xmlSetListDoc(prop->children, doc); |
| 2746 | prop = prop->next; |
| 2747 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2748 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2749 | if (tree->children != NULL) |
| 2750 | xmlSetListDoc(tree->children, doc); |
| 2751 | tree->doc = doc; |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | /** |
| 2756 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2757 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2758 | * @doc: the document |
| 2759 | * |
| 2760 | * update all nodes in the list to point to the right document |
| 2761 | */ |
| 2762 | void |
| 2763 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2764 | xmlNodePtr cur; |
| 2765 | |
| 2766 | if (list == NULL) |
| 2767 | return; |
| 2768 | cur = list; |
| 2769 | while (cur != NULL) { |
| 2770 | if (cur->doc != doc) |
| 2771 | xmlSetTreeDoc(cur, doc); |
| 2772 | cur = cur->next; |
| 2773 | } |
| 2774 | } |
| 2775 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 2776 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2777 | /** |
| 2778 | * xmlNewChild: |
| 2779 | * @parent: the parent node |
| 2780 | * @ns: a namespace if any |
| 2781 | * @name: the name of the child |
| 2782 | * @content: the XML content of the child if any. |
| 2783 | * |
| 2784 | * 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] | 2785 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2786 | * created element inherits the namespace of @parent. If @content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2787 | * 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] | 2788 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 2789 | * references. XML special chars must be escaped first by using |
| 2790 | * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2791 | * |
| 2792 | * Returns a pointer to the new node object. |
| 2793 | */ |
| 2794 | xmlNodePtr |
| 2795 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2796 | const xmlChar *name, const xmlChar *content) { |
| 2797 | xmlNodePtr cur, prev; |
| 2798 | |
| 2799 | if (parent == NULL) { |
| 2800 | #ifdef DEBUG_TREE |
| 2801 | xmlGenericError(xmlGenericErrorContext, |
| 2802 | "xmlNewChild : parent == NULL\n"); |
| 2803 | #endif |
| 2804 | return(NULL); |
| 2805 | } |
| 2806 | |
| 2807 | if (name == NULL) { |
| 2808 | #ifdef DEBUG_TREE |
| 2809 | xmlGenericError(xmlGenericErrorContext, |
| 2810 | "xmlNewChild : name == NULL\n"); |
| 2811 | #endif |
| 2812 | return(NULL); |
| 2813 | } |
| 2814 | |
| 2815 | /* |
| 2816 | * Allocate a new node |
| 2817 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2818 | if (parent->type == XML_ELEMENT_NODE) { |
| 2819 | if (ns == NULL) |
| 2820 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2821 | else |
| 2822 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2823 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2824 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2825 | if (ns == NULL) |
| 2826 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2827 | else |
| 2828 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2829 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2830 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2831 | } else { |
| 2832 | return(NULL); |
| 2833 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2834 | if (cur == NULL) return(NULL); |
| 2835 | |
| 2836 | /* |
| 2837 | * add the new element at the end of the children list. |
| 2838 | */ |
| 2839 | cur->type = XML_ELEMENT_NODE; |
| 2840 | cur->parent = parent; |
| 2841 | cur->doc = parent->doc; |
| 2842 | if (parent->children == NULL) { |
| 2843 | parent->children = cur; |
| 2844 | parent->last = cur; |
| 2845 | } else { |
| 2846 | prev = parent->last; |
| 2847 | prev->next = cur; |
| 2848 | cur->prev = prev; |
| 2849 | parent->last = cur; |
| 2850 | } |
| 2851 | |
| 2852 | return(cur); |
| 2853 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2854 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2855 | |
| 2856 | /** |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2857 | * xmlAddPropSibling: |
| 2858 | * @prev: the attribute to which @prop is added after |
| 2859 | * @cur: the base attribute passed to calling function |
| 2860 | * @prop: the new attribute |
| 2861 | * |
| 2862 | * Add a new attribute after @prev using @cur as base attribute. |
| 2863 | * When inserting before @cur, @prev is passed as @cur->prev. |
| 2864 | * When inserting after @cur, @prev is passed as @cur. |
| 2865 | * If an existing attribute is found it is detroyed prior to adding @prop. |
| 2866 | * |
| 2867 | * Returns the attribute being inserted or NULL in case of error. |
| 2868 | */ |
| 2869 | static xmlNodePtr |
| 2870 | xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) { |
| 2871 | xmlAttrPtr attr; |
| 2872 | |
| 2873 | if (cur->type != XML_ATTRIBUTE_NODE) |
| 2874 | return(NULL); |
| 2875 | |
| 2876 | /* check if an attribute with the same name exists */ |
| 2877 | if (prop->ns == NULL) |
| 2878 | attr = xmlHasNsProp(cur->parent, prop->name, NULL); |
| 2879 | else |
| 2880 | attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href); |
| 2881 | |
| 2882 | if (prop->doc != cur->doc) { |
| 2883 | xmlSetTreeDoc(prop, cur->doc); |
| 2884 | } |
| 2885 | prop->parent = cur->parent; |
| 2886 | prop->prev = prev; |
| 2887 | if (prev != NULL) { |
| 2888 | prop->next = prev->next; |
| 2889 | prev->next = prop; |
| 2890 | if (prop->next) |
| 2891 | prop->next->prev = prop; |
| 2892 | } else { |
| 2893 | prop->next = cur; |
| 2894 | cur->prev = prop; |
| 2895 | } |
| 2896 | if (prop->prev == NULL && prop->parent != NULL) |
| 2897 | prop->parent->properties = (xmlAttrPtr) prop; |
| 2898 | if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) { |
| 2899 | /* different instance, destroy it (attributes must be unique) */ |
| 2900 | xmlRemoveProp((xmlAttrPtr) attr); |
| 2901 | } |
| 2902 | return prop; |
| 2903 | } |
| 2904 | |
| 2905 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2906 | * xmlAddNextSibling: |
| 2907 | * @cur: the child node |
| 2908 | * @elem: the new node |
| 2909 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2910 | * Add a new node @elem as the next sibling of @cur |
| 2911 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2912 | * first unlinked from its existing context. |
| 2913 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2914 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2915 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2916 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2917 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2918 | */ |
| 2919 | xmlNodePtr |
| 2920 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2921 | if (cur == NULL) { |
| 2922 | #ifdef DEBUG_TREE |
| 2923 | xmlGenericError(xmlGenericErrorContext, |
| 2924 | "xmlAddNextSibling : cur == NULL\n"); |
| 2925 | #endif |
| 2926 | return(NULL); |
| 2927 | } |
| 2928 | if (elem == NULL) { |
| 2929 | #ifdef DEBUG_TREE |
| 2930 | xmlGenericError(xmlGenericErrorContext, |
| 2931 | "xmlAddNextSibling : elem == NULL\n"); |
| 2932 | #endif |
| 2933 | return(NULL); |
| 2934 | } |
| 2935 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2936 | if (cur == elem) { |
| 2937 | #ifdef DEBUG_TREE |
| 2938 | xmlGenericError(xmlGenericErrorContext, |
| 2939 | "xmlAddNextSibling : cur == elem\n"); |
| 2940 | #endif |
| 2941 | return(NULL); |
| 2942 | } |
| 2943 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2944 | xmlUnlinkNode(elem); |
| 2945 | |
| 2946 | if (elem->type == XML_TEXT_NODE) { |
| 2947 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2948 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2949 | xmlFreeNode(elem); |
| 2950 | return(cur); |
| 2951 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2952 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 2953 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2954 | xmlChar *tmp; |
| 2955 | |
| 2956 | tmp = xmlStrdup(elem->content); |
| 2957 | tmp = xmlStrcat(tmp, cur->next->content); |
| 2958 | xmlNodeSetContent(cur->next, tmp); |
| 2959 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2960 | xmlFreeNode(elem); |
| 2961 | return(cur->next); |
| 2962 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2963 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2964 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | if (elem->doc != cur->doc) { |
| 2968 | xmlSetTreeDoc(elem, cur->doc); |
| 2969 | } |
| 2970 | elem->parent = cur->parent; |
| 2971 | elem->prev = cur; |
| 2972 | elem->next = cur->next; |
| 2973 | cur->next = elem; |
| 2974 | if (elem->next != NULL) |
| 2975 | elem->next->prev = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2976 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2977 | elem->parent->last = elem; |
| 2978 | return(elem); |
| 2979 | } |
| 2980 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 2981 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 2982 | defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2983 | /** |
| 2984 | * xmlAddPrevSibling: |
| 2985 | * @cur: the child node |
| 2986 | * @elem: the new node |
| 2987 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2988 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2989 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2990 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2991 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2992 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2993 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2994 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2995 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2996 | */ |
| 2997 | xmlNodePtr |
| 2998 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2999 | if (cur == NULL) { |
| 3000 | #ifdef DEBUG_TREE |
| 3001 | xmlGenericError(xmlGenericErrorContext, |
| 3002 | "xmlAddPrevSibling : cur == NULL\n"); |
| 3003 | #endif |
| 3004 | return(NULL); |
| 3005 | } |
| 3006 | if (elem == NULL) { |
| 3007 | #ifdef DEBUG_TREE |
| 3008 | xmlGenericError(xmlGenericErrorContext, |
| 3009 | "xmlAddPrevSibling : elem == NULL\n"); |
| 3010 | #endif |
| 3011 | return(NULL); |
| 3012 | } |
| 3013 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3014 | if (cur == elem) { |
| 3015 | #ifdef DEBUG_TREE |
| 3016 | xmlGenericError(xmlGenericErrorContext, |
| 3017 | "xmlAddPrevSibling : cur == elem\n"); |
| 3018 | #endif |
| 3019 | return(NULL); |
| 3020 | } |
| 3021 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3022 | xmlUnlinkNode(elem); |
| 3023 | |
| 3024 | if (elem->type == XML_TEXT_NODE) { |
| 3025 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3026 | xmlChar *tmp; |
| 3027 | |
| 3028 | tmp = xmlStrdup(elem->content); |
| 3029 | tmp = xmlStrcat(tmp, cur->content); |
| 3030 | xmlNodeSetContent(cur, tmp); |
| 3031 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3032 | xmlFreeNode(elem); |
| 3033 | return(cur); |
| 3034 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 3035 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 3036 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3037 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3038 | xmlFreeNode(elem); |
| 3039 | return(cur->prev); |
| 3040 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3041 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3042 | return xmlAddPropSibling(cur->prev, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | if (elem->doc != cur->doc) { |
| 3046 | xmlSetTreeDoc(elem, cur->doc); |
| 3047 | } |
| 3048 | elem->parent = cur->parent; |
| 3049 | elem->next = cur; |
| 3050 | elem->prev = cur->prev; |
| 3051 | cur->prev = elem; |
| 3052 | if (elem->prev != NULL) |
| 3053 | elem->prev->next = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3054 | if ((elem->parent != NULL) && (elem->parent->children == cur)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3055 | elem->parent->children = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3056 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3057 | return(elem); |
| 3058 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3059 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3060 | |
| 3061 | /** |
| 3062 | * xmlAddSibling: |
| 3063 | * @cur: the child node |
| 3064 | * @elem: the new node |
| 3065 | * |
| 3066 | * Add a new element @elem to the list of siblings of @cur |
| 3067 | * merging adjacent TEXT nodes (@elem may be freed) |
| 3068 | * If the new element was already inserted in a document it is |
| 3069 | * first unlinked from its existing context. |
| 3070 | * |
| 3071 | * Returns the new element or NULL in case of error. |
| 3072 | */ |
| 3073 | xmlNodePtr |
| 3074 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 3075 | xmlNodePtr parent; |
| 3076 | |
| 3077 | if (cur == NULL) { |
| 3078 | #ifdef DEBUG_TREE |
| 3079 | xmlGenericError(xmlGenericErrorContext, |
| 3080 | "xmlAddSibling : cur == NULL\n"); |
| 3081 | #endif |
| 3082 | return(NULL); |
| 3083 | } |
| 3084 | |
| 3085 | if (elem == NULL) { |
| 3086 | #ifdef DEBUG_TREE |
| 3087 | xmlGenericError(xmlGenericErrorContext, |
| 3088 | "xmlAddSibling : elem == NULL\n"); |
| 3089 | #endif |
| 3090 | return(NULL); |
| 3091 | } |
| 3092 | |
| 3093 | /* |
| 3094 | * Constant time is we can rely on the ->parent->last to find |
| 3095 | * the last sibling. |
| 3096 | */ |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3097 | if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3098 | (cur->parent->children != NULL) && |
| 3099 | (cur->parent->last != NULL) && |
| 3100 | (cur->parent->last->next == NULL)) { |
| 3101 | cur = cur->parent->last; |
| 3102 | } else { |
| 3103 | while (cur->next != NULL) cur = cur->next; |
| 3104 | } |
| 3105 | |
| 3106 | xmlUnlinkNode(elem); |
| 3107 | |
Daniel Veillard | e22dd5c | 2003-10-29 12:53:27 +0000 | [diff] [blame] | 3108 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) && |
| 3109 | (cur->name == elem->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3110 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3111 | xmlFreeNode(elem); |
| 3112 | return(cur); |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3113 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 3114 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | if (elem->doc != cur->doc) { |
| 3118 | xmlSetTreeDoc(elem, cur->doc); |
| 3119 | } |
| 3120 | parent = cur->parent; |
| 3121 | elem->prev = cur; |
| 3122 | elem->next = NULL; |
| 3123 | elem->parent = parent; |
| 3124 | cur->next = elem; |
| 3125 | if (parent != NULL) |
| 3126 | parent->last = elem; |
| 3127 | |
| 3128 | return(elem); |
| 3129 | } |
| 3130 | |
| 3131 | /** |
| 3132 | * xmlAddChildList: |
| 3133 | * @parent: the parent node |
| 3134 | * @cur: the first node in the list |
| 3135 | * |
| 3136 | * Add a list of node at the end of the child list of the parent |
| 3137 | * merging adjacent TEXT nodes (@cur may be freed) |
| 3138 | * |
| 3139 | * Returns the last child or NULL in case of error. |
| 3140 | */ |
| 3141 | xmlNodePtr |
| 3142 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 3143 | xmlNodePtr prev; |
| 3144 | |
| 3145 | if (parent == NULL) { |
| 3146 | #ifdef DEBUG_TREE |
| 3147 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3148 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3149 | #endif |
| 3150 | return(NULL); |
| 3151 | } |
| 3152 | |
| 3153 | if (cur == NULL) { |
| 3154 | #ifdef DEBUG_TREE |
| 3155 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3156 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3157 | #endif |
| 3158 | return(NULL); |
| 3159 | } |
| 3160 | |
| 3161 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 3162 | (cur->doc != parent->doc)) { |
| 3163 | #ifdef DEBUG_TREE |
| 3164 | xmlGenericError(xmlGenericErrorContext, |
| 3165 | "Elements moved to a different document\n"); |
| 3166 | #endif |
| 3167 | } |
| 3168 | |
| 3169 | /* |
| 3170 | * add the first element at the end of the children list. |
| 3171 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3172 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3173 | if (parent->children == NULL) { |
| 3174 | parent->children = cur; |
| 3175 | } else { |
| 3176 | /* |
| 3177 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 3178 | */ |
| 3179 | if ((cur->type == XML_TEXT_NODE) && |
| 3180 | (parent->last->type == XML_TEXT_NODE) && |
| 3181 | (cur->name == parent->last->name)) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3182 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3183 | /* |
| 3184 | * if it's the only child, nothing more to be done. |
| 3185 | */ |
| 3186 | if (cur->next == NULL) { |
| 3187 | xmlFreeNode(cur); |
| 3188 | return(parent->last); |
| 3189 | } |
| 3190 | prev = cur; |
| 3191 | cur = cur->next; |
| 3192 | xmlFreeNode(prev); |
| 3193 | } |
| 3194 | prev = parent->last; |
| 3195 | prev->next = cur; |
| 3196 | cur->prev = prev; |
| 3197 | } |
| 3198 | while (cur->next != NULL) { |
| 3199 | cur->parent = parent; |
| 3200 | if (cur->doc != parent->doc) { |
| 3201 | xmlSetTreeDoc(cur, parent->doc); |
| 3202 | } |
| 3203 | cur = cur->next; |
| 3204 | } |
| 3205 | cur->parent = parent; |
| 3206 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 3207 | parent->last = cur; |
| 3208 | |
| 3209 | return(cur); |
| 3210 | } |
| 3211 | |
| 3212 | /** |
| 3213 | * xmlAddChild: |
| 3214 | * @parent: the parent node |
| 3215 | * @cur: the child node |
| 3216 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3217 | * 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] | 3218 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3219 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 3220 | * If there is an attribute with equal name, it is first destroyed. |
| 3221 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3222 | * Returns the child or NULL in case of error. |
| 3223 | */ |
| 3224 | xmlNodePtr |
| 3225 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 3226 | xmlNodePtr prev; |
| 3227 | |
| 3228 | if (parent == NULL) { |
| 3229 | #ifdef DEBUG_TREE |
| 3230 | xmlGenericError(xmlGenericErrorContext, |
| 3231 | "xmlAddChild : parent == NULL\n"); |
| 3232 | #endif |
| 3233 | return(NULL); |
| 3234 | } |
| 3235 | |
| 3236 | if (cur == NULL) { |
| 3237 | #ifdef DEBUG_TREE |
| 3238 | xmlGenericError(xmlGenericErrorContext, |
| 3239 | "xmlAddChild : child == NULL\n"); |
| 3240 | #endif |
| 3241 | return(NULL); |
| 3242 | } |
| 3243 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3244 | if (parent == cur) { |
| 3245 | #ifdef DEBUG_TREE |
| 3246 | xmlGenericError(xmlGenericErrorContext, |
| 3247 | "xmlAddChild : parent == cur\n"); |
| 3248 | #endif |
| 3249 | return(NULL); |
| 3250 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3251 | /* |
| 3252 | * 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] | 3253 | * cur is then freed. |
| 3254 | */ |
| 3255 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3256 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3257 | (parent->content != NULL) && |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3258 | (parent->name == cur->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3259 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3260 | xmlFreeNode(cur); |
| 3261 | return(parent); |
| 3262 | } |
| 3263 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3264 | (parent->last->name == cur->name) && |
| 3265 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3266 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3267 | xmlFreeNode(cur); |
| 3268 | return(parent->last); |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | /* |
| 3273 | * add the new element at the end of the children list. |
| 3274 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3275 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3276 | cur->parent = parent; |
| 3277 | if (cur->doc != parent->doc) { |
| 3278 | xmlSetTreeDoc(cur, parent->doc); |
| 3279 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3280 | /* this check prevents a loop on tree-traversions if a developer |
| 3281 | * tries to add a node to its parent multiple times |
| 3282 | */ |
| 3283 | if (prev == parent) |
| 3284 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3285 | |
| 3286 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3287 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3288 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3289 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3290 | (parent->content != NULL) && |
| 3291 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3292 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3293 | xmlFreeNode(cur); |
| 3294 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3295 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3296 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3297 | if (parent->type != XML_ELEMENT_NODE) |
| 3298 | return(NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3299 | if (parent->properties == NULL) { |
| 3300 | parent->properties = (xmlAttrPtr) cur; |
| 3301 | } else { |
| 3302 | /* check if an attribute with the same name exists */ |
| 3303 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3304 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3305 | if (cur->ns == NULL) |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3306 | lastattr = xmlHasNsProp(parent, cur->name, NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3307 | else |
| 3308 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3309 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3310 | /* different instance, destroy it (attributes must be unique) */ |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3311 | xmlUnlinkNode((xmlNodePtr) lastattr); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3312 | xmlFreeProp(lastattr); |
| 3313 | } |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3314 | if (lastattr == (xmlAttrPtr) cur) |
| 3315 | return(cur); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3316 | /* find the end */ |
| 3317 | lastattr = parent->properties; |
| 3318 | while (lastattr->next != NULL) { |
| 3319 | lastattr = lastattr->next; |
| 3320 | } |
| 3321 | lastattr->next = (xmlAttrPtr) cur; |
| 3322 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3323 | } |
| 3324 | } else { |
| 3325 | if (parent->children == NULL) { |
| 3326 | parent->children = cur; |
| 3327 | parent->last = cur; |
| 3328 | } else { |
| 3329 | prev = parent->last; |
| 3330 | prev->next = cur; |
| 3331 | cur->prev = prev; |
| 3332 | parent->last = cur; |
| 3333 | } |
| 3334 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3335 | return(cur); |
| 3336 | } |
| 3337 | |
| 3338 | /** |
| 3339 | * xmlGetLastChild: |
| 3340 | * @parent: the parent node |
| 3341 | * |
| 3342 | * Search the last child of a node. |
| 3343 | * Returns the last child or NULL if none. |
| 3344 | */ |
| 3345 | xmlNodePtr |
| 3346 | xmlGetLastChild(xmlNodePtr parent) { |
| 3347 | if (parent == NULL) { |
| 3348 | #ifdef DEBUG_TREE |
| 3349 | xmlGenericError(xmlGenericErrorContext, |
| 3350 | "xmlGetLastChild : parent == NULL\n"); |
| 3351 | #endif |
| 3352 | return(NULL); |
| 3353 | } |
| 3354 | return(parent->last); |
| 3355 | } |
| 3356 | |
| 3357 | /** |
| 3358 | * xmlFreeNodeList: |
| 3359 | * @cur: the first node in the list |
| 3360 | * |
| 3361 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3362 | * the children are freed too. |
| 3363 | */ |
| 3364 | void |
| 3365 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3366 | xmlNodePtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3367 | xmlDictPtr dict = NULL; |
| 3368 | |
| 3369 | if (cur == NULL) return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3370 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3371 | xmlFreeNsList((xmlNsPtr) cur); |
| 3372 | return; |
| 3373 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3374 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3375 | #ifdef LIBXML_DOCB_ENABLED |
| 3376 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3377 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3378 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3379 | xmlFreeDoc((xmlDocPtr) cur); |
| 3380 | return; |
| 3381 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3382 | if (cur->doc != NULL) dict = cur->doc->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3383 | while (cur != NULL) { |
| 3384 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3385 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3386 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3387 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3388 | xmlDeregisterNodeDefaultValue(cur); |
| 3389 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3390 | if ((cur->children != NULL) && |
| 3391 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3392 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3393 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3394 | (cur->type == XML_XINCLUDE_START) || |
| 3395 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3396 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3397 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3398 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3399 | (cur->type != XML_XINCLUDE_START) && |
| 3400 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3401 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3402 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3403 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3404 | } |
| 3405 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3406 | (cur->type == XML_XINCLUDE_START) || |
| 3407 | (cur->type == XML_XINCLUDE_END)) && |
| 3408 | (cur->nsDef != NULL)) |
| 3409 | xmlFreeNsList(cur->nsDef); |
| 3410 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3411 | /* |
| 3412 | * When a node is a text node or a comment, it uses a global static |
| 3413 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3414 | * Otherwise the node name might come from the document's |
| 3415 | * dictionnary |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3416 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3417 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3418 | (cur->type != XML_TEXT_NODE) && |
| 3419 | (cur->type != XML_COMMENT_NODE)) |
| 3420 | DICT_FREE(cur->name) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3421 | xmlFree(cur); |
| 3422 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3423 | cur = next; |
| 3424 | } |
| 3425 | } |
| 3426 | |
| 3427 | /** |
| 3428 | * xmlFreeNode: |
| 3429 | * @cur: the node |
| 3430 | * |
| 3431 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3432 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3433 | */ |
| 3434 | void |
| 3435 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3436 | xmlDictPtr dict = NULL; |
| 3437 | |
| 3438 | if (cur == NULL) return; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3439 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3440 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3441 | if (cur->type == XML_DTD_NODE) { |
| 3442 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3443 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3444 | } |
| 3445 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3446 | xmlFreeNs((xmlNsPtr) cur); |
| 3447 | return; |
| 3448 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3449 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3450 | xmlFreeProp((xmlAttrPtr) cur); |
| 3451 | return; |
| 3452 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3453 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3454 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3455 | xmlDeregisterNodeDefaultValue(cur); |
| 3456 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3457 | if (cur->doc != NULL) dict = cur->doc->dict; |
| 3458 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3459 | if ((cur->children != NULL) && |
| 3460 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3461 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3462 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3463 | (cur->type == XML_XINCLUDE_START) || |
| 3464 | (cur->type == XML_XINCLUDE_END)) && |
| 3465 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3466 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3467 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3468 | (cur->content != NULL) && |
| 3469 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3470 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3471 | (cur->type != XML_XINCLUDE_START) && |
| 3472 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3473 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3476 | /* |
| 3477 | * When a node is a text node or a comment, it uses a global static |
| 3478 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3479 | * Otherwise the node name might come from the document's dictionnary |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3480 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3481 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3482 | (cur->type != XML_TEXT_NODE) && |
| 3483 | (cur->type != XML_COMMENT_NODE)) |
| 3484 | DICT_FREE(cur->name) |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3485 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3486 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3487 | (cur->type == XML_XINCLUDE_START) || |
| 3488 | (cur->type == XML_XINCLUDE_END)) && |
| 3489 | (cur->nsDef != NULL)) |
| 3490 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3491 | xmlFree(cur); |
| 3492 | } |
| 3493 | |
| 3494 | /** |
| 3495 | * xmlUnlinkNode: |
| 3496 | * @cur: the node |
| 3497 | * |
| 3498 | * Unlink a node from it's current context, the node is not freed |
| 3499 | */ |
| 3500 | void |
| 3501 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3502 | if (cur == NULL) { |
| 3503 | #ifdef DEBUG_TREE |
| 3504 | xmlGenericError(xmlGenericErrorContext, |
| 3505 | "xmlUnlinkNode : node == NULL\n"); |
| 3506 | #endif |
| 3507 | return; |
| 3508 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3509 | if (cur->type == XML_DTD_NODE) { |
| 3510 | xmlDocPtr doc; |
| 3511 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3512 | if (doc != NULL) { |
| 3513 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3514 | doc->intSubset = NULL; |
| 3515 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3516 | doc->extSubset = NULL; |
| 3517 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3518 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3519 | if (cur->parent != NULL) { |
| 3520 | xmlNodePtr parent; |
| 3521 | parent = cur->parent; |
| 3522 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3523 | if (parent->properties == (xmlAttrPtr) cur) |
| 3524 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3525 | } else { |
| 3526 | if (parent->children == cur) |
| 3527 | parent->children = cur->next; |
| 3528 | if (parent->last == cur) |
| 3529 | parent->last = cur->prev; |
| 3530 | } |
| 3531 | cur->parent = NULL; |
| 3532 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3533 | if (cur->next != NULL) |
| 3534 | cur->next->prev = cur->prev; |
| 3535 | if (cur->prev != NULL) |
| 3536 | cur->prev->next = cur->next; |
| 3537 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 3540 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3541 | /** |
| 3542 | * xmlReplaceNode: |
| 3543 | * @old: the old node |
| 3544 | * @cur: the node |
| 3545 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 3546 | * Unlink the old node from its current context, prune the new one |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3547 | * 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] | 3548 | * first unlinked from its existing context. |
| 3549 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3550 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3551 | */ |
| 3552 | xmlNodePtr |
| 3553 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 3554 | if (old == cur) return(NULL); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3555 | if ((old == NULL) || (old->parent == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3556 | #ifdef DEBUG_TREE |
| 3557 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3558 | "xmlReplaceNode : old == NULL or without parent\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3559 | #endif |
| 3560 | return(NULL); |
| 3561 | } |
| 3562 | if (cur == NULL) { |
| 3563 | xmlUnlinkNode(old); |
| 3564 | return(old); |
| 3565 | } |
| 3566 | if (cur == old) { |
| 3567 | return(old); |
| 3568 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3569 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3570 | #ifdef DEBUG_TREE |
| 3571 | xmlGenericError(xmlGenericErrorContext, |
| 3572 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3573 | #endif |
| 3574 | return(old); |
| 3575 | } |
| 3576 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3577 | #ifdef DEBUG_TREE |
| 3578 | xmlGenericError(xmlGenericErrorContext, |
| 3579 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3580 | #endif |
| 3581 | return(old); |
| 3582 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3583 | xmlUnlinkNode(cur); |
Daniel Veillard | 64d7d12 | 2005-05-11 18:03:42 +0000 | [diff] [blame] | 3584 | xmlSetTreeDoc(cur, old->doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3585 | cur->parent = old->parent; |
| 3586 | cur->next = old->next; |
| 3587 | if (cur->next != NULL) |
| 3588 | cur->next->prev = cur; |
| 3589 | cur->prev = old->prev; |
| 3590 | if (cur->prev != NULL) |
| 3591 | cur->prev->next = cur; |
| 3592 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3593 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3594 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3595 | cur->parent->properties = ((xmlAttrPtr) cur); |
| 3596 | } else { |
| 3597 | if (cur->parent->children == old) |
| 3598 | cur->parent->children = cur; |
| 3599 | if (cur->parent->last == old) |
| 3600 | cur->parent->last = cur; |
| 3601 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3602 | } |
| 3603 | old->next = old->prev = NULL; |
| 3604 | old->parent = NULL; |
| 3605 | return(old); |
| 3606 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3607 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3608 | |
| 3609 | /************************************************************************ |
| 3610 | * * |
| 3611 | * Copy operations * |
| 3612 | * * |
| 3613 | ************************************************************************/ |
| 3614 | |
| 3615 | /** |
| 3616 | * xmlCopyNamespace: |
| 3617 | * @cur: the namespace |
| 3618 | * |
| 3619 | * Do a copy of the namespace. |
| 3620 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3621 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3622 | */ |
| 3623 | xmlNsPtr |
| 3624 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3625 | xmlNsPtr ret; |
| 3626 | |
| 3627 | if (cur == NULL) return(NULL); |
| 3628 | switch (cur->type) { |
| 3629 | case XML_LOCAL_NAMESPACE: |
| 3630 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3631 | break; |
| 3632 | default: |
| 3633 | #ifdef DEBUG_TREE |
| 3634 | xmlGenericError(xmlGenericErrorContext, |
| 3635 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3636 | #endif |
| 3637 | return(NULL); |
| 3638 | } |
| 3639 | return(ret); |
| 3640 | } |
| 3641 | |
| 3642 | /** |
| 3643 | * xmlCopyNamespaceList: |
| 3644 | * @cur: the first namespace |
| 3645 | * |
| 3646 | * Do a copy of an namespace list. |
| 3647 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3648 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3649 | */ |
| 3650 | xmlNsPtr |
| 3651 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3652 | xmlNsPtr ret = NULL; |
| 3653 | xmlNsPtr p = NULL,q; |
| 3654 | |
| 3655 | while (cur != NULL) { |
| 3656 | q = xmlCopyNamespace(cur); |
| 3657 | if (p == NULL) { |
| 3658 | ret = p = q; |
| 3659 | } else { |
| 3660 | p->next = q; |
| 3661 | p = q; |
| 3662 | } |
| 3663 | cur = cur->next; |
| 3664 | } |
| 3665 | return(ret); |
| 3666 | } |
| 3667 | |
| 3668 | static xmlNodePtr |
| 3669 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3670 | |
| 3671 | static xmlAttrPtr |
| 3672 | xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3673 | xmlAttrPtr ret; |
| 3674 | |
| 3675 | if (cur == NULL) return(NULL); |
| 3676 | if (target != NULL) |
| 3677 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3678 | else if (doc != NULL) |
| 3679 | ret = xmlNewDocProp(doc, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3680 | else if (cur->parent != NULL) |
| 3681 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 3682 | else if (cur->children != NULL) |
| 3683 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 3684 | else |
| 3685 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 3686 | if (ret == NULL) return(NULL); |
| 3687 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3688 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3689 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3690 | xmlNsPtr ns; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3691 | |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3692 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3693 | if (ns == NULL) { |
| 3694 | /* |
| 3695 | * Humm, we are copying an element whose namespace is defined |
| 3696 | * out of the new tree scope. Search it in the original tree |
| 3697 | * and add it at the top of the new tree |
| 3698 | */ |
| 3699 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 3700 | if (ns != NULL) { |
| 3701 | xmlNodePtr root = target; |
| 3702 | xmlNodePtr pred = NULL; |
| 3703 | |
| 3704 | while (root->parent != NULL) { |
| 3705 | pred = root; |
| 3706 | root = root->parent; |
| 3707 | } |
| 3708 | if (root == (xmlNodePtr) target->doc) { |
| 3709 | /* correct possibly cycling above the document elt */ |
| 3710 | root = pred; |
| 3711 | } |
| 3712 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 3713 | } |
| 3714 | } else { |
| 3715 | /* |
| 3716 | * we have to find something appropriate here since |
| 3717 | * we cant be sure, that the namespce we found is identified |
| 3718 | * by the prefix |
| 3719 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3720 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3721 | /* this is the nice case */ |
| 3722 | ret->ns = ns; |
| 3723 | } else { |
| 3724 | /* |
| 3725 | * we are in trouble: we need a new reconcilied namespace. |
| 3726 | * This is expensive |
| 3727 | */ |
| 3728 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 3729 | } |
| 3730 | } |
| 3731 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3732 | } else |
| 3733 | ret->ns = NULL; |
| 3734 | |
| 3735 | if (cur->children != NULL) { |
| 3736 | xmlNodePtr tmp; |
| 3737 | |
| 3738 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 3739 | ret->last = NULL; |
| 3740 | tmp = ret->children; |
| 3741 | while (tmp != NULL) { |
| 3742 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 3743 | if (tmp->next == NULL) |
| 3744 | ret->last = tmp; |
| 3745 | tmp = tmp->next; |
| 3746 | } |
| 3747 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3748 | /* |
| 3749 | * Try to handle IDs |
| 3750 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 3751 | if ((target!= NULL) && (cur!= NULL) && |
| 3752 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3753 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 3754 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 3755 | xmlChar *id; |
| 3756 | |
| 3757 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 3758 | if (id != NULL) { |
| 3759 | xmlAddID(NULL, target->doc, id, ret); |
| 3760 | xmlFree(id); |
| 3761 | } |
| 3762 | } |
| 3763 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3764 | return(ret); |
| 3765 | } |
| 3766 | |
| 3767 | /** |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3768 | * xmlCopyProp: |
| 3769 | * @target: the element where the attribute will be grafted |
| 3770 | * @cur: the attribute |
| 3771 | * |
| 3772 | * Do a copy of the attribute. |
| 3773 | * |
| 3774 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
| 3775 | */ |
| 3776 | xmlAttrPtr |
| 3777 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 3778 | return xmlCopyPropInternal(NULL, target, cur); |
| 3779 | } |
| 3780 | |
| 3781 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3782 | * xmlCopyPropList: |
| 3783 | * @target: the element where the attributes will be grafted |
| 3784 | * @cur: the first attribute |
| 3785 | * |
| 3786 | * Do a copy of an attribute list. |
| 3787 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3788 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3789 | */ |
| 3790 | xmlAttrPtr |
| 3791 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 3792 | xmlAttrPtr ret = NULL; |
| 3793 | xmlAttrPtr p = NULL,q; |
| 3794 | |
| 3795 | while (cur != NULL) { |
| 3796 | q = xmlCopyProp(target, cur); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 3797 | if (q == NULL) |
| 3798 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3799 | if (p == NULL) { |
| 3800 | ret = p = q; |
| 3801 | } else { |
| 3802 | p->next = q; |
| 3803 | q->prev = p; |
| 3804 | p = q; |
| 3805 | } |
| 3806 | cur = cur->next; |
| 3807 | } |
| 3808 | return(ret); |
| 3809 | } |
| 3810 | |
| 3811 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3812 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3813 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3814 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3815 | * tricky reason: namespaces. Doing a direct copy of a node |
| 3816 | * say RPM:Copyright without changing the namespace pointer to |
| 3817 | * something else can produce stale links. One way to do it is |
| 3818 | * to keep a reference counter but this doesn't work as soon |
| 3819 | * as one move the element or the subtree out of the scope of |
| 3820 | * the existing namespace. The actual solution seems to add |
| 3821 | * a copy of the namespace at the top of the copied tree if |
| 3822 | * not available in the subtree. |
| 3823 | * Hence two functions, the public front-end call the inner ones |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3824 | * The argument "recursive" normally indicates a recursive copy |
| 3825 | * of the node with values 0 (no) and 1 (yes). For XInclude, |
| 3826 | * however, we allow a value of 2 to indicate copy properties and |
| 3827 | * namespace info, but don't recurse on children. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3828 | */ |
| 3829 | |
| 3830 | static xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3831 | xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3832 | int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3833 | xmlNodePtr ret; |
| 3834 | |
| 3835 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3836 | switch (node->type) { |
| 3837 | case XML_TEXT_NODE: |
| 3838 | case XML_CDATA_SECTION_NODE: |
| 3839 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 3840 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3841 | case XML_ENTITY_REF_NODE: |
| 3842 | case XML_ENTITY_NODE: |
| 3843 | case XML_PI_NODE: |
| 3844 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3845 | case XML_XINCLUDE_START: |
| 3846 | case XML_XINCLUDE_END: |
| 3847 | break; |
| 3848 | case XML_ATTRIBUTE_NODE: |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3849 | return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node)); |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3850 | case XML_NAMESPACE_DECL: |
| 3851 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
| 3852 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3853 | case XML_DOCUMENT_NODE: |
| 3854 | case XML_HTML_DOCUMENT_NODE: |
| 3855 | #ifdef LIBXML_DOCB_ENABLED |
| 3856 | case XML_DOCB_DOCUMENT_NODE: |
| 3857 | #endif |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3858 | #ifdef LIBXML_TREE_ENABLED |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3859 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended)); |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3860 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3861 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3862 | case XML_NOTATION_NODE: |
| 3863 | case XML_DTD_NODE: |
| 3864 | case XML_ELEMENT_DECL: |
| 3865 | case XML_ATTRIBUTE_DECL: |
| 3866 | case XML_ENTITY_DECL: |
| 3867 | return(NULL); |
| 3868 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3869 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3870 | /* |
| 3871 | * Allocate a new node and fill the fields. |
| 3872 | */ |
| 3873 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 3874 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 3875 | xmlTreeErrMemory("copying node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3876 | return(NULL); |
| 3877 | } |
| 3878 | memset(ret, 0, sizeof(xmlNode)); |
| 3879 | ret->type = node->type; |
| 3880 | |
| 3881 | ret->doc = doc; |
| 3882 | ret->parent = parent; |
| 3883 | if (node->name == xmlStringText) |
| 3884 | ret->name = xmlStringText; |
| 3885 | else if (node->name == xmlStringTextNoenc) |
| 3886 | ret->name = xmlStringTextNoenc; |
| 3887 | else if (node->name == xmlStringComment) |
| 3888 | ret->name = xmlStringComment; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 3889 | else if (node->name != NULL) { |
| 3890 | if ((doc != NULL) && (doc->dict != NULL)) |
| 3891 | ret->name = xmlDictLookup(doc->dict, node->name, -1); |
| 3892 | else |
| 3893 | ret->name = xmlStrdup(node->name); |
| 3894 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3895 | if ((node->type != XML_ELEMENT_NODE) && |
| 3896 | (node->content != NULL) && |
| 3897 | (node->type != XML_ENTITY_REF_NODE) && |
| 3898 | (node->type != XML_XINCLUDE_END) && |
| 3899 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3900 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3901 | }else{ |
| 3902 | if (node->type == XML_ELEMENT_NODE) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 3903 | ret->line = node->line; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3904 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3905 | if (parent != NULL) { |
| 3906 | xmlNodePtr tmp; |
| 3907 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3908 | /* |
| 3909 | * this is a tricky part for the node register thing: |
| 3910 | * in case ret does get coalesced in xmlAddChild |
| 3911 | * the deregister-node callback is called; so we register ret now already |
| 3912 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3913 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3914 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 3915 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3916 | tmp = xmlAddChild(parent, ret); |
| 3917 | /* node could have coalesced */ |
| 3918 | if (tmp != ret) |
| 3919 | return(tmp); |
| 3920 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3921 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3922 | if (!extended) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3923 | goto out; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3924 | if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3925 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 3926 | |
| 3927 | if (node->ns != NULL) { |
| 3928 | xmlNsPtr ns; |
| 3929 | |
| 3930 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 3931 | if (ns == NULL) { |
| 3932 | /* |
| 3933 | * Humm, we are copying an element whose namespace is defined |
| 3934 | * out of the new tree scope. Search it in the original tree |
| 3935 | * and add it at the top of the new tree |
| 3936 | */ |
| 3937 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 3938 | if (ns != NULL) { |
| 3939 | xmlNodePtr root = ret; |
| 3940 | |
| 3941 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 3942 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3943 | } |
| 3944 | } else { |
| 3945 | /* |
| 3946 | * reference the existing namespace definition in our own tree. |
| 3947 | */ |
| 3948 | ret->ns = ns; |
| 3949 | } |
| 3950 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3951 | if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3952 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3953 | if (node->type == XML_ENTITY_REF_NODE) { |
| 3954 | if ((doc == NULL) || (node->doc != doc)) { |
| 3955 | /* |
| 3956 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3957 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3958 | * we cannot keep the reference. Try to find it in the |
| 3959 | * target document. |
| 3960 | */ |
| 3961 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 3962 | } else { |
| 3963 | ret->children = node->children; |
| 3964 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3965 | ret->last = ret->children; |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3966 | } else if ((node->children != NULL) && (extended != 2)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3967 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3968 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 3969 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3970 | |
| 3971 | out: |
| 3972 | /* if parent != NULL we already registered the node above */ |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 3973 | if ((parent == NULL) && |
| 3974 | ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3975 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3976 | return(ret); |
| 3977 | } |
| 3978 | |
| 3979 | static xmlNodePtr |
| 3980 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 3981 | xmlNodePtr ret = NULL; |
| 3982 | xmlNodePtr p = NULL,q; |
| 3983 | |
| 3984 | while (node != NULL) { |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3985 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3986 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 3987 | if (doc == NULL) { |
| 3988 | node = node->next; |
| 3989 | continue; |
| 3990 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3991 | if (doc->intSubset == NULL) { |
| 3992 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
| 3993 | q->doc = doc; |
| 3994 | q->parent = parent; |
| 3995 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3996 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3997 | } else { |
| 3998 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3999 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4000 | } |
| 4001 | } else |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4002 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4003 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4004 | if (ret == NULL) { |
| 4005 | q->prev = NULL; |
| 4006 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4007 | } else if (p != q) { |
| 4008 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4009 | p->next = q; |
| 4010 | q->prev = p; |
| 4011 | p = q; |
| 4012 | } |
| 4013 | node = node->next; |
| 4014 | } |
| 4015 | return(ret); |
| 4016 | } |
| 4017 | |
| 4018 | /** |
| 4019 | * xmlCopyNode: |
| 4020 | * @node: the node |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4021 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4022 | * when applicable) |
| 4023 | * if 2 copy properties and namespaces (when applicable) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4024 | * |
| 4025 | * Do a copy of the node. |
| 4026 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4027 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4028 | */ |
| 4029 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4030 | xmlCopyNode(const xmlNodePtr node, int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4031 | xmlNodePtr ret; |
| 4032 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4033 | ret = xmlStaticCopyNode(node, NULL, NULL, extended); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4034 | return(ret); |
| 4035 | } |
| 4036 | |
| 4037 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4038 | * xmlDocCopyNode: |
| 4039 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4040 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4041 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4042 | * when applicable) |
| 4043 | * if 2 copy properties and namespaces (when applicable) |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4044 | * |
| 4045 | * Do a copy of the node to a given document. |
| 4046 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4047 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4048 | */ |
| 4049 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4050 | xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4051 | xmlNodePtr ret; |
| 4052 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4053 | ret = xmlStaticCopyNode(node, doc, NULL, extended); |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4054 | return(ret); |
| 4055 | } |
| 4056 | |
| 4057 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4058 | * xmlDocCopyNodeList: |
| 4059 | * @doc: the target document |
| 4060 | * @node: the first node in the list. |
| 4061 | * |
| 4062 | * Do a recursive copy of the node list. |
| 4063 | * |
| 4064 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
| 4065 | */ |
| 4066 | xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) { |
| 4067 | xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL); |
| 4068 | return(ret); |
| 4069 | } |
| 4070 | |
| 4071 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4072 | * xmlCopyNodeList: |
| 4073 | * @node: the first node in the list. |
| 4074 | * |
| 4075 | * Do a recursive copy of the node list. |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4076 | * Use xmlDocCopyNodeList() if possible to ensure string interning. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4077 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4078 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4079 | */ |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 4080 | xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4081 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 4082 | return(ret); |
| 4083 | } |
| 4084 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4085 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4086 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4087 | * xmlCopyDtd: |
| 4088 | * @dtd: the dtd |
| 4089 | * |
| 4090 | * Do a copy of the dtd. |
| 4091 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4092 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4093 | */ |
| 4094 | xmlDtdPtr |
| 4095 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 4096 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4097 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4098 | |
| 4099 | if (dtd == NULL) return(NULL); |
| 4100 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 4101 | if (ret == NULL) return(NULL); |
| 4102 | if (dtd->entities != NULL) |
| 4103 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 4104 | (xmlEntitiesTablePtr) dtd->entities); |
| 4105 | if (dtd->notations != NULL) |
| 4106 | ret->notations = (void *) xmlCopyNotationTable( |
| 4107 | (xmlNotationTablePtr) dtd->notations); |
| 4108 | if (dtd->elements != NULL) |
| 4109 | ret->elements = (void *) xmlCopyElementTable( |
| 4110 | (xmlElementTablePtr) dtd->elements); |
| 4111 | if (dtd->attributes != NULL) |
| 4112 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 4113 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4114 | if (dtd->pentities != NULL) |
| 4115 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 4116 | (xmlEntitiesTablePtr) dtd->pentities); |
| 4117 | |
| 4118 | cur = dtd->children; |
| 4119 | while (cur != NULL) { |
| 4120 | q = NULL; |
| 4121 | |
| 4122 | if (cur->type == XML_ENTITY_DECL) { |
| 4123 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 4124 | switch (tmp->etype) { |
| 4125 | case XML_INTERNAL_GENERAL_ENTITY: |
| 4126 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 4127 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 4128 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 4129 | break; |
| 4130 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 4131 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 4132 | q = (xmlNodePtr) |
| 4133 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 4134 | break; |
| 4135 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 4136 | break; |
| 4137 | } |
| 4138 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 4139 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 4140 | q = (xmlNodePtr) |
| 4141 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 4142 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4143 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
| 4144 | q = (xmlNodePtr) |
| 4145 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 4146 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4147 | q = xmlCopyNode(cur, 0); |
| 4148 | } |
| 4149 | |
| 4150 | if (q == NULL) { |
| 4151 | cur = cur->next; |
| 4152 | continue; |
| 4153 | } |
| 4154 | |
| 4155 | if (p == NULL) |
| 4156 | ret->children = q; |
| 4157 | else |
| 4158 | p->next = q; |
| 4159 | |
| 4160 | q->prev = p; |
| 4161 | q->parent = (xmlNodePtr) ret; |
| 4162 | q->next = NULL; |
| 4163 | ret->last = q; |
| 4164 | p = q; |
| 4165 | cur = cur->next; |
| 4166 | } |
| 4167 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4168 | return(ret); |
| 4169 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4170 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4171 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4172 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4173 | /** |
| 4174 | * xmlCopyDoc: |
| 4175 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4176 | * @recursive: if not zero do a recursive copy. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4177 | * |
| 4178 | * 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] | 4179 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4180 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4181 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4182 | */ |
| 4183 | xmlDocPtr |
| 4184 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 4185 | xmlDocPtr ret; |
| 4186 | |
| 4187 | if (doc == NULL) return(NULL); |
| 4188 | ret = xmlNewDoc(doc->version); |
| 4189 | if (ret == NULL) return(NULL); |
| 4190 | if (doc->name != NULL) |
| 4191 | ret->name = xmlMemStrdup(doc->name); |
| 4192 | if (doc->encoding != NULL) |
| 4193 | ret->encoding = xmlStrdup(doc->encoding); |
Daniel Veillard | f59507d | 2005-01-27 17:26:49 +0000 | [diff] [blame] | 4194 | if (doc->URL != NULL) |
| 4195 | ret->URL = xmlStrdup(doc->URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4196 | ret->charset = doc->charset; |
| 4197 | ret->compression = doc->compression; |
| 4198 | ret->standalone = doc->standalone; |
| 4199 | if (!recursive) return(ret); |
| 4200 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4201 | ret->last = NULL; |
| 4202 | ret->children = NULL; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4203 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4204 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4205 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4206 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4207 | ret->intSubset->parent = ret; |
| 4208 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4209 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4210 | if (doc->oldNs != NULL) |
| 4211 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 4212 | if (doc->children != NULL) { |
| 4213 | xmlNodePtr tmp; |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4214 | |
| 4215 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 4216 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4217 | ret->last = NULL; |
| 4218 | tmp = ret->children; |
| 4219 | while (tmp != NULL) { |
| 4220 | if (tmp->next == NULL) |
| 4221 | ret->last = tmp; |
| 4222 | tmp = tmp->next; |
| 4223 | } |
| 4224 | } |
| 4225 | return(ret); |
| 4226 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4227 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4228 | |
| 4229 | /************************************************************************ |
| 4230 | * * |
| 4231 | * Content access functions * |
| 4232 | * * |
| 4233 | ************************************************************************/ |
| 4234 | |
| 4235 | /** |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4236 | * xmlGetLineNo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4237 | * @node: valid node |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4238 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 4239 | * Get line number of @node. This requires activation of this option |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4240 | * before invoking the parser by calling xmlLineNumbersDefault(1) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4241 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4242 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4243 | */ |
| 4244 | long |
| 4245 | xmlGetLineNo(xmlNodePtr node) |
| 4246 | { |
| 4247 | long result = -1; |
| 4248 | |
| 4249 | if (!node) |
| 4250 | return result; |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4251 | if ((node->type == XML_ELEMENT_NODE) || |
| 4252 | (node->type == XML_TEXT_NODE) || |
| 4253 | (node->type == XML_COMMENT_NODE) || |
| 4254 | (node->type == XML_PI_NODE)) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 4255 | result = (long) node->line; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4256 | else if ((node->prev != NULL) && |
| 4257 | ((node->prev->type == XML_ELEMENT_NODE) || |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4258 | (node->prev->type == XML_TEXT_NODE) || |
| 4259 | (node->prev->type == XML_COMMENT_NODE) || |
| 4260 | (node->prev->type == XML_PI_NODE))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4261 | result = xmlGetLineNo(node->prev); |
| 4262 | else if ((node->parent != NULL) && |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4263 | (node->parent->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4264 | result = xmlGetLineNo(node->parent); |
| 4265 | |
| 4266 | return result; |
| 4267 | } |
| 4268 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4269 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4270 | /** |
| 4271 | * xmlGetNodePath: |
| 4272 | * @node: a node |
| 4273 | * |
| 4274 | * Build a structure based Path for the given node |
| 4275 | * |
| 4276 | * Returns the new path or NULL in case of error. The caller must free |
| 4277 | * the returned string |
| 4278 | */ |
| 4279 | xmlChar * |
| 4280 | xmlGetNodePath(xmlNodePtr node) |
| 4281 | { |
| 4282 | xmlNodePtr cur, tmp, next; |
| 4283 | xmlChar *buffer = NULL, *temp; |
| 4284 | size_t buf_len; |
| 4285 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4286 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4287 | const char *name; |
| 4288 | char nametemp[100]; |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4289 | int occur = 0, generic; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4290 | |
| 4291 | if (node == NULL) |
| 4292 | return (NULL); |
| 4293 | |
| 4294 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4295 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4296 | if (buffer == NULL) { |
| 4297 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4298 | return (NULL); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4299 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4300 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4301 | if (buf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4302 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4303 | xmlFree(buffer); |
| 4304 | return (NULL); |
| 4305 | } |
| 4306 | |
| 4307 | buffer[0] = 0; |
| 4308 | cur = node; |
| 4309 | do { |
| 4310 | name = ""; |
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 | occur = 0; |
| 4313 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4314 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4315 | if (buffer[0] == '/') |
| 4316 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4317 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4318 | next = NULL; |
| 4319 | } else if (cur->type == XML_ELEMENT_NODE) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4320 | generic = 0; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4321 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4322 | name = (const char *) cur->name; |
| 4323 | if (cur->ns) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4324 | if (cur->ns->prefix != NULL) { |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4325 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
| 4326 | (char *)cur->ns->prefix, (char *)cur->name); |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4327 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4328 | name = nametemp; |
| 4329 | } else { |
| 4330 | /* |
| 4331 | * We cannot express named elements in the default |
| 4332 | * namespace, so use "*". |
| 4333 | */ |
| 4334 | generic = 1; |
| 4335 | name = "*"; |
| 4336 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4337 | } |
| 4338 | next = cur->parent; |
| 4339 | |
| 4340 | /* |
| 4341 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4342 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4343 | */ |
| 4344 | tmp = cur->prev; |
| 4345 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4346 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4347 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4348 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4349 | ((tmp->ns == cur->ns) || |
| 4350 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4351 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4352 | occur++; |
| 4353 | tmp = tmp->prev; |
| 4354 | } |
| 4355 | if (occur == 0) { |
| 4356 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4357 | while (tmp != NULL && occur == 0) { |
| 4358 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4359 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4360 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4361 | ((tmp->ns == cur->ns) || |
| 4362 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4363 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4364 | occur++; |
| 4365 | tmp = tmp->next; |
| 4366 | } |
| 4367 | if (occur != 0) |
| 4368 | occur = 1; |
| 4369 | } else |
| 4370 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4371 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4372 | sep = "/"; |
| 4373 | name = "comment()"; |
| 4374 | next = cur->parent; |
| 4375 | |
| 4376 | /* |
| 4377 | * Thumbler index computation |
| 4378 | */ |
| 4379 | tmp = cur->prev; |
| 4380 | while (tmp != NULL) { |
| 4381 | if (tmp->type == XML_COMMENT_NODE) |
| 4382 | occur++; |
| 4383 | tmp = tmp->prev; |
| 4384 | } |
| 4385 | if (occur == 0) { |
| 4386 | tmp = cur->next; |
| 4387 | while (tmp != NULL && occur == 0) { |
| 4388 | if (tmp->type == XML_COMMENT_NODE) |
| 4389 | occur++; |
| 4390 | tmp = tmp->next; |
| 4391 | } |
| 4392 | if (occur != 0) |
| 4393 | occur = 1; |
| 4394 | } else |
| 4395 | occur++; |
| 4396 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4397 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4398 | sep = "/"; |
| 4399 | name = "text()"; |
| 4400 | next = cur->parent; |
| 4401 | |
| 4402 | /* |
| 4403 | * Thumbler index computation |
| 4404 | */ |
| 4405 | tmp = cur->prev; |
| 4406 | while (tmp != NULL) { |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4407 | if ((tmp->type == XML_TEXT_NODE) || |
| 4408 | (tmp->type == XML_CDATA_SECTION_NODE)) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4409 | occur++; |
| 4410 | tmp = tmp->prev; |
| 4411 | } |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4412 | /* |
| 4413 | * Evaluate if this is the only text- or CDATA-section-node; |
| 4414 | * if yes, then we'll get "text()", otherwise "text()[1]". |
| 4415 | */ |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4416 | if (occur == 0) { |
| 4417 | tmp = cur->next; |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4418 | while (tmp != NULL) { |
| 4419 | if ((tmp->type == XML_TEXT_NODE) || |
| 4420 | (tmp->type == XML_CDATA_SECTION_NODE)) |
| 4421 | { |
| 4422 | occur = 1; |
| 4423 | break; |
| 4424 | } |
| 4425 | tmp = tmp->next; |
| 4426 | } |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4427 | } else |
| 4428 | occur++; |
| 4429 | } else if (cur->type == XML_PI_NODE) { |
| 4430 | sep = "/"; |
| 4431 | snprintf(nametemp, sizeof(nametemp) - 1, |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4432 | "processing-instruction('%s')", (char *)cur->name); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4433 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4434 | name = nametemp; |
| 4435 | |
| 4436 | next = cur->parent; |
| 4437 | |
| 4438 | /* |
| 4439 | * Thumbler index computation |
| 4440 | */ |
| 4441 | tmp = cur->prev; |
| 4442 | while (tmp != NULL) { |
| 4443 | if ((tmp->type == XML_PI_NODE) && |
| 4444 | (xmlStrEqual(cur->name, tmp->name))) |
| 4445 | occur++; |
| 4446 | tmp = tmp->prev; |
| 4447 | } |
| 4448 | if (occur == 0) { |
| 4449 | tmp = cur->next; |
| 4450 | while (tmp != NULL && occur == 0) { |
| 4451 | if ((tmp->type == XML_PI_NODE) && |
| 4452 | (xmlStrEqual(cur->name, tmp->name))) |
| 4453 | occur++; |
| 4454 | tmp = tmp->next; |
| 4455 | } |
| 4456 | if (occur != 0) |
| 4457 | occur = 1; |
| 4458 | } else |
| 4459 | occur++; |
| 4460 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4461 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4462 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4463 | name = (const char *) (((xmlAttrPtr) cur)->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4464 | if (cur->ns) { |
| 4465 | if (cur->ns->prefix != NULL) |
| 4466 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
| 4467 | (char *)cur->ns->prefix, (char *)cur->name); |
| 4468 | else |
| 4469 | snprintf(nametemp, sizeof(nametemp) - 1, "%s", |
| 4470 | (char *)cur->name); |
| 4471 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4472 | name = nametemp; |
| 4473 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4474 | next = ((xmlAttrPtr) cur)->parent; |
| 4475 | } else { |
| 4476 | next = cur->parent; |
| 4477 | } |
| 4478 | |
| 4479 | /* |
| 4480 | * Make sure there is enough room |
| 4481 | */ |
| 4482 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4483 | buf_len = |
| 4484 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4485 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4486 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4487 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4488 | xmlFree(buf); |
| 4489 | xmlFree(buffer); |
| 4490 | return (NULL); |
| 4491 | } |
| 4492 | buffer = temp; |
| 4493 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4494 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4495 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4496 | xmlFree(buf); |
| 4497 | xmlFree(buffer); |
| 4498 | return (NULL); |
| 4499 | } |
| 4500 | buf = temp; |
| 4501 | } |
| 4502 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4503 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4504 | sep, name, (char *) buffer); |
| 4505 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4506 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4507 | sep, name, occur, (char *) buffer); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4508 | snprintf((char *) buffer, buf_len, "%s", (char *)buf); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4509 | cur = next; |
| 4510 | } while (cur != NULL); |
| 4511 | xmlFree(buf); |
| 4512 | return (buffer); |
| 4513 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4514 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4515 | |
| 4516 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4517 | * xmlDocGetRootElement: |
| 4518 | * @doc: the document |
| 4519 | * |
| 4520 | * Get the root element of the document (doc->children is a list |
| 4521 | * containing possibly comments, PIs, etc ...). |
| 4522 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4523 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4524 | */ |
| 4525 | xmlNodePtr |
| 4526 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 4527 | xmlNodePtr ret; |
| 4528 | |
| 4529 | if (doc == NULL) return(NULL); |
| 4530 | ret = doc->children; |
| 4531 | while (ret != NULL) { |
| 4532 | if (ret->type == XML_ELEMENT_NODE) |
| 4533 | return(ret); |
| 4534 | ret = ret->next; |
| 4535 | } |
| 4536 | return(ret); |
| 4537 | } |
| 4538 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4539 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4540 | /** |
| 4541 | * xmlDocSetRootElement: |
| 4542 | * @doc: the document |
Daniel Veillard | 26a45c8 | 2006-10-20 12:55:34 +0000 | [diff] [blame] | 4543 | * @root: the new document root element, if root is NULL no action is taken, |
| 4544 | * to remove a node from a document use xmlUnlinkNode(root) instead. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4545 | * |
| 4546 | * Set the root element of the document (doc->children is a list |
| 4547 | * containing possibly comments, PIs, etc ...). |
| 4548 | * |
Daniel Veillard | 26a45c8 | 2006-10-20 12:55:34 +0000 | [diff] [blame] | 4549 | * Returns the old root element if any was found, NULL if root was NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4550 | */ |
| 4551 | xmlNodePtr |
| 4552 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4553 | xmlNodePtr old = NULL; |
| 4554 | |
| 4555 | if (doc == NULL) return(NULL); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4556 | if (root == NULL) |
| 4557 | return(NULL); |
| 4558 | xmlUnlinkNode(root); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 4559 | xmlSetTreeDoc(root, doc); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4560 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4561 | old = doc->children; |
| 4562 | while (old != NULL) { |
| 4563 | if (old->type == XML_ELEMENT_NODE) |
| 4564 | break; |
| 4565 | old = old->next; |
| 4566 | } |
| 4567 | if (old == NULL) { |
| 4568 | if (doc->children == NULL) { |
| 4569 | doc->children = root; |
| 4570 | doc->last = root; |
| 4571 | } else { |
| 4572 | xmlAddSibling(doc->children, root); |
| 4573 | } |
| 4574 | } else { |
| 4575 | xmlReplaceNode(old, root); |
| 4576 | } |
| 4577 | return(old); |
| 4578 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4579 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4580 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4581 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4582 | /** |
| 4583 | * xmlNodeSetLang: |
| 4584 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4585 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4586 | * |
| 4587 | * Set the language of a node, i.e. the values of the xml:lang |
| 4588 | * attribute. |
| 4589 | */ |
| 4590 | void |
| 4591 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4592 | xmlNsPtr ns; |
| 4593 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4594 | if (cur == NULL) return; |
| 4595 | switch(cur->type) { |
| 4596 | case XML_TEXT_NODE: |
| 4597 | case XML_CDATA_SECTION_NODE: |
| 4598 | case XML_COMMENT_NODE: |
| 4599 | case XML_DOCUMENT_NODE: |
| 4600 | case XML_DOCUMENT_TYPE_NODE: |
| 4601 | case XML_DOCUMENT_FRAG_NODE: |
| 4602 | case XML_NOTATION_NODE: |
| 4603 | case XML_HTML_DOCUMENT_NODE: |
| 4604 | case XML_DTD_NODE: |
| 4605 | case XML_ELEMENT_DECL: |
| 4606 | case XML_ATTRIBUTE_DECL: |
| 4607 | case XML_ENTITY_DECL: |
| 4608 | case XML_PI_NODE: |
| 4609 | case XML_ENTITY_REF_NODE: |
| 4610 | case XML_ENTITY_NODE: |
| 4611 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4612 | #ifdef LIBXML_DOCB_ENABLED |
| 4613 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4614 | #endif |
| 4615 | case XML_XINCLUDE_START: |
| 4616 | case XML_XINCLUDE_END: |
| 4617 | return; |
| 4618 | case XML_ELEMENT_NODE: |
| 4619 | case XML_ATTRIBUTE_NODE: |
| 4620 | break; |
| 4621 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4622 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4623 | if (ns == NULL) |
| 4624 | return; |
| 4625 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4626 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4627 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4628 | |
| 4629 | /** |
| 4630 | * xmlNodeGetLang: |
| 4631 | * @cur: the node being checked |
| 4632 | * |
| 4633 | * Searches the language of a node, i.e. the values of the xml:lang |
| 4634 | * attribute or the one carried by the nearest ancestor. |
| 4635 | * |
| 4636 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4637 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4638 | */ |
| 4639 | xmlChar * |
| 4640 | xmlNodeGetLang(xmlNodePtr cur) { |
| 4641 | xmlChar *lang; |
| 4642 | |
| 4643 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 4644 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4645 | if (lang != NULL) |
| 4646 | return(lang); |
| 4647 | cur = cur->parent; |
| 4648 | } |
| 4649 | return(NULL); |
| 4650 | } |
| 4651 | |
| 4652 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4653 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4654 | /** |
| 4655 | * xmlNodeSetSpacePreserve: |
| 4656 | * @cur: the node being changed |
| 4657 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 4658 | * |
| 4659 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 4660 | * value of the xml:space attribute. |
| 4661 | */ |
| 4662 | void |
| 4663 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4664 | xmlNsPtr ns; |
| 4665 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4666 | if (cur == NULL) return; |
| 4667 | switch(cur->type) { |
| 4668 | case XML_TEXT_NODE: |
| 4669 | case XML_CDATA_SECTION_NODE: |
| 4670 | case XML_COMMENT_NODE: |
| 4671 | case XML_DOCUMENT_NODE: |
| 4672 | case XML_DOCUMENT_TYPE_NODE: |
| 4673 | case XML_DOCUMENT_FRAG_NODE: |
| 4674 | case XML_NOTATION_NODE: |
| 4675 | case XML_HTML_DOCUMENT_NODE: |
| 4676 | case XML_DTD_NODE: |
| 4677 | case XML_ELEMENT_DECL: |
| 4678 | case XML_ATTRIBUTE_DECL: |
| 4679 | case XML_ENTITY_DECL: |
| 4680 | case XML_PI_NODE: |
| 4681 | case XML_ENTITY_REF_NODE: |
| 4682 | case XML_ENTITY_NODE: |
| 4683 | case XML_NAMESPACE_DECL: |
| 4684 | case XML_XINCLUDE_START: |
| 4685 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4686 | #ifdef LIBXML_DOCB_ENABLED |
| 4687 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4688 | #endif |
| 4689 | return; |
| 4690 | case XML_ELEMENT_NODE: |
| 4691 | case XML_ATTRIBUTE_NODE: |
| 4692 | break; |
| 4693 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4694 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4695 | if (ns == NULL) |
| 4696 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4697 | switch (val) { |
| 4698 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4699 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4700 | break; |
| 4701 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4702 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4703 | break; |
| 4704 | } |
| 4705 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4706 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4707 | |
| 4708 | /** |
| 4709 | * xmlNodeGetSpacePreserve: |
| 4710 | * @cur: the node being checked |
| 4711 | * |
| 4712 | * Searches the space preserving behaviour of a node, i.e. the values |
| 4713 | * of the xml:space attribute or the one carried by the nearest |
| 4714 | * ancestor. |
| 4715 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4716 | * 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] | 4717 | */ |
| 4718 | int |
| 4719 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 4720 | xmlChar *space; |
| 4721 | |
| 4722 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4723 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4724 | if (space != NULL) { |
| 4725 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 4726 | xmlFree(space); |
| 4727 | return(1); |
| 4728 | } |
| 4729 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 4730 | xmlFree(space); |
| 4731 | return(0); |
| 4732 | } |
| 4733 | xmlFree(space); |
| 4734 | } |
| 4735 | cur = cur->parent; |
| 4736 | } |
| 4737 | return(-1); |
| 4738 | } |
| 4739 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4740 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4741 | /** |
| 4742 | * xmlNodeSetName: |
| 4743 | * @cur: the node being changed |
| 4744 | * @name: the new tag name |
| 4745 | * |
| 4746 | * Set (or reset) the name of a node. |
| 4747 | */ |
| 4748 | void |
| 4749 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 4750 | xmlDocPtr doc; |
| 4751 | xmlDictPtr dict; |
| 4752 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4753 | if (cur == NULL) return; |
| 4754 | if (name == NULL) return; |
| 4755 | switch(cur->type) { |
| 4756 | case XML_TEXT_NODE: |
| 4757 | case XML_CDATA_SECTION_NODE: |
| 4758 | case XML_COMMENT_NODE: |
| 4759 | case XML_DOCUMENT_TYPE_NODE: |
| 4760 | case XML_DOCUMENT_FRAG_NODE: |
| 4761 | case XML_NOTATION_NODE: |
| 4762 | case XML_HTML_DOCUMENT_NODE: |
| 4763 | case XML_NAMESPACE_DECL: |
| 4764 | case XML_XINCLUDE_START: |
| 4765 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4766 | #ifdef LIBXML_DOCB_ENABLED |
| 4767 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4768 | #endif |
| 4769 | return; |
| 4770 | case XML_ELEMENT_NODE: |
| 4771 | case XML_ATTRIBUTE_NODE: |
| 4772 | case XML_PI_NODE: |
| 4773 | case XML_ENTITY_REF_NODE: |
| 4774 | case XML_ENTITY_NODE: |
| 4775 | case XML_DTD_NODE: |
| 4776 | case XML_DOCUMENT_NODE: |
| 4777 | case XML_ELEMENT_DECL: |
| 4778 | case XML_ATTRIBUTE_DECL: |
| 4779 | case XML_ENTITY_DECL: |
| 4780 | break; |
| 4781 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 4782 | doc = cur->doc; |
| 4783 | if (doc != NULL) |
| 4784 | dict = doc->dict; |
| 4785 | else |
| 4786 | dict = NULL; |
| 4787 | if (dict != NULL) { |
| 4788 | if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name))) |
| 4789 | xmlFree((xmlChar *) cur->name); |
| 4790 | cur->name = xmlDictLookup(dict, name, -1); |
| 4791 | } else { |
| 4792 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 4793 | cur->name = xmlStrdup(name); |
| 4794 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4795 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4796 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4797 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4798 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4799 | /** |
| 4800 | * xmlNodeSetBase: |
| 4801 | * @cur: the node being changed |
| 4802 | * @uri: the new base URI |
| 4803 | * |
| 4804 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 4805 | * xml:base attribute. |
| 4806 | */ |
| 4807 | void |
Daniel Veillard | f85ce8e | 2003-09-22 10:24:45 +0000 | [diff] [blame] | 4808 | xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4809 | xmlNsPtr ns; |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 4810 | const xmlChar* fixed; |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4811 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4812 | if (cur == NULL) return; |
| 4813 | switch(cur->type) { |
| 4814 | case XML_TEXT_NODE: |
| 4815 | case XML_CDATA_SECTION_NODE: |
| 4816 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4817 | case XML_DOCUMENT_TYPE_NODE: |
| 4818 | case XML_DOCUMENT_FRAG_NODE: |
| 4819 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4820 | case XML_DTD_NODE: |
| 4821 | case XML_ELEMENT_DECL: |
| 4822 | case XML_ATTRIBUTE_DECL: |
| 4823 | case XML_ENTITY_DECL: |
| 4824 | case XML_PI_NODE: |
| 4825 | case XML_ENTITY_REF_NODE: |
| 4826 | case XML_ENTITY_NODE: |
| 4827 | case XML_NAMESPACE_DECL: |
| 4828 | case XML_XINCLUDE_START: |
| 4829 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4830 | return; |
| 4831 | case XML_ELEMENT_NODE: |
| 4832 | case XML_ATTRIBUTE_NODE: |
| 4833 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 4834 | case XML_DOCUMENT_NODE: |
| 4835 | #ifdef LIBXML_DOCB_ENABLED |
| 4836 | case XML_DOCB_DOCUMENT_NODE: |
| 4837 | #endif |
| 4838 | case XML_HTML_DOCUMENT_NODE: { |
| 4839 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 4840 | |
| 4841 | if (doc->URL != NULL) |
| 4842 | xmlFree((xmlChar *) doc->URL); |
| 4843 | if (uri == NULL) |
| 4844 | doc->URL = NULL; |
| 4845 | else |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 4846 | doc->URL = xmlPathToURI(uri); |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 4847 | return; |
| 4848 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4849 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4850 | |
| 4851 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4852 | if (ns == NULL) |
| 4853 | return; |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 4854 | fixed = xmlPathToURI(uri); |
| 4855 | if (fixed != NULL) { |
| 4856 | xmlSetNsProp(cur, ns, BAD_CAST "base", fixed); |
William M. Brack | 38d452a | 2007-05-22 16:00:06 +0000 | [diff] [blame] | 4857 | xmlFree((xmlChar *)fixed); |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 4858 | } else { |
| 4859 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
| 4860 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4861 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4862 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4863 | |
| 4864 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4865 | * xmlNodeGetBase: |
| 4866 | * @doc: the document the node pertains to |
| 4867 | * @cur: the node being checked |
| 4868 | * |
| 4869 | * Searches for the BASE URL. The code should work on both XML |
| 4870 | * and HTML document even if base mechanisms are completely different. |
| 4871 | * It returns the base as defined in RFC 2396 sections |
| 4872 | * 5.1.1. Base URI within Document Content |
| 4873 | * and |
| 4874 | * 5.1.2. Base URI from the Encapsulating Entity |
| 4875 | * However it does not return the document base (5.1.3), use |
| 4876 | * xmlDocumentGetBase() for this |
| 4877 | * |
| 4878 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4879 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4880 | */ |
| 4881 | xmlChar * |
| 4882 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4883 | xmlChar *oldbase = NULL; |
| 4884 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4885 | |
| 4886 | if ((cur == NULL) && (doc == NULL)) |
| 4887 | return(NULL); |
| 4888 | if (doc == NULL) doc = cur->doc; |
| 4889 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 4890 | cur = doc->children; |
| 4891 | while ((cur != NULL) && (cur->name != NULL)) { |
| 4892 | if (cur->type != XML_ELEMENT_NODE) { |
| 4893 | cur = cur->next; |
| 4894 | continue; |
| 4895 | } |
| 4896 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 4897 | cur = cur->children; |
| 4898 | continue; |
| 4899 | } |
| 4900 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 4901 | cur = cur->children; |
| 4902 | continue; |
| 4903 | } |
| 4904 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 4905 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 4906 | } |
| 4907 | cur = cur->next; |
| 4908 | } |
| 4909 | return(NULL); |
| 4910 | } |
| 4911 | while (cur != NULL) { |
| 4912 | if (cur->type == XML_ENTITY_DECL) { |
| 4913 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 4914 | return(xmlStrdup(ent->URI)); |
| 4915 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4916 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4917 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4918 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4919 | if (oldbase != NULL) { |
| 4920 | newbase = xmlBuildURI(oldbase, base); |
| 4921 | if (newbase != NULL) { |
| 4922 | xmlFree(oldbase); |
| 4923 | xmlFree(base); |
| 4924 | oldbase = newbase; |
| 4925 | } else { |
| 4926 | xmlFree(oldbase); |
| 4927 | xmlFree(base); |
| 4928 | return(NULL); |
| 4929 | } |
| 4930 | } else { |
| 4931 | oldbase = base; |
| 4932 | } |
| 4933 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 4934 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 4935 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 4936 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4937 | } |
| 4938 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4939 | cur = cur->parent; |
| 4940 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4941 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 4942 | if (oldbase == NULL) |
| 4943 | return(xmlStrdup(doc->URL)); |
| 4944 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 4945 | xmlFree(oldbase); |
| 4946 | return(newbase); |
| 4947 | } |
| 4948 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | /** |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 4952 | * xmlNodeBufGetContent: |
| 4953 | * @buffer: a buffer |
| 4954 | * @cur: the node being read |
| 4955 | * |
| 4956 | * Read the value of a node @cur, this can be either the text carried |
| 4957 | * directly by this node if it's a TEXT node or the aggregate string |
| 4958 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 4959 | * Entity references are substituted. |
| 4960 | * Fills up the buffer @buffer with this value |
| 4961 | * |
| 4962 | * Returns 0 in case of success and -1 in case of error. |
| 4963 | */ |
| 4964 | int |
| 4965 | xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur) |
| 4966 | { |
| 4967 | if ((cur == NULL) || (buffer == NULL)) return(-1); |
| 4968 | switch (cur->type) { |
| 4969 | case XML_CDATA_SECTION_NODE: |
| 4970 | case XML_TEXT_NODE: |
| 4971 | xmlBufferCat(buffer, cur->content); |
| 4972 | break; |
| 4973 | case XML_DOCUMENT_FRAG_NODE: |
| 4974 | case XML_ELEMENT_NODE:{ |
| 4975 | xmlNodePtr tmp = cur; |
| 4976 | |
| 4977 | while (tmp != NULL) { |
| 4978 | switch (tmp->type) { |
| 4979 | case XML_CDATA_SECTION_NODE: |
| 4980 | case XML_TEXT_NODE: |
| 4981 | if (tmp->content != NULL) |
| 4982 | xmlBufferCat(buffer, tmp->content); |
| 4983 | break; |
| 4984 | case XML_ENTITY_REF_NODE: |
Rob Richards | 77b92ff | 2005-12-20 15:55:14 +0000 | [diff] [blame] | 4985 | xmlNodeBufGetContent(buffer, tmp); |
| 4986 | break; |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 4987 | default: |
| 4988 | break; |
| 4989 | } |
| 4990 | /* |
| 4991 | * Skip to next node |
| 4992 | */ |
| 4993 | if (tmp->children != NULL) { |
| 4994 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4995 | tmp = tmp->children; |
| 4996 | continue; |
| 4997 | } |
| 4998 | } |
| 4999 | if (tmp == cur) |
| 5000 | break; |
| 5001 | |
| 5002 | if (tmp->next != NULL) { |
| 5003 | tmp = tmp->next; |
| 5004 | continue; |
| 5005 | } |
| 5006 | |
| 5007 | do { |
| 5008 | tmp = tmp->parent; |
| 5009 | if (tmp == NULL) |
| 5010 | break; |
| 5011 | if (tmp == cur) { |
| 5012 | tmp = NULL; |
| 5013 | break; |
| 5014 | } |
| 5015 | if (tmp->next != NULL) { |
| 5016 | tmp = tmp->next; |
| 5017 | break; |
| 5018 | } |
| 5019 | } while (tmp != NULL); |
| 5020 | } |
| 5021 | break; |
| 5022 | } |
| 5023 | case XML_ATTRIBUTE_NODE:{ |
| 5024 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 5025 | xmlNodePtr tmp = attr->children; |
| 5026 | |
| 5027 | while (tmp != NULL) { |
| 5028 | if (tmp->type == XML_TEXT_NODE) |
| 5029 | xmlBufferCat(buffer, tmp->content); |
| 5030 | else |
| 5031 | xmlNodeBufGetContent(buffer, tmp); |
| 5032 | tmp = tmp->next; |
| 5033 | } |
| 5034 | break; |
| 5035 | } |
| 5036 | case XML_COMMENT_NODE: |
| 5037 | case XML_PI_NODE: |
| 5038 | xmlBufferCat(buffer, cur->content); |
| 5039 | break; |
| 5040 | case XML_ENTITY_REF_NODE:{ |
| 5041 | xmlEntityPtr ent; |
| 5042 | xmlNodePtr tmp; |
| 5043 | |
| 5044 | /* lookup entity declaration */ |
| 5045 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5046 | if (ent == NULL) |
| 5047 | return(-1); |
| 5048 | |
| 5049 | /* an entity content can be any "well balanced chunk", |
| 5050 | * i.e. the result of the content [43] production: |
| 5051 | * http://www.w3.org/TR/REC-xml#NT-content |
| 5052 | * -> we iterate through child nodes and recursive call |
| 5053 | * xmlNodeGetContent() which handles all possible node types */ |
| 5054 | tmp = ent->children; |
| 5055 | while (tmp) { |
| 5056 | xmlNodeBufGetContent(buffer, tmp); |
| 5057 | tmp = tmp->next; |
| 5058 | } |
| 5059 | break; |
| 5060 | } |
| 5061 | case XML_ENTITY_NODE: |
| 5062 | case XML_DOCUMENT_TYPE_NODE: |
| 5063 | case XML_NOTATION_NODE: |
| 5064 | case XML_DTD_NODE: |
| 5065 | case XML_XINCLUDE_START: |
| 5066 | case XML_XINCLUDE_END: |
| 5067 | break; |
| 5068 | case XML_DOCUMENT_NODE: |
| 5069 | #ifdef LIBXML_DOCB_ENABLED |
| 5070 | case XML_DOCB_DOCUMENT_NODE: |
| 5071 | #endif |
| 5072 | case XML_HTML_DOCUMENT_NODE: |
| 5073 | cur = cur->children; |
| 5074 | while (cur!= NULL) { |
| 5075 | if ((cur->type == XML_ELEMENT_NODE) || |
| 5076 | (cur->type == XML_TEXT_NODE) || |
| 5077 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 5078 | xmlNodeBufGetContent(buffer, cur); |
| 5079 | } |
| 5080 | cur = cur->next; |
| 5081 | } |
| 5082 | break; |
| 5083 | case XML_NAMESPACE_DECL: |
| 5084 | xmlBufferCat(buffer, ((xmlNsPtr) cur)->href); |
| 5085 | break; |
| 5086 | case XML_ELEMENT_DECL: |
| 5087 | case XML_ATTRIBUTE_DECL: |
| 5088 | case XML_ENTITY_DECL: |
| 5089 | break; |
| 5090 | } |
| 5091 | return(0); |
| 5092 | } |
| 5093 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5094 | * xmlNodeGetContent: |
| 5095 | * @cur: the node being read |
| 5096 | * |
| 5097 | * Read the value of a node, this can be either the text carried |
| 5098 | * directly by this node if it's a TEXT node or the aggregate string |
| 5099 | * 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] | 5100 | * Entity references are substituted. |
| 5101 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5102 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5103 | */ |
| 5104 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5105 | xmlNodeGetContent(xmlNodePtr cur) |
| 5106 | { |
| 5107 | if (cur == NULL) |
| 5108 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5109 | switch (cur->type) { |
| 5110 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5111 | case XML_ELEMENT_NODE:{ |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5112 | xmlBufferPtr buffer; |
| 5113 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5114 | |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 5115 | buffer = xmlBufferCreateSize(64); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5116 | if (buffer == NULL) |
| 5117 | return (NULL); |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5118 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5119 | ret = buffer->content; |
| 5120 | buffer->content = NULL; |
| 5121 | xmlBufferFree(buffer); |
| 5122 | return (ret); |
| 5123 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5124 | case XML_ATTRIBUTE_NODE: |
| 5125 | return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5126 | case XML_COMMENT_NODE: |
| 5127 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5128 | if (cur->content != NULL) |
| 5129 | return (xmlStrdup(cur->content)); |
| 5130 | return (NULL); |
| 5131 | case XML_ENTITY_REF_NODE:{ |
| 5132 | xmlEntityPtr ent; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5133 | xmlBufferPtr buffer; |
| 5134 | xmlChar *ret; |
| 5135 | |
| 5136 | /* lookup entity declaration */ |
| 5137 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5138 | if (ent == NULL) |
| 5139 | return (NULL); |
| 5140 | |
| 5141 | buffer = xmlBufferCreate(); |
| 5142 | if (buffer == NULL) |
| 5143 | return (NULL); |
| 5144 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5145 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5146 | |
| 5147 | ret = buffer->content; |
| 5148 | buffer->content = NULL; |
| 5149 | xmlBufferFree(buffer); |
| 5150 | return (ret); |
| 5151 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5152 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5153 | case XML_DOCUMENT_TYPE_NODE: |
| 5154 | case XML_NOTATION_NODE: |
| 5155 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5156 | case XML_XINCLUDE_START: |
| 5157 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5158 | return (NULL); |
| 5159 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5160 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5161 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5162 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5163 | case XML_HTML_DOCUMENT_NODE: { |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5164 | xmlBufferPtr buffer; |
| 5165 | xmlChar *ret; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5166 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5167 | buffer = xmlBufferCreate(); |
| 5168 | if (buffer == NULL) |
| 5169 | return (NULL); |
| 5170 | |
| 5171 | xmlNodeBufGetContent(buffer, (xmlNodePtr) cur); |
| 5172 | |
| 5173 | ret = buffer->content; |
| 5174 | buffer->content = NULL; |
| 5175 | xmlBufferFree(buffer); |
| 5176 | return (ret); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5177 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 5178 | case XML_NAMESPACE_DECL: { |
| 5179 | xmlChar *tmp; |
| 5180 | |
| 5181 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 5182 | return (tmp); |
| 5183 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5184 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5185 | /* TODO !!! */ |
| 5186 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5187 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5188 | /* TODO !!! */ |
| 5189 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5190 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5191 | /* TODO !!! */ |
| 5192 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5193 | case XML_CDATA_SECTION_NODE: |
| 5194 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5195 | if (cur->content != NULL) |
| 5196 | return (xmlStrdup(cur->content)); |
| 5197 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5198 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5199 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5200 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5201 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5202 | /** |
| 5203 | * xmlNodeSetContent: |
| 5204 | * @cur: the node being modified |
| 5205 | * @content: the new value of the content |
| 5206 | * |
| 5207 | * Replace the content of a node. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5208 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 5209 | * references, but XML special chars need to be escaped first by using |
| 5210 | * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5211 | */ |
| 5212 | void |
| 5213 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 5214 | if (cur == NULL) { |
| 5215 | #ifdef DEBUG_TREE |
| 5216 | xmlGenericError(xmlGenericErrorContext, |
| 5217 | "xmlNodeSetContent : node == NULL\n"); |
| 5218 | #endif |
| 5219 | return; |
| 5220 | } |
| 5221 | switch (cur->type) { |
| 5222 | case XML_DOCUMENT_FRAG_NODE: |
| 5223 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5224 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5225 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5226 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 5227 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5228 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5229 | case XML_TEXT_NODE: |
| 5230 | case XML_CDATA_SECTION_NODE: |
| 5231 | case XML_ENTITY_REF_NODE: |
| 5232 | case XML_ENTITY_NODE: |
| 5233 | case XML_PI_NODE: |
| 5234 | case XML_COMMENT_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5235 | if ((cur->content != NULL) && |
| 5236 | (cur->content != (xmlChar *) &(cur->properties))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5237 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
Daniel Veillard | c587bce | 2005-05-10 15:28:08 +0000 | [diff] [blame] | 5238 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5239 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5240 | } |
| 5241 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5242 | cur->last = cur->children = NULL; |
| 5243 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5244 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5245 | } else |
| 5246 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5247 | cur->properties = NULL; |
| 5248 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5249 | break; |
| 5250 | case XML_DOCUMENT_NODE: |
| 5251 | case XML_HTML_DOCUMENT_NODE: |
| 5252 | case XML_DOCUMENT_TYPE_NODE: |
| 5253 | case XML_XINCLUDE_START: |
| 5254 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5255 | #ifdef LIBXML_DOCB_ENABLED |
| 5256 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5257 | #endif |
| 5258 | break; |
| 5259 | case XML_NOTATION_NODE: |
| 5260 | break; |
| 5261 | case XML_DTD_NODE: |
| 5262 | break; |
| 5263 | case XML_NAMESPACE_DECL: |
| 5264 | break; |
| 5265 | case XML_ELEMENT_DECL: |
| 5266 | /* TODO !!! */ |
| 5267 | break; |
| 5268 | case XML_ATTRIBUTE_DECL: |
| 5269 | /* TODO !!! */ |
| 5270 | break; |
| 5271 | case XML_ENTITY_DECL: |
| 5272 | /* TODO !!! */ |
| 5273 | break; |
| 5274 | } |
| 5275 | } |
| 5276 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5277 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5278 | /** |
| 5279 | * xmlNodeSetContentLen: |
| 5280 | * @cur: the node being modified |
| 5281 | * @content: the new value of the content |
| 5282 | * @len: the size of @content |
| 5283 | * |
| 5284 | * Replace the content of a node. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5285 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 5286 | * references, but XML special chars need to be escaped first by using |
| 5287 | * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5288 | */ |
| 5289 | void |
| 5290 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5291 | if (cur == NULL) { |
| 5292 | #ifdef DEBUG_TREE |
| 5293 | xmlGenericError(xmlGenericErrorContext, |
| 5294 | "xmlNodeSetContentLen : node == NULL\n"); |
| 5295 | #endif |
| 5296 | return; |
| 5297 | } |
| 5298 | switch (cur->type) { |
| 5299 | case XML_DOCUMENT_FRAG_NODE: |
| 5300 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5301 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5302 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5303 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 5304 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5305 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5306 | case XML_TEXT_NODE: |
| 5307 | case XML_CDATA_SECTION_NODE: |
| 5308 | case XML_ENTITY_REF_NODE: |
| 5309 | case XML_ENTITY_NODE: |
| 5310 | case XML_PI_NODE: |
| 5311 | case XML_COMMENT_NODE: |
| 5312 | case XML_NOTATION_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5313 | if ((cur->content != NULL) && |
| 5314 | (cur->content != (xmlChar *) &(cur->properties))) { |
| 5315 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5316 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
| 5317 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5318 | } |
| 5319 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5320 | cur->children = cur->last = NULL; |
| 5321 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5322 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5323 | } else |
| 5324 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5325 | cur->properties = NULL; |
| 5326 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5327 | break; |
| 5328 | case XML_DOCUMENT_NODE: |
| 5329 | case XML_DTD_NODE: |
| 5330 | case XML_HTML_DOCUMENT_NODE: |
| 5331 | case XML_DOCUMENT_TYPE_NODE: |
| 5332 | case XML_NAMESPACE_DECL: |
| 5333 | case XML_XINCLUDE_START: |
| 5334 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5335 | #ifdef LIBXML_DOCB_ENABLED |
| 5336 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5337 | #endif |
| 5338 | break; |
| 5339 | case XML_ELEMENT_DECL: |
| 5340 | /* TODO !!! */ |
| 5341 | break; |
| 5342 | case XML_ATTRIBUTE_DECL: |
| 5343 | /* TODO !!! */ |
| 5344 | break; |
| 5345 | case XML_ENTITY_DECL: |
| 5346 | /* TODO !!! */ |
| 5347 | break; |
| 5348 | } |
| 5349 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5350 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5351 | |
| 5352 | /** |
| 5353 | * xmlNodeAddContentLen: |
| 5354 | * @cur: the node being modified |
| 5355 | * @content: extra content |
| 5356 | * @len: the size of @content |
| 5357 | * |
| 5358 | * Append the extra substring to the node content. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5359 | * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be |
| 5360 | * raw text, so unescaped XML special chars are allowed, entity |
| 5361 | * references are not supported. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5362 | */ |
| 5363 | void |
| 5364 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5365 | if (cur == NULL) { |
| 5366 | #ifdef DEBUG_TREE |
| 5367 | xmlGenericError(xmlGenericErrorContext, |
| 5368 | "xmlNodeAddContentLen : node == NULL\n"); |
| 5369 | #endif |
| 5370 | return; |
| 5371 | } |
| 5372 | if (len <= 0) return; |
| 5373 | switch (cur->type) { |
| 5374 | case XML_DOCUMENT_FRAG_NODE: |
| 5375 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5376 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5377 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 5378 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5379 | newNode = xmlNewTextLen(content, len); |
| 5380 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5381 | tmp = xmlAddChild(cur, newNode); |
| 5382 | if (tmp != newNode) |
| 5383 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5384 | if ((last != NULL) && (last->next == newNode)) { |
| 5385 | xmlTextMerge(last, newNode); |
| 5386 | } |
| 5387 | } |
| 5388 | break; |
| 5389 | } |
| 5390 | case XML_ATTRIBUTE_NODE: |
| 5391 | break; |
| 5392 | case XML_TEXT_NODE: |
| 5393 | case XML_CDATA_SECTION_NODE: |
| 5394 | case XML_ENTITY_REF_NODE: |
| 5395 | case XML_ENTITY_NODE: |
| 5396 | case XML_PI_NODE: |
| 5397 | case XML_COMMENT_NODE: |
| 5398 | case XML_NOTATION_NODE: |
| 5399 | if (content != NULL) { |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5400 | if ((cur->content == (xmlChar *) &(cur->properties)) || |
| 5401 | ((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5402 | xmlDictOwns(cur->doc->dict, cur->content))) { |
| 5403 | cur->content = xmlStrncatNew(cur->content, content, len); |
| 5404 | cur->properties = NULL; |
| 5405 | cur->nsDef = NULL; |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5406 | break; |
| 5407 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5408 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5409 | } |
| 5410 | case XML_DOCUMENT_NODE: |
| 5411 | case XML_DTD_NODE: |
| 5412 | case XML_HTML_DOCUMENT_NODE: |
| 5413 | case XML_DOCUMENT_TYPE_NODE: |
| 5414 | case XML_NAMESPACE_DECL: |
| 5415 | case XML_XINCLUDE_START: |
| 5416 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5417 | #ifdef LIBXML_DOCB_ENABLED |
| 5418 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5419 | #endif |
| 5420 | break; |
| 5421 | case XML_ELEMENT_DECL: |
| 5422 | case XML_ATTRIBUTE_DECL: |
| 5423 | case XML_ENTITY_DECL: |
| 5424 | break; |
| 5425 | } |
| 5426 | } |
| 5427 | |
| 5428 | /** |
| 5429 | * xmlNodeAddContent: |
| 5430 | * @cur: the node being modified |
| 5431 | * @content: extra content |
| 5432 | * |
| 5433 | * Append the extra substring to the node content. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5434 | * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be |
| 5435 | * raw text, so unescaped XML special chars are allowed, entity |
| 5436 | * references are not supported. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5437 | */ |
| 5438 | void |
| 5439 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5440 | int len; |
| 5441 | |
| 5442 | if (cur == NULL) { |
| 5443 | #ifdef DEBUG_TREE |
| 5444 | xmlGenericError(xmlGenericErrorContext, |
| 5445 | "xmlNodeAddContent : node == NULL\n"); |
| 5446 | #endif |
| 5447 | return; |
| 5448 | } |
| 5449 | if (content == NULL) return; |
| 5450 | len = xmlStrlen(content); |
| 5451 | xmlNodeAddContentLen(cur, content, len); |
| 5452 | } |
| 5453 | |
| 5454 | /** |
| 5455 | * xmlTextMerge: |
| 5456 | * @first: the first text node |
| 5457 | * @second: the second text node being merged |
| 5458 | * |
| 5459 | * Merge two text nodes into one |
| 5460 | * Returns the first text node augmented |
| 5461 | */ |
| 5462 | xmlNodePtr |
| 5463 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5464 | if (first == NULL) return(second); |
| 5465 | if (second == NULL) return(first); |
| 5466 | if (first->type != XML_TEXT_NODE) return(first); |
| 5467 | if (second->type != XML_TEXT_NODE) return(first); |
| 5468 | if (second->name != first->name) |
| 5469 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5470 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5471 | xmlUnlinkNode(second); |
| 5472 | xmlFreeNode(second); |
| 5473 | return(first); |
| 5474 | } |
| 5475 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 5476 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5477 | /** |
| 5478 | * xmlGetNsList: |
| 5479 | * @doc: the document |
| 5480 | * @node: the current node |
| 5481 | * |
| 5482 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5483 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5484 | * that need to be freed by the caller or NULL if no |
| 5485 | * namespace if defined |
| 5486 | */ |
| 5487 | xmlNsPtr * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5488 | xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) |
| 5489 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5490 | xmlNsPtr cur; |
| 5491 | xmlNsPtr *ret = NULL; |
| 5492 | int nbns = 0; |
| 5493 | int maxns = 10; |
| 5494 | int i; |
| 5495 | |
| 5496 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5497 | if (node->type == XML_ELEMENT_NODE) { |
| 5498 | cur = node->nsDef; |
| 5499 | while (cur != NULL) { |
| 5500 | if (ret == NULL) { |
| 5501 | ret = |
| 5502 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5503 | sizeof(xmlNsPtr)); |
| 5504 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5505 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5506 | return (NULL); |
| 5507 | } |
| 5508 | ret[nbns] = NULL; |
| 5509 | } |
| 5510 | for (i = 0; i < nbns; i++) { |
| 5511 | if ((cur->prefix == ret[i]->prefix) || |
| 5512 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5513 | break; |
| 5514 | } |
| 5515 | if (i >= nbns) { |
| 5516 | if (nbns >= maxns) { |
| 5517 | maxns *= 2; |
| 5518 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5519 | (maxns + |
| 5520 | 1) * |
| 5521 | sizeof(xmlNsPtr)); |
| 5522 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5523 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5524 | return (NULL); |
| 5525 | } |
| 5526 | } |
| 5527 | ret[nbns++] = cur; |
| 5528 | ret[nbns] = NULL; |
| 5529 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5530 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5531 | cur = cur->next; |
| 5532 | } |
| 5533 | } |
| 5534 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5535 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5536 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5537 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5538 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5539 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5540 | /* |
| 5541 | * xmlTreeEnsureXMLDecl: |
| 5542 | * @doc: the doc |
| 5543 | * |
| 5544 | * Ensures that there is an XML namespace declaration on the doc. |
| 5545 | * |
| 5546 | * Returns the XML ns-struct or NULL on API and internal errors. |
| 5547 | */ |
| 5548 | static xmlNsPtr |
| 5549 | xmlTreeEnsureXMLDecl(xmlDocPtr doc) |
| 5550 | { |
| 5551 | if (doc == NULL) |
| 5552 | return (NULL); |
| 5553 | if (doc->oldNs != NULL) |
| 5554 | return (doc->oldNs); |
| 5555 | { |
| 5556 | xmlNsPtr ns; |
| 5557 | ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5558 | if (ns == NULL) { |
| 5559 | xmlTreeErrMemory( |
| 5560 | "allocating the XML namespace"); |
| 5561 | return (NULL); |
| 5562 | } |
| 5563 | memset(ns, 0, sizeof(xmlNs)); |
| 5564 | ns->type = XML_LOCAL_NAMESPACE; |
| 5565 | ns->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5566 | ns->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5567 | doc->oldNs = ns; |
| 5568 | return (ns); |
| 5569 | } |
| 5570 | } |
| 5571 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5572 | /** |
| 5573 | * xmlSearchNs: |
| 5574 | * @doc: the document |
| 5575 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5576 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5577 | * |
| 5578 | * Search a Ns registered under a given name space for a document. |
| 5579 | * recurse on the parents until it finds the defined namespace |
| 5580 | * or return NULL otherwise. |
| 5581 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5582 | * We don't allow to cross entities boundaries. If you don't declare |
| 5583 | * the namespace within those you will be in troubles !!! A warning |
| 5584 | * is generated to cover this case. |
| 5585 | * |
| 5586 | * Returns the namespace pointer or NULL. |
| 5587 | */ |
| 5588 | xmlNsPtr |
| 5589 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5590 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5591 | xmlNsPtr cur; |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5592 | xmlNodePtr orig = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5593 | |
| 5594 | if (node == NULL) return(NULL); |
| 5595 | if ((nameSpace != NULL) && |
| 5596 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5597 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5598 | /* |
| 5599 | * The XML-1.0 namespace is normally held on the root |
| 5600 | * element. In this case exceptionally create it on the |
| 5601 | * node element. |
| 5602 | */ |
| 5603 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5604 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5605 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5606 | return(NULL); |
| 5607 | } |
| 5608 | memset(cur, 0, sizeof(xmlNs)); |
| 5609 | cur->type = XML_LOCAL_NAMESPACE; |
| 5610 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5611 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5612 | cur->next = node->nsDef; |
| 5613 | node->nsDef = cur; |
| 5614 | return(cur); |
| 5615 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5616 | if (doc == NULL) { |
| 5617 | doc = node->doc; |
| 5618 | if (doc == NULL) |
| 5619 | return(NULL); |
| 5620 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5621 | /* |
| 5622 | * Return the XML namespace declaration held by the doc. |
| 5623 | */ |
| 5624 | if (doc->oldNs == NULL) |
| 5625 | return(xmlTreeEnsureXMLDecl(doc)); |
| 5626 | else |
| 5627 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5628 | } |
| 5629 | while (node != NULL) { |
| 5630 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5631 | (node->type == XML_ENTITY_NODE) || |
| 5632 | (node->type == XML_ENTITY_DECL)) |
| 5633 | return(NULL); |
| 5634 | if (node->type == XML_ELEMENT_NODE) { |
| 5635 | cur = node->nsDef; |
| 5636 | while (cur != NULL) { |
| 5637 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5638 | (cur->href != NULL)) |
| 5639 | return(cur); |
| 5640 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5641 | (cur->href != NULL) && |
| 5642 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5643 | return(cur); |
| 5644 | cur = cur->next; |
| 5645 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5646 | if (orig != node) { |
| 5647 | cur = node->ns; |
| 5648 | if (cur != NULL) { |
| 5649 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5650 | (cur->href != NULL)) |
| 5651 | return(cur); |
| 5652 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5653 | (cur->href != NULL) && |
| 5654 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5655 | return(cur); |
| 5656 | } |
| 5657 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5658 | } |
| 5659 | node = node->parent; |
| 5660 | } |
| 5661 | return(NULL); |
| 5662 | } |
| 5663 | |
| 5664 | /** |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5665 | * xmlNsInScope: |
| 5666 | * @doc: the document |
| 5667 | * @node: the current node |
| 5668 | * @ancestor: the ancestor carrying the namespace |
| 5669 | * @prefix: the namespace prefix |
| 5670 | * |
| 5671 | * Verify that the given namespace held on @ancestor is still in scope |
| 5672 | * on node. |
| 5673 | * |
| 5674 | * Returns 1 if true, 0 if false and -1 in case of error. |
| 5675 | */ |
| 5676 | static int |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5677 | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 5678 | xmlNodePtr ancestor, const xmlChar * prefix) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5679 | { |
| 5680 | xmlNsPtr tst; |
| 5681 | |
| 5682 | while ((node != NULL) && (node != ancestor)) { |
| 5683 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5684 | (node->type == XML_ENTITY_NODE) || |
| 5685 | (node->type == XML_ENTITY_DECL)) |
| 5686 | return (-1); |
| 5687 | if (node->type == XML_ELEMENT_NODE) { |
| 5688 | tst = node->nsDef; |
| 5689 | while (tst != NULL) { |
| 5690 | if ((tst->prefix == NULL) |
| 5691 | && (prefix == NULL)) |
| 5692 | return (0); |
| 5693 | if ((tst->prefix != NULL) |
| 5694 | && (prefix != NULL) |
| 5695 | && (xmlStrEqual(tst->prefix, prefix))) |
| 5696 | return (0); |
| 5697 | tst = tst->next; |
| 5698 | } |
| 5699 | } |
| 5700 | node = node->parent; |
| 5701 | } |
| 5702 | if (node != ancestor) |
| 5703 | return (-1); |
| 5704 | return (1); |
| 5705 | } |
| 5706 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5707 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5708 | * xmlSearchNsByHref: |
| 5709 | * @doc: the document |
| 5710 | * @node: the current node |
| 5711 | * @href: the namespace value |
| 5712 | * |
| 5713 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 5714 | * the defined namespace or return NULL otherwise. |
| 5715 | * Returns the namespace pointer or NULL. |
| 5716 | */ |
| 5717 | xmlNsPtr |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5718 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) |
| 5719 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5720 | xmlNsPtr cur; |
| 5721 | xmlNodePtr orig = node; |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5722 | int is_attr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5723 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5724 | if ((node == NULL) || (href == NULL)) |
| 5725 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5726 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5727 | /* |
| 5728 | * Only the document can hold the XML spec namespace. |
| 5729 | */ |
| 5730 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5731 | /* |
| 5732 | * The XML-1.0 namespace is normally held on the root |
| 5733 | * element. In this case exceptionally create it on the |
| 5734 | * node element. |
| 5735 | */ |
| 5736 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5737 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5738 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5739 | return (NULL); |
| 5740 | } |
| 5741 | memset(cur, 0, sizeof(xmlNs)); |
| 5742 | cur->type = XML_LOCAL_NAMESPACE; |
| 5743 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5744 | cur->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5745 | cur->next = node->nsDef; |
| 5746 | node->nsDef = cur; |
| 5747 | return (cur); |
| 5748 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5749 | if (doc == NULL) { |
| 5750 | doc = node->doc; |
| 5751 | if (doc == NULL) |
| 5752 | return(NULL); |
| 5753 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5754 | /* |
| 5755 | * Return the XML namespace declaration held by the doc. |
| 5756 | */ |
| 5757 | if (doc->oldNs == NULL) |
| 5758 | return(xmlTreeEnsureXMLDecl(doc)); |
| 5759 | else |
| 5760 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5761 | } |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5762 | is_attr = (node->type == XML_ATTRIBUTE_NODE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5763 | while (node != NULL) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5764 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5765 | (node->type == XML_ENTITY_NODE) || |
| 5766 | (node->type == XML_ENTITY_DECL)) |
| 5767 | return (NULL); |
| 5768 | if (node->type == XML_ELEMENT_NODE) { |
| 5769 | cur = node->nsDef; |
| 5770 | while (cur != NULL) { |
| 5771 | if ((cur->href != NULL) && (href != NULL) && |
| 5772 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5773 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 5774 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5775 | return (cur); |
| 5776 | } |
| 5777 | cur = cur->next; |
| 5778 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5779 | if (orig != node) { |
| 5780 | cur = node->ns; |
| 5781 | if (cur != NULL) { |
| 5782 | if ((cur->href != NULL) && (href != NULL) && |
| 5783 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5784 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 5785 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5786 | return (cur); |
| 5787 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5788 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5789 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5790 | } |
| 5791 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5792 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5793 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5794 | } |
| 5795 | |
| 5796 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5797 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5798 | * @doc: the document |
| 5799 | * @tree: a node expected to hold the new namespace |
| 5800 | * @ns: the original namespace |
| 5801 | * |
| 5802 | * This function tries to locate a namespace definition in a tree |
| 5803 | * ancestors, or create a new namespace definition node similar to |
| 5804 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 5805 | * null (default namespace) or reused within the subtree defined by |
| 5806 | * @tree or on one of its ancestors then a new prefix is generated. |
| 5807 | * Returns the (new) namespace definition or NULL in case of error |
| 5808 | */ |
| 5809 | xmlNsPtr |
| 5810 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 5811 | xmlNsPtr def; |
| 5812 | xmlChar prefix[50]; |
| 5813 | int counter = 1; |
| 5814 | |
| 5815 | if (tree == NULL) { |
| 5816 | #ifdef DEBUG_TREE |
| 5817 | xmlGenericError(xmlGenericErrorContext, |
| 5818 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 5819 | #endif |
| 5820 | return(NULL); |
| 5821 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5822 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5823 | #ifdef DEBUG_TREE |
| 5824 | xmlGenericError(xmlGenericErrorContext, |
| 5825 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 5826 | #endif |
| 5827 | return(NULL); |
| 5828 | } |
| 5829 | /* |
| 5830 | * Search an existing namespace definition inherited. |
| 5831 | */ |
| 5832 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 5833 | if (def != NULL) |
| 5834 | return(def); |
| 5835 | |
| 5836 | /* |
| 5837 | * Find a close prefix which is not already in use. |
| 5838 | * Let's strip namespace prefixes longer than 20 chars ! |
| 5839 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5840 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5841 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5842 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 5843 | snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5844 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5845 | def = xmlSearchNs(doc, tree, prefix); |
| 5846 | while (def != NULL) { |
| 5847 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5848 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5849 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5850 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 5851 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", |
| 5852 | (char *)ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5853 | def = xmlSearchNs(doc, tree, prefix); |
| 5854 | } |
| 5855 | |
| 5856 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5857 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5858 | */ |
| 5859 | def = xmlNewNs(tree, ns->href, prefix); |
| 5860 | return(def); |
| 5861 | } |
| 5862 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5863 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5864 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5865 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5866 | * @doc: the document |
| 5867 | * @tree: a node defining the subtree to reconciliate |
| 5868 | * |
| 5869 | * This function checks that all the namespaces declared within the given |
| 5870 | * tree are properly declared. This is needed for example after Copy or Cut |
| 5871 | * and then paste operations. The subtree may still hold pointers to |
| 5872 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5873 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5874 | * the new environment. If not possible the new namespaces are redeclared |
| 5875 | * on @tree at the top of the given subtree. |
| 5876 | * Returns the number of namespace declarations created or -1 in case of error. |
| 5877 | */ |
| 5878 | int |
| 5879 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 5880 | xmlNsPtr *oldNs = NULL; |
| 5881 | xmlNsPtr *newNs = NULL; |
| 5882 | int sizeCache = 0; |
| 5883 | int nbCache = 0; |
| 5884 | |
| 5885 | xmlNsPtr n; |
| 5886 | xmlNodePtr node = tree; |
| 5887 | xmlAttrPtr attr; |
| 5888 | int ret = 0, i; |
| 5889 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5890 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); |
| 5891 | if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1); |
| 5892 | if (node->doc != doc) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5893 | while (node != NULL) { |
| 5894 | /* |
| 5895 | * Reconciliate the node namespace |
| 5896 | */ |
| 5897 | if (node->ns != NULL) { |
| 5898 | /* |
| 5899 | * initialize the cache if needed |
| 5900 | */ |
| 5901 | if (sizeCache == 0) { |
| 5902 | sizeCache = 10; |
| 5903 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5904 | sizeof(xmlNsPtr)); |
| 5905 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5906 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5907 | return(-1); |
| 5908 | } |
| 5909 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5910 | sizeof(xmlNsPtr)); |
| 5911 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5912 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5913 | xmlFree(oldNs); |
| 5914 | return(-1); |
| 5915 | } |
| 5916 | } |
| 5917 | for (i = 0;i < nbCache;i++) { |
| 5918 | if (oldNs[i] == node->ns) { |
| 5919 | node->ns = newNs[i]; |
| 5920 | break; |
| 5921 | } |
| 5922 | } |
| 5923 | if (i == nbCache) { |
| 5924 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5925 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5926 | */ |
| 5927 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 5928 | if (n != NULL) { /* :-( what if else ??? */ |
| 5929 | /* |
| 5930 | * check if we need to grow the cache buffers. |
| 5931 | */ |
| 5932 | if (sizeCache <= nbCache) { |
| 5933 | sizeCache *= 2; |
| 5934 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5935 | sizeof(xmlNsPtr)); |
| 5936 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5937 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5938 | xmlFree(newNs); |
| 5939 | return(-1); |
| 5940 | } |
| 5941 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5942 | sizeof(xmlNsPtr)); |
| 5943 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5944 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5945 | xmlFree(oldNs); |
| 5946 | return(-1); |
| 5947 | } |
| 5948 | } |
| 5949 | newNs[nbCache] = n; |
| 5950 | oldNs[nbCache++] = node->ns; |
| 5951 | node->ns = n; |
| 5952 | } |
| 5953 | } |
| 5954 | } |
| 5955 | /* |
| 5956 | * now check for namespace hold by attributes on the node. |
| 5957 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 5958 | if (node->type == XML_ELEMENT_NODE) { |
| 5959 | attr = node->properties; |
| 5960 | while (attr != NULL) { |
| 5961 | if (attr->ns != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5962 | /* |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 5963 | * initialize the cache if needed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5964 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 5965 | if (sizeCache == 0) { |
| 5966 | sizeCache = 10; |
| 5967 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5968 | sizeof(xmlNsPtr)); |
| 5969 | if (oldNs == NULL) { |
| 5970 | xmlTreeErrMemory("fixing namespaces"); |
| 5971 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5972 | } |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 5973 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5974 | sizeof(xmlNsPtr)); |
| 5975 | if (newNs == NULL) { |
| 5976 | xmlTreeErrMemory("fixing namespaces"); |
| 5977 | xmlFree(oldNs); |
| 5978 | return(-1); |
| 5979 | } |
| 5980 | } |
| 5981 | for (i = 0;i < nbCache;i++) { |
| 5982 | if (oldNs[i] == attr->ns) { |
| 5983 | attr->ns = newNs[i]; |
| 5984 | break; |
| 5985 | } |
| 5986 | } |
| 5987 | if (i == nbCache) { |
| 5988 | /* |
| 5989 | * OK we need to recreate a new namespace definition |
| 5990 | */ |
| 5991 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 5992 | if (n != NULL) { /* :-( what if else ??? */ |
| 5993 | /* |
| 5994 | * check if we need to grow the cache buffers. |
| 5995 | */ |
| 5996 | if (sizeCache <= nbCache) { |
| 5997 | sizeCache *= 2; |
| 5998 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, |
| 5999 | sizeCache * sizeof(xmlNsPtr)); |
| 6000 | if (oldNs == NULL) { |
| 6001 | xmlTreeErrMemory("fixing namespaces"); |
| 6002 | xmlFree(newNs); |
| 6003 | return(-1); |
| 6004 | } |
| 6005 | newNs = (xmlNsPtr *) xmlRealloc(newNs, |
| 6006 | sizeCache * sizeof(xmlNsPtr)); |
| 6007 | if (newNs == NULL) { |
| 6008 | xmlTreeErrMemory("fixing namespaces"); |
| 6009 | xmlFree(oldNs); |
| 6010 | return(-1); |
| 6011 | } |
| 6012 | } |
| 6013 | newNs[nbCache] = n; |
| 6014 | oldNs[nbCache++] = attr->ns; |
| 6015 | attr->ns = n; |
| 6016 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6017 | } |
| 6018 | } |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6019 | attr = attr->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6020 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
| 6023 | /* |
| 6024 | * Browse the full subtree, deep first |
| 6025 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6026 | if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6027 | /* deep first */ |
| 6028 | node = node->children; |
| 6029 | } else if ((node != tree) && (node->next != NULL)) { |
| 6030 | /* then siblings */ |
| 6031 | node = node->next; |
| 6032 | } else if (node != tree) { |
| 6033 | /* go up to parents->next if needed */ |
| 6034 | while (node != tree) { |
| 6035 | if (node->parent != NULL) |
| 6036 | node = node->parent; |
| 6037 | if ((node != tree) && (node->next != NULL)) { |
| 6038 | node = node->next; |
| 6039 | break; |
| 6040 | } |
| 6041 | if (node->parent == NULL) { |
| 6042 | node = NULL; |
| 6043 | break; |
| 6044 | } |
| 6045 | } |
| 6046 | /* exit condition */ |
| 6047 | if (node == tree) |
| 6048 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 6049 | } else |
| 6050 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6051 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6052 | if (oldNs != NULL) |
| 6053 | xmlFree(oldNs); |
| 6054 | if (newNs != NULL) |
| 6055 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6056 | return(ret); |
| 6057 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6058 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6059 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6060 | static xmlAttrPtr |
| 6061 | xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name, |
| 6062 | const xmlChar *nsName, int useDTD) |
| 6063 | { |
| 6064 | xmlAttrPtr prop; |
| 6065 | |
| 6066 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6067 | return(NULL); |
| 6068 | |
| 6069 | if (node->properties != NULL) { |
| 6070 | prop = node->properties; |
| 6071 | if (nsName == NULL) { |
| 6072 | /* |
| 6073 | * We want the attr to be in no namespace. |
| 6074 | */ |
| 6075 | do { |
| 6076 | if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) { |
| 6077 | return(prop); |
| 6078 | } |
| 6079 | prop = prop->next; |
| 6080 | } while (prop != NULL); |
| 6081 | } else { |
| 6082 | /* |
| 6083 | * We want the attr to be in the specified namespace. |
| 6084 | */ |
| 6085 | do { |
| 6086 | if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) && |
| 6087 | ((prop->ns->href == nsName) || |
| 6088 | xmlStrEqual(prop->ns->href, nsName))) |
| 6089 | { |
| 6090 | return(prop); |
| 6091 | } |
| 6092 | prop = prop->next; |
| 6093 | } while (prop != NULL); |
| 6094 | } |
| 6095 | } |
| 6096 | |
| 6097 | #ifdef LIBXML_TREE_ENABLED |
| 6098 | if (! useDTD) |
| 6099 | return(NULL); |
| 6100 | /* |
| 6101 | * Check if there is a default/fixed attribute declaration in |
| 6102 | * the internal or external subset. |
| 6103 | */ |
| 6104 | if ((node->doc != NULL) && (node->doc->intSubset != NULL)) { |
| 6105 | xmlDocPtr doc = node->doc; |
| 6106 | xmlAttributePtr attrDecl = NULL; |
| 6107 | xmlChar *elemQName, *tmpstr = NULL; |
| 6108 | |
| 6109 | /* |
| 6110 | * We need the QName of the element for the DTD-lookup. |
| 6111 | */ |
| 6112 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 6113 | tmpstr = xmlStrdup(node->ns->prefix); |
| 6114 | tmpstr = xmlStrcat(tmpstr, BAD_CAST ":"); |
| 6115 | tmpstr = xmlStrcat(tmpstr, node->name); |
| 6116 | if (tmpstr == NULL) |
| 6117 | return(NULL); |
| 6118 | elemQName = tmpstr; |
| 6119 | } else |
| 6120 | elemQName = (xmlChar *) node->name; |
| 6121 | if (nsName == NULL) { |
| 6122 | /* |
| 6123 | * The common and nice case: Attr in no namespace. |
| 6124 | */ |
| 6125 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, |
| 6126 | elemQName, name, NULL); |
| 6127 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 6128 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, |
| 6129 | elemQName, name, NULL); |
| 6130 | } |
| 6131 | } else { |
| 6132 | xmlNsPtr *nsList, *cur; |
| 6133 | |
| 6134 | /* |
| 6135 | * The ugly case: Search using the prefixes of in-scope |
| 6136 | * ns-decls corresponding to @nsName. |
| 6137 | */ |
| 6138 | nsList = xmlGetNsList(node->doc, node); |
| 6139 | if (nsList == NULL) { |
| 6140 | if (tmpstr != NULL) |
| 6141 | xmlFree(tmpstr); |
| 6142 | return(NULL); |
| 6143 | } |
| 6144 | cur = nsList; |
| 6145 | while (*cur != NULL) { |
| 6146 | if (xmlStrEqual((*cur)->href, nsName)) { |
| 6147 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName, |
| 6148 | name, (*cur)->prefix); |
| 6149 | if (attrDecl) |
| 6150 | break; |
| 6151 | if (doc->extSubset != NULL) { |
| 6152 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName, |
| 6153 | name, (*cur)->prefix); |
| 6154 | if (attrDecl) |
| 6155 | break; |
| 6156 | } |
| 6157 | } |
| 6158 | cur++; |
| 6159 | } |
| 6160 | xmlFree(nsList); |
| 6161 | } |
| 6162 | if (tmpstr != NULL) |
| 6163 | xmlFree(tmpstr); |
| 6164 | /* |
| 6165 | * Only default/fixed attrs are relevant. |
| 6166 | */ |
| 6167 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6168 | return((xmlAttrPtr) attrDecl); |
| 6169 | } |
| 6170 | #endif /* LIBXML_TREE_ENABLED */ |
| 6171 | return(NULL); |
| 6172 | } |
| 6173 | |
| 6174 | static xmlChar* |
| 6175 | xmlGetPropNodeValueInternal(xmlAttrPtr prop) |
| 6176 | { |
| 6177 | if (prop == NULL) |
| 6178 | return(NULL); |
| 6179 | if (prop->type == XML_ATTRIBUTE_NODE) { |
| 6180 | /* |
| 6181 | * Note that we return at least the empty string. |
| 6182 | * TODO: Do we really always want that? |
| 6183 | */ |
| 6184 | if (prop->children != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 6185 | if ((prop->children->next == NULL) && |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6186 | ((prop->children->type == XML_TEXT_NODE) || |
| 6187 | (prop->children->type == XML_CDATA_SECTION_NODE))) |
| 6188 | { |
| 6189 | /* |
| 6190 | * Optimization for the common case: only 1 text node. |
| 6191 | */ |
| 6192 | return(xmlStrdup(prop->children->content)); |
| 6193 | } else { |
| 6194 | xmlChar *ret; |
| 6195 | |
| 6196 | ret = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 6197 | if (ret != NULL) |
| 6198 | return(ret); |
| 6199 | } |
| 6200 | } |
| 6201 | return(xmlStrdup((xmlChar *)"")); |
| 6202 | } else if (prop->type == XML_ATTRIBUTE_DECL) { |
| 6203 | return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue)); |
| 6204 | } |
| 6205 | return(NULL); |
| 6206 | } |
| 6207 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6208 | /** |
| 6209 | * xmlHasProp: |
| 6210 | * @node: the node |
| 6211 | * @name: the attribute name |
| 6212 | * |
| 6213 | * Search an attribute associated to a node |
| 6214 | * This function also looks in DTD attribute declaration for #FIXED or |
| 6215 | * default declaration values unless DTD use has been turned off. |
| 6216 | * |
| 6217 | * Returns the attribute or the attribute declaration or NULL if |
| 6218 | * neither was found. |
| 6219 | */ |
| 6220 | xmlAttrPtr |
| 6221 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 6222 | xmlAttrPtr prop; |
| 6223 | xmlDocPtr doc; |
| 6224 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6225 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6226 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6227 | /* |
| 6228 | * Check on the properties attached to the node |
| 6229 | */ |
| 6230 | prop = node->properties; |
| 6231 | while (prop != NULL) { |
| 6232 | if (xmlStrEqual(prop->name, name)) { |
| 6233 | return(prop); |
| 6234 | } |
| 6235 | prop = prop->next; |
| 6236 | } |
| 6237 | if (!xmlCheckDTD) return(NULL); |
| 6238 | |
| 6239 | /* |
| 6240 | * Check if there is a default declaration in the internal |
| 6241 | * or external subsets |
| 6242 | */ |
| 6243 | doc = node->doc; |
| 6244 | if (doc != NULL) { |
| 6245 | xmlAttributePtr attrDecl; |
| 6246 | if (doc->intSubset != NULL) { |
| 6247 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6248 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6249 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6250 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6251 | /* return attribute declaration only if a default value is given |
| 6252 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6253 | return((xmlAttrPtr) attrDecl); |
| 6254 | } |
| 6255 | } |
| 6256 | return(NULL); |
| 6257 | } |
| 6258 | |
| 6259 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6260 | * xmlHasNsProp: |
| 6261 | * @node: the node |
| 6262 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6263 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6264 | * |
| 6265 | * Search for an attribute associated to a node |
| 6266 | * This attribute has to be anchored in the namespace specified. |
| 6267 | * This does the entity substitution. |
| 6268 | * This function looks in DTD attribute declaration for #FIXED or |
| 6269 | * default declaration values unless DTD use has been turned off. |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6270 | * Note that a namespace of NULL indicates to use the default namespace. |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6271 | * |
| 6272 | * Returns the attribute or the attribute declaration or NULL |
| 6273 | * if neither was found. |
| 6274 | */ |
| 6275 | xmlAttrPtr |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6276 | xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6277 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6278 | return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD)); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6279 | } |
| 6280 | |
| 6281 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6282 | * xmlGetProp: |
| 6283 | * @node: the node |
| 6284 | * @name: the attribute name |
| 6285 | * |
| 6286 | * Search and get the value of an attribute associated to a node |
| 6287 | * This does the entity substitution. |
| 6288 | * This function looks in DTD attribute declaration for #FIXED or |
| 6289 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 6290 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6291 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 6292 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6293 | * |
| 6294 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6295 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6296 | */ |
| 6297 | xmlChar * |
| 6298 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6299 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6300 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6301 | prop = xmlHasProp(node, name); |
| 6302 | if (prop == NULL) |
| 6303 | return(NULL); |
| 6304 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6305 | } |
| 6306 | |
| 6307 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6308 | * xmlGetNoNsProp: |
| 6309 | * @node: the node |
| 6310 | * @name: the attribute name |
| 6311 | * |
| 6312 | * Search and get the value of an attribute associated to a node |
| 6313 | * This does the entity substitution. |
| 6314 | * This function looks in DTD attribute declaration for #FIXED or |
| 6315 | * default declaration values unless DTD use has been turned off. |
| 6316 | * This function is similar to xmlGetProp except it will accept only |
| 6317 | * an attribute in no namespace. |
| 6318 | * |
| 6319 | * Returns the attribute value or NULL if not found. |
| 6320 | * It's up to the caller to free the memory with xmlFree(). |
| 6321 | */ |
| 6322 | xmlChar * |
| 6323 | xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) { |
| 6324 | xmlAttrPtr prop; |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6325 | |
| 6326 | prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD); |
| 6327 | if (prop == NULL) |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6328 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6329 | return(xmlGetPropNodeValueInternal(prop)); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6330 | } |
| 6331 | |
| 6332 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6333 | * xmlGetNsProp: |
| 6334 | * @node: the node |
| 6335 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6336 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6337 | * |
| 6338 | * Search and get the value of an attribute associated to a node |
| 6339 | * This attribute has to be anchored in the namespace specified. |
| 6340 | * This does the entity substitution. |
| 6341 | * This function looks in DTD attribute declaration for #FIXED or |
| 6342 | * default declaration values unless DTD use has been turned off. |
| 6343 | * |
| 6344 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6345 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6346 | */ |
| 6347 | xmlChar * |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6348 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6349 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6350 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6351 | prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD); |
| 6352 | if (prop == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6353 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6354 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6357 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
| 6358 | /** |
| 6359 | * xmlUnsetProp: |
| 6360 | * @node: the node |
| 6361 | * @name: the attribute name |
| 6362 | * |
| 6363 | * Remove an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6364 | * This handles only attributes in no namespace. |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6365 | * Returns 0 if successful, -1 if not found |
| 6366 | */ |
| 6367 | int |
| 6368 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6369 | xmlAttrPtr prop; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6370 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6371 | prop = xmlGetPropNodeInternal(node, name, 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 | |
| 6379 | /** |
| 6380 | * xmlUnsetNsProp: |
| 6381 | * @node: the node |
| 6382 | * @ns: the namespace definition |
| 6383 | * @name: the attribute name |
| 6384 | * |
| 6385 | * Remove an attribute carried by a node. |
| 6386 | * Returns 0 if successful, -1 if not found |
| 6387 | */ |
| 6388 | int |
| 6389 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6390 | xmlAttrPtr prop; |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6391 | |
| 6392 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6393 | if (prop == NULL) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6394 | return(-1); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6395 | xmlUnlinkNode((xmlNodePtr) prop); |
| 6396 | xmlFreeProp(prop); |
| 6397 | return(0); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6398 | } |
| 6399 | #endif |
| 6400 | |
| 6401 | #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] | 6402 | /** |
| 6403 | * xmlSetProp: |
| 6404 | * @node: the node |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6405 | * @name: the attribute name (a QName) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6406 | * @value: the attribute value |
| 6407 | * |
| 6408 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6409 | * If @name has a prefix, then the corresponding |
| 6410 | * namespace-binding will be used, if in scope; it is an |
| 6411 | * error it there's no such ns-binding for the prefix in |
| 6412 | * scope. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6413 | * Returns the attribute pointer. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6414 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6415 | */ |
| 6416 | xmlAttrPtr |
| 6417 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6418 | int len; |
| 6419 | const xmlChar *nqname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6420 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6421 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6422 | return(NULL); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6423 | |
| 6424 | /* |
| 6425 | * handle QNames |
| 6426 | */ |
| 6427 | nqname = xmlSplitQName3(name, &len); |
| 6428 | if (nqname != NULL) { |
| 6429 | xmlNsPtr ns; |
| 6430 | xmlChar *prefix = xmlStrndup(name, len); |
| 6431 | ns = xmlSearchNs(node->doc, node, prefix); |
| 6432 | if (prefix != NULL) |
| 6433 | xmlFree(prefix); |
| 6434 | if (ns != NULL) |
| 6435 | return(xmlSetNsProp(node, ns, nqname, value)); |
| 6436 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6437 | return(xmlSetNsProp(node, NULL, name, value)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
| 6440 | /** |
| 6441 | * xmlSetNsProp: |
| 6442 | * @node: the node |
| 6443 | * @ns: the namespace definition |
| 6444 | * @name: the attribute name |
| 6445 | * @value: the attribute value |
| 6446 | * |
| 6447 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6448 | * The ns structure must be in scope, this is not checked |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6449 | * |
| 6450 | * Returns the attribute pointer. |
| 6451 | */ |
| 6452 | xmlAttrPtr |
| 6453 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6454 | const xmlChar *value) |
| 6455 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6456 | xmlAttrPtr prop; |
| 6457 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6458 | if (ns && (ns->href == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6459 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6460 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6461 | if (prop != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6462 | /* |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6463 | * Modify the attribute's value. |
| 6464 | */ |
| 6465 | if (prop->atype == XML_ATTRIBUTE_ID) { |
| 6466 | xmlRemoveID(node->doc, prop); |
| 6467 | prop->atype = XML_ATTRIBUTE_ID; |
| 6468 | } |
| 6469 | if (prop->children != NULL) |
| 6470 | xmlFreeNodeList(prop->children); |
| 6471 | prop->children = NULL; |
| 6472 | prop->last = NULL; |
| 6473 | prop->ns = ns; |
| 6474 | if (value != NULL) { |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6475 | xmlNodePtr tmp; |
| 6476 | |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame^] | 6477 | if(!xmlCheckUTF8(value)) { |
| 6478 | xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc, |
| 6479 | NULL); |
| 6480 | if (node->doc != NULL) |
| 6481 | node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 6482 | } |
| 6483 | prop->children = xmlNewDocText(node->doc, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6484 | prop->last = NULL; |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6485 | tmp = prop->children; |
| 6486 | while (tmp != NULL) { |
| 6487 | tmp->parent = (xmlNodePtr) prop; |
| 6488 | if (tmp->next == NULL) |
| 6489 | prop->last = tmp; |
| 6490 | tmp = tmp->next; |
| 6491 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6492 | } |
| 6493 | if (prop->atype == XML_ATTRIBUTE_ID) |
| 6494 | xmlAddID(NULL, node->doc, value, prop); |
| 6495 | return(prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6496 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6497 | /* |
| 6498 | * No equal attr found; create a new one. |
| 6499 | */ |
| 6500 | return(xmlNewPropInternal(node, ns, name, value, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6501 | } |
| 6502 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6503 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6504 | |
| 6505 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6506 | * xmlNodeIsText: |
| 6507 | * @node: the node |
| 6508 | * |
| 6509 | * Is this node a Text node ? |
| 6510 | * Returns 1 yes, 0 no |
| 6511 | */ |
| 6512 | int |
| 6513 | xmlNodeIsText(xmlNodePtr node) { |
| 6514 | if (node == NULL) return(0); |
| 6515 | |
| 6516 | if (node->type == XML_TEXT_NODE) return(1); |
| 6517 | return(0); |
| 6518 | } |
| 6519 | |
| 6520 | /** |
| 6521 | * xmlIsBlankNode: |
| 6522 | * @node: the node |
| 6523 | * |
| 6524 | * Checks whether this node is an empty or whitespace only |
| 6525 | * (and possibly ignorable) text-node. |
| 6526 | * |
| 6527 | * Returns 1 yes, 0 no |
| 6528 | */ |
| 6529 | int |
| 6530 | xmlIsBlankNode(xmlNodePtr node) { |
| 6531 | const xmlChar *cur; |
| 6532 | if (node == NULL) return(0); |
| 6533 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6534 | if ((node->type != XML_TEXT_NODE) && |
| 6535 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6536 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6537 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6538 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6539 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6540 | if (!IS_BLANK_CH(*cur)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6541 | cur++; |
| 6542 | } |
| 6543 | |
| 6544 | return(1); |
| 6545 | } |
| 6546 | |
| 6547 | /** |
| 6548 | * xmlTextConcat: |
| 6549 | * @node: the node |
| 6550 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6551 | * @len: @content length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6552 | * |
| 6553 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6554 | * |
| 6555 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6556 | */ |
| 6557 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6558 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6559 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6560 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6561 | |
| 6562 | if ((node->type != XML_TEXT_NODE) && |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 6563 | (node->type != XML_CDATA_SECTION_NODE) && |
| 6564 | (node->type != XML_COMMENT_NODE) && |
| 6565 | (node->type != XML_PI_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6566 | #ifdef DEBUG_TREE |
| 6567 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6568 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6569 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6570 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6571 | } |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6572 | /* need to check if content is currently in the dictionary */ |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6573 | if ((node->content == (xmlChar *) &(node->properties)) || |
| 6574 | ((node->doc != NULL) && (node->doc->dict != NULL) && |
| 6575 | xmlDictOwns(node->doc->dict, node->content))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6576 | node->content = xmlStrncatNew(node->content, content, len); |
| 6577 | } else { |
| 6578 | node->content = xmlStrncat(node->content, content, len); |
| 6579 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6580 | node->properties = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6581 | if (node->content == NULL) |
| 6582 | return(-1); |
| 6583 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
| 6586 | /************************************************************************ |
| 6587 | * * |
| 6588 | * Output : to a FILE or in memory * |
| 6589 | * * |
| 6590 | ************************************************************************/ |
| 6591 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6592 | /** |
| 6593 | * xmlBufferCreate: |
| 6594 | * |
| 6595 | * routine to create an XML buffer. |
| 6596 | * returns the new structure. |
| 6597 | */ |
| 6598 | xmlBufferPtr |
| 6599 | xmlBufferCreate(void) { |
| 6600 | xmlBufferPtr ret; |
| 6601 | |
| 6602 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6603 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6604 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6605 | return(NULL); |
| 6606 | } |
| 6607 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 6608 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6609 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6610 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6611 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6612 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6613 | xmlFree(ret); |
| 6614 | return(NULL); |
| 6615 | } |
| 6616 | ret->content[0] = 0; |
| 6617 | return(ret); |
| 6618 | } |
| 6619 | |
| 6620 | /** |
| 6621 | * xmlBufferCreateSize: |
| 6622 | * @size: initial size of buffer |
| 6623 | * |
| 6624 | * routine to create an XML buffer. |
| 6625 | * returns the new structure. |
| 6626 | */ |
| 6627 | xmlBufferPtr |
| 6628 | xmlBufferCreateSize(size_t size) { |
| 6629 | xmlBufferPtr ret; |
| 6630 | |
| 6631 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6632 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6633 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6634 | return(NULL); |
| 6635 | } |
| 6636 | ret->use = 0; |
| 6637 | ret->alloc = xmlBufferAllocScheme; |
| 6638 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 6639 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6640 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6641 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6642 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6643 | xmlFree(ret); |
| 6644 | return(NULL); |
| 6645 | } |
| 6646 | ret->content[0] = 0; |
| 6647 | } else |
| 6648 | ret->content = NULL; |
| 6649 | return(ret); |
| 6650 | } |
| 6651 | |
| 6652 | /** |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6653 | * xmlBufferCreateStatic: |
| 6654 | * @mem: the memory area |
| 6655 | * @size: the size in byte |
| 6656 | * |
MST 2003 John Fleck | a0e7e93 | 2003-12-19 03:13:47 +0000 | [diff] [blame] | 6657 | * routine to create an XML buffer from an immutable memory area. |
| 6658 | * 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] | 6659 | * present until the end of the buffer lifetime. |
| 6660 | * |
| 6661 | * returns the new structure. |
| 6662 | */ |
| 6663 | xmlBufferPtr |
| 6664 | xmlBufferCreateStatic(void *mem, size_t size) { |
| 6665 | xmlBufferPtr ret; |
| 6666 | |
| 6667 | if ((mem == NULL) || (size == 0)) |
| 6668 | return(NULL); |
| 6669 | |
| 6670 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6671 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6672 | xmlTreeErrMemory("creating buffer"); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6673 | return(NULL); |
| 6674 | } |
| 6675 | ret->use = size; |
| 6676 | ret->size = size; |
| 6677 | ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; |
| 6678 | ret->content = (xmlChar *) mem; |
| 6679 | return(ret); |
| 6680 | } |
| 6681 | |
| 6682 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6683 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6684 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6685 | * @scheme: allocation scheme to use |
| 6686 | * |
| 6687 | * Sets the allocation scheme for this buffer |
| 6688 | */ |
| 6689 | void |
| 6690 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 6691 | xmlBufferAllocationScheme scheme) { |
| 6692 | if (buf == NULL) { |
| 6693 | #ifdef DEBUG_BUFFER |
| 6694 | xmlGenericError(xmlGenericErrorContext, |
| 6695 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 6696 | #endif |
| 6697 | return; |
| 6698 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6699 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6700 | |
| 6701 | buf->alloc = scheme; |
| 6702 | } |
| 6703 | |
| 6704 | /** |
| 6705 | * xmlBufferFree: |
| 6706 | * @buf: the buffer to free |
| 6707 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 6708 | * Frees an XML buffer. It frees both the content and the structure which |
| 6709 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6710 | */ |
| 6711 | void |
| 6712 | xmlBufferFree(xmlBufferPtr buf) { |
| 6713 | if (buf == NULL) { |
| 6714 | #ifdef DEBUG_BUFFER |
| 6715 | xmlGenericError(xmlGenericErrorContext, |
| 6716 | "xmlBufferFree: buf == NULL\n"); |
| 6717 | #endif |
| 6718 | return; |
| 6719 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6720 | |
| 6721 | if ((buf->content != NULL) && |
| 6722 | (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6723 | xmlFree(buf->content); |
| 6724 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6725 | xmlFree(buf); |
| 6726 | } |
| 6727 | |
| 6728 | /** |
| 6729 | * xmlBufferEmpty: |
| 6730 | * @buf: the buffer |
| 6731 | * |
| 6732 | * empty a buffer. |
| 6733 | */ |
| 6734 | void |
| 6735 | xmlBufferEmpty(xmlBufferPtr buf) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6736 | if (buf == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6737 | if (buf->content == NULL) return; |
| 6738 | buf->use = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6739 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 6740 | buf->content = BAD_CAST ""; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6741 | } else { |
| 6742 | memset(buf->content, 0, buf->size); |
| 6743 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6744 | } |
| 6745 | |
| 6746 | /** |
| 6747 | * xmlBufferShrink: |
| 6748 | * @buf: the buffer to dump |
| 6749 | * @len: the number of xmlChar to remove |
| 6750 | * |
| 6751 | * Remove the beginning of an XML buffer. |
| 6752 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6753 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6754 | */ |
| 6755 | int |
| 6756 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 6757 | if (buf == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6758 | if (len == 0) return(0); |
| 6759 | if (len > buf->use) return(-1); |
| 6760 | |
| 6761 | buf->use -= len; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6762 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
| 6763 | buf->content += len; |
| 6764 | } else { |
| 6765 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
| 6766 | buf->content[buf->use] = 0; |
| 6767 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6768 | return(len); |
| 6769 | } |
| 6770 | |
| 6771 | /** |
| 6772 | * xmlBufferGrow: |
| 6773 | * @buf: the buffer |
| 6774 | * @len: the minimum free size to allocate |
| 6775 | * |
| 6776 | * Grow the available space of an XML buffer. |
| 6777 | * |
| 6778 | * Returns the new available space or -1 in case of error |
| 6779 | */ |
| 6780 | int |
| 6781 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 6782 | int size; |
| 6783 | xmlChar *newbuf; |
| 6784 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 6785 | if (buf == NULL) return(-1); |
| 6786 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6787 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6788 | if (len + buf->use < buf->size) return(0); |
| 6789 | |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 6790 | /* |
| 6791 | * Windows has a BIG problem on realloc timing, so we try to double |
| 6792 | * the buffer size (if that's enough) (bug 146697) |
| 6793 | */ |
| 6794 | #ifdef WIN32 |
| 6795 | if (buf->size > len) |
| 6796 | size = buf->size * 2; |
| 6797 | else |
| 6798 | size = buf->use + len + 100; |
| 6799 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6800 | size = buf->use + len + 100; |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 6801 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6802 | |
| 6803 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6804 | if (newbuf == NULL) { |
| 6805 | xmlTreeErrMemory("growing buffer"); |
| 6806 | return(-1); |
| 6807 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6808 | buf->content = newbuf; |
| 6809 | buf->size = size; |
| 6810 | return(buf->size - buf->use); |
| 6811 | } |
| 6812 | |
| 6813 | /** |
| 6814 | * xmlBufferDump: |
| 6815 | * @file: the file output |
| 6816 | * @buf: the buffer to dump |
| 6817 | * |
| 6818 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6819 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6820 | */ |
| 6821 | int |
| 6822 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 6823 | int ret; |
| 6824 | |
| 6825 | if (buf == NULL) { |
| 6826 | #ifdef DEBUG_BUFFER |
| 6827 | xmlGenericError(xmlGenericErrorContext, |
| 6828 | "xmlBufferDump: buf == NULL\n"); |
| 6829 | #endif |
| 6830 | return(0); |
| 6831 | } |
| 6832 | if (buf->content == NULL) { |
| 6833 | #ifdef DEBUG_BUFFER |
| 6834 | xmlGenericError(xmlGenericErrorContext, |
| 6835 | "xmlBufferDump: buf->content == NULL\n"); |
| 6836 | #endif |
| 6837 | return(0); |
| 6838 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 6839 | if (file == NULL) |
| 6840 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6841 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 6842 | return(ret); |
| 6843 | } |
| 6844 | |
| 6845 | /** |
| 6846 | * xmlBufferContent: |
| 6847 | * @buf: the buffer |
| 6848 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6849 | * Function to extract the content of a buffer |
| 6850 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6851 | * Returns the internal content |
| 6852 | */ |
| 6853 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6854 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6855 | xmlBufferContent(const xmlBufferPtr buf) |
| 6856 | { |
| 6857 | if(!buf) |
| 6858 | return NULL; |
| 6859 | |
| 6860 | return buf->content; |
| 6861 | } |
| 6862 | |
| 6863 | /** |
| 6864 | * xmlBufferLength: |
| 6865 | * @buf: the buffer |
| 6866 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6867 | * Function to get the length of a buffer |
| 6868 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6869 | * Returns the length of data in the internal content |
| 6870 | */ |
| 6871 | |
| 6872 | int |
| 6873 | xmlBufferLength(const xmlBufferPtr buf) |
| 6874 | { |
| 6875 | if(!buf) |
| 6876 | return 0; |
| 6877 | |
| 6878 | return buf->use; |
| 6879 | } |
| 6880 | |
| 6881 | /** |
| 6882 | * xmlBufferResize: |
| 6883 | * @buf: the buffer to resize |
| 6884 | * @size: the desired size |
| 6885 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6886 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6887 | * |
| 6888 | * Returns 0 in case of problems, 1 otherwise |
| 6889 | */ |
| 6890 | int |
| 6891 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 6892 | { |
| 6893 | unsigned int newSize; |
| 6894 | xmlChar* rebuf = NULL; |
| 6895 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6896 | if (buf == NULL) |
| 6897 | return(0); |
| 6898 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6899 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
| 6900 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6901 | /* Don't resize if we don't have to */ |
| 6902 | if (size < buf->size) |
| 6903 | return 1; |
| 6904 | |
| 6905 | /* figure out new size */ |
| 6906 | switch (buf->alloc){ |
| 6907 | case XML_BUFFER_ALLOC_DOUBLEIT: |
Daniel Veillard | bf62949 | 2004-04-20 22:20:59 +0000 | [diff] [blame] | 6908 | /*take care of empty case*/ |
| 6909 | newSize = (buf->size ? buf->size*2 : size + 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6910 | while (size > newSize) newSize *= 2; |
| 6911 | break; |
| 6912 | case XML_BUFFER_ALLOC_EXACT: |
| 6913 | newSize = size+10; |
| 6914 | break; |
| 6915 | default: |
| 6916 | newSize = size+10; |
| 6917 | break; |
| 6918 | } |
| 6919 | |
| 6920 | if (buf->content == NULL) |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6921 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6922 | else if (buf->size - buf->use < 100) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6923 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 6924 | newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6925 | } else { |
| 6926 | /* |
| 6927 | * if we are reallocating a buffer far from being full, it's |
| 6928 | * better to make a new allocation and copy only the used range |
| 6929 | * and free the old one. |
| 6930 | */ |
| 6931 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
| 6932 | if (rebuf != NULL) { |
| 6933 | memcpy(rebuf, buf->content, buf->use); |
| 6934 | xmlFree(buf->content); |
William M. Brack | 42331a9 | 2004-07-29 07:07:16 +0000 | [diff] [blame] | 6935 | rebuf[buf->use] = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6936 | } |
| 6937 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6938 | if (rebuf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6939 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6940 | return 0; |
| 6941 | } |
| 6942 | buf->content = rebuf; |
| 6943 | buf->size = newSize; |
| 6944 | |
| 6945 | return 1; |
| 6946 | } |
| 6947 | |
| 6948 | /** |
| 6949 | * xmlBufferAdd: |
| 6950 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6951 | * @str: the #xmlChar string |
| 6952 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6953 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6954 | * 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] | 6955 | * str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6956 | * |
| 6957 | * Returns 0 successful, a positive error code number otherwise |
| 6958 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6959 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6960 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6961 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6962 | unsigned int needSize; |
| 6963 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6964 | if ((str == NULL) || (buf == NULL)) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6965 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6966 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6967 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6968 | if (len < -1) { |
| 6969 | #ifdef DEBUG_BUFFER |
| 6970 | xmlGenericError(xmlGenericErrorContext, |
| 6971 | "xmlBufferAdd: len < 0\n"); |
| 6972 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6973 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6974 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6975 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6976 | |
| 6977 | if (len < 0) |
| 6978 | len = xmlStrlen(str); |
| 6979 | |
Daniel Veillard | c992332 | 2007-04-24 18:12:06 +0000 | [diff] [blame] | 6980 | if (len < 0) return -1; |
| 6981 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6982 | |
| 6983 | needSize = buf->use + len + 2; |
| 6984 | if (needSize > buf->size){ |
| 6985 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6986 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6987 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6988 | } |
| 6989 | } |
| 6990 | |
| 6991 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 6992 | buf->use += len; |
| 6993 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6994 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
| 6997 | /** |
| 6998 | * xmlBufferAddHead: |
| 6999 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7000 | * @str: the #xmlChar string |
| 7001 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7002 | * |
| 7003 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7004 | * if len == -1, the length of @str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7005 | * |
| 7006 | * Returns 0 successful, a positive error code number otherwise |
| 7007 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7008 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7009 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7010 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 7011 | unsigned int needSize; |
| 7012 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7013 | if (buf == NULL) |
| 7014 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7015 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7016 | if (str == NULL) { |
| 7017 | #ifdef DEBUG_BUFFER |
| 7018 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7019 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7020 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7021 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7022 | } |
| 7023 | if (len < -1) { |
| 7024 | #ifdef DEBUG_BUFFER |
| 7025 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7026 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7027 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7028 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7029 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7030 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7031 | |
| 7032 | if (len < 0) |
| 7033 | len = xmlStrlen(str); |
| 7034 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7035 | if (len <= 0) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7036 | |
| 7037 | needSize = buf->use + len + 2; |
| 7038 | if (needSize > buf->size){ |
| 7039 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7040 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7041 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7042 | } |
| 7043 | } |
| 7044 | |
| 7045 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 7046 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 7047 | buf->use += len; |
| 7048 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7049 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7050 | } |
| 7051 | |
| 7052 | /** |
| 7053 | * xmlBufferCat: |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7054 | * @buf: the buffer to add to |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7055 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7056 | * |
| 7057 | * Append a zero terminated string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7058 | * |
| 7059 | * Returns 0 successful, a positive error code number otherwise |
| 7060 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7061 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7062 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7063 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7064 | if (buf == NULL) |
| 7065 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7066 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
| 7067 | if (str == NULL) return -1; |
| 7068 | return xmlBufferAdd(buf, str, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7069 | } |
| 7070 | |
| 7071 | /** |
| 7072 | * xmlBufferCCat: |
| 7073 | * @buf: the buffer to dump |
| 7074 | * @str: the C char string |
| 7075 | * |
| 7076 | * Append a zero terminated C string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7077 | * |
| 7078 | * Returns 0 successful, a positive error code number otherwise |
| 7079 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7080 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7081 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7082 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 7083 | const char *cur; |
| 7084 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7085 | if (buf == NULL) |
| 7086 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7087 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7088 | if (str == NULL) { |
| 7089 | #ifdef DEBUG_BUFFER |
| 7090 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7091 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7092 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7093 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7094 | } |
| 7095 | for (cur = str;*cur != 0;cur++) { |
| 7096 | if (buf->use + 10 >= buf->size) { |
| 7097 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7098 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7099 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7100 | } |
| 7101 | } |
| 7102 | buf->content[buf->use++] = *cur; |
| 7103 | } |
| 7104 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7105 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7106 | } |
| 7107 | |
| 7108 | /** |
| 7109 | * xmlBufferWriteCHAR: |
| 7110 | * @buf: the XML buffer |
| 7111 | * @string: the string to add |
| 7112 | * |
| 7113 | * routine which manages and grows an output buffer. This one adds |
| 7114 | * xmlChars at the end of the buffer. |
| 7115 | */ |
| 7116 | void |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7117 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7118 | if (buf == NULL) |
| 7119 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7120 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7121 | xmlBufferCat(buf, string); |
| 7122 | } |
| 7123 | |
| 7124 | /** |
| 7125 | * xmlBufferWriteChar: |
| 7126 | * @buf: the XML buffer output |
| 7127 | * @string: the string to add |
| 7128 | * |
| 7129 | * routine which manage and grows an output buffer. This one add |
| 7130 | * C chars at the end of the array. |
| 7131 | */ |
| 7132 | void |
| 7133 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7134 | if (buf == NULL) |
| 7135 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7136 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7137 | xmlBufferCCat(buf, string); |
| 7138 | } |
| 7139 | |
| 7140 | |
| 7141 | /** |
| 7142 | * xmlBufferWriteQuotedString: |
| 7143 | * @buf: the XML buffer output |
| 7144 | * @string: the string to add |
| 7145 | * |
| 7146 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7147 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7148 | * quote or double-quotes internally |
| 7149 | */ |
| 7150 | void |
| 7151 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7152 | const xmlChar *cur, *base; |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7153 | if (buf == NULL) |
| 7154 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7155 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7156 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 7157 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7158 | #ifdef DEBUG_BUFFER |
| 7159 | xmlGenericError(xmlGenericErrorContext, |
| 7160 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 7161 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7162 | xmlBufferCCat(buf, "\""); |
| 7163 | base = cur = string; |
| 7164 | while(*cur != 0){ |
| 7165 | if(*cur == '"'){ |
| 7166 | if (base != cur) |
| 7167 | xmlBufferAdd(buf, base, cur - base); |
| 7168 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 7169 | cur++; |
| 7170 | base = cur; |
| 7171 | } |
| 7172 | else { |
| 7173 | cur++; |
| 7174 | } |
| 7175 | } |
| 7176 | if (base != cur) |
| 7177 | xmlBufferAdd(buf, base, cur - base); |
| 7178 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7179 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7180 | else{ |
| 7181 | xmlBufferCCat(buf, "\'"); |
| 7182 | xmlBufferCat(buf, string); |
| 7183 | xmlBufferCCat(buf, "\'"); |
| 7184 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7185 | } else { |
| 7186 | xmlBufferCCat(buf, "\""); |
| 7187 | xmlBufferCat(buf, string); |
| 7188 | xmlBufferCCat(buf, "\""); |
| 7189 | } |
| 7190 | } |
| 7191 | |
| 7192 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7193 | /** |
| 7194 | * xmlGetDocCompressMode: |
| 7195 | * @doc: the document |
| 7196 | * |
| 7197 | * get the compression ratio for a document, ZLIB based |
| 7198 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7199 | */ |
| 7200 | int |
| 7201 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 7202 | if (doc == NULL) return(-1); |
| 7203 | return(doc->compression); |
| 7204 | } |
| 7205 | |
| 7206 | /** |
| 7207 | * xmlSetDocCompressMode: |
| 7208 | * @doc: the document |
| 7209 | * @mode: the compression ratio |
| 7210 | * |
| 7211 | * set the compression ratio for a document, ZLIB based |
| 7212 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7213 | */ |
| 7214 | void |
| 7215 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 7216 | if (doc == NULL) return; |
| 7217 | if (mode < 0) doc->compression = 0; |
| 7218 | else if (mode > 9) doc->compression = 9; |
| 7219 | else doc->compression = mode; |
| 7220 | } |
| 7221 | |
| 7222 | /** |
| 7223 | * xmlGetCompressMode: |
| 7224 | * |
| 7225 | * get the default compression mode used, ZLIB based. |
| 7226 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7227 | */ |
| 7228 | int |
| 7229 | xmlGetCompressMode(void) |
| 7230 | { |
| 7231 | return (xmlCompressMode); |
| 7232 | } |
| 7233 | |
| 7234 | /** |
| 7235 | * xmlSetCompressMode: |
| 7236 | * @mode: the compression ratio |
| 7237 | * |
| 7238 | * set the default compression mode used, ZLIB based |
| 7239 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7240 | */ |
| 7241 | void |
| 7242 | xmlSetCompressMode(int mode) { |
| 7243 | if (mode < 0) xmlCompressMode = 0; |
| 7244 | else if (mode > 9) xmlCompressMode = 9; |
| 7245 | else xmlCompressMode = mode; |
| 7246 | } |
| 7247 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7248 | #define XML_TREE_NSMAP_PARENT -1 |
| 7249 | #define XML_TREE_NSMAP_XML -2 |
| 7250 | #define XML_TREE_NSMAP_DOC -3 |
| 7251 | #define XML_TREE_NSMAP_CUSTOM -4 |
| 7252 | |
| 7253 | typedef struct xmlNsMapItem *xmlNsMapItemPtr; |
| 7254 | struct xmlNsMapItem { |
| 7255 | xmlNsMapItemPtr next; |
| 7256 | xmlNsMapItemPtr prev; |
| 7257 | xmlNsPtr oldNs; /* old ns decl reference */ |
| 7258 | xmlNsPtr newNs; /* new ns decl reference */ |
| 7259 | int shadowDepth; /* Shadowed at this depth */ |
| 7260 | /* |
| 7261 | * depth: |
| 7262 | * >= 0 == @node's ns-decls |
| 7263 | * -1 == @parent's ns-decls |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7264 | * -2 == the doc->oldNs XML ns-decl |
| 7265 | * -3 == the doc->oldNs storage ns-decls |
| 7266 | * -4 == ns-decls provided via custom ns-handling |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7267 | */ |
| 7268 | int depth; |
| 7269 | }; |
| 7270 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7271 | typedef struct xmlNsMap *xmlNsMapPtr; |
| 7272 | struct xmlNsMap { |
| 7273 | xmlNsMapItemPtr first; |
| 7274 | xmlNsMapItemPtr last; |
| 7275 | xmlNsMapItemPtr pool; |
| 7276 | }; |
| 7277 | |
| 7278 | #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL)) |
| 7279 | #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next) |
| 7280 | #define XML_NSMAP_POP(m, i) \ |
| 7281 | i = (m)->last; \ |
| 7282 | (m)->last = (i)->prev; \ |
| 7283 | if ((m)->last == NULL) \ |
| 7284 | (m)->first = NULL; \ |
| 7285 | else \ |
| 7286 | (m)->last->next = NULL; \ |
| 7287 | (i)->next = (m)->pool; \ |
| 7288 | (m)->pool = i; |
| 7289 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7290 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7291 | * xmlDOMWrapNsMapFree: |
| 7292 | * @map: the ns-map |
| 7293 | * |
| 7294 | * Frees the ns-map |
| 7295 | */ |
| 7296 | static void |
| 7297 | xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap) |
| 7298 | { |
| 7299 | xmlNsMapItemPtr cur, tmp; |
| 7300 | |
| 7301 | if (nsmap == NULL) |
| 7302 | return; |
| 7303 | cur = nsmap->pool; |
| 7304 | while (cur != NULL) { |
| 7305 | tmp = cur; |
| 7306 | cur = cur->next; |
| 7307 | xmlFree(tmp); |
| 7308 | } |
| 7309 | cur = nsmap->first; |
| 7310 | while (cur != NULL) { |
| 7311 | tmp = cur; |
| 7312 | cur = cur->next; |
| 7313 | xmlFree(tmp); |
| 7314 | } |
| 7315 | xmlFree(nsmap); |
| 7316 | } |
| 7317 | |
| 7318 | /* |
| 7319 | * xmlDOMWrapNsMapAddItem: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7320 | * @map: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7321 | * @oldNs: the old ns-struct |
| 7322 | * @newNs: the new ns-struct |
| 7323 | * @depth: depth and ns-kind information |
| 7324 | * |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7325 | * Adds an ns-mapping item. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7326 | */ |
| 7327 | static xmlNsMapItemPtr |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7328 | xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7329 | xmlNsPtr oldNs, xmlNsPtr newNs, int depth) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7330 | { |
| 7331 | xmlNsMapItemPtr ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7332 | xmlNsMapPtr map; |
| 7333 | |
| 7334 | if (nsmap == NULL) |
| 7335 | return(NULL); |
| 7336 | if ((position != -1) && (position != 0)) |
| 7337 | return(NULL); |
| 7338 | map = *nsmap; |
| 7339 | |
| 7340 | if (map == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7341 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7342 | * Create the ns-map. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7343 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7344 | map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap)); |
| 7345 | if (map == NULL) { |
| 7346 | xmlTreeErrMemory("allocating namespace map"); |
| 7347 | return (NULL); |
| 7348 | } |
| 7349 | memset(map, 0, sizeof(struct xmlNsMap)); |
| 7350 | *nsmap = map; |
| 7351 | } |
| 7352 | |
| 7353 | if (map->pool != NULL) { |
| 7354 | /* |
| 7355 | * Reuse an item from the pool. |
| 7356 | */ |
| 7357 | ret = map->pool; |
| 7358 | map->pool = ret->next; |
| 7359 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7360 | } else { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7361 | /* |
| 7362 | * Create a new item. |
| 7363 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7364 | ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem)); |
| 7365 | if (ret == NULL) { |
| 7366 | xmlTreeErrMemory("allocating namespace map item"); |
| 7367 | return (NULL); |
| 7368 | } |
| 7369 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7370 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7371 | |
| 7372 | if (map->first == NULL) { |
| 7373 | /* |
| 7374 | * First ever. |
| 7375 | */ |
| 7376 | map->first = ret; |
| 7377 | map->last = ret; |
| 7378 | } else if (position == -1) { |
| 7379 | /* |
| 7380 | * Append. |
| 7381 | */ |
| 7382 | ret->prev = map->last; |
| 7383 | map->last->next = ret; |
| 7384 | map->last = ret; |
| 7385 | } else if (position == 0) { |
| 7386 | /* |
| 7387 | * Set on first position. |
| 7388 | */ |
| 7389 | map->first->prev = ret; |
| 7390 | ret->next = map->first; |
| 7391 | map->first = ret; |
| 7392 | } else |
| 7393 | return(NULL); |
| 7394 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7395 | ret->oldNs = oldNs; |
| 7396 | ret->newNs = newNs; |
| 7397 | ret->shadowDepth = -1; |
| 7398 | ret->depth = depth; |
| 7399 | return (ret); |
| 7400 | } |
| 7401 | |
| 7402 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7403 | * xmlDOMWrapStoreNs: |
| 7404 | * @doc: the doc |
| 7405 | * @nsName: the namespace name |
| 7406 | * @prefix: the prefix |
| 7407 | * |
| 7408 | * Creates or reuses an xmlNs struct on doc->oldNs with |
| 7409 | * the given prefix and namespace name. |
| 7410 | * |
| 7411 | * Returns the aquired ns struct or NULL in case of an API |
| 7412 | * or internal error. |
| 7413 | */ |
| 7414 | static xmlNsPtr |
| 7415 | xmlDOMWrapStoreNs(xmlDocPtr doc, |
| 7416 | const xmlChar *nsName, |
| 7417 | const xmlChar *prefix) |
| 7418 | { |
| 7419 | xmlNsPtr ns; |
| 7420 | |
| 7421 | if (doc == NULL) |
| 7422 | return (NULL); |
| 7423 | ns = xmlTreeEnsureXMLDecl(doc); |
| 7424 | if (ns == NULL) |
| 7425 | return (NULL); |
| 7426 | if (ns->next != NULL) { |
| 7427 | /* Reuse. */ |
| 7428 | ns = ns->next; |
| 7429 | while (ns != NULL) { |
| 7430 | if (((ns->prefix == prefix) || |
| 7431 | xmlStrEqual(ns->prefix, prefix)) && |
| 7432 | xmlStrEqual(ns->href, nsName)) { |
| 7433 | return (ns); |
| 7434 | } |
| 7435 | if (ns->next == NULL) |
| 7436 | break; |
| 7437 | ns = ns->next; |
| 7438 | } |
| 7439 | } |
| 7440 | /* Create. */ |
| 7441 | ns->next = xmlNewNs(NULL, nsName, prefix); |
| 7442 | return (ns->next); |
| 7443 | } |
| 7444 | |
| 7445 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7446 | * xmlDOMWrapNewCtxt: |
| 7447 | * |
| 7448 | * Allocates and initializes a new DOM-wrapper context. |
| 7449 | * |
| 7450 | * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror. |
| 7451 | */ |
| 7452 | xmlDOMWrapCtxtPtr |
| 7453 | xmlDOMWrapNewCtxt(void) |
| 7454 | { |
| 7455 | xmlDOMWrapCtxtPtr ret; |
| 7456 | |
| 7457 | ret = xmlMalloc(sizeof(xmlDOMWrapCtxt)); |
| 7458 | if (ret == NULL) { |
| 7459 | xmlTreeErrMemory("allocating DOM-wrapper context"); |
| 7460 | return (NULL); |
| 7461 | } |
| 7462 | memset(ret, 0, sizeof(xmlDOMWrapCtxt)); |
| 7463 | return (ret); |
| 7464 | } |
| 7465 | |
| 7466 | /* |
| 7467 | * xmlDOMWrapFreeCtxt: |
| 7468 | * @ctxt: the DOM-wrapper context |
| 7469 | * |
| 7470 | * Frees the DOM-wrapper context. |
| 7471 | */ |
| 7472 | void |
| 7473 | xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt) |
| 7474 | { |
| 7475 | if (ctxt == NULL) |
| 7476 | return; |
| 7477 | if (ctxt->namespaceMap != NULL) |
| 7478 | xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap); |
| 7479 | /* |
| 7480 | * TODO: Store the namespace map in the context. |
| 7481 | */ |
| 7482 | xmlFree(ctxt); |
| 7483 | } |
| 7484 | |
| 7485 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7486 | * xmlTreeLookupNsListByPrefix: |
| 7487 | * @nsList: a list of ns-structs |
| 7488 | * @prefix: the searched prefix |
| 7489 | * |
| 7490 | * Searches for a ns-decl with the given prefix in @nsList. |
| 7491 | * |
| 7492 | * Returns the ns-decl if found, NULL if not found and on |
| 7493 | * API errors. |
| 7494 | */ |
| 7495 | static xmlNsPtr |
| 7496 | xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix) |
| 7497 | { |
| 7498 | if (nsList == NULL) |
| 7499 | return (NULL); |
| 7500 | { |
| 7501 | xmlNsPtr ns; |
| 7502 | ns = nsList; |
| 7503 | do { |
| 7504 | if ((prefix == ns->prefix) || |
| 7505 | xmlStrEqual(prefix, ns->prefix)) { |
| 7506 | return (ns); |
| 7507 | } |
| 7508 | ns = ns->next; |
| 7509 | } while (ns != NULL); |
| 7510 | } |
| 7511 | return (NULL); |
| 7512 | } |
| 7513 | |
| 7514 | /* |
| 7515 | * |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7516 | * xmlDOMWrapNSNormGatherInScopeNs: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7517 | * @map: the namespace map |
| 7518 | * @node: the node to start with |
| 7519 | * |
| 7520 | * Puts in-scope namespaces into the ns-map. |
| 7521 | * |
| 7522 | * Returns 0 on success, -1 on API or internal errors. |
| 7523 | */ |
| 7524 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7525 | xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7526 | xmlNodePtr node) |
| 7527 | { |
| 7528 | xmlNodePtr cur; |
| 7529 | xmlNsPtr ns; |
| 7530 | xmlNsMapItemPtr mi; |
| 7531 | int shadowed; |
| 7532 | |
| 7533 | if ((map == NULL) || (*map != NULL)) |
| 7534 | return (-1); |
| 7535 | /* |
| 7536 | * Get in-scope ns-decls of @parent. |
| 7537 | */ |
| 7538 | cur = node; |
| 7539 | while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) { |
| 7540 | if (cur->type == XML_ELEMENT_NODE) { |
| 7541 | if (cur->nsDef != NULL) { |
| 7542 | ns = cur->nsDef; |
| 7543 | do { |
| 7544 | shadowed = 0; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7545 | if (XML_NSMAP_NOTEMPTY(*map)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7546 | /* |
| 7547 | * Skip shadowed prefixes. |
| 7548 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7549 | XML_NSMAP_FOREACH(*map, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7550 | if ((ns->prefix == mi->newNs->prefix) || |
| 7551 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) { |
| 7552 | shadowed = 1; |
| 7553 | break; |
| 7554 | } |
| 7555 | } |
| 7556 | } |
| 7557 | /* |
| 7558 | * Insert mapping. |
| 7559 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7560 | mi = xmlDOMWrapNsMapAddItem(map, 0, NULL, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7561 | ns, XML_TREE_NSMAP_PARENT); |
| 7562 | if (mi == NULL) |
| 7563 | return (-1); |
| 7564 | if (shadowed) |
| 7565 | mi->shadowDepth = 0; |
| 7566 | ns = ns->next; |
| 7567 | } while (ns != NULL); |
| 7568 | } |
| 7569 | } |
| 7570 | cur = cur->parent; |
| 7571 | } |
| 7572 | return (0); |
| 7573 | } |
| 7574 | |
| 7575 | /* |
| 7576 | * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict; |
| 7577 | * otherwise copy it, when it was in the source-dict. |
| 7578 | */ |
| 7579 | #define XML_TREE_ADOPT_STR(str) \ |
| 7580 | if (adoptStr && (str != NULL)) { \ |
| 7581 | if (destDoc->dict) { \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7582 | const xmlChar *old = str; \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7583 | str = xmlDictLookup(destDoc->dict, str, -1); \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7584 | if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \ |
| 7585 | (!xmlDictOwns(sourceDoc->dict, old))) \ |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 7586 | xmlFree((char *)old); \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7587 | } else if ((sourceDoc) && (sourceDoc->dict) && \ |
| 7588 | xmlDictOwns(sourceDoc->dict, str)) { \ |
| 7589 | str = BAD_CAST xmlStrdup(str); \ |
| 7590 | } \ |
| 7591 | } |
| 7592 | |
| 7593 | /* |
| 7594 | * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then |
| 7595 | * put it in dest-dict or copy it. |
| 7596 | */ |
| 7597 | #define XML_TREE_ADOPT_STR_2(str) \ |
| 7598 | if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \ |
| 7599 | (sourceDoc->dict != NULL) && \ |
| 7600 | xmlDictOwns(sourceDoc->dict, cur->content)) { \ |
| 7601 | if (destDoc->dict) \ |
| 7602 | cur->content = (xmlChar *) \ |
| 7603 | xmlDictLookup(destDoc->dict, cur->content, -1); \ |
| 7604 | else \ |
| 7605 | cur->content = xmlStrdup(BAD_CAST cur->content); \ |
| 7606 | } |
| 7607 | |
| 7608 | /* |
| 7609 | * xmlDOMWrapNSNormAddNsMapItem2: |
| 7610 | * |
| 7611 | * For internal use. Adds a ns-decl mapping. |
| 7612 | * |
| 7613 | * Returns 0 on success, -1 on internal errors. |
| 7614 | */ |
| 7615 | static int |
| 7616 | xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number, |
| 7617 | xmlNsPtr oldNs, xmlNsPtr newNs) |
| 7618 | { |
| 7619 | if (*list == NULL) { |
| 7620 | *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr)); |
| 7621 | if (*list == NULL) { |
| 7622 | xmlTreeErrMemory("alloc ns map item"); |
| 7623 | return(-1); |
| 7624 | } |
| 7625 | *size = 3; |
| 7626 | *number = 0; |
| 7627 | } else if ((*number) >= (*size)) { |
| 7628 | *size *= 2; |
| 7629 | *list = (xmlNsPtr *) xmlRealloc(*list, |
| 7630 | (*size) * 2 * sizeof(xmlNsPtr)); |
| 7631 | if (*list == NULL) { |
| 7632 | xmlTreeErrMemory("realloc ns map item"); |
| 7633 | return(-1); |
| 7634 | } |
| 7635 | } |
| 7636 | (*list)[2 * (*number)] = oldNs; |
| 7637 | (*list)[2 * (*number) +1] = newNs; |
| 7638 | (*number)++; |
| 7639 | return (0); |
| 7640 | } |
| 7641 | |
| 7642 | /* |
| 7643 | * xmlDOMWrapRemoveNode: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7644 | * @ctxt: a DOM wrapper context |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7645 | * @doc: the doc |
| 7646 | * @node: the node to be removed. |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7647 | * @options: set of options, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7648 | * |
| 7649 | * Unlinks the given node from its owner. |
| 7650 | * This will substitute ns-references to node->nsDef for |
| 7651 | * ns-references to doc->oldNs, thus ensuring the removed |
| 7652 | * branch to be autark wrt ns-references. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7653 | * |
| 7654 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7655 | * |
| 7656 | * Returns 0 on success, 1 if the node is not supported, |
| 7657 | * -1 on API and internal errors. |
| 7658 | */ |
| 7659 | int |
| 7660 | xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc, |
| 7661 | xmlNodePtr node, int options ATTRIBUTE_UNUSED) |
| 7662 | { |
| 7663 | xmlNsPtr *list = NULL; |
| 7664 | int sizeList, nbList, i, j; |
| 7665 | xmlNsPtr ns; |
| 7666 | |
| 7667 | if ((node == NULL) || (doc == NULL) || (node->doc != doc)) |
| 7668 | return (-1); |
| 7669 | |
| 7670 | /* TODO: 0 or -1 ? */ |
| 7671 | if (node->parent == NULL) |
| 7672 | return (0); |
| 7673 | |
| 7674 | switch (node->type) { |
| 7675 | case XML_TEXT_NODE: |
| 7676 | case XML_CDATA_SECTION_NODE: |
| 7677 | case XML_ENTITY_REF_NODE: |
| 7678 | case XML_PI_NODE: |
| 7679 | case XML_COMMENT_NODE: |
| 7680 | xmlUnlinkNode(node); |
| 7681 | return (0); |
| 7682 | case XML_ELEMENT_NODE: |
| 7683 | case XML_ATTRIBUTE_NODE: |
| 7684 | break; |
| 7685 | default: |
| 7686 | return (1); |
| 7687 | } |
| 7688 | xmlUnlinkNode(node); |
| 7689 | /* |
| 7690 | * Save out-of-scope ns-references in doc->oldNs. |
| 7691 | */ |
| 7692 | do { |
| 7693 | switch (node->type) { |
| 7694 | case XML_ELEMENT_NODE: |
| 7695 | if ((ctxt == NULL) && (node->nsDef != NULL)) { |
| 7696 | ns = node->nsDef; |
| 7697 | do { |
| 7698 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 7699 | &nbList, ns, ns) == -1) |
| 7700 | goto internal_error; |
| 7701 | ns = ns->next; |
| 7702 | } while (ns != NULL); |
| 7703 | } |
| 7704 | /* No break on purpose. */ |
| 7705 | case XML_ATTRIBUTE_NODE: |
| 7706 | if (node->ns != NULL) { |
| 7707 | /* |
| 7708 | * Find a mapping. |
| 7709 | */ |
| 7710 | if (list != NULL) { |
| 7711 | for (i = 0, j = 0; i < nbList; i++, j += 2) { |
| 7712 | if (node->ns == list[j]) { |
| 7713 | node->ns = list[++j]; |
| 7714 | goto next_node; |
| 7715 | } |
| 7716 | } |
| 7717 | } |
| 7718 | ns = NULL; |
| 7719 | if (ctxt != NULL) { |
| 7720 | /* |
| 7721 | * User defined. |
| 7722 | */ |
| 7723 | } else { |
| 7724 | /* |
| 7725 | * Add to doc's oldNs. |
| 7726 | */ |
| 7727 | ns = xmlDOMWrapStoreNs(doc, node->ns->href, |
| 7728 | node->ns->prefix); |
| 7729 | if (ns == NULL) |
| 7730 | goto internal_error; |
| 7731 | } |
| 7732 | if (ns != NULL) { |
| 7733 | /* |
| 7734 | * Add mapping. |
| 7735 | */ |
| 7736 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 7737 | &nbList, node->ns, ns) == -1) |
| 7738 | goto internal_error; |
| 7739 | } |
| 7740 | node->ns = ns; |
| 7741 | } |
| 7742 | if ((node->type == XML_ELEMENT_NODE) && |
| 7743 | (node->properties != NULL)) { |
| 7744 | node = (xmlNodePtr) node->properties; |
| 7745 | continue; |
| 7746 | } |
| 7747 | break; |
| 7748 | default: |
| 7749 | goto next_sibling; |
| 7750 | } |
| 7751 | next_node: |
| 7752 | if ((node->type == XML_ELEMENT_NODE) && |
| 7753 | (node->children != NULL)) { |
| 7754 | node = node->children; |
| 7755 | continue; |
| 7756 | } |
| 7757 | next_sibling: |
| 7758 | if (node == NULL) |
| 7759 | break; |
| 7760 | if (node->next != NULL) |
| 7761 | node = node->next; |
| 7762 | else { |
| 7763 | node = node->parent; |
| 7764 | goto next_sibling; |
| 7765 | } |
| 7766 | } while (node != NULL); |
| 7767 | |
| 7768 | if (list != NULL) |
| 7769 | xmlFree(list); |
| 7770 | return (0); |
| 7771 | |
| 7772 | internal_error: |
| 7773 | if (list != NULL) |
| 7774 | xmlFree(list); |
| 7775 | return (-1); |
| 7776 | } |
| 7777 | |
| 7778 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7779 | * xmlSearchNsByNamespaceStrict: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7780 | * @doc: the document |
| 7781 | * @node: the start node |
| 7782 | * @nsName: the searched namespace name |
| 7783 | * @retNs: the resulting ns-decl |
| 7784 | * @prefixed: if the found ns-decl must have a prefix (for attributes) |
| 7785 | * |
| 7786 | * Dynamically searches for a ns-declaration which matches |
| 7787 | * the given @nsName in the ancestor-or-self axis of @node. |
| 7788 | * |
| 7789 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 7790 | * and internal errors. |
| 7791 | */ |
| 7792 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7793 | xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node, |
| 7794 | const xmlChar* nsName, |
| 7795 | xmlNsPtr *retNs, int prefixed) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7796 | { |
| 7797 | xmlNodePtr cur, prev = NULL, out = NULL; |
| 7798 | xmlNsPtr ns, prevns; |
| 7799 | |
| 7800 | if ((doc == NULL) || (nsName == NULL) || (retNs == NULL)) |
| 7801 | return (-1); |
| 7802 | |
| 7803 | *retNs = NULL; |
| 7804 | if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
| 7805 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 7806 | if (*retNs == NULL) |
| 7807 | return (-1); |
| 7808 | return (1); |
| 7809 | } |
| 7810 | cur = node; |
| 7811 | do { |
| 7812 | if (cur->type == XML_ELEMENT_NODE) { |
| 7813 | if (cur->nsDef != NULL) { |
| 7814 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 7815 | if (prefixed && (ns->prefix == NULL)) |
| 7816 | continue; |
| 7817 | if (prev != NULL) { |
| 7818 | /* |
| 7819 | * Check the last level of ns-decls for a |
| 7820 | * shadowing prefix. |
| 7821 | */ |
| 7822 | prevns = prev->nsDef; |
| 7823 | do { |
| 7824 | if ((prevns->prefix == ns->prefix) || |
| 7825 | ((prevns->prefix != NULL) && |
| 7826 | (ns->prefix != NULL) && |
| 7827 | xmlStrEqual(prevns->prefix, ns->prefix))) { |
| 7828 | /* |
| 7829 | * Shadowed. |
| 7830 | */ |
| 7831 | break; |
| 7832 | } |
| 7833 | prevns = prevns->next; |
| 7834 | } while (prevns != NULL); |
| 7835 | if (prevns != NULL) |
| 7836 | continue; |
| 7837 | } |
| 7838 | /* |
| 7839 | * Ns-name comparison. |
| 7840 | */ |
| 7841 | if ((nsName == ns->href) || |
| 7842 | xmlStrEqual(nsName, ns->href)) { |
| 7843 | /* |
| 7844 | * At this point the prefix can only be shadowed, |
| 7845 | * if we are the the (at least) 3rd level of |
| 7846 | * ns-decls. |
| 7847 | */ |
| 7848 | if (out) { |
| 7849 | int ret; |
| 7850 | |
| 7851 | ret = xmlNsInScope(doc, node, prev, ns->prefix); |
| 7852 | if (ret < 0) |
| 7853 | return (-1); |
| 7854 | /* |
| 7855 | * TODO: Should we try to find a matching ns-name |
| 7856 | * only once? This here keeps on searching. |
| 7857 | * I think we should try further since, there might |
| 7858 | * be an other matching ns-decl with an unshadowed |
| 7859 | * prefix. |
| 7860 | */ |
| 7861 | if (! ret) |
| 7862 | continue; |
| 7863 | } |
| 7864 | *retNs = ns; |
| 7865 | return (1); |
| 7866 | } |
| 7867 | } |
| 7868 | out = prev; |
| 7869 | prev = cur; |
| 7870 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7871 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 7872 | (cur->type == XML_ENTITY_DECL)) |
| 7873 | return (0); |
| 7874 | cur = cur->parent; |
| 7875 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 7876 | return (0); |
| 7877 | } |
| 7878 | |
| 7879 | /* |
| 7880 | * xmlSearchNsByPrefixStrict: |
| 7881 | * @doc: the document |
| 7882 | * @node: the start node |
| 7883 | * @prefix: the searched namespace prefix |
| 7884 | * @retNs: the resulting ns-decl |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7885 | * |
| 7886 | * Dynamically searches for a ns-declaration which matches |
| 7887 | * the given @nsName in the ancestor-or-self axis of @node. |
| 7888 | * |
| 7889 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 7890 | * and internal errors. |
| 7891 | */ |
| 7892 | static int |
| 7893 | xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node, |
| 7894 | const xmlChar* prefix, |
| 7895 | xmlNsPtr *retNs) |
| 7896 | { |
| 7897 | xmlNodePtr cur; |
| 7898 | xmlNsPtr ns; |
| 7899 | |
| 7900 | if ((doc == NULL) || (node == NULL)) |
| 7901 | return (-1); |
| 7902 | |
| 7903 | if (retNs) |
| 7904 | *retNs = NULL; |
| 7905 | if (IS_STR_XML(prefix)) { |
| 7906 | if (retNs) { |
| 7907 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 7908 | if (*retNs == NULL) |
| 7909 | return (-1); |
| 7910 | } |
| 7911 | return (1); |
| 7912 | } |
| 7913 | cur = node; |
| 7914 | do { |
| 7915 | if (cur->type == XML_ELEMENT_NODE) { |
| 7916 | if (cur->nsDef != NULL) { |
| 7917 | ns = cur->nsDef; |
| 7918 | do { |
| 7919 | if ((prefix == ns->prefix) || |
| 7920 | xmlStrEqual(prefix, ns->prefix)) |
| 7921 | { |
| 7922 | /* |
| 7923 | * Disabled namespaces, e.g. xmlns:abc="". |
| 7924 | */ |
| 7925 | if (ns->href == NULL) |
| 7926 | return(0); |
| 7927 | if (retNs) |
| 7928 | *retNs = ns; |
| 7929 | return (1); |
| 7930 | } |
| 7931 | ns = ns->next; |
| 7932 | } while (ns != NULL); |
| 7933 | } |
| 7934 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 7935 | (cur->type == XML_ENTITY_DECL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7936 | return (0); |
| 7937 | cur = cur->parent; |
| 7938 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 7939 | return (0); |
| 7940 | } |
| 7941 | |
| 7942 | /* |
| 7943 | * xmlDOMWrapNSNormDeclareNsForced: |
| 7944 | * @doc: the doc |
| 7945 | * @elem: the element-node to declare on |
| 7946 | * @nsName: the namespace-name of the ns-decl |
| 7947 | * @prefix: the preferred prefix of the ns-decl |
| 7948 | * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls |
| 7949 | * |
| 7950 | * Declares a new namespace on @elem. It tries to use the |
| 7951 | * given @prefix; if a ns-decl with the given prefix is already existent |
| 7952 | * on @elem, it will generate an other prefix. |
| 7953 | * |
| 7954 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 7955 | * and internal errors. |
| 7956 | */ |
| 7957 | static xmlNsPtr |
| 7958 | xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc, |
| 7959 | xmlNodePtr elem, |
| 7960 | const xmlChar *nsName, |
| 7961 | const xmlChar *prefix, |
| 7962 | int checkShadow) |
| 7963 | { |
| 7964 | |
| 7965 | xmlNsPtr ret; |
| 7966 | char buf[50]; |
| 7967 | const xmlChar *pref; |
| 7968 | int counter = 0; |
| 7969 | /* |
| 7970 | * Create a ns-decl on @anchor. |
| 7971 | */ |
| 7972 | pref = prefix; |
| 7973 | while (1) { |
| 7974 | /* |
| 7975 | * Lookup whether the prefix is unused in elem's ns-decls. |
| 7976 | */ |
| 7977 | if ((elem->nsDef != NULL) && |
| 7978 | (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL)) |
| 7979 | goto ns_next_prefix; |
| 7980 | if (checkShadow && elem->parent && |
| 7981 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 7982 | /* |
| 7983 | * Does it shadow ancestor ns-decls? |
| 7984 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7985 | if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7986 | goto ns_next_prefix; |
| 7987 | } |
| 7988 | ret = xmlNewNs(NULL, nsName, pref); |
| 7989 | if (ret == NULL) |
| 7990 | return (NULL); |
| 7991 | if (elem->nsDef == NULL) |
| 7992 | elem->nsDef = ret; |
| 7993 | else { |
| 7994 | xmlNsPtr ns2 = elem->nsDef; |
| 7995 | while (ns2->next != NULL) |
| 7996 | ns2 = ns2->next; |
| 7997 | ns2->next = ret; |
| 7998 | } |
| 7999 | return (ret); |
| 8000 | ns_next_prefix: |
| 8001 | counter++; |
| 8002 | if (counter > 1000) |
| 8003 | return (NULL); |
| 8004 | if (prefix == NULL) { |
| 8005 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8006 | "ns_%d", counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8007 | } else |
| 8008 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8009 | "%.30s_%d", (char *)prefix, counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8010 | pref = BAD_CAST buf; |
| 8011 | } |
| 8012 | } |
| 8013 | |
| 8014 | /* |
| 8015 | * xmlDOMWrapNSNormAquireNormalizedNs: |
| 8016 | * @doc: the doc |
| 8017 | * @elem: the element-node to declare namespaces on |
| 8018 | * @ns: the ns-struct to use for the search |
| 8019 | * @retNs: the found/created ns-struct |
| 8020 | * @nsMap: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8021 | * @depth: the current tree depth |
| 8022 | * @ancestorsOnly: search in ancestor ns-decls only |
| 8023 | * @prefixed: if the searched ns-decl must have a prefix (for attributes) |
| 8024 | * |
| 8025 | * Searches for a matching ns-name in the ns-decls of @nsMap, if not |
| 8026 | * found it will either declare it on @elem, or store it in doc->oldNs. |
| 8027 | * If a new ns-decl needs to be declared on @elem, it tries to use the |
| 8028 | * @ns->prefix for it, if this prefix is already in use on @elem, it will |
| 8029 | * change the prefix or the new ns-decl. |
| 8030 | * |
| 8031 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8032 | */ |
| 8033 | static int |
| 8034 | xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc, |
| 8035 | xmlNodePtr elem, |
| 8036 | xmlNsPtr ns, |
| 8037 | xmlNsPtr *retNs, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8038 | xmlNsMapPtr *nsMap, |
| 8039 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8040 | int depth, |
| 8041 | int ancestorsOnly, |
| 8042 | int prefixed) |
| 8043 | { |
| 8044 | xmlNsMapItemPtr mi; |
| 8045 | |
| 8046 | if ((doc == NULL) || (ns == NULL) || (retNs == NULL) || |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8047 | (nsMap == NULL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8048 | return (-1); |
| 8049 | |
| 8050 | *retNs = NULL; |
| 8051 | /* |
| 8052 | * Handle XML namespace. |
| 8053 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8054 | if (IS_STR_XML(ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8055 | /* |
| 8056 | * Insert XML namespace mapping. |
| 8057 | */ |
| 8058 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8059 | if (*retNs == NULL) |
| 8060 | return (-1); |
| 8061 | return (0); |
| 8062 | } |
| 8063 | /* |
| 8064 | * If the search should be done in ancestors only and no |
| 8065 | * @elem (the first ancestor) was specified, then skip the search. |
| 8066 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8067 | if ((XML_NSMAP_NOTEMPTY(*nsMap)) && |
| 8068 | (! (ancestorsOnly && (elem == NULL)))) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8069 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8070 | /* |
| 8071 | * Try to find an equal ns-name in in-scope ns-decls. |
| 8072 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8073 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8074 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8075 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8076 | * ancestorsOnly: This should be turned on to gain speed, |
| 8077 | * if one knows that the branch itself was already |
| 8078 | * ns-wellformed and no stale references existed. |
| 8079 | * I.e. it searches in the ancestor axis only. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8080 | */ |
| 8081 | ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) && |
| 8082 | /* Skip shadowed prefixes. */ |
| 8083 | (mi->shadowDepth == -1) && |
| 8084 | /* Skip xmlns="" or xmlns:foo="". */ |
| 8085 | ((mi->newNs->href != NULL) && |
| 8086 | (mi->newNs->href[0] != 0)) && |
| 8087 | /* Ensure a prefix if wanted. */ |
| 8088 | ((! prefixed) || (mi->newNs->prefix != NULL)) && |
| 8089 | /* Equal ns name */ |
| 8090 | ((mi->newNs->href == ns->href) || |
| 8091 | xmlStrEqual(mi->newNs->href, ns->href))) { |
| 8092 | /* Set the mapping. */ |
| 8093 | mi->oldNs = ns; |
| 8094 | *retNs = mi->newNs; |
| 8095 | return (0); |
| 8096 | } |
| 8097 | } |
| 8098 | } |
| 8099 | /* |
| 8100 | * No luck, the namespace is out of scope or shadowed. |
| 8101 | */ |
| 8102 | if (elem == NULL) { |
| 8103 | xmlNsPtr tmpns; |
| 8104 | |
| 8105 | /* |
| 8106 | * Store ns-decls in "oldNs" of the document-node. |
| 8107 | */ |
| 8108 | tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix); |
| 8109 | if (tmpns == NULL) |
| 8110 | return (-1); |
| 8111 | /* |
| 8112 | * Insert mapping. |
| 8113 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8114 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8115 | tmpns, XML_TREE_NSMAP_DOC) == NULL) { |
| 8116 | xmlFreeNs(tmpns); |
| 8117 | return (-1); |
| 8118 | } |
| 8119 | *retNs = tmpns; |
| 8120 | } else { |
| 8121 | xmlNsPtr tmpns; |
| 8122 | |
| 8123 | tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href, |
| 8124 | ns->prefix, 0); |
| 8125 | if (tmpns == NULL) |
| 8126 | return (-1); |
| 8127 | |
| 8128 | if (*nsMap != NULL) { |
| 8129 | /* |
| 8130 | * Does it shadow ancestor ns-decls? |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8131 | */ |
| 8132 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8133 | if ((mi->depth < depth) && |
| 8134 | (mi->shadowDepth == -1) && |
| 8135 | ((ns->prefix == mi->newNs->prefix) || |
| 8136 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8137 | /* |
| 8138 | * Shadows. |
| 8139 | */ |
| 8140 | mi->shadowDepth = depth; |
| 8141 | break; |
| 8142 | } |
| 8143 | } |
| 8144 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8145 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8146 | xmlFreeNs(tmpns); |
| 8147 | return (-1); |
| 8148 | } |
| 8149 | *retNs = tmpns; |
| 8150 | } |
| 8151 | return (0); |
| 8152 | } |
| 8153 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8154 | typedef enum { |
| 8155 | XML_DOM_RECONNS_REMOVEREDUND = 1<<0 |
| 8156 | } xmlDOMReconcileNSOptions; |
| 8157 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8158 | /* |
| 8159 | * xmlDOMWrapReconcileNamespaces: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8160 | * @ctxt: DOM wrapper context, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8161 | * @elem: the element-node |
| 8162 | * @options: option flags |
| 8163 | * |
| 8164 | * Ensures that ns-references point to ns-decls hold on element-nodes. |
| 8165 | * Ensures that the tree is namespace wellformed by creating additional |
| 8166 | * ns-decls where needed. Note that, since prefixes of already existent |
| 8167 | * ns-decls can be shadowed by this process, it could break QNames in |
| 8168 | * attribute values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8169 | * |
| 8170 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8171 | * |
| 8172 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8173 | */ |
| 8174 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8175 | int |
| 8176 | xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8177 | xmlNodePtr elem, |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8178 | int options) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8179 | { |
| 8180 | int depth = -1, adoptns = 0, parnsdone = 0; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8181 | xmlNsPtr ns, prevns; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8182 | xmlDocPtr doc; |
| 8183 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8184 | xmlNsMapPtr nsMap = NULL; |
| 8185 | xmlNsMapItemPtr /* topmi = NULL, */ mi; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8186 | /* @ancestorsOnly should be set by an option flag. */ |
| 8187 | int ancestorsOnly = 0; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8188 | int optRemoveRedundantNS = |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8189 | ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0; |
| 8190 | xmlNsPtr *listRedund = NULL; |
| 8191 | int sizeRedund = 0, nbRedund = 0, ret, i, j; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8192 | |
| 8193 | if ((elem == NULL) || (elem->doc == NULL) || |
| 8194 | (elem->type != XML_ELEMENT_NODE)) |
| 8195 | return (-1); |
| 8196 | |
| 8197 | doc = elem->doc; |
| 8198 | cur = elem; |
| 8199 | do { |
| 8200 | switch (cur->type) { |
| 8201 | case XML_ELEMENT_NODE: |
| 8202 | adoptns = 1; |
| 8203 | curElem = cur; |
| 8204 | depth++; |
| 8205 | /* |
| 8206 | * Namespace declarations. |
| 8207 | */ |
| 8208 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8209 | prevns = NULL; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8210 | ns = cur->nsDef; |
| 8211 | while (ns != NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8212 | if (! parnsdone) { |
| 8213 | if ((elem->parent) && |
| 8214 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8215 | /* |
| 8216 | * Gather ancestor in-scope ns-decls. |
| 8217 | */ |
| 8218 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8219 | elem->parent) == -1) |
| 8220 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8221 | } |
| 8222 | parnsdone = 1; |
| 8223 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8224 | |
| 8225 | /* |
| 8226 | * Lookup the ns ancestor-axis for equal ns-decls in scope. |
| 8227 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8228 | if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8229 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8230 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8231 | (mi->shadowDepth == -1) && |
| 8232 | ((ns->prefix == mi->newNs->prefix) || |
| 8233 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) && |
| 8234 | ((ns->href == mi->newNs->href) || |
| 8235 | xmlStrEqual(ns->href, mi->newNs->href))) |
| 8236 | { |
| 8237 | /* |
| 8238 | * A redundant ns-decl was found. |
| 8239 | * Add it to the list of redundant ns-decls. |
| 8240 | */ |
| 8241 | if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund, |
| 8242 | &sizeRedund, &nbRedund, ns, mi->newNs) == -1) |
| 8243 | goto internal_error; |
| 8244 | /* |
| 8245 | * Remove the ns-decl from the element-node. |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8246 | */ |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8247 | if (prevns) |
| 8248 | prevns->next = ns->next; |
| 8249 | else |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8250 | cur->nsDef = ns->next; |
| 8251 | goto next_ns_decl; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8252 | } |
| 8253 | } |
| 8254 | } |
| 8255 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8256 | /* |
| 8257 | * Skip ns-references handling if the referenced |
| 8258 | * ns-decl is declared on the same element. |
| 8259 | */ |
| 8260 | if ((cur->ns != NULL) && adoptns && (cur->ns == ns)) |
| 8261 | adoptns = 0; |
| 8262 | /* |
| 8263 | * Does it shadow any ns-decl? |
| 8264 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8265 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
| 8266 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8267 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8268 | (mi->shadowDepth == -1) && |
| 8269 | ((ns->prefix == mi->newNs->prefix) || |
| 8270 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8271 | |
| 8272 | mi->shadowDepth = depth; |
| 8273 | } |
| 8274 | } |
| 8275 | } |
| 8276 | /* |
| 8277 | * Push mapping. |
| 8278 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8279 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8280 | depth) == NULL) |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8281 | goto internal_error; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8282 | |
| 8283 | prevns = ns; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8284 | next_ns_decl: |
| 8285 | ns = ns->next; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8286 | } |
| 8287 | } |
| 8288 | if (! adoptns) |
| 8289 | goto ns_end; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8290 | /* No break on purpose. */ |
| 8291 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8292 | /* No ns, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8293 | if (cur->ns == NULL) |
| 8294 | goto ns_end; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8295 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8296 | if (! parnsdone) { |
| 8297 | if ((elem->parent) && |
| 8298 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8299 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8300 | elem->parent) == -1) |
| 8301 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8302 | } |
| 8303 | parnsdone = 1; |
| 8304 | } |
| 8305 | /* |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8306 | * Adjust the reference if this was a redundant ns-decl. |
| 8307 | */ |
| 8308 | if (listRedund) { |
| 8309 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8310 | if (cur->ns == listRedund[j]) { |
| 8311 | cur->ns = listRedund[++j]; |
| 8312 | break; |
| 8313 | } |
| 8314 | } |
| 8315 | } |
| 8316 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8317 | * Adopt ns-references. |
| 8318 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8319 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8320 | /* |
| 8321 | * Search for a mapping. |
| 8322 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8323 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8324 | if ((mi->shadowDepth == -1) && |
| 8325 | (cur->ns == mi->oldNs)) { |
| 8326 | |
| 8327 | cur->ns = mi->newNs; |
| 8328 | goto ns_end; |
| 8329 | } |
| 8330 | } |
| 8331 | } |
| 8332 | /* |
| 8333 | * Aquire a normalized ns-decl and add it to the map. |
| 8334 | */ |
| 8335 | if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem, |
| 8336 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8337 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8338 | ancestorsOnly, |
| 8339 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8340 | goto internal_error; |
| 8341 | cur->ns = ns; |
| 8342 | |
| 8343 | ns_end: |
| 8344 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8345 | (cur->properties != NULL)) { |
| 8346 | /* |
| 8347 | * Process attributes. |
| 8348 | */ |
| 8349 | cur = (xmlNodePtr) cur->properties; |
| 8350 | continue; |
| 8351 | } |
| 8352 | break; |
| 8353 | default: |
| 8354 | goto next_sibling; |
| 8355 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8356 | into_content: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8357 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8358 | (cur->children != NULL)) { |
| 8359 | /* |
| 8360 | * Process content of element-nodes only. |
| 8361 | */ |
| 8362 | cur = cur->children; |
| 8363 | continue; |
| 8364 | } |
| 8365 | next_sibling: |
| 8366 | if (cur == elem) |
| 8367 | break; |
| 8368 | if (cur->type == XML_ELEMENT_NODE) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8369 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8370 | /* |
| 8371 | * Pop mappings. |
| 8372 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8373 | while ((nsMap->last != NULL) && |
| 8374 | (nsMap->last->depth >= depth)) |
| 8375 | { |
| 8376 | XML_NSMAP_POP(nsMap, mi) |
| 8377 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8378 | /* |
| 8379 | * Unshadow. |
| 8380 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8381 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8382 | if (mi->shadowDepth >= depth) |
| 8383 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8384 | } |
| 8385 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8386 | depth--; |
| 8387 | } |
| 8388 | if (cur->next != NULL) |
| 8389 | cur = cur->next; |
| 8390 | else { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8391 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 8392 | cur = cur->parent; |
| 8393 | goto into_content; |
| 8394 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8395 | cur = cur->parent; |
| 8396 | goto next_sibling; |
| 8397 | } |
| 8398 | } while (cur != NULL); |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8399 | |
| 8400 | ret = 0; |
| 8401 | goto exit; |
| 8402 | internal_error: |
| 8403 | ret = -1; |
| 8404 | exit: |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8405 | if (listRedund) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8406 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8407 | xmlFreeNs(listRedund[j]); |
| 8408 | } |
| 8409 | xmlFree(listRedund); |
| 8410 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8411 | if (nsMap != NULL) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8412 | xmlDOMWrapNsMapFree(nsMap); |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8413 | return (ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8414 | } |
| 8415 | |
| 8416 | /* |
| 8417 | * xmlDOMWrapAdoptBranch: |
| 8418 | * @ctxt: the optional context for custom processing |
| 8419 | * @sourceDoc: the optional sourceDoc |
| 8420 | * @node: the element-node to start with |
| 8421 | * @destDoc: the destination doc for adoption |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8422 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8423 | * @options: option flags |
| 8424 | * |
| 8425 | * Ensures that ns-references point to @destDoc: either to |
| 8426 | * elements->nsDef entries if @destParent is given, or to |
| 8427 | * @destDoc->oldNs otherwise. |
| 8428 | * If @destParent is given, it ensures that the tree is namespace |
| 8429 | * wellformed by creating additional ns-decls where needed. |
| 8430 | * Note that, since prefixes of already existent ns-decls can be |
| 8431 | * shadowed by this process, it could break QNames in attribute |
| 8432 | * values or element content. |
| 8433 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8434 | * NOTE: This function was not intensively tested. |
| 8435 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8436 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8437 | */ |
| 8438 | static int |
| 8439 | xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt, |
| 8440 | xmlDocPtr sourceDoc, |
| 8441 | xmlNodePtr node, |
| 8442 | xmlDocPtr destDoc, |
| 8443 | xmlNodePtr destParent, |
| 8444 | int options ATTRIBUTE_UNUSED) |
| 8445 | { |
| 8446 | int ret = 0; |
| 8447 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8448 | xmlNsMapPtr nsMap = NULL; |
| 8449 | xmlNsMapItemPtr mi; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 8450 | xmlNsPtr ns = NULL; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8451 | int depth = -1, adoptStr = 1; |
| 8452 | /* gather @parent's ns-decls. */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8453 | int parnsdone; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8454 | /* @ancestorsOnly should be set per option. */ |
| 8455 | int ancestorsOnly = 0; |
| 8456 | |
| 8457 | /* |
| 8458 | * Optimize string adoption for equal or none dicts. |
| 8459 | */ |
| 8460 | if ((sourceDoc != NULL) && |
| 8461 | (sourceDoc->dict == destDoc->dict)) |
| 8462 | adoptStr = 0; |
| 8463 | else |
| 8464 | adoptStr = 1; |
| 8465 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8466 | /* |
| 8467 | * Get the ns-map from the context if available. |
| 8468 | */ |
| 8469 | if (ctxt) |
| 8470 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 8471 | /* |
| 8472 | * Disable search for ns-decls in the parent-axis of the |
| 8473 | * desination element, if: |
| 8474 | * 1) there's no destination parent |
| 8475 | * 2) custom ns-reference handling is used |
| 8476 | */ |
| 8477 | if ((destParent == NULL) || |
| 8478 | (ctxt && ctxt->getNsForNodeFunc)) |
| 8479 | { |
| 8480 | parnsdone = 1; |
| 8481 | } else |
| 8482 | parnsdone = 0; |
| 8483 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8484 | cur = node; |
| 8485 | while (cur != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8486 | /* |
| 8487 | * Paranoid source-doc sanity check. |
| 8488 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8489 | if (cur->doc != sourceDoc) { |
| 8490 | /* |
| 8491 | * We'll assume XIncluded nodes if the doc differs. |
| 8492 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 8493 | * This here skips XIncluded nodes and tries to handle |
| 8494 | * broken sequences. |
| 8495 | */ |
| 8496 | if (cur->next == NULL) |
| 8497 | goto leave_node; |
| 8498 | do { |
| 8499 | cur = cur->next; |
| 8500 | if ((cur->type == XML_XINCLUDE_END) || |
| 8501 | (cur->doc == node->doc)) |
| 8502 | break; |
| 8503 | } while (cur->next != NULL); |
| 8504 | |
| 8505 | if (cur->doc != node->doc) |
| 8506 | goto leave_node; |
| 8507 | } |
| 8508 | cur->doc = destDoc; |
| 8509 | switch (cur->type) { |
| 8510 | case XML_XINCLUDE_START: |
| 8511 | case XML_XINCLUDE_END: |
| 8512 | /* |
| 8513 | * TODO |
| 8514 | */ |
| 8515 | return (-1); |
| 8516 | case XML_ELEMENT_NODE: |
| 8517 | curElem = cur; |
| 8518 | depth++; |
| 8519 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8520 | * Namespace declarations. |
| 8521 | * - ns->href and ns->prefix are never in the dict, so |
| 8522 | * we need not move the values over to the destination dict. |
| 8523 | * - Note that for custom handling of ns-references, |
| 8524 | * the ns-decls need not be stored in the ns-map, |
| 8525 | * since they won't be referenced by node->ns. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8526 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8527 | if ((cur->nsDef) && |
| 8528 | ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL))) |
| 8529 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8530 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8531 | /* |
| 8532 | * Gather @parent's in-scope ns-decls. |
| 8533 | */ |
| 8534 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8535 | destParent) == -1) |
| 8536 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8537 | parnsdone = 1; |
| 8538 | } |
| 8539 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8540 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8541 | * NOTE: ns->prefix and ns->href are never in the dict. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8542 | * XML_TREE_ADOPT_STR(ns->prefix) |
| 8543 | * XML_TREE_ADOPT_STR(ns->href) |
| 8544 | */ |
| 8545 | /* |
| 8546 | * Does it shadow any ns-decl? |
| 8547 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8548 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8549 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8550 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8551 | (mi->shadowDepth == -1) && |
| 8552 | ((ns->prefix == mi->newNs->prefix) || |
| 8553 | xmlStrEqual(ns->prefix, |
| 8554 | mi->newNs->prefix))) { |
| 8555 | |
| 8556 | mi->shadowDepth = depth; |
| 8557 | } |
| 8558 | } |
| 8559 | } |
| 8560 | /* |
| 8561 | * Push mapping. |
| 8562 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8563 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8564 | ns, ns, depth) == NULL) |
| 8565 | goto internal_error; |
| 8566 | } |
| 8567 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8568 | /* No break on purpose. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8569 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8570 | /* No namespace, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8571 | if (cur->ns == NULL) |
| 8572 | goto ns_end; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8573 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8574 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8575 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8576 | destParent) == -1) |
| 8577 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8578 | parnsdone = 1; |
| 8579 | } |
| 8580 | /* |
| 8581 | * Adopt ns-references. |
| 8582 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8583 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8584 | /* |
| 8585 | * Search for a mapping. |
| 8586 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8587 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8588 | if ((mi->shadowDepth == -1) && |
| 8589 | (cur->ns == mi->oldNs)) { |
| 8590 | |
| 8591 | cur->ns = mi->newNs; |
| 8592 | goto ns_end; |
| 8593 | } |
| 8594 | } |
| 8595 | } |
| 8596 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8597 | * No matching namespace in scope. We need a new one. |
| 8598 | */ |
| 8599 | if ((ctxt) && (ctxt->getNsForNodeFunc)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8600 | /* |
| 8601 | * User-defined behaviour. |
| 8602 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8603 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 8604 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8605 | /* |
| 8606 | * Insert mapping if ns is available; it's the users fault |
| 8607 | * if not. |
| 8608 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8609 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8610 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8611 | goto internal_error; |
| 8612 | cur->ns = ns; |
| 8613 | } else { |
| 8614 | /* |
| 8615 | * Aquire a normalized ns-decl and add it to the map. |
| 8616 | */ |
| 8617 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
| 8618 | /* ns-decls on curElem or on destDoc->oldNs */ |
| 8619 | destParent ? curElem : NULL, |
| 8620 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8621 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8622 | ancestorsOnly, |
| 8623 | /* ns-decls must be prefixed for attributes. */ |
| 8624 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8625 | goto internal_error; |
| 8626 | cur->ns = ns; |
| 8627 | } |
| 8628 | ns_end: |
| 8629 | /* |
| 8630 | * Further node properties. |
| 8631 | * TODO: Is this all? |
| 8632 | */ |
| 8633 | XML_TREE_ADOPT_STR(cur->name) |
| 8634 | if (cur->type == XML_ELEMENT_NODE) { |
| 8635 | cur->psvi = NULL; |
| 8636 | cur->line = 0; |
| 8637 | cur->extra = 0; |
| 8638 | /* |
| 8639 | * Walk attributes. |
| 8640 | */ |
| 8641 | if (cur->properties != NULL) { |
| 8642 | /* |
| 8643 | * Process first attribute node. |
| 8644 | */ |
| 8645 | cur = (xmlNodePtr) cur->properties; |
| 8646 | continue; |
| 8647 | } |
| 8648 | } else { |
| 8649 | /* |
| 8650 | * Attributes. |
| 8651 | */ |
| 8652 | if ((sourceDoc != NULL) && |
| 8653 | (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID)) |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8654 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8655 | xmlRemoveID(sourceDoc, (xmlAttrPtr) cur); |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8656 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8657 | ((xmlAttrPtr) cur)->atype = 0; |
| 8658 | ((xmlAttrPtr) cur)->psvi = NULL; |
| 8659 | } |
| 8660 | break; |
| 8661 | case XML_TEXT_NODE: |
| 8662 | case XML_CDATA_SECTION_NODE: |
| 8663 | /* |
| 8664 | * This puts the content in the dest dict, only if |
| 8665 | * it was previously in the source dict. |
| 8666 | */ |
| 8667 | XML_TREE_ADOPT_STR_2(cur->content) |
| 8668 | goto leave_node; |
| 8669 | case XML_ENTITY_REF_NODE: |
| 8670 | /* |
| 8671 | * Remove reference to the entitity-node. |
| 8672 | */ |
| 8673 | cur->content = NULL; |
| 8674 | cur->children = NULL; |
| 8675 | cur->last = NULL; |
| 8676 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 8677 | xmlEntityPtr ent; |
| 8678 | /* |
| 8679 | * Assign new entity-node if available. |
| 8680 | */ |
| 8681 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 8682 | if (ent != NULL) { |
| 8683 | cur->content = ent->content; |
| 8684 | cur->children = (xmlNodePtr) ent; |
| 8685 | cur->last = (xmlNodePtr) ent; |
| 8686 | } |
| 8687 | } |
| 8688 | goto leave_node; |
| 8689 | case XML_PI_NODE: |
| 8690 | XML_TREE_ADOPT_STR(cur->name) |
| 8691 | XML_TREE_ADOPT_STR_2(cur->content) |
| 8692 | break; |
| 8693 | case XML_COMMENT_NODE: |
| 8694 | break; |
| 8695 | default: |
| 8696 | goto internal_error; |
| 8697 | } |
| 8698 | /* |
| 8699 | * Walk the tree. |
| 8700 | */ |
| 8701 | if (cur->children != NULL) { |
| 8702 | cur = cur->children; |
| 8703 | continue; |
| 8704 | } |
| 8705 | |
| 8706 | leave_node: |
| 8707 | if (cur == node) |
| 8708 | break; |
| 8709 | if ((cur->type == XML_ELEMENT_NODE) || |
| 8710 | (cur->type == XML_XINCLUDE_START) || |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8711 | (cur->type == XML_XINCLUDE_END)) |
| 8712 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8713 | /* |
| 8714 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 8715 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8716 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8717 | /* |
| 8718 | * Pop mappings. |
| 8719 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8720 | while ((nsMap->last != NULL) && |
| 8721 | (nsMap->last->depth >= depth)) |
| 8722 | { |
| 8723 | XML_NSMAP_POP(nsMap, mi) |
| 8724 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8725 | /* |
| 8726 | * Unshadow. |
| 8727 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8728 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8729 | if (mi->shadowDepth >= depth) |
| 8730 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8731 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8732 | } |
| 8733 | depth--; |
| 8734 | } |
| 8735 | if (cur->next != NULL) |
| 8736 | cur = cur->next; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8737 | else if ((cur->type == XML_ATTRIBUTE_NODE) && |
| 8738 | (cur->parent->children != NULL)) |
| 8739 | { |
| 8740 | cur = cur->parent->children; |
| 8741 | } else { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8742 | cur = cur->parent; |
| 8743 | goto leave_node; |
| 8744 | } |
| 8745 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8746 | |
| 8747 | goto exit; |
| 8748 | |
| 8749 | internal_error: |
| 8750 | ret = -1; |
| 8751 | |
| 8752 | exit: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8753 | /* |
| 8754 | * Cleanup. |
| 8755 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8756 | if (nsMap != NULL) { |
| 8757 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 8758 | /* |
| 8759 | * Just cleanup the map but don't free. |
| 8760 | */ |
| 8761 | if (nsMap->first) { |
| 8762 | if (nsMap->pool) |
| 8763 | nsMap->last->next = nsMap->pool; |
| 8764 | nsMap->pool = nsMap->first; |
| 8765 | nsMap->first = NULL; |
| 8766 | } |
| 8767 | } else |
| 8768 | xmlDOMWrapNsMapFree(nsMap); |
| 8769 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8770 | return(ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8771 | } |
| 8772 | |
| 8773 | /* |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8774 | * xmlDOMWrapCloneNode: |
| 8775 | * @ctxt: the optional context for custom processing |
| 8776 | * @sourceDoc: the optional sourceDoc |
| 8777 | * @node: the node to start with |
| 8778 | * @resNode: the clone of the given @node |
| 8779 | * @destDoc: the destination doc |
| 8780 | * @destParent: the optional new parent of @node in @destDoc |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 8781 | * @deep: descend into child if set |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8782 | * @options: option flags |
| 8783 | * |
| 8784 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 8785 | * 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] | 8786 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used. |
| 8787 | * This is the case when you don't know already where the cloned branch |
| 8788 | * will be added to. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8789 | * |
| 8790 | * If @destParent is given, it ensures that the tree is namespace |
| 8791 | * wellformed by creating additional ns-decls where needed. |
| 8792 | * Note that, since prefixes of already existent ns-decls can be |
| 8793 | * shadowed by this process, it could break QNames in attribute |
| 8794 | * values or element content. |
| 8795 | * TODO: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8796 | * 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] | 8797 | * |
| 8798 | * Returns 0 if the operation succeeded, |
| 8799 | * 1 if a node of unsupported (or not yet supported) type was given, |
| 8800 | * -1 on API/internal errors. |
| 8801 | */ |
| 8802 | |
| 8803 | int |
| 8804 | xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt, |
| 8805 | xmlDocPtr sourceDoc, |
| 8806 | xmlNodePtr node, |
| 8807 | xmlNodePtr *resNode, |
| 8808 | xmlDocPtr destDoc, |
| 8809 | xmlNodePtr destParent, |
| 8810 | int deep, |
| 8811 | int options ATTRIBUTE_UNUSED) |
| 8812 | { |
| 8813 | int ret = 0; |
| 8814 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8815 | xmlNsMapPtr nsMap = NULL; |
| 8816 | xmlNsMapItemPtr mi; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8817 | xmlNsPtr ns; |
| 8818 | int depth = -1; |
| 8819 | /* int adoptStr = 1; */ |
| 8820 | /* gather @parent's ns-decls. */ |
| 8821 | int parnsdone = 0; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8822 | /* |
| 8823 | * @ancestorsOnly: |
| 8824 | * TODO: @ancestorsOnly should be set per option. |
| 8825 | * |
| 8826 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8827 | int ancestorsOnly = 0; |
| 8828 | xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8829 | xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL; |
| 8830 | xmlDictPtr dict; /* The destination dict */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8831 | |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 8832 | if ((node == NULL) || (resNode == NULL) || (destDoc == NULL)) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8833 | return(-1); |
| 8834 | /* |
| 8835 | * TODO: Initially we support only element-nodes. |
| 8836 | */ |
| 8837 | if (node->type != XML_ELEMENT_NODE) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8838 | return(1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8839 | /* |
| 8840 | * Check node->doc sanity. |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8841 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8842 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 8843 | (node->doc != sourceDoc)) { |
| 8844 | /* |
| 8845 | * Might be an XIncluded node. |
| 8846 | */ |
| 8847 | return (-1); |
| 8848 | } |
| 8849 | if (sourceDoc == NULL) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8850 | sourceDoc = node->doc; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 8851 | if (sourceDoc == NULL) |
| 8852 | return (-1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8853 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8854 | dict = destDoc->dict; |
| 8855 | /* |
| 8856 | * Reuse the namespace map of the context. |
| 8857 | */ |
| 8858 | if (ctxt) |
| 8859 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 8860 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8861 | *resNode = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8862 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8863 | cur = node; |
| 8864 | while (cur != NULL) { |
| 8865 | if (cur->doc != sourceDoc) { |
| 8866 | /* |
| 8867 | * We'll assume XIncluded nodes if the doc differs. |
| 8868 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 8869 | * TODO: This here returns -1 in this case. |
| 8870 | */ |
| 8871 | goto internal_error; |
| 8872 | } |
| 8873 | /* |
| 8874 | * Create a new node. |
| 8875 | */ |
| 8876 | switch (cur->type) { |
| 8877 | case XML_XINCLUDE_START: |
| 8878 | case XML_XINCLUDE_END: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8879 | /* |
| 8880 | * TODO: What to do with XInclude? |
| 8881 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8882 | goto internal_error; |
| 8883 | break; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8884 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8885 | case XML_TEXT_NODE: |
| 8886 | case XML_CDATA_SECTION_NODE: |
| 8887 | case XML_COMMENT_NODE: |
| 8888 | case XML_PI_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8889 | case XML_DOCUMENT_FRAG_NODE: |
| 8890 | case XML_ENTITY_REF_NODE: |
| 8891 | case XML_ENTITY_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8892 | /* |
| 8893 | * Nodes of xmlNode structure. |
| 8894 | */ |
| 8895 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 8896 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8897 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8898 | goto internal_error; |
| 8899 | } |
| 8900 | memset(clone, 0, sizeof(xmlNode)); |
| 8901 | /* |
| 8902 | * Set hierachical links. |
| 8903 | */ |
| 8904 | if (resultClone != NULL) { |
| 8905 | clone->parent = parentClone; |
| 8906 | if (prevClone) { |
| 8907 | prevClone->next = clone; |
| 8908 | clone->prev = prevClone; |
| 8909 | } else |
| 8910 | parentClone->children = clone; |
| 8911 | } else |
| 8912 | resultClone = clone; |
| 8913 | |
| 8914 | break; |
| 8915 | case XML_ATTRIBUTE_NODE: |
| 8916 | /* |
| 8917 | * Attributes (xmlAttr). |
| 8918 | */ |
| 8919 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr)); |
| 8920 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8921 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8922 | goto internal_error; |
| 8923 | } |
| 8924 | memset(clone, 0, sizeof(xmlAttr)); |
| 8925 | /* |
| 8926 | * Set hierachical links. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8927 | * TODO: Change this to add to the end of attributes. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8928 | */ |
| 8929 | if (resultClone != NULL) { |
| 8930 | clone->parent = parentClone; |
| 8931 | if (prevClone) { |
| 8932 | prevClone->next = clone; |
| 8933 | clone->prev = prevClone; |
| 8934 | } else |
| 8935 | parentClone->properties = (xmlAttrPtr) clone; |
| 8936 | } else |
| 8937 | resultClone = clone; |
| 8938 | break; |
| 8939 | default: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8940 | /* |
| 8941 | * TODO QUESTION: Any other nodes expected? |
| 8942 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8943 | goto internal_error; |
| 8944 | } |
| 8945 | |
| 8946 | clone->type = cur->type; |
| 8947 | clone->doc = destDoc; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8948 | |
| 8949 | /* |
| 8950 | * Clone the name of the node if any. |
| 8951 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8952 | if (cur->name == xmlStringText) |
| 8953 | clone->name = xmlStringText; |
| 8954 | else if (cur->name == xmlStringTextNoenc) |
| 8955 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8956 | * NOTE: Although xmlStringTextNoenc is never assigned to a node |
| 8957 | * in tree.c, it might be set in Libxslt via |
| 8958 | * "xsl:disable-output-escaping". |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8959 | */ |
| 8960 | clone->name = xmlStringTextNoenc; |
| 8961 | else if (cur->name == xmlStringComment) |
| 8962 | clone->name = xmlStringComment; |
| 8963 | else if (cur->name != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8964 | DICT_CONST_COPY(cur->name, clone->name); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8965 | } |
| 8966 | |
| 8967 | switch (cur->type) { |
| 8968 | case XML_XINCLUDE_START: |
| 8969 | case XML_XINCLUDE_END: |
| 8970 | /* |
| 8971 | * TODO |
| 8972 | */ |
| 8973 | return (-1); |
| 8974 | case XML_ELEMENT_NODE: |
| 8975 | curElem = cur; |
| 8976 | depth++; |
| 8977 | /* |
| 8978 | * Namespace declarations. |
| 8979 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8980 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8981 | if (! parnsdone) { |
| 8982 | if (destParent && (ctxt == NULL)) { |
| 8983 | /* |
| 8984 | * Gather @parent's in-scope ns-decls. |
| 8985 | */ |
| 8986 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8987 | destParent) == -1) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8988 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 8989 | } |
| 8990 | parnsdone = 1; |
| 8991 | } |
| 8992 | /* |
| 8993 | * Clone namespace declarations. |
| 8994 | */ |
| 8995 | cloneNsDefSlot = &(clone->nsDef); |
| 8996 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8997 | /* |
| 8998 | * Create a new xmlNs. |
| 8999 | */ |
| 9000 | cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 9001 | if (cloneNs == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9002 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): " |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9003 | "allocating namespace"); |
| 9004 | return(-1); |
| 9005 | } |
| 9006 | memset(cloneNs, 0, sizeof(xmlNs)); |
| 9007 | cloneNs->type = XML_LOCAL_NAMESPACE; |
| 9008 | |
| 9009 | if (ns->href != NULL) |
| 9010 | cloneNs->href = xmlStrdup(ns->href); |
| 9011 | if (ns->prefix != NULL) |
| 9012 | cloneNs->prefix = xmlStrdup(ns->prefix); |
| 9013 | |
| 9014 | *cloneNsDefSlot = cloneNs; |
| 9015 | cloneNsDefSlot = &(cloneNs->next); |
| 9016 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9017 | /* |
| 9018 | * Note that for custom handling of ns-references, |
| 9019 | * the ns-decls need not be stored in the ns-map, |
| 9020 | * since they won't be referenced by node->ns. |
| 9021 | */ |
| 9022 | if ((ctxt == NULL) || |
| 9023 | (ctxt->getNsForNodeFunc == NULL)) |
| 9024 | { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9025 | /* |
| 9026 | * Does it shadow any ns-decl? |
| 9027 | */ |
| 9028 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
| 9029 | XML_NSMAP_FOREACH(nsMap, mi) { |
| 9030 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 9031 | (mi->shadowDepth == -1) && |
| 9032 | ((ns->prefix == mi->newNs->prefix) || |
| 9033 | xmlStrEqual(ns->prefix, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9034 | mi->newNs->prefix))) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9035 | /* |
| 9036 | * Mark as shadowed at the current |
| 9037 | * depth. |
| 9038 | */ |
| 9039 | mi->shadowDepth = depth; |
| 9040 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9041 | } |
| 9042 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9043 | /* |
| 9044 | * Push mapping. |
| 9045 | */ |
| 9046 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
| 9047 | ns, cloneNs, depth) == NULL) |
| 9048 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9049 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9050 | } |
| 9051 | } |
| 9052 | /* cur->ns will be processed further down. */ |
| 9053 | break; |
| 9054 | case XML_ATTRIBUTE_NODE: |
| 9055 | /* IDs will be processed further down. */ |
| 9056 | /* cur->ns will be processed further down. */ |
| 9057 | break; |
| 9058 | case XML_TEXT_NODE: |
| 9059 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9060 | /* |
| 9061 | * Note that this will also cover the values of attributes. |
| 9062 | */ |
| 9063 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9064 | goto leave_node; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9065 | case XML_ENTITY_NODE: |
| 9066 | /* TODO: What to do here? */ |
| 9067 | goto leave_node; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9068 | case XML_ENTITY_REF_NODE: |
| 9069 | if (sourceDoc != destDoc) { |
| 9070 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9071 | xmlEntityPtr ent; |
| 9072 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9073 | * Different doc: Assign new entity-node if available. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9074 | */ |
| 9075 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9076 | if (ent != NULL) { |
| 9077 | clone->content = ent->content; |
| 9078 | clone->children = (xmlNodePtr) ent; |
| 9079 | clone->last = (xmlNodePtr) ent; |
| 9080 | } |
| 9081 | } |
| 9082 | } else { |
| 9083 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9084 | * Same doc: Use the current node's entity declaration |
| 9085 | * and value. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9086 | */ |
| 9087 | clone->content = cur->content; |
| 9088 | clone->children = cur->children; |
| 9089 | clone->last = cur->last; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9090 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9091 | goto leave_node; |
| 9092 | case XML_PI_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9093 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9094 | goto leave_node; |
| 9095 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9096 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9097 | goto leave_node; |
| 9098 | default: |
| 9099 | goto internal_error; |
| 9100 | } |
| 9101 | |
| 9102 | if (cur->ns == NULL) |
| 9103 | goto end_ns_reference; |
| 9104 | |
| 9105 | /* handle_ns_reference: */ |
| 9106 | /* |
| 9107 | ** The following will take care of references to ns-decls ******** |
| 9108 | ** and is intended only for element- and attribute-nodes. |
| 9109 | ** |
| 9110 | */ |
| 9111 | if (! parnsdone) { |
| 9112 | if (destParent && (ctxt == NULL)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9113 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1) |
| 9114 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9115 | } |
| 9116 | parnsdone = 1; |
| 9117 | } |
| 9118 | /* |
| 9119 | * Adopt ns-references. |
| 9120 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9121 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9122 | /* |
| 9123 | * Search for a mapping. |
| 9124 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9125 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9126 | if ((mi->shadowDepth == -1) && |
| 9127 | (cur->ns == mi->oldNs)) { |
| 9128 | /* |
| 9129 | * This is the nice case: a mapping was found. |
| 9130 | */ |
| 9131 | clone->ns = mi->newNs; |
| 9132 | goto end_ns_reference; |
| 9133 | } |
| 9134 | } |
| 9135 | } |
| 9136 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9137 | * No matching namespace in scope. We need a new one. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9138 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9139 | if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9140 | /* |
| 9141 | * User-defined behaviour. |
| 9142 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9143 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 9144 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9145 | /* |
| 9146 | * Add user's mapping. |
| 9147 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9148 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9149 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
| 9150 | goto internal_error; |
| 9151 | clone->ns = ns; |
| 9152 | } else { |
| 9153 | /* |
| 9154 | * Aquire a normalized ns-decl and add it to the map. |
| 9155 | */ |
| 9156 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
| 9157 | /* ns-decls on curElem or on destDoc->oldNs */ |
| 9158 | destParent ? curElem : NULL, |
| 9159 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9160 | &nsMap, depth, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9161 | /* if we need to search only in the ancestor-axis */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9162 | ancestorsOnly, |
| 9163 | /* ns-decls must be prefixed for attributes. */ |
| 9164 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 9165 | goto internal_error; |
| 9166 | clone->ns = ns; |
| 9167 | } |
| 9168 | |
| 9169 | end_ns_reference: |
| 9170 | |
| 9171 | /* |
| 9172 | * Some post-processing. |
| 9173 | * |
| 9174 | * Handle ID attributes. |
| 9175 | */ |
| 9176 | if ((clone->type == XML_ATTRIBUTE_NODE) && |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9177 | (clone->parent != NULL)) |
| 9178 | { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9179 | if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) { |
| 9180 | |
| 9181 | xmlChar *idVal; |
| 9182 | |
| 9183 | idVal = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 9184 | if (idVal != NULL) { |
| 9185 | if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) { |
| 9186 | /* TODO: error message. */ |
| 9187 | xmlFree(idVal); |
| 9188 | goto internal_error; |
| 9189 | } |
| 9190 | xmlFree(idVal); |
| 9191 | } |
| 9192 | } |
| 9193 | } |
| 9194 | /* |
| 9195 | ** |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9196 | ** The following will traverse the tree ************************** |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9197 | ** |
| 9198 | * |
| 9199 | * Walk the element's attributes before descending into child-nodes. |
| 9200 | */ |
| 9201 | if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) { |
| 9202 | prevClone = NULL; |
| 9203 | parentClone = clone; |
| 9204 | cur = (xmlNodePtr) cur->properties; |
| 9205 | continue; |
| 9206 | } |
| 9207 | into_content: |
| 9208 | /* |
| 9209 | * Descend into child-nodes. |
| 9210 | */ |
| 9211 | if (cur->children != NULL) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9212 | if (deep || (cur->type == XML_ATTRIBUTE_NODE)) { |
| 9213 | prevClone = NULL; |
| 9214 | parentClone = clone; |
| 9215 | cur = cur->children; |
| 9216 | continue; |
| 9217 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9218 | } |
| 9219 | |
| 9220 | leave_node: |
| 9221 | /* |
| 9222 | * At this point we are done with the node, its content |
| 9223 | * and an element-nodes's attribute-nodes. |
| 9224 | */ |
| 9225 | if (cur == node) |
| 9226 | break; |
| 9227 | if ((cur->type == XML_ELEMENT_NODE) || |
| 9228 | (cur->type == XML_XINCLUDE_START) || |
| 9229 | (cur->type == XML_XINCLUDE_END)) { |
| 9230 | /* |
| 9231 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 9232 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9233 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9234 | /* |
| 9235 | * Pop mappings. |
| 9236 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9237 | while ((nsMap->last != NULL) && |
| 9238 | (nsMap->last->depth >= depth)) |
| 9239 | { |
| 9240 | XML_NSMAP_POP(nsMap, mi) |
| 9241 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9242 | /* |
| 9243 | * Unshadow. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9244 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9245 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9246 | if (mi->shadowDepth >= depth) |
| 9247 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9248 | } |
| 9249 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9250 | depth--; |
| 9251 | } |
| 9252 | if (cur->next != NULL) { |
| 9253 | prevClone = clone; |
| 9254 | cur = cur->next; |
| 9255 | } else if (cur->type != XML_ATTRIBUTE_NODE) { |
| 9256 | /* |
| 9257 | * Set clone->last. |
| 9258 | */ |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9259 | if (clone->parent != NULL) |
| 9260 | clone->parent->last = clone; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9261 | clone = clone->parent; |
| 9262 | parentClone = clone->parent; |
| 9263 | /* |
| 9264 | * Process parent --> next; |
| 9265 | */ |
| 9266 | cur = cur->parent; |
| 9267 | goto leave_node; |
| 9268 | } else { |
| 9269 | /* This is for attributes only. */ |
| 9270 | clone = clone->parent; |
| 9271 | parentClone = clone->parent; |
| 9272 | /* |
| 9273 | * Process parent-element --> children. |
| 9274 | */ |
| 9275 | cur = cur->parent; |
| 9276 | goto into_content; |
| 9277 | } |
| 9278 | } |
| 9279 | goto exit; |
| 9280 | |
| 9281 | internal_error: |
| 9282 | ret = -1; |
| 9283 | |
| 9284 | exit: |
| 9285 | /* |
| 9286 | * Cleanup. |
| 9287 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9288 | if (nsMap != NULL) { |
| 9289 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 9290 | /* |
| 9291 | * Just cleanup the map but don't free. |
| 9292 | */ |
| 9293 | if (nsMap->first) { |
| 9294 | if (nsMap->pool) |
| 9295 | nsMap->last->next = nsMap->pool; |
| 9296 | nsMap->pool = nsMap->first; |
| 9297 | nsMap->first = NULL; |
| 9298 | } |
| 9299 | } else |
| 9300 | xmlDOMWrapNsMapFree(nsMap); |
| 9301 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9302 | /* |
| 9303 | * TODO: Should we try a cleanup of the cloned node in case of a |
| 9304 | * fatal error? |
| 9305 | */ |
| 9306 | *resNode = resultClone; |
| 9307 | return (ret); |
| 9308 | } |
| 9309 | |
| 9310 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9311 | * xmlDOMWrapAdoptAttr: |
| 9312 | * @ctxt: the optional context for custom processing |
| 9313 | * @sourceDoc: the optional source document of attr |
| 9314 | * @attr: the attribute-node to be adopted |
| 9315 | * @destDoc: the destination doc for adoption |
| 9316 | * @destParent: the optional new parent of @attr in @destDoc |
| 9317 | * @options: option flags |
| 9318 | * |
| 9319 | * @attr is adopted by @destDoc. |
| 9320 | * Ensures that ns-references point to @destDoc: either to |
| 9321 | * elements->nsDef entries if @destParent is given, or to |
| 9322 | * @destDoc->oldNs otherwise. |
| 9323 | * |
| 9324 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 9325 | */ |
| 9326 | static int |
| 9327 | xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt, |
| 9328 | xmlDocPtr sourceDoc, |
| 9329 | xmlAttrPtr attr, |
| 9330 | xmlDocPtr destDoc, |
| 9331 | xmlNodePtr destParent, |
| 9332 | int options ATTRIBUTE_UNUSED) |
| 9333 | { |
| 9334 | xmlNodePtr cur; |
| 9335 | int adoptStr = 1; |
| 9336 | |
| 9337 | if ((attr == NULL) || (destDoc == NULL)) |
| 9338 | return (-1); |
| 9339 | |
| 9340 | attr->doc = destDoc; |
| 9341 | if (attr->ns != NULL) { |
| 9342 | xmlNsPtr ns = NULL; |
| 9343 | |
| 9344 | if (ctxt != NULL) { |
| 9345 | /* TODO: User defined. */ |
| 9346 | } |
| 9347 | /* XML Namespace. */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9348 | if (IS_STR_XML(attr->ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9349 | ns = xmlTreeEnsureXMLDecl(destDoc); |
| 9350 | } else if (destParent == NULL) { |
| 9351 | /* |
| 9352 | * Store in @destDoc->oldNs. |
| 9353 | */ |
| 9354 | ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix); |
| 9355 | } else { |
| 9356 | /* |
| 9357 | * Declare on @destParent. |
| 9358 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9359 | if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9360 | &ns, 1) == -1) |
| 9361 | goto internal_error; |
| 9362 | if (ns == NULL) { |
| 9363 | ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent, |
| 9364 | attr->ns->href, attr->ns->prefix, 1); |
| 9365 | } |
| 9366 | } |
| 9367 | if (ns == NULL) |
| 9368 | goto internal_error; |
| 9369 | attr->ns = ns; |
| 9370 | } |
| 9371 | |
| 9372 | XML_TREE_ADOPT_STR(attr->name); |
| 9373 | attr->atype = 0; |
| 9374 | attr->psvi = NULL; |
| 9375 | /* |
| 9376 | * Walk content. |
| 9377 | */ |
| 9378 | if (attr->children == NULL) |
| 9379 | return (0); |
| 9380 | cur = attr->children; |
| 9381 | while (cur != NULL) { |
| 9382 | cur->doc = destDoc; |
| 9383 | switch (cur->type) { |
| 9384 | case XML_TEXT_NODE: |
| 9385 | case XML_CDATA_SECTION_NODE: |
| 9386 | XML_TREE_ADOPT_STR_2(cur->content) |
| 9387 | break; |
| 9388 | case XML_ENTITY_REF_NODE: |
| 9389 | /* |
| 9390 | * Remove reference to the entitity-node. |
| 9391 | */ |
| 9392 | cur->content = NULL; |
| 9393 | cur->children = NULL; |
| 9394 | cur->last = NULL; |
| 9395 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9396 | xmlEntityPtr ent; |
| 9397 | /* |
| 9398 | * Assign new entity-node if available. |
| 9399 | */ |
| 9400 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9401 | if (ent != NULL) { |
| 9402 | cur->content = ent->content; |
| 9403 | cur->children = (xmlNodePtr) ent; |
| 9404 | cur->last = (xmlNodePtr) ent; |
| 9405 | } |
| 9406 | } |
| 9407 | break; |
| 9408 | default: |
| 9409 | break; |
| 9410 | } |
| 9411 | if (cur->children != NULL) { |
| 9412 | cur = cur->children; |
| 9413 | continue; |
| 9414 | } |
| 9415 | next_sibling: |
| 9416 | if (cur == (xmlNodePtr) attr) |
| 9417 | break; |
| 9418 | if (cur->next != NULL) |
| 9419 | cur = cur->next; |
| 9420 | else { |
| 9421 | cur = cur->parent; |
| 9422 | goto next_sibling; |
| 9423 | } |
| 9424 | } |
| 9425 | return (0); |
| 9426 | internal_error: |
| 9427 | return (-1); |
| 9428 | } |
| 9429 | |
| 9430 | /* |
| 9431 | * xmlDOMWrapAdoptNode: |
| 9432 | * @ctxt: the optional context for custom processing |
| 9433 | * @sourceDoc: the optional sourceDoc |
| 9434 | * @node: the node to start with |
| 9435 | * @destDoc: the destination doc |
Kasimier T. Buchcik | 4d9c948 | 2005-06-27 15:04:46 +0000 | [diff] [blame] | 9436 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9437 | * @options: option flags |
| 9438 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9439 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 9440 | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
| 9441 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used |
| 9442 | * This is the case when you have an unliked node and just want to move it |
| 9443 | * to the context of |
| 9444 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9445 | * If @destParent is given, it ensures that the tree is namespace |
| 9446 | * wellformed by creating additional ns-decls where needed. |
| 9447 | * Note that, since prefixes of already existent ns-decls can be |
| 9448 | * shadowed by this process, it could break QNames in attribute |
| 9449 | * values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9450 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9451 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9452 | * Returns 0 if the operation succeeded, |
| 9453 | * 1 if a node of unsupported type was given, |
| 9454 | * 2 if a node of not yet supported type was given and |
| 9455 | * -1 on API/internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9456 | */ |
| 9457 | int |
| 9458 | xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt, |
| 9459 | xmlDocPtr sourceDoc, |
| 9460 | xmlNodePtr node, |
| 9461 | xmlDocPtr destDoc, |
| 9462 | xmlNodePtr destParent, |
| 9463 | int options) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9464 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9465 | if ((node == NULL) || (destDoc == NULL) || |
| 9466 | ((destParent != NULL) && (destParent->doc != destDoc))) |
| 9467 | return(-1); |
| 9468 | /* |
| 9469 | * Check node->doc sanity. |
| 9470 | */ |
| 9471 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 9472 | (node->doc != sourceDoc)) { |
| 9473 | /* |
| 9474 | * Might be an XIncluded node. |
| 9475 | */ |
| 9476 | return (-1); |
| 9477 | } |
| 9478 | if (sourceDoc == NULL) |
| 9479 | sourceDoc = node->doc; |
| 9480 | if (sourceDoc == destDoc) |
| 9481 | return (-1); |
| 9482 | switch (node->type) { |
| 9483 | case XML_ELEMENT_NODE: |
| 9484 | case XML_ATTRIBUTE_NODE: |
| 9485 | case XML_TEXT_NODE: |
| 9486 | case XML_CDATA_SECTION_NODE: |
| 9487 | case XML_ENTITY_REF_NODE: |
| 9488 | case XML_PI_NODE: |
| 9489 | case XML_COMMENT_NODE: |
| 9490 | break; |
| 9491 | case XML_DOCUMENT_FRAG_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9492 | /* TODO: Support document-fragment-nodes. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9493 | return (2); |
| 9494 | default: |
| 9495 | return (1); |
| 9496 | } |
| 9497 | /* |
| 9498 | * Unlink only if @node was not already added to @destParent. |
| 9499 | */ |
| 9500 | if ((node->parent != NULL) && (destParent != node->parent)) |
| 9501 | xmlUnlinkNode(node); |
| 9502 | |
| 9503 | if (node->type == XML_ELEMENT_NODE) { |
| 9504 | return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node, |
| 9505 | destDoc, destParent, options)); |
| 9506 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
| 9507 | return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc, |
| 9508 | (xmlAttrPtr) node, destDoc, destParent, options)); |
| 9509 | } else { |
| 9510 | xmlNodePtr cur = node; |
| 9511 | int adoptStr = 1; |
| 9512 | |
| 9513 | cur->doc = destDoc; |
| 9514 | /* |
| 9515 | * Optimize string adoption. |
| 9516 | */ |
| 9517 | if ((sourceDoc != NULL) && |
| 9518 | (sourceDoc->dict == destDoc->dict)) |
| 9519 | adoptStr = 0; |
| 9520 | switch (node->type) { |
| 9521 | case XML_TEXT_NODE: |
| 9522 | case XML_CDATA_SECTION_NODE: |
| 9523 | XML_TREE_ADOPT_STR_2(node->content) |
| 9524 | break; |
| 9525 | case XML_ENTITY_REF_NODE: |
| 9526 | /* |
| 9527 | * Remove reference to the entitity-node. |
| 9528 | */ |
| 9529 | node->content = NULL; |
| 9530 | node->children = NULL; |
| 9531 | node->last = NULL; |
| 9532 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9533 | xmlEntityPtr ent; |
| 9534 | /* |
| 9535 | * Assign new entity-node if available. |
| 9536 | */ |
| 9537 | ent = xmlGetDocEntity(destDoc, node->name); |
| 9538 | if (ent != NULL) { |
| 9539 | node->content = ent->content; |
| 9540 | node->children = (xmlNodePtr) ent; |
| 9541 | node->last = (xmlNodePtr) ent; |
| 9542 | } |
| 9543 | } |
| 9544 | XML_TREE_ADOPT_STR(node->name) |
| 9545 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 9546 | case XML_PI_NODE: { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9547 | XML_TREE_ADOPT_STR(node->name) |
| 9548 | XML_TREE_ADOPT_STR_2(node->content) |
| 9549 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 9550 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9551 | default: |
| 9552 | break; |
| 9553 | } |
| 9554 | } |
| 9555 | return (0); |
| 9556 | } |
| 9557 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 9558 | #define bottom_tree |
| 9559 | #include "elfgcchack.h" |