Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2 | * tree.c : implementation of access function for an XML tree. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3 | * |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 4 | * References: |
| 5 | * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/ |
| 6 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7 | * See Copyright for the status of this software. |
| 8 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 9 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 13 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 14 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 16 | #include <string.h> /* for memset() only ! */ |
| 17 | |
| 18 | #ifdef HAVE_CTYPE_H |
| 19 | #include <ctype.h> |
| 20 | #endif |
| 21 | #ifdef HAVE_STDLIB_H |
| 22 | #include <stdlib.h> |
| 23 | #endif |
| 24 | #ifdef HAVE_ZLIB_H |
| 25 | #include <zlib.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <libxml/xmlmemory.h> |
| 29 | #include <libxml/tree.h> |
| 30 | #include <libxml/parser.h> |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 31 | #include <libxml/uri.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 32 | #include <libxml/entities.h> |
| 33 | #include <libxml/valid.h> |
| 34 | #include <libxml/xmlerror.h> |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 35 | #include <libxml/parserInternals.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 36 | #include <libxml/globals.h> |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 37 | #ifdef LIBXML_HTML_ENABLED |
| 38 | #include <libxml/HTMLtree.h> |
| 39 | #endif |
William M. Brack | 1d8c9b2 | 2004-12-25 10:14:57 +0000 | [diff] [blame] | 40 | #ifdef LIBXML_DEBUG_ENABLED |
| 41 | #include <libxml/debugXML.h> |
| 42 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 43 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 44 | int __xmlRegisterCallbacks = 0; |
| 45 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 46 | xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns); |
| 47 | |
| 48 | /************************************************************************ |
| 49 | * * |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 50 | * Tree memory error handler * |
| 51 | * * |
| 52 | ************************************************************************/ |
| 53 | /** |
| 54 | * xmlTreeErrMemory: |
| 55 | * @extra: extra informations |
| 56 | * |
| 57 | * Handle an out of memory condition |
| 58 | */ |
| 59 | static void |
| 60 | xmlTreeErrMemory(const char *extra) |
| 61 | { |
| 62 | __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * xmlTreeErr: |
| 67 | * @code: the error number |
| 68 | * @extra: extra informations |
| 69 | * |
| 70 | * Handle an out of memory condition |
| 71 | */ |
| 72 | static void |
| 73 | xmlTreeErr(int code, xmlNodePtr node, const char *extra) |
| 74 | { |
| 75 | const char *msg = NULL; |
| 76 | |
| 77 | switch(code) { |
| 78 | case XML_TREE_INVALID_HEX: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 79 | msg = "invalid hexadecimal character value\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 80 | break; |
| 81 | case XML_TREE_INVALID_DEC: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 82 | msg = "invalid decimal character value\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 83 | break; |
| 84 | case XML_TREE_UNTERMINATED_ENTITY: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 85 | msg = "unterminated entity reference %15s\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 86 | break; |
| 87 | default: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 88 | msg = "unexpected error number\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 89 | } |
| 90 | __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra); |
| 91 | } |
| 92 | |
| 93 | /************************************************************************ |
| 94 | * * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 95 | * A few static variables and macros * |
| 96 | * * |
| 97 | ************************************************************************/ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 98 | /* #undef xmlStringText */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 99 | const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 100 | /* #undef xmlStringTextNoenc */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 101 | const xmlChar xmlStringTextNoenc[] = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 102 | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 103 | /* #undef xmlStringComment */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 104 | const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
| 105 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 106 | static int xmlCompressMode = 0; |
| 107 | static int xmlCheckDTD = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 108 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 109 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
| 110 | xmlNodePtr ulccur = (n)->children; \ |
| 111 | if (ulccur == NULL) { \ |
| 112 | (n)->last = NULL; \ |
| 113 | } else { \ |
| 114 | while (ulccur->next != NULL) { \ |
| 115 | ulccur->parent = (n); \ |
| 116 | ulccur = ulccur->next; \ |
| 117 | } \ |
| 118 | ulccur->parent = (n); \ |
| 119 | (n)->last = ulccur; \ |
| 120 | }} |
| 121 | |
| 122 | /* #define DEBUG_BUFFER */ |
| 123 | /* #define DEBUG_TREE */ |
| 124 | |
| 125 | /************************************************************************ |
| 126 | * * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 127 | * Functions to move to entities.c once the * |
| 128 | * API freeze is smoothen and they can be made public. * |
| 129 | * * |
| 130 | ************************************************************************/ |
| 131 | #include <libxml/hash.h> |
| 132 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 133 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 134 | /** |
| 135 | * xmlGetEntityFromDtd: |
| 136 | * @dtd: A pointer to the DTD to search |
| 137 | * @name: The entity name |
| 138 | * |
| 139 | * Do an entity lookup in the DTD entity hash table and |
| 140 | * return the corresponding entity, if found. |
| 141 | * |
| 142 | * Returns A pointer to the entity structure or NULL if not found. |
| 143 | */ |
| 144 | static xmlEntityPtr |
| 145 | xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 146 | xmlEntitiesTablePtr table; |
| 147 | |
| 148 | if((dtd != NULL) && (dtd->entities != NULL)) { |
| 149 | table = (xmlEntitiesTablePtr) dtd->entities; |
| 150 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 151 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 152 | } |
| 153 | return(NULL); |
| 154 | } |
| 155 | /** |
| 156 | * xmlGetParameterEntityFromDtd: |
| 157 | * @dtd: A pointer to the DTD to search |
| 158 | * @name: The entity name |
| 159 | * |
| 160 | * Do an entity lookup in the DTD pararmeter entity hash table and |
| 161 | * return the corresponding entity, if found. |
| 162 | * |
| 163 | * Returns A pointer to the entity structure or NULL if not found. |
| 164 | */ |
| 165 | static xmlEntityPtr |
| 166 | xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 167 | xmlEntitiesTablePtr table; |
| 168 | |
| 169 | if ((dtd != NULL) && (dtd->pentities != NULL)) { |
| 170 | table = (xmlEntitiesTablePtr) dtd->pentities; |
| 171 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 172 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 173 | } |
| 174 | return(NULL); |
| 175 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 176 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 177 | |
| 178 | /************************************************************************ |
| 179 | * * |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 180 | * QName handling helper * |
| 181 | * * |
| 182 | ************************************************************************/ |
| 183 | |
| 184 | /** |
| 185 | * xmlBuildQName: |
| 186 | * @ncname: the Name |
| 187 | * @prefix: the prefix |
| 188 | * @memory: preallocated memory |
| 189 | * @len: preallocated memory length |
| 190 | * |
| 191 | * Builds the QName @prefix:@ncname in @memory if there is enough space |
| 192 | * and prefix is not NULL nor empty, otherwise allocate a new string. |
| 193 | * If prefix is NULL or empty it returns ncname. |
| 194 | * |
| 195 | * Returns the new string which must be freed by the caller if different from |
| 196 | * @memory and @ncname or NULL in case of error |
| 197 | */ |
| 198 | xmlChar * |
| 199 | xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix, |
| 200 | xmlChar *memory, int len) { |
| 201 | int lenn, lenp; |
| 202 | xmlChar *ret; |
| 203 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 204 | if (ncname == NULL) return(NULL); |
| 205 | if (prefix == NULL) return((xmlChar *) ncname); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 206 | |
| 207 | lenn = strlen((char *) ncname); |
| 208 | lenp = strlen((char *) prefix); |
| 209 | |
| 210 | if ((memory == NULL) || (len < lenn + lenp + 2)) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 211 | ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 212 | if (ret == NULL) { |
| 213 | xmlTreeErrMemory("building QName"); |
| 214 | return(NULL); |
| 215 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 216 | } else { |
| 217 | ret = memory; |
| 218 | } |
| 219 | memcpy(&ret[0], prefix, lenp); |
| 220 | ret[lenp] = ':'; |
| 221 | memcpy(&ret[lenp + 1], ncname, lenn); |
| 222 | ret[lenn + lenp + 1] = 0; |
| 223 | return(ret); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * xmlSplitQName2: |
| 228 | * @name: the full QName |
| 229 | * @prefix: a xmlChar ** |
| 230 | * |
| 231 | * parse an XML qualified name string |
| 232 | * |
| 233 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 234 | * |
| 235 | * [NS 6] Prefix ::= NCName |
| 236 | * |
| 237 | * [NS 7] LocalPart ::= NCName |
| 238 | * |
| 239 | * Returns NULL if not a QName, otherwise the local part, and prefix |
| 240 | * is updated to get the Prefix if any. |
| 241 | */ |
| 242 | |
| 243 | xmlChar * |
| 244 | xmlSplitQName2(const xmlChar *name, xmlChar **prefix) { |
| 245 | int len = 0; |
| 246 | xmlChar *ret = NULL; |
| 247 | |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 248 | if (prefix == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 249 | *prefix = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 250 | if (name == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 251 | |
| 252 | #ifndef XML_XML_NAMESPACE |
| 253 | /* xml: prefix is not really a namespace */ |
| 254 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 255 | (name[2] == 'l') && (name[3] == ':')) |
| 256 | return(NULL); |
| 257 | #endif |
| 258 | |
| 259 | /* nasty but valid */ |
| 260 | if (name[0] == ':') |
| 261 | return(NULL); |
| 262 | |
| 263 | /* |
| 264 | * we are not trying to validate but just to cut, and yes it will |
| 265 | * work even if this is as set of UTF-8 encoded chars |
| 266 | */ |
| 267 | while ((name[len] != 0) && (name[len] != ':')) |
| 268 | len++; |
| 269 | |
| 270 | if (name[len] == 0) |
| 271 | return(NULL); |
| 272 | |
| 273 | *prefix = xmlStrndup(name, len); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 274 | if (*prefix == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 275 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 276 | return(NULL); |
| 277 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 278 | ret = xmlStrdup(&name[len + 1]); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 279 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 280 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 281 | if (*prefix != NULL) { |
| 282 | xmlFree(*prefix); |
| 283 | *prefix = NULL; |
| 284 | } |
| 285 | return(NULL); |
| 286 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 287 | |
| 288 | return(ret); |
| 289 | } |
| 290 | |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 291 | /** |
| 292 | * xmlSplitQName3: |
| 293 | * @name: the full QName |
| 294 | * @len: an int * |
| 295 | * |
| 296 | * parse an XML qualified name string,i |
| 297 | * |
| 298 | * returns NULL if it is not a Qualified Name, otherwise, update len |
| 299 | * with the lenght in byte of the prefix and return a pointer |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 300 | * to the start of the name without the prefix |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 301 | */ |
| 302 | |
| 303 | const xmlChar * |
| 304 | xmlSplitQName3(const xmlChar *name, int *len) { |
| 305 | int l = 0; |
| 306 | |
| 307 | if (name == NULL) return(NULL); |
| 308 | if (len == NULL) return(NULL); |
| 309 | |
| 310 | /* nasty but valid */ |
| 311 | if (name[0] == ':') |
| 312 | return(NULL); |
| 313 | |
| 314 | /* |
| 315 | * we are not trying to validate but just to cut, and yes it will |
| 316 | * work even if this is as set of UTF-8 encoded chars |
| 317 | */ |
| 318 | while ((name[l] != 0) && (name[l] != ':')) |
| 319 | l++; |
| 320 | |
| 321 | if (name[l] == 0) |
| 322 | return(NULL); |
| 323 | |
| 324 | *len = l; |
| 325 | |
| 326 | return(&name[l+1]); |
| 327 | } |
| 328 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 329 | /************************************************************************ |
| 330 | * * |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 331 | * Check Name, NCName and QName strings * |
| 332 | * * |
| 333 | ************************************************************************/ |
| 334 | |
| 335 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l) |
| 336 | |
Daniel Veillard | 6b6d680 | 2005-07-03 21:00:34 +0000 | [diff] [blame] | 337 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_DEBUG_ENABLED) || defined (LIBXML_HTML_ENABLED) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 338 | /** |
| 339 | * xmlValidateNCName: |
| 340 | * @value: the value to check |
| 341 | * @space: allow spaces in front and end of the string |
| 342 | * |
| 343 | * Check that a value conforms to the lexical space of NCName |
| 344 | * |
| 345 | * Returns 0 if this validates, a positive error code number otherwise |
| 346 | * and -1 in case of internal or API error. |
| 347 | */ |
| 348 | int |
| 349 | xmlValidateNCName(const xmlChar *value, int space) { |
| 350 | const xmlChar *cur = value; |
| 351 | int c,l; |
| 352 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 353 | if (value == NULL) |
| 354 | return(-1); |
| 355 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 356 | /* |
| 357 | * First quick algorithm for ASCII range |
| 358 | */ |
| 359 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 360 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 361 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 362 | (*cur == '_')) |
| 363 | cur++; |
| 364 | else |
| 365 | goto try_complex; |
| 366 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 367 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 368 | ((*cur >= '0') && (*cur <= '9')) || |
| 369 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 370 | cur++; |
| 371 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 372 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 373 | if (*cur == 0) |
| 374 | return(0); |
| 375 | |
| 376 | try_complex: |
| 377 | /* |
| 378 | * Second check for chars outside the ASCII range |
| 379 | */ |
| 380 | cur = value; |
| 381 | c = CUR_SCHAR(cur, l); |
| 382 | if (space) { |
| 383 | while (IS_BLANK(c)) { |
| 384 | cur += l; |
| 385 | c = CUR_SCHAR(cur, l); |
| 386 | } |
| 387 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 388 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 389 | return(1); |
| 390 | cur += l; |
| 391 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 392 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 393 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 394 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 395 | cur += l; |
| 396 | c = CUR_SCHAR(cur, l); |
| 397 | } |
| 398 | if (space) { |
| 399 | while (IS_BLANK(c)) { |
| 400 | cur += l; |
| 401 | c = CUR_SCHAR(cur, l); |
| 402 | } |
| 403 | } |
| 404 | if (c != 0) |
| 405 | return(1); |
| 406 | |
| 407 | return(0); |
| 408 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 409 | #endif |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 410 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 411 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 412 | /** |
| 413 | * xmlValidateQName: |
| 414 | * @value: the value to check |
| 415 | * @space: allow spaces in front and end of the string |
| 416 | * |
| 417 | * Check that a value conforms to the lexical space of QName |
| 418 | * |
| 419 | * Returns 0 if this validates, a positive error code number otherwise |
| 420 | * and -1 in case of internal or API error. |
| 421 | */ |
| 422 | int |
| 423 | xmlValidateQName(const xmlChar *value, int space) { |
| 424 | const xmlChar *cur = value; |
| 425 | int c,l; |
| 426 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 427 | if (value == NULL) |
| 428 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 429 | /* |
| 430 | * First quick algorithm for ASCII range |
| 431 | */ |
| 432 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 433 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 434 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 435 | (*cur == '_')) |
| 436 | cur++; |
| 437 | else |
| 438 | goto try_complex; |
| 439 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 440 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 441 | ((*cur >= '0') && (*cur <= '9')) || |
| 442 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 443 | cur++; |
| 444 | if (*cur == ':') { |
| 445 | cur++; |
| 446 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 447 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 448 | (*cur == '_')) |
| 449 | cur++; |
| 450 | else |
| 451 | goto try_complex; |
| 452 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 453 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 454 | ((*cur >= '0') && (*cur <= '9')) || |
| 455 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 456 | cur++; |
| 457 | } |
| 458 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 459 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 460 | if (*cur == 0) |
| 461 | return(0); |
| 462 | |
| 463 | try_complex: |
| 464 | /* |
| 465 | * Second check for chars outside the ASCII range |
| 466 | */ |
| 467 | cur = value; |
| 468 | c = CUR_SCHAR(cur, l); |
| 469 | if (space) { |
| 470 | while (IS_BLANK(c)) { |
| 471 | cur += l; |
| 472 | c = CUR_SCHAR(cur, l); |
| 473 | } |
| 474 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 475 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 476 | return(1); |
| 477 | cur += l; |
| 478 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 479 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 480 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 481 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 482 | cur += l; |
| 483 | c = CUR_SCHAR(cur, l); |
| 484 | } |
| 485 | if (c == ':') { |
| 486 | cur += l; |
| 487 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 488 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 489 | return(1); |
| 490 | cur += l; |
| 491 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 492 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 493 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 494 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 495 | cur += l; |
| 496 | c = CUR_SCHAR(cur, l); |
| 497 | } |
| 498 | } |
| 499 | if (space) { |
| 500 | while (IS_BLANK(c)) { |
| 501 | cur += l; |
| 502 | c = CUR_SCHAR(cur, l); |
| 503 | } |
| 504 | } |
| 505 | if (c != 0) |
| 506 | return(1); |
| 507 | return(0); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * xmlValidateName: |
| 512 | * @value: the value to check |
| 513 | * @space: allow spaces in front and end of the string |
| 514 | * |
| 515 | * Check that a value conforms to the lexical space of Name |
| 516 | * |
| 517 | * Returns 0 if this validates, a positive error code number otherwise |
| 518 | * and -1 in case of internal or API error. |
| 519 | */ |
| 520 | int |
| 521 | xmlValidateName(const xmlChar *value, int space) { |
| 522 | const xmlChar *cur = value; |
| 523 | int c,l; |
| 524 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 525 | if (value == NULL) |
| 526 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 527 | /* |
| 528 | * First quick algorithm for ASCII range |
| 529 | */ |
| 530 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 531 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 532 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 533 | (*cur == '_') || (*cur == ':')) |
| 534 | cur++; |
| 535 | else |
| 536 | goto try_complex; |
| 537 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 538 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 539 | ((*cur >= '0') && (*cur <= '9')) || |
| 540 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 541 | cur++; |
| 542 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 543 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 544 | if (*cur == 0) |
| 545 | return(0); |
| 546 | |
| 547 | try_complex: |
| 548 | /* |
| 549 | * Second check for chars outside the ASCII range |
| 550 | */ |
| 551 | cur = value; |
| 552 | c = CUR_SCHAR(cur, l); |
| 553 | if (space) { |
| 554 | while (IS_BLANK(c)) { |
| 555 | cur += l; |
| 556 | c = CUR_SCHAR(cur, l); |
| 557 | } |
| 558 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 559 | if ((!IS_LETTER(c)) && (c != '_') && (c != ':')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 560 | return(1); |
| 561 | cur += l; |
| 562 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 563 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 564 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 565 | cur += l; |
| 566 | c = CUR_SCHAR(cur, l); |
| 567 | } |
| 568 | if (space) { |
| 569 | while (IS_BLANK(c)) { |
| 570 | cur += l; |
| 571 | c = CUR_SCHAR(cur, l); |
| 572 | } |
| 573 | } |
| 574 | if (c != 0) |
| 575 | return(1); |
| 576 | return(0); |
| 577 | } |
| 578 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 579 | /** |
| 580 | * xmlValidateNMToken: |
| 581 | * @value: the value to check |
| 582 | * @space: allow spaces in front and end of the string |
| 583 | * |
| 584 | * Check that a value conforms to the lexical space of NMToken |
| 585 | * |
| 586 | * Returns 0 if this validates, a positive error code number otherwise |
| 587 | * and -1 in case of internal or API error. |
| 588 | */ |
| 589 | int |
| 590 | xmlValidateNMToken(const xmlChar *value, int space) { |
| 591 | const xmlChar *cur = value; |
| 592 | int c,l; |
| 593 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 594 | if (value == NULL) |
| 595 | return(-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 596 | /* |
| 597 | * First quick algorithm for ASCII range |
| 598 | */ |
| 599 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 600 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 601 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 602 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 603 | ((*cur >= '0') && (*cur <= '9')) || |
| 604 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 605 | cur++; |
| 606 | else |
| 607 | goto try_complex; |
| 608 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 609 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 610 | ((*cur >= '0') && (*cur <= '9')) || |
| 611 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 612 | cur++; |
| 613 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 614 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 615 | if (*cur == 0) |
| 616 | return(0); |
| 617 | |
| 618 | try_complex: |
| 619 | /* |
| 620 | * Second check for chars outside the ASCII range |
| 621 | */ |
| 622 | cur = value; |
| 623 | c = CUR_SCHAR(cur, l); |
| 624 | if (space) { |
| 625 | while (IS_BLANK(c)) { |
| 626 | cur += l; |
| 627 | c = CUR_SCHAR(cur, l); |
| 628 | } |
| 629 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 630 | if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 631 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c))) |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 632 | return(1); |
| 633 | cur += l; |
| 634 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 635 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 636 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 637 | cur += l; |
| 638 | c = CUR_SCHAR(cur, l); |
| 639 | } |
| 640 | if (space) { |
| 641 | while (IS_BLANK(c)) { |
| 642 | cur += l; |
| 643 | c = CUR_SCHAR(cur, l); |
| 644 | } |
| 645 | } |
| 646 | if (c != 0) |
| 647 | return(1); |
| 648 | return(0); |
| 649 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 650 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 651 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 652 | /************************************************************************ |
| 653 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 654 | * Allocation and deallocation of basic structures * |
| 655 | * * |
| 656 | ************************************************************************/ |
| 657 | |
| 658 | /** |
| 659 | * xmlSetBufferAllocationScheme: |
| 660 | * @scheme: allocation method to use |
| 661 | * |
| 662 | * Set the buffer allocation method. Types are |
| 663 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 664 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 665 | * improves performance |
| 666 | */ |
| 667 | void |
| 668 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
| 669 | xmlBufferAllocScheme = scheme; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * xmlGetBufferAllocationScheme: |
| 674 | * |
| 675 | * Types are |
| 676 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 677 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 678 | * improves performance |
| 679 | * |
| 680 | * Returns the current allocation scheme |
| 681 | */ |
| 682 | xmlBufferAllocationScheme |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 683 | xmlGetBufferAllocationScheme(void) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 684 | return(xmlBufferAllocScheme); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /** |
| 688 | * xmlNewNs: |
| 689 | * @node: the element carrying the namespace |
| 690 | * @href: the URI associated |
| 691 | * @prefix: the prefix for the namespace |
| 692 | * |
| 693 | * Creation of a new Namespace. This function will refuse to create |
| 694 | * a namespace with a similar prefix than an existing one present on this |
| 695 | * node. |
| 696 | * We use href==NULL in the case of an element creation where the namespace |
| 697 | * was not defined. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 698 | * Returns a new namespace pointer or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 699 | */ |
| 700 | xmlNsPtr |
| 701 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
| 702 | xmlNsPtr cur; |
| 703 | |
| 704 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 705 | return(NULL); |
| 706 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 707 | if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) |
| 708 | return(NULL); |
| 709 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 710 | /* |
| 711 | * Allocate a new Namespace and fill the fields. |
| 712 | */ |
| 713 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 714 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 715 | xmlTreeErrMemory("building namespace"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 716 | return(NULL); |
| 717 | } |
| 718 | memset(cur, 0, sizeof(xmlNs)); |
| 719 | cur->type = XML_LOCAL_NAMESPACE; |
| 720 | |
| 721 | if (href != NULL) |
| 722 | cur->href = xmlStrdup(href); |
| 723 | if (prefix != NULL) |
| 724 | cur->prefix = xmlStrdup(prefix); |
| 725 | |
| 726 | /* |
| 727 | * Add it at the end to preserve parsing order ... |
| 728 | * and checks for existing use of the prefix |
| 729 | */ |
| 730 | if (node != NULL) { |
| 731 | if (node->nsDef == NULL) { |
| 732 | node->nsDef = cur; |
| 733 | } else { |
| 734 | xmlNsPtr prev = node->nsDef; |
| 735 | |
| 736 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 737 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 738 | xmlFreeNs(cur); |
| 739 | return(NULL); |
| 740 | } |
| 741 | while (prev->next != NULL) { |
| 742 | prev = prev->next; |
| 743 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 744 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 745 | xmlFreeNs(cur); |
| 746 | return(NULL); |
| 747 | } |
| 748 | } |
| 749 | prev->next = cur; |
| 750 | } |
| 751 | } |
| 752 | return(cur); |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * xmlSetNs: |
| 757 | * @node: a node in the document |
| 758 | * @ns: a namespace pointer |
| 759 | * |
| 760 | * Associate a namespace to a node, a posteriori. |
| 761 | */ |
| 762 | void |
| 763 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
| 764 | if (node == NULL) { |
| 765 | #ifdef DEBUG_TREE |
| 766 | xmlGenericError(xmlGenericErrorContext, |
| 767 | "xmlSetNs: node == NULL\n"); |
| 768 | #endif |
| 769 | return; |
| 770 | } |
| 771 | node->ns = ns; |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * xmlFreeNs: |
| 776 | * @cur: the namespace pointer |
| 777 | * |
| 778 | * Free up the structures associated to a namespace |
| 779 | */ |
| 780 | void |
| 781 | xmlFreeNs(xmlNsPtr cur) { |
| 782 | if (cur == NULL) { |
| 783 | #ifdef DEBUG_TREE |
| 784 | xmlGenericError(xmlGenericErrorContext, |
| 785 | "xmlFreeNs : ns == NULL\n"); |
| 786 | #endif |
| 787 | return; |
| 788 | } |
| 789 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 790 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 791 | xmlFree(cur); |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * xmlFreeNsList: |
| 796 | * @cur: the first namespace pointer |
| 797 | * |
| 798 | * Free up all the structures associated to the chained namespaces. |
| 799 | */ |
| 800 | void |
| 801 | xmlFreeNsList(xmlNsPtr cur) { |
| 802 | xmlNsPtr next; |
| 803 | if (cur == NULL) { |
| 804 | #ifdef DEBUG_TREE |
| 805 | xmlGenericError(xmlGenericErrorContext, |
| 806 | "xmlFreeNsList : ns == NULL\n"); |
| 807 | #endif |
| 808 | return; |
| 809 | } |
| 810 | while (cur != NULL) { |
| 811 | next = cur->next; |
| 812 | xmlFreeNs(cur); |
| 813 | cur = next; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * xmlNewDtd: |
| 819 | * @doc: the document pointer |
| 820 | * @name: the DTD name |
| 821 | * @ExternalID: the external ID |
| 822 | * @SystemID: the system ID |
| 823 | * |
| 824 | * Creation of a new DTD for the external subset. To create an |
| 825 | * internal subset, use xmlCreateIntSubset(). |
| 826 | * |
| 827 | * Returns a pointer to the new DTD structure |
| 828 | */ |
| 829 | xmlDtdPtr |
| 830 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 831 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 832 | xmlDtdPtr cur; |
| 833 | |
| 834 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
| 835 | #ifdef DEBUG_TREE |
| 836 | xmlGenericError(xmlGenericErrorContext, |
| 837 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
| 838 | /* !!! */ (char *) name, doc->name, |
| 839 | /* !!! */ (char *)doc->extSubset->name); |
| 840 | #endif |
| 841 | return(NULL); |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Allocate a new DTD and fill the fields. |
| 846 | */ |
| 847 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 848 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 849 | xmlTreeErrMemory("building DTD"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 850 | return(NULL); |
| 851 | } |
| 852 | memset(cur, 0 , sizeof(xmlDtd)); |
| 853 | cur->type = XML_DTD_NODE; |
| 854 | |
| 855 | if (name != NULL) |
| 856 | cur->name = xmlStrdup(name); |
| 857 | if (ExternalID != NULL) |
| 858 | cur->ExternalID = xmlStrdup(ExternalID); |
| 859 | if (SystemID != NULL) |
| 860 | cur->SystemID = xmlStrdup(SystemID); |
| 861 | if (doc != NULL) |
| 862 | doc->extSubset = cur; |
| 863 | cur->doc = doc; |
| 864 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 865 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 866 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 867 | return(cur); |
| 868 | } |
| 869 | |
| 870 | /** |
| 871 | * xmlGetIntSubset: |
| 872 | * @doc: the document pointer |
| 873 | * |
| 874 | * Get the internal subset of a document |
| 875 | * Returns a pointer to the DTD structure or NULL if not found |
| 876 | */ |
| 877 | |
| 878 | xmlDtdPtr |
| 879 | xmlGetIntSubset(xmlDocPtr doc) { |
| 880 | xmlNodePtr cur; |
| 881 | |
| 882 | if (doc == NULL) |
| 883 | return(NULL); |
| 884 | cur = doc->children; |
| 885 | while (cur != NULL) { |
| 886 | if (cur->type == XML_DTD_NODE) |
| 887 | return((xmlDtdPtr) cur); |
| 888 | cur = cur->next; |
| 889 | } |
| 890 | return((xmlDtdPtr) doc->intSubset); |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * xmlCreateIntSubset: |
| 895 | * @doc: the document pointer |
| 896 | * @name: the DTD name |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 897 | * @ExternalID: the external (PUBLIC) ID |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 898 | * @SystemID: the system ID |
| 899 | * |
| 900 | * Create the internal subset of a document |
| 901 | * Returns a pointer to the new DTD structure |
| 902 | */ |
| 903 | xmlDtdPtr |
| 904 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 905 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 906 | xmlDtdPtr cur; |
| 907 | |
| 908 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
| 909 | #ifdef DEBUG_TREE |
| 910 | xmlGenericError(xmlGenericErrorContext, |
| 911 | |
| 912 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 913 | doc->name); |
| 914 | #endif |
| 915 | return(NULL); |
| 916 | } |
| 917 | |
| 918 | /* |
| 919 | * Allocate a new DTD and fill the fields. |
| 920 | */ |
| 921 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 922 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 923 | xmlTreeErrMemory("building internal subset"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 924 | return(NULL); |
| 925 | } |
| 926 | memset(cur, 0, sizeof(xmlDtd)); |
| 927 | cur->type = XML_DTD_NODE; |
| 928 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 929 | if (name != NULL) { |
| 930 | cur->name = xmlStrdup(name); |
| 931 | if (cur->name == NULL) { |
| 932 | xmlTreeErrMemory("building internal subset"); |
| 933 | xmlFree(cur); |
| 934 | return(NULL); |
| 935 | } |
| 936 | } |
| 937 | if (ExternalID != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 938 | cur->ExternalID = xmlStrdup(ExternalID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 939 | if (cur->ExternalID == NULL) { |
| 940 | xmlTreeErrMemory("building internal subset"); |
| 941 | if (cur->name != NULL) |
| 942 | xmlFree((char *)cur->name); |
| 943 | xmlFree(cur); |
| 944 | return(NULL); |
| 945 | } |
| 946 | } |
| 947 | if (SystemID != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 948 | cur->SystemID = xmlStrdup(SystemID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 949 | if (cur->SystemID == NULL) { |
| 950 | xmlTreeErrMemory("building internal subset"); |
| 951 | if (cur->name != NULL) |
| 952 | xmlFree((char *)cur->name); |
| 953 | if (cur->ExternalID != NULL) |
| 954 | xmlFree((char *)cur->ExternalID); |
| 955 | xmlFree(cur); |
| 956 | return(NULL); |
| 957 | } |
| 958 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 959 | if (doc != NULL) { |
| 960 | doc->intSubset = cur; |
| 961 | cur->parent = doc; |
| 962 | cur->doc = doc; |
| 963 | if (doc->children == NULL) { |
| 964 | doc->children = (xmlNodePtr) cur; |
| 965 | doc->last = (xmlNodePtr) cur; |
| 966 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 967 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 968 | xmlNodePtr prev; |
| 969 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 970 | prev = doc->children; |
| 971 | prev->prev = (xmlNodePtr) cur; |
| 972 | cur->next = prev; |
| 973 | doc->children = (xmlNodePtr) cur; |
| 974 | } else { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 975 | xmlNodePtr next; |
| 976 | |
| 977 | next = doc->children; |
| 978 | while ((next != NULL) && (next->type != XML_ELEMENT_NODE)) |
| 979 | next = next->next; |
| 980 | if (next == NULL) { |
| 981 | cur->prev = doc->last; |
| 982 | cur->prev->next = (xmlNodePtr) cur; |
| 983 | cur->next = NULL; |
| 984 | doc->last = (xmlNodePtr) cur; |
| 985 | } else { |
| 986 | cur->next = next; |
| 987 | cur->prev = next->prev; |
| 988 | if (cur->prev == NULL) |
| 989 | doc->children = (xmlNodePtr) cur; |
| 990 | else |
| 991 | cur->prev->next = (xmlNodePtr) cur; |
| 992 | next->prev = (xmlNodePtr) cur; |
| 993 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 997 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 998 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 999 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1000 | return(cur); |
| 1001 | } |
| 1002 | |
| 1003 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1004 | * DICT_FREE: |
| 1005 | * @str: a string |
| 1006 | * |
| 1007 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 1008 | * current scope |
| 1009 | */ |
| 1010 | #define DICT_FREE(str) \ |
| 1011 | if ((str) && ((!dict) || \ |
| 1012 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 1013 | xmlFree((char *)(str)); |
| 1014 | |
| 1015 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1016 | * xmlFreeDtd: |
| 1017 | * @cur: the DTD structure to free up |
| 1018 | * |
| 1019 | * Free a DTD structure. |
| 1020 | */ |
| 1021 | void |
| 1022 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1023 | xmlDictPtr dict = NULL; |
| 1024 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1025 | if (cur == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1026 | return; |
| 1027 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1028 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1029 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1030 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1031 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1032 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1033 | if (cur->children != NULL) { |
| 1034 | xmlNodePtr next, c = cur->children; |
| 1035 | |
| 1036 | /* |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1037 | * Cleanup all nodes which are not part of the specific lists |
| 1038 | * of notations, elements, attributes and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1039 | */ |
| 1040 | while (c != NULL) { |
| 1041 | next = c->next; |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1042 | if ((c->type != XML_NOTATION_NODE) && |
| 1043 | (c->type != XML_ELEMENT_DECL) && |
| 1044 | (c->type != XML_ATTRIBUTE_DECL) && |
| 1045 | (c->type != XML_ENTITY_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1046 | xmlUnlinkNode(c); |
| 1047 | xmlFreeNode(c); |
| 1048 | } |
| 1049 | c = next; |
| 1050 | } |
| 1051 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1052 | DICT_FREE(cur->name) |
| 1053 | DICT_FREE(cur->SystemID) |
| 1054 | DICT_FREE(cur->ExternalID) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1055 | /* TODO !!! */ |
| 1056 | if (cur->notations != NULL) |
| 1057 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
| 1058 | |
| 1059 | if (cur->elements != NULL) |
| 1060 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
| 1061 | if (cur->attributes != NULL) |
| 1062 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
| 1063 | if (cur->entities != NULL) |
| 1064 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 1065 | if (cur->pentities != NULL) |
| 1066 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
| 1067 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1068 | xmlFree(cur); |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * xmlNewDoc: |
| 1073 | * @version: xmlChar string giving the version of XML "1.0" |
| 1074 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1075 | * Creates a new XML document |
| 1076 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1077 | * Returns a new document |
| 1078 | */ |
| 1079 | xmlDocPtr |
| 1080 | xmlNewDoc(const xmlChar *version) { |
| 1081 | xmlDocPtr cur; |
| 1082 | |
| 1083 | if (version == NULL) |
| 1084 | version = (const xmlChar *) "1.0"; |
| 1085 | |
| 1086 | /* |
| 1087 | * Allocate a new document and fill the fields. |
| 1088 | */ |
| 1089 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
| 1090 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1091 | xmlTreeErrMemory("building doc"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1092 | return(NULL); |
| 1093 | } |
| 1094 | memset(cur, 0, sizeof(xmlDoc)); |
| 1095 | cur->type = XML_DOCUMENT_NODE; |
| 1096 | |
| 1097 | cur->version = xmlStrdup(version); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 1098 | if (cur->version == NULL) { |
| 1099 | xmlTreeErrMemory("building doc"); |
| 1100 | xmlFree(cur); |
| 1101 | return(NULL); |
| 1102 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1103 | cur->standalone = -1; |
| 1104 | cur->compression = -1; /* not initialized */ |
| 1105 | cur->doc = cur; |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 1106 | /* |
| 1107 | * The in memory encoding is always UTF8 |
| 1108 | * This field will never change and would |
| 1109 | * be obsolete if not for binary compatibility. |
| 1110 | */ |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 1111 | cur->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1112 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1113 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1114 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1115 | return(cur); |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * xmlFreeDoc: |
| 1120 | * @cur: pointer to the document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1121 | * |
| 1122 | * Free up all the structures used by a document, tree included. |
| 1123 | */ |
| 1124 | void |
| 1125 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1126 | xmlDtdPtr extSubset, intSubset; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1127 | xmlDictPtr dict = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1128 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1129 | if (cur == NULL) { |
| 1130 | #ifdef DEBUG_TREE |
| 1131 | xmlGenericError(xmlGenericErrorContext, |
| 1132 | "xmlFreeDoc : document == NULL\n"); |
| 1133 | #endif |
| 1134 | return; |
| 1135 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1136 | #ifdef LIBXML_DEBUG_RUNTIME |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1137 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1138 | xmlDebugCheckDocument(stderr, cur); |
| 1139 | #endif |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1140 | #endif |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1141 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1142 | if (cur != NULL) dict = cur->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1143 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1144 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1145 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1146 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1147 | /* |
| 1148 | * Do this before freeing the children list to avoid ID lookups |
| 1149 | */ |
| 1150 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
| 1151 | cur->ids = NULL; |
| 1152 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
| 1153 | cur->refs = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1154 | extSubset = cur->extSubset; |
| 1155 | intSubset = cur->intSubset; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 1156 | if (intSubset == extSubset) |
| 1157 | extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1158 | if (extSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1159 | xmlUnlinkNode((xmlNodePtr) cur->extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1160 | cur->extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1161 | xmlFreeDtd(extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1162 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1163 | if (intSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1164 | xmlUnlinkNode((xmlNodePtr) cur->intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1165 | cur->intSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1166 | xmlFreeDtd(intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1170 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1171 | |
| 1172 | DICT_FREE(cur->version) |
| 1173 | DICT_FREE(cur->name) |
| 1174 | DICT_FREE(cur->encoding) |
| 1175 | DICT_FREE(cur->URL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1176 | xmlFree(cur); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1177 | if (dict) xmlDictFree(dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * xmlStringLenGetNodeList: |
| 1182 | * @doc: the document |
| 1183 | * @value: the value of the text |
| 1184 | * @len: the length of the string value |
| 1185 | * |
| 1186 | * Parse the value string and build the node list associated. Should |
| 1187 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1188 | * Returns a pointer to the first child |
| 1189 | */ |
| 1190 | xmlNodePtr |
| 1191 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
| 1192 | xmlNodePtr ret = NULL, last = NULL; |
| 1193 | xmlNodePtr node; |
| 1194 | xmlChar *val; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1195 | const xmlChar *cur = value, *end = cur + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1196 | const xmlChar *q; |
| 1197 | xmlEntityPtr ent; |
| 1198 | |
| 1199 | if (value == NULL) return(NULL); |
| 1200 | |
| 1201 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1202 | while ((cur < end) && (*cur != 0)) { |
| 1203 | if (cur[0] == '&') { |
| 1204 | int charval = 0; |
| 1205 | xmlChar tmp; |
| 1206 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1207 | /* |
| 1208 | * Save the current text. |
| 1209 | */ |
| 1210 | if (cur != q) { |
| 1211 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1212 | xmlNodeAddContentLen(last, q, cur - q); |
| 1213 | } else { |
| 1214 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1215 | if (node == NULL) return(ret); |
| 1216 | if (last == NULL) |
| 1217 | last = ret = node; |
| 1218 | else { |
| 1219 | last->next = node; |
| 1220 | node->prev = last; |
| 1221 | last = node; |
| 1222 | } |
| 1223 | } |
| 1224 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1225 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1226 | if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) { |
| 1227 | cur += 3; |
| 1228 | if (cur < end) |
| 1229 | tmp = *cur; |
| 1230 | else |
| 1231 | tmp = 0; |
| 1232 | while (tmp != ';') { /* Non input consuming loop */ |
| 1233 | if ((tmp >= '0') && (tmp <= '9')) |
| 1234 | charval = charval * 16 + (tmp - '0'); |
| 1235 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1236 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1237 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1238 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1239 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1240 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1241 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1242 | charval = 0; |
| 1243 | break; |
| 1244 | } |
| 1245 | cur++; |
| 1246 | if (cur < end) |
| 1247 | tmp = *cur; |
| 1248 | else |
| 1249 | tmp = 0; |
| 1250 | } |
| 1251 | if (tmp == ';') |
| 1252 | cur++; |
| 1253 | q = cur; |
| 1254 | } else if ((cur + 1 < end) && (cur[1] == '#')) { |
| 1255 | cur += 2; |
| 1256 | if (cur < end) |
| 1257 | tmp = *cur; |
| 1258 | else |
| 1259 | tmp = 0; |
| 1260 | while (tmp != ';') { /* Non input consuming loops */ |
| 1261 | if ((tmp >= '0') && (tmp <= '9')) |
| 1262 | charval = charval * 10 + (tmp - '0'); |
| 1263 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1264 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1265 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1266 | charval = 0; |
| 1267 | break; |
| 1268 | } |
| 1269 | cur++; |
| 1270 | if (cur < end) |
| 1271 | tmp = *cur; |
| 1272 | else |
| 1273 | tmp = 0; |
| 1274 | } |
| 1275 | if (tmp == ';') |
| 1276 | cur++; |
| 1277 | q = cur; |
| 1278 | } else { |
| 1279 | /* |
| 1280 | * Read the entity string |
| 1281 | */ |
| 1282 | cur++; |
| 1283 | q = cur; |
| 1284 | while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++; |
| 1285 | if ((cur >= end) || (*cur == 0)) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1286 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc, |
| 1287 | (const char *) q); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1288 | return(ret); |
| 1289 | } |
| 1290 | if (cur != q) { |
| 1291 | /* |
| 1292 | * Predefined entities don't generate nodes |
| 1293 | */ |
| 1294 | val = xmlStrndup(q, cur - q); |
| 1295 | ent = xmlGetDocEntity(doc, val); |
| 1296 | if ((ent != NULL) && |
| 1297 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1298 | if (last == NULL) { |
| 1299 | node = xmlNewDocText(doc, ent->content); |
| 1300 | last = ret = node; |
| 1301 | } else if (last->type != XML_TEXT_NODE) { |
| 1302 | node = xmlNewDocText(doc, ent->content); |
| 1303 | last = xmlAddNextSibling(last, node); |
| 1304 | } else |
| 1305 | xmlNodeAddContent(last, ent->content); |
| 1306 | |
| 1307 | } else { |
| 1308 | /* |
| 1309 | * Create a new REFERENCE_REF node |
| 1310 | */ |
| 1311 | node = xmlNewReference(doc, val); |
| 1312 | if (node == NULL) { |
| 1313 | if (val != NULL) xmlFree(val); |
| 1314 | return(ret); |
| 1315 | } |
| 1316 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1317 | xmlNodePtr temp; |
| 1318 | |
| 1319 | ent->children = xmlStringGetNodeList(doc, |
| 1320 | (const xmlChar*)node->content); |
| 1321 | ent->owner = 1; |
| 1322 | temp = ent->children; |
| 1323 | while (temp) { |
| 1324 | temp->parent = (xmlNodePtr)ent; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1325 | ent->last = temp; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1326 | temp = temp->next; |
| 1327 | } |
| 1328 | } |
| 1329 | if (last == NULL) { |
| 1330 | last = ret = node; |
| 1331 | } else { |
| 1332 | last = xmlAddNextSibling(last, node); |
| 1333 | } |
| 1334 | } |
| 1335 | xmlFree(val); |
| 1336 | } |
| 1337 | cur++; |
| 1338 | q = cur; |
| 1339 | } |
| 1340 | if (charval != 0) { |
| 1341 | xmlChar buf[10]; |
| 1342 | int l; |
| 1343 | |
| 1344 | l = xmlCopyCharMultiByte(buf, charval); |
| 1345 | buf[l] = 0; |
| 1346 | node = xmlNewDocText(doc, buf); |
| 1347 | if (node != NULL) { |
| 1348 | if (last == NULL) { |
| 1349 | last = ret = node; |
| 1350 | } else { |
| 1351 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1352 | } |
| 1353 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1354 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1355 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1356 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1357 | cur++; |
| 1358 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1359 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1360 | /* |
| 1361 | * Handle the last piece of text. |
| 1362 | */ |
| 1363 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1364 | xmlNodeAddContentLen(last, q, cur - q); |
| 1365 | } else { |
| 1366 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1367 | if (node == NULL) return(ret); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1368 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1369 | last = ret = node; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1370 | } else { |
| 1371 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | return(ret); |
| 1376 | } |
| 1377 | |
| 1378 | /** |
| 1379 | * xmlStringGetNodeList: |
| 1380 | * @doc: the document |
| 1381 | * @value: the value of the attribute |
| 1382 | * |
| 1383 | * Parse the value string and build the node list associated. Should |
| 1384 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1385 | * Returns a pointer to the first child |
| 1386 | */ |
| 1387 | xmlNodePtr |
| 1388 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
| 1389 | xmlNodePtr ret = NULL, last = NULL; |
| 1390 | xmlNodePtr node; |
| 1391 | xmlChar *val; |
| 1392 | const xmlChar *cur = value; |
| 1393 | const xmlChar *q; |
| 1394 | xmlEntityPtr ent; |
| 1395 | |
| 1396 | if (value == NULL) return(NULL); |
| 1397 | |
| 1398 | q = cur; |
| 1399 | while (*cur != 0) { |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1400 | if (cur[0] == '&') { |
| 1401 | int charval = 0; |
| 1402 | xmlChar tmp; |
| 1403 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1404 | /* |
| 1405 | * Save the current text. |
| 1406 | */ |
| 1407 | if (cur != q) { |
| 1408 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1409 | xmlNodeAddContentLen(last, q, cur - q); |
| 1410 | } else { |
| 1411 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1412 | if (node == NULL) return(ret); |
| 1413 | if (last == NULL) |
| 1414 | last = ret = node; |
| 1415 | else { |
| 1416 | last->next = node; |
| 1417 | node->prev = last; |
| 1418 | last = node; |
| 1419 | } |
| 1420 | } |
| 1421 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1422 | q = cur; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1423 | if ((cur[1] == '#') && (cur[2] == 'x')) { |
| 1424 | cur += 3; |
| 1425 | tmp = *cur; |
| 1426 | while (tmp != ';') { /* Non input consuming loop */ |
| 1427 | if ((tmp >= '0') && (tmp <= '9')) |
| 1428 | charval = charval * 16 + (tmp - '0'); |
| 1429 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1430 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1431 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1432 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1433 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1434 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1435 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1436 | charval = 0; |
| 1437 | break; |
| 1438 | } |
| 1439 | cur++; |
| 1440 | tmp = *cur; |
| 1441 | } |
| 1442 | if (tmp == ';') |
| 1443 | cur++; |
| 1444 | q = cur; |
| 1445 | } else if (cur[1] == '#') { |
| 1446 | cur += 2; |
| 1447 | tmp = *cur; |
| 1448 | while (tmp != ';') { /* Non input consuming loops */ |
| 1449 | if ((tmp >= '0') && (tmp <= '9')) |
| 1450 | charval = charval * 10 + (tmp - '0'); |
| 1451 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1452 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1453 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1454 | charval = 0; |
| 1455 | break; |
| 1456 | } |
| 1457 | cur++; |
| 1458 | tmp = *cur; |
| 1459 | } |
| 1460 | if (tmp == ';') |
| 1461 | cur++; |
| 1462 | q = cur; |
| 1463 | } else { |
| 1464 | /* |
| 1465 | * Read the entity string |
| 1466 | */ |
| 1467 | cur++; |
| 1468 | q = cur; |
| 1469 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 1470 | if (*cur == 0) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1471 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, |
| 1472 | (xmlNodePtr) doc, (const char *) q); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1473 | return(ret); |
| 1474 | } |
| 1475 | if (cur != q) { |
| 1476 | /* |
| 1477 | * Predefined entities don't generate nodes |
| 1478 | */ |
| 1479 | val = xmlStrndup(q, cur - q); |
| 1480 | ent = xmlGetDocEntity(doc, val); |
| 1481 | if ((ent != NULL) && |
| 1482 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1483 | if (last == NULL) { |
| 1484 | node = xmlNewDocText(doc, ent->content); |
| 1485 | last = ret = node; |
Daniel Veillard | 6f42c13 | 2002-01-06 23:05:13 +0000 | [diff] [blame] | 1486 | } else if (last->type != XML_TEXT_NODE) { |
| 1487 | node = xmlNewDocText(doc, ent->content); |
| 1488 | last = xmlAddNextSibling(last, node); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1489 | } else |
| 1490 | xmlNodeAddContent(last, ent->content); |
| 1491 | |
| 1492 | } else { |
| 1493 | /* |
| 1494 | * Create a new REFERENCE_REF node |
| 1495 | */ |
| 1496 | node = xmlNewReference(doc, val); |
| 1497 | if (node == NULL) { |
| 1498 | if (val != NULL) xmlFree(val); |
| 1499 | return(ret); |
| 1500 | } |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1501 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1502 | xmlNodePtr temp; |
| 1503 | |
| 1504 | ent->children = xmlStringGetNodeList(doc, |
| 1505 | (const xmlChar*)node->content); |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 1506 | ent->owner = 1; |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1507 | temp = ent->children; |
| 1508 | while (temp) { |
| 1509 | temp->parent = (xmlNodePtr)ent; |
| 1510 | temp = temp->next; |
| 1511 | } |
| 1512 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1513 | if (last == NULL) { |
| 1514 | last = ret = node; |
| 1515 | } else { |
| 1516 | last = xmlAddNextSibling(last, node); |
| 1517 | } |
| 1518 | } |
| 1519 | xmlFree(val); |
| 1520 | } |
| 1521 | cur++; |
| 1522 | q = cur; |
| 1523 | } |
| 1524 | if (charval != 0) { |
| 1525 | xmlChar buf[10]; |
| 1526 | int len; |
| 1527 | |
| 1528 | len = xmlCopyCharMultiByte(buf, charval); |
| 1529 | buf[len] = 0; |
| 1530 | node = xmlNewDocText(doc, buf); |
| 1531 | if (node != NULL) { |
| 1532 | if (last == NULL) { |
| 1533 | last = ret = node; |
| 1534 | } else { |
| 1535 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1536 | } |
| 1537 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1538 | |
| 1539 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1540 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1541 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1542 | cur++; |
| 1543 | } |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 1544 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1545 | /* |
| 1546 | * Handle the last piece of text. |
| 1547 | */ |
| 1548 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1549 | xmlNodeAddContentLen(last, q, cur - q); |
| 1550 | } else { |
| 1551 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1552 | if (node == NULL) return(ret); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1553 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1554 | last = ret = node; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1555 | } else { |
| 1556 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | return(ret); |
| 1561 | } |
| 1562 | |
| 1563 | /** |
| 1564 | * xmlNodeListGetString: |
| 1565 | * @doc: the document |
| 1566 | * @list: a Node list |
| 1567 | * @inLine: should we replace entity contents or show their external form |
| 1568 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1569 | * Build the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1570 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1571 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1572 | * Returns a pointer to the string copy, the caller must free it with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1573 | */ |
| 1574 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1575 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1576 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1577 | xmlNodePtr node = list; |
| 1578 | xmlChar *ret = NULL; |
| 1579 | xmlEntityPtr ent; |
| 1580 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1581 | if (list == NULL) |
| 1582 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1583 | |
| 1584 | while (node != NULL) { |
| 1585 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1586 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1587 | if (inLine) { |
| 1588 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1589 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1590 | xmlChar *buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1591 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1592 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
| 1593 | if (buffer != NULL) { |
| 1594 | ret = xmlStrcat(ret, buffer); |
| 1595 | xmlFree(buffer); |
| 1596 | } |
| 1597 | } |
| 1598 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1599 | if (inLine) { |
| 1600 | ent = xmlGetDocEntity(doc, node->name); |
| 1601 | if (ent != NULL) { |
| 1602 | xmlChar *buffer; |
| 1603 | |
| 1604 | /* an entity content can be any "well balanced chunk", |
| 1605 | * i.e. the result of the content [43] production: |
| 1606 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1607 | * So it can contain text, CDATA section or nested |
| 1608 | * entity reference nodes (among others). |
| 1609 | * -> we recursive call xmlNodeListGetString() |
| 1610 | * which handles these types */ |
| 1611 | buffer = xmlNodeListGetString(doc, ent->children, 1); |
| 1612 | if (buffer != NULL) { |
| 1613 | ret = xmlStrcat(ret, buffer); |
| 1614 | xmlFree(buffer); |
| 1615 | } |
| 1616 | } else { |
| 1617 | ret = xmlStrcat(ret, node->content); |
| 1618 | } |
| 1619 | } else { |
| 1620 | xmlChar buf[2]; |
| 1621 | |
| 1622 | buf[0] = '&'; |
| 1623 | buf[1] = 0; |
| 1624 | ret = xmlStrncat(ret, buf, 1); |
| 1625 | ret = xmlStrcat(ret, node->name); |
| 1626 | buf[0] = ';'; |
| 1627 | buf[1] = 0; |
| 1628 | ret = xmlStrncat(ret, buf, 1); |
| 1629 | } |
| 1630 | } |
| 1631 | #if 0 |
| 1632 | else { |
| 1633 | xmlGenericError(xmlGenericErrorContext, |
| 1634 | "xmlGetNodeListString : invalid node type %d\n", |
| 1635 | node->type); |
| 1636 | } |
| 1637 | #endif |
| 1638 | node = node->next; |
| 1639 | } |
| 1640 | return (ret); |
| 1641 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1642 | |
| 1643 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1644 | /** |
| 1645 | * xmlNodeListGetRawString: |
| 1646 | * @doc: the document |
| 1647 | * @list: a Node list |
| 1648 | * @inLine: should we replace entity contents or show their external form |
| 1649 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1650 | * Builds the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1651 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 1652 | * this function doesn't do any character encoding handling. |
| 1653 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1654 | * Returns a pointer to the string copy, the caller must free it with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1655 | */ |
| 1656 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1657 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1658 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1659 | xmlNodePtr node = list; |
| 1660 | xmlChar *ret = NULL; |
| 1661 | xmlEntityPtr ent; |
| 1662 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1663 | if (list == NULL) |
| 1664 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1665 | |
| 1666 | while (node != NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1667 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1668 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1669 | if (inLine) { |
| 1670 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1671 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1672 | xmlChar *buffer; |
| 1673 | |
| 1674 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 1675 | if (buffer != NULL) { |
| 1676 | ret = xmlStrcat(ret, buffer); |
| 1677 | xmlFree(buffer); |
| 1678 | } |
| 1679 | } |
| 1680 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1681 | if (inLine) { |
| 1682 | ent = xmlGetDocEntity(doc, node->name); |
| 1683 | if (ent != NULL) { |
| 1684 | xmlChar *buffer; |
| 1685 | |
| 1686 | /* an entity content can be any "well balanced chunk", |
| 1687 | * i.e. the result of the content [43] production: |
| 1688 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1689 | * So it can contain text, CDATA section or nested |
| 1690 | * entity reference nodes (among others). |
| 1691 | * -> we recursive call xmlNodeListGetRawString() |
| 1692 | * which handles these types */ |
| 1693 | buffer = |
| 1694 | xmlNodeListGetRawString(doc, ent->children, 1); |
| 1695 | if (buffer != NULL) { |
| 1696 | ret = xmlStrcat(ret, buffer); |
| 1697 | xmlFree(buffer); |
| 1698 | } |
| 1699 | } else { |
| 1700 | ret = xmlStrcat(ret, node->content); |
| 1701 | } |
| 1702 | } else { |
| 1703 | xmlChar buf[2]; |
| 1704 | |
| 1705 | buf[0] = '&'; |
| 1706 | buf[1] = 0; |
| 1707 | ret = xmlStrncat(ret, buf, 1); |
| 1708 | ret = xmlStrcat(ret, node->name); |
| 1709 | buf[0] = ';'; |
| 1710 | buf[1] = 0; |
| 1711 | ret = xmlStrncat(ret, buf, 1); |
| 1712 | } |
| 1713 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1714 | #if 0 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1715 | else { |
| 1716 | xmlGenericError(xmlGenericErrorContext, |
| 1717 | "xmlGetNodeListString : invalid node type %d\n", |
| 1718 | node->type); |
| 1719 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1720 | #endif |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1721 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1722 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1723 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1724 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1725 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1726 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1727 | static xmlAttrPtr |
| 1728 | xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns, |
| 1729 | const xmlChar * name, const xmlChar * value, |
| 1730 | int eatname) |
| 1731 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1732 | xmlAttrPtr cur; |
| 1733 | xmlDocPtr doc = NULL; |
| 1734 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1735 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1736 | if (eatname == 1) |
| 1737 | xmlFree((xmlChar *) name); |
| 1738 | return (NULL); |
| 1739 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1740 | |
| 1741 | /* |
| 1742 | * Allocate a new property and fill the fields. |
| 1743 | */ |
| 1744 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1745 | if (cur == NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1746 | if (eatname == 1) |
| 1747 | xmlFree((xmlChar *) name); |
| 1748 | xmlTreeErrMemory("building attribute"); |
| 1749 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1750 | } |
| 1751 | memset(cur, 0, sizeof(xmlAttr)); |
| 1752 | cur->type = XML_ATTRIBUTE_NODE; |
| 1753 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1754 | cur->parent = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1755 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1756 | doc = node->doc; |
| 1757 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1758 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1759 | cur->ns = ns; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1760 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1761 | if (eatname == 0) { |
| 1762 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1763 | cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1); |
| 1764 | else |
| 1765 | cur->name = xmlStrdup(name); |
| 1766 | } else |
| 1767 | cur->name = name; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1768 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1769 | if (value != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1770 | xmlChar *buffer; |
| 1771 | xmlNodePtr tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1772 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1773 | buffer = xmlEncodeEntitiesReentrant(doc, value); |
| 1774 | cur->children = xmlStringGetNodeList(doc, buffer); |
| 1775 | cur->last = NULL; |
| 1776 | tmp = cur->children; |
| 1777 | while (tmp != NULL) { |
| 1778 | tmp->parent = (xmlNodePtr) cur; |
| 1779 | if (tmp->next == NULL) |
| 1780 | cur->last = tmp; |
| 1781 | tmp = tmp->next; |
| 1782 | } |
| 1783 | xmlFree(buffer); |
| 1784 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1785 | |
| 1786 | /* |
| 1787 | * Add it at the end to preserve parsing order ... |
| 1788 | */ |
| 1789 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1790 | if (node->properties == NULL) { |
| 1791 | node->properties = cur; |
| 1792 | } else { |
| 1793 | xmlAttrPtr prev = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1794 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1795 | while (prev->next != NULL) |
| 1796 | prev = prev->next; |
| 1797 | prev->next = cur; |
| 1798 | cur->prev = prev; |
| 1799 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1800 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1801 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1802 | if (xmlIsID((node == NULL) ? NULL : node->doc, node, cur) == 1) |
| 1803 | xmlAddID(NULL, node->doc, value, cur); |
| 1804 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1805 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1806 | xmlRegisterNodeDefaultValue((xmlNodePtr) cur); |
| 1807 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1808 | } |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1809 | |
| 1810 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 1811 | defined(LIBXML_SCHEMAS_ENABLED) |
| 1812 | /** |
| 1813 | * xmlNewProp: |
| 1814 | * @node: the holding node |
| 1815 | * @name: the name of the attribute |
| 1816 | * @value: the value of the attribute |
| 1817 | * |
| 1818 | * Create a new property carried by a node. |
| 1819 | * Returns a pointer to the attribute |
| 1820 | */ |
| 1821 | xmlAttrPtr |
| 1822 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 1823 | |
| 1824 | if (name == NULL) { |
| 1825 | #ifdef DEBUG_TREE |
| 1826 | xmlGenericError(xmlGenericErrorContext, |
| 1827 | "xmlNewProp : name == NULL\n"); |
| 1828 | #endif |
| 1829 | return(NULL); |
| 1830 | } |
| 1831 | |
| 1832 | return xmlNewPropInternal(node, NULL, name, value, 0); |
| 1833 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1834 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1835 | |
| 1836 | /** |
| 1837 | * xmlNewNsProp: |
| 1838 | * @node: the holding node |
| 1839 | * @ns: the namespace |
| 1840 | * @name: the name of the attribute |
| 1841 | * @value: the value of the attribute |
| 1842 | * |
| 1843 | * Create a new property tagged with a namespace and carried by a node. |
| 1844 | * Returns a pointer to the attribute |
| 1845 | */ |
| 1846 | xmlAttrPtr |
| 1847 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 1848 | const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1849 | |
| 1850 | if (name == NULL) { |
| 1851 | #ifdef DEBUG_TREE |
| 1852 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1853 | "xmlNewNsProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1854 | #endif |
| 1855 | return(NULL); |
| 1856 | } |
| 1857 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1858 | return xmlNewPropInternal(node, ns, name, value, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1862 | * xmlNewNsPropEatName: |
| 1863 | * @node: the holding node |
| 1864 | * @ns: the namespace |
| 1865 | * @name: the name of the attribute |
| 1866 | * @value: the value of the attribute |
| 1867 | * |
| 1868 | * Create a new property tagged with a namespace and carried by a node. |
| 1869 | * Returns a pointer to the attribute |
| 1870 | */ |
| 1871 | xmlAttrPtr |
| 1872 | xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name, |
| 1873 | const xmlChar *value) { |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1874 | |
| 1875 | if (name == NULL) { |
| 1876 | #ifdef DEBUG_TREE |
| 1877 | xmlGenericError(xmlGenericErrorContext, |
| 1878 | "xmlNewNsPropEatName : name == NULL\n"); |
| 1879 | #endif |
| 1880 | return(NULL); |
| 1881 | } |
| 1882 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1883 | return xmlNewPropInternal(node, ns, name, value, 1); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1887 | * xmlNewDocProp: |
| 1888 | * @doc: the document |
| 1889 | * @name: the name of the attribute |
| 1890 | * @value: the value of the attribute |
| 1891 | * |
| 1892 | * Create a new property carried by a document. |
| 1893 | * Returns a pointer to the attribute |
| 1894 | */ |
| 1895 | xmlAttrPtr |
| 1896 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
| 1897 | xmlAttrPtr cur; |
| 1898 | |
| 1899 | if (name == NULL) { |
| 1900 | #ifdef DEBUG_TREE |
| 1901 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1902 | "xmlNewDocProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1903 | #endif |
| 1904 | return(NULL); |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Allocate a new property and fill the fields. |
| 1909 | */ |
| 1910 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1911 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1912 | xmlTreeErrMemory("building attribute"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1913 | return(NULL); |
| 1914 | } |
| 1915 | memset(cur, 0, sizeof(xmlAttr)); |
| 1916 | cur->type = XML_ATTRIBUTE_NODE; |
| 1917 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 1918 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1919 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 1920 | else |
| 1921 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1922 | cur->doc = doc; |
| 1923 | if (value != NULL) { |
| 1924 | xmlNodePtr tmp; |
| 1925 | |
| 1926 | cur->children = xmlStringGetNodeList(doc, value); |
| 1927 | cur->last = NULL; |
| 1928 | |
| 1929 | tmp = cur->children; |
| 1930 | while (tmp != NULL) { |
| 1931 | tmp->parent = (xmlNodePtr) cur; |
| 1932 | if (tmp->next == NULL) |
| 1933 | cur->last = tmp; |
| 1934 | tmp = tmp->next; |
| 1935 | } |
| 1936 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1937 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1938 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1939 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1940 | return(cur); |
| 1941 | } |
| 1942 | |
| 1943 | /** |
| 1944 | * xmlFreePropList: |
| 1945 | * @cur: the first property in the list |
| 1946 | * |
| 1947 | * Free a property and all its siblings, all the children are freed too. |
| 1948 | */ |
| 1949 | void |
| 1950 | xmlFreePropList(xmlAttrPtr cur) { |
| 1951 | xmlAttrPtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1952 | if (cur == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1953 | while (cur != NULL) { |
| 1954 | next = cur->next; |
| 1955 | xmlFreeProp(cur); |
| 1956 | cur = next; |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | /** |
| 1961 | * xmlFreeProp: |
| 1962 | * @cur: an attribute |
| 1963 | * |
| 1964 | * Free one attribute, all the content is freed too |
| 1965 | */ |
| 1966 | void |
| 1967 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1968 | xmlDictPtr dict = NULL; |
| 1969 | if (cur == NULL) return; |
| 1970 | |
| 1971 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1972 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1973 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1974 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1975 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1976 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | da6f4af | 2005-06-20 17:17:54 +0000 | [diff] [blame] | 1977 | if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) { |
| 1978 | xmlRemoveID(cur->doc, cur); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1979 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1980 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1981 | DICT_FREE(cur->name) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1982 | xmlFree(cur); |
| 1983 | } |
| 1984 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1985 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1986 | /** |
| 1987 | * xmlRemoveProp: |
| 1988 | * @cur: an attribute |
| 1989 | * |
| 1990 | * Unlink and free one attribute, all the content is freed too |
| 1991 | * Note this doesn't work for namespace definition attributes |
| 1992 | * |
| 1993 | * Returns 0 if success and -1 in case of error. |
| 1994 | */ |
| 1995 | int |
| 1996 | xmlRemoveProp(xmlAttrPtr cur) { |
| 1997 | xmlAttrPtr tmp; |
| 1998 | if (cur == NULL) { |
| 1999 | #ifdef DEBUG_TREE |
| 2000 | xmlGenericError(xmlGenericErrorContext, |
| 2001 | "xmlRemoveProp : cur == NULL\n"); |
| 2002 | #endif |
| 2003 | return(-1); |
| 2004 | } |
| 2005 | if (cur->parent == NULL) { |
| 2006 | #ifdef DEBUG_TREE |
| 2007 | xmlGenericError(xmlGenericErrorContext, |
| 2008 | "xmlRemoveProp : cur->parent == NULL\n"); |
| 2009 | #endif |
| 2010 | return(-1); |
| 2011 | } |
| 2012 | tmp = cur->parent->properties; |
| 2013 | if (tmp == cur) { |
| 2014 | cur->parent->properties = cur->next; |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2015 | if (cur->next != NULL) |
| 2016 | cur->next->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2017 | xmlFreeProp(cur); |
| 2018 | return(0); |
| 2019 | } |
| 2020 | while (tmp != NULL) { |
| 2021 | if (tmp->next == cur) { |
| 2022 | tmp->next = cur->next; |
| 2023 | if (tmp->next != NULL) |
| 2024 | tmp->next->prev = tmp; |
| 2025 | xmlFreeProp(cur); |
| 2026 | return(0); |
| 2027 | } |
| 2028 | tmp = tmp->next; |
| 2029 | } |
| 2030 | #ifdef DEBUG_TREE |
| 2031 | xmlGenericError(xmlGenericErrorContext, |
| 2032 | "xmlRemoveProp : attribute not owned by its node\n"); |
| 2033 | #endif |
| 2034 | return(-1); |
| 2035 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2036 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2037 | |
| 2038 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2039 | * xmlNewDocPI: |
| 2040 | * @doc: the target document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2041 | * @name: the processing instruction name |
| 2042 | * @content: the PI content |
| 2043 | * |
| 2044 | * Creation of a processing instruction element. |
| 2045 | * Returns a pointer to the new node object. |
| 2046 | */ |
| 2047 | xmlNodePtr |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2048 | xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2049 | xmlNodePtr cur; |
| 2050 | |
| 2051 | if (name == NULL) { |
| 2052 | #ifdef DEBUG_TREE |
| 2053 | xmlGenericError(xmlGenericErrorContext, |
| 2054 | "xmlNewPI : name == NULL\n"); |
| 2055 | #endif |
| 2056 | return(NULL); |
| 2057 | } |
| 2058 | |
| 2059 | /* |
| 2060 | * Allocate a new node and fill the fields. |
| 2061 | */ |
| 2062 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2063 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2064 | xmlTreeErrMemory("building PI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2065 | return(NULL); |
| 2066 | } |
| 2067 | memset(cur, 0, sizeof(xmlNode)); |
| 2068 | cur->type = XML_PI_NODE; |
| 2069 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2070 | if ((doc != NULL) && (doc->dict != NULL)) |
| 2071 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 2072 | else |
| 2073 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2074 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2075 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2076 | } |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 2077 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2078 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2079 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2080 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2081 | return(cur); |
| 2082 | } |
| 2083 | |
| 2084 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2085 | * xmlNewPI: |
| 2086 | * @name: the processing instruction name |
| 2087 | * @content: the PI content |
| 2088 | * |
| 2089 | * Creation of a processing instruction element. |
| 2090 | * Use xmlDocNewPI preferably to get string interning |
| 2091 | * |
| 2092 | * Returns a pointer to the new node object. |
| 2093 | */ |
| 2094 | xmlNodePtr |
| 2095 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
| 2096 | return(xmlNewDocPI(NULL, name, content)); |
| 2097 | } |
| 2098 | |
| 2099 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2100 | * xmlNewNode: |
| 2101 | * @ns: namespace if any |
| 2102 | * @name: the node name |
| 2103 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2104 | * Creation of a new node element. @ns is optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2105 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2106 | * Returns a pointer to the new node object. Uses xmlStrdup() to make |
| 2107 | * copy of @name. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2108 | */ |
| 2109 | xmlNodePtr |
| 2110 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 2111 | xmlNodePtr cur; |
| 2112 | |
| 2113 | if (name == NULL) { |
| 2114 | #ifdef DEBUG_TREE |
| 2115 | xmlGenericError(xmlGenericErrorContext, |
| 2116 | "xmlNewNode : name == NULL\n"); |
| 2117 | #endif |
| 2118 | return(NULL); |
| 2119 | } |
| 2120 | |
| 2121 | /* |
| 2122 | * Allocate a new node and fill the fields. |
| 2123 | */ |
| 2124 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2125 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2126 | xmlTreeErrMemory("building node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2127 | return(NULL); |
| 2128 | } |
| 2129 | memset(cur, 0, sizeof(xmlNode)); |
| 2130 | cur->type = XML_ELEMENT_NODE; |
| 2131 | |
| 2132 | cur->name = xmlStrdup(name); |
| 2133 | cur->ns = ns; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2134 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2135 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2136 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2137 | return(cur); |
| 2138 | } |
| 2139 | |
| 2140 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2141 | * xmlNewNodeEatName: |
| 2142 | * @ns: namespace if any |
| 2143 | * @name: the node name |
| 2144 | * |
| 2145 | * Creation of a new node element. @ns is optional (NULL). |
| 2146 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2147 | * Returns a pointer to the new node object, with pointer @name as |
| 2148 | * new node's name. Use xmlNewNode() if a copy of @name string is |
| 2149 | * is needed as new node's name. |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2150 | */ |
| 2151 | xmlNodePtr |
| 2152 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
| 2153 | xmlNodePtr cur; |
| 2154 | |
| 2155 | if (name == NULL) { |
| 2156 | #ifdef DEBUG_TREE |
| 2157 | xmlGenericError(xmlGenericErrorContext, |
| 2158 | "xmlNewNode : name == NULL\n"); |
| 2159 | #endif |
| 2160 | return(NULL); |
| 2161 | } |
| 2162 | |
| 2163 | /* |
| 2164 | * Allocate a new node and fill the fields. |
| 2165 | */ |
| 2166 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2167 | if (cur == NULL) { |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 2168 | xmlFree(name); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2169 | xmlTreeErrMemory("building node"); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2170 | return(NULL); |
| 2171 | } |
| 2172 | memset(cur, 0, sizeof(xmlNode)); |
| 2173 | cur->type = XML_ELEMENT_NODE; |
| 2174 | |
| 2175 | cur->name = name; |
| 2176 | cur->ns = ns; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2177 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2178 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2179 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2180 | return(cur); |
| 2181 | } |
| 2182 | |
| 2183 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2184 | * xmlNewDocNode: |
| 2185 | * @doc: the document |
| 2186 | * @ns: namespace if any |
| 2187 | * @name: the node name |
| 2188 | * @content: the XML text content if any |
| 2189 | * |
| 2190 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2191 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2192 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2193 | * references, but XML special chars need to be escaped first by using |
| 2194 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2195 | * need entities support. |
| 2196 | * |
| 2197 | * Returns a pointer to the new node object. |
| 2198 | */ |
| 2199 | xmlNodePtr |
| 2200 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2201 | const xmlChar *name, const xmlChar *content) { |
| 2202 | xmlNodePtr cur; |
| 2203 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2204 | if ((doc != NULL) && (doc->dict != NULL)) |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2205 | cur = xmlNewNodeEatName(ns, (xmlChar *) |
| 2206 | xmlDictLookup(doc->dict, name, -1)); |
| 2207 | else |
| 2208 | cur = xmlNewNode(ns, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2209 | if (cur != NULL) { |
| 2210 | cur->doc = doc; |
| 2211 | if (content != NULL) { |
| 2212 | cur->children = xmlStringGetNodeList(doc, content); |
| 2213 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2214 | } |
| 2215 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2216 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2217 | return(cur); |
| 2218 | } |
| 2219 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2220 | /** |
| 2221 | * xmlNewDocNodeEatName: |
| 2222 | * @doc: the document |
| 2223 | * @ns: namespace if any |
| 2224 | * @name: the node name |
| 2225 | * @content: the XML text content if any |
| 2226 | * |
| 2227 | * Creation of a new node element within a document. @ns and @content |
| 2228 | * are optional (NULL). |
| 2229 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2230 | * references, but XML special chars need to be escaped first by using |
| 2231 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2232 | * need entities support. |
| 2233 | * |
| 2234 | * Returns a pointer to the new node object. |
| 2235 | */ |
| 2236 | xmlNodePtr |
| 2237 | xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns, |
| 2238 | xmlChar *name, const xmlChar *content) { |
| 2239 | xmlNodePtr cur; |
| 2240 | |
| 2241 | cur = xmlNewNodeEatName(ns, name); |
| 2242 | if (cur != NULL) { |
| 2243 | cur->doc = doc; |
| 2244 | if (content != NULL) { |
| 2245 | cur->children = xmlStringGetNodeList(doc, content); |
| 2246 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2247 | } |
| 2248 | } |
| 2249 | return(cur); |
| 2250 | } |
| 2251 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2252 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2253 | /** |
| 2254 | * xmlNewDocRawNode: |
| 2255 | * @doc: the document |
| 2256 | * @ns: namespace if any |
| 2257 | * @name: the node name |
| 2258 | * @content: the text content if any |
| 2259 | * |
| 2260 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2261 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2262 | * |
| 2263 | * Returns a pointer to the new node object. |
| 2264 | */ |
| 2265 | xmlNodePtr |
| 2266 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2267 | const xmlChar *name, const xmlChar *content) { |
| 2268 | xmlNodePtr cur; |
| 2269 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2270 | cur = xmlNewDocNode(doc, ns, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2271 | if (cur != NULL) { |
| 2272 | cur->doc = doc; |
| 2273 | if (content != NULL) { |
| 2274 | cur->children = xmlNewDocText(doc, content); |
| 2275 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2276 | } |
| 2277 | } |
| 2278 | return(cur); |
| 2279 | } |
| 2280 | |
| 2281 | /** |
| 2282 | * xmlNewDocFragment: |
| 2283 | * @doc: the document owning the fragment |
| 2284 | * |
| 2285 | * Creation of a new Fragment node. |
| 2286 | * Returns a pointer to the new node object. |
| 2287 | */ |
| 2288 | xmlNodePtr |
| 2289 | xmlNewDocFragment(xmlDocPtr doc) { |
| 2290 | xmlNodePtr cur; |
| 2291 | |
| 2292 | /* |
| 2293 | * Allocate a new DocumentFragment node and fill the fields. |
| 2294 | */ |
| 2295 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2296 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2297 | xmlTreeErrMemory("building fragment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2298 | return(NULL); |
| 2299 | } |
| 2300 | memset(cur, 0, sizeof(xmlNode)); |
| 2301 | cur->type = XML_DOCUMENT_FRAG_NODE; |
| 2302 | |
| 2303 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2304 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2305 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2306 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2307 | return(cur); |
| 2308 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2309 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2310 | |
| 2311 | /** |
| 2312 | * xmlNewText: |
| 2313 | * @content: the text content |
| 2314 | * |
| 2315 | * Creation of a new text node. |
| 2316 | * Returns a pointer to the new node object. |
| 2317 | */ |
| 2318 | xmlNodePtr |
| 2319 | xmlNewText(const xmlChar *content) { |
| 2320 | xmlNodePtr cur; |
| 2321 | |
| 2322 | /* |
| 2323 | * Allocate a new node and fill the fields. |
| 2324 | */ |
| 2325 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2326 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2327 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2328 | return(NULL); |
| 2329 | } |
| 2330 | memset(cur, 0, sizeof(xmlNode)); |
| 2331 | cur->type = XML_TEXT_NODE; |
| 2332 | |
| 2333 | cur->name = xmlStringText; |
| 2334 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2335 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2336 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2337 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2338 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2339 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2340 | return(cur); |
| 2341 | } |
| 2342 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2343 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2344 | /** |
| 2345 | * xmlNewTextChild: |
| 2346 | * @parent: the parent node |
| 2347 | * @ns: a namespace if any |
| 2348 | * @name: the name of the child |
| 2349 | * @content: the text content of the child if any. |
| 2350 | * |
| 2351 | * Creation of a new child element, added at the end of @parent children list. |
MST 2003 John Fleck | e1f7049 | 2003-12-20 23:43:28 +0000 | [diff] [blame] | 2352 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2353 | * created element inherits the namespace of @parent. If @content is non NULL, |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2354 | * a child TEXT node will be created containing the string @content. |
MST 2003 John Fleck | 8b03bc5 | 2003-12-17 03:45:01 +0000 | [diff] [blame] | 2355 | * NOTE: Use xmlNewChild() if @content will contain entities that need to be |
| 2356 | * preserved. Use this function, xmlNewTextChild(), if you need to ensure that |
| 2357 | * reserved XML chars that might appear in @content, such as the ampersand, |
| 2358 | * greater-than or less-than signs, are automatically replaced by their XML |
| 2359 | * escaped entity representations. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2360 | * |
| 2361 | * Returns a pointer to the new node object. |
| 2362 | */ |
| 2363 | xmlNodePtr |
| 2364 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2365 | const xmlChar *name, const xmlChar *content) { |
| 2366 | xmlNodePtr cur, prev; |
| 2367 | |
| 2368 | if (parent == NULL) { |
| 2369 | #ifdef DEBUG_TREE |
| 2370 | xmlGenericError(xmlGenericErrorContext, |
| 2371 | "xmlNewTextChild : parent == NULL\n"); |
| 2372 | #endif |
| 2373 | return(NULL); |
| 2374 | } |
| 2375 | |
| 2376 | if (name == NULL) { |
| 2377 | #ifdef DEBUG_TREE |
| 2378 | xmlGenericError(xmlGenericErrorContext, |
| 2379 | "xmlNewTextChild : name == NULL\n"); |
| 2380 | #endif |
| 2381 | return(NULL); |
| 2382 | } |
| 2383 | |
| 2384 | /* |
| 2385 | * Allocate a new node |
| 2386 | */ |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 2387 | if (parent->type == XML_ELEMENT_NODE) { |
| 2388 | if (ns == NULL) |
| 2389 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 2390 | else |
| 2391 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 2392 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2393 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2394 | if (ns == NULL) |
| 2395 | cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content); |
| 2396 | else |
| 2397 | cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content); |
| 2398 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2399 | cur = xmlNewDocRawNode( parent->doc, ns, name, content); |
| 2400 | } else { |
| 2401 | return(NULL); |
| 2402 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2403 | if (cur == NULL) return(NULL); |
| 2404 | |
| 2405 | /* |
| 2406 | * add the new element at the end of the children list. |
| 2407 | */ |
| 2408 | cur->type = XML_ELEMENT_NODE; |
| 2409 | cur->parent = parent; |
| 2410 | cur->doc = parent->doc; |
| 2411 | if (parent->children == NULL) { |
| 2412 | parent->children = cur; |
| 2413 | parent->last = cur; |
| 2414 | } else { |
| 2415 | prev = parent->last; |
| 2416 | prev->next = cur; |
| 2417 | cur->prev = prev; |
| 2418 | parent->last = cur; |
| 2419 | } |
| 2420 | |
| 2421 | return(cur); |
| 2422 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2423 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2424 | |
| 2425 | /** |
| 2426 | * xmlNewCharRef: |
| 2427 | * @doc: the document |
| 2428 | * @name: the char ref string, starting with # or "&# ... ;" |
| 2429 | * |
| 2430 | * Creation of a new character reference node. |
| 2431 | * Returns a pointer to the new node object. |
| 2432 | */ |
| 2433 | xmlNodePtr |
| 2434 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 2435 | xmlNodePtr cur; |
| 2436 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2437 | if (name == NULL) |
| 2438 | return(NULL); |
| 2439 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2440 | /* |
| 2441 | * Allocate a new node and fill the fields. |
| 2442 | */ |
| 2443 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2444 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2445 | xmlTreeErrMemory("building character reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2446 | return(NULL); |
| 2447 | } |
| 2448 | memset(cur, 0, sizeof(xmlNode)); |
| 2449 | cur->type = XML_ENTITY_REF_NODE; |
| 2450 | |
| 2451 | cur->doc = doc; |
| 2452 | if (name[0] == '&') { |
| 2453 | int len; |
| 2454 | name++; |
| 2455 | len = xmlStrlen(name); |
| 2456 | if (name[len - 1] == ';') |
| 2457 | cur->name = xmlStrndup(name, len - 1); |
| 2458 | else |
| 2459 | cur->name = xmlStrndup(name, len); |
| 2460 | } else |
| 2461 | cur->name = xmlStrdup(name); |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2462 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2463 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2464 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2465 | return(cur); |
| 2466 | } |
| 2467 | |
| 2468 | /** |
| 2469 | * xmlNewReference: |
| 2470 | * @doc: the document |
| 2471 | * @name: the reference name, or the reference string with & and ; |
| 2472 | * |
| 2473 | * Creation of a new reference node. |
| 2474 | * Returns a pointer to the new node object. |
| 2475 | */ |
| 2476 | xmlNodePtr |
| 2477 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 2478 | xmlNodePtr cur; |
| 2479 | xmlEntityPtr ent; |
| 2480 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2481 | if (name == NULL) |
| 2482 | return(NULL); |
| 2483 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2484 | /* |
| 2485 | * Allocate a new node and fill the fields. |
| 2486 | */ |
| 2487 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2488 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2489 | xmlTreeErrMemory("building reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2490 | return(NULL); |
| 2491 | } |
| 2492 | memset(cur, 0, sizeof(xmlNode)); |
| 2493 | cur->type = XML_ENTITY_REF_NODE; |
| 2494 | |
| 2495 | cur->doc = doc; |
| 2496 | if (name[0] == '&') { |
| 2497 | int len; |
| 2498 | name++; |
| 2499 | len = xmlStrlen(name); |
| 2500 | if (name[len - 1] == ';') |
| 2501 | cur->name = xmlStrndup(name, len - 1); |
| 2502 | else |
| 2503 | cur->name = xmlStrndup(name, len); |
| 2504 | } else |
| 2505 | cur->name = xmlStrdup(name); |
| 2506 | |
| 2507 | ent = xmlGetDocEntity(doc, cur->name); |
| 2508 | if (ent != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2509 | cur->content = ent->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2510 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2511 | * The parent pointer in entity is a DTD pointer and thus is NOT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2512 | * updated. Not sure if this is 100% correct. |
| 2513 | * -George |
| 2514 | */ |
| 2515 | cur->children = (xmlNodePtr) ent; |
| 2516 | cur->last = (xmlNodePtr) ent; |
| 2517 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2518 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2519 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2520 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2521 | return(cur); |
| 2522 | } |
| 2523 | |
| 2524 | /** |
| 2525 | * xmlNewDocText: |
| 2526 | * @doc: the document |
| 2527 | * @content: the text content |
| 2528 | * |
| 2529 | * Creation of a new text node within a document. |
| 2530 | * Returns a pointer to the new node object. |
| 2531 | */ |
| 2532 | xmlNodePtr |
| 2533 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 2534 | xmlNodePtr cur; |
| 2535 | |
| 2536 | cur = xmlNewText(content); |
| 2537 | if (cur != NULL) cur->doc = doc; |
| 2538 | return(cur); |
| 2539 | } |
| 2540 | |
| 2541 | /** |
| 2542 | * xmlNewTextLen: |
| 2543 | * @content: the text content |
| 2544 | * @len: the text len. |
| 2545 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2546 | * Creation of a new text node with an extra parameter for the content's length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2547 | * Returns a pointer to the new node object. |
| 2548 | */ |
| 2549 | xmlNodePtr |
| 2550 | xmlNewTextLen(const xmlChar *content, int len) { |
| 2551 | xmlNodePtr cur; |
| 2552 | |
| 2553 | /* |
| 2554 | * Allocate a new node and fill the fields. |
| 2555 | */ |
| 2556 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2557 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2558 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2559 | return(NULL); |
| 2560 | } |
| 2561 | memset(cur, 0, sizeof(xmlNode)); |
| 2562 | cur->type = XML_TEXT_NODE; |
| 2563 | |
| 2564 | cur->name = xmlStringText; |
| 2565 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2566 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2567 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2568 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2569 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2570 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2571 | return(cur); |
| 2572 | } |
| 2573 | |
| 2574 | /** |
| 2575 | * xmlNewDocTextLen: |
| 2576 | * @doc: the document |
| 2577 | * @content: the text content |
| 2578 | * @len: the text len. |
| 2579 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2580 | * Creation of a new text node with an extra content length parameter. The |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2581 | * text node pertain to a given document. |
| 2582 | * Returns a pointer to the new node object. |
| 2583 | */ |
| 2584 | xmlNodePtr |
| 2585 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2586 | xmlNodePtr cur; |
| 2587 | |
| 2588 | cur = xmlNewTextLen(content, len); |
| 2589 | if (cur != NULL) cur->doc = doc; |
| 2590 | return(cur); |
| 2591 | } |
| 2592 | |
| 2593 | /** |
| 2594 | * xmlNewComment: |
| 2595 | * @content: the comment content |
| 2596 | * |
| 2597 | * Creation of a new node containing a comment. |
| 2598 | * Returns a pointer to the new node object. |
| 2599 | */ |
| 2600 | xmlNodePtr |
| 2601 | xmlNewComment(const xmlChar *content) { |
| 2602 | xmlNodePtr cur; |
| 2603 | |
| 2604 | /* |
| 2605 | * Allocate a new node and fill the fields. |
| 2606 | */ |
| 2607 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2608 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2609 | xmlTreeErrMemory("building comment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2610 | return(NULL); |
| 2611 | } |
| 2612 | memset(cur, 0, sizeof(xmlNode)); |
| 2613 | cur->type = XML_COMMENT_NODE; |
| 2614 | |
| 2615 | cur->name = xmlStringComment; |
| 2616 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2617 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2618 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2619 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2620 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2621 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2622 | return(cur); |
| 2623 | } |
| 2624 | |
| 2625 | /** |
| 2626 | * xmlNewCDataBlock: |
| 2627 | * @doc: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2628 | * @content: the CDATA block content content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2629 | * @len: the length of the block |
| 2630 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2631 | * Creation of a new node containing a CDATA block. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2632 | * Returns a pointer to the new node object. |
| 2633 | */ |
| 2634 | xmlNodePtr |
| 2635 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2636 | xmlNodePtr cur; |
| 2637 | |
| 2638 | /* |
| 2639 | * Allocate a new node and fill the fields. |
| 2640 | */ |
| 2641 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2642 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2643 | xmlTreeErrMemory("building CDATA"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2644 | return(NULL); |
| 2645 | } |
| 2646 | memset(cur, 0, sizeof(xmlNode)); |
| 2647 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2648 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2649 | |
| 2650 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2651 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2652 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2653 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2654 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2655 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2656 | return(cur); |
| 2657 | } |
| 2658 | |
| 2659 | /** |
| 2660 | * xmlNewDocComment: |
| 2661 | * @doc: the document |
| 2662 | * @content: the comment content |
| 2663 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2664 | * Creation of a new node containing a comment within a document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2665 | * Returns a pointer to the new node object. |
| 2666 | */ |
| 2667 | xmlNodePtr |
| 2668 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 2669 | xmlNodePtr cur; |
| 2670 | |
| 2671 | cur = xmlNewComment(content); |
| 2672 | if (cur != NULL) cur->doc = doc; |
| 2673 | return(cur); |
| 2674 | } |
| 2675 | |
| 2676 | /** |
| 2677 | * xmlSetTreeDoc: |
| 2678 | * @tree: the top element |
| 2679 | * @doc: the document |
| 2680 | * |
| 2681 | * update all nodes under the tree to point to the right document |
| 2682 | */ |
| 2683 | void |
| 2684 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2685 | xmlAttrPtr prop; |
| 2686 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2687 | if (tree == NULL) |
| 2688 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2689 | if (tree->doc != doc) { |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2690 | if(tree->type == XML_ELEMENT_NODE) { |
| 2691 | prop = tree->properties; |
| 2692 | while (prop != NULL) { |
| 2693 | prop->doc = doc; |
| 2694 | xmlSetListDoc(prop->children, doc); |
| 2695 | prop = prop->next; |
| 2696 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2697 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2698 | if (tree->children != NULL) |
| 2699 | xmlSetListDoc(tree->children, doc); |
| 2700 | tree->doc = doc; |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | /** |
| 2705 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2706 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2707 | * @doc: the document |
| 2708 | * |
| 2709 | * update all nodes in the list to point to the right document |
| 2710 | */ |
| 2711 | void |
| 2712 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2713 | xmlNodePtr cur; |
| 2714 | |
| 2715 | if (list == NULL) |
| 2716 | return; |
| 2717 | cur = list; |
| 2718 | while (cur != NULL) { |
| 2719 | if (cur->doc != doc) |
| 2720 | xmlSetTreeDoc(cur, doc); |
| 2721 | cur = cur->next; |
| 2722 | } |
| 2723 | } |
| 2724 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 2725 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2726 | /** |
| 2727 | * xmlNewChild: |
| 2728 | * @parent: the parent node |
| 2729 | * @ns: a namespace if any |
| 2730 | * @name: the name of the child |
| 2731 | * @content: the XML content of the child if any. |
| 2732 | * |
| 2733 | * Creation of a new child element, added at the end of @parent children list. |
MST 2003 John Fleck | e1f7049 | 2003-12-20 23:43:28 +0000 | [diff] [blame] | 2734 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2735 | * created element inherits the namespace of @parent. If @content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2736 | * a child list containing the TEXTs and ENTITY_REFs node will be created. |
MST 2003 John Fleck | 8b03bc5 | 2003-12-17 03:45:01 +0000 | [diff] [blame] | 2737 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 2738 | * references. XML special chars must be escaped first by using |
| 2739 | * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2740 | * |
| 2741 | * Returns a pointer to the new node object. |
| 2742 | */ |
| 2743 | xmlNodePtr |
| 2744 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2745 | const xmlChar *name, const xmlChar *content) { |
| 2746 | xmlNodePtr cur, prev; |
| 2747 | |
| 2748 | if (parent == NULL) { |
| 2749 | #ifdef DEBUG_TREE |
| 2750 | xmlGenericError(xmlGenericErrorContext, |
| 2751 | "xmlNewChild : parent == NULL\n"); |
| 2752 | #endif |
| 2753 | return(NULL); |
| 2754 | } |
| 2755 | |
| 2756 | if (name == NULL) { |
| 2757 | #ifdef DEBUG_TREE |
| 2758 | xmlGenericError(xmlGenericErrorContext, |
| 2759 | "xmlNewChild : name == NULL\n"); |
| 2760 | #endif |
| 2761 | return(NULL); |
| 2762 | } |
| 2763 | |
| 2764 | /* |
| 2765 | * Allocate a new node |
| 2766 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2767 | if (parent->type == XML_ELEMENT_NODE) { |
| 2768 | if (ns == NULL) |
| 2769 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2770 | else |
| 2771 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2772 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2773 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2774 | if (ns == NULL) |
| 2775 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2776 | else |
| 2777 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2778 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2779 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2780 | } else { |
| 2781 | return(NULL); |
| 2782 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2783 | if (cur == NULL) return(NULL); |
| 2784 | |
| 2785 | /* |
| 2786 | * add the new element at the end of the children list. |
| 2787 | */ |
| 2788 | cur->type = XML_ELEMENT_NODE; |
| 2789 | cur->parent = parent; |
| 2790 | cur->doc = parent->doc; |
| 2791 | if (parent->children == NULL) { |
| 2792 | parent->children = cur; |
| 2793 | parent->last = cur; |
| 2794 | } else { |
| 2795 | prev = parent->last; |
| 2796 | prev->next = cur; |
| 2797 | cur->prev = prev; |
| 2798 | parent->last = cur; |
| 2799 | } |
| 2800 | |
| 2801 | return(cur); |
| 2802 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2803 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2804 | |
| 2805 | /** |
| 2806 | * xmlAddNextSibling: |
| 2807 | * @cur: the child node |
| 2808 | * @elem: the new node |
| 2809 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2810 | * Add a new node @elem as the next sibling of @cur |
| 2811 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2812 | * first unlinked from its existing context. |
| 2813 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2814 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2815 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2816 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2817 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2818 | */ |
| 2819 | xmlNodePtr |
| 2820 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2821 | if (cur == NULL) { |
| 2822 | #ifdef DEBUG_TREE |
| 2823 | xmlGenericError(xmlGenericErrorContext, |
| 2824 | "xmlAddNextSibling : cur == NULL\n"); |
| 2825 | #endif |
| 2826 | return(NULL); |
| 2827 | } |
| 2828 | if (elem == NULL) { |
| 2829 | #ifdef DEBUG_TREE |
| 2830 | xmlGenericError(xmlGenericErrorContext, |
| 2831 | "xmlAddNextSibling : elem == NULL\n"); |
| 2832 | #endif |
| 2833 | return(NULL); |
| 2834 | } |
| 2835 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2836 | if (cur == elem) { |
| 2837 | #ifdef DEBUG_TREE |
| 2838 | xmlGenericError(xmlGenericErrorContext, |
| 2839 | "xmlAddNextSibling : cur == elem\n"); |
| 2840 | #endif |
| 2841 | return(NULL); |
| 2842 | } |
| 2843 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2844 | xmlUnlinkNode(elem); |
| 2845 | |
| 2846 | if (elem->type == XML_TEXT_NODE) { |
| 2847 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2848 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2849 | xmlFreeNode(elem); |
| 2850 | return(cur); |
| 2851 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2852 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 2853 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2854 | xmlChar *tmp; |
| 2855 | |
| 2856 | tmp = xmlStrdup(elem->content); |
| 2857 | tmp = xmlStrcat(tmp, cur->next->content); |
| 2858 | xmlNodeSetContent(cur->next, tmp); |
| 2859 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2860 | xmlFreeNode(elem); |
| 2861 | return(cur->next); |
| 2862 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2863 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2864 | /* check if an attribute with the same name exists */ |
| 2865 | xmlAttrPtr attr; |
| 2866 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2867 | if (cur->type != XML_ATTRIBUTE_NODE) |
| 2868 | return(NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2869 | if (elem->ns == NULL) |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 2870 | attr = xmlHasNsProp(cur->parent, elem->name, NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2871 | else |
| 2872 | attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href); |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 2873 | /* elem has already been unlinked so can never be attr */ |
| 2874 | if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2875 | /* different instance, destroy it (attributes must be unique) */ |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2876 | xmlUnlinkNode((xmlNodePtr) attr); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2877 | xmlFreeProp(attr); |
| 2878 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | if (elem->doc != cur->doc) { |
| 2882 | xmlSetTreeDoc(elem, cur->doc); |
| 2883 | } |
| 2884 | elem->parent = cur->parent; |
| 2885 | elem->prev = cur; |
| 2886 | elem->next = cur->next; |
| 2887 | cur->next = elem; |
| 2888 | if (elem->next != NULL) |
| 2889 | elem->next->prev = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2890 | if ((elem->parent != NULL) && (elem->parent->last == cur) && (elem->type != XML_ATTRIBUTE_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2891 | elem->parent->last = elem; |
| 2892 | return(elem); |
| 2893 | } |
| 2894 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 2895 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 2896 | defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2897 | /** |
| 2898 | * xmlAddPrevSibling: |
| 2899 | * @cur: the child node |
| 2900 | * @elem: the new node |
| 2901 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2902 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2903 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2904 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2905 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2906 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 2907 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2908 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2909 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2910 | */ |
| 2911 | xmlNodePtr |
| 2912 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2913 | if (cur == NULL) { |
| 2914 | #ifdef DEBUG_TREE |
| 2915 | xmlGenericError(xmlGenericErrorContext, |
| 2916 | "xmlAddPrevSibling : cur == NULL\n"); |
| 2917 | #endif |
| 2918 | return(NULL); |
| 2919 | } |
| 2920 | if (elem == NULL) { |
| 2921 | #ifdef DEBUG_TREE |
| 2922 | xmlGenericError(xmlGenericErrorContext, |
| 2923 | "xmlAddPrevSibling : elem == NULL\n"); |
| 2924 | #endif |
| 2925 | return(NULL); |
| 2926 | } |
| 2927 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2928 | if (cur == elem) { |
| 2929 | #ifdef DEBUG_TREE |
| 2930 | xmlGenericError(xmlGenericErrorContext, |
| 2931 | "xmlAddPrevSibling : cur == elem\n"); |
| 2932 | #endif |
| 2933 | return(NULL); |
| 2934 | } |
| 2935 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2936 | xmlUnlinkNode(elem); |
| 2937 | |
| 2938 | if (elem->type == XML_TEXT_NODE) { |
| 2939 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2940 | xmlChar *tmp; |
| 2941 | |
| 2942 | tmp = xmlStrdup(elem->content); |
| 2943 | tmp = xmlStrcat(tmp, cur->content); |
| 2944 | xmlNodeSetContent(cur, tmp); |
| 2945 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2946 | xmlFreeNode(elem); |
| 2947 | return(cur); |
| 2948 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2949 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 2950 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2951 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2952 | xmlFreeNode(elem); |
| 2953 | return(cur->prev); |
| 2954 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2955 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2956 | /* check if an attribute with the same name exists */ |
| 2957 | xmlAttrPtr attr; |
| 2958 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2959 | if (cur->type != XML_ATTRIBUTE_NODE) |
| 2960 | return(NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2961 | if (elem->ns == NULL) |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 2962 | attr = xmlHasNsProp(cur->parent, elem->name, NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2963 | else |
| 2964 | attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href); |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 2965 | /* elem has already been unlinked so can never be attr */ |
| 2966 | if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2967 | /* different instance, destroy it (attributes must be unique) */ |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2968 | xmlUnlinkNode((xmlNodePtr) attr); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2969 | xmlFreeProp(attr); |
| 2970 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | if (elem->doc != cur->doc) { |
| 2974 | xmlSetTreeDoc(elem, cur->doc); |
| 2975 | } |
| 2976 | elem->parent = cur->parent; |
| 2977 | elem->next = cur; |
| 2978 | elem->prev = cur->prev; |
| 2979 | cur->prev = elem; |
| 2980 | if (elem->prev != NULL) |
| 2981 | elem->prev->next = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2982 | if (elem->parent != NULL) { |
| 2983 | if (elem->type == XML_ATTRIBUTE_NODE) { |
| 2984 | if (elem->parent->properties == (xmlAttrPtr) cur) { |
| 2985 | elem->parent->properties = (xmlAttrPtr) elem; |
| 2986 | } |
| 2987 | } else { |
| 2988 | if (elem->parent->children == cur) { |
| 2989 | elem->parent->children = elem; |
| 2990 | } |
| 2991 | } |
| 2992 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2993 | return(elem); |
| 2994 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2995 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2996 | |
| 2997 | /** |
| 2998 | * xmlAddSibling: |
| 2999 | * @cur: the child node |
| 3000 | * @elem: the new node |
| 3001 | * |
| 3002 | * Add a new element @elem to the list of siblings of @cur |
| 3003 | * merging adjacent TEXT nodes (@elem may be freed) |
| 3004 | * If the new element was already inserted in a document it is |
| 3005 | * first unlinked from its existing context. |
| 3006 | * |
| 3007 | * Returns the new element or NULL in case of error. |
| 3008 | */ |
| 3009 | xmlNodePtr |
| 3010 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 3011 | xmlNodePtr parent; |
| 3012 | |
| 3013 | if (cur == NULL) { |
| 3014 | #ifdef DEBUG_TREE |
| 3015 | xmlGenericError(xmlGenericErrorContext, |
| 3016 | "xmlAddSibling : cur == NULL\n"); |
| 3017 | #endif |
| 3018 | return(NULL); |
| 3019 | } |
| 3020 | |
| 3021 | if (elem == NULL) { |
| 3022 | #ifdef DEBUG_TREE |
| 3023 | xmlGenericError(xmlGenericErrorContext, |
| 3024 | "xmlAddSibling : elem == NULL\n"); |
| 3025 | #endif |
| 3026 | return(NULL); |
| 3027 | } |
| 3028 | |
| 3029 | /* |
| 3030 | * Constant time is we can rely on the ->parent->last to find |
| 3031 | * the last sibling. |
| 3032 | */ |
| 3033 | if ((cur->parent != NULL) && |
| 3034 | (cur->parent->children != NULL) && |
| 3035 | (cur->parent->last != NULL) && |
| 3036 | (cur->parent->last->next == NULL)) { |
| 3037 | cur = cur->parent->last; |
| 3038 | } else { |
| 3039 | while (cur->next != NULL) cur = cur->next; |
| 3040 | } |
| 3041 | |
| 3042 | xmlUnlinkNode(elem); |
| 3043 | |
Daniel Veillard | e22dd5c | 2003-10-29 12:53:27 +0000 | [diff] [blame] | 3044 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) && |
| 3045 | (cur->name == elem->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3046 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3047 | xmlFreeNode(elem); |
| 3048 | return(cur); |
| 3049 | } |
| 3050 | |
| 3051 | if (elem->doc != cur->doc) { |
| 3052 | xmlSetTreeDoc(elem, cur->doc); |
| 3053 | } |
| 3054 | parent = cur->parent; |
| 3055 | elem->prev = cur; |
| 3056 | elem->next = NULL; |
| 3057 | elem->parent = parent; |
| 3058 | cur->next = elem; |
| 3059 | if (parent != NULL) |
| 3060 | parent->last = elem; |
| 3061 | |
| 3062 | return(elem); |
| 3063 | } |
| 3064 | |
| 3065 | /** |
| 3066 | * xmlAddChildList: |
| 3067 | * @parent: the parent node |
| 3068 | * @cur: the first node in the list |
| 3069 | * |
| 3070 | * Add a list of node at the end of the child list of the parent |
| 3071 | * merging adjacent TEXT nodes (@cur may be freed) |
| 3072 | * |
| 3073 | * Returns the last child or NULL in case of error. |
| 3074 | */ |
| 3075 | xmlNodePtr |
| 3076 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 3077 | xmlNodePtr prev; |
| 3078 | |
| 3079 | if (parent == NULL) { |
| 3080 | #ifdef DEBUG_TREE |
| 3081 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3082 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3083 | #endif |
| 3084 | return(NULL); |
| 3085 | } |
| 3086 | |
| 3087 | if (cur == NULL) { |
| 3088 | #ifdef DEBUG_TREE |
| 3089 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3090 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3091 | #endif |
| 3092 | return(NULL); |
| 3093 | } |
| 3094 | |
| 3095 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 3096 | (cur->doc != parent->doc)) { |
| 3097 | #ifdef DEBUG_TREE |
| 3098 | xmlGenericError(xmlGenericErrorContext, |
| 3099 | "Elements moved to a different document\n"); |
| 3100 | #endif |
| 3101 | } |
| 3102 | |
| 3103 | /* |
| 3104 | * add the first element at the end of the children list. |
| 3105 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3106 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3107 | if (parent->children == NULL) { |
| 3108 | parent->children = cur; |
| 3109 | } else { |
| 3110 | /* |
| 3111 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 3112 | */ |
| 3113 | if ((cur->type == XML_TEXT_NODE) && |
| 3114 | (parent->last->type == XML_TEXT_NODE) && |
| 3115 | (cur->name == parent->last->name)) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3116 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3117 | /* |
| 3118 | * if it's the only child, nothing more to be done. |
| 3119 | */ |
| 3120 | if (cur->next == NULL) { |
| 3121 | xmlFreeNode(cur); |
| 3122 | return(parent->last); |
| 3123 | } |
| 3124 | prev = cur; |
| 3125 | cur = cur->next; |
| 3126 | xmlFreeNode(prev); |
| 3127 | } |
| 3128 | prev = parent->last; |
| 3129 | prev->next = cur; |
| 3130 | cur->prev = prev; |
| 3131 | } |
| 3132 | while (cur->next != NULL) { |
| 3133 | cur->parent = parent; |
| 3134 | if (cur->doc != parent->doc) { |
| 3135 | xmlSetTreeDoc(cur, parent->doc); |
| 3136 | } |
| 3137 | cur = cur->next; |
| 3138 | } |
| 3139 | cur->parent = parent; |
| 3140 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 3141 | parent->last = cur; |
| 3142 | |
| 3143 | return(cur); |
| 3144 | } |
| 3145 | |
| 3146 | /** |
| 3147 | * xmlAddChild: |
| 3148 | * @parent: the parent node |
| 3149 | * @cur: the child node |
| 3150 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3151 | * 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] | 3152 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3153 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
| 3154 | * If there is an attribute with equal name, it is first destroyed. |
| 3155 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3156 | * Returns the child or NULL in case of error. |
| 3157 | */ |
| 3158 | xmlNodePtr |
| 3159 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 3160 | xmlNodePtr prev; |
| 3161 | |
| 3162 | if (parent == NULL) { |
| 3163 | #ifdef DEBUG_TREE |
| 3164 | xmlGenericError(xmlGenericErrorContext, |
| 3165 | "xmlAddChild : parent == NULL\n"); |
| 3166 | #endif |
| 3167 | return(NULL); |
| 3168 | } |
| 3169 | |
| 3170 | if (cur == NULL) { |
| 3171 | #ifdef DEBUG_TREE |
| 3172 | xmlGenericError(xmlGenericErrorContext, |
| 3173 | "xmlAddChild : child == NULL\n"); |
| 3174 | #endif |
| 3175 | return(NULL); |
| 3176 | } |
| 3177 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3178 | if (parent == cur) { |
| 3179 | #ifdef DEBUG_TREE |
| 3180 | xmlGenericError(xmlGenericErrorContext, |
| 3181 | "xmlAddChild : parent == cur\n"); |
| 3182 | #endif |
| 3183 | return(NULL); |
| 3184 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3185 | /* |
| 3186 | * 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] | 3187 | * cur is then freed. |
| 3188 | */ |
| 3189 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3190 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3191 | (parent->content != NULL) && |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3192 | (parent->name == cur->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3193 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3194 | xmlFreeNode(cur); |
| 3195 | return(parent); |
| 3196 | } |
| 3197 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3198 | (parent->last->name == cur->name) && |
| 3199 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3200 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3201 | xmlFreeNode(cur); |
| 3202 | return(parent->last); |
| 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | /* |
| 3207 | * add the new element at the end of the children list. |
| 3208 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3209 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3210 | cur->parent = parent; |
| 3211 | if (cur->doc != parent->doc) { |
| 3212 | xmlSetTreeDoc(cur, parent->doc); |
| 3213 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3214 | /* this check prevents a loop on tree-traversions if a developer |
| 3215 | * tries to add a node to its parent multiple times |
| 3216 | */ |
| 3217 | if (prev == parent) |
| 3218 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3219 | |
| 3220 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3221 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3222 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3223 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3224 | (parent->content != NULL) && |
| 3225 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3226 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3227 | xmlFreeNode(cur); |
| 3228 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3229 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3230 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3231 | if (parent->type != XML_ELEMENT_NODE) |
| 3232 | return(NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3233 | if (parent->properties == NULL) { |
| 3234 | parent->properties = (xmlAttrPtr) cur; |
| 3235 | } else { |
| 3236 | /* check if an attribute with the same name exists */ |
| 3237 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3238 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3239 | if (cur->ns == NULL) |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3240 | lastattr = xmlHasNsProp(parent, cur->name, NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3241 | else |
| 3242 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3243 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3244 | /* different instance, destroy it (attributes must be unique) */ |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3245 | xmlUnlinkNode((xmlNodePtr) lastattr); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3246 | xmlFreeProp(lastattr); |
| 3247 | } |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3248 | if (lastattr == (xmlAttrPtr) cur) |
| 3249 | return(cur); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3250 | /* find the end */ |
| 3251 | lastattr = parent->properties; |
| 3252 | while (lastattr->next != NULL) { |
| 3253 | lastattr = lastattr->next; |
| 3254 | } |
| 3255 | lastattr->next = (xmlAttrPtr) cur; |
| 3256 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3257 | } |
| 3258 | } else { |
| 3259 | if (parent->children == NULL) { |
| 3260 | parent->children = cur; |
| 3261 | parent->last = cur; |
| 3262 | } else { |
| 3263 | prev = parent->last; |
| 3264 | prev->next = cur; |
| 3265 | cur->prev = prev; |
| 3266 | parent->last = cur; |
| 3267 | } |
| 3268 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3269 | return(cur); |
| 3270 | } |
| 3271 | |
| 3272 | /** |
| 3273 | * xmlGetLastChild: |
| 3274 | * @parent: the parent node |
| 3275 | * |
| 3276 | * Search the last child of a node. |
| 3277 | * Returns the last child or NULL if none. |
| 3278 | */ |
| 3279 | xmlNodePtr |
| 3280 | xmlGetLastChild(xmlNodePtr parent) { |
| 3281 | if (parent == NULL) { |
| 3282 | #ifdef DEBUG_TREE |
| 3283 | xmlGenericError(xmlGenericErrorContext, |
| 3284 | "xmlGetLastChild : parent == NULL\n"); |
| 3285 | #endif |
| 3286 | return(NULL); |
| 3287 | } |
| 3288 | return(parent->last); |
| 3289 | } |
| 3290 | |
| 3291 | /** |
| 3292 | * xmlFreeNodeList: |
| 3293 | * @cur: the first node in the list |
| 3294 | * |
| 3295 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3296 | * the children are freed too. |
| 3297 | */ |
| 3298 | void |
| 3299 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3300 | xmlNodePtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3301 | xmlDictPtr dict = NULL; |
| 3302 | |
| 3303 | if (cur == NULL) return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3304 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3305 | xmlFreeNsList((xmlNsPtr) cur); |
| 3306 | return; |
| 3307 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3308 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3309 | #ifdef LIBXML_DOCB_ENABLED |
| 3310 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3311 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3312 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3313 | xmlFreeDoc((xmlDocPtr) cur); |
| 3314 | return; |
| 3315 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3316 | if (cur->doc != NULL) dict = cur->doc->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3317 | while (cur != NULL) { |
| 3318 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3319 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3320 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3321 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3322 | xmlDeregisterNodeDefaultValue(cur); |
| 3323 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3324 | if ((cur->children != NULL) && |
| 3325 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3326 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3327 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3328 | (cur->type == XML_XINCLUDE_START) || |
| 3329 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3330 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3331 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3332 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3333 | (cur->type != XML_XINCLUDE_START) && |
| 3334 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3335 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3336 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3337 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3338 | } |
| 3339 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3340 | (cur->type == XML_XINCLUDE_START) || |
| 3341 | (cur->type == XML_XINCLUDE_END)) && |
| 3342 | (cur->nsDef != NULL)) |
| 3343 | xmlFreeNsList(cur->nsDef); |
| 3344 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3345 | /* |
| 3346 | * When a node is a text node or a comment, it uses a global static |
| 3347 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3348 | * Otherwise the node name might come from the document's |
| 3349 | * dictionnary |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3350 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3351 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3352 | (cur->type != XML_TEXT_NODE) && |
| 3353 | (cur->type != XML_COMMENT_NODE)) |
| 3354 | DICT_FREE(cur->name) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3355 | xmlFree(cur); |
| 3356 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3357 | cur = next; |
| 3358 | } |
| 3359 | } |
| 3360 | |
| 3361 | /** |
| 3362 | * xmlFreeNode: |
| 3363 | * @cur: the node |
| 3364 | * |
| 3365 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3366 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3367 | */ |
| 3368 | void |
| 3369 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3370 | xmlDictPtr dict = NULL; |
| 3371 | |
| 3372 | if (cur == NULL) return; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3373 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3374 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3375 | if (cur->type == XML_DTD_NODE) { |
| 3376 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3377 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3378 | } |
| 3379 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3380 | xmlFreeNs((xmlNsPtr) cur); |
| 3381 | return; |
| 3382 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3383 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3384 | xmlFreeProp((xmlAttrPtr) cur); |
| 3385 | return; |
| 3386 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3387 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3388 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3389 | xmlDeregisterNodeDefaultValue(cur); |
| 3390 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3391 | if (cur->doc != NULL) dict = cur->doc->dict; |
| 3392 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3393 | if ((cur->children != NULL) && |
| 3394 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3395 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3396 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3397 | (cur->type == XML_XINCLUDE_START) || |
| 3398 | (cur->type == XML_XINCLUDE_END)) && |
| 3399 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3400 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3401 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3402 | (cur->content != NULL) && |
| 3403 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3404 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3405 | (cur->type != XML_XINCLUDE_START) && |
| 3406 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3407 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3410 | /* |
| 3411 | * When a node is a text node or a comment, it uses a global static |
| 3412 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3413 | * Otherwise the node name might come from the document's dictionnary |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3414 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3415 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3416 | (cur->type != XML_TEXT_NODE) && |
| 3417 | (cur->type != XML_COMMENT_NODE)) |
| 3418 | DICT_FREE(cur->name) |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3419 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3420 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3421 | (cur->type == XML_XINCLUDE_START) || |
| 3422 | (cur->type == XML_XINCLUDE_END)) && |
| 3423 | (cur->nsDef != NULL)) |
| 3424 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3425 | xmlFree(cur); |
| 3426 | } |
| 3427 | |
| 3428 | /** |
| 3429 | * xmlUnlinkNode: |
| 3430 | * @cur: the node |
| 3431 | * |
| 3432 | * Unlink a node from it's current context, the node is not freed |
| 3433 | */ |
| 3434 | void |
| 3435 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3436 | if (cur == NULL) { |
| 3437 | #ifdef DEBUG_TREE |
| 3438 | xmlGenericError(xmlGenericErrorContext, |
| 3439 | "xmlUnlinkNode : node == NULL\n"); |
| 3440 | #endif |
| 3441 | return; |
| 3442 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3443 | if (cur->type == XML_DTD_NODE) { |
| 3444 | xmlDocPtr doc; |
| 3445 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3446 | if (doc != NULL) { |
| 3447 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3448 | doc->intSubset = NULL; |
| 3449 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3450 | doc->extSubset = NULL; |
| 3451 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3452 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3453 | if (cur->parent != NULL) { |
| 3454 | xmlNodePtr parent; |
| 3455 | parent = cur->parent; |
| 3456 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | da6f4af | 2005-06-20 17:17:54 +0000 | [diff] [blame] | 3457 | /* If attribute is an ID from subset then remove it */ |
| 3458 | if ((((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID) && |
| 3459 | xmlIsID(parent->doc, parent, (xmlAttrPtr) cur)) { |
| 3460 | xmlRemoveID(cur->doc, (xmlAttrPtr) cur); |
| 3461 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3462 | if (parent->properties == (xmlAttrPtr) cur) |
| 3463 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3464 | } else { |
| 3465 | if (parent->children == cur) |
| 3466 | parent->children = cur->next; |
| 3467 | if (parent->last == cur) |
| 3468 | parent->last = cur->prev; |
| 3469 | } |
| 3470 | cur->parent = NULL; |
| 3471 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3472 | if (cur->next != NULL) |
| 3473 | cur->next->prev = cur->prev; |
| 3474 | if (cur->prev != NULL) |
| 3475 | cur->prev->next = cur->next; |
| 3476 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 3479 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3480 | /** |
| 3481 | * xmlReplaceNode: |
| 3482 | * @old: the old node |
| 3483 | * @cur: the node |
| 3484 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 3485 | * Unlink the old node from its current context, prune the new one |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3486 | * 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] | 3487 | * first unlinked from its existing context. |
| 3488 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3489 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3490 | */ |
| 3491 | xmlNodePtr |
| 3492 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 3493 | if (old == cur) return(NULL); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3494 | if ((old == NULL) || (old->parent == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3495 | #ifdef DEBUG_TREE |
| 3496 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3497 | "xmlReplaceNode : old == NULL or without parent\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3498 | #endif |
| 3499 | return(NULL); |
| 3500 | } |
| 3501 | if (cur == NULL) { |
| 3502 | xmlUnlinkNode(old); |
| 3503 | return(old); |
| 3504 | } |
| 3505 | if (cur == old) { |
| 3506 | return(old); |
| 3507 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3508 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3509 | #ifdef DEBUG_TREE |
| 3510 | xmlGenericError(xmlGenericErrorContext, |
| 3511 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3512 | #endif |
| 3513 | return(old); |
| 3514 | } |
| 3515 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3516 | #ifdef DEBUG_TREE |
| 3517 | xmlGenericError(xmlGenericErrorContext, |
| 3518 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3519 | #endif |
| 3520 | return(old); |
| 3521 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3522 | xmlUnlinkNode(cur); |
Daniel Veillard | 64d7d12 | 2005-05-11 18:03:42 +0000 | [diff] [blame] | 3523 | xmlSetTreeDoc(cur, old->doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3524 | cur->parent = old->parent; |
| 3525 | cur->next = old->next; |
| 3526 | if (cur->next != NULL) |
| 3527 | cur->next->prev = cur; |
| 3528 | cur->prev = old->prev; |
| 3529 | if (cur->prev != NULL) |
| 3530 | cur->prev->next = cur; |
| 3531 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3532 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3533 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3534 | cur->parent->properties = ((xmlAttrPtr) cur); |
Daniel Veillard | da6f4af | 2005-06-20 17:17:54 +0000 | [diff] [blame] | 3535 | |
| 3536 | /* If old attribute is ID and defined in DTD then remove ID */ |
| 3537 | if ((((xmlAttrPtr) old)->atype == XML_ATTRIBUTE_ID) && |
| 3538 | xmlIsID(old->doc, old->parent, (xmlAttrPtr) old)) { |
| 3539 | xmlRemoveID(old->doc, (xmlAttrPtr) old); |
| 3540 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3541 | } else { |
| 3542 | if (cur->parent->children == old) |
| 3543 | cur->parent->children = cur; |
| 3544 | if (cur->parent->last == old) |
| 3545 | cur->parent->last = cur; |
| 3546 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3547 | } |
| 3548 | old->next = old->prev = NULL; |
| 3549 | old->parent = NULL; |
| 3550 | return(old); |
| 3551 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3552 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3553 | |
| 3554 | /************************************************************************ |
| 3555 | * * |
| 3556 | * Copy operations * |
| 3557 | * * |
| 3558 | ************************************************************************/ |
| 3559 | |
| 3560 | /** |
| 3561 | * xmlCopyNamespace: |
| 3562 | * @cur: the namespace |
| 3563 | * |
| 3564 | * Do a copy of the namespace. |
| 3565 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3566 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3567 | */ |
| 3568 | xmlNsPtr |
| 3569 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3570 | xmlNsPtr ret; |
| 3571 | |
| 3572 | if (cur == NULL) return(NULL); |
| 3573 | switch (cur->type) { |
| 3574 | case XML_LOCAL_NAMESPACE: |
| 3575 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3576 | break; |
| 3577 | default: |
| 3578 | #ifdef DEBUG_TREE |
| 3579 | xmlGenericError(xmlGenericErrorContext, |
| 3580 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3581 | #endif |
| 3582 | return(NULL); |
| 3583 | } |
| 3584 | return(ret); |
| 3585 | } |
| 3586 | |
| 3587 | /** |
| 3588 | * xmlCopyNamespaceList: |
| 3589 | * @cur: the first namespace |
| 3590 | * |
| 3591 | * Do a copy of an namespace list. |
| 3592 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3593 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3594 | */ |
| 3595 | xmlNsPtr |
| 3596 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3597 | xmlNsPtr ret = NULL; |
| 3598 | xmlNsPtr p = NULL,q; |
| 3599 | |
| 3600 | while (cur != NULL) { |
| 3601 | q = xmlCopyNamespace(cur); |
| 3602 | if (p == NULL) { |
| 3603 | ret = p = q; |
| 3604 | } else { |
| 3605 | p->next = q; |
| 3606 | p = q; |
| 3607 | } |
| 3608 | cur = cur->next; |
| 3609 | } |
| 3610 | return(ret); |
| 3611 | } |
| 3612 | |
| 3613 | static xmlNodePtr |
| 3614 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3615 | |
| 3616 | static xmlAttrPtr |
| 3617 | xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3618 | xmlAttrPtr ret; |
| 3619 | |
| 3620 | if (cur == NULL) return(NULL); |
| 3621 | if (target != NULL) |
| 3622 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3623 | else if (doc != NULL) |
| 3624 | ret = xmlNewDocProp(doc, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3625 | else if (cur->parent != NULL) |
| 3626 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 3627 | else if (cur->children != NULL) |
| 3628 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 3629 | else |
| 3630 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 3631 | if (ret == NULL) return(NULL); |
| 3632 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3633 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3634 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3635 | xmlNsPtr ns; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3636 | |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3637 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3638 | if (ns == NULL) { |
| 3639 | /* |
| 3640 | * Humm, we are copying an element whose namespace is defined |
| 3641 | * out of the new tree scope. Search it in the original tree |
| 3642 | * and add it at the top of the new tree |
| 3643 | */ |
| 3644 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 3645 | if (ns != NULL) { |
| 3646 | xmlNodePtr root = target; |
| 3647 | xmlNodePtr pred = NULL; |
| 3648 | |
| 3649 | while (root->parent != NULL) { |
| 3650 | pred = root; |
| 3651 | root = root->parent; |
| 3652 | } |
| 3653 | if (root == (xmlNodePtr) target->doc) { |
| 3654 | /* correct possibly cycling above the document elt */ |
| 3655 | root = pred; |
| 3656 | } |
| 3657 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 3658 | } |
| 3659 | } else { |
| 3660 | /* |
| 3661 | * we have to find something appropriate here since |
| 3662 | * we cant be sure, that the namespce we found is identified |
| 3663 | * by the prefix |
| 3664 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3665 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3666 | /* this is the nice case */ |
| 3667 | ret->ns = ns; |
| 3668 | } else { |
| 3669 | /* |
| 3670 | * we are in trouble: we need a new reconcilied namespace. |
| 3671 | * This is expensive |
| 3672 | */ |
| 3673 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 3674 | } |
| 3675 | } |
| 3676 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3677 | } else |
| 3678 | ret->ns = NULL; |
| 3679 | |
| 3680 | if (cur->children != NULL) { |
| 3681 | xmlNodePtr tmp; |
| 3682 | |
| 3683 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 3684 | ret->last = NULL; |
| 3685 | tmp = ret->children; |
| 3686 | while (tmp != NULL) { |
| 3687 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 3688 | if (tmp->next == NULL) |
| 3689 | ret->last = tmp; |
| 3690 | tmp = tmp->next; |
| 3691 | } |
| 3692 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3693 | /* |
| 3694 | * Try to handle IDs |
| 3695 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 3696 | if ((target!= NULL) && (cur!= NULL) && |
| 3697 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3698 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 3699 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 3700 | xmlChar *id; |
| 3701 | |
| 3702 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 3703 | if (id != NULL) { |
| 3704 | xmlAddID(NULL, target->doc, id, ret); |
| 3705 | xmlFree(id); |
| 3706 | } |
| 3707 | } |
| 3708 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3709 | return(ret); |
| 3710 | } |
| 3711 | |
| 3712 | /** |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3713 | * xmlCopyProp: |
| 3714 | * @target: the element where the attribute will be grafted |
| 3715 | * @cur: the attribute |
| 3716 | * |
| 3717 | * Do a copy of the attribute. |
| 3718 | * |
| 3719 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
| 3720 | */ |
| 3721 | xmlAttrPtr |
| 3722 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 3723 | return xmlCopyPropInternal(NULL, target, cur); |
| 3724 | } |
| 3725 | |
| 3726 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3727 | * xmlCopyPropList: |
| 3728 | * @target: the element where the attributes will be grafted |
| 3729 | * @cur: the first attribute |
| 3730 | * |
| 3731 | * Do a copy of an attribute list. |
| 3732 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3733 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3734 | */ |
| 3735 | xmlAttrPtr |
| 3736 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 3737 | xmlAttrPtr ret = NULL; |
| 3738 | xmlAttrPtr p = NULL,q; |
| 3739 | |
| 3740 | while (cur != NULL) { |
| 3741 | q = xmlCopyProp(target, cur); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 3742 | if (q == NULL) |
| 3743 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3744 | if (p == NULL) { |
| 3745 | ret = p = q; |
| 3746 | } else { |
| 3747 | p->next = q; |
| 3748 | q->prev = p; |
| 3749 | p = q; |
| 3750 | } |
| 3751 | cur = cur->next; |
| 3752 | } |
| 3753 | return(ret); |
| 3754 | } |
| 3755 | |
| 3756 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3757 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3758 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3759 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3760 | * tricky reason: namespaces. Doing a direct copy of a node |
| 3761 | * say RPM:Copyright without changing the namespace pointer to |
| 3762 | * something else can produce stale links. One way to do it is |
| 3763 | * to keep a reference counter but this doesn't work as soon |
| 3764 | * as one move the element or the subtree out of the scope of |
| 3765 | * the existing namespace. The actual solution seems to add |
| 3766 | * a copy of the namespace at the top of the copied tree if |
| 3767 | * not available in the subtree. |
| 3768 | * Hence two functions, the public front-end call the inner ones |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3769 | * The argument "recursive" normally indicates a recursive copy |
| 3770 | * of the node with values 0 (no) and 1 (yes). For XInclude, |
| 3771 | * however, we allow a value of 2 to indicate copy properties and |
| 3772 | * namespace info, but don't recurse on children. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3773 | */ |
| 3774 | |
| 3775 | static xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 3776 | xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3777 | int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3778 | xmlNodePtr ret; |
| 3779 | |
| 3780 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3781 | switch (node->type) { |
| 3782 | case XML_TEXT_NODE: |
| 3783 | case XML_CDATA_SECTION_NODE: |
| 3784 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 3785 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3786 | case XML_ENTITY_REF_NODE: |
| 3787 | case XML_ENTITY_NODE: |
| 3788 | case XML_PI_NODE: |
| 3789 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3790 | case XML_XINCLUDE_START: |
| 3791 | case XML_XINCLUDE_END: |
| 3792 | break; |
| 3793 | case XML_ATTRIBUTE_NODE: |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3794 | return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node)); |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3795 | case XML_NAMESPACE_DECL: |
| 3796 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
| 3797 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3798 | case XML_DOCUMENT_NODE: |
| 3799 | case XML_HTML_DOCUMENT_NODE: |
| 3800 | #ifdef LIBXML_DOCB_ENABLED |
| 3801 | case XML_DOCB_DOCUMENT_NODE: |
| 3802 | #endif |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3803 | #ifdef LIBXML_TREE_ENABLED |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3804 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended)); |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3805 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3806 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 3807 | case XML_NOTATION_NODE: |
| 3808 | case XML_DTD_NODE: |
| 3809 | case XML_ELEMENT_DECL: |
| 3810 | case XML_ATTRIBUTE_DECL: |
| 3811 | case XML_ENTITY_DECL: |
| 3812 | return(NULL); |
| 3813 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3814 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3815 | /* |
| 3816 | * Allocate a new node and fill the fields. |
| 3817 | */ |
| 3818 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 3819 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 3820 | xmlTreeErrMemory("copying node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3821 | return(NULL); |
| 3822 | } |
| 3823 | memset(ret, 0, sizeof(xmlNode)); |
| 3824 | ret->type = node->type; |
| 3825 | |
| 3826 | ret->doc = doc; |
| 3827 | ret->parent = parent; |
| 3828 | if (node->name == xmlStringText) |
| 3829 | ret->name = xmlStringText; |
| 3830 | else if (node->name == xmlStringTextNoenc) |
| 3831 | ret->name = xmlStringTextNoenc; |
| 3832 | else if (node->name == xmlStringComment) |
| 3833 | ret->name = xmlStringComment; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 3834 | else if (node->name != NULL) { |
| 3835 | if ((doc != NULL) && (doc->dict != NULL)) |
| 3836 | ret->name = xmlDictLookup(doc->dict, node->name, -1); |
| 3837 | else |
| 3838 | ret->name = xmlStrdup(node->name); |
| 3839 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3840 | if ((node->type != XML_ELEMENT_NODE) && |
| 3841 | (node->content != NULL) && |
| 3842 | (node->type != XML_ENTITY_REF_NODE) && |
| 3843 | (node->type != XML_XINCLUDE_END) && |
| 3844 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3845 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3846 | }else{ |
| 3847 | if (node->type == XML_ELEMENT_NODE) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 3848 | ret->line = node->line; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3849 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3850 | if (parent != NULL) { |
| 3851 | xmlNodePtr tmp; |
| 3852 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3853 | /* |
| 3854 | * this is a tricky part for the node register thing: |
| 3855 | * in case ret does get coalesced in xmlAddChild |
| 3856 | * the deregister-node callback is called; so we register ret now already |
| 3857 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3858 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3859 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 3860 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3861 | tmp = xmlAddChild(parent, ret); |
| 3862 | /* node could have coalesced */ |
| 3863 | if (tmp != ret) |
| 3864 | return(tmp); |
| 3865 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3866 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3867 | if (!extended) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3868 | goto out; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3869 | if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3870 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 3871 | |
| 3872 | if (node->ns != NULL) { |
| 3873 | xmlNsPtr ns; |
| 3874 | |
| 3875 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 3876 | if (ns == NULL) { |
| 3877 | /* |
| 3878 | * Humm, we are copying an element whose namespace is defined |
| 3879 | * out of the new tree scope. Search it in the original tree |
| 3880 | * and add it at the top of the new tree |
| 3881 | */ |
| 3882 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 3883 | if (ns != NULL) { |
| 3884 | xmlNodePtr root = ret; |
| 3885 | |
| 3886 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 3887 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3888 | } |
| 3889 | } else { |
| 3890 | /* |
| 3891 | * reference the existing namespace definition in our own tree. |
| 3892 | */ |
| 3893 | ret->ns = ns; |
| 3894 | } |
| 3895 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3896 | if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3897 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3898 | if (node->type == XML_ENTITY_REF_NODE) { |
| 3899 | if ((doc == NULL) || (node->doc != doc)) { |
| 3900 | /* |
| 3901 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3902 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3903 | * we cannot keep the reference. Try to find it in the |
| 3904 | * target document. |
| 3905 | */ |
| 3906 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 3907 | } else { |
| 3908 | ret->children = node->children; |
| 3909 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3910 | ret->last = ret->children; |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3911 | } else if ((node->children != NULL) && (extended != 2)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3912 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 3913 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 3914 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3915 | |
| 3916 | out: |
| 3917 | /* if parent != NULL we already registered the node above */ |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 3918 | if ((parent == NULL) && |
| 3919 | ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 3920 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3921 | return(ret); |
| 3922 | } |
| 3923 | |
| 3924 | static xmlNodePtr |
| 3925 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 3926 | xmlNodePtr ret = NULL; |
| 3927 | xmlNodePtr p = NULL,q; |
| 3928 | |
| 3929 | while (node != NULL) { |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3930 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 3931 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 3932 | if (doc == NULL) { |
| 3933 | node = node->next; |
| 3934 | continue; |
| 3935 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3936 | if (doc->intSubset == NULL) { |
| 3937 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
| 3938 | q->doc = doc; |
| 3939 | q->parent = parent; |
| 3940 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3941 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3942 | } else { |
| 3943 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 3944 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3945 | } |
| 3946 | } else |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3947 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 3948 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3949 | if (ret == NULL) { |
| 3950 | q->prev = NULL; |
| 3951 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 3952 | } else if (p != q) { |
| 3953 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3954 | p->next = q; |
| 3955 | q->prev = p; |
| 3956 | p = q; |
| 3957 | } |
| 3958 | node = node->next; |
| 3959 | } |
| 3960 | return(ret); |
| 3961 | } |
| 3962 | |
| 3963 | /** |
| 3964 | * xmlCopyNode: |
| 3965 | * @node: the node |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3966 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 3967 | * when applicable) |
| 3968 | * if 2 copy properties and namespaces (when applicable) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3969 | * |
| 3970 | * Do a copy of the node. |
| 3971 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3972 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3973 | */ |
| 3974 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3975 | xmlCopyNode(const xmlNodePtr node, int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3976 | xmlNodePtr ret; |
| 3977 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3978 | ret = xmlStaticCopyNode(node, NULL, NULL, extended); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3979 | return(ret); |
| 3980 | } |
| 3981 | |
| 3982 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3983 | * xmlDocCopyNode: |
| 3984 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3985 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3986 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 3987 | * when applicable) |
| 3988 | * if 2 copy properties and namespaces (when applicable) |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3989 | * |
| 3990 | * Do a copy of the node to a given document. |
| 3991 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3992 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3993 | */ |
| 3994 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3995 | xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3996 | xmlNodePtr ret; |
| 3997 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 3998 | ret = xmlStaticCopyNode(node, doc, NULL, extended); |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 3999 | return(ret); |
| 4000 | } |
| 4001 | |
| 4002 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4003 | * xmlDocCopyNodeList: |
| 4004 | * @doc: the target document |
| 4005 | * @node: the first node in the list. |
| 4006 | * |
| 4007 | * Do a recursive copy of the node list. |
| 4008 | * |
| 4009 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
| 4010 | */ |
| 4011 | xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) { |
| 4012 | xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL); |
| 4013 | return(ret); |
| 4014 | } |
| 4015 | |
| 4016 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4017 | * xmlCopyNodeList: |
| 4018 | * @node: the first node in the list. |
| 4019 | * |
| 4020 | * Do a recursive copy of the node list. |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4021 | * Use xmlDocCopyNodeList() if possible to ensure string interning. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4022 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4023 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4024 | */ |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 4025 | xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4026 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 4027 | return(ret); |
| 4028 | } |
| 4029 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4030 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4031 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4032 | * xmlCopyDtd: |
| 4033 | * @dtd: the dtd |
| 4034 | * |
| 4035 | * Do a copy of the dtd. |
| 4036 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4037 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4038 | */ |
| 4039 | xmlDtdPtr |
| 4040 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 4041 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4042 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4043 | |
| 4044 | if (dtd == NULL) return(NULL); |
| 4045 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 4046 | if (ret == NULL) return(NULL); |
| 4047 | if (dtd->entities != NULL) |
| 4048 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 4049 | (xmlEntitiesTablePtr) dtd->entities); |
| 4050 | if (dtd->notations != NULL) |
| 4051 | ret->notations = (void *) xmlCopyNotationTable( |
| 4052 | (xmlNotationTablePtr) dtd->notations); |
| 4053 | if (dtd->elements != NULL) |
| 4054 | ret->elements = (void *) xmlCopyElementTable( |
| 4055 | (xmlElementTablePtr) dtd->elements); |
| 4056 | if (dtd->attributes != NULL) |
| 4057 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 4058 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4059 | if (dtd->pentities != NULL) |
| 4060 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 4061 | (xmlEntitiesTablePtr) dtd->pentities); |
| 4062 | |
| 4063 | cur = dtd->children; |
| 4064 | while (cur != NULL) { |
| 4065 | q = NULL; |
| 4066 | |
| 4067 | if (cur->type == XML_ENTITY_DECL) { |
| 4068 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 4069 | switch (tmp->etype) { |
| 4070 | case XML_INTERNAL_GENERAL_ENTITY: |
| 4071 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 4072 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 4073 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 4074 | break; |
| 4075 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 4076 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 4077 | q = (xmlNodePtr) |
| 4078 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 4079 | break; |
| 4080 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 4081 | break; |
| 4082 | } |
| 4083 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 4084 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 4085 | q = (xmlNodePtr) |
| 4086 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 4087 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4088 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
| 4089 | q = (xmlNodePtr) |
| 4090 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 4091 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4092 | q = xmlCopyNode(cur, 0); |
| 4093 | } |
| 4094 | |
| 4095 | if (q == NULL) { |
| 4096 | cur = cur->next; |
| 4097 | continue; |
| 4098 | } |
| 4099 | |
| 4100 | if (p == NULL) |
| 4101 | ret->children = q; |
| 4102 | else |
| 4103 | p->next = q; |
| 4104 | |
| 4105 | q->prev = p; |
| 4106 | q->parent = (xmlNodePtr) ret; |
| 4107 | q->next = NULL; |
| 4108 | ret->last = q; |
| 4109 | p = q; |
| 4110 | cur = cur->next; |
| 4111 | } |
| 4112 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4113 | return(ret); |
| 4114 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4115 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4116 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4117 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4118 | /** |
| 4119 | * xmlCopyDoc: |
| 4120 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4121 | * @recursive: if not zero do a recursive copy. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4122 | * |
| 4123 | * 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] | 4124 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4125 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4126 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4127 | */ |
| 4128 | xmlDocPtr |
| 4129 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 4130 | xmlDocPtr ret; |
| 4131 | |
| 4132 | if (doc == NULL) return(NULL); |
| 4133 | ret = xmlNewDoc(doc->version); |
| 4134 | if (ret == NULL) return(NULL); |
| 4135 | if (doc->name != NULL) |
| 4136 | ret->name = xmlMemStrdup(doc->name); |
| 4137 | if (doc->encoding != NULL) |
| 4138 | ret->encoding = xmlStrdup(doc->encoding); |
Daniel Veillard | f59507d | 2005-01-27 17:26:49 +0000 | [diff] [blame] | 4139 | if (doc->URL != NULL) |
| 4140 | ret->URL = xmlStrdup(doc->URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4141 | ret->charset = doc->charset; |
| 4142 | ret->compression = doc->compression; |
| 4143 | ret->standalone = doc->standalone; |
| 4144 | if (!recursive) return(ret); |
| 4145 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4146 | ret->last = NULL; |
| 4147 | ret->children = NULL; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4148 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4149 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4150 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4151 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4152 | ret->intSubset->parent = ret; |
| 4153 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4154 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4155 | if (doc->oldNs != NULL) |
| 4156 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 4157 | if (doc->children != NULL) { |
| 4158 | xmlNodePtr tmp; |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4159 | |
| 4160 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 4161 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4162 | ret->last = NULL; |
| 4163 | tmp = ret->children; |
| 4164 | while (tmp != NULL) { |
| 4165 | if (tmp->next == NULL) |
| 4166 | ret->last = tmp; |
| 4167 | tmp = tmp->next; |
| 4168 | } |
| 4169 | } |
| 4170 | return(ret); |
| 4171 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4172 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4173 | |
| 4174 | /************************************************************************ |
| 4175 | * * |
| 4176 | * Content access functions * |
| 4177 | * * |
| 4178 | ************************************************************************/ |
| 4179 | |
| 4180 | /** |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4181 | * xmlGetLineNo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4182 | * @node: valid node |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4183 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 4184 | * Get line number of @node. This requires activation of this option |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4185 | * before invoking the parser by calling xmlLineNumbersDefault(1) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4186 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4187 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4188 | */ |
| 4189 | long |
| 4190 | xmlGetLineNo(xmlNodePtr node) |
| 4191 | { |
| 4192 | long result = -1; |
| 4193 | |
| 4194 | if (!node) |
| 4195 | return result; |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4196 | if ((node->type == XML_ELEMENT_NODE) || |
| 4197 | (node->type == XML_TEXT_NODE) || |
| 4198 | (node->type == XML_COMMENT_NODE) || |
| 4199 | (node->type == XML_PI_NODE)) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 4200 | result = (long) node->line; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4201 | else if ((node->prev != NULL) && |
| 4202 | ((node->prev->type == XML_ELEMENT_NODE) || |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4203 | (node->prev->type == XML_TEXT_NODE) || |
| 4204 | (node->prev->type == XML_COMMENT_NODE) || |
| 4205 | (node->prev->type == XML_PI_NODE))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4206 | result = xmlGetLineNo(node->prev); |
| 4207 | else if ((node->parent != NULL) && |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4208 | (node->parent->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4209 | result = xmlGetLineNo(node->parent); |
| 4210 | |
| 4211 | return result; |
| 4212 | } |
| 4213 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4214 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4215 | /** |
| 4216 | * xmlGetNodePath: |
| 4217 | * @node: a node |
| 4218 | * |
| 4219 | * Build a structure based Path for the given node |
| 4220 | * |
| 4221 | * Returns the new path or NULL in case of error. The caller must free |
| 4222 | * the returned string |
| 4223 | */ |
| 4224 | xmlChar * |
| 4225 | xmlGetNodePath(xmlNodePtr node) |
| 4226 | { |
| 4227 | xmlNodePtr cur, tmp, next; |
| 4228 | xmlChar *buffer = NULL, *temp; |
| 4229 | size_t buf_len; |
| 4230 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4231 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4232 | const char *name; |
| 4233 | char nametemp[100]; |
| 4234 | int occur = 0; |
| 4235 | |
| 4236 | if (node == NULL) |
| 4237 | return (NULL); |
| 4238 | |
| 4239 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4240 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4241 | if (buffer == NULL) { |
| 4242 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4243 | return (NULL); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4244 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4245 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4246 | if (buf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4247 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4248 | xmlFree(buffer); |
| 4249 | return (NULL); |
| 4250 | } |
| 4251 | |
| 4252 | buffer[0] = 0; |
| 4253 | cur = node; |
| 4254 | do { |
| 4255 | name = ""; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4256 | sep = "?"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4257 | occur = 0; |
| 4258 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4259 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4260 | if (buffer[0] == '/') |
| 4261 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4262 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4263 | next = NULL; |
| 4264 | } else if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4265 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4266 | name = (const char *) cur->name; |
| 4267 | if (cur->ns) { |
William M. Brack | 84d83e3 | 2003-12-23 03:45:17 +0000 | [diff] [blame] | 4268 | if (cur->ns->prefix != NULL) |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4269 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
| 4270 | (char *)cur->ns->prefix, (char *)cur->name); |
William M. Brack | 84d83e3 | 2003-12-23 03:45:17 +0000 | [diff] [blame] | 4271 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4272 | snprintf(nametemp, sizeof(nametemp) - 1, "%s", |
| 4273 | (char *)cur->name); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4274 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4275 | name = nametemp; |
| 4276 | } |
| 4277 | next = cur->parent; |
| 4278 | |
| 4279 | /* |
| 4280 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4281 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4282 | */ |
| 4283 | tmp = cur->prev; |
| 4284 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4285 | if ((tmp->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 0996a16 | 2005-02-05 14:00:10 +0000 | [diff] [blame] | 4286 | (xmlStrEqual(cur->name, tmp->name)) && |
| 4287 | ((tmp->ns == cur->ns) || |
| 4288 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
| 4289 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4290 | occur++; |
| 4291 | tmp = tmp->prev; |
| 4292 | } |
| 4293 | if (occur == 0) { |
| 4294 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4295 | while (tmp != NULL && occur == 0) { |
| 4296 | if ((tmp->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 0996a16 | 2005-02-05 14:00:10 +0000 | [diff] [blame] | 4297 | (xmlStrEqual(cur->name, tmp->name)) && |
| 4298 | ((tmp->ns == cur->ns) || |
| 4299 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
| 4300 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4301 | occur++; |
| 4302 | tmp = tmp->next; |
| 4303 | } |
| 4304 | if (occur != 0) |
| 4305 | occur = 1; |
| 4306 | } else |
| 4307 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4308 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4309 | sep = "/"; |
| 4310 | name = "comment()"; |
| 4311 | next = cur->parent; |
| 4312 | |
| 4313 | /* |
| 4314 | * Thumbler index computation |
| 4315 | */ |
| 4316 | tmp = cur->prev; |
| 4317 | while (tmp != NULL) { |
| 4318 | if (tmp->type == XML_COMMENT_NODE) |
| 4319 | occur++; |
| 4320 | tmp = tmp->prev; |
| 4321 | } |
| 4322 | if (occur == 0) { |
| 4323 | tmp = cur->next; |
| 4324 | while (tmp != NULL && occur == 0) { |
| 4325 | if (tmp->type == XML_COMMENT_NODE) |
| 4326 | occur++; |
| 4327 | tmp = tmp->next; |
| 4328 | } |
| 4329 | if (occur != 0) |
| 4330 | occur = 1; |
| 4331 | } else |
| 4332 | occur++; |
| 4333 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4334 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4335 | sep = "/"; |
| 4336 | name = "text()"; |
| 4337 | next = cur->parent; |
| 4338 | |
| 4339 | /* |
| 4340 | * Thumbler index computation |
| 4341 | */ |
| 4342 | tmp = cur->prev; |
| 4343 | while (tmp != NULL) { |
| 4344 | if ((cur->type == XML_TEXT_NODE) || |
| 4345 | (cur->type == XML_CDATA_SECTION_NODE)) |
| 4346 | occur++; |
| 4347 | tmp = tmp->prev; |
| 4348 | } |
| 4349 | if (occur == 0) { |
| 4350 | tmp = cur->next; |
| 4351 | while (tmp != NULL && occur == 0) { |
Daniel Veillard | 1f8658a | 2004-08-14 21:46:31 +0000 | [diff] [blame] | 4352 | if ((tmp->type == XML_TEXT_NODE) || |
| 4353 | (tmp->type == XML_CDATA_SECTION_NODE)) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4354 | occur++; |
| 4355 | tmp = tmp->next; |
| 4356 | } |
| 4357 | if (occur != 0) |
| 4358 | occur = 1; |
| 4359 | } else |
| 4360 | occur++; |
| 4361 | } else if (cur->type == XML_PI_NODE) { |
| 4362 | sep = "/"; |
| 4363 | snprintf(nametemp, sizeof(nametemp) - 1, |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4364 | "processing-instruction('%s')", (char *)cur->name); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4365 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4366 | name = nametemp; |
| 4367 | |
| 4368 | next = cur->parent; |
| 4369 | |
| 4370 | /* |
| 4371 | * Thumbler index computation |
| 4372 | */ |
| 4373 | tmp = cur->prev; |
| 4374 | while (tmp != NULL) { |
| 4375 | if ((tmp->type == XML_PI_NODE) && |
| 4376 | (xmlStrEqual(cur->name, tmp->name))) |
| 4377 | occur++; |
| 4378 | tmp = tmp->prev; |
| 4379 | } |
| 4380 | if (occur == 0) { |
| 4381 | tmp = cur->next; |
| 4382 | while (tmp != NULL && occur == 0) { |
| 4383 | if ((tmp->type == XML_PI_NODE) && |
| 4384 | (xmlStrEqual(cur->name, tmp->name))) |
| 4385 | occur++; |
| 4386 | tmp = tmp->next; |
| 4387 | } |
| 4388 | if (occur != 0) |
| 4389 | occur = 1; |
| 4390 | } else |
| 4391 | occur++; |
| 4392 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4393 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4394 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4395 | name = (const char *) (((xmlAttrPtr) cur)->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4396 | if (cur->ns) { |
| 4397 | if (cur->ns->prefix != NULL) |
| 4398 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
| 4399 | (char *)cur->ns->prefix, (char *)cur->name); |
| 4400 | else |
| 4401 | snprintf(nametemp, sizeof(nametemp) - 1, "%s", |
| 4402 | (char *)cur->name); |
| 4403 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4404 | name = nametemp; |
| 4405 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4406 | next = ((xmlAttrPtr) cur)->parent; |
| 4407 | } else { |
| 4408 | next = cur->parent; |
| 4409 | } |
| 4410 | |
| 4411 | /* |
| 4412 | * Make sure there is enough room |
| 4413 | */ |
| 4414 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4415 | buf_len = |
| 4416 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4417 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4418 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4419 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4420 | xmlFree(buf); |
| 4421 | xmlFree(buffer); |
| 4422 | return (NULL); |
| 4423 | } |
| 4424 | buffer = temp; |
| 4425 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4426 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4427 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4428 | xmlFree(buf); |
| 4429 | xmlFree(buffer); |
| 4430 | return (NULL); |
| 4431 | } |
| 4432 | buf = temp; |
| 4433 | } |
| 4434 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4435 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4436 | sep, name, (char *) buffer); |
| 4437 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4438 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4439 | sep, name, occur, (char *) buffer); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4440 | snprintf((char *) buffer, buf_len, "%s", (char *)buf); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4441 | cur = next; |
| 4442 | } while (cur != NULL); |
| 4443 | xmlFree(buf); |
| 4444 | return (buffer); |
| 4445 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4446 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4447 | |
| 4448 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4449 | * xmlDocGetRootElement: |
| 4450 | * @doc: the document |
| 4451 | * |
| 4452 | * Get the root element of the document (doc->children is a list |
| 4453 | * containing possibly comments, PIs, etc ...). |
| 4454 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4455 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4456 | */ |
| 4457 | xmlNodePtr |
| 4458 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 4459 | xmlNodePtr ret; |
| 4460 | |
| 4461 | if (doc == NULL) return(NULL); |
| 4462 | ret = doc->children; |
| 4463 | while (ret != NULL) { |
| 4464 | if (ret->type == XML_ELEMENT_NODE) |
| 4465 | return(ret); |
| 4466 | ret = ret->next; |
| 4467 | } |
| 4468 | return(ret); |
| 4469 | } |
| 4470 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4471 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4472 | /** |
| 4473 | * xmlDocSetRootElement: |
| 4474 | * @doc: the document |
| 4475 | * @root: the new document root element |
| 4476 | * |
| 4477 | * Set the root element of the document (doc->children is a list |
| 4478 | * containing possibly comments, PIs, etc ...). |
| 4479 | * |
| 4480 | * Returns the old root element if any was found |
| 4481 | */ |
| 4482 | xmlNodePtr |
| 4483 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4484 | xmlNodePtr old = NULL; |
| 4485 | |
| 4486 | if (doc == NULL) return(NULL); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4487 | if (root == NULL) |
| 4488 | return(NULL); |
| 4489 | xmlUnlinkNode(root); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 4490 | xmlSetTreeDoc(root, doc); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4491 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4492 | old = doc->children; |
| 4493 | while (old != NULL) { |
| 4494 | if (old->type == XML_ELEMENT_NODE) |
| 4495 | break; |
| 4496 | old = old->next; |
| 4497 | } |
| 4498 | if (old == NULL) { |
| 4499 | if (doc->children == NULL) { |
| 4500 | doc->children = root; |
| 4501 | doc->last = root; |
| 4502 | } else { |
| 4503 | xmlAddSibling(doc->children, root); |
| 4504 | } |
| 4505 | } else { |
| 4506 | xmlReplaceNode(old, root); |
| 4507 | } |
| 4508 | return(old); |
| 4509 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4510 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4511 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4512 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4513 | /** |
| 4514 | * xmlNodeSetLang: |
| 4515 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4516 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4517 | * |
| 4518 | * Set the language of a node, i.e. the values of the xml:lang |
| 4519 | * attribute. |
| 4520 | */ |
| 4521 | void |
| 4522 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4523 | xmlNsPtr ns; |
| 4524 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4525 | if (cur == NULL) return; |
| 4526 | switch(cur->type) { |
| 4527 | case XML_TEXT_NODE: |
| 4528 | case XML_CDATA_SECTION_NODE: |
| 4529 | case XML_COMMENT_NODE: |
| 4530 | case XML_DOCUMENT_NODE: |
| 4531 | case XML_DOCUMENT_TYPE_NODE: |
| 4532 | case XML_DOCUMENT_FRAG_NODE: |
| 4533 | case XML_NOTATION_NODE: |
| 4534 | case XML_HTML_DOCUMENT_NODE: |
| 4535 | case XML_DTD_NODE: |
| 4536 | case XML_ELEMENT_DECL: |
| 4537 | case XML_ATTRIBUTE_DECL: |
| 4538 | case XML_ENTITY_DECL: |
| 4539 | case XML_PI_NODE: |
| 4540 | case XML_ENTITY_REF_NODE: |
| 4541 | case XML_ENTITY_NODE: |
| 4542 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4543 | #ifdef LIBXML_DOCB_ENABLED |
| 4544 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4545 | #endif |
| 4546 | case XML_XINCLUDE_START: |
| 4547 | case XML_XINCLUDE_END: |
| 4548 | return; |
| 4549 | case XML_ELEMENT_NODE: |
| 4550 | case XML_ATTRIBUTE_NODE: |
| 4551 | break; |
| 4552 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4553 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4554 | if (ns == NULL) |
| 4555 | return; |
| 4556 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4557 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4558 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4559 | |
| 4560 | /** |
| 4561 | * xmlNodeGetLang: |
| 4562 | * @cur: the node being checked |
| 4563 | * |
| 4564 | * Searches the language of a node, i.e. the values of the xml:lang |
| 4565 | * attribute or the one carried by the nearest ancestor. |
| 4566 | * |
| 4567 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4568 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4569 | */ |
| 4570 | xmlChar * |
| 4571 | xmlNodeGetLang(xmlNodePtr cur) { |
| 4572 | xmlChar *lang; |
| 4573 | |
| 4574 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 4575 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4576 | if (lang != NULL) |
| 4577 | return(lang); |
| 4578 | cur = cur->parent; |
| 4579 | } |
| 4580 | return(NULL); |
| 4581 | } |
| 4582 | |
| 4583 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4584 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4585 | /** |
| 4586 | * xmlNodeSetSpacePreserve: |
| 4587 | * @cur: the node being changed |
| 4588 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 4589 | * |
| 4590 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 4591 | * value of the xml:space attribute. |
| 4592 | */ |
| 4593 | void |
| 4594 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4595 | xmlNsPtr ns; |
| 4596 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4597 | if (cur == NULL) return; |
| 4598 | switch(cur->type) { |
| 4599 | case XML_TEXT_NODE: |
| 4600 | case XML_CDATA_SECTION_NODE: |
| 4601 | case XML_COMMENT_NODE: |
| 4602 | case XML_DOCUMENT_NODE: |
| 4603 | case XML_DOCUMENT_TYPE_NODE: |
| 4604 | case XML_DOCUMENT_FRAG_NODE: |
| 4605 | case XML_NOTATION_NODE: |
| 4606 | case XML_HTML_DOCUMENT_NODE: |
| 4607 | case XML_DTD_NODE: |
| 4608 | case XML_ELEMENT_DECL: |
| 4609 | case XML_ATTRIBUTE_DECL: |
| 4610 | case XML_ENTITY_DECL: |
| 4611 | case XML_PI_NODE: |
| 4612 | case XML_ENTITY_REF_NODE: |
| 4613 | case XML_ENTITY_NODE: |
| 4614 | case XML_NAMESPACE_DECL: |
| 4615 | case XML_XINCLUDE_START: |
| 4616 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4617 | #ifdef LIBXML_DOCB_ENABLED |
| 4618 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4619 | #endif |
| 4620 | return; |
| 4621 | case XML_ELEMENT_NODE: |
| 4622 | case XML_ATTRIBUTE_NODE: |
| 4623 | break; |
| 4624 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4625 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4626 | if (ns == NULL) |
| 4627 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4628 | switch (val) { |
| 4629 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4630 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4631 | break; |
| 4632 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4633 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4634 | break; |
| 4635 | } |
| 4636 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4637 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4638 | |
| 4639 | /** |
| 4640 | * xmlNodeGetSpacePreserve: |
| 4641 | * @cur: the node being checked |
| 4642 | * |
| 4643 | * Searches the space preserving behaviour of a node, i.e. the values |
| 4644 | * of the xml:space attribute or the one carried by the nearest |
| 4645 | * ancestor. |
| 4646 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4647 | * 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] | 4648 | */ |
| 4649 | int |
| 4650 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 4651 | xmlChar *space; |
| 4652 | |
| 4653 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4654 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4655 | if (space != NULL) { |
| 4656 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 4657 | xmlFree(space); |
| 4658 | return(1); |
| 4659 | } |
| 4660 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 4661 | xmlFree(space); |
| 4662 | return(0); |
| 4663 | } |
| 4664 | xmlFree(space); |
| 4665 | } |
| 4666 | cur = cur->parent; |
| 4667 | } |
| 4668 | return(-1); |
| 4669 | } |
| 4670 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4671 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4672 | /** |
| 4673 | * xmlNodeSetName: |
| 4674 | * @cur: the node being changed |
| 4675 | * @name: the new tag name |
| 4676 | * |
| 4677 | * Set (or reset) the name of a node. |
| 4678 | */ |
| 4679 | void |
| 4680 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 4681 | xmlDocPtr doc; |
| 4682 | xmlDictPtr dict; |
| 4683 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4684 | if (cur == NULL) return; |
| 4685 | if (name == NULL) return; |
| 4686 | switch(cur->type) { |
| 4687 | case XML_TEXT_NODE: |
| 4688 | case XML_CDATA_SECTION_NODE: |
| 4689 | case XML_COMMENT_NODE: |
| 4690 | case XML_DOCUMENT_TYPE_NODE: |
| 4691 | case XML_DOCUMENT_FRAG_NODE: |
| 4692 | case XML_NOTATION_NODE: |
| 4693 | case XML_HTML_DOCUMENT_NODE: |
| 4694 | case XML_NAMESPACE_DECL: |
| 4695 | case XML_XINCLUDE_START: |
| 4696 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4697 | #ifdef LIBXML_DOCB_ENABLED |
| 4698 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4699 | #endif |
| 4700 | return; |
| 4701 | case XML_ELEMENT_NODE: |
| 4702 | case XML_ATTRIBUTE_NODE: |
| 4703 | case XML_PI_NODE: |
| 4704 | case XML_ENTITY_REF_NODE: |
| 4705 | case XML_ENTITY_NODE: |
| 4706 | case XML_DTD_NODE: |
| 4707 | case XML_DOCUMENT_NODE: |
| 4708 | case XML_ELEMENT_DECL: |
| 4709 | case XML_ATTRIBUTE_DECL: |
| 4710 | case XML_ENTITY_DECL: |
| 4711 | break; |
| 4712 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 4713 | doc = cur->doc; |
| 4714 | if (doc != NULL) |
| 4715 | dict = doc->dict; |
| 4716 | else |
| 4717 | dict = NULL; |
| 4718 | if (dict != NULL) { |
| 4719 | if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name))) |
| 4720 | xmlFree((xmlChar *) cur->name); |
| 4721 | cur->name = xmlDictLookup(dict, name, -1); |
| 4722 | } else { |
| 4723 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 4724 | cur->name = xmlStrdup(name); |
| 4725 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4726 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4727 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4728 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4729 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4730 | /** |
| 4731 | * xmlNodeSetBase: |
| 4732 | * @cur: the node being changed |
| 4733 | * @uri: the new base URI |
| 4734 | * |
| 4735 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 4736 | * xml:base attribute. |
| 4737 | */ |
| 4738 | void |
Daniel Veillard | f85ce8e | 2003-09-22 10:24:45 +0000 | [diff] [blame] | 4739 | xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4740 | xmlNsPtr ns; |
| 4741 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4742 | if (cur == NULL) return; |
| 4743 | switch(cur->type) { |
| 4744 | case XML_TEXT_NODE: |
| 4745 | case XML_CDATA_SECTION_NODE: |
| 4746 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4747 | case XML_DOCUMENT_TYPE_NODE: |
| 4748 | case XML_DOCUMENT_FRAG_NODE: |
| 4749 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4750 | case XML_DTD_NODE: |
| 4751 | case XML_ELEMENT_DECL: |
| 4752 | case XML_ATTRIBUTE_DECL: |
| 4753 | case XML_ENTITY_DECL: |
| 4754 | case XML_PI_NODE: |
| 4755 | case XML_ENTITY_REF_NODE: |
| 4756 | case XML_ENTITY_NODE: |
| 4757 | case XML_NAMESPACE_DECL: |
| 4758 | case XML_XINCLUDE_START: |
| 4759 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4760 | return; |
| 4761 | case XML_ELEMENT_NODE: |
| 4762 | case XML_ATTRIBUTE_NODE: |
| 4763 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 4764 | case XML_DOCUMENT_NODE: |
| 4765 | #ifdef LIBXML_DOCB_ENABLED |
| 4766 | case XML_DOCB_DOCUMENT_NODE: |
| 4767 | #endif |
| 4768 | case XML_HTML_DOCUMENT_NODE: { |
| 4769 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 4770 | |
| 4771 | if (doc->URL != NULL) |
| 4772 | xmlFree((xmlChar *) doc->URL); |
| 4773 | if (uri == NULL) |
| 4774 | doc->URL = NULL; |
| 4775 | else |
| 4776 | doc->URL = xmlStrdup(uri); |
| 4777 | return; |
| 4778 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4779 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4780 | |
| 4781 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4782 | if (ns == NULL) |
| 4783 | return; |
| 4784 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4785 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4786 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4787 | |
| 4788 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4789 | * xmlNodeGetBase: |
| 4790 | * @doc: the document the node pertains to |
| 4791 | * @cur: the node being checked |
| 4792 | * |
| 4793 | * Searches for the BASE URL. The code should work on both XML |
| 4794 | * and HTML document even if base mechanisms are completely different. |
| 4795 | * It returns the base as defined in RFC 2396 sections |
| 4796 | * 5.1.1. Base URI within Document Content |
| 4797 | * and |
| 4798 | * 5.1.2. Base URI from the Encapsulating Entity |
| 4799 | * However it does not return the document base (5.1.3), use |
| 4800 | * xmlDocumentGetBase() for this |
| 4801 | * |
| 4802 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4803 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4804 | */ |
| 4805 | xmlChar * |
| 4806 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4807 | xmlChar *oldbase = NULL; |
| 4808 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4809 | |
| 4810 | if ((cur == NULL) && (doc == NULL)) |
| 4811 | return(NULL); |
| 4812 | if (doc == NULL) doc = cur->doc; |
| 4813 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 4814 | cur = doc->children; |
| 4815 | while ((cur != NULL) && (cur->name != NULL)) { |
| 4816 | if (cur->type != XML_ELEMENT_NODE) { |
| 4817 | cur = cur->next; |
| 4818 | continue; |
| 4819 | } |
| 4820 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 4821 | cur = cur->children; |
| 4822 | continue; |
| 4823 | } |
| 4824 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 4825 | cur = cur->children; |
| 4826 | continue; |
| 4827 | } |
| 4828 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 4829 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 4830 | } |
| 4831 | cur = cur->next; |
| 4832 | } |
| 4833 | return(NULL); |
| 4834 | } |
| 4835 | while (cur != NULL) { |
| 4836 | if (cur->type == XML_ENTITY_DECL) { |
| 4837 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 4838 | return(xmlStrdup(ent->URI)); |
| 4839 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4840 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4841 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4842 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4843 | if (oldbase != NULL) { |
| 4844 | newbase = xmlBuildURI(oldbase, base); |
| 4845 | if (newbase != NULL) { |
| 4846 | xmlFree(oldbase); |
| 4847 | xmlFree(base); |
| 4848 | oldbase = newbase; |
| 4849 | } else { |
| 4850 | xmlFree(oldbase); |
| 4851 | xmlFree(base); |
| 4852 | return(NULL); |
| 4853 | } |
| 4854 | } else { |
| 4855 | oldbase = base; |
| 4856 | } |
| 4857 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 4858 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 4859 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 4860 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 4861 | } |
| 4862 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4863 | cur = cur->parent; |
| 4864 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 4865 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 4866 | if (oldbase == NULL) |
| 4867 | return(xmlStrdup(doc->URL)); |
| 4868 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 4869 | xmlFree(oldbase); |
| 4870 | return(newbase); |
| 4871 | } |
| 4872 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | /** |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 4876 | * xmlNodeBufGetContent: |
| 4877 | * @buffer: a buffer |
| 4878 | * @cur: the node being read |
| 4879 | * |
| 4880 | * Read the value of a node @cur, this can be either the text carried |
| 4881 | * directly by this node if it's a TEXT node or the aggregate string |
| 4882 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 4883 | * Entity references are substituted. |
| 4884 | * Fills up the buffer @buffer with this value |
| 4885 | * |
| 4886 | * Returns 0 in case of success and -1 in case of error. |
| 4887 | */ |
| 4888 | int |
| 4889 | xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur) |
| 4890 | { |
| 4891 | if ((cur == NULL) || (buffer == NULL)) return(-1); |
| 4892 | switch (cur->type) { |
| 4893 | case XML_CDATA_SECTION_NODE: |
| 4894 | case XML_TEXT_NODE: |
| 4895 | xmlBufferCat(buffer, cur->content); |
| 4896 | break; |
| 4897 | case XML_DOCUMENT_FRAG_NODE: |
| 4898 | case XML_ELEMENT_NODE:{ |
| 4899 | xmlNodePtr tmp = cur; |
| 4900 | |
| 4901 | while (tmp != NULL) { |
| 4902 | switch (tmp->type) { |
| 4903 | case XML_CDATA_SECTION_NODE: |
| 4904 | case XML_TEXT_NODE: |
| 4905 | if (tmp->content != NULL) |
| 4906 | xmlBufferCat(buffer, tmp->content); |
| 4907 | break; |
| 4908 | case XML_ENTITY_REF_NODE: |
| 4909 | xmlNodeBufGetContent(buffer, tmp->children); |
| 4910 | break; |
| 4911 | default: |
| 4912 | break; |
| 4913 | } |
| 4914 | /* |
| 4915 | * Skip to next node |
| 4916 | */ |
| 4917 | if (tmp->children != NULL) { |
| 4918 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4919 | tmp = tmp->children; |
| 4920 | continue; |
| 4921 | } |
| 4922 | } |
| 4923 | if (tmp == cur) |
| 4924 | break; |
| 4925 | |
| 4926 | if (tmp->next != NULL) { |
| 4927 | tmp = tmp->next; |
| 4928 | continue; |
| 4929 | } |
| 4930 | |
| 4931 | do { |
| 4932 | tmp = tmp->parent; |
| 4933 | if (tmp == NULL) |
| 4934 | break; |
| 4935 | if (tmp == cur) { |
| 4936 | tmp = NULL; |
| 4937 | break; |
| 4938 | } |
| 4939 | if (tmp->next != NULL) { |
| 4940 | tmp = tmp->next; |
| 4941 | break; |
| 4942 | } |
| 4943 | } while (tmp != NULL); |
| 4944 | } |
| 4945 | break; |
| 4946 | } |
| 4947 | case XML_ATTRIBUTE_NODE:{ |
| 4948 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 4949 | xmlNodePtr tmp = attr->children; |
| 4950 | |
| 4951 | while (tmp != NULL) { |
| 4952 | if (tmp->type == XML_TEXT_NODE) |
| 4953 | xmlBufferCat(buffer, tmp->content); |
| 4954 | else |
| 4955 | xmlNodeBufGetContent(buffer, tmp); |
| 4956 | tmp = tmp->next; |
| 4957 | } |
| 4958 | break; |
| 4959 | } |
| 4960 | case XML_COMMENT_NODE: |
| 4961 | case XML_PI_NODE: |
| 4962 | xmlBufferCat(buffer, cur->content); |
| 4963 | break; |
| 4964 | case XML_ENTITY_REF_NODE:{ |
| 4965 | xmlEntityPtr ent; |
| 4966 | xmlNodePtr tmp; |
| 4967 | |
| 4968 | /* lookup entity declaration */ |
| 4969 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 4970 | if (ent == NULL) |
| 4971 | return(-1); |
| 4972 | |
| 4973 | /* an entity content can be any "well balanced chunk", |
| 4974 | * i.e. the result of the content [43] production: |
| 4975 | * http://www.w3.org/TR/REC-xml#NT-content |
| 4976 | * -> we iterate through child nodes and recursive call |
| 4977 | * xmlNodeGetContent() which handles all possible node types */ |
| 4978 | tmp = ent->children; |
| 4979 | while (tmp) { |
| 4980 | xmlNodeBufGetContent(buffer, tmp); |
| 4981 | tmp = tmp->next; |
| 4982 | } |
| 4983 | break; |
| 4984 | } |
| 4985 | case XML_ENTITY_NODE: |
| 4986 | case XML_DOCUMENT_TYPE_NODE: |
| 4987 | case XML_NOTATION_NODE: |
| 4988 | case XML_DTD_NODE: |
| 4989 | case XML_XINCLUDE_START: |
| 4990 | case XML_XINCLUDE_END: |
| 4991 | break; |
| 4992 | case XML_DOCUMENT_NODE: |
| 4993 | #ifdef LIBXML_DOCB_ENABLED |
| 4994 | case XML_DOCB_DOCUMENT_NODE: |
| 4995 | #endif |
| 4996 | case XML_HTML_DOCUMENT_NODE: |
| 4997 | cur = cur->children; |
| 4998 | while (cur!= NULL) { |
| 4999 | if ((cur->type == XML_ELEMENT_NODE) || |
| 5000 | (cur->type == XML_TEXT_NODE) || |
| 5001 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 5002 | xmlNodeBufGetContent(buffer, cur); |
| 5003 | } |
| 5004 | cur = cur->next; |
| 5005 | } |
| 5006 | break; |
| 5007 | case XML_NAMESPACE_DECL: |
| 5008 | xmlBufferCat(buffer, ((xmlNsPtr) cur)->href); |
| 5009 | break; |
| 5010 | case XML_ELEMENT_DECL: |
| 5011 | case XML_ATTRIBUTE_DECL: |
| 5012 | case XML_ENTITY_DECL: |
| 5013 | break; |
| 5014 | } |
| 5015 | return(0); |
| 5016 | } |
| 5017 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5018 | * xmlNodeGetContent: |
| 5019 | * @cur: the node being read |
| 5020 | * |
| 5021 | * Read the value of a node, this can be either the text carried |
| 5022 | * directly by this node if it's a TEXT node or the aggregate string |
| 5023 | * 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] | 5024 | * Entity references are substituted. |
| 5025 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5026 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5027 | */ |
| 5028 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5029 | xmlNodeGetContent(xmlNodePtr cur) |
| 5030 | { |
| 5031 | if (cur == NULL) |
| 5032 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5033 | switch (cur->type) { |
| 5034 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5035 | case XML_ELEMENT_NODE:{ |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5036 | xmlBufferPtr buffer; |
| 5037 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5038 | |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 5039 | buffer = xmlBufferCreateSize(64); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5040 | if (buffer == NULL) |
| 5041 | return (NULL); |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5042 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5043 | ret = buffer->content; |
| 5044 | buffer->content = NULL; |
| 5045 | xmlBufferFree(buffer); |
| 5046 | return (ret); |
| 5047 | } |
| 5048 | case XML_ATTRIBUTE_NODE:{ |
| 5049 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 5050 | |
| 5051 | if (attr->parent != NULL) |
| 5052 | return (xmlNodeListGetString |
| 5053 | (attr->parent->doc, attr->children, 1)); |
| 5054 | else |
| 5055 | return (xmlNodeListGetString(NULL, attr->children, 1)); |
| 5056 | break; |
| 5057 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5058 | case XML_COMMENT_NODE: |
| 5059 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5060 | if (cur->content != NULL) |
| 5061 | return (xmlStrdup(cur->content)); |
| 5062 | return (NULL); |
| 5063 | case XML_ENTITY_REF_NODE:{ |
| 5064 | xmlEntityPtr ent; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5065 | xmlBufferPtr buffer; |
| 5066 | xmlChar *ret; |
| 5067 | |
| 5068 | /* lookup entity declaration */ |
| 5069 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5070 | if (ent == NULL) |
| 5071 | return (NULL); |
| 5072 | |
| 5073 | buffer = xmlBufferCreate(); |
| 5074 | if (buffer == NULL) |
| 5075 | return (NULL); |
| 5076 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5077 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5078 | |
| 5079 | ret = buffer->content; |
| 5080 | buffer->content = NULL; |
| 5081 | xmlBufferFree(buffer); |
| 5082 | return (ret); |
| 5083 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5084 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5085 | case XML_DOCUMENT_TYPE_NODE: |
| 5086 | case XML_NOTATION_NODE: |
| 5087 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5088 | case XML_XINCLUDE_START: |
| 5089 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5090 | return (NULL); |
| 5091 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5092 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5093 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5094 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5095 | case XML_HTML_DOCUMENT_NODE: { |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5096 | xmlBufferPtr buffer; |
| 5097 | xmlChar *ret; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5098 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5099 | buffer = xmlBufferCreate(); |
| 5100 | if (buffer == NULL) |
| 5101 | return (NULL); |
| 5102 | |
| 5103 | xmlNodeBufGetContent(buffer, (xmlNodePtr) cur); |
| 5104 | |
| 5105 | ret = buffer->content; |
| 5106 | buffer->content = NULL; |
| 5107 | xmlBufferFree(buffer); |
| 5108 | return (ret); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5109 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 5110 | case XML_NAMESPACE_DECL: { |
| 5111 | xmlChar *tmp; |
| 5112 | |
| 5113 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 5114 | return (tmp); |
| 5115 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5116 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5117 | /* TODO !!! */ |
| 5118 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5119 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5120 | /* TODO !!! */ |
| 5121 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5122 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5123 | /* TODO !!! */ |
| 5124 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5125 | case XML_CDATA_SECTION_NODE: |
| 5126 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5127 | if (cur->content != NULL) |
| 5128 | return (xmlStrdup(cur->content)); |
| 5129 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5130 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5131 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5132 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5133 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5134 | /** |
| 5135 | * xmlNodeSetContent: |
| 5136 | * @cur: the node being modified |
| 5137 | * @content: the new value of the content |
| 5138 | * |
| 5139 | * Replace the content of a node. |
| 5140 | */ |
| 5141 | void |
| 5142 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 5143 | if (cur == NULL) { |
| 5144 | #ifdef DEBUG_TREE |
| 5145 | xmlGenericError(xmlGenericErrorContext, |
| 5146 | "xmlNodeSetContent : node == NULL\n"); |
| 5147 | #endif |
| 5148 | return; |
| 5149 | } |
| 5150 | switch (cur->type) { |
| 5151 | case XML_DOCUMENT_FRAG_NODE: |
| 5152 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5153 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5154 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5155 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 5156 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5157 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5158 | case XML_TEXT_NODE: |
| 5159 | case XML_CDATA_SECTION_NODE: |
| 5160 | case XML_ENTITY_REF_NODE: |
| 5161 | case XML_ENTITY_NODE: |
| 5162 | case XML_PI_NODE: |
| 5163 | case XML_COMMENT_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5164 | if ((cur->content != NULL) && |
| 5165 | (cur->content != (xmlChar *) &(cur->properties))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5166 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
Daniel Veillard | c587bce | 2005-05-10 15:28:08 +0000 | [diff] [blame] | 5167 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5168 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5169 | } |
| 5170 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5171 | cur->last = cur->children = NULL; |
| 5172 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5173 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5174 | } else |
| 5175 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5176 | cur->properties = NULL; |
| 5177 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5178 | break; |
| 5179 | case XML_DOCUMENT_NODE: |
| 5180 | case XML_HTML_DOCUMENT_NODE: |
| 5181 | case XML_DOCUMENT_TYPE_NODE: |
| 5182 | case XML_XINCLUDE_START: |
| 5183 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5184 | #ifdef LIBXML_DOCB_ENABLED |
| 5185 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5186 | #endif |
| 5187 | break; |
| 5188 | case XML_NOTATION_NODE: |
| 5189 | break; |
| 5190 | case XML_DTD_NODE: |
| 5191 | break; |
| 5192 | case XML_NAMESPACE_DECL: |
| 5193 | break; |
| 5194 | case XML_ELEMENT_DECL: |
| 5195 | /* TODO !!! */ |
| 5196 | break; |
| 5197 | case XML_ATTRIBUTE_DECL: |
| 5198 | /* TODO !!! */ |
| 5199 | break; |
| 5200 | case XML_ENTITY_DECL: |
| 5201 | /* TODO !!! */ |
| 5202 | break; |
| 5203 | } |
| 5204 | } |
| 5205 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5206 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5207 | /** |
| 5208 | * xmlNodeSetContentLen: |
| 5209 | * @cur: the node being modified |
| 5210 | * @content: the new value of the content |
| 5211 | * @len: the size of @content |
| 5212 | * |
| 5213 | * Replace the content of a node. |
| 5214 | */ |
| 5215 | void |
| 5216 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5217 | if (cur == NULL) { |
| 5218 | #ifdef DEBUG_TREE |
| 5219 | xmlGenericError(xmlGenericErrorContext, |
| 5220 | "xmlNodeSetContentLen : node == NULL\n"); |
| 5221 | #endif |
| 5222 | return; |
| 5223 | } |
| 5224 | switch (cur->type) { |
| 5225 | case XML_DOCUMENT_FRAG_NODE: |
| 5226 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5227 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5228 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5229 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 5230 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5231 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5232 | case XML_TEXT_NODE: |
| 5233 | case XML_CDATA_SECTION_NODE: |
| 5234 | case XML_ENTITY_REF_NODE: |
| 5235 | case XML_ENTITY_NODE: |
| 5236 | case XML_PI_NODE: |
| 5237 | case XML_COMMENT_NODE: |
| 5238 | case XML_NOTATION_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5239 | if ((cur->content != NULL) && |
| 5240 | (cur->content != (xmlChar *) &(cur->properties))) { |
| 5241 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5242 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
| 5243 | xmlFree(cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5244 | } |
| 5245 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5246 | cur->children = cur->last = NULL; |
| 5247 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5248 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5249 | } else |
| 5250 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5251 | cur->properties = NULL; |
| 5252 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5253 | break; |
| 5254 | case XML_DOCUMENT_NODE: |
| 5255 | case XML_DTD_NODE: |
| 5256 | case XML_HTML_DOCUMENT_NODE: |
| 5257 | case XML_DOCUMENT_TYPE_NODE: |
| 5258 | case XML_NAMESPACE_DECL: |
| 5259 | case XML_XINCLUDE_START: |
| 5260 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5261 | #ifdef LIBXML_DOCB_ENABLED |
| 5262 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5263 | #endif |
| 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 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5276 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5277 | |
| 5278 | /** |
| 5279 | * xmlNodeAddContentLen: |
| 5280 | * @cur: the node being modified |
| 5281 | * @content: extra content |
| 5282 | * @len: the size of @content |
| 5283 | * |
| 5284 | * Append the extra substring to the node content. |
| 5285 | */ |
| 5286 | void |
| 5287 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5288 | if (cur == NULL) { |
| 5289 | #ifdef DEBUG_TREE |
| 5290 | xmlGenericError(xmlGenericErrorContext, |
| 5291 | "xmlNodeAddContentLen : node == NULL\n"); |
| 5292 | #endif |
| 5293 | return; |
| 5294 | } |
| 5295 | if (len <= 0) return; |
| 5296 | switch (cur->type) { |
| 5297 | case XML_DOCUMENT_FRAG_NODE: |
| 5298 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5299 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5300 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 5301 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5302 | newNode = xmlNewTextLen(content, len); |
| 5303 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5304 | tmp = xmlAddChild(cur, newNode); |
| 5305 | if (tmp != newNode) |
| 5306 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5307 | if ((last != NULL) && (last->next == newNode)) { |
| 5308 | xmlTextMerge(last, newNode); |
| 5309 | } |
| 5310 | } |
| 5311 | break; |
| 5312 | } |
| 5313 | case XML_ATTRIBUTE_NODE: |
| 5314 | break; |
| 5315 | case XML_TEXT_NODE: |
| 5316 | case XML_CDATA_SECTION_NODE: |
| 5317 | case XML_ENTITY_REF_NODE: |
| 5318 | case XML_ENTITY_NODE: |
| 5319 | case XML_PI_NODE: |
| 5320 | case XML_COMMENT_NODE: |
| 5321 | case XML_NOTATION_NODE: |
| 5322 | if (content != NULL) { |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5323 | if ((cur->content == (xmlChar *) &(cur->properties)) || |
| 5324 | ((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5325 | xmlDictOwns(cur->doc->dict, cur->content))) { |
| 5326 | cur->content = xmlStrncatNew(cur->content, content, len); |
| 5327 | cur->properties = NULL; |
| 5328 | cur->nsDef = NULL; |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5329 | break; |
| 5330 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5331 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5332 | } |
| 5333 | case XML_DOCUMENT_NODE: |
| 5334 | case XML_DTD_NODE: |
| 5335 | case XML_HTML_DOCUMENT_NODE: |
| 5336 | case XML_DOCUMENT_TYPE_NODE: |
| 5337 | case XML_NAMESPACE_DECL: |
| 5338 | case XML_XINCLUDE_START: |
| 5339 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5340 | #ifdef LIBXML_DOCB_ENABLED |
| 5341 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5342 | #endif |
| 5343 | break; |
| 5344 | case XML_ELEMENT_DECL: |
| 5345 | case XML_ATTRIBUTE_DECL: |
| 5346 | case XML_ENTITY_DECL: |
| 5347 | break; |
| 5348 | } |
| 5349 | } |
| 5350 | |
| 5351 | /** |
| 5352 | * xmlNodeAddContent: |
| 5353 | * @cur: the node being modified |
| 5354 | * @content: extra content |
| 5355 | * |
| 5356 | * Append the extra substring to the node content. |
| 5357 | */ |
| 5358 | void |
| 5359 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5360 | int len; |
| 5361 | |
| 5362 | if (cur == NULL) { |
| 5363 | #ifdef DEBUG_TREE |
| 5364 | xmlGenericError(xmlGenericErrorContext, |
| 5365 | "xmlNodeAddContent : node == NULL\n"); |
| 5366 | #endif |
| 5367 | return; |
| 5368 | } |
| 5369 | if (content == NULL) return; |
| 5370 | len = xmlStrlen(content); |
| 5371 | xmlNodeAddContentLen(cur, content, len); |
| 5372 | } |
| 5373 | |
| 5374 | /** |
| 5375 | * xmlTextMerge: |
| 5376 | * @first: the first text node |
| 5377 | * @second: the second text node being merged |
| 5378 | * |
| 5379 | * Merge two text nodes into one |
| 5380 | * Returns the first text node augmented |
| 5381 | */ |
| 5382 | xmlNodePtr |
| 5383 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5384 | if (first == NULL) return(second); |
| 5385 | if (second == NULL) return(first); |
| 5386 | if (first->type != XML_TEXT_NODE) return(first); |
| 5387 | if (second->type != XML_TEXT_NODE) return(first); |
| 5388 | if (second->name != first->name) |
| 5389 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5390 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5391 | xmlUnlinkNode(second); |
| 5392 | xmlFreeNode(second); |
| 5393 | return(first); |
| 5394 | } |
| 5395 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 5396 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5397 | /** |
| 5398 | * xmlGetNsList: |
| 5399 | * @doc: the document |
| 5400 | * @node: the current node |
| 5401 | * |
| 5402 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5403 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5404 | * that need to be freed by the caller or NULL if no |
| 5405 | * namespace if defined |
| 5406 | */ |
| 5407 | xmlNsPtr * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5408 | xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) |
| 5409 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5410 | xmlNsPtr cur; |
| 5411 | xmlNsPtr *ret = NULL; |
| 5412 | int nbns = 0; |
| 5413 | int maxns = 10; |
| 5414 | int i; |
| 5415 | |
| 5416 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5417 | if (node->type == XML_ELEMENT_NODE) { |
| 5418 | cur = node->nsDef; |
| 5419 | while (cur != NULL) { |
| 5420 | if (ret == NULL) { |
| 5421 | ret = |
| 5422 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5423 | sizeof(xmlNsPtr)); |
| 5424 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5425 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5426 | return (NULL); |
| 5427 | } |
| 5428 | ret[nbns] = NULL; |
| 5429 | } |
| 5430 | for (i = 0; i < nbns; i++) { |
| 5431 | if ((cur->prefix == ret[i]->prefix) || |
| 5432 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5433 | break; |
| 5434 | } |
| 5435 | if (i >= nbns) { |
| 5436 | if (nbns >= maxns) { |
| 5437 | maxns *= 2; |
| 5438 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5439 | (maxns + |
| 5440 | 1) * |
| 5441 | sizeof(xmlNsPtr)); |
| 5442 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5443 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5444 | return (NULL); |
| 5445 | } |
| 5446 | } |
| 5447 | ret[nbns++] = cur; |
| 5448 | ret[nbns] = NULL; |
| 5449 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5450 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5451 | cur = cur->next; |
| 5452 | } |
| 5453 | } |
| 5454 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5455 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5456 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5457 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5458 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5459 | |
| 5460 | /** |
| 5461 | * xmlSearchNs: |
| 5462 | * @doc: the document |
| 5463 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5464 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5465 | * |
| 5466 | * Search a Ns registered under a given name space for a document. |
| 5467 | * recurse on the parents until it finds the defined namespace |
| 5468 | * or return NULL otherwise. |
| 5469 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5470 | * We don't allow to cross entities boundaries. If you don't declare |
| 5471 | * the namespace within those you will be in troubles !!! A warning |
| 5472 | * is generated to cover this case. |
| 5473 | * |
| 5474 | * Returns the namespace pointer or NULL. |
| 5475 | */ |
| 5476 | xmlNsPtr |
| 5477 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5478 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5479 | xmlNsPtr cur; |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5480 | xmlNodePtr orig = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5481 | |
| 5482 | if (node == NULL) return(NULL); |
| 5483 | if ((nameSpace != NULL) && |
| 5484 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5485 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5486 | /* |
| 5487 | * The XML-1.0 namespace is normally held on the root |
| 5488 | * element. In this case exceptionally create it on the |
| 5489 | * node element. |
| 5490 | */ |
| 5491 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5492 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5493 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5494 | return(NULL); |
| 5495 | } |
| 5496 | memset(cur, 0, sizeof(xmlNs)); |
| 5497 | cur->type = XML_LOCAL_NAMESPACE; |
| 5498 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5499 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5500 | cur->next = node->nsDef; |
| 5501 | node->nsDef = cur; |
| 5502 | return(cur); |
| 5503 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5504 | if (doc->oldNs == NULL) { |
| 5505 | /* |
| 5506 | * Allocate a new Namespace and fill the fields. |
| 5507 | */ |
| 5508 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5509 | if (doc->oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5510 | xmlTreeErrMemory("searching namespace"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5511 | return(NULL); |
| 5512 | } |
| 5513 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5514 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
| 5515 | |
| 5516 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5517 | doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5518 | } |
| 5519 | return(doc->oldNs); |
| 5520 | } |
| 5521 | while (node != NULL) { |
| 5522 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5523 | (node->type == XML_ENTITY_NODE) || |
| 5524 | (node->type == XML_ENTITY_DECL)) |
| 5525 | return(NULL); |
| 5526 | if (node->type == XML_ELEMENT_NODE) { |
| 5527 | cur = node->nsDef; |
| 5528 | while (cur != NULL) { |
| 5529 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5530 | (cur->href != NULL)) |
| 5531 | return(cur); |
| 5532 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5533 | (cur->href != NULL) && |
| 5534 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5535 | return(cur); |
| 5536 | cur = cur->next; |
| 5537 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5538 | if (orig != node) { |
| 5539 | cur = node->ns; |
| 5540 | if (cur != NULL) { |
| 5541 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5542 | (cur->href != NULL)) |
| 5543 | return(cur); |
| 5544 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5545 | (cur->href != NULL) && |
| 5546 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5547 | return(cur); |
| 5548 | } |
| 5549 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5550 | } |
| 5551 | node = node->parent; |
| 5552 | } |
| 5553 | return(NULL); |
| 5554 | } |
| 5555 | |
| 5556 | /** |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5557 | * xmlNsInScope: |
| 5558 | * @doc: the document |
| 5559 | * @node: the current node |
| 5560 | * @ancestor: the ancestor carrying the namespace |
| 5561 | * @prefix: the namespace prefix |
| 5562 | * |
| 5563 | * Verify that the given namespace held on @ancestor is still in scope |
| 5564 | * on node. |
| 5565 | * |
| 5566 | * Returns 1 if true, 0 if false and -1 in case of error. |
| 5567 | */ |
| 5568 | static int |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5569 | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 5570 | xmlNodePtr ancestor, const xmlChar * prefix) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5571 | { |
| 5572 | xmlNsPtr tst; |
| 5573 | |
| 5574 | while ((node != NULL) && (node != ancestor)) { |
| 5575 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5576 | (node->type == XML_ENTITY_NODE) || |
| 5577 | (node->type == XML_ENTITY_DECL)) |
| 5578 | return (-1); |
| 5579 | if (node->type == XML_ELEMENT_NODE) { |
| 5580 | tst = node->nsDef; |
| 5581 | while (tst != NULL) { |
| 5582 | if ((tst->prefix == NULL) |
| 5583 | && (prefix == NULL)) |
| 5584 | return (0); |
| 5585 | if ((tst->prefix != NULL) |
| 5586 | && (prefix != NULL) |
| 5587 | && (xmlStrEqual(tst->prefix, prefix))) |
| 5588 | return (0); |
| 5589 | tst = tst->next; |
| 5590 | } |
| 5591 | } |
| 5592 | node = node->parent; |
| 5593 | } |
| 5594 | if (node != ancestor) |
| 5595 | return (-1); |
| 5596 | return (1); |
| 5597 | } |
| 5598 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5599 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5600 | * xmlSearchNsByHref: |
| 5601 | * @doc: the document |
| 5602 | * @node: the current node |
| 5603 | * @href: the namespace value |
| 5604 | * |
| 5605 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 5606 | * the defined namespace or return NULL otherwise. |
| 5607 | * Returns the namespace pointer or NULL. |
| 5608 | */ |
| 5609 | xmlNsPtr |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5610 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) |
| 5611 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5612 | xmlNsPtr cur; |
| 5613 | xmlNodePtr orig = node; |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5614 | int is_attr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5615 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5616 | if ((node == NULL) || (href == NULL)) |
| 5617 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5618 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5619 | /* |
| 5620 | * Only the document can hold the XML spec namespace. |
| 5621 | */ |
| 5622 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5623 | /* |
| 5624 | * The XML-1.0 namespace is normally held on the root |
| 5625 | * element. In this case exceptionally create it on the |
| 5626 | * node element. |
| 5627 | */ |
| 5628 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5629 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5630 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5631 | return (NULL); |
| 5632 | } |
| 5633 | memset(cur, 0, sizeof(xmlNs)); |
| 5634 | cur->type = XML_LOCAL_NAMESPACE; |
| 5635 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5636 | cur->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5637 | cur->next = node->nsDef; |
| 5638 | node->nsDef = cur; |
| 5639 | return (cur); |
| 5640 | } |
| 5641 | if (doc->oldNs == NULL) { |
| 5642 | /* |
| 5643 | * Allocate a new Namespace and fill the fields. |
| 5644 | */ |
| 5645 | doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5646 | if (doc->oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5647 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5648 | return (NULL); |
| 5649 | } |
| 5650 | memset(doc->oldNs, 0, sizeof(xmlNs)); |
| 5651 | doc->oldNs->type = XML_LOCAL_NAMESPACE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5652 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5653 | doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5654 | doc->oldNs->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5655 | } |
| 5656 | return (doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5657 | } |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5658 | is_attr = (node->type == XML_ATTRIBUTE_NODE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5659 | while (node != NULL) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5660 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5661 | (node->type == XML_ENTITY_NODE) || |
| 5662 | (node->type == XML_ENTITY_DECL)) |
| 5663 | return (NULL); |
| 5664 | if (node->type == XML_ELEMENT_NODE) { |
| 5665 | cur = node->nsDef; |
| 5666 | while (cur != NULL) { |
| 5667 | if ((cur->href != NULL) && (href != NULL) && |
| 5668 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5669 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 5670 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5671 | return (cur); |
| 5672 | } |
| 5673 | cur = cur->next; |
| 5674 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5675 | if (orig != node) { |
| 5676 | cur = node->ns; |
| 5677 | if (cur != NULL) { |
| 5678 | if ((cur->href != NULL) && (href != NULL) && |
| 5679 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5680 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 5681 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5682 | return (cur); |
| 5683 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5684 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5685 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5686 | } |
| 5687 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5688 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5689 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5693 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5694 | * @doc: the document |
| 5695 | * @tree: a node expected to hold the new namespace |
| 5696 | * @ns: the original namespace |
| 5697 | * |
| 5698 | * This function tries to locate a namespace definition in a tree |
| 5699 | * ancestors, or create a new namespace definition node similar to |
| 5700 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 5701 | * null (default namespace) or reused within the subtree defined by |
| 5702 | * @tree or on one of its ancestors then a new prefix is generated. |
| 5703 | * Returns the (new) namespace definition or NULL in case of error |
| 5704 | */ |
| 5705 | xmlNsPtr |
| 5706 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 5707 | xmlNsPtr def; |
| 5708 | xmlChar prefix[50]; |
| 5709 | int counter = 1; |
| 5710 | |
| 5711 | if (tree == NULL) { |
| 5712 | #ifdef DEBUG_TREE |
| 5713 | xmlGenericError(xmlGenericErrorContext, |
| 5714 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 5715 | #endif |
| 5716 | return(NULL); |
| 5717 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5718 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5719 | #ifdef DEBUG_TREE |
| 5720 | xmlGenericError(xmlGenericErrorContext, |
| 5721 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 5722 | #endif |
| 5723 | return(NULL); |
| 5724 | } |
| 5725 | /* |
| 5726 | * Search an existing namespace definition inherited. |
| 5727 | */ |
| 5728 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 5729 | if (def != NULL) |
| 5730 | return(def); |
| 5731 | |
| 5732 | /* |
| 5733 | * Find a close prefix which is not already in use. |
| 5734 | * Let's strip namespace prefixes longer than 20 chars ! |
| 5735 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5736 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5737 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5738 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 5739 | snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5740 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5741 | def = xmlSearchNs(doc, tree, prefix); |
| 5742 | while (def != NULL) { |
| 5743 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5744 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5745 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5746 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 5747 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", |
| 5748 | (char *)ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5749 | def = xmlSearchNs(doc, tree, prefix); |
| 5750 | } |
| 5751 | |
| 5752 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5753 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5754 | */ |
| 5755 | def = xmlNewNs(tree, ns->href, prefix); |
| 5756 | return(def); |
| 5757 | } |
| 5758 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5759 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5760 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5761 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5762 | * @doc: the document |
| 5763 | * @tree: a node defining the subtree to reconciliate |
| 5764 | * |
| 5765 | * This function checks that all the namespaces declared within the given |
| 5766 | * tree are properly declared. This is needed for example after Copy or Cut |
| 5767 | * and then paste operations. The subtree may still hold pointers to |
| 5768 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5769 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5770 | * the new environment. If not possible the new namespaces are redeclared |
| 5771 | * on @tree at the top of the given subtree. |
| 5772 | * Returns the number of namespace declarations created or -1 in case of error. |
| 5773 | */ |
| 5774 | int |
| 5775 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 5776 | xmlNsPtr *oldNs = NULL; |
| 5777 | xmlNsPtr *newNs = NULL; |
| 5778 | int sizeCache = 0; |
| 5779 | int nbCache = 0; |
| 5780 | |
| 5781 | xmlNsPtr n; |
| 5782 | xmlNodePtr node = tree; |
| 5783 | xmlAttrPtr attr; |
| 5784 | int ret = 0, i; |
| 5785 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5786 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); |
| 5787 | if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1); |
| 5788 | if (node->doc != doc) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5789 | while (node != NULL) { |
| 5790 | /* |
| 5791 | * Reconciliate the node namespace |
| 5792 | */ |
| 5793 | if (node->ns != NULL) { |
| 5794 | /* |
| 5795 | * initialize the cache if needed |
| 5796 | */ |
| 5797 | if (sizeCache == 0) { |
| 5798 | sizeCache = 10; |
| 5799 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5800 | sizeof(xmlNsPtr)); |
| 5801 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5802 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5803 | return(-1); |
| 5804 | } |
| 5805 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5806 | sizeof(xmlNsPtr)); |
| 5807 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5808 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5809 | xmlFree(oldNs); |
| 5810 | return(-1); |
| 5811 | } |
| 5812 | } |
| 5813 | for (i = 0;i < nbCache;i++) { |
| 5814 | if (oldNs[i] == node->ns) { |
| 5815 | node->ns = newNs[i]; |
| 5816 | break; |
| 5817 | } |
| 5818 | } |
| 5819 | if (i == nbCache) { |
| 5820 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5821 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5822 | */ |
| 5823 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 5824 | if (n != NULL) { /* :-( what if else ??? */ |
| 5825 | /* |
| 5826 | * check if we need to grow the cache buffers. |
| 5827 | */ |
| 5828 | if (sizeCache <= nbCache) { |
| 5829 | sizeCache *= 2; |
| 5830 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5831 | sizeof(xmlNsPtr)); |
| 5832 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5833 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5834 | xmlFree(newNs); |
| 5835 | return(-1); |
| 5836 | } |
| 5837 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5838 | sizeof(xmlNsPtr)); |
| 5839 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5840 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5841 | xmlFree(oldNs); |
| 5842 | return(-1); |
| 5843 | } |
| 5844 | } |
| 5845 | newNs[nbCache] = n; |
| 5846 | oldNs[nbCache++] = node->ns; |
| 5847 | node->ns = n; |
| 5848 | } |
| 5849 | } |
| 5850 | } |
| 5851 | /* |
| 5852 | * now check for namespace hold by attributes on the node. |
| 5853 | */ |
| 5854 | attr = node->properties; |
| 5855 | while (attr != NULL) { |
| 5856 | if (attr->ns != NULL) { |
| 5857 | /* |
| 5858 | * initialize the cache if needed |
| 5859 | */ |
| 5860 | if (sizeCache == 0) { |
| 5861 | sizeCache = 10; |
| 5862 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5863 | sizeof(xmlNsPtr)); |
| 5864 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5865 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5866 | return(-1); |
| 5867 | } |
| 5868 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 5869 | sizeof(xmlNsPtr)); |
| 5870 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5871 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5872 | xmlFree(oldNs); |
| 5873 | return(-1); |
| 5874 | } |
| 5875 | } |
| 5876 | for (i = 0;i < nbCache;i++) { |
| 5877 | if (oldNs[i] == attr->ns) { |
Daniel Veillard | ce66ce1 | 2002-10-28 19:01:59 +0000 | [diff] [blame] | 5878 | attr->ns = newNs[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5879 | break; |
| 5880 | } |
| 5881 | } |
| 5882 | if (i == nbCache) { |
| 5883 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5884 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5885 | */ |
| 5886 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 5887 | if (n != NULL) { /* :-( what if else ??? */ |
| 5888 | /* |
| 5889 | * check if we need to grow the cache buffers. |
| 5890 | */ |
| 5891 | if (sizeCache <= nbCache) { |
| 5892 | sizeCache *= 2; |
| 5893 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 5894 | sizeof(xmlNsPtr)); |
| 5895 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5896 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5897 | xmlFree(newNs); |
| 5898 | return(-1); |
| 5899 | } |
| 5900 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 5901 | sizeof(xmlNsPtr)); |
| 5902 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5903 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5904 | xmlFree(oldNs); |
| 5905 | return(-1); |
| 5906 | } |
| 5907 | } |
| 5908 | newNs[nbCache] = n; |
| 5909 | oldNs[nbCache++] = attr->ns; |
| 5910 | attr->ns = n; |
| 5911 | } |
| 5912 | } |
| 5913 | } |
| 5914 | attr = attr->next; |
| 5915 | } |
| 5916 | |
| 5917 | /* |
| 5918 | * Browse the full subtree, deep first |
| 5919 | */ |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 5920 | if (node->children != NULL && node->type != XML_ENTITY_REF_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5921 | /* deep first */ |
| 5922 | node = node->children; |
| 5923 | } else if ((node != tree) && (node->next != NULL)) { |
| 5924 | /* then siblings */ |
| 5925 | node = node->next; |
| 5926 | } else if (node != tree) { |
| 5927 | /* go up to parents->next if needed */ |
| 5928 | while (node != tree) { |
| 5929 | if (node->parent != NULL) |
| 5930 | node = node->parent; |
| 5931 | if ((node != tree) && (node->next != NULL)) { |
| 5932 | node = node->next; |
| 5933 | break; |
| 5934 | } |
| 5935 | if (node->parent == NULL) { |
| 5936 | node = NULL; |
| 5937 | break; |
| 5938 | } |
| 5939 | } |
| 5940 | /* exit condition */ |
| 5941 | if (node == tree) |
| 5942 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 5943 | } else |
| 5944 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5945 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 5946 | if (oldNs != NULL) |
| 5947 | xmlFree(oldNs); |
| 5948 | if (newNs != NULL) |
| 5949 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5950 | return(ret); |
| 5951 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5952 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5953 | |
| 5954 | /** |
| 5955 | * xmlHasProp: |
| 5956 | * @node: the node |
| 5957 | * @name: the attribute name |
| 5958 | * |
| 5959 | * Search an attribute associated to a node |
| 5960 | * This function also looks in DTD attribute declaration for #FIXED or |
| 5961 | * default declaration values unless DTD use has been turned off. |
| 5962 | * |
| 5963 | * Returns the attribute or the attribute declaration or NULL if |
| 5964 | * neither was found. |
| 5965 | */ |
| 5966 | xmlAttrPtr |
| 5967 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 5968 | xmlAttrPtr prop; |
| 5969 | xmlDocPtr doc; |
| 5970 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5971 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 5972 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5973 | /* |
| 5974 | * Check on the properties attached to the node |
| 5975 | */ |
| 5976 | prop = node->properties; |
| 5977 | while (prop != NULL) { |
| 5978 | if (xmlStrEqual(prop->name, name)) { |
| 5979 | return(prop); |
| 5980 | } |
| 5981 | prop = prop->next; |
| 5982 | } |
| 5983 | if (!xmlCheckDTD) return(NULL); |
| 5984 | |
| 5985 | /* |
| 5986 | * Check if there is a default declaration in the internal |
| 5987 | * or external subsets |
| 5988 | */ |
| 5989 | doc = node->doc; |
| 5990 | if (doc != NULL) { |
| 5991 | xmlAttributePtr attrDecl; |
| 5992 | if (doc->intSubset != NULL) { |
| 5993 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 5994 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 5995 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 5996 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 5997 | /* return attribute declaration only if a default value is given |
| 5998 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5999 | return((xmlAttrPtr) attrDecl); |
| 6000 | } |
| 6001 | } |
| 6002 | return(NULL); |
| 6003 | } |
| 6004 | |
| 6005 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6006 | * xmlHasNsProp: |
| 6007 | * @node: the node |
| 6008 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6009 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6010 | * |
| 6011 | * Search for an attribute associated to a node |
| 6012 | * This attribute has to be anchored in the namespace specified. |
| 6013 | * This does the entity substitution. |
| 6014 | * This function looks in DTD attribute declaration for #FIXED or |
| 6015 | * default declaration values unless DTD use has been turned off. |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6016 | * Note that a namespace of NULL indicates to use the default namespace. |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6017 | * |
| 6018 | * Returns the attribute or the attribute declaration or NULL |
| 6019 | * if neither was found. |
| 6020 | */ |
| 6021 | xmlAttrPtr |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6022 | xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6023 | xmlAttrPtr prop; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6024 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6025 | xmlDocPtr doc; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6026 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6027 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6028 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6029 | return(NULL); |
| 6030 | |
| 6031 | prop = node->properties; |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6032 | while (prop != NULL) { |
| 6033 | /* |
| 6034 | * One need to have |
| 6035 | * - same attribute names |
| 6036 | * - and the attribute carrying that namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6037 | */ |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6038 | if (xmlStrEqual(prop->name, name)) { |
| 6039 | if (((prop->ns != NULL) && |
| 6040 | (xmlStrEqual(prop->ns->href, nameSpace))) || |
| 6041 | ((prop->ns == NULL) && (nameSpace == NULL))) { |
| 6042 | return(prop); |
| 6043 | } |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6044 | } |
| 6045 | prop = prop->next; |
| 6046 | } |
| 6047 | if (!xmlCheckDTD) return(NULL); |
| 6048 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6049 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6050 | /* |
| 6051 | * Check if there is a default declaration in the internal |
| 6052 | * or external subsets |
| 6053 | */ |
| 6054 | doc = node->doc; |
| 6055 | if (doc != NULL) { |
| 6056 | if (doc->intSubset != NULL) { |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 6057 | xmlAttributePtr attrDecl = NULL; |
| 6058 | xmlNsPtr *nsList, *cur; |
| 6059 | xmlChar *ename; |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6060 | |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 6061 | nsList = xmlGetNsList(node->doc, node); |
| 6062 | if (nsList == NULL) |
| 6063 | return(NULL); |
| 6064 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 6065 | ename = xmlStrdup(node->ns->prefix); |
| 6066 | ename = xmlStrcat(ename, BAD_CAST ":"); |
| 6067 | ename = xmlStrcat(ename, node->name); |
| 6068 | } else { |
| 6069 | ename = xmlStrdup(node->name); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6070 | } |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 6071 | if (ename == NULL) { |
| 6072 | xmlFree(nsList); |
| 6073 | return(NULL); |
| 6074 | } |
| 6075 | |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6076 | if (nameSpace == NULL) { |
| 6077 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename, |
| 6078 | name, NULL); |
| 6079 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 6080 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename, |
| 6081 | name, NULL); |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 6082 | } |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6083 | } else { |
| 6084 | cur = nsList; |
| 6085 | while (*cur != NULL) { |
| 6086 | if (xmlStrEqual((*cur)->href, nameSpace)) { |
| 6087 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename, |
| 6088 | name, (*cur)->prefix); |
| 6089 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6090 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename, |
| 6091 | name, (*cur)->prefix); |
| 6092 | } |
| 6093 | cur++; |
| 6094 | } |
Daniel Veillard | ef6c46f | 2002-03-07 22:21:56 +0000 | [diff] [blame] | 6095 | } |
| 6096 | xmlFree(nsList); |
| 6097 | xmlFree(ename); |
| 6098 | return((xmlAttrPtr) attrDecl); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6099 | } |
| 6100 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6101 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6102 | return(NULL); |
| 6103 | } |
| 6104 | |
| 6105 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6106 | * xmlGetProp: |
| 6107 | * @node: the node |
| 6108 | * @name: the attribute name |
| 6109 | * |
| 6110 | * Search and get the value of an attribute associated to a node |
| 6111 | * This does the entity substitution. |
| 6112 | * This function looks in DTD attribute declaration for #FIXED or |
| 6113 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 6114 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6115 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 6116 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6117 | * |
| 6118 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6119 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6120 | */ |
| 6121 | xmlChar * |
| 6122 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
| 6123 | xmlAttrPtr prop; |
| 6124 | xmlDocPtr doc; |
| 6125 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6126 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6127 | return(NULL); |
| 6128 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6129 | /* |
| 6130 | * Check on the properties attached to the node |
| 6131 | */ |
| 6132 | prop = node->properties; |
| 6133 | while (prop != NULL) { |
| 6134 | if (xmlStrEqual(prop->name, name)) { |
| 6135 | xmlChar *ret; |
| 6136 | |
| 6137 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 6138 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 6139 | return(ret); |
| 6140 | } |
| 6141 | prop = prop->next; |
| 6142 | } |
| 6143 | if (!xmlCheckDTD) return(NULL); |
| 6144 | |
| 6145 | /* |
| 6146 | * Check if there is a default declaration in the internal |
| 6147 | * or external subsets |
| 6148 | */ |
| 6149 | doc = node->doc; |
| 6150 | if (doc != NULL) { |
| 6151 | xmlAttributePtr attrDecl; |
| 6152 | if (doc->intSubset != NULL) { |
| 6153 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6154 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6155 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6156 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6157 | /* return attribute declaration only if a default value is given |
| 6158 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6159 | return(xmlStrdup(attrDecl->defaultValue)); |
| 6160 | } |
| 6161 | } |
| 6162 | return(NULL); |
| 6163 | } |
| 6164 | |
| 6165 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6166 | * xmlGetNoNsProp: |
| 6167 | * @node: the node |
| 6168 | * @name: the attribute name |
| 6169 | * |
| 6170 | * Search and get the value of an attribute associated to a node |
| 6171 | * This does the entity substitution. |
| 6172 | * This function looks in DTD attribute declaration for #FIXED or |
| 6173 | * default declaration values unless DTD use has been turned off. |
| 6174 | * This function is similar to xmlGetProp except it will accept only |
| 6175 | * an attribute in no namespace. |
| 6176 | * |
| 6177 | * Returns the attribute value or NULL if not found. |
| 6178 | * It's up to the caller to free the memory with xmlFree(). |
| 6179 | */ |
| 6180 | xmlChar * |
| 6181 | xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) { |
| 6182 | xmlAttrPtr prop; |
| 6183 | xmlDocPtr doc; |
| 6184 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6185 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6186 | return(NULL); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6187 | /* |
| 6188 | * Check on the properties attached to the node |
| 6189 | */ |
| 6190 | prop = node->properties; |
| 6191 | while (prop != NULL) { |
| 6192 | if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) { |
| 6193 | xmlChar *ret; |
| 6194 | |
| 6195 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 6196 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 6197 | return(ret); |
| 6198 | } |
| 6199 | prop = prop->next; |
| 6200 | } |
| 6201 | if (!xmlCheckDTD) return(NULL); |
| 6202 | |
| 6203 | /* |
| 6204 | * Check if there is a default declaration in the internal |
| 6205 | * or external subsets |
| 6206 | */ |
| 6207 | doc = node->doc; |
| 6208 | if (doc != NULL) { |
| 6209 | xmlAttributePtr attrDecl; |
| 6210 | if (doc->intSubset != NULL) { |
| 6211 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6212 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6213 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6214 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6215 | /* return attribute declaration only if a default value is given |
| 6216 | (that includes #FIXED declarations) */ |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6217 | return(xmlStrdup(attrDecl->defaultValue)); |
| 6218 | } |
| 6219 | } |
| 6220 | return(NULL); |
| 6221 | } |
| 6222 | |
| 6223 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6224 | * xmlGetNsProp: |
| 6225 | * @node: the node |
| 6226 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6227 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6228 | * |
| 6229 | * Search and get the value of an attribute associated to a node |
| 6230 | * This attribute has to be anchored in the namespace specified. |
| 6231 | * This does the entity substitution. |
| 6232 | * This function looks in DTD attribute declaration for #FIXED or |
| 6233 | * default declaration values unless DTD use has been turned off. |
| 6234 | * |
| 6235 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6236 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6237 | */ |
| 6238 | xmlChar * |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6239 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6240 | xmlAttrPtr prop; |
| 6241 | xmlDocPtr doc; |
| 6242 | xmlNsPtr ns; |
| 6243 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6244 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6245 | return(NULL); |
| 6246 | |
| 6247 | prop = node->properties; |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6248 | if (nameSpace == NULL) |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6249 | return(xmlGetNoNsProp(node, name)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6250 | while (prop != NULL) { |
| 6251 | /* |
| 6252 | * One need to have |
| 6253 | * - same attribute names |
| 6254 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6255 | */ |
| 6256 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | e8fc08e | 2001-06-07 19:35:47 +0000 | [diff] [blame] | 6257 | ((prop->ns != NULL) && |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6258 | (xmlStrEqual(prop->ns->href, nameSpace)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6259 | xmlChar *ret; |
| 6260 | |
| 6261 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
| 6262 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 6263 | return(ret); |
| 6264 | } |
| 6265 | prop = prop->next; |
| 6266 | } |
| 6267 | if (!xmlCheckDTD) return(NULL); |
| 6268 | |
| 6269 | /* |
| 6270 | * Check if there is a default declaration in the internal |
| 6271 | * or external subsets |
| 6272 | */ |
| 6273 | doc = node->doc; |
| 6274 | if (doc != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6275 | if (doc->intSubset != NULL) { |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 6276 | xmlAttributePtr attrDecl; |
| 6277 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6278 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6279 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6280 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 6281 | |
| 6282 | if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) { |
| 6283 | /* |
| 6284 | * The DTD declaration only allows a prefix search |
| 6285 | */ |
| 6286 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6287 | if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6288 | return(xmlStrdup(attrDecl->defaultValue)); |
| 6289 | } |
| 6290 | } |
| 6291 | } |
| 6292 | return(NULL); |
| 6293 | } |
| 6294 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6295 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
| 6296 | /** |
| 6297 | * xmlUnsetProp: |
| 6298 | * @node: the node |
| 6299 | * @name: the attribute name |
| 6300 | * |
| 6301 | * Remove an attribute carried by a node. |
| 6302 | * Returns 0 if successful, -1 if not found |
| 6303 | */ |
| 6304 | int |
| 6305 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6306 | xmlAttrPtr prop; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6307 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6308 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6309 | return(-1); |
| 6310 | prop = node->properties; |
| 6311 | while (prop != NULL) { |
| 6312 | if ((xmlStrEqual(prop->name, name)) && |
| 6313 | (prop->ns == NULL)) { |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 6314 | xmlUnlinkNode((xmlNodePtr) prop); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6315 | xmlFreeProp(prop); |
| 6316 | return(0); |
| 6317 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6318 | prop = prop->next; |
| 6319 | } |
| 6320 | return(-1); |
| 6321 | } |
| 6322 | |
| 6323 | /** |
| 6324 | * xmlUnsetNsProp: |
| 6325 | * @node: the node |
| 6326 | * @ns: the namespace definition |
| 6327 | * @name: the attribute name |
| 6328 | * |
| 6329 | * Remove an attribute carried by a node. |
| 6330 | * Returns 0 if successful, -1 if not found |
| 6331 | */ |
| 6332 | int |
| 6333 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6334 | xmlAttrPtr prop; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6335 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6336 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6337 | return(-1); |
Daniel Veillard | 27f2010 | 2004-11-05 11:50:11 +0000 | [diff] [blame] | 6338 | prop = node->properties; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6339 | if (ns == NULL) |
| 6340 | return(xmlUnsetProp(node, name)); |
| 6341 | if (ns->href == NULL) |
| 6342 | return(-1); |
| 6343 | while (prop != NULL) { |
| 6344 | if ((xmlStrEqual(prop->name, name)) && |
| 6345 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 6346 | xmlUnlinkNode((xmlNodePtr) prop); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6347 | xmlFreeProp(prop); |
| 6348 | return(0); |
| 6349 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6350 | prop = prop->next; |
| 6351 | } |
| 6352 | return(-1); |
| 6353 | } |
| 6354 | #endif |
| 6355 | |
| 6356 | #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] | 6357 | /** |
| 6358 | * xmlSetProp: |
| 6359 | * @node: the node |
| 6360 | * @name: the attribute name |
| 6361 | * @value: the attribute value |
| 6362 | * |
| 6363 | * Set (or reset) an attribute carried by a node. |
| 6364 | * Returns the attribute pointer. |
| 6365 | */ |
| 6366 | xmlAttrPtr |
| 6367 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6368 | xmlAttrPtr prop; |
| 6369 | xmlDocPtr doc; |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6370 | int len; |
| 6371 | const xmlChar *nqname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6372 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6373 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6374 | return(NULL); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6375 | |
| 6376 | /* |
| 6377 | * handle QNames |
| 6378 | */ |
| 6379 | nqname = xmlSplitQName3(name, &len); |
| 6380 | if (nqname != NULL) { |
| 6381 | xmlNsPtr ns; |
| 6382 | xmlChar *prefix = xmlStrndup(name, len); |
| 6383 | ns = xmlSearchNs(node->doc, node, prefix); |
| 6384 | if (prefix != NULL) |
| 6385 | xmlFree(prefix); |
| 6386 | if (ns != NULL) |
| 6387 | return(xmlSetNsProp(node, ns, nqname, value)); |
| 6388 | } |
| 6389 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6390 | doc = node->doc; |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6391 | prop = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6392 | while (prop != NULL) { |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6393 | if ((xmlStrEqual(prop->name, name)) && |
| 6394 | (prop->ns == NULL)){ |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6395 | xmlNodePtr oldprop = prop->children; |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6396 | int id = xmlIsID(node->doc, node, prop); |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6397 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6398 | if (id == 1) |
| 6399 | xmlRemoveID(node->doc, prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6400 | prop->children = NULL; |
| 6401 | prop->last = NULL; |
| 6402 | if (value != NULL) { |
| 6403 | xmlChar *buffer; |
| 6404 | xmlNodePtr tmp; |
| 6405 | |
| 6406 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6407 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 6408 | prop->last = NULL; |
| 6409 | prop->doc = doc; |
| 6410 | tmp = prop->children; |
| 6411 | while (tmp != NULL) { |
| 6412 | tmp->parent = (xmlNodePtr) prop; |
| 6413 | tmp->doc = doc; |
| 6414 | if (tmp->next == NULL) |
| 6415 | prop->last = tmp; |
| 6416 | tmp = tmp->next; |
| 6417 | } |
| 6418 | xmlFree(buffer); |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6419 | } |
Daniel Veillard | 4855c8c | 2001-11-25 10:35:25 +0000 | [diff] [blame] | 6420 | if (oldprop != NULL) |
| 6421 | xmlFreeNodeList(oldprop); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6422 | if (id) |
| 6423 | xmlAddID(NULL, node->doc, value, prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6424 | return(prop); |
| 6425 | } |
| 6426 | prop = prop->next; |
| 6427 | } |
| 6428 | prop = xmlNewProp(node, name, value); |
| 6429 | return(prop); |
| 6430 | } |
| 6431 | |
| 6432 | /** |
| 6433 | * xmlSetNsProp: |
| 6434 | * @node: the node |
| 6435 | * @ns: the namespace definition |
| 6436 | * @name: the attribute name |
| 6437 | * @value: the attribute value |
| 6438 | * |
| 6439 | * Set (or reset) an attribute carried by a node. |
| 6440 | * The ns structure must be in scope, this is not checked. |
| 6441 | * |
| 6442 | * Returns the attribute pointer. |
| 6443 | */ |
| 6444 | xmlAttrPtr |
| 6445 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 6446 | const xmlChar *value) { |
| 6447 | xmlAttrPtr prop; |
| 6448 | |
Daniel Veillard | b6b36d3 | 2005-02-09 16:48:53 +0000 | [diff] [blame] | 6449 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6450 | return(NULL); |
| 6451 | |
| 6452 | if (ns == NULL) |
| 6453 | return(xmlSetProp(node, name, value)); |
| 6454 | if (ns->href == NULL) |
| 6455 | return(NULL); |
| 6456 | prop = node->properties; |
| 6457 | |
| 6458 | while (prop != NULL) { |
| 6459 | /* |
| 6460 | * One need to have |
| 6461 | * - same attribute names |
| 6462 | * - and the attribute carrying that namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6463 | */ |
| 6464 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | a57c26e | 2002-08-01 12:52:24 +0000 | [diff] [blame] | 6465 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6466 | int id = xmlIsID(node->doc, node, prop); |
| 6467 | |
| 6468 | if (id == 1) |
| 6469 | xmlRemoveID(node->doc, prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6470 | if (prop->children != NULL) |
| 6471 | xmlFreeNodeList(prop->children); |
| 6472 | prop->children = NULL; |
| 6473 | prop->last = NULL; |
| 6474 | prop->ns = ns; |
| 6475 | if (value != NULL) { |
| 6476 | xmlChar *buffer; |
| 6477 | xmlNodePtr tmp; |
| 6478 | |
| 6479 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 6480 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 6481 | prop->last = NULL; |
| 6482 | tmp = prop->children; |
| 6483 | while (tmp != NULL) { |
| 6484 | tmp->parent = (xmlNodePtr) prop; |
| 6485 | if (tmp->next == NULL) |
| 6486 | prop->last = tmp; |
| 6487 | tmp = tmp->next; |
| 6488 | } |
| 6489 | xmlFree(buffer); |
| 6490 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6491 | if (id) |
| 6492 | xmlAddID(NULL, node->doc, value, prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6493 | return(prop); |
| 6494 | } |
| 6495 | prop = prop->next; |
| 6496 | } |
| 6497 | prop = xmlNewNsProp(node, ns, name, value); |
| 6498 | return(prop); |
| 6499 | } |
| 6500 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6501 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6502 | |
| 6503 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6504 | * xmlNodeIsText: |
| 6505 | * @node: the node |
| 6506 | * |
| 6507 | * Is this node a Text node ? |
| 6508 | * Returns 1 yes, 0 no |
| 6509 | */ |
| 6510 | int |
| 6511 | xmlNodeIsText(xmlNodePtr node) { |
| 6512 | if (node == NULL) return(0); |
| 6513 | |
| 6514 | if (node->type == XML_TEXT_NODE) return(1); |
| 6515 | return(0); |
| 6516 | } |
| 6517 | |
| 6518 | /** |
| 6519 | * xmlIsBlankNode: |
| 6520 | * @node: the node |
| 6521 | * |
| 6522 | * Checks whether this node is an empty or whitespace only |
| 6523 | * (and possibly ignorable) text-node. |
| 6524 | * |
| 6525 | * Returns 1 yes, 0 no |
| 6526 | */ |
| 6527 | int |
| 6528 | xmlIsBlankNode(xmlNodePtr node) { |
| 6529 | const xmlChar *cur; |
| 6530 | if (node == NULL) return(0); |
| 6531 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6532 | if ((node->type != XML_TEXT_NODE) && |
| 6533 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6534 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6535 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6536 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6537 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6538 | if (!IS_BLANK_CH(*cur)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6539 | cur++; |
| 6540 | } |
| 6541 | |
| 6542 | return(1); |
| 6543 | } |
| 6544 | |
| 6545 | /** |
| 6546 | * xmlTextConcat: |
| 6547 | * @node: the node |
| 6548 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6549 | * @len: @content length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6550 | * |
| 6551 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6552 | * |
| 6553 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6554 | */ |
| 6555 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6556 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6557 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6558 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6559 | |
| 6560 | if ((node->type != XML_TEXT_NODE) && |
| 6561 | (node->type != XML_CDATA_SECTION_NODE)) { |
| 6562 | #ifdef DEBUG_TREE |
| 6563 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6564 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6565 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6566 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6567 | } |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6568 | /* need to check if content is currently in the dictionary */ |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6569 | if ((node->content == (xmlChar *) &(node->properties)) || |
| 6570 | ((node->doc != NULL) && (node->doc->dict != NULL) && |
| 6571 | xmlDictOwns(node->doc->dict, node->content))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6572 | node->content = xmlStrncatNew(node->content, content, len); |
| 6573 | } else { |
| 6574 | node->content = xmlStrncat(node->content, content, len); |
| 6575 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6576 | node->properties = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6577 | if (node->content == NULL) |
| 6578 | return(-1); |
| 6579 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6580 | } |
| 6581 | |
| 6582 | /************************************************************************ |
| 6583 | * * |
| 6584 | * Output : to a FILE or in memory * |
| 6585 | * * |
| 6586 | ************************************************************************/ |
| 6587 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6588 | /** |
| 6589 | * xmlBufferCreate: |
| 6590 | * |
| 6591 | * routine to create an XML buffer. |
| 6592 | * returns the new structure. |
| 6593 | */ |
| 6594 | xmlBufferPtr |
| 6595 | xmlBufferCreate(void) { |
| 6596 | xmlBufferPtr ret; |
| 6597 | |
| 6598 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6599 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6600 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6601 | return(NULL); |
| 6602 | } |
| 6603 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 6604 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6605 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6606 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6607 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6608 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6609 | xmlFree(ret); |
| 6610 | return(NULL); |
| 6611 | } |
| 6612 | ret->content[0] = 0; |
| 6613 | return(ret); |
| 6614 | } |
| 6615 | |
| 6616 | /** |
| 6617 | * xmlBufferCreateSize: |
| 6618 | * @size: initial size of buffer |
| 6619 | * |
| 6620 | * routine to create an XML buffer. |
| 6621 | * returns the new structure. |
| 6622 | */ |
| 6623 | xmlBufferPtr |
| 6624 | xmlBufferCreateSize(size_t size) { |
| 6625 | xmlBufferPtr ret; |
| 6626 | |
| 6627 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6628 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6629 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6630 | return(NULL); |
| 6631 | } |
| 6632 | ret->use = 0; |
| 6633 | ret->alloc = xmlBufferAllocScheme; |
| 6634 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 6635 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6636 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6637 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6638 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6639 | xmlFree(ret); |
| 6640 | return(NULL); |
| 6641 | } |
| 6642 | ret->content[0] = 0; |
| 6643 | } else |
| 6644 | ret->content = NULL; |
| 6645 | return(ret); |
| 6646 | } |
| 6647 | |
| 6648 | /** |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6649 | * xmlBufferCreateStatic: |
| 6650 | * @mem: the memory area |
| 6651 | * @size: the size in byte |
| 6652 | * |
MST 2003 John Fleck | a0e7e93 | 2003-12-19 03:13:47 +0000 | [diff] [blame] | 6653 | * routine to create an XML buffer from an immutable memory area. |
| 6654 | * 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] | 6655 | * present until the end of the buffer lifetime. |
| 6656 | * |
| 6657 | * returns the new structure. |
| 6658 | */ |
| 6659 | xmlBufferPtr |
| 6660 | xmlBufferCreateStatic(void *mem, size_t size) { |
| 6661 | xmlBufferPtr ret; |
| 6662 | |
| 6663 | if ((mem == NULL) || (size == 0)) |
| 6664 | return(NULL); |
| 6665 | |
| 6666 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6667 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6668 | xmlTreeErrMemory("creating buffer"); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6669 | return(NULL); |
| 6670 | } |
| 6671 | ret->use = size; |
| 6672 | ret->size = size; |
| 6673 | ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; |
| 6674 | ret->content = (xmlChar *) mem; |
| 6675 | return(ret); |
| 6676 | } |
| 6677 | |
| 6678 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6679 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6680 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6681 | * @scheme: allocation scheme to use |
| 6682 | * |
| 6683 | * Sets the allocation scheme for this buffer |
| 6684 | */ |
| 6685 | void |
| 6686 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 6687 | xmlBufferAllocationScheme scheme) { |
| 6688 | if (buf == NULL) { |
| 6689 | #ifdef DEBUG_BUFFER |
| 6690 | xmlGenericError(xmlGenericErrorContext, |
| 6691 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 6692 | #endif |
| 6693 | return; |
| 6694 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6695 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6696 | |
| 6697 | buf->alloc = scheme; |
| 6698 | } |
| 6699 | |
| 6700 | /** |
| 6701 | * xmlBufferFree: |
| 6702 | * @buf: the buffer to free |
| 6703 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 6704 | * Frees an XML buffer. It frees both the content and the structure which |
| 6705 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6706 | */ |
| 6707 | void |
| 6708 | xmlBufferFree(xmlBufferPtr buf) { |
| 6709 | if (buf == NULL) { |
| 6710 | #ifdef DEBUG_BUFFER |
| 6711 | xmlGenericError(xmlGenericErrorContext, |
| 6712 | "xmlBufferFree: buf == NULL\n"); |
| 6713 | #endif |
| 6714 | return; |
| 6715 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6716 | |
| 6717 | if ((buf->content != NULL) && |
| 6718 | (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6719 | xmlFree(buf->content); |
| 6720 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6721 | xmlFree(buf); |
| 6722 | } |
| 6723 | |
| 6724 | /** |
| 6725 | * xmlBufferEmpty: |
| 6726 | * @buf: the buffer |
| 6727 | * |
| 6728 | * empty a buffer. |
| 6729 | */ |
| 6730 | void |
| 6731 | xmlBufferEmpty(xmlBufferPtr buf) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6732 | if (buf == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6733 | if (buf->content == NULL) return; |
| 6734 | buf->use = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6735 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 6736 | buf->content = BAD_CAST ""; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6737 | } else { |
| 6738 | memset(buf->content, 0, buf->size); |
| 6739 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6740 | } |
| 6741 | |
| 6742 | /** |
| 6743 | * xmlBufferShrink: |
| 6744 | * @buf: the buffer to dump |
| 6745 | * @len: the number of xmlChar to remove |
| 6746 | * |
| 6747 | * Remove the beginning of an XML buffer. |
| 6748 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6749 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6750 | */ |
| 6751 | int |
| 6752 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 6753 | if (buf == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6754 | if (len == 0) return(0); |
| 6755 | if (len > buf->use) return(-1); |
| 6756 | |
| 6757 | buf->use -= len; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6758 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
| 6759 | buf->content += len; |
| 6760 | } else { |
| 6761 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
| 6762 | buf->content[buf->use] = 0; |
| 6763 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6764 | return(len); |
| 6765 | } |
| 6766 | |
| 6767 | /** |
| 6768 | * xmlBufferGrow: |
| 6769 | * @buf: the buffer |
| 6770 | * @len: the minimum free size to allocate |
| 6771 | * |
| 6772 | * Grow the available space of an XML buffer. |
| 6773 | * |
| 6774 | * Returns the new available space or -1 in case of error |
| 6775 | */ |
| 6776 | int |
| 6777 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 6778 | int size; |
| 6779 | xmlChar *newbuf; |
| 6780 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 6781 | if (buf == NULL) return(-1); |
| 6782 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6783 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6784 | if (len + buf->use < buf->size) return(0); |
| 6785 | |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 6786 | /* |
| 6787 | * Windows has a BIG problem on realloc timing, so we try to double |
| 6788 | * the buffer size (if that's enough) (bug 146697) |
| 6789 | */ |
| 6790 | #ifdef WIN32 |
| 6791 | if (buf->size > len) |
| 6792 | size = buf->size * 2; |
| 6793 | else |
| 6794 | size = buf->use + len + 100; |
| 6795 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6796 | size = buf->use + len + 100; |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 6797 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6798 | |
| 6799 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6800 | if (newbuf == NULL) { |
| 6801 | xmlTreeErrMemory("growing buffer"); |
| 6802 | return(-1); |
| 6803 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6804 | buf->content = newbuf; |
| 6805 | buf->size = size; |
| 6806 | return(buf->size - buf->use); |
| 6807 | } |
| 6808 | |
| 6809 | /** |
| 6810 | * xmlBufferDump: |
| 6811 | * @file: the file output |
| 6812 | * @buf: the buffer to dump |
| 6813 | * |
| 6814 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6815 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6816 | */ |
| 6817 | int |
| 6818 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 6819 | int ret; |
| 6820 | |
| 6821 | if (buf == NULL) { |
| 6822 | #ifdef DEBUG_BUFFER |
| 6823 | xmlGenericError(xmlGenericErrorContext, |
| 6824 | "xmlBufferDump: buf == NULL\n"); |
| 6825 | #endif |
| 6826 | return(0); |
| 6827 | } |
| 6828 | if (buf->content == NULL) { |
| 6829 | #ifdef DEBUG_BUFFER |
| 6830 | xmlGenericError(xmlGenericErrorContext, |
| 6831 | "xmlBufferDump: buf->content == NULL\n"); |
| 6832 | #endif |
| 6833 | return(0); |
| 6834 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 6835 | if (file == NULL) |
| 6836 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6837 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 6838 | return(ret); |
| 6839 | } |
| 6840 | |
| 6841 | /** |
| 6842 | * xmlBufferContent: |
| 6843 | * @buf: the buffer |
| 6844 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6845 | * Function to extract the content of a buffer |
| 6846 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6847 | * Returns the internal content |
| 6848 | */ |
| 6849 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6850 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6851 | xmlBufferContent(const xmlBufferPtr buf) |
| 6852 | { |
| 6853 | if(!buf) |
| 6854 | return NULL; |
| 6855 | |
| 6856 | return buf->content; |
| 6857 | } |
| 6858 | |
| 6859 | /** |
| 6860 | * xmlBufferLength: |
| 6861 | * @buf: the buffer |
| 6862 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 6863 | * Function to get the length of a buffer |
| 6864 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6865 | * Returns the length of data in the internal content |
| 6866 | */ |
| 6867 | |
| 6868 | int |
| 6869 | xmlBufferLength(const xmlBufferPtr buf) |
| 6870 | { |
| 6871 | if(!buf) |
| 6872 | return 0; |
| 6873 | |
| 6874 | return buf->use; |
| 6875 | } |
| 6876 | |
| 6877 | /** |
| 6878 | * xmlBufferResize: |
| 6879 | * @buf: the buffer to resize |
| 6880 | * @size: the desired size |
| 6881 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6882 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6883 | * |
| 6884 | * Returns 0 in case of problems, 1 otherwise |
| 6885 | */ |
| 6886 | int |
| 6887 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 6888 | { |
| 6889 | unsigned int newSize; |
| 6890 | xmlChar* rebuf = NULL; |
| 6891 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6892 | if (buf == NULL) |
| 6893 | return(0); |
| 6894 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6895 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
| 6896 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6897 | /* Don't resize if we don't have to */ |
| 6898 | if (size < buf->size) |
| 6899 | return 1; |
| 6900 | |
| 6901 | /* figure out new size */ |
| 6902 | switch (buf->alloc){ |
| 6903 | case XML_BUFFER_ALLOC_DOUBLEIT: |
Daniel Veillard | bf62949 | 2004-04-20 22:20:59 +0000 | [diff] [blame] | 6904 | /*take care of empty case*/ |
| 6905 | newSize = (buf->size ? buf->size*2 : size + 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6906 | while (size > newSize) newSize *= 2; |
| 6907 | break; |
| 6908 | case XML_BUFFER_ALLOC_EXACT: |
| 6909 | newSize = size+10; |
| 6910 | break; |
| 6911 | default: |
| 6912 | newSize = size+10; |
| 6913 | break; |
| 6914 | } |
| 6915 | |
| 6916 | if (buf->content == NULL) |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6917 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6918 | else if (buf->size - buf->use < 100) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6919 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 6920 | newSize * sizeof(xmlChar)); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6921 | } else { |
| 6922 | /* |
| 6923 | * if we are reallocating a buffer far from being full, it's |
| 6924 | * better to make a new allocation and copy only the used range |
| 6925 | * and free the old one. |
| 6926 | */ |
| 6927 | rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar)); |
| 6928 | if (rebuf != NULL) { |
| 6929 | memcpy(rebuf, buf->content, buf->use); |
| 6930 | xmlFree(buf->content); |
William M. Brack | 42331a9 | 2004-07-29 07:07:16 +0000 | [diff] [blame] | 6931 | rebuf[buf->use] = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6932 | } |
| 6933 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6934 | if (rebuf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6935 | xmlTreeErrMemory("growing buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6936 | return 0; |
| 6937 | } |
| 6938 | buf->content = rebuf; |
| 6939 | buf->size = newSize; |
| 6940 | |
| 6941 | return 1; |
| 6942 | } |
| 6943 | |
| 6944 | /** |
| 6945 | * xmlBufferAdd: |
| 6946 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6947 | * @str: the #xmlChar string |
| 6948 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6949 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6950 | * 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] | 6951 | * str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6952 | * |
| 6953 | * Returns 0 successful, a positive error code number otherwise |
| 6954 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6955 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6956 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6957 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 6958 | unsigned int needSize; |
| 6959 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6960 | if ((str == NULL) || (buf == NULL)) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6961 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6962 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6963 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6964 | if (len < -1) { |
| 6965 | #ifdef DEBUG_BUFFER |
| 6966 | xmlGenericError(xmlGenericErrorContext, |
| 6967 | "xmlBufferAdd: len < 0\n"); |
| 6968 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6969 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6970 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6971 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6972 | |
| 6973 | if (len < 0) |
| 6974 | len = xmlStrlen(str); |
| 6975 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6976 | if (len <= 0) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6977 | |
| 6978 | needSize = buf->use + len + 2; |
| 6979 | if (needSize > buf->size){ |
| 6980 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6981 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6982 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6983 | } |
| 6984 | } |
| 6985 | |
| 6986 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 6987 | buf->use += len; |
| 6988 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 6989 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6990 | } |
| 6991 | |
| 6992 | /** |
| 6993 | * xmlBufferAddHead: |
| 6994 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6995 | * @str: the #xmlChar string |
| 6996 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6997 | * |
| 6998 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6999 | * if len == -1, the length of @str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7000 | * |
| 7001 | * Returns 0 successful, a positive error code number otherwise |
| 7002 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7003 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7004 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7005 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 7006 | unsigned int needSize; |
| 7007 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7008 | if (buf == NULL) |
| 7009 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7010 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7011 | if (str == NULL) { |
| 7012 | #ifdef DEBUG_BUFFER |
| 7013 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7014 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7015 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7016 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7017 | } |
| 7018 | if (len < -1) { |
| 7019 | #ifdef DEBUG_BUFFER |
| 7020 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7021 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7022 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7023 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7024 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7025 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7026 | |
| 7027 | if (len < 0) |
| 7028 | len = xmlStrlen(str); |
| 7029 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7030 | if (len <= 0) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7031 | |
| 7032 | needSize = buf->use + len + 2; |
| 7033 | if (needSize > buf->size){ |
| 7034 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7035 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7036 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7037 | } |
| 7038 | } |
| 7039 | |
| 7040 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 7041 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 7042 | buf->use += len; |
| 7043 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7044 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7045 | } |
| 7046 | |
| 7047 | /** |
| 7048 | * xmlBufferCat: |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7049 | * @buf: the buffer to add to |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7050 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7051 | * |
| 7052 | * Append a zero terminated string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7053 | * |
| 7054 | * Returns 0 successful, a positive error code number otherwise |
| 7055 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7056 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7057 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7058 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7059 | if (buf == NULL) |
| 7060 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7061 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
| 7062 | if (str == NULL) return -1; |
| 7063 | return xmlBufferAdd(buf, str, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7064 | } |
| 7065 | |
| 7066 | /** |
| 7067 | * xmlBufferCCat: |
| 7068 | * @buf: the buffer to dump |
| 7069 | * @str: the C char string |
| 7070 | * |
| 7071 | * Append a zero terminated C string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7072 | * |
| 7073 | * Returns 0 successful, a positive error code number otherwise |
| 7074 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7075 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7076 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7077 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 7078 | const char *cur; |
| 7079 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7080 | if (buf == NULL) |
| 7081 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7082 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7083 | if (str == NULL) { |
| 7084 | #ifdef DEBUG_BUFFER |
| 7085 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7086 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7087 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7088 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7089 | } |
| 7090 | for (cur = str;*cur != 0;cur++) { |
| 7091 | if (buf->use + 10 >= buf->size) { |
| 7092 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7093 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7094 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7095 | } |
| 7096 | } |
| 7097 | buf->content[buf->use++] = *cur; |
| 7098 | } |
| 7099 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7100 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7101 | } |
| 7102 | |
| 7103 | /** |
| 7104 | * xmlBufferWriteCHAR: |
| 7105 | * @buf: the XML buffer |
| 7106 | * @string: the string to add |
| 7107 | * |
| 7108 | * routine which manages and grows an output buffer. This one adds |
| 7109 | * xmlChars at the end of the buffer. |
| 7110 | */ |
| 7111 | void |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7112 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7113 | if (buf == NULL) |
| 7114 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7115 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7116 | xmlBufferCat(buf, string); |
| 7117 | } |
| 7118 | |
| 7119 | /** |
| 7120 | * xmlBufferWriteChar: |
| 7121 | * @buf: the XML buffer output |
| 7122 | * @string: the string to add |
| 7123 | * |
| 7124 | * routine which manage and grows an output buffer. This one add |
| 7125 | * C chars at the end of the array. |
| 7126 | */ |
| 7127 | void |
| 7128 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7129 | if (buf == NULL) |
| 7130 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7131 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7132 | xmlBufferCCat(buf, string); |
| 7133 | } |
| 7134 | |
| 7135 | |
| 7136 | /** |
| 7137 | * xmlBufferWriteQuotedString: |
| 7138 | * @buf: the XML buffer output |
| 7139 | * @string: the string to add |
| 7140 | * |
| 7141 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7142 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7143 | * quote or double-quotes internally |
| 7144 | */ |
| 7145 | void |
| 7146 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7147 | const xmlChar *cur, *base; |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7148 | if (buf == NULL) |
| 7149 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7150 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7151 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 7152 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7153 | #ifdef DEBUG_BUFFER |
| 7154 | xmlGenericError(xmlGenericErrorContext, |
| 7155 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 7156 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7157 | xmlBufferCCat(buf, "\""); |
| 7158 | base = cur = string; |
| 7159 | while(*cur != 0){ |
| 7160 | if(*cur == '"'){ |
| 7161 | if (base != cur) |
| 7162 | xmlBufferAdd(buf, base, cur - base); |
| 7163 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 7164 | cur++; |
| 7165 | base = cur; |
| 7166 | } |
| 7167 | else { |
| 7168 | cur++; |
| 7169 | } |
| 7170 | } |
| 7171 | if (base != cur) |
| 7172 | xmlBufferAdd(buf, base, cur - base); |
| 7173 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7174 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7175 | else{ |
| 7176 | xmlBufferCCat(buf, "\'"); |
| 7177 | xmlBufferCat(buf, string); |
| 7178 | xmlBufferCCat(buf, "\'"); |
| 7179 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7180 | } else { |
| 7181 | xmlBufferCCat(buf, "\""); |
| 7182 | xmlBufferCat(buf, string); |
| 7183 | xmlBufferCCat(buf, "\""); |
| 7184 | } |
| 7185 | } |
| 7186 | |
| 7187 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7188 | /** |
| 7189 | * xmlGetDocCompressMode: |
| 7190 | * @doc: the document |
| 7191 | * |
| 7192 | * get the compression ratio for a document, ZLIB based |
| 7193 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7194 | */ |
| 7195 | int |
| 7196 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 7197 | if (doc == NULL) return(-1); |
| 7198 | return(doc->compression); |
| 7199 | } |
| 7200 | |
| 7201 | /** |
| 7202 | * xmlSetDocCompressMode: |
| 7203 | * @doc: the document |
| 7204 | * @mode: the compression ratio |
| 7205 | * |
| 7206 | * set the compression ratio for a document, ZLIB based |
| 7207 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7208 | */ |
| 7209 | void |
| 7210 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 7211 | if (doc == NULL) return; |
| 7212 | if (mode < 0) doc->compression = 0; |
| 7213 | else if (mode > 9) doc->compression = 9; |
| 7214 | else doc->compression = mode; |
| 7215 | } |
| 7216 | |
| 7217 | /** |
| 7218 | * xmlGetCompressMode: |
| 7219 | * |
| 7220 | * get the default compression mode used, ZLIB based. |
| 7221 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7222 | */ |
| 7223 | int |
| 7224 | xmlGetCompressMode(void) |
| 7225 | { |
| 7226 | return (xmlCompressMode); |
| 7227 | } |
| 7228 | |
| 7229 | /** |
| 7230 | * xmlSetCompressMode: |
| 7231 | * @mode: the compression ratio |
| 7232 | * |
| 7233 | * set the default compression mode used, ZLIB based |
| 7234 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7235 | */ |
| 7236 | void |
| 7237 | xmlSetCompressMode(int mode) { |
| 7238 | if (mode < 0) xmlCompressMode = 0; |
| 7239 | else if (mode > 9) xmlCompressMode = 9; |
| 7240 | else xmlCompressMode = mode; |
| 7241 | } |
| 7242 | |
Kasimier T. Buchcik | 4d9c948 | 2005-06-27 15:04:46 +0000 | [diff] [blame] | 7243 | /* |
| 7244 | * xmlDOMWrapNewCtxt: |
| 7245 | * |
| 7246 | * Allocates and initializes a new DOM-wrapper context. |
| 7247 | * |
| 7248 | * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror. |
| 7249 | */ |
| 7250 | xmlDOMWrapCtxtPtr |
| 7251 | xmlDOMWrapNewCtxt(void) |
| 7252 | { |
| 7253 | xmlDOMWrapCtxtPtr ret; |
| 7254 | |
| 7255 | ret = xmlMalloc(sizeof(xmlDOMWrapCtxt)); |
| 7256 | if (ret == NULL) { |
| 7257 | xmlTreeErrMemory("allocating DOM-wrapper context"); |
| 7258 | return (NULL); |
| 7259 | } |
| 7260 | memset(ret, 0, sizeof(xmlDOMWrapCtxt)); |
| 7261 | return (ret); |
| 7262 | } |
| 7263 | |
| 7264 | /* |
| 7265 | * xmlDOMWrapFreeCtxt: |
| 7266 | * @ctxt: the DOM-wrapper context |
| 7267 | * |
| 7268 | * Frees the DOM-wrapper context. |
| 7269 | */ |
| 7270 | void |
| 7271 | xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt) |
| 7272 | { |
| 7273 | if (ctxt == NULL) |
| 7274 | return; |
| 7275 | xmlFree(ctxt); |
| 7276 | } |
| 7277 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7278 | #define XML_TREE_NSMAP_PARENT -1 |
| 7279 | #define XML_TREE_NSMAP_XML -2 |
| 7280 | #define XML_TREE_NSMAP_DOC -3 |
| 7281 | #define XML_TREE_NSMAP_CUSTOM -4 |
| 7282 | |
| 7283 | typedef struct xmlNsMapItem *xmlNsMapItemPtr; |
| 7284 | struct xmlNsMapItem { |
| 7285 | xmlNsMapItemPtr next; |
| 7286 | xmlNsMapItemPtr prev; |
| 7287 | xmlNsPtr oldNs; /* old ns decl reference */ |
| 7288 | xmlNsPtr newNs; /* new ns decl reference */ |
| 7289 | int shadowDepth; /* Shadowed at this depth */ |
| 7290 | /* |
| 7291 | * depth: |
| 7292 | * >= 0 == @node's ns-decls |
| 7293 | * -1 == @parent's ns-decls |
| 7294 | * -2 == @parent's out-of-scope ns-decls |
| 7295 | * -3 == the doc->oldNs XML ns-decl |
| 7296 | * -4 == the doc->oldNs storage ns-decls |
| 7297 | */ |
| 7298 | int depth; |
| 7299 | }; |
| 7300 | |
| 7301 | /* |
| 7302 | * xmlTreeAddNsMapItem: |
| 7303 | * @map: the ns-map |
| 7304 | * @cur: the current map entry to append a new entry to |
| 7305 | * @oldNs: the old ns-struct |
| 7306 | * @newNs: the new ns-struct |
| 7307 | * @depth: depth and ns-kind information |
| 7308 | * |
| 7309 | * Frees the ns-map |
| 7310 | */ |
| 7311 | static xmlNsMapItemPtr |
| 7312 | xmlDOMWrapNSNormAddNsMapItem(xmlNsMapItemPtr *map, |
| 7313 | xmlNsMapItemPtr *cur, |
| 7314 | xmlNsPtr oldNs, |
| 7315 | xmlNsPtr newNs, |
| 7316 | int depth) |
| 7317 | { |
| 7318 | xmlNsMapItemPtr ret; |
| 7319 | |
| 7320 | if ((cur != NULL) && (*cur != NULL) && ((*cur)->next != NULL)) { |
| 7321 | /* |
| 7322 | * Reuse. |
| 7323 | */ |
| 7324 | ret = (*cur)->next; |
| 7325 | *cur = ret; |
| 7326 | } else { |
| 7327 | ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem)); |
| 7328 | if (ret == NULL) { |
| 7329 | xmlTreeErrMemory("allocating namespace map item"); |
| 7330 | return (NULL); |
| 7331 | } |
| 7332 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
| 7333 | if (*map == NULL) { |
| 7334 | /* |
| 7335 | * First ever. |
| 7336 | */ |
| 7337 | *map = ret; |
| 7338 | ret->prev = ret; |
| 7339 | if (cur != NULL) |
| 7340 | *cur = ret; |
| 7341 | } else { |
| 7342 | if (cur) { |
| 7343 | /* |
| 7344 | * Append. |
| 7345 | */ |
| 7346 | (*cur)->next = ret; |
| 7347 | ret->prev = *cur; |
| 7348 | *cur = ret; |
| 7349 | } else { |
| 7350 | /* |
| 7351 | * Set on first position. |
| 7352 | */ |
| 7353 | ret->next = (*map); |
| 7354 | ret->prev = (*map)->prev; |
| 7355 | (*map)->prev = ret; |
| 7356 | *map = ret; |
| 7357 | } |
| 7358 | } |
| 7359 | } |
| 7360 | ret->oldNs = oldNs; |
| 7361 | ret->newNs = newNs; |
| 7362 | ret->shadowDepth = -1; |
| 7363 | ret->depth = depth; |
| 7364 | return (ret); |
| 7365 | } |
| 7366 | |
| 7367 | /* |
| 7368 | * xmlTreeFreeNsMap: |
| 7369 | * @map: the ns-map |
| 7370 | * |
| 7371 | * Frees the ns-map |
| 7372 | */ |
| 7373 | static void |
| 7374 | xmlDOMWrapNSNormFreeNsMap(xmlNsMapItemPtr map) |
| 7375 | { |
| 7376 | xmlNsMapItemPtr mi = map, miprev; |
| 7377 | |
| 7378 | while (mi != NULL) { |
| 7379 | miprev = mi; |
| 7380 | mi = mi->next; |
| 7381 | xmlFree(miprev); |
| 7382 | } |
| 7383 | } |
| 7384 | |
| 7385 | /* |
| 7386 | * xmlTreeEnsureXMLDecl: |
| 7387 | * @doc: the doc |
| 7388 | * |
| 7389 | * Ensures that there is an XML namespace declaration on the doc. |
| 7390 | * |
| 7391 | * Returns the XML ns-struct or NULL on API and internal errors. |
| 7392 | */ |
| 7393 | static xmlNsPtr |
| 7394 | xmlTreeEnsureXMLDecl(xmlDocPtr doc) |
| 7395 | { |
| 7396 | if (doc == NULL) |
| 7397 | return (NULL); |
| 7398 | if (doc->oldNs != NULL) |
| 7399 | return (doc->oldNs); |
| 7400 | { |
| 7401 | xmlNsPtr ns; |
| 7402 | ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 7403 | if (ns == NULL) { |
| 7404 | xmlTreeErrMemory( |
| 7405 | "allocating the XML namespace"); |
| 7406 | return (NULL); |
| 7407 | } |
| 7408 | memset(ns, 0, sizeof(xmlNs)); |
| 7409 | ns->type = XML_LOCAL_NAMESPACE; |
| 7410 | ns->href = xmlStrdup(XML_XML_NAMESPACE); |
| 7411 | ns->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 7412 | doc->oldNs = ns; |
| 7413 | return (ns); |
| 7414 | } |
| 7415 | } |
| 7416 | |
| 7417 | /* |
| 7418 | * xmlDOMWrapStoreNs: |
| 7419 | * @doc: the doc |
| 7420 | * @nsName: the namespace name |
| 7421 | * @prefix: the prefix |
| 7422 | * |
| 7423 | * Creates or reuses an xmlNs struct on doc->oldNs with |
| 7424 | * the given prefix and namespace name. |
| 7425 | * |
| 7426 | * Returns the aquired ns struct or NULL in case of an API |
| 7427 | * or internal error. |
| 7428 | */ |
| 7429 | static xmlNsPtr |
| 7430 | xmlDOMWrapStoreNs(xmlDocPtr doc, |
| 7431 | const xmlChar *nsName, |
| 7432 | const xmlChar *prefix) |
| 7433 | { |
| 7434 | xmlNsPtr ns; |
| 7435 | |
| 7436 | if (doc == NULL) |
| 7437 | return (NULL); |
| 7438 | ns = xmlTreeEnsureXMLDecl(doc); |
| 7439 | if (ns == NULL) |
| 7440 | return (NULL); |
| 7441 | if (ns->next != NULL) { |
| 7442 | /* Reuse. */ |
| 7443 | ns = ns->next; |
| 7444 | while (ns != NULL) { |
| 7445 | if (((ns->prefix == prefix) || |
| 7446 | xmlStrEqual(ns->prefix, prefix)) && |
| 7447 | xmlStrEqual(ns->href, nsName)) { |
| 7448 | return (ns); |
| 7449 | } |
| 7450 | if (ns->next == NULL) |
| 7451 | break; |
| 7452 | ns = ns->next; |
| 7453 | } |
| 7454 | } |
| 7455 | /* Create. */ |
| 7456 | ns->next = xmlNewNs(NULL, nsName, prefix); |
| 7457 | return (ns->next); |
| 7458 | } |
| 7459 | |
| 7460 | /* |
| 7461 | * xmlTreeLookupNsListByPrefix: |
| 7462 | * @nsList: a list of ns-structs |
| 7463 | * @prefix: the searched prefix |
| 7464 | * |
| 7465 | * Searches for a ns-decl with the given prefix in @nsList. |
| 7466 | * |
| 7467 | * Returns the ns-decl if found, NULL if not found and on |
| 7468 | * API errors. |
| 7469 | */ |
| 7470 | static xmlNsPtr |
| 7471 | xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix) |
| 7472 | { |
| 7473 | if (nsList == NULL) |
| 7474 | return (NULL); |
| 7475 | { |
| 7476 | xmlNsPtr ns; |
| 7477 | ns = nsList; |
| 7478 | do { |
| 7479 | if ((prefix == ns->prefix) || |
| 7480 | xmlStrEqual(prefix, ns->prefix)) { |
| 7481 | return (ns); |
| 7482 | } |
| 7483 | ns = ns->next; |
| 7484 | } while (ns != NULL); |
| 7485 | } |
| 7486 | return (NULL); |
| 7487 | } |
| 7488 | |
| 7489 | /* |
| 7490 | * |
| 7491 | * xmlTreeGetInScopeNamespaces: |
| 7492 | * @map: the namespace map |
| 7493 | * @node: the node to start with |
| 7494 | * |
| 7495 | * Puts in-scope namespaces into the ns-map. |
| 7496 | * |
| 7497 | * Returns 0 on success, -1 on API or internal errors. |
| 7498 | */ |
| 7499 | static int |
| 7500 | xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapItemPtr *map, |
| 7501 | xmlNodePtr node) |
| 7502 | { |
| 7503 | xmlNodePtr cur; |
| 7504 | xmlNsPtr ns; |
| 7505 | xmlNsMapItemPtr mi; |
| 7506 | int shadowed; |
| 7507 | |
| 7508 | if ((map == NULL) || (*map != NULL)) |
| 7509 | return (-1); |
| 7510 | /* |
| 7511 | * Get in-scope ns-decls of @parent. |
| 7512 | */ |
| 7513 | cur = node; |
| 7514 | while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) { |
| 7515 | if (cur->type == XML_ELEMENT_NODE) { |
| 7516 | if (cur->nsDef != NULL) { |
| 7517 | ns = cur->nsDef; |
| 7518 | do { |
| 7519 | shadowed = 0; |
| 7520 | if (*map != NULL) { |
| 7521 | /* |
| 7522 | * Skip shadowed prefixes. |
| 7523 | */ |
| 7524 | for (mi = *map; mi != NULL; mi = mi->next) { |
| 7525 | if ((ns->prefix == mi->newNs->prefix) || |
| 7526 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) { |
| 7527 | shadowed = 1; |
| 7528 | break; |
| 7529 | } |
| 7530 | } |
| 7531 | } |
| 7532 | /* |
| 7533 | * Insert mapping. |
| 7534 | */ |
| 7535 | mi = xmlDOMWrapNSNormAddNsMapItem(map, NULL, NULL, |
| 7536 | ns, XML_TREE_NSMAP_PARENT); |
| 7537 | if (mi == NULL) |
| 7538 | return (-1); |
| 7539 | if (shadowed) |
| 7540 | mi->shadowDepth = 0; |
| 7541 | ns = ns->next; |
| 7542 | } while (ns != NULL); |
| 7543 | } |
| 7544 | } |
| 7545 | cur = cur->parent; |
| 7546 | } |
| 7547 | return (0); |
| 7548 | } |
| 7549 | |
| 7550 | /* |
| 7551 | * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict; |
| 7552 | * otherwise copy it, when it was in the source-dict. |
| 7553 | */ |
| 7554 | #define XML_TREE_ADOPT_STR(str) \ |
| 7555 | if (adoptStr && (str != NULL)) { \ |
| 7556 | if (destDoc->dict) { \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7557 | const xmlChar *old = str; \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7558 | str = xmlDictLookup(destDoc->dict, str, -1); \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7559 | if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \ |
| 7560 | (!xmlDictOwns(sourceDoc->dict, old))) \ |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 7561 | xmlFree((char *)old); \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7562 | } else if ((sourceDoc) && (sourceDoc->dict) && \ |
| 7563 | xmlDictOwns(sourceDoc->dict, str)) { \ |
| 7564 | str = BAD_CAST xmlStrdup(str); \ |
| 7565 | } \ |
| 7566 | } |
| 7567 | |
| 7568 | /* |
| 7569 | * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then |
| 7570 | * put it in dest-dict or copy it. |
| 7571 | */ |
| 7572 | #define XML_TREE_ADOPT_STR_2(str) \ |
| 7573 | if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \ |
| 7574 | (sourceDoc->dict != NULL) && \ |
| 7575 | xmlDictOwns(sourceDoc->dict, cur->content)) { \ |
| 7576 | if (destDoc->dict) \ |
| 7577 | cur->content = (xmlChar *) \ |
| 7578 | xmlDictLookup(destDoc->dict, cur->content, -1); \ |
| 7579 | else \ |
| 7580 | cur->content = xmlStrdup(BAD_CAST cur->content); \ |
| 7581 | } |
| 7582 | |
| 7583 | /* |
| 7584 | * xmlDOMWrapNSNormAddNsMapItem2: |
| 7585 | * |
| 7586 | * For internal use. Adds a ns-decl mapping. |
| 7587 | * |
| 7588 | * Returns 0 on success, -1 on internal errors. |
| 7589 | */ |
| 7590 | static int |
| 7591 | xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number, |
| 7592 | xmlNsPtr oldNs, xmlNsPtr newNs) |
| 7593 | { |
| 7594 | if (*list == NULL) { |
| 7595 | *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr)); |
| 7596 | if (*list == NULL) { |
| 7597 | xmlTreeErrMemory("alloc ns map item"); |
| 7598 | return(-1); |
| 7599 | } |
| 7600 | *size = 3; |
| 7601 | *number = 0; |
| 7602 | } else if ((*number) >= (*size)) { |
| 7603 | *size *= 2; |
| 7604 | *list = (xmlNsPtr *) xmlRealloc(*list, |
| 7605 | (*size) * 2 * sizeof(xmlNsPtr)); |
| 7606 | if (*list == NULL) { |
| 7607 | xmlTreeErrMemory("realloc ns map item"); |
| 7608 | return(-1); |
| 7609 | } |
| 7610 | } |
| 7611 | (*list)[2 * (*number)] = oldNs; |
| 7612 | (*list)[2 * (*number) +1] = newNs; |
| 7613 | (*number)++; |
| 7614 | return (0); |
| 7615 | } |
| 7616 | |
| 7617 | /* |
| 7618 | * xmlDOMWrapRemoveNode: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7619 | * @ctxt: a DOM wrapper context |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7620 | * @doc: the doc |
| 7621 | * @node: the node to be removed. |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7622 | * @options: set of options, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7623 | * |
| 7624 | * Unlinks the given node from its owner. |
| 7625 | * This will substitute ns-references to node->nsDef for |
| 7626 | * ns-references to doc->oldNs, thus ensuring the removed |
| 7627 | * branch to be autark wrt ns-references. |
Kasimier T. Buchcik | 017264f | 2005-06-27 13:45:24 +0000 | [diff] [blame] | 7628 | * WARNING: This function is in a experimental state. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7629 | * |
| 7630 | * Returns 0 on success, 1 if the node is not supported, |
| 7631 | * -1 on API and internal errors. |
| 7632 | */ |
| 7633 | int |
| 7634 | xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc, |
| 7635 | xmlNodePtr node, int options ATTRIBUTE_UNUSED) |
| 7636 | { |
| 7637 | xmlNsPtr *list = NULL; |
| 7638 | int sizeList, nbList, i, j; |
| 7639 | xmlNsPtr ns; |
| 7640 | |
| 7641 | if ((node == NULL) || (doc == NULL) || (node->doc != doc)) |
| 7642 | return (-1); |
| 7643 | |
| 7644 | /* TODO: 0 or -1 ? */ |
| 7645 | if (node->parent == NULL) |
| 7646 | return (0); |
| 7647 | |
| 7648 | switch (node->type) { |
| 7649 | case XML_TEXT_NODE: |
| 7650 | case XML_CDATA_SECTION_NODE: |
| 7651 | case XML_ENTITY_REF_NODE: |
| 7652 | case XML_PI_NODE: |
| 7653 | case XML_COMMENT_NODE: |
| 7654 | xmlUnlinkNode(node); |
| 7655 | return (0); |
| 7656 | case XML_ELEMENT_NODE: |
| 7657 | case XML_ATTRIBUTE_NODE: |
| 7658 | break; |
| 7659 | default: |
| 7660 | return (1); |
| 7661 | } |
| 7662 | xmlUnlinkNode(node); |
| 7663 | /* |
| 7664 | * Save out-of-scope ns-references in doc->oldNs. |
| 7665 | */ |
| 7666 | do { |
| 7667 | switch (node->type) { |
| 7668 | case XML_ELEMENT_NODE: |
| 7669 | if ((ctxt == NULL) && (node->nsDef != NULL)) { |
| 7670 | ns = node->nsDef; |
| 7671 | do { |
| 7672 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 7673 | &nbList, ns, ns) == -1) |
| 7674 | goto internal_error; |
| 7675 | ns = ns->next; |
| 7676 | } while (ns != NULL); |
| 7677 | } |
| 7678 | /* No break on purpose. */ |
| 7679 | case XML_ATTRIBUTE_NODE: |
| 7680 | if (node->ns != NULL) { |
| 7681 | /* |
| 7682 | * Find a mapping. |
| 7683 | */ |
| 7684 | if (list != NULL) { |
| 7685 | for (i = 0, j = 0; i < nbList; i++, j += 2) { |
| 7686 | if (node->ns == list[j]) { |
| 7687 | node->ns = list[++j]; |
| 7688 | goto next_node; |
| 7689 | } |
| 7690 | } |
| 7691 | } |
| 7692 | ns = NULL; |
| 7693 | if (ctxt != NULL) { |
| 7694 | /* |
| 7695 | * User defined. |
| 7696 | */ |
| 7697 | } else { |
| 7698 | /* |
| 7699 | * Add to doc's oldNs. |
| 7700 | */ |
| 7701 | ns = xmlDOMWrapStoreNs(doc, node->ns->href, |
| 7702 | node->ns->prefix); |
| 7703 | if (ns == NULL) |
| 7704 | goto internal_error; |
| 7705 | } |
| 7706 | if (ns != NULL) { |
| 7707 | /* |
| 7708 | * Add mapping. |
| 7709 | */ |
| 7710 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 7711 | &nbList, node->ns, ns) == -1) |
| 7712 | goto internal_error; |
| 7713 | } |
| 7714 | node->ns = ns; |
| 7715 | } |
| 7716 | if ((node->type == XML_ELEMENT_NODE) && |
| 7717 | (node->properties != NULL)) { |
| 7718 | node = (xmlNodePtr) node->properties; |
| 7719 | continue; |
| 7720 | } |
| 7721 | break; |
| 7722 | default: |
| 7723 | goto next_sibling; |
| 7724 | } |
| 7725 | next_node: |
| 7726 | if ((node->type == XML_ELEMENT_NODE) && |
| 7727 | (node->children != NULL)) { |
| 7728 | node = node->children; |
| 7729 | continue; |
| 7730 | } |
| 7731 | next_sibling: |
| 7732 | if (node == NULL) |
| 7733 | break; |
| 7734 | if (node->next != NULL) |
| 7735 | node = node->next; |
| 7736 | else { |
| 7737 | node = node->parent; |
| 7738 | goto next_sibling; |
| 7739 | } |
| 7740 | } while (node != NULL); |
| 7741 | |
| 7742 | if (list != NULL) |
| 7743 | xmlFree(list); |
| 7744 | return (0); |
| 7745 | |
| 7746 | internal_error: |
| 7747 | if (list != NULL) |
| 7748 | xmlFree(list); |
| 7749 | return (-1); |
| 7750 | } |
| 7751 | |
| 7752 | /* |
| 7753 | * xmlSearchNsByHrefStrict: |
| 7754 | * @doc: the document |
| 7755 | * @node: the start node |
| 7756 | * @nsName: the searched namespace name |
| 7757 | * @retNs: the resulting ns-decl |
| 7758 | * @prefixed: if the found ns-decl must have a prefix (for attributes) |
| 7759 | * |
| 7760 | * Dynamically searches for a ns-declaration which matches |
| 7761 | * the given @nsName in the ancestor-or-self axis of @node. |
| 7762 | * |
| 7763 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 7764 | * and internal errors. |
| 7765 | */ |
| 7766 | static int |
| 7767 | xmlSearchNsByHrefStrict(xmlDocPtr doc, xmlNodePtr node, const xmlChar* nsName, |
| 7768 | xmlNsPtr *retNs, int prefixed) |
| 7769 | { |
| 7770 | xmlNodePtr cur, prev = NULL, out = NULL; |
| 7771 | xmlNsPtr ns, prevns; |
| 7772 | |
| 7773 | if ((doc == NULL) || (nsName == NULL) || (retNs == NULL)) |
| 7774 | return (-1); |
| 7775 | |
| 7776 | *retNs = NULL; |
| 7777 | if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
| 7778 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 7779 | if (*retNs == NULL) |
| 7780 | return (-1); |
| 7781 | return (1); |
| 7782 | } |
| 7783 | cur = node; |
| 7784 | do { |
| 7785 | if (cur->type == XML_ELEMENT_NODE) { |
| 7786 | if (cur->nsDef != NULL) { |
| 7787 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 7788 | if (prefixed && (ns->prefix == NULL)) |
| 7789 | continue; |
| 7790 | if (prev != NULL) { |
| 7791 | /* |
| 7792 | * Check the last level of ns-decls for a |
| 7793 | * shadowing prefix. |
| 7794 | */ |
| 7795 | prevns = prev->nsDef; |
| 7796 | do { |
| 7797 | if ((prevns->prefix == ns->prefix) || |
| 7798 | ((prevns->prefix != NULL) && |
| 7799 | (ns->prefix != NULL) && |
| 7800 | xmlStrEqual(prevns->prefix, ns->prefix))) { |
| 7801 | /* |
| 7802 | * Shadowed. |
| 7803 | */ |
| 7804 | break; |
| 7805 | } |
| 7806 | prevns = prevns->next; |
| 7807 | } while (prevns != NULL); |
| 7808 | if (prevns != NULL) |
| 7809 | continue; |
| 7810 | } |
| 7811 | /* |
| 7812 | * Ns-name comparison. |
| 7813 | */ |
| 7814 | if ((nsName == ns->href) || |
| 7815 | xmlStrEqual(nsName, ns->href)) { |
| 7816 | /* |
| 7817 | * At this point the prefix can only be shadowed, |
| 7818 | * if we are the the (at least) 3rd level of |
| 7819 | * ns-decls. |
| 7820 | */ |
| 7821 | if (out) { |
| 7822 | int ret; |
| 7823 | |
| 7824 | ret = xmlNsInScope(doc, node, prev, ns->prefix); |
| 7825 | if (ret < 0) |
| 7826 | return (-1); |
| 7827 | /* |
| 7828 | * TODO: Should we try to find a matching ns-name |
| 7829 | * only once? This here keeps on searching. |
| 7830 | * I think we should try further since, there might |
| 7831 | * be an other matching ns-decl with an unshadowed |
| 7832 | * prefix. |
| 7833 | */ |
| 7834 | if (! ret) |
| 7835 | continue; |
| 7836 | } |
| 7837 | *retNs = ns; |
| 7838 | return (1); |
| 7839 | } |
| 7840 | } |
| 7841 | out = prev; |
| 7842 | prev = cur; |
| 7843 | } |
| 7844 | } else if ((node->type == XML_ENTITY_REF_NODE) || |
| 7845 | (node->type == XML_ENTITY_NODE) || |
| 7846 | (node->type == XML_ENTITY_DECL)) |
| 7847 | return (0); |
| 7848 | cur = cur->parent; |
| 7849 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 7850 | return (0); |
| 7851 | } |
| 7852 | |
| 7853 | /* |
| 7854 | * xmlDOMWrapNSNormDeclareNsForced: |
| 7855 | * @doc: the doc |
| 7856 | * @elem: the element-node to declare on |
| 7857 | * @nsName: the namespace-name of the ns-decl |
| 7858 | * @prefix: the preferred prefix of the ns-decl |
| 7859 | * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls |
| 7860 | * |
| 7861 | * Declares a new namespace on @elem. It tries to use the |
| 7862 | * given @prefix; if a ns-decl with the given prefix is already existent |
| 7863 | * on @elem, it will generate an other prefix. |
| 7864 | * |
| 7865 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 7866 | * and internal errors. |
| 7867 | */ |
| 7868 | static xmlNsPtr |
| 7869 | xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc, |
| 7870 | xmlNodePtr elem, |
| 7871 | const xmlChar *nsName, |
| 7872 | const xmlChar *prefix, |
| 7873 | int checkShadow) |
| 7874 | { |
| 7875 | |
| 7876 | xmlNsPtr ret; |
| 7877 | char buf[50]; |
| 7878 | const xmlChar *pref; |
| 7879 | int counter = 0; |
| 7880 | /* |
| 7881 | * Create a ns-decl on @anchor. |
| 7882 | */ |
| 7883 | pref = prefix; |
| 7884 | while (1) { |
| 7885 | /* |
| 7886 | * Lookup whether the prefix is unused in elem's ns-decls. |
| 7887 | */ |
| 7888 | if ((elem->nsDef != NULL) && |
| 7889 | (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL)) |
| 7890 | goto ns_next_prefix; |
| 7891 | if (checkShadow && elem->parent && |
| 7892 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 7893 | /* |
| 7894 | * Does it shadow ancestor ns-decls? |
| 7895 | */ |
| 7896 | if (xmlSearchNs(doc, elem->parent, pref) != NULL) |
| 7897 | goto ns_next_prefix; |
| 7898 | } |
| 7899 | ret = xmlNewNs(NULL, nsName, pref); |
| 7900 | if (ret == NULL) |
| 7901 | return (NULL); |
| 7902 | if (elem->nsDef == NULL) |
| 7903 | elem->nsDef = ret; |
| 7904 | else { |
| 7905 | xmlNsPtr ns2 = elem->nsDef; |
| 7906 | while (ns2->next != NULL) |
| 7907 | ns2 = ns2->next; |
| 7908 | ns2->next = ret; |
| 7909 | } |
| 7910 | return (ret); |
| 7911 | ns_next_prefix: |
| 7912 | counter++; |
| 7913 | if (counter > 1000) |
| 7914 | return (NULL); |
| 7915 | if (prefix == NULL) { |
| 7916 | snprintf((char *) buf, sizeof(buf), |
| 7917 | "default%d", counter); |
| 7918 | } else |
| 7919 | snprintf((char *) buf, sizeof(buf), |
| 7920 | "%.30s%d", (char *)prefix, counter); |
| 7921 | pref = BAD_CAST buf; |
| 7922 | } |
| 7923 | } |
| 7924 | |
| 7925 | /* |
| 7926 | * xmlDOMWrapNSNormAquireNormalizedNs: |
| 7927 | * @doc: the doc |
| 7928 | * @elem: the element-node to declare namespaces on |
| 7929 | * @ns: the ns-struct to use for the search |
| 7930 | * @retNs: the found/created ns-struct |
| 7931 | * @nsMap: the ns-map |
| 7932 | * @topmi: the last ns-map entry |
| 7933 | * @depth: the current tree depth |
| 7934 | * @ancestorsOnly: search in ancestor ns-decls only |
| 7935 | * @prefixed: if the searched ns-decl must have a prefix (for attributes) |
| 7936 | * |
| 7937 | * Searches for a matching ns-name in the ns-decls of @nsMap, if not |
| 7938 | * found it will either declare it on @elem, or store it in doc->oldNs. |
| 7939 | * If a new ns-decl needs to be declared on @elem, it tries to use the |
| 7940 | * @ns->prefix for it, if this prefix is already in use on @elem, it will |
| 7941 | * change the prefix or the new ns-decl. |
| 7942 | * |
| 7943 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 7944 | */ |
| 7945 | static int |
| 7946 | xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc, |
| 7947 | xmlNodePtr elem, |
| 7948 | xmlNsPtr ns, |
| 7949 | xmlNsPtr *retNs, |
| 7950 | xmlNsMapItemPtr *nsMap, |
| 7951 | xmlNsMapItemPtr *topmi, |
| 7952 | int depth, |
| 7953 | int ancestorsOnly, |
| 7954 | int prefixed) |
| 7955 | { |
| 7956 | xmlNsMapItemPtr mi; |
| 7957 | |
| 7958 | if ((doc == NULL) || (ns == NULL) || (retNs == NULL) || |
| 7959 | (nsMap == NULL) || (topmi == NULL)) |
| 7960 | return (-1); |
| 7961 | |
| 7962 | *retNs = NULL; |
| 7963 | /* |
| 7964 | * Handle XML namespace. |
| 7965 | */ |
| 7966 | if ((ns->prefix) && |
| 7967 | (ns->prefix[0] == 'x') && |
| 7968 | (ns->prefix[1] == 'm') && |
| 7969 | (ns->prefix[2] == 'l') && |
| 7970 | (ns->prefix[3] == 0)) { |
| 7971 | /* |
| 7972 | * Insert XML namespace mapping. |
| 7973 | */ |
| 7974 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 7975 | if (*retNs == NULL) |
| 7976 | return (-1); |
| 7977 | return (0); |
| 7978 | } |
| 7979 | /* |
| 7980 | * If the search should be done in ancestors only and no |
| 7981 | * @elem (the first ancestor) was specified, then skip the search. |
| 7982 | */ |
| 7983 | if ((! (ancestorsOnly && (elem == NULL))) && |
| 7984 | (*nsMap != NULL)) { |
| 7985 | |
| 7986 | /* |
| 7987 | * Try to find an equal ns-name in in-scope ns-decls. |
| 7988 | */ |
| 7989 | for (mi = *nsMap; mi != (*topmi)->next; mi = mi->next) { |
| 7990 | |
| 7991 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 7992 | /* |
| 7993 | * This should be turned on to gain speed, if one knows |
| 7994 | * that the branch itself was already ns-wellformed and no |
| 7995 | * stale references existed. I.e. it searches in the ancestor |
| 7996 | * axis only. |
| 7997 | */ |
| 7998 | ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) && |
| 7999 | /* Skip shadowed prefixes. */ |
| 8000 | (mi->shadowDepth == -1) && |
| 8001 | /* Skip xmlns="" or xmlns:foo="". */ |
| 8002 | ((mi->newNs->href != NULL) && |
| 8003 | (mi->newNs->href[0] != 0)) && |
| 8004 | /* Ensure a prefix if wanted. */ |
| 8005 | ((! prefixed) || (mi->newNs->prefix != NULL)) && |
| 8006 | /* Equal ns name */ |
| 8007 | ((mi->newNs->href == ns->href) || |
| 8008 | xmlStrEqual(mi->newNs->href, ns->href))) { |
| 8009 | /* Set the mapping. */ |
| 8010 | mi->oldNs = ns; |
| 8011 | *retNs = mi->newNs; |
| 8012 | return (0); |
| 8013 | } |
| 8014 | } |
| 8015 | } |
| 8016 | /* |
| 8017 | * No luck, the namespace is out of scope or shadowed. |
| 8018 | */ |
| 8019 | if (elem == NULL) { |
| 8020 | xmlNsPtr tmpns; |
| 8021 | |
| 8022 | /* |
| 8023 | * Store ns-decls in "oldNs" of the document-node. |
| 8024 | */ |
| 8025 | tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix); |
| 8026 | if (tmpns == NULL) |
| 8027 | return (-1); |
| 8028 | /* |
| 8029 | * Insert mapping. |
| 8030 | */ |
| 8031 | if (xmlDOMWrapNSNormAddNsMapItem(nsMap, NULL, ns, |
| 8032 | tmpns, XML_TREE_NSMAP_DOC) == NULL) { |
| 8033 | xmlFreeNs(tmpns); |
| 8034 | return (-1); |
| 8035 | } |
| 8036 | *retNs = tmpns; |
| 8037 | } else { |
| 8038 | xmlNsPtr tmpns; |
| 8039 | |
| 8040 | tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href, |
| 8041 | ns->prefix, 0); |
| 8042 | if (tmpns == NULL) |
| 8043 | return (-1); |
| 8044 | |
| 8045 | if (*nsMap != NULL) { |
| 8046 | /* |
| 8047 | * Does it shadow ancestor ns-decls? |
| 8048 | */ |
| 8049 | for (mi = *nsMap; mi != (*topmi)->next; mi = mi->next) { |
| 8050 | if ((mi->depth < depth) && |
| 8051 | (mi->shadowDepth == -1) && |
| 8052 | ((ns->prefix == mi->newNs->prefix) || |
| 8053 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8054 | /* |
| 8055 | * Shadows. |
| 8056 | */ |
| 8057 | mi->shadowDepth = depth; |
| 8058 | break; |
| 8059 | } |
| 8060 | } |
| 8061 | } |
| 8062 | if (xmlDOMWrapNSNormAddNsMapItem(nsMap, topmi, ns, |
| 8063 | tmpns, depth) == NULL) { |
| 8064 | xmlFreeNs(tmpns); |
| 8065 | return (-1); |
| 8066 | } |
| 8067 | *retNs = tmpns; |
| 8068 | } |
| 8069 | return (0); |
| 8070 | } |
| 8071 | |
| 8072 | /* |
| 8073 | * xmlDOMWrapReconcileNamespaces: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8074 | * @ctxt: DOM wrapper context, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8075 | * @elem: the element-node |
| 8076 | * @options: option flags |
| 8077 | * |
| 8078 | * Ensures that ns-references point to ns-decls hold on element-nodes. |
| 8079 | * Ensures that the tree is namespace wellformed by creating additional |
| 8080 | * ns-decls where needed. Note that, since prefixes of already existent |
| 8081 | * ns-decls can be shadowed by this process, it could break QNames in |
| 8082 | * attribute values or element content. |
Kasimier T. Buchcik | 017264f | 2005-06-27 13:45:24 +0000 | [diff] [blame] | 8083 | * WARNING: This function is in a experimental state. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8084 | * |
| 8085 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8086 | */ |
| 8087 | int |
| 8088 | xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8089 | xmlNodePtr elem, |
| 8090 | int options ATTRIBUTE_UNUSED) |
| 8091 | { |
| 8092 | int depth = -1, adoptns = 0, parnsdone = 0; |
| 8093 | xmlNsPtr ns; |
| 8094 | xmlDocPtr doc; |
| 8095 | xmlNodePtr cur, curElem = NULL; |
| 8096 | xmlNsMapItemPtr nsMap = NULL, topmi = NULL, mi; |
| 8097 | /* @ancestorsOnly should be set by an option flag. */ |
| 8098 | int ancestorsOnly = 0; |
| 8099 | |
| 8100 | if ((elem == NULL) || (elem->doc == NULL) || |
| 8101 | (elem->type != XML_ELEMENT_NODE)) |
| 8102 | return (-1); |
| 8103 | |
| 8104 | doc = elem->doc; |
| 8105 | cur = elem; |
| 8106 | do { |
| 8107 | switch (cur->type) { |
| 8108 | case XML_ELEMENT_NODE: |
| 8109 | adoptns = 1; |
| 8110 | curElem = cur; |
| 8111 | depth++; |
| 8112 | /* |
| 8113 | * Namespace declarations. |
| 8114 | */ |
| 8115 | if (cur->nsDef != NULL) { |
| 8116 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8117 | if (! parnsdone) { |
| 8118 | if ((elem->parent) && |
| 8119 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8120 | /* |
| 8121 | * Gather ancestor in-scope ns-decls. |
| 8122 | */ |
| 8123 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8124 | elem->parent) == -1) |
| 8125 | goto internal_error; |
| 8126 | if (nsMap != NULL) |
| 8127 | topmi = nsMap->prev; |
| 8128 | } |
| 8129 | parnsdone = 1; |
| 8130 | } |
| 8131 | /* |
| 8132 | * Skip ns-references handling if the referenced |
| 8133 | * ns-decl is declared on the same element. |
| 8134 | */ |
| 8135 | if ((cur->ns != NULL) && adoptns && (cur->ns == ns)) |
| 8136 | adoptns = 0; |
| 8137 | /* |
| 8138 | * Does it shadow any ns-decl? |
| 8139 | */ |
| 8140 | if (nsMap) { |
| 8141 | for (mi = nsMap; mi != topmi->next; mi = mi->next) { |
| 8142 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8143 | (mi->shadowDepth == -1) && |
| 8144 | ((ns->prefix == mi->newNs->prefix) || |
| 8145 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8146 | |
| 8147 | mi->shadowDepth = depth; |
| 8148 | } |
| 8149 | } |
| 8150 | } |
| 8151 | /* |
| 8152 | * Push mapping. |
| 8153 | */ |
| 8154 | if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi, ns, ns, |
| 8155 | depth) == NULL) |
| 8156 | goto internal_error; |
| 8157 | } |
| 8158 | } |
| 8159 | if (! adoptns) |
| 8160 | goto ns_end; |
| 8161 | |
| 8162 | /* No break on purpose. */ |
| 8163 | case XML_ATTRIBUTE_NODE: |
| 8164 | if (cur->ns == NULL) |
| 8165 | goto ns_end; |
| 8166 | if (! parnsdone) { |
| 8167 | if ((elem->parent) && |
| 8168 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8169 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8170 | elem->parent) == -1) |
| 8171 | goto internal_error; |
| 8172 | if (nsMap != NULL) |
| 8173 | topmi = nsMap->prev; |
| 8174 | } |
| 8175 | parnsdone = 1; |
| 8176 | } |
| 8177 | /* |
| 8178 | * Adopt ns-references. |
| 8179 | */ |
| 8180 | if (nsMap != NULL) { |
| 8181 | /* |
| 8182 | * Search for a mapping. |
| 8183 | */ |
| 8184 | for (mi = nsMap; mi != topmi->next; mi = mi->next) { |
| 8185 | if ((mi->shadowDepth == -1) && |
| 8186 | (cur->ns == mi->oldNs)) { |
| 8187 | |
| 8188 | cur->ns = mi->newNs; |
| 8189 | goto ns_end; |
| 8190 | } |
| 8191 | } |
| 8192 | } |
| 8193 | /* |
| 8194 | * Aquire a normalized ns-decl and add it to the map. |
| 8195 | */ |
| 8196 | if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem, |
| 8197 | cur->ns, &ns, |
| 8198 | &nsMap, &topmi, depth, |
| 8199 | ancestorsOnly, |
| 8200 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8201 | goto internal_error; |
| 8202 | cur->ns = ns; |
| 8203 | |
| 8204 | ns_end: |
| 8205 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8206 | (cur->properties != NULL)) { |
| 8207 | /* |
| 8208 | * Process attributes. |
| 8209 | */ |
| 8210 | cur = (xmlNodePtr) cur->properties; |
| 8211 | continue; |
| 8212 | } |
| 8213 | break; |
| 8214 | default: |
| 8215 | goto next_sibling; |
| 8216 | } |
| 8217 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8218 | (cur->children != NULL)) { |
| 8219 | /* |
| 8220 | * Process content of element-nodes only. |
| 8221 | */ |
| 8222 | cur = cur->children; |
| 8223 | continue; |
| 8224 | } |
| 8225 | next_sibling: |
| 8226 | if (cur == elem) |
| 8227 | break; |
| 8228 | if (cur->type == XML_ELEMENT_NODE) { |
| 8229 | if (nsMap != NULL) { |
| 8230 | /* |
| 8231 | * Pop mappings. |
| 8232 | */ |
| 8233 | while ((topmi->depth >= 0) && (topmi->depth >= depth)) |
| 8234 | topmi = topmi->prev; |
| 8235 | /* |
| 8236 | * Unshadow. |
| 8237 | */ |
| 8238 | for (mi = nsMap; mi != topmi->next; mi = mi->next) |
| 8239 | if (mi->shadowDepth >= depth) |
| 8240 | mi->shadowDepth = -1; |
| 8241 | } |
| 8242 | depth--; |
| 8243 | } |
| 8244 | if (cur->next != NULL) |
| 8245 | cur = cur->next; |
| 8246 | else { |
| 8247 | cur = cur->parent; |
| 8248 | goto next_sibling; |
| 8249 | } |
| 8250 | } while (cur != NULL); |
| 8251 | |
| 8252 | if (nsMap != NULL) |
| 8253 | xmlDOMWrapNSNormFreeNsMap(nsMap); |
| 8254 | return (0); |
| 8255 | internal_error: |
| 8256 | if (nsMap != NULL) |
| 8257 | xmlDOMWrapNSNormFreeNsMap(nsMap); |
| 8258 | return (-1); |
| 8259 | } |
| 8260 | |
| 8261 | /* |
| 8262 | * xmlDOMWrapAdoptBranch: |
| 8263 | * @ctxt: the optional context for custom processing |
| 8264 | * @sourceDoc: the optional sourceDoc |
| 8265 | * @node: the element-node to start with |
| 8266 | * @destDoc: the destination doc for adoption |
| 8267 | * @parent: the optional new parent of @node in @destDoc |
| 8268 | * @options: option flags |
| 8269 | * |
| 8270 | * Ensures that ns-references point to @destDoc: either to |
| 8271 | * elements->nsDef entries if @destParent is given, or to |
| 8272 | * @destDoc->oldNs otherwise. |
| 8273 | * If @destParent is given, it ensures that the tree is namespace |
| 8274 | * wellformed by creating additional ns-decls where needed. |
| 8275 | * Note that, since prefixes of already existent ns-decls can be |
| 8276 | * shadowed by this process, it could break QNames in attribute |
| 8277 | * values or element content. |
| 8278 | * |
| 8279 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8280 | */ |
| 8281 | static int |
| 8282 | xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt, |
| 8283 | xmlDocPtr sourceDoc, |
| 8284 | xmlNodePtr node, |
| 8285 | xmlDocPtr destDoc, |
| 8286 | xmlNodePtr destParent, |
| 8287 | int options ATTRIBUTE_UNUSED) |
| 8288 | { |
| 8289 | int ret = 0; |
| 8290 | xmlNodePtr cur, curElem = NULL; |
| 8291 | xmlNsMapItemPtr nsMap = NULL, topmi = NULL, mi; |
| 8292 | xmlNsPtr ns; |
| 8293 | int depth = -1, adoptStr = 1; |
| 8294 | /* gather @parent's ns-decls. */ |
| 8295 | int parnsdone = 0; |
| 8296 | /* @ancestorsOnly should be set per option. */ |
| 8297 | int ancestorsOnly = 0; |
| 8298 | |
| 8299 | /* |
| 8300 | * Optimize string adoption for equal or none dicts. |
| 8301 | */ |
| 8302 | if ((sourceDoc != NULL) && |
| 8303 | (sourceDoc->dict == destDoc->dict)) |
| 8304 | adoptStr = 0; |
| 8305 | else |
| 8306 | adoptStr = 1; |
| 8307 | |
| 8308 | cur = node; |
| 8309 | while (cur != NULL) { |
| 8310 | if (cur->doc != sourceDoc) { |
| 8311 | /* |
| 8312 | * We'll assume XIncluded nodes if the doc differs. |
| 8313 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 8314 | * This here skips XIncluded nodes and tries to handle |
| 8315 | * broken sequences. |
| 8316 | */ |
| 8317 | if (cur->next == NULL) |
| 8318 | goto leave_node; |
| 8319 | do { |
| 8320 | cur = cur->next; |
| 8321 | if ((cur->type == XML_XINCLUDE_END) || |
| 8322 | (cur->doc == node->doc)) |
| 8323 | break; |
| 8324 | } while (cur->next != NULL); |
| 8325 | |
| 8326 | if (cur->doc != node->doc) |
| 8327 | goto leave_node; |
| 8328 | } |
| 8329 | cur->doc = destDoc; |
| 8330 | switch (cur->type) { |
| 8331 | case XML_XINCLUDE_START: |
| 8332 | case XML_XINCLUDE_END: |
| 8333 | /* |
| 8334 | * TODO |
| 8335 | */ |
| 8336 | return (-1); |
| 8337 | case XML_ELEMENT_NODE: |
| 8338 | curElem = cur; |
| 8339 | depth++; |
| 8340 | /* |
| 8341 | * Namespace declarations. |
| 8342 | */ |
| 8343 | if ((ctxt == NULL) && (cur->nsDef != NULL)) { |
| 8344 | if (! parnsdone) { |
| 8345 | if (destParent && (ctxt == NULL)) { |
| 8346 | /* |
| 8347 | * Gather @parent's in-scope ns-decls. |
| 8348 | */ |
| 8349 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8350 | destParent) == -1) |
| 8351 | goto internal_error; |
| 8352 | if (nsMap != NULL) |
| 8353 | topmi = nsMap->prev; |
| 8354 | } |
| 8355 | parnsdone = 1; |
| 8356 | } |
| 8357 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8358 | /* |
| 8359 | * ns->prefix and ns->href seem not to be in the dict. |
| 8360 | * XML_TREE_ADOPT_STR(ns->prefix) |
| 8361 | * XML_TREE_ADOPT_STR(ns->href) |
| 8362 | */ |
| 8363 | /* |
| 8364 | * Does it shadow any ns-decl? |
| 8365 | */ |
| 8366 | if (nsMap) { |
| 8367 | for (mi = nsMap; mi != topmi->next; |
| 8368 | mi = mi->next) { |
| 8369 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8370 | (mi->shadowDepth == -1) && |
| 8371 | ((ns->prefix == mi->newNs->prefix) || |
| 8372 | xmlStrEqual(ns->prefix, |
| 8373 | mi->newNs->prefix))) { |
| 8374 | |
| 8375 | mi->shadowDepth = depth; |
| 8376 | } |
| 8377 | } |
| 8378 | } |
| 8379 | /* |
| 8380 | * Push mapping. |
| 8381 | */ |
| 8382 | if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi, |
| 8383 | ns, ns, depth) == NULL) |
| 8384 | goto internal_error; |
| 8385 | } |
| 8386 | } |
| 8387 | /* No break on purpose. */ |
| 8388 | case XML_ATTRIBUTE_NODE: |
| 8389 | |
| 8390 | if (cur->ns == NULL) |
| 8391 | goto ns_end; |
| 8392 | if (! parnsdone) { |
| 8393 | if (destParent && (ctxt == NULL)) { |
| 8394 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8395 | destParent) == -1) |
| 8396 | goto internal_error; |
| 8397 | if (nsMap != NULL) |
| 8398 | topmi = nsMap->prev; |
| 8399 | } |
| 8400 | parnsdone = 1; |
| 8401 | } |
| 8402 | /* |
| 8403 | * Adopt ns-references. |
| 8404 | */ |
| 8405 | if (nsMap != NULL) { |
| 8406 | /* |
| 8407 | * Search for a mapping. |
| 8408 | */ |
| 8409 | for (mi = nsMap; mi != topmi->next; mi = mi->next) { |
| 8410 | if ((mi->shadowDepth == -1) && |
| 8411 | (cur->ns == mi->oldNs)) { |
| 8412 | |
| 8413 | cur->ns = mi->newNs; |
| 8414 | goto ns_end; |
| 8415 | } |
| 8416 | } |
| 8417 | } |
| 8418 | /* |
| 8419 | * Start searching for an in-scope ns-decl. |
| 8420 | */ |
| 8421 | if (ctxt != NULL) { |
| 8422 | /* |
| 8423 | * User-defined behaviour. |
| 8424 | */ |
| 8425 | #if 0 |
| 8426 | ctxt->aquireNsDecl(ctxt, cur->ns, &ns); |
| 8427 | #endif |
| 8428 | /* |
| 8429 | * Insert mapping if ns is available; it's the users fault |
| 8430 | * if not. |
| 8431 | */ |
| 8432 | if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi, |
| 8433 | ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
| 8434 | goto internal_error; |
| 8435 | cur->ns = ns; |
| 8436 | } else { |
| 8437 | /* |
| 8438 | * Aquire a normalized ns-decl and add it to the map. |
| 8439 | */ |
| 8440 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
| 8441 | /* ns-decls on curElem or on destDoc->oldNs */ |
| 8442 | destParent ? curElem : NULL, |
| 8443 | cur->ns, &ns, |
| 8444 | &nsMap, &topmi, depth, |
| 8445 | ancestorsOnly, |
| 8446 | /* ns-decls must be prefixed for attributes. */ |
| 8447 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8448 | goto internal_error; |
| 8449 | cur->ns = ns; |
| 8450 | } |
| 8451 | ns_end: |
| 8452 | /* |
| 8453 | * Further node properties. |
| 8454 | * TODO: Is this all? |
| 8455 | */ |
| 8456 | XML_TREE_ADOPT_STR(cur->name) |
| 8457 | if (cur->type == XML_ELEMENT_NODE) { |
| 8458 | cur->psvi = NULL; |
| 8459 | cur->line = 0; |
| 8460 | cur->extra = 0; |
| 8461 | /* |
| 8462 | * Walk attributes. |
| 8463 | */ |
| 8464 | if (cur->properties != NULL) { |
| 8465 | /* |
| 8466 | * Process first attribute node. |
| 8467 | */ |
| 8468 | cur = (xmlNodePtr) cur->properties; |
| 8469 | continue; |
| 8470 | } |
| 8471 | } else { |
| 8472 | /* |
| 8473 | * Attributes. |
| 8474 | */ |
| 8475 | if ((sourceDoc != NULL) && |
| 8476 | (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID)) |
| 8477 | xmlRemoveID(sourceDoc, (xmlAttrPtr) cur); |
| 8478 | ((xmlAttrPtr) cur)->atype = 0; |
| 8479 | ((xmlAttrPtr) cur)->psvi = NULL; |
| 8480 | } |
| 8481 | break; |
| 8482 | case XML_TEXT_NODE: |
| 8483 | case XML_CDATA_SECTION_NODE: |
| 8484 | /* |
| 8485 | * This puts the content in the dest dict, only if |
| 8486 | * it was previously in the source dict. |
| 8487 | */ |
| 8488 | XML_TREE_ADOPT_STR_2(cur->content) |
| 8489 | goto leave_node; |
| 8490 | case XML_ENTITY_REF_NODE: |
| 8491 | /* |
| 8492 | * Remove reference to the entitity-node. |
| 8493 | */ |
| 8494 | cur->content = NULL; |
| 8495 | cur->children = NULL; |
| 8496 | cur->last = NULL; |
| 8497 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 8498 | xmlEntityPtr ent; |
| 8499 | /* |
| 8500 | * Assign new entity-node if available. |
| 8501 | */ |
| 8502 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 8503 | if (ent != NULL) { |
| 8504 | cur->content = ent->content; |
| 8505 | cur->children = (xmlNodePtr) ent; |
| 8506 | cur->last = (xmlNodePtr) ent; |
| 8507 | } |
| 8508 | } |
| 8509 | goto leave_node; |
| 8510 | case XML_PI_NODE: |
| 8511 | XML_TREE_ADOPT_STR(cur->name) |
| 8512 | XML_TREE_ADOPT_STR_2(cur->content) |
| 8513 | break; |
| 8514 | case XML_COMMENT_NODE: |
| 8515 | break; |
| 8516 | default: |
| 8517 | goto internal_error; |
| 8518 | } |
| 8519 | /* |
| 8520 | * Walk the tree. |
| 8521 | */ |
| 8522 | if (cur->children != NULL) { |
| 8523 | cur = cur->children; |
| 8524 | continue; |
| 8525 | } |
| 8526 | |
| 8527 | leave_node: |
| 8528 | if (cur == node) |
| 8529 | break; |
| 8530 | if ((cur->type == XML_ELEMENT_NODE) || |
| 8531 | (cur->type == XML_XINCLUDE_START) || |
| 8532 | (cur->type == XML_XINCLUDE_END)) { |
| 8533 | /* |
| 8534 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 8535 | */ |
| 8536 | if (nsMap != NULL) { |
| 8537 | /* |
| 8538 | * Pop mappings. |
| 8539 | */ |
| 8540 | while (topmi->depth >= depth) |
| 8541 | topmi = topmi->prev; |
| 8542 | /* |
| 8543 | * Unshadow. |
| 8544 | */ |
| 8545 | for (mi = nsMap; mi != topmi->next; mi = mi->next) |
| 8546 | if (mi->shadowDepth >= depth) |
| 8547 | mi->shadowDepth = -1; |
| 8548 | } |
| 8549 | depth--; |
| 8550 | } |
| 8551 | if (cur->next != NULL) |
| 8552 | cur = cur->next; |
| 8553 | else { |
| 8554 | cur = cur->parent; |
| 8555 | goto leave_node; |
| 8556 | } |
| 8557 | } |
| 8558 | /* |
| 8559 | * Cleanup. |
| 8560 | */ |
| 8561 | if (nsMap != NULL) |
| 8562 | xmlDOMWrapNSNormFreeNsMap(nsMap); |
| 8563 | return (ret); |
| 8564 | internal_error: |
| 8565 | if (nsMap != NULL) |
| 8566 | xmlDOMWrapNSNormFreeNsMap(nsMap); |
| 8567 | return (-1); |
| 8568 | } |
| 8569 | |
| 8570 | /* |
| 8571 | * xmlDOMWrapAdoptAttr: |
| 8572 | * @ctxt: the optional context for custom processing |
| 8573 | * @sourceDoc: the optional source document of attr |
| 8574 | * @attr: the attribute-node to be adopted |
| 8575 | * @destDoc: the destination doc for adoption |
| 8576 | * @destParent: the optional new parent of @attr in @destDoc |
| 8577 | * @options: option flags |
| 8578 | * |
| 8579 | * @attr is adopted by @destDoc. |
| 8580 | * Ensures that ns-references point to @destDoc: either to |
| 8581 | * elements->nsDef entries if @destParent is given, or to |
| 8582 | * @destDoc->oldNs otherwise. |
| 8583 | * |
| 8584 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8585 | */ |
| 8586 | static int |
| 8587 | xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt, |
| 8588 | xmlDocPtr sourceDoc, |
| 8589 | xmlAttrPtr attr, |
| 8590 | xmlDocPtr destDoc, |
| 8591 | xmlNodePtr destParent, |
| 8592 | int options ATTRIBUTE_UNUSED) |
| 8593 | { |
| 8594 | xmlNodePtr cur; |
| 8595 | int adoptStr = 1; |
| 8596 | |
| 8597 | if ((attr == NULL) || (destDoc == NULL)) |
| 8598 | return (-1); |
| 8599 | |
| 8600 | attr->doc = destDoc; |
| 8601 | if (attr->ns != NULL) { |
| 8602 | xmlNsPtr ns = NULL; |
| 8603 | |
| 8604 | if (ctxt != NULL) { |
| 8605 | /* TODO: User defined. */ |
| 8606 | } |
| 8607 | /* XML Namespace. */ |
| 8608 | if ((attr->ns->prefix[0] == 'x') && (attr->ns->prefix[1] == 'm') && |
| 8609 | (attr->ns->prefix[2] == 'l') && (attr->ns->prefix[3] == 0)) { |
| 8610 | ns = xmlTreeEnsureXMLDecl(destDoc); |
| 8611 | } else if (destParent == NULL) { |
| 8612 | /* |
| 8613 | * Store in @destDoc->oldNs. |
| 8614 | */ |
| 8615 | ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix); |
| 8616 | } else { |
| 8617 | /* |
| 8618 | * Declare on @destParent. |
| 8619 | */ |
| 8620 | if (xmlSearchNsByHrefStrict(destDoc, destParent, attr->ns->href, |
| 8621 | &ns, 1) == -1) |
| 8622 | goto internal_error; |
| 8623 | if (ns == NULL) { |
| 8624 | ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent, |
| 8625 | attr->ns->href, attr->ns->prefix, 1); |
| 8626 | } |
| 8627 | } |
| 8628 | if (ns == NULL) |
| 8629 | goto internal_error; |
| 8630 | attr->ns = ns; |
| 8631 | } |
| 8632 | |
| 8633 | XML_TREE_ADOPT_STR(attr->name); |
| 8634 | attr->atype = 0; |
| 8635 | attr->psvi = NULL; |
| 8636 | /* |
| 8637 | * Walk content. |
| 8638 | */ |
| 8639 | if (attr->children == NULL) |
| 8640 | return (0); |
| 8641 | cur = attr->children; |
| 8642 | while (cur != NULL) { |
| 8643 | cur->doc = destDoc; |
| 8644 | switch (cur->type) { |
| 8645 | case XML_TEXT_NODE: |
| 8646 | case XML_CDATA_SECTION_NODE: |
| 8647 | XML_TREE_ADOPT_STR_2(cur->content) |
| 8648 | break; |
| 8649 | case XML_ENTITY_REF_NODE: |
| 8650 | /* |
| 8651 | * Remove reference to the entitity-node. |
| 8652 | */ |
| 8653 | cur->content = NULL; |
| 8654 | cur->children = NULL; |
| 8655 | cur->last = NULL; |
| 8656 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 8657 | xmlEntityPtr ent; |
| 8658 | /* |
| 8659 | * Assign new entity-node if available. |
| 8660 | */ |
| 8661 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 8662 | if (ent != NULL) { |
| 8663 | cur->content = ent->content; |
| 8664 | cur->children = (xmlNodePtr) ent; |
| 8665 | cur->last = (xmlNodePtr) ent; |
| 8666 | } |
| 8667 | } |
| 8668 | break; |
| 8669 | default: |
| 8670 | break; |
| 8671 | } |
| 8672 | if (cur->children != NULL) { |
| 8673 | cur = cur->children; |
| 8674 | continue; |
| 8675 | } |
| 8676 | next_sibling: |
| 8677 | if (cur == (xmlNodePtr) attr) |
| 8678 | break; |
| 8679 | if (cur->next != NULL) |
| 8680 | cur = cur->next; |
| 8681 | else { |
| 8682 | cur = cur->parent; |
| 8683 | goto next_sibling; |
| 8684 | } |
| 8685 | } |
| 8686 | return (0); |
| 8687 | internal_error: |
| 8688 | return (-1); |
| 8689 | } |
| 8690 | |
| 8691 | /* |
| 8692 | * xmlDOMWrapAdoptNode: |
| 8693 | * @ctxt: the optional context for custom processing |
| 8694 | * @sourceDoc: the optional sourceDoc |
| 8695 | * @node: the node to start with |
| 8696 | * @destDoc: the destination doc |
Kasimier T. Buchcik | 4d9c948 | 2005-06-27 15:04:46 +0000 | [diff] [blame] | 8697 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8698 | * @options: option flags |
| 8699 | * |
| 8700 | * Ensures that ns-references point to @destDoc: either to |
| 8701 | * elements->nsDef entries if @destParent is given, or to |
| 8702 | * @destDoc->oldNs otherwise. |
| 8703 | * If @destParent is given, it ensures that the tree is namespace |
| 8704 | * wellformed by creating additional ns-decls where needed. |
| 8705 | * Note that, since prefixes of already existent ns-decls can be |
| 8706 | * shadowed by this process, it could break QNames in attribute |
| 8707 | * values or element content. |
Kasimier T. Buchcik | 017264f | 2005-06-27 13:45:24 +0000 | [diff] [blame] | 8708 | * WARNING: This function is in a experimental state. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8709 | * |
| 8710 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8711 | */ |
| 8712 | int |
| 8713 | xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt, |
| 8714 | xmlDocPtr sourceDoc, |
| 8715 | xmlNodePtr node, |
| 8716 | xmlDocPtr destDoc, |
| 8717 | xmlNodePtr destParent, |
| 8718 | int options) |
| 8719 | { |
| 8720 | if ((node == NULL) || (destDoc == NULL) || |
| 8721 | ((destParent != NULL) && (destParent->doc != destDoc))) |
| 8722 | return(-1); |
| 8723 | /* |
| 8724 | * Check node->doc sanity. |
| 8725 | */ |
| 8726 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 8727 | (node->doc != sourceDoc)) { |
| 8728 | /* |
| 8729 | * Might be an XIncluded node. |
| 8730 | */ |
| 8731 | return (-1); |
| 8732 | } |
| 8733 | if (sourceDoc == NULL) |
| 8734 | sourceDoc = node->doc; |
| 8735 | if (sourceDoc == destDoc) |
| 8736 | return (-1); |
| 8737 | switch (node->type) { |
| 8738 | case XML_ELEMENT_NODE: |
| 8739 | case XML_ATTRIBUTE_NODE: |
| 8740 | case XML_TEXT_NODE: |
| 8741 | case XML_CDATA_SECTION_NODE: |
| 8742 | case XML_ENTITY_REF_NODE: |
| 8743 | case XML_PI_NODE: |
| 8744 | case XML_COMMENT_NODE: |
| 8745 | break; |
| 8746 | case XML_DOCUMENT_FRAG_NODE: |
| 8747 | return (2); |
| 8748 | default: |
| 8749 | return (1); |
| 8750 | } |
| 8751 | /* |
| 8752 | * Unlink only if @node was not already added to @destParent. |
| 8753 | */ |
| 8754 | if ((node->parent != NULL) && (destParent != node->parent)) |
| 8755 | xmlUnlinkNode(node); |
| 8756 | |
| 8757 | if (node->type == XML_ELEMENT_NODE) { |
| 8758 | return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node, |
| 8759 | destDoc, destParent, options)); |
| 8760 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
| 8761 | return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc, |
| 8762 | (xmlAttrPtr) node, destDoc, destParent, options)); |
| 8763 | } else { |
| 8764 | xmlNodePtr cur = node; |
| 8765 | int adoptStr = 1; |
| 8766 | |
| 8767 | cur->doc = destDoc; |
| 8768 | /* |
| 8769 | * Optimize string adoption. |
| 8770 | */ |
| 8771 | if ((sourceDoc != NULL) && |
| 8772 | (sourceDoc->dict == destDoc->dict)) |
| 8773 | adoptStr = 0; |
| 8774 | switch (node->type) { |
| 8775 | case XML_TEXT_NODE: |
| 8776 | case XML_CDATA_SECTION_NODE: |
| 8777 | XML_TREE_ADOPT_STR_2(node->content) |
| 8778 | break; |
| 8779 | case XML_ENTITY_REF_NODE: |
| 8780 | /* |
| 8781 | * Remove reference to the entitity-node. |
| 8782 | */ |
| 8783 | node->content = NULL; |
| 8784 | node->children = NULL; |
| 8785 | node->last = NULL; |
| 8786 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 8787 | xmlEntityPtr ent; |
| 8788 | /* |
| 8789 | * Assign new entity-node if available. |
| 8790 | */ |
| 8791 | ent = xmlGetDocEntity(destDoc, node->name); |
| 8792 | if (ent != NULL) { |
| 8793 | node->content = ent->content; |
| 8794 | node->children = (xmlNodePtr) ent; |
| 8795 | node->last = (xmlNodePtr) ent; |
| 8796 | } |
| 8797 | } |
| 8798 | XML_TREE_ADOPT_STR(node->name) |
| 8799 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 8800 | case XML_PI_NODE: { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8801 | XML_TREE_ADOPT_STR(node->name) |
| 8802 | XML_TREE_ADOPT_STR_2(node->content) |
| 8803 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 8804 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8805 | default: |
| 8806 | break; |
| 8807 | } |
| 8808 | } |
| 8809 | return (0); |
| 8810 | } |
| 8811 | |
| 8812 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 8813 | #define bottom_tree |
| 8814 | #include "elfgcchack.h" |