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 ! */ |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 17 | #include <limits.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 18 | #ifdef HAVE_CTYPE_H |
| 19 | #include <ctype.h> |
| 20 | #endif |
| 21 | #ifdef HAVE_STDLIB_H |
| 22 | #include <stdlib.h> |
| 23 | #endif |
| 24 | #ifdef HAVE_ZLIB_H |
| 25 | #include <zlib.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <libxml/xmlmemory.h> |
| 29 | #include <libxml/tree.h> |
| 30 | #include <libxml/parser.h> |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 31 | #include <libxml/uri.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 32 | #include <libxml/entities.h> |
| 33 | #include <libxml/valid.h> |
| 34 | #include <libxml/xmlerror.h> |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 35 | #include <libxml/parserInternals.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 36 | #include <libxml/globals.h> |
Daniel Veillard | d5c2f92 | 2002-11-21 14:10:52 +0000 | [diff] [blame] | 37 | #ifdef LIBXML_HTML_ENABLED |
| 38 | #include <libxml/HTMLtree.h> |
| 39 | #endif |
William M. Brack | 1d8c9b2 | 2004-12-25 10:14:57 +0000 | [diff] [blame] | 40 | #ifdef LIBXML_DEBUG_ENABLED |
| 41 | #include <libxml/debugXML.h> |
| 42 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 43 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 44 | int __xmlRegisterCallbacks = 0; |
| 45 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 46 | /************************************************************************ |
| 47 | * * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 48 | * Forward declarations * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 49 | * * |
| 50 | ************************************************************************/ |
| 51 | |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 52 | static xmlNsPtr |
| 53 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 54 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 55 | static xmlChar* xmlGetPropNodeValueInternal(xmlAttrPtr prop); |
| 56 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 57 | /************************************************************************ |
| 58 | * * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 59 | * Tree memory error handler * |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 60 | * * |
| 61 | ************************************************************************/ |
| 62 | /** |
| 63 | * xmlTreeErrMemory: |
| 64 | * @extra: extra informations |
| 65 | * |
| 66 | * Handle an out of memory condition |
| 67 | */ |
| 68 | static void |
| 69 | xmlTreeErrMemory(const char *extra) |
| 70 | { |
| 71 | __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * xmlTreeErr: |
| 76 | * @code: the error number |
| 77 | * @extra: extra informations |
| 78 | * |
| 79 | * Handle an out of memory condition |
| 80 | */ |
| 81 | static void |
| 82 | xmlTreeErr(int code, xmlNodePtr node, const char *extra) |
| 83 | { |
| 84 | const char *msg = NULL; |
| 85 | |
| 86 | switch(code) { |
| 87 | case XML_TREE_INVALID_HEX: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 88 | msg = "invalid hexadecimal character value\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 89 | break; |
| 90 | case XML_TREE_INVALID_DEC: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 91 | msg = "invalid decimal character value\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 92 | break; |
| 93 | case XML_TREE_UNTERMINATED_ENTITY: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 94 | msg = "unterminated entity reference %15s\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 95 | break; |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame] | 96 | case XML_TREE_NOT_UTF8: |
| 97 | msg = "string is not in UTF-8\n"; |
| 98 | break; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 99 | default: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 100 | msg = "unexpected error number\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 101 | } |
| 102 | __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra); |
| 103 | } |
| 104 | |
| 105 | /************************************************************************ |
| 106 | * * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 107 | * A few static variables and macros * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 108 | * * |
| 109 | ************************************************************************/ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 110 | /* #undef xmlStringText */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 111 | const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 112 | /* #undef xmlStringTextNoenc */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 113 | const xmlChar xmlStringTextNoenc[] = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 114 | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 115 | /* #undef xmlStringComment */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 116 | const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
| 117 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 118 | static int xmlCompressMode = 0; |
| 119 | static int xmlCheckDTD = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 120 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 121 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
| 122 | xmlNodePtr ulccur = (n)->children; \ |
| 123 | if (ulccur == NULL) { \ |
| 124 | (n)->last = NULL; \ |
| 125 | } else { \ |
| 126 | while (ulccur->next != NULL) { \ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 127 | ulccur->parent = (n); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 128 | ulccur = ulccur->next; \ |
| 129 | } \ |
| 130 | ulccur->parent = (n); \ |
| 131 | (n)->last = ulccur; \ |
| 132 | }} |
| 133 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 134 | #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \ |
| 135 | (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0)) |
| 136 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 137 | /* #define DEBUG_BUFFER */ |
| 138 | /* #define DEBUG_TREE */ |
| 139 | |
| 140 | /************************************************************************ |
| 141 | * * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 142 | * Functions to move to entities.c once the * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 143 | * API freeze is smoothen and they can be made public. * |
| 144 | * * |
| 145 | ************************************************************************/ |
| 146 | #include <libxml/hash.h> |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 147 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 148 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 149 | /** |
| 150 | * xmlGetEntityFromDtd: |
| 151 | * @dtd: A pointer to the DTD to search |
| 152 | * @name: The entity name |
| 153 | * |
| 154 | * Do an entity lookup in the DTD entity hash table and |
| 155 | * return the corresponding entity, if found. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 156 | * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 157 | * Returns A pointer to the entity structure or NULL if not found. |
| 158 | */ |
| 159 | static xmlEntityPtr |
| 160 | xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 161 | xmlEntitiesTablePtr table; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 162 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 163 | if((dtd != NULL) && (dtd->entities != NULL)) { |
| 164 | table = (xmlEntitiesTablePtr) dtd->entities; |
| 165 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 166 | /* return(xmlGetEntityFromTable(table, name)); */ |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 167 | } |
| 168 | return(NULL); |
| 169 | } |
| 170 | /** |
| 171 | * xmlGetParameterEntityFromDtd: |
| 172 | * @dtd: A pointer to the DTD to search |
| 173 | * @name: The entity name |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 174 | * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 175 | * Do an entity lookup in the DTD pararmeter entity hash table and |
| 176 | * return the corresponding entity, if found. |
| 177 | * |
| 178 | * Returns A pointer to the entity structure or NULL if not found. |
| 179 | */ |
| 180 | static xmlEntityPtr |
| 181 | xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 182 | xmlEntitiesTablePtr table; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 183 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 184 | if ((dtd != NULL) && (dtd->pentities != NULL)) { |
| 185 | table = (xmlEntitiesTablePtr) dtd->pentities; |
| 186 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 187 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 188 | } |
| 189 | return(NULL); |
| 190 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 191 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 192 | |
| 193 | /************************************************************************ |
| 194 | * * |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 195 | * QName handling helper * |
| 196 | * * |
| 197 | ************************************************************************/ |
| 198 | |
| 199 | /** |
| 200 | * xmlBuildQName: |
| 201 | * @ncname: the Name |
| 202 | * @prefix: the prefix |
| 203 | * @memory: preallocated memory |
| 204 | * @len: preallocated memory length |
| 205 | * |
| 206 | * Builds the QName @prefix:@ncname in @memory if there is enough space |
| 207 | * and prefix is not NULL nor empty, otherwise allocate a new string. |
| 208 | * If prefix is NULL or empty it returns ncname. |
| 209 | * |
| 210 | * Returns the new string which must be freed by the caller if different from |
| 211 | * @memory and @ncname or NULL in case of error |
| 212 | */ |
| 213 | xmlChar * |
| 214 | xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix, |
| 215 | xmlChar *memory, int len) { |
| 216 | int lenn, lenp; |
| 217 | xmlChar *ret; |
| 218 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 219 | if (ncname == NULL) return(NULL); |
| 220 | if (prefix == NULL) return((xmlChar *) ncname); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 221 | |
| 222 | lenn = strlen((char *) ncname); |
| 223 | lenp = strlen((char *) prefix); |
| 224 | |
| 225 | if ((memory == NULL) || (len < lenn + lenp + 2)) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 226 | ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 227 | if (ret == NULL) { |
| 228 | xmlTreeErrMemory("building QName"); |
| 229 | return(NULL); |
| 230 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 231 | } else { |
| 232 | ret = memory; |
| 233 | } |
| 234 | memcpy(&ret[0], prefix, lenp); |
| 235 | ret[lenp] = ':'; |
| 236 | memcpy(&ret[lenp + 1], ncname, lenn); |
| 237 | ret[lenn + lenp + 1] = 0; |
| 238 | return(ret); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * xmlSplitQName2: |
| 243 | * @name: the full QName |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 244 | * @prefix: a xmlChar ** |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 245 | * |
| 246 | * parse an XML qualified name string |
| 247 | * |
| 248 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 249 | * |
| 250 | * [NS 6] Prefix ::= NCName |
| 251 | * |
| 252 | * [NS 7] LocalPart ::= NCName |
| 253 | * |
| 254 | * Returns NULL if not a QName, otherwise the local part, and prefix |
| 255 | * is updated to get the Prefix if any. |
| 256 | */ |
| 257 | |
| 258 | xmlChar * |
| 259 | xmlSplitQName2(const xmlChar *name, xmlChar **prefix) { |
| 260 | int len = 0; |
| 261 | xmlChar *ret = NULL; |
| 262 | |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 263 | if (prefix == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 264 | *prefix = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 265 | if (name == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 266 | |
| 267 | #ifndef XML_XML_NAMESPACE |
| 268 | /* xml: prefix is not really a namespace */ |
| 269 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 270 | (name[2] == 'l') && (name[3] == ':')) |
| 271 | return(NULL); |
| 272 | #endif |
| 273 | |
| 274 | /* nasty but valid */ |
| 275 | if (name[0] == ':') |
| 276 | return(NULL); |
| 277 | |
| 278 | /* |
| 279 | * we are not trying to validate but just to cut, and yes it will |
| 280 | * work even if this is as set of UTF-8 encoded chars |
| 281 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 282 | while ((name[len] != 0) && (name[len] != ':')) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 283 | len++; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 284 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 285 | if (name[len] == 0) |
| 286 | return(NULL); |
| 287 | |
| 288 | *prefix = xmlStrndup(name, len); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 289 | if (*prefix == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 290 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 291 | return(NULL); |
| 292 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 293 | ret = xmlStrdup(&name[len + 1]); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 294 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 295 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 296 | if (*prefix != NULL) { |
| 297 | xmlFree(*prefix); |
| 298 | *prefix = NULL; |
| 299 | } |
| 300 | return(NULL); |
| 301 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 302 | |
| 303 | return(ret); |
| 304 | } |
| 305 | |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 306 | /** |
| 307 | * xmlSplitQName3: |
| 308 | * @name: the full QName |
| 309 | * @len: an int * |
| 310 | * |
| 311 | * parse an XML qualified name string,i |
| 312 | * |
| 313 | * returns NULL if it is not a Qualified Name, otherwise, update len |
| 314 | * with the lenght in byte of the prefix and return a pointer |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 315 | * to the start of the name without the prefix |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 316 | */ |
| 317 | |
| 318 | const xmlChar * |
| 319 | xmlSplitQName3(const xmlChar *name, int *len) { |
| 320 | int l = 0; |
| 321 | |
| 322 | if (name == NULL) return(NULL); |
| 323 | if (len == NULL) return(NULL); |
| 324 | |
| 325 | /* nasty but valid */ |
| 326 | if (name[0] == ':') |
| 327 | return(NULL); |
| 328 | |
| 329 | /* |
| 330 | * we are not trying to validate but just to cut, and yes it will |
| 331 | * work even if this is as set of UTF-8 encoded chars |
| 332 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 333 | while ((name[l] != 0) && (name[l] != ':')) |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 334 | l++; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 335 | |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 336 | if (name[l] == 0) |
| 337 | return(NULL); |
| 338 | |
| 339 | *len = l; |
| 340 | |
| 341 | return(&name[l+1]); |
| 342 | } |
| 343 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 344 | /************************************************************************ |
| 345 | * * |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 346 | * Check Name, NCName and QName strings * |
| 347 | * * |
| 348 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 349 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 350 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l) |
| 351 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 352 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_DEBUG_ENABLED) || defined (LIBXML_HTML_ENABLED) || defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 353 | /** |
| 354 | * xmlValidateNCName: |
| 355 | * @value: the value to check |
| 356 | * @space: allow spaces in front and end of the string |
| 357 | * |
| 358 | * Check that a value conforms to the lexical space of NCName |
| 359 | * |
| 360 | * Returns 0 if this validates, a positive error code number otherwise |
| 361 | * and -1 in case of internal or API error. |
| 362 | */ |
| 363 | int |
| 364 | xmlValidateNCName(const xmlChar *value, int space) { |
| 365 | const xmlChar *cur = value; |
| 366 | int c,l; |
| 367 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 368 | if (value == NULL) |
| 369 | return(-1); |
| 370 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 371 | /* |
| 372 | * First quick algorithm for ASCII range |
| 373 | */ |
| 374 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 375 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 376 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 377 | (*cur == '_')) |
| 378 | cur++; |
| 379 | else |
| 380 | goto try_complex; |
| 381 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 382 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 383 | ((*cur >= '0') && (*cur <= '9')) || |
| 384 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 385 | cur++; |
| 386 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 387 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 388 | if (*cur == 0) |
| 389 | return(0); |
| 390 | |
| 391 | try_complex: |
| 392 | /* |
| 393 | * Second check for chars outside the ASCII range |
| 394 | */ |
| 395 | cur = value; |
| 396 | c = CUR_SCHAR(cur, l); |
| 397 | if (space) { |
| 398 | while (IS_BLANK(c)) { |
| 399 | cur += l; |
| 400 | c = CUR_SCHAR(cur, l); |
| 401 | } |
| 402 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 403 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 404 | return(1); |
| 405 | cur += l; |
| 406 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 407 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 408 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 409 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 410 | cur += l; |
| 411 | c = CUR_SCHAR(cur, l); |
| 412 | } |
| 413 | if (space) { |
| 414 | while (IS_BLANK(c)) { |
| 415 | cur += l; |
| 416 | c = CUR_SCHAR(cur, l); |
| 417 | } |
| 418 | } |
| 419 | if (c != 0) |
| 420 | return(1); |
| 421 | |
| 422 | return(0); |
| 423 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 424 | #endif |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 425 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 426 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 427 | /** |
| 428 | * xmlValidateQName: |
| 429 | * @value: the value to check |
| 430 | * @space: allow spaces in front and end of the string |
| 431 | * |
| 432 | * Check that a value conforms to the lexical space of QName |
| 433 | * |
| 434 | * Returns 0 if this validates, a positive error code number otherwise |
| 435 | * and -1 in case of internal or API error. |
| 436 | */ |
| 437 | int |
| 438 | xmlValidateQName(const xmlChar *value, int space) { |
| 439 | const xmlChar *cur = value; |
| 440 | int c,l; |
| 441 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 442 | if (value == NULL) |
| 443 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 444 | /* |
| 445 | * First quick algorithm for ASCII range |
| 446 | */ |
| 447 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 448 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 449 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 450 | (*cur == '_')) |
| 451 | cur++; |
| 452 | else |
| 453 | goto try_complex; |
| 454 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 455 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 456 | ((*cur >= '0') && (*cur <= '9')) || |
| 457 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 458 | cur++; |
| 459 | if (*cur == ':') { |
| 460 | cur++; |
| 461 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 462 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 463 | (*cur == '_')) |
| 464 | cur++; |
| 465 | else |
| 466 | goto try_complex; |
| 467 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 468 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 469 | ((*cur >= '0') && (*cur <= '9')) || |
| 470 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 471 | cur++; |
| 472 | } |
| 473 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 474 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 475 | if (*cur == 0) |
| 476 | return(0); |
| 477 | |
| 478 | try_complex: |
| 479 | /* |
| 480 | * Second check for chars outside the ASCII range |
| 481 | */ |
| 482 | cur = value; |
| 483 | c = CUR_SCHAR(cur, l); |
| 484 | if (space) { |
| 485 | while (IS_BLANK(c)) { |
| 486 | cur += l; |
| 487 | c = CUR_SCHAR(cur, l); |
| 488 | } |
| 489 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 490 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 491 | return(1); |
| 492 | cur += l; |
| 493 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 494 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 495 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 496 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 497 | cur += l; |
| 498 | c = CUR_SCHAR(cur, l); |
| 499 | } |
| 500 | if (c == ':') { |
| 501 | cur += l; |
| 502 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 503 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 504 | return(1); |
| 505 | cur += l; |
| 506 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 507 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 508 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 509 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 510 | cur += l; |
| 511 | c = CUR_SCHAR(cur, l); |
| 512 | } |
| 513 | } |
| 514 | if (space) { |
| 515 | while (IS_BLANK(c)) { |
| 516 | cur += l; |
| 517 | c = CUR_SCHAR(cur, l); |
| 518 | } |
| 519 | } |
| 520 | if (c != 0) |
| 521 | return(1); |
| 522 | return(0); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * xmlValidateName: |
| 527 | * @value: the value to check |
| 528 | * @space: allow spaces in front and end of the string |
| 529 | * |
| 530 | * Check that a value conforms to the lexical space of Name |
| 531 | * |
| 532 | * Returns 0 if this validates, a positive error code number otherwise |
| 533 | * and -1 in case of internal or API error. |
| 534 | */ |
| 535 | int |
| 536 | xmlValidateName(const xmlChar *value, int space) { |
| 537 | const xmlChar *cur = value; |
| 538 | int c,l; |
| 539 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 540 | if (value == NULL) |
| 541 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 542 | /* |
| 543 | * First quick algorithm for ASCII range |
| 544 | */ |
| 545 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 546 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 547 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 548 | (*cur == '_') || (*cur == ':')) |
| 549 | cur++; |
| 550 | else |
| 551 | goto try_complex; |
| 552 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 553 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 554 | ((*cur >= '0') && (*cur <= '9')) || |
| 555 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 556 | cur++; |
| 557 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 558 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 559 | if (*cur == 0) |
| 560 | return(0); |
| 561 | |
| 562 | try_complex: |
| 563 | /* |
| 564 | * Second check for chars outside the ASCII range |
| 565 | */ |
| 566 | cur = value; |
| 567 | c = CUR_SCHAR(cur, l); |
| 568 | if (space) { |
| 569 | while (IS_BLANK(c)) { |
| 570 | cur += l; |
| 571 | c = CUR_SCHAR(cur, l); |
| 572 | } |
| 573 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 574 | if ((!IS_LETTER(c)) && (c != '_') && (c != ':')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 575 | return(1); |
| 576 | cur += l; |
| 577 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 578 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 579 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 580 | cur += l; |
| 581 | c = CUR_SCHAR(cur, l); |
| 582 | } |
| 583 | if (space) { |
| 584 | while (IS_BLANK(c)) { |
| 585 | cur += l; |
| 586 | c = CUR_SCHAR(cur, l); |
| 587 | } |
| 588 | } |
| 589 | if (c != 0) |
| 590 | return(1); |
| 591 | return(0); |
| 592 | } |
| 593 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 594 | /** |
| 595 | * xmlValidateNMToken: |
| 596 | * @value: the value to check |
| 597 | * @space: allow spaces in front and end of the string |
| 598 | * |
| 599 | * Check that a value conforms to the lexical space of NMToken |
| 600 | * |
| 601 | * Returns 0 if this validates, a positive error code number otherwise |
| 602 | * and -1 in case of internal or API error. |
| 603 | */ |
| 604 | int |
| 605 | xmlValidateNMToken(const xmlChar *value, int space) { |
| 606 | const xmlChar *cur = value; |
| 607 | int c,l; |
| 608 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 609 | if (value == NULL) |
| 610 | return(-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 611 | /* |
| 612 | * First quick algorithm for ASCII range |
| 613 | */ |
| 614 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 615 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 616 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 617 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 618 | ((*cur >= '0') && (*cur <= '9')) || |
| 619 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 620 | cur++; |
| 621 | else |
| 622 | goto try_complex; |
| 623 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 624 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 625 | ((*cur >= '0') && (*cur <= '9')) || |
| 626 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 627 | cur++; |
| 628 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 629 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 630 | if (*cur == 0) |
| 631 | return(0); |
| 632 | |
| 633 | try_complex: |
| 634 | /* |
| 635 | * Second check for chars outside the ASCII range |
| 636 | */ |
| 637 | cur = value; |
| 638 | c = CUR_SCHAR(cur, l); |
| 639 | if (space) { |
| 640 | while (IS_BLANK(c)) { |
| 641 | cur += l; |
| 642 | c = CUR_SCHAR(cur, l); |
| 643 | } |
| 644 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 645 | if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 646 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c))) |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 647 | return(1); |
| 648 | cur += l; |
| 649 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 650 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 651 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 652 | cur += l; |
| 653 | c = CUR_SCHAR(cur, l); |
| 654 | } |
| 655 | if (space) { |
| 656 | while (IS_BLANK(c)) { |
| 657 | cur += l; |
| 658 | c = CUR_SCHAR(cur, l); |
| 659 | } |
| 660 | } |
| 661 | if (c != 0) |
| 662 | return(1); |
| 663 | return(0); |
| 664 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 665 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 666 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 667 | /************************************************************************ |
| 668 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 669 | * Allocation and deallocation of basic structures * |
| 670 | * * |
| 671 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 672 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 673 | /** |
| 674 | * xmlSetBufferAllocationScheme: |
| 675 | * @scheme: allocation method to use |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 676 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 677 | * Set the buffer allocation method. Types are |
| 678 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 679 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 680 | * improves performance |
| 681 | */ |
| 682 | void |
| 683 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 684 | if ((scheme == XML_BUFFER_ALLOC_EXACT) || |
| 685 | (scheme == XML_BUFFER_ALLOC_DOUBLEIT)) |
| 686 | xmlBufferAllocScheme = scheme; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /** |
| 690 | * xmlGetBufferAllocationScheme: |
| 691 | * |
| 692 | * Types are |
| 693 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 694 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 695 | * improves performance |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 696 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 697 | * Returns the current allocation scheme |
| 698 | */ |
| 699 | xmlBufferAllocationScheme |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 700 | xmlGetBufferAllocationScheme(void) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 701 | return(xmlBufferAllocScheme); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /** |
| 705 | * xmlNewNs: |
| 706 | * @node: the element carrying the namespace |
| 707 | * @href: the URI associated |
| 708 | * @prefix: the prefix for the namespace |
| 709 | * |
| 710 | * Creation of a new Namespace. This function will refuse to create |
| 711 | * a namespace with a similar prefix than an existing one present on this |
| 712 | * node. |
| 713 | * We use href==NULL in the case of an element creation where the namespace |
| 714 | * was not defined. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 715 | * Returns a new namespace pointer or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 716 | */ |
| 717 | xmlNsPtr |
| 718 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
| 719 | xmlNsPtr cur; |
| 720 | |
| 721 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 722 | return(NULL); |
| 723 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 724 | if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) |
| 725 | return(NULL); |
| 726 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 727 | /* |
| 728 | * Allocate a new Namespace and fill the fields. |
| 729 | */ |
| 730 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 731 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 732 | xmlTreeErrMemory("building namespace"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 733 | return(NULL); |
| 734 | } |
| 735 | memset(cur, 0, sizeof(xmlNs)); |
| 736 | cur->type = XML_LOCAL_NAMESPACE; |
| 737 | |
| 738 | if (href != NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 739 | cur->href = xmlStrdup(href); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 740 | if (prefix != NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 741 | cur->prefix = xmlStrdup(prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 742 | |
| 743 | /* |
| 744 | * Add it at the end to preserve parsing order ... |
| 745 | * and checks for existing use of the prefix |
| 746 | */ |
| 747 | if (node != NULL) { |
| 748 | if (node->nsDef == NULL) { |
| 749 | node->nsDef = cur; |
| 750 | } else { |
| 751 | xmlNsPtr prev = node->nsDef; |
| 752 | |
| 753 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 754 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 755 | xmlFreeNs(cur); |
| 756 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 757 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 758 | while (prev->next != NULL) { |
| 759 | prev = prev->next; |
| 760 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 761 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 762 | xmlFreeNs(cur); |
| 763 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 764 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 765 | } |
| 766 | prev->next = cur; |
| 767 | } |
| 768 | } |
| 769 | return(cur); |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * xmlSetNs: |
| 774 | * @node: a node in the document |
| 775 | * @ns: a namespace pointer |
| 776 | * |
| 777 | * Associate a namespace to a node, a posteriori. |
| 778 | */ |
| 779 | void |
| 780 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
| 781 | if (node == NULL) { |
| 782 | #ifdef DEBUG_TREE |
| 783 | xmlGenericError(xmlGenericErrorContext, |
| 784 | "xmlSetNs: node == NULL\n"); |
| 785 | #endif |
| 786 | return; |
| 787 | } |
| 788 | node->ns = ns; |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * xmlFreeNs: |
| 793 | * @cur: the namespace pointer |
| 794 | * |
| 795 | * Free up the structures associated to a namespace |
| 796 | */ |
| 797 | void |
| 798 | xmlFreeNs(xmlNsPtr cur) { |
| 799 | if (cur == NULL) { |
| 800 | #ifdef DEBUG_TREE |
| 801 | xmlGenericError(xmlGenericErrorContext, |
| 802 | "xmlFreeNs : ns == NULL\n"); |
| 803 | #endif |
| 804 | return; |
| 805 | } |
| 806 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 807 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 808 | xmlFree(cur); |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * xmlFreeNsList: |
| 813 | * @cur: the first namespace pointer |
| 814 | * |
| 815 | * Free up all the structures associated to the chained namespaces. |
| 816 | */ |
| 817 | void |
| 818 | xmlFreeNsList(xmlNsPtr cur) { |
| 819 | xmlNsPtr next; |
| 820 | if (cur == NULL) { |
| 821 | #ifdef DEBUG_TREE |
| 822 | xmlGenericError(xmlGenericErrorContext, |
| 823 | "xmlFreeNsList : ns == NULL\n"); |
| 824 | #endif |
| 825 | return; |
| 826 | } |
| 827 | while (cur != NULL) { |
| 828 | next = cur->next; |
| 829 | xmlFreeNs(cur); |
| 830 | cur = next; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | /** |
| 835 | * xmlNewDtd: |
| 836 | * @doc: the document pointer |
| 837 | * @name: the DTD name |
| 838 | * @ExternalID: the external ID |
| 839 | * @SystemID: the system ID |
| 840 | * |
| 841 | * Creation of a new DTD for the external subset. To create an |
| 842 | * internal subset, use xmlCreateIntSubset(). |
| 843 | * |
| 844 | * Returns a pointer to the new DTD structure |
| 845 | */ |
| 846 | xmlDtdPtr |
| 847 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 848 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 849 | xmlDtdPtr cur; |
| 850 | |
| 851 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
| 852 | #ifdef DEBUG_TREE |
| 853 | xmlGenericError(xmlGenericErrorContext, |
| 854 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
| 855 | /* !!! */ (char *) name, doc->name, |
| 856 | /* !!! */ (char *)doc->extSubset->name); |
| 857 | #endif |
| 858 | return(NULL); |
| 859 | } |
| 860 | |
| 861 | /* |
| 862 | * Allocate a new DTD and fill the fields. |
| 863 | */ |
| 864 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 865 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 866 | xmlTreeErrMemory("building DTD"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 867 | return(NULL); |
| 868 | } |
| 869 | memset(cur, 0 , sizeof(xmlDtd)); |
| 870 | cur->type = XML_DTD_NODE; |
| 871 | |
| 872 | if (name != NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 873 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 874 | if (ExternalID != NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 875 | cur->ExternalID = xmlStrdup(ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 876 | if (SystemID != NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 877 | cur->SystemID = xmlStrdup(SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 878 | if (doc != NULL) |
| 879 | doc->extSubset = cur; |
| 880 | cur->doc = doc; |
| 881 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 882 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 883 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 884 | return(cur); |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * xmlGetIntSubset: |
| 889 | * @doc: the document pointer |
| 890 | * |
| 891 | * Get the internal subset of a document |
| 892 | * Returns a pointer to the DTD structure or NULL if not found |
| 893 | */ |
| 894 | |
| 895 | xmlDtdPtr |
| 896 | xmlGetIntSubset(xmlDocPtr doc) { |
| 897 | xmlNodePtr cur; |
| 898 | |
| 899 | if (doc == NULL) |
| 900 | return(NULL); |
| 901 | cur = doc->children; |
| 902 | while (cur != NULL) { |
| 903 | if (cur->type == XML_DTD_NODE) |
| 904 | return((xmlDtdPtr) cur); |
| 905 | cur = cur->next; |
| 906 | } |
| 907 | return((xmlDtdPtr) doc->intSubset); |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * xmlCreateIntSubset: |
| 912 | * @doc: the document pointer |
| 913 | * @name: the DTD name |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 914 | * @ExternalID: the external (PUBLIC) ID |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 915 | * @SystemID: the system ID |
| 916 | * |
| 917 | * Create the internal subset of a document |
| 918 | * Returns a pointer to the new DTD structure |
| 919 | */ |
| 920 | xmlDtdPtr |
| 921 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 922 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 923 | xmlDtdPtr cur; |
| 924 | |
| 925 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
| 926 | #ifdef DEBUG_TREE |
| 927 | xmlGenericError(xmlGenericErrorContext, |
| 928 | |
| 929 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 930 | doc->name); |
| 931 | #endif |
| 932 | return(NULL); |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * Allocate a new DTD and fill the fields. |
| 937 | */ |
| 938 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 939 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 940 | xmlTreeErrMemory("building internal subset"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 941 | return(NULL); |
| 942 | } |
| 943 | memset(cur, 0, sizeof(xmlDtd)); |
| 944 | cur->type = XML_DTD_NODE; |
| 945 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 946 | if (name != NULL) { |
| 947 | cur->name = xmlStrdup(name); |
| 948 | if (cur->name == NULL) { |
| 949 | xmlTreeErrMemory("building internal subset"); |
| 950 | xmlFree(cur); |
| 951 | return(NULL); |
| 952 | } |
| 953 | } |
| 954 | if (ExternalID != NULL) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 955 | cur->ExternalID = xmlStrdup(ExternalID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 956 | if (cur->ExternalID == NULL) { |
| 957 | xmlTreeErrMemory("building internal subset"); |
| 958 | if (cur->name != NULL) |
| 959 | xmlFree((char *)cur->name); |
| 960 | xmlFree(cur); |
| 961 | return(NULL); |
| 962 | } |
| 963 | } |
| 964 | if (SystemID != NULL) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 965 | cur->SystemID = xmlStrdup(SystemID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 966 | if (cur->SystemID == NULL) { |
| 967 | xmlTreeErrMemory("building internal subset"); |
| 968 | if (cur->name != NULL) |
| 969 | xmlFree((char *)cur->name); |
| 970 | if (cur->ExternalID != NULL) |
| 971 | xmlFree((char *)cur->ExternalID); |
| 972 | xmlFree(cur); |
| 973 | return(NULL); |
| 974 | } |
| 975 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 976 | if (doc != NULL) { |
| 977 | doc->intSubset = cur; |
| 978 | cur->parent = doc; |
| 979 | cur->doc = doc; |
| 980 | if (doc->children == NULL) { |
| 981 | doc->children = (xmlNodePtr) cur; |
| 982 | doc->last = (xmlNodePtr) cur; |
| 983 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 984 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 985 | xmlNodePtr prev; |
| 986 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 987 | prev = doc->children; |
| 988 | prev->prev = (xmlNodePtr) cur; |
| 989 | cur->next = prev; |
| 990 | doc->children = (xmlNodePtr) cur; |
| 991 | } else { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 992 | xmlNodePtr next; |
| 993 | |
| 994 | next = doc->children; |
| 995 | while ((next != NULL) && (next->type != XML_ELEMENT_NODE)) |
| 996 | next = next->next; |
| 997 | if (next == NULL) { |
| 998 | cur->prev = doc->last; |
| 999 | cur->prev->next = (xmlNodePtr) cur; |
| 1000 | cur->next = NULL; |
| 1001 | doc->last = (xmlNodePtr) cur; |
| 1002 | } else { |
| 1003 | cur->next = next; |
| 1004 | cur->prev = next->prev; |
| 1005 | if (cur->prev == NULL) |
| 1006 | doc->children = (xmlNodePtr) cur; |
| 1007 | else |
| 1008 | cur->prev->next = (xmlNodePtr) cur; |
| 1009 | next->prev = (xmlNodePtr) cur; |
| 1010 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1014 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1015 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1016 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1017 | return(cur); |
| 1018 | } |
| 1019 | |
| 1020 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1021 | * DICT_FREE: |
| 1022 | * @str: a string |
| 1023 | * |
| 1024 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 1025 | * current scope |
| 1026 | */ |
| 1027 | #define DICT_FREE(str) \ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1028 | if ((str) && ((!dict) || \ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1029 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 1030 | xmlFree((char *)(str)); |
| 1031 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 1032 | |
| 1033 | /** |
| 1034 | * DICT_COPY: |
| 1035 | * @str: a string |
| 1036 | * |
| 1037 | * Copy a string using a "dict" dictionnary in the current scope, |
| 1038 | * if availabe. |
| 1039 | */ |
| 1040 | #define DICT_COPY(str, cpy) \ |
| 1041 | if (str) { \ |
| 1042 | if (dict) { \ |
| 1043 | if (xmlDictOwns(dict, (const xmlChar *)(str))) \ |
| 1044 | cpy = (xmlChar *) (str); \ |
| 1045 | else \ |
| 1046 | cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \ |
| 1047 | } else \ |
| 1048 | cpy = xmlStrdup((const xmlChar *)(str)); } |
| 1049 | |
| 1050 | /** |
| 1051 | * DICT_CONST_COPY: |
| 1052 | * @str: a string |
| 1053 | * |
| 1054 | * Copy a string using a "dict" dictionnary in the current scope, |
| 1055 | * if availabe. |
| 1056 | */ |
| 1057 | #define DICT_CONST_COPY(str, cpy) \ |
| 1058 | if (str) { \ |
| 1059 | if (dict) { \ |
| 1060 | if (xmlDictOwns(dict, (const xmlChar *)(str))) \ |
| 1061 | cpy = (const xmlChar *) (str); \ |
| 1062 | else \ |
| 1063 | cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \ |
| 1064 | } else \ |
| 1065 | cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); } |
| 1066 | |
| 1067 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1068 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1069 | * xmlFreeDtd: |
| 1070 | * @cur: the DTD structure to free up |
| 1071 | * |
| 1072 | * Free a DTD structure. |
| 1073 | */ |
| 1074 | void |
| 1075 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1076 | xmlDictPtr dict = NULL; |
| 1077 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1078 | if (cur == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1079 | return; |
| 1080 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1081 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1082 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1083 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1084 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1085 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1086 | if (cur->children != NULL) { |
| 1087 | xmlNodePtr next, c = cur->children; |
| 1088 | |
| 1089 | /* |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1090 | * Cleanup all nodes which are not part of the specific lists |
| 1091 | * of notations, elements, attributes and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1092 | */ |
| 1093 | while (c != NULL) { |
| 1094 | next = c->next; |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1095 | if ((c->type != XML_NOTATION_NODE) && |
| 1096 | (c->type != XML_ELEMENT_DECL) && |
| 1097 | (c->type != XML_ATTRIBUTE_DECL) && |
| 1098 | (c->type != XML_ENTITY_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1099 | xmlUnlinkNode(c); |
| 1100 | xmlFreeNode(c); |
| 1101 | } |
| 1102 | c = next; |
| 1103 | } |
| 1104 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1105 | DICT_FREE(cur->name) |
| 1106 | DICT_FREE(cur->SystemID) |
| 1107 | DICT_FREE(cur->ExternalID) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1108 | /* TODO !!! */ |
| 1109 | if (cur->notations != NULL) |
| 1110 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1111 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1112 | if (cur->elements != NULL) |
| 1113 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
| 1114 | if (cur->attributes != NULL) |
| 1115 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
| 1116 | if (cur->entities != NULL) |
| 1117 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 1118 | if (cur->pentities != NULL) |
| 1119 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
| 1120 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1121 | xmlFree(cur); |
| 1122 | } |
| 1123 | |
| 1124 | /** |
| 1125 | * xmlNewDoc: |
| 1126 | * @version: xmlChar string giving the version of XML "1.0" |
| 1127 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1128 | * Creates a new XML document |
| 1129 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1130 | * Returns a new document |
| 1131 | */ |
| 1132 | xmlDocPtr |
| 1133 | xmlNewDoc(const xmlChar *version) { |
| 1134 | xmlDocPtr cur; |
| 1135 | |
| 1136 | if (version == NULL) |
| 1137 | version = (const xmlChar *) "1.0"; |
| 1138 | |
| 1139 | /* |
| 1140 | * Allocate a new document and fill the fields. |
| 1141 | */ |
| 1142 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
| 1143 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1144 | xmlTreeErrMemory("building doc"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1145 | return(NULL); |
| 1146 | } |
| 1147 | memset(cur, 0, sizeof(xmlDoc)); |
| 1148 | cur->type = XML_DOCUMENT_NODE; |
| 1149 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1150 | cur->version = xmlStrdup(version); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 1151 | if (cur->version == NULL) { |
| 1152 | xmlTreeErrMemory("building doc"); |
| 1153 | xmlFree(cur); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1154 | return(NULL); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 1155 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1156 | cur->standalone = -1; |
| 1157 | cur->compression = -1; /* not initialized */ |
| 1158 | cur->doc = cur; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1159 | cur->parseFlags = 0; |
| 1160 | cur->properties = XML_DOC_USERBUILT; |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 1161 | /* |
| 1162 | * The in memory encoding is always UTF8 |
| 1163 | * This field will never change and would |
| 1164 | * be obsolete if not for binary compatibility. |
| 1165 | */ |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 1166 | cur->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1167 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1168 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1169 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1170 | return(cur); |
| 1171 | } |
| 1172 | |
| 1173 | /** |
| 1174 | * xmlFreeDoc: |
| 1175 | * @cur: pointer to the document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1176 | * |
| 1177 | * Free up all the structures used by a document, tree included. |
| 1178 | */ |
| 1179 | void |
| 1180 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1181 | xmlDtdPtr extSubset, intSubset; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1182 | xmlDictPtr dict = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1183 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1184 | if (cur == NULL) { |
| 1185 | #ifdef DEBUG_TREE |
| 1186 | xmlGenericError(xmlGenericErrorContext, |
| 1187 | "xmlFreeDoc : document == NULL\n"); |
| 1188 | #endif |
| 1189 | return; |
| 1190 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1191 | #ifdef LIBXML_DEBUG_RUNTIME |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1192 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1193 | xmlDebugCheckDocument(stderr, cur); |
| 1194 | #endif |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1195 | #endif |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1196 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1197 | if (cur != NULL) dict = cur->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1198 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1199 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1200 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1201 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1202 | /* |
| 1203 | * Do this before freeing the children list to avoid ID lookups |
| 1204 | */ |
| 1205 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
| 1206 | cur->ids = NULL; |
| 1207 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
| 1208 | cur->refs = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1209 | extSubset = cur->extSubset; |
| 1210 | intSubset = cur->intSubset; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 1211 | if (intSubset == extSubset) |
| 1212 | extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1213 | if (extSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1214 | xmlUnlinkNode((xmlNodePtr) cur->extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1215 | cur->extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1216 | xmlFreeDtd(extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1217 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1218 | if (intSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1219 | xmlUnlinkNode((xmlNodePtr) cur->intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1220 | cur->intSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1221 | xmlFreeDtd(intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1225 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1226 | |
| 1227 | DICT_FREE(cur->version) |
| 1228 | DICT_FREE(cur->name) |
| 1229 | DICT_FREE(cur->encoding) |
| 1230 | DICT_FREE(cur->URL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1231 | xmlFree(cur); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1232 | if (dict) xmlDictFree(dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | /** |
| 1236 | * xmlStringLenGetNodeList: |
| 1237 | * @doc: the document |
| 1238 | * @value: the value of the text |
| 1239 | * @len: the length of the string value |
| 1240 | * |
| 1241 | * Parse the value string and build the node list associated. Should |
| 1242 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1243 | * Returns a pointer to the first child |
| 1244 | */ |
| 1245 | xmlNodePtr |
| 1246 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
| 1247 | xmlNodePtr ret = NULL, last = NULL; |
| 1248 | xmlNodePtr node; |
| 1249 | xmlChar *val; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1250 | const xmlChar *cur = value, *end = cur + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1251 | const xmlChar *q; |
| 1252 | xmlEntityPtr ent; |
| 1253 | |
| 1254 | if (value == NULL) return(NULL); |
| 1255 | |
| 1256 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1257 | while ((cur < end) && (*cur != 0)) { |
| 1258 | if (cur[0] == '&') { |
| 1259 | int charval = 0; |
| 1260 | xmlChar tmp; |
| 1261 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1262 | /* |
| 1263 | * Save the current text. |
| 1264 | */ |
| 1265 | if (cur != q) { |
| 1266 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1267 | xmlNodeAddContentLen(last, q, cur - q); |
| 1268 | } else { |
| 1269 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1270 | if (node == NULL) return(ret); |
| 1271 | if (last == NULL) |
| 1272 | last = ret = node; |
| 1273 | else { |
| 1274 | last->next = node; |
| 1275 | node->prev = last; |
| 1276 | last = node; |
| 1277 | } |
| 1278 | } |
| 1279 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1280 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1281 | if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) { |
| 1282 | cur += 3; |
| 1283 | if (cur < end) |
| 1284 | tmp = *cur; |
| 1285 | else |
| 1286 | tmp = 0; |
| 1287 | while (tmp != ';') { /* Non input consuming loop */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1288 | if ((tmp >= '0') && (tmp <= '9')) |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1289 | charval = charval * 16 + (tmp - '0'); |
| 1290 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1291 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1292 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1293 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1294 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1295 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1296 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1297 | charval = 0; |
| 1298 | break; |
| 1299 | } |
| 1300 | cur++; |
| 1301 | if (cur < end) |
| 1302 | tmp = *cur; |
| 1303 | else |
| 1304 | tmp = 0; |
| 1305 | } |
| 1306 | if (tmp == ';') |
| 1307 | cur++; |
| 1308 | q = cur; |
| 1309 | } else if ((cur + 1 < end) && (cur[1] == '#')) { |
| 1310 | cur += 2; |
| 1311 | if (cur < end) |
| 1312 | tmp = *cur; |
| 1313 | else |
| 1314 | tmp = 0; |
| 1315 | while (tmp != ';') { /* Non input consuming loops */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1316 | if ((tmp >= '0') && (tmp <= '9')) |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1317 | charval = charval * 10 + (tmp - '0'); |
| 1318 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1319 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1320 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1321 | charval = 0; |
| 1322 | break; |
| 1323 | } |
| 1324 | cur++; |
| 1325 | if (cur < end) |
| 1326 | tmp = *cur; |
| 1327 | else |
| 1328 | tmp = 0; |
| 1329 | } |
| 1330 | if (tmp == ';') |
| 1331 | cur++; |
| 1332 | q = cur; |
| 1333 | } else { |
| 1334 | /* |
| 1335 | * Read the entity string |
| 1336 | */ |
| 1337 | cur++; |
| 1338 | q = cur; |
| 1339 | while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++; |
| 1340 | if ((cur >= end) || (*cur == 0)) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1341 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc, |
| 1342 | (const char *) q); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1343 | return(ret); |
| 1344 | } |
| 1345 | if (cur != q) { |
| 1346 | /* |
| 1347 | * Predefined entities don't generate nodes |
| 1348 | */ |
| 1349 | val = xmlStrndup(q, cur - q); |
| 1350 | ent = xmlGetDocEntity(doc, val); |
| 1351 | if ((ent != NULL) && |
| 1352 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1353 | if (last == NULL) { |
| 1354 | node = xmlNewDocText(doc, ent->content); |
| 1355 | last = ret = node; |
| 1356 | } else if (last->type != XML_TEXT_NODE) { |
| 1357 | node = xmlNewDocText(doc, ent->content); |
| 1358 | last = xmlAddNextSibling(last, node); |
| 1359 | } else |
| 1360 | xmlNodeAddContent(last, ent->content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1361 | |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1362 | } else { |
| 1363 | /* |
| 1364 | * Create a new REFERENCE_REF node |
| 1365 | */ |
| 1366 | node = xmlNewReference(doc, val); |
| 1367 | if (node == NULL) { |
| 1368 | if (val != NULL) xmlFree(val); |
| 1369 | return(ret); |
| 1370 | } |
| 1371 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1372 | xmlNodePtr temp; |
| 1373 | |
| 1374 | ent->children = xmlStringGetNodeList(doc, |
| 1375 | (const xmlChar*)node->content); |
| 1376 | ent->owner = 1; |
| 1377 | temp = ent->children; |
| 1378 | while (temp) { |
| 1379 | temp->parent = (xmlNodePtr)ent; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1380 | ent->last = temp; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1381 | temp = temp->next; |
| 1382 | } |
| 1383 | } |
| 1384 | if (last == NULL) { |
| 1385 | last = ret = node; |
| 1386 | } else { |
| 1387 | last = xmlAddNextSibling(last, node); |
| 1388 | } |
| 1389 | } |
| 1390 | xmlFree(val); |
| 1391 | } |
| 1392 | cur++; |
| 1393 | q = cur; |
| 1394 | } |
| 1395 | if (charval != 0) { |
| 1396 | xmlChar buf[10]; |
| 1397 | int l; |
| 1398 | |
| 1399 | l = xmlCopyCharMultiByte(buf, charval); |
| 1400 | buf[l] = 0; |
| 1401 | node = xmlNewDocText(doc, buf); |
| 1402 | if (node != NULL) { |
| 1403 | if (last == NULL) { |
| 1404 | last = ret = node; |
| 1405 | } else { |
| 1406 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1407 | } |
| 1408 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1409 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1410 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1411 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1412 | cur++; |
| 1413 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1414 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1415 | /* |
| 1416 | * Handle the last piece of text. |
| 1417 | */ |
| 1418 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1419 | xmlNodeAddContentLen(last, q, cur - q); |
| 1420 | } else { |
| 1421 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1422 | if (node == NULL) return(ret); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1423 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1424 | last = ret = node; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1425 | } else { |
| 1426 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | return(ret); |
| 1431 | } |
| 1432 | |
| 1433 | /** |
| 1434 | * xmlStringGetNodeList: |
| 1435 | * @doc: the document |
| 1436 | * @value: the value of the attribute |
| 1437 | * |
| 1438 | * Parse the value string and build the node list associated. Should |
| 1439 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1440 | * Returns a pointer to the first child |
| 1441 | */ |
| 1442 | xmlNodePtr |
| 1443 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
| 1444 | xmlNodePtr ret = NULL, last = NULL; |
| 1445 | xmlNodePtr node; |
| 1446 | xmlChar *val; |
| 1447 | const xmlChar *cur = value; |
| 1448 | const xmlChar *q; |
| 1449 | xmlEntityPtr ent; |
| 1450 | |
| 1451 | if (value == NULL) return(NULL); |
| 1452 | |
| 1453 | q = cur; |
| 1454 | while (*cur != 0) { |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1455 | if (cur[0] == '&') { |
| 1456 | int charval = 0; |
| 1457 | xmlChar tmp; |
| 1458 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1459 | /* |
| 1460 | * Save the current text. |
| 1461 | */ |
| 1462 | if (cur != q) { |
| 1463 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1464 | xmlNodeAddContentLen(last, q, cur - q); |
| 1465 | } else { |
| 1466 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1467 | if (node == NULL) return(ret); |
| 1468 | if (last == NULL) |
| 1469 | last = ret = node; |
| 1470 | else { |
| 1471 | last->next = node; |
| 1472 | node->prev = last; |
| 1473 | last = node; |
| 1474 | } |
| 1475 | } |
| 1476 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1477 | q = cur; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1478 | if ((cur[1] == '#') && (cur[2] == 'x')) { |
| 1479 | cur += 3; |
| 1480 | tmp = *cur; |
| 1481 | while (tmp != ';') { /* Non input consuming loop */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1482 | if ((tmp >= '0') && (tmp <= '9')) |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1483 | charval = charval * 16 + (tmp - '0'); |
| 1484 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1485 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1486 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1487 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1488 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1489 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1490 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1491 | charval = 0; |
| 1492 | break; |
| 1493 | } |
| 1494 | cur++; |
| 1495 | tmp = *cur; |
| 1496 | } |
| 1497 | if (tmp == ';') |
| 1498 | cur++; |
| 1499 | q = cur; |
| 1500 | } else if (cur[1] == '#') { |
| 1501 | cur += 2; |
| 1502 | tmp = *cur; |
| 1503 | while (tmp != ';') { /* Non input consuming loops */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1504 | if ((tmp >= '0') && (tmp <= '9')) |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1505 | charval = charval * 10 + (tmp - '0'); |
| 1506 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1507 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1508 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1509 | charval = 0; |
| 1510 | break; |
| 1511 | } |
| 1512 | cur++; |
| 1513 | tmp = *cur; |
| 1514 | } |
| 1515 | if (tmp == ';') |
| 1516 | cur++; |
| 1517 | q = cur; |
| 1518 | } else { |
| 1519 | /* |
| 1520 | * Read the entity string |
| 1521 | */ |
| 1522 | cur++; |
| 1523 | q = cur; |
| 1524 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 1525 | if (*cur == 0) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1526 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, |
| 1527 | (xmlNodePtr) doc, (const char *) q); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1528 | return(ret); |
| 1529 | } |
| 1530 | if (cur != q) { |
| 1531 | /* |
| 1532 | * Predefined entities don't generate nodes |
| 1533 | */ |
| 1534 | val = xmlStrndup(q, cur - q); |
| 1535 | ent = xmlGetDocEntity(doc, val); |
| 1536 | if ((ent != NULL) && |
| 1537 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1538 | if (last == NULL) { |
| 1539 | node = xmlNewDocText(doc, ent->content); |
| 1540 | last = ret = node; |
Daniel Veillard | 6f42c13 | 2002-01-06 23:05:13 +0000 | [diff] [blame] | 1541 | } else if (last->type != XML_TEXT_NODE) { |
| 1542 | node = xmlNewDocText(doc, ent->content); |
| 1543 | last = xmlAddNextSibling(last, node); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1544 | } else |
| 1545 | xmlNodeAddContent(last, ent->content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1546 | |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1547 | } else { |
| 1548 | /* |
| 1549 | * Create a new REFERENCE_REF node |
| 1550 | */ |
| 1551 | node = xmlNewReference(doc, val); |
| 1552 | if (node == NULL) { |
| 1553 | if (val != NULL) xmlFree(val); |
| 1554 | return(ret); |
| 1555 | } |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1556 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1557 | xmlNodePtr temp; |
| 1558 | |
| 1559 | ent->children = xmlStringGetNodeList(doc, |
| 1560 | (const xmlChar*)node->content); |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 1561 | ent->owner = 1; |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1562 | temp = ent->children; |
| 1563 | while (temp) { |
| 1564 | temp->parent = (xmlNodePtr)ent; |
| 1565 | temp = temp->next; |
| 1566 | } |
| 1567 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1568 | if (last == NULL) { |
| 1569 | last = ret = node; |
| 1570 | } else { |
| 1571 | last = xmlAddNextSibling(last, node); |
| 1572 | } |
| 1573 | } |
| 1574 | xmlFree(val); |
| 1575 | } |
| 1576 | cur++; |
| 1577 | q = cur; |
| 1578 | } |
| 1579 | if (charval != 0) { |
| 1580 | xmlChar buf[10]; |
| 1581 | int len; |
| 1582 | |
| 1583 | len = xmlCopyCharMultiByte(buf, charval); |
| 1584 | buf[len] = 0; |
| 1585 | node = xmlNewDocText(doc, buf); |
| 1586 | if (node != NULL) { |
| 1587 | if (last == NULL) { |
| 1588 | last = ret = node; |
| 1589 | } else { |
| 1590 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1591 | } |
| 1592 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1593 | |
| 1594 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1595 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1596 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1597 | cur++; |
| 1598 | } |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 1599 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1600 | /* |
| 1601 | * Handle the last piece of text. |
| 1602 | */ |
| 1603 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1604 | xmlNodeAddContentLen(last, q, cur - q); |
| 1605 | } else { |
| 1606 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1607 | if (node == NULL) return(ret); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1608 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1609 | last = ret = node; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1610 | } else { |
| 1611 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | } |
| 1615 | return(ret); |
| 1616 | } |
| 1617 | |
| 1618 | /** |
| 1619 | * xmlNodeListGetString: |
| 1620 | * @doc: the document |
| 1621 | * @list: a Node list |
| 1622 | * @inLine: should we replace entity contents or show their external form |
| 1623 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1624 | * Build the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1625 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1626 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1627 | * 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] | 1628 | */ |
| 1629 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1630 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1631 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1632 | xmlNodePtr node = list; |
| 1633 | xmlChar *ret = NULL; |
| 1634 | xmlEntityPtr ent; |
| 1635 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1636 | if (list == NULL) |
| 1637 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1638 | |
| 1639 | while (node != NULL) { |
| 1640 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1641 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1642 | if (inLine) { |
| 1643 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1644 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1645 | xmlChar *buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1646 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1647 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
| 1648 | if (buffer != NULL) { |
| 1649 | ret = xmlStrcat(ret, buffer); |
| 1650 | xmlFree(buffer); |
| 1651 | } |
| 1652 | } |
| 1653 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1654 | if (inLine) { |
| 1655 | ent = xmlGetDocEntity(doc, node->name); |
| 1656 | if (ent != NULL) { |
| 1657 | xmlChar *buffer; |
| 1658 | |
| 1659 | /* an entity content can be any "well balanced chunk", |
| 1660 | * i.e. the result of the content [43] production: |
| 1661 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1662 | * So it can contain text, CDATA section or nested |
| 1663 | * entity reference nodes (among others). |
| 1664 | * -> we recursive call xmlNodeListGetString() |
| 1665 | * which handles these types */ |
| 1666 | buffer = xmlNodeListGetString(doc, ent->children, 1); |
| 1667 | if (buffer != NULL) { |
| 1668 | ret = xmlStrcat(ret, buffer); |
| 1669 | xmlFree(buffer); |
| 1670 | } |
| 1671 | } else { |
| 1672 | ret = xmlStrcat(ret, node->content); |
| 1673 | } |
| 1674 | } else { |
| 1675 | xmlChar buf[2]; |
| 1676 | |
| 1677 | buf[0] = '&'; |
| 1678 | buf[1] = 0; |
| 1679 | ret = xmlStrncat(ret, buf, 1); |
| 1680 | ret = xmlStrcat(ret, node->name); |
| 1681 | buf[0] = ';'; |
| 1682 | buf[1] = 0; |
| 1683 | ret = xmlStrncat(ret, buf, 1); |
| 1684 | } |
| 1685 | } |
| 1686 | #if 0 |
| 1687 | else { |
| 1688 | xmlGenericError(xmlGenericErrorContext, |
| 1689 | "xmlGetNodeListString : invalid node type %d\n", |
| 1690 | node->type); |
| 1691 | } |
| 1692 | #endif |
| 1693 | node = node->next; |
| 1694 | } |
| 1695 | return (ret); |
| 1696 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1697 | |
| 1698 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1699 | /** |
| 1700 | * xmlNodeListGetRawString: |
| 1701 | * @doc: the document |
| 1702 | * @list: a Node list |
| 1703 | * @inLine: should we replace entity contents or show their external form |
| 1704 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1705 | * Builds the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1706 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 1707 | * this function doesn't do any character encoding handling. |
| 1708 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1709 | * 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] | 1710 | */ |
| 1711 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1712 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1713 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1714 | xmlNodePtr node = list; |
| 1715 | xmlChar *ret = NULL; |
| 1716 | xmlEntityPtr ent; |
| 1717 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1718 | if (list == NULL) |
| 1719 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1720 | |
| 1721 | while (node != NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1722 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1723 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1724 | if (inLine) { |
| 1725 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1726 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1727 | xmlChar *buffer; |
| 1728 | |
| 1729 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 1730 | if (buffer != NULL) { |
| 1731 | ret = xmlStrcat(ret, buffer); |
| 1732 | xmlFree(buffer); |
| 1733 | } |
| 1734 | } |
| 1735 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1736 | if (inLine) { |
| 1737 | ent = xmlGetDocEntity(doc, node->name); |
| 1738 | if (ent != NULL) { |
| 1739 | xmlChar *buffer; |
| 1740 | |
| 1741 | /* an entity content can be any "well balanced chunk", |
| 1742 | * i.e. the result of the content [43] production: |
| 1743 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1744 | * So it can contain text, CDATA section or nested |
| 1745 | * entity reference nodes (among others). |
| 1746 | * -> we recursive call xmlNodeListGetRawString() |
| 1747 | * which handles these types */ |
| 1748 | buffer = |
| 1749 | xmlNodeListGetRawString(doc, ent->children, 1); |
| 1750 | if (buffer != NULL) { |
| 1751 | ret = xmlStrcat(ret, buffer); |
| 1752 | xmlFree(buffer); |
| 1753 | } |
| 1754 | } else { |
| 1755 | ret = xmlStrcat(ret, node->content); |
| 1756 | } |
| 1757 | } else { |
| 1758 | xmlChar buf[2]; |
| 1759 | |
| 1760 | buf[0] = '&'; |
| 1761 | buf[1] = 0; |
| 1762 | ret = xmlStrncat(ret, buf, 1); |
| 1763 | ret = xmlStrcat(ret, node->name); |
| 1764 | buf[0] = ';'; |
| 1765 | buf[1] = 0; |
| 1766 | ret = xmlStrncat(ret, buf, 1); |
| 1767 | } |
| 1768 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1769 | #if 0 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1770 | else { |
| 1771 | xmlGenericError(xmlGenericErrorContext, |
| 1772 | "xmlGetNodeListString : invalid node type %d\n", |
| 1773 | node->type); |
| 1774 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1775 | #endif |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1776 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1777 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1778 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1779 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1780 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1781 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1782 | static xmlAttrPtr |
| 1783 | xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns, |
| 1784 | const xmlChar * name, const xmlChar * value, |
| 1785 | int eatname) |
| 1786 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1787 | xmlAttrPtr cur; |
| 1788 | xmlDocPtr doc = NULL; |
| 1789 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1790 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) { |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 1791 | if ((eatname == 1) && |
| 1792 | ((node->doc == NULL) || |
Daniel Veillard | ed939f8 | 2008-04-08 08:20:08 +0000 | [diff] [blame] | 1793 | (!(xmlDictOwns(node->doc->dict, name))))) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1794 | xmlFree((xmlChar *) name); |
| 1795 | return (NULL); |
| 1796 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1797 | |
| 1798 | /* |
| 1799 | * Allocate a new property and fill the fields. |
| 1800 | */ |
| 1801 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1802 | if (cur == NULL) { |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 1803 | if ((eatname == 1) && |
| 1804 | ((node->doc == NULL) || |
Daniel Veillard | ed939f8 | 2008-04-08 08:20:08 +0000 | [diff] [blame] | 1805 | (!(xmlDictOwns(node->doc->dict, name))))) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1806 | xmlFree((xmlChar *) name); |
| 1807 | xmlTreeErrMemory("building attribute"); |
| 1808 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1809 | } |
| 1810 | memset(cur, 0, sizeof(xmlAttr)); |
| 1811 | cur->type = XML_ATTRIBUTE_NODE; |
| 1812 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1813 | cur->parent = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1814 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1815 | doc = node->doc; |
| 1816 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1817 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1818 | cur->ns = ns; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1819 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1820 | if (eatname == 0) { |
| 1821 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1822 | cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1); |
| 1823 | else |
| 1824 | cur->name = xmlStrdup(name); |
| 1825 | } else |
| 1826 | cur->name = name; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1827 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1828 | if (value != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1829 | xmlNodePtr tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1830 | |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame] | 1831 | if(!xmlCheckUTF8(value)) { |
| 1832 | xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc, |
| 1833 | NULL); |
| 1834 | if (doc != NULL) |
| 1835 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 1836 | } |
| 1837 | cur->children = xmlNewDocText(doc, value); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1838 | cur->last = NULL; |
| 1839 | tmp = cur->children; |
| 1840 | while (tmp != NULL) { |
| 1841 | tmp->parent = (xmlNodePtr) cur; |
| 1842 | if (tmp->next == NULL) |
| 1843 | cur->last = tmp; |
| 1844 | tmp = tmp->next; |
| 1845 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1846 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1847 | |
| 1848 | /* |
| 1849 | * Add it at the end to preserve parsing order ... |
| 1850 | */ |
| 1851 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1852 | if (node->properties == NULL) { |
| 1853 | node->properties = cur; |
| 1854 | } else { |
| 1855 | xmlAttrPtr prev = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1856 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1857 | while (prev->next != NULL) |
| 1858 | prev = prev->next; |
| 1859 | prev->next = cur; |
| 1860 | cur->prev = prev; |
| 1861 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1862 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1863 | |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 1864 | if ((value != NULL) && |
| 1865 | (xmlIsID((node == NULL) ? NULL : node->doc, node, cur) == 1)) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1866 | xmlAddID(NULL, node->doc, value, cur); |
| 1867 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1868 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1869 | xmlRegisterNodeDefaultValue((xmlNodePtr) cur); |
| 1870 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1871 | } |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1872 | |
| 1873 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 1874 | defined(LIBXML_SCHEMAS_ENABLED) |
| 1875 | /** |
| 1876 | * xmlNewProp: |
| 1877 | * @node: the holding node |
| 1878 | * @name: the name of the attribute |
| 1879 | * @value: the value of the attribute |
| 1880 | * |
| 1881 | * Create a new property carried by a node. |
| 1882 | * Returns a pointer to the attribute |
| 1883 | */ |
| 1884 | xmlAttrPtr |
| 1885 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 1886 | |
| 1887 | if (name == NULL) { |
| 1888 | #ifdef DEBUG_TREE |
| 1889 | xmlGenericError(xmlGenericErrorContext, |
| 1890 | "xmlNewProp : name == NULL\n"); |
| 1891 | #endif |
| 1892 | return(NULL); |
| 1893 | } |
| 1894 | |
| 1895 | return xmlNewPropInternal(node, NULL, name, value, 0); |
| 1896 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1897 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1898 | |
| 1899 | /** |
| 1900 | * xmlNewNsProp: |
| 1901 | * @node: the holding node |
| 1902 | * @ns: the namespace |
| 1903 | * @name: the name of the attribute |
| 1904 | * @value: the value of the attribute |
| 1905 | * |
| 1906 | * Create a new property tagged with a namespace and carried by a node. |
| 1907 | * Returns a pointer to the attribute |
| 1908 | */ |
| 1909 | xmlAttrPtr |
| 1910 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 1911 | const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1912 | |
| 1913 | if (name == NULL) { |
| 1914 | #ifdef DEBUG_TREE |
| 1915 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1916 | "xmlNewNsProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1917 | #endif |
| 1918 | return(NULL); |
| 1919 | } |
| 1920 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1921 | return xmlNewPropInternal(node, ns, name, value, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1925 | * xmlNewNsPropEatName: |
| 1926 | * @node: the holding node |
| 1927 | * @ns: the namespace |
| 1928 | * @name: the name of the attribute |
| 1929 | * @value: the value of the attribute |
| 1930 | * |
| 1931 | * Create a new property tagged with a namespace and carried by a node. |
| 1932 | * Returns a pointer to the attribute |
| 1933 | */ |
| 1934 | xmlAttrPtr |
| 1935 | xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name, |
| 1936 | const xmlChar *value) { |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1937 | |
| 1938 | if (name == NULL) { |
| 1939 | #ifdef DEBUG_TREE |
| 1940 | xmlGenericError(xmlGenericErrorContext, |
| 1941 | "xmlNewNsPropEatName : name == NULL\n"); |
| 1942 | #endif |
| 1943 | return(NULL); |
| 1944 | } |
| 1945 | |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 1946 | return xmlNewPropInternal(node, ns, name, value, 1); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1950 | * xmlNewDocProp: |
| 1951 | * @doc: the document |
| 1952 | * @name: the name of the attribute |
| 1953 | * @value: the value of the attribute |
| 1954 | * |
| 1955 | * Create a new property carried by a document. |
| 1956 | * Returns a pointer to the attribute |
| 1957 | */ |
| 1958 | xmlAttrPtr |
| 1959 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
| 1960 | xmlAttrPtr cur; |
| 1961 | |
| 1962 | if (name == NULL) { |
| 1963 | #ifdef DEBUG_TREE |
| 1964 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1965 | "xmlNewDocProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1966 | #endif |
| 1967 | return(NULL); |
| 1968 | } |
| 1969 | |
| 1970 | /* |
| 1971 | * Allocate a new property and fill the fields. |
| 1972 | */ |
| 1973 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1974 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1975 | xmlTreeErrMemory("building attribute"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1976 | return(NULL); |
| 1977 | } |
| 1978 | memset(cur, 0, sizeof(xmlAttr)); |
| 1979 | cur->type = XML_ATTRIBUTE_NODE; |
| 1980 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 1981 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1982 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 1983 | else |
| 1984 | cur->name = xmlStrdup(name); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1985 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1986 | if (value != NULL) { |
| 1987 | xmlNodePtr tmp; |
| 1988 | |
| 1989 | cur->children = xmlStringGetNodeList(doc, value); |
| 1990 | cur->last = NULL; |
| 1991 | |
| 1992 | tmp = cur->children; |
| 1993 | while (tmp != NULL) { |
| 1994 | tmp->parent = (xmlNodePtr) cur; |
| 1995 | if (tmp->next == NULL) |
| 1996 | cur->last = tmp; |
| 1997 | tmp = tmp->next; |
| 1998 | } |
| 1999 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2000 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2001 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2002 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2003 | return(cur); |
| 2004 | } |
| 2005 | |
| 2006 | /** |
| 2007 | * xmlFreePropList: |
| 2008 | * @cur: the first property in the list |
| 2009 | * |
| 2010 | * Free a property and all its siblings, all the children are freed too. |
| 2011 | */ |
| 2012 | void |
| 2013 | xmlFreePropList(xmlAttrPtr cur) { |
| 2014 | xmlAttrPtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2015 | if (cur == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2016 | while (cur != NULL) { |
| 2017 | next = cur->next; |
| 2018 | xmlFreeProp(cur); |
| 2019 | cur = next; |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | /** |
| 2024 | * xmlFreeProp: |
| 2025 | * @cur: an attribute |
| 2026 | * |
| 2027 | * Free one attribute, all the content is freed too |
| 2028 | */ |
| 2029 | void |
| 2030 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2031 | xmlDictPtr dict = NULL; |
| 2032 | if (cur == NULL) return; |
| 2033 | |
| 2034 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2035 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2036 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2037 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 2038 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2039 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | da6f4af | 2005-06-20 17:17:54 +0000 | [diff] [blame] | 2040 | if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) { |
| 2041 | xmlRemoveID(cur->doc, cur); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2042 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2043 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2044 | DICT_FREE(cur->name) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2045 | xmlFree(cur); |
| 2046 | } |
| 2047 | |
| 2048 | /** |
| 2049 | * xmlRemoveProp: |
| 2050 | * @cur: an attribute |
| 2051 | * |
| 2052 | * Unlink and free one attribute, all the content is freed too |
| 2053 | * Note this doesn't work for namespace definition attributes |
| 2054 | * |
| 2055 | * Returns 0 if success and -1 in case of error. |
| 2056 | */ |
| 2057 | int |
| 2058 | xmlRemoveProp(xmlAttrPtr cur) { |
| 2059 | xmlAttrPtr tmp; |
| 2060 | if (cur == NULL) { |
| 2061 | #ifdef DEBUG_TREE |
| 2062 | xmlGenericError(xmlGenericErrorContext, |
| 2063 | "xmlRemoveProp : cur == NULL\n"); |
| 2064 | #endif |
| 2065 | return(-1); |
| 2066 | } |
| 2067 | if (cur->parent == NULL) { |
| 2068 | #ifdef DEBUG_TREE |
| 2069 | xmlGenericError(xmlGenericErrorContext, |
| 2070 | "xmlRemoveProp : cur->parent == NULL\n"); |
| 2071 | #endif |
| 2072 | return(-1); |
| 2073 | } |
| 2074 | tmp = cur->parent->properties; |
| 2075 | if (tmp == cur) { |
| 2076 | cur->parent->properties = cur->next; |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2077 | if (cur->next != NULL) |
| 2078 | cur->next->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2079 | xmlFreeProp(cur); |
| 2080 | return(0); |
| 2081 | } |
| 2082 | while (tmp != NULL) { |
| 2083 | if (tmp->next == cur) { |
| 2084 | tmp->next = cur->next; |
| 2085 | if (tmp->next != NULL) |
| 2086 | tmp->next->prev = tmp; |
| 2087 | xmlFreeProp(cur); |
| 2088 | return(0); |
| 2089 | } |
| 2090 | tmp = tmp->next; |
| 2091 | } |
| 2092 | #ifdef DEBUG_TREE |
| 2093 | xmlGenericError(xmlGenericErrorContext, |
| 2094 | "xmlRemoveProp : attribute not owned by its node\n"); |
| 2095 | #endif |
| 2096 | return(-1); |
| 2097 | } |
| 2098 | |
| 2099 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2100 | * xmlNewDocPI: |
| 2101 | * @doc: the target document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2102 | * @name: the processing instruction name |
| 2103 | * @content: the PI content |
| 2104 | * |
| 2105 | * Creation of a processing instruction element. |
| 2106 | * Returns a pointer to the new node object. |
| 2107 | */ |
| 2108 | xmlNodePtr |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2109 | xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2110 | xmlNodePtr cur; |
| 2111 | |
| 2112 | if (name == NULL) { |
| 2113 | #ifdef DEBUG_TREE |
| 2114 | xmlGenericError(xmlGenericErrorContext, |
| 2115 | "xmlNewPI : name == NULL\n"); |
| 2116 | #endif |
| 2117 | return(NULL); |
| 2118 | } |
| 2119 | |
| 2120 | /* |
| 2121 | * Allocate a new node and fill the fields. |
| 2122 | */ |
| 2123 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2124 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2125 | xmlTreeErrMemory("building PI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2126 | return(NULL); |
| 2127 | } |
| 2128 | memset(cur, 0, sizeof(xmlNode)); |
| 2129 | cur->type = XML_PI_NODE; |
| 2130 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2131 | if ((doc != NULL) && (doc->dict != NULL)) |
| 2132 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 2133 | else |
| 2134 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2135 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2136 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2137 | } |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 2138 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2139 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2140 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2141 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2142 | return(cur); |
| 2143 | } |
| 2144 | |
| 2145 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2146 | * xmlNewPI: |
| 2147 | * @name: the processing instruction name |
| 2148 | * @content: the PI content |
| 2149 | * |
| 2150 | * Creation of a processing instruction element. |
| 2151 | * Use xmlDocNewPI preferably to get string interning |
| 2152 | * |
| 2153 | * Returns a pointer to the new node object. |
| 2154 | */ |
| 2155 | xmlNodePtr |
| 2156 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
| 2157 | return(xmlNewDocPI(NULL, name, content)); |
| 2158 | } |
| 2159 | |
| 2160 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2161 | * xmlNewNode: |
| 2162 | * @ns: namespace if any |
| 2163 | * @name: the node name |
| 2164 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2165 | * Creation of a new node element. @ns is optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2166 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2167 | * Returns a pointer to the new node object. Uses xmlStrdup() to make |
| 2168 | * copy of @name. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2169 | */ |
| 2170 | xmlNodePtr |
| 2171 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 2172 | xmlNodePtr cur; |
| 2173 | |
| 2174 | if (name == NULL) { |
| 2175 | #ifdef DEBUG_TREE |
| 2176 | xmlGenericError(xmlGenericErrorContext, |
| 2177 | "xmlNewNode : name == NULL\n"); |
| 2178 | #endif |
| 2179 | return(NULL); |
| 2180 | } |
| 2181 | |
| 2182 | /* |
| 2183 | * Allocate a new node and fill the fields. |
| 2184 | */ |
| 2185 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2186 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2187 | xmlTreeErrMemory("building node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2188 | return(NULL); |
| 2189 | } |
| 2190 | memset(cur, 0, sizeof(xmlNode)); |
| 2191 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2192 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2193 | cur->name = xmlStrdup(name); |
| 2194 | cur->ns = ns; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2195 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2196 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2197 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2198 | return(cur); |
| 2199 | } |
| 2200 | |
| 2201 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2202 | * xmlNewNodeEatName: |
| 2203 | * @ns: namespace if any |
| 2204 | * @name: the node name |
| 2205 | * |
| 2206 | * Creation of a new node element. @ns is optional (NULL). |
| 2207 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2208 | * Returns a pointer to the new node object, with pointer @name as |
| 2209 | * new node's name. Use xmlNewNode() if a copy of @name string is |
| 2210 | * is needed as new node's name. |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2211 | */ |
| 2212 | xmlNodePtr |
| 2213 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
| 2214 | xmlNodePtr cur; |
| 2215 | |
| 2216 | if (name == NULL) { |
| 2217 | #ifdef DEBUG_TREE |
| 2218 | xmlGenericError(xmlGenericErrorContext, |
| 2219 | "xmlNewNode : name == NULL\n"); |
| 2220 | #endif |
| 2221 | return(NULL); |
| 2222 | } |
| 2223 | |
| 2224 | /* |
| 2225 | * Allocate a new node and fill the fields. |
| 2226 | */ |
| 2227 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2228 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2229 | xmlTreeErrMemory("building node"); |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 2230 | /* we can't check here that name comes from the doc dictionnary */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2231 | return(NULL); |
| 2232 | } |
| 2233 | memset(cur, 0, sizeof(xmlNode)); |
| 2234 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2235 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2236 | cur->name = name; |
| 2237 | cur->ns = ns; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2238 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2239 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2240 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2241 | return(cur); |
| 2242 | } |
| 2243 | |
| 2244 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2245 | * xmlNewDocNode: |
| 2246 | * @doc: the document |
| 2247 | * @ns: namespace if any |
| 2248 | * @name: the node name |
| 2249 | * @content: the XML text content if any |
| 2250 | * |
| 2251 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2252 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2253 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2254 | * references, but XML special chars need to be escaped first by using |
| 2255 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2256 | * need entities support. |
| 2257 | * |
| 2258 | * Returns a pointer to the new node object. |
| 2259 | */ |
| 2260 | xmlNodePtr |
| 2261 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2262 | const xmlChar *name, const xmlChar *content) { |
| 2263 | xmlNodePtr cur; |
| 2264 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2265 | if ((doc != NULL) && (doc->dict != NULL)) |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2266 | cur = xmlNewNodeEatName(ns, (xmlChar *) |
| 2267 | xmlDictLookup(doc->dict, name, -1)); |
| 2268 | else |
| 2269 | cur = xmlNewNode(ns, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2270 | if (cur != NULL) { |
| 2271 | cur->doc = doc; |
| 2272 | if (content != NULL) { |
| 2273 | cur->children = xmlStringGetNodeList(doc, content); |
| 2274 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2275 | } |
| 2276 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2277 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2278 | return(cur); |
| 2279 | } |
| 2280 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2281 | /** |
| 2282 | * xmlNewDocNodeEatName: |
| 2283 | * @doc: the document |
| 2284 | * @ns: namespace if any |
| 2285 | * @name: the node name |
| 2286 | * @content: the XML text content if any |
| 2287 | * |
| 2288 | * Creation of a new node element within a document. @ns and @content |
| 2289 | * are optional (NULL). |
| 2290 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2291 | * references, but XML special chars need to be escaped first by using |
| 2292 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2293 | * need entities support. |
| 2294 | * |
| 2295 | * Returns a pointer to the new node object. |
| 2296 | */ |
| 2297 | xmlNodePtr |
| 2298 | xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns, |
| 2299 | xmlChar *name, const xmlChar *content) { |
| 2300 | xmlNodePtr cur; |
| 2301 | |
| 2302 | cur = xmlNewNodeEatName(ns, name); |
| 2303 | if (cur != NULL) { |
| 2304 | cur->doc = doc; |
| 2305 | if (content != NULL) { |
| 2306 | cur->children = xmlStringGetNodeList(doc, content); |
| 2307 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2308 | } |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 2309 | } else { |
| 2310 | /* if name don't come from the doc dictionnary free it here */ |
| 2311 | if ((name != NULL) && (doc != NULL) && |
| 2312 | (!(xmlDictOwns(doc->dict, name)))) |
| 2313 | xmlFree(name); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2314 | } |
| 2315 | return(cur); |
| 2316 | } |
| 2317 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2318 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2319 | /** |
| 2320 | * xmlNewDocRawNode: |
| 2321 | * @doc: the document |
| 2322 | * @ns: namespace if any |
| 2323 | * @name: the node name |
| 2324 | * @content: the text content if any |
| 2325 | * |
| 2326 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2327 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2328 | * |
| 2329 | * Returns a pointer to the new node object. |
| 2330 | */ |
| 2331 | xmlNodePtr |
| 2332 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2333 | const xmlChar *name, const xmlChar *content) { |
| 2334 | xmlNodePtr cur; |
| 2335 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2336 | cur = xmlNewDocNode(doc, ns, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2337 | if (cur != NULL) { |
| 2338 | cur->doc = doc; |
| 2339 | if (content != NULL) { |
| 2340 | cur->children = xmlNewDocText(doc, content); |
| 2341 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2342 | } |
| 2343 | } |
| 2344 | return(cur); |
| 2345 | } |
| 2346 | |
| 2347 | /** |
| 2348 | * xmlNewDocFragment: |
| 2349 | * @doc: the document owning the fragment |
| 2350 | * |
| 2351 | * Creation of a new Fragment node. |
| 2352 | * Returns a pointer to the new node object. |
| 2353 | */ |
| 2354 | xmlNodePtr |
| 2355 | xmlNewDocFragment(xmlDocPtr doc) { |
| 2356 | xmlNodePtr cur; |
| 2357 | |
| 2358 | /* |
| 2359 | * Allocate a new DocumentFragment node and fill the fields. |
| 2360 | */ |
| 2361 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2362 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2363 | xmlTreeErrMemory("building fragment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2364 | return(NULL); |
| 2365 | } |
| 2366 | memset(cur, 0, sizeof(xmlNode)); |
| 2367 | cur->type = XML_DOCUMENT_FRAG_NODE; |
| 2368 | |
| 2369 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2370 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2371 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2372 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2373 | return(cur); |
| 2374 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2375 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2376 | |
| 2377 | /** |
| 2378 | * xmlNewText: |
| 2379 | * @content: the text content |
| 2380 | * |
| 2381 | * Creation of a new text node. |
| 2382 | * Returns a pointer to the new node object. |
| 2383 | */ |
| 2384 | xmlNodePtr |
| 2385 | xmlNewText(const xmlChar *content) { |
| 2386 | xmlNodePtr cur; |
| 2387 | |
| 2388 | /* |
| 2389 | * Allocate a new node and fill the fields. |
| 2390 | */ |
| 2391 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2392 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2393 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2394 | return(NULL); |
| 2395 | } |
| 2396 | memset(cur, 0, sizeof(xmlNode)); |
| 2397 | cur->type = XML_TEXT_NODE; |
| 2398 | |
| 2399 | cur->name = xmlStringText; |
| 2400 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2401 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2402 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2403 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2404 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2405 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2406 | return(cur); |
| 2407 | } |
| 2408 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2409 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2410 | /** |
| 2411 | * xmlNewTextChild: |
| 2412 | * @parent: the parent node |
| 2413 | * @ns: a namespace if any |
| 2414 | * @name: the name of the child |
| 2415 | * @content: the text content of the child if any. |
| 2416 | * |
| 2417 | * 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] | 2418 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2419 | * 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] | 2420 | * 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] | 2421 | * NOTE: Use xmlNewChild() if @content will contain entities that need to be |
| 2422 | * preserved. Use this function, xmlNewTextChild(), if you need to ensure that |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2423 | * reserved XML chars that might appear in @content, such as the ampersand, |
| 2424 | * greater-than or less-than signs, are automatically replaced by their XML |
| 2425 | * escaped entity representations. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2426 | * |
| 2427 | * Returns a pointer to the new node object. |
| 2428 | */ |
| 2429 | xmlNodePtr |
| 2430 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2431 | const xmlChar *name, const xmlChar *content) { |
| 2432 | xmlNodePtr cur, prev; |
| 2433 | |
| 2434 | if (parent == NULL) { |
| 2435 | #ifdef DEBUG_TREE |
| 2436 | xmlGenericError(xmlGenericErrorContext, |
| 2437 | "xmlNewTextChild : parent == NULL\n"); |
| 2438 | #endif |
| 2439 | return(NULL); |
| 2440 | } |
| 2441 | |
| 2442 | if (name == NULL) { |
| 2443 | #ifdef DEBUG_TREE |
| 2444 | xmlGenericError(xmlGenericErrorContext, |
| 2445 | "xmlNewTextChild : name == NULL\n"); |
| 2446 | #endif |
| 2447 | return(NULL); |
| 2448 | } |
| 2449 | |
| 2450 | /* |
| 2451 | * Allocate a new node |
| 2452 | */ |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 2453 | if (parent->type == XML_ELEMENT_NODE) { |
| 2454 | if (ns == NULL) |
| 2455 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 2456 | else |
| 2457 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 2458 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2459 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2460 | if (ns == NULL) |
| 2461 | cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content); |
| 2462 | else |
| 2463 | cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content); |
| 2464 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2465 | cur = xmlNewDocRawNode( parent->doc, ns, name, content); |
| 2466 | } else { |
| 2467 | return(NULL); |
| 2468 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2469 | if (cur == NULL) return(NULL); |
| 2470 | |
| 2471 | /* |
| 2472 | * add the new element at the end of the children list. |
| 2473 | */ |
| 2474 | cur->type = XML_ELEMENT_NODE; |
| 2475 | cur->parent = parent; |
| 2476 | cur->doc = parent->doc; |
| 2477 | if (parent->children == NULL) { |
| 2478 | parent->children = cur; |
| 2479 | parent->last = cur; |
| 2480 | } else { |
| 2481 | prev = parent->last; |
| 2482 | prev->next = cur; |
| 2483 | cur->prev = prev; |
| 2484 | parent->last = cur; |
| 2485 | } |
| 2486 | |
| 2487 | return(cur); |
| 2488 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2489 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2490 | |
| 2491 | /** |
| 2492 | * xmlNewCharRef: |
| 2493 | * @doc: the document |
| 2494 | * @name: the char ref string, starting with # or "&# ... ;" |
| 2495 | * |
| 2496 | * Creation of a new character reference node. |
| 2497 | * Returns a pointer to the new node object. |
| 2498 | */ |
| 2499 | xmlNodePtr |
| 2500 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 2501 | xmlNodePtr cur; |
| 2502 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2503 | if (name == NULL) |
| 2504 | return(NULL); |
| 2505 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2506 | /* |
| 2507 | * Allocate a new node and fill the fields. |
| 2508 | */ |
| 2509 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2510 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2511 | xmlTreeErrMemory("building character reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2512 | return(NULL); |
| 2513 | } |
| 2514 | memset(cur, 0, sizeof(xmlNode)); |
| 2515 | cur->type = XML_ENTITY_REF_NODE; |
| 2516 | |
| 2517 | cur->doc = doc; |
| 2518 | if (name[0] == '&') { |
| 2519 | int len; |
| 2520 | name++; |
| 2521 | len = xmlStrlen(name); |
| 2522 | if (name[len - 1] == ';') |
| 2523 | cur->name = xmlStrndup(name, len - 1); |
| 2524 | else |
| 2525 | cur->name = xmlStrndup(name, len); |
| 2526 | } else |
| 2527 | cur->name = xmlStrdup(name); |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2528 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2529 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2530 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2531 | return(cur); |
| 2532 | } |
| 2533 | |
| 2534 | /** |
| 2535 | * xmlNewReference: |
| 2536 | * @doc: the document |
| 2537 | * @name: the reference name, or the reference string with & and ; |
| 2538 | * |
| 2539 | * Creation of a new reference node. |
| 2540 | * Returns a pointer to the new node object. |
| 2541 | */ |
| 2542 | xmlNodePtr |
| 2543 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 2544 | xmlNodePtr cur; |
| 2545 | xmlEntityPtr ent; |
| 2546 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2547 | if (name == NULL) |
| 2548 | return(NULL); |
| 2549 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2550 | /* |
| 2551 | * Allocate a new node and fill the fields. |
| 2552 | */ |
| 2553 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2554 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2555 | xmlTreeErrMemory("building reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2556 | return(NULL); |
| 2557 | } |
| 2558 | memset(cur, 0, sizeof(xmlNode)); |
| 2559 | cur->type = XML_ENTITY_REF_NODE; |
| 2560 | |
| 2561 | cur->doc = doc; |
| 2562 | if (name[0] == '&') { |
| 2563 | int len; |
| 2564 | name++; |
| 2565 | len = xmlStrlen(name); |
| 2566 | if (name[len - 1] == ';') |
| 2567 | cur->name = xmlStrndup(name, len - 1); |
| 2568 | else |
| 2569 | cur->name = xmlStrndup(name, len); |
| 2570 | } else |
| 2571 | cur->name = xmlStrdup(name); |
| 2572 | |
| 2573 | ent = xmlGetDocEntity(doc, cur->name); |
| 2574 | if (ent != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2575 | cur->content = ent->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2576 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2577 | * 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] | 2578 | * updated. Not sure if this is 100% correct. |
| 2579 | * -George |
| 2580 | */ |
| 2581 | cur->children = (xmlNodePtr) ent; |
| 2582 | cur->last = (xmlNodePtr) ent; |
| 2583 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2584 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2585 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2586 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2587 | return(cur); |
| 2588 | } |
| 2589 | |
| 2590 | /** |
| 2591 | * xmlNewDocText: |
| 2592 | * @doc: the document |
| 2593 | * @content: the text content |
| 2594 | * |
| 2595 | * Creation of a new text node within a document. |
| 2596 | * Returns a pointer to the new node object. |
| 2597 | */ |
| 2598 | xmlNodePtr |
| 2599 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 2600 | xmlNodePtr cur; |
| 2601 | |
| 2602 | cur = xmlNewText(content); |
| 2603 | if (cur != NULL) cur->doc = doc; |
| 2604 | return(cur); |
| 2605 | } |
| 2606 | |
| 2607 | /** |
| 2608 | * xmlNewTextLen: |
| 2609 | * @content: the text content |
| 2610 | * @len: the text len. |
| 2611 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2612 | * 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] | 2613 | * Returns a pointer to the new node object. |
| 2614 | */ |
| 2615 | xmlNodePtr |
| 2616 | xmlNewTextLen(const xmlChar *content, int len) { |
| 2617 | xmlNodePtr cur; |
| 2618 | |
| 2619 | /* |
| 2620 | * Allocate a new node and fill the fields. |
| 2621 | */ |
| 2622 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2623 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2624 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2625 | return(NULL); |
| 2626 | } |
| 2627 | memset(cur, 0, sizeof(xmlNode)); |
| 2628 | cur->type = XML_TEXT_NODE; |
| 2629 | |
| 2630 | cur->name = xmlStringText; |
| 2631 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2632 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2633 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2634 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2635 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2636 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2637 | return(cur); |
| 2638 | } |
| 2639 | |
| 2640 | /** |
| 2641 | * xmlNewDocTextLen: |
| 2642 | * @doc: the document |
| 2643 | * @content: the text content |
| 2644 | * @len: the text len. |
| 2645 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2646 | * 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] | 2647 | * text node pertain to a given document. |
| 2648 | * Returns a pointer to the new node object. |
| 2649 | */ |
| 2650 | xmlNodePtr |
| 2651 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2652 | xmlNodePtr cur; |
| 2653 | |
| 2654 | cur = xmlNewTextLen(content, len); |
| 2655 | if (cur != NULL) cur->doc = doc; |
| 2656 | return(cur); |
| 2657 | } |
| 2658 | |
| 2659 | /** |
| 2660 | * xmlNewComment: |
| 2661 | * @content: the comment content |
| 2662 | * |
| 2663 | * Creation of a new node containing a comment. |
| 2664 | * Returns a pointer to the new node object. |
| 2665 | */ |
| 2666 | xmlNodePtr |
| 2667 | xmlNewComment(const xmlChar *content) { |
| 2668 | xmlNodePtr cur; |
| 2669 | |
| 2670 | /* |
| 2671 | * Allocate a new node and fill the fields. |
| 2672 | */ |
| 2673 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2674 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2675 | xmlTreeErrMemory("building comment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2676 | return(NULL); |
| 2677 | } |
| 2678 | memset(cur, 0, sizeof(xmlNode)); |
| 2679 | cur->type = XML_COMMENT_NODE; |
| 2680 | |
| 2681 | cur->name = xmlStringComment; |
| 2682 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2683 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2684 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2685 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2686 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2687 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2688 | return(cur); |
| 2689 | } |
| 2690 | |
| 2691 | /** |
| 2692 | * xmlNewCDataBlock: |
| 2693 | * @doc: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2694 | * @content: the CDATA block content content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2695 | * @len: the length of the block |
| 2696 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2697 | * Creation of a new node containing a CDATA block. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2698 | * Returns a pointer to the new node object. |
| 2699 | */ |
| 2700 | xmlNodePtr |
| 2701 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2702 | xmlNodePtr cur; |
| 2703 | |
| 2704 | /* |
| 2705 | * Allocate a new node and fill the fields. |
| 2706 | */ |
| 2707 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2708 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2709 | xmlTreeErrMemory("building CDATA"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2710 | return(NULL); |
| 2711 | } |
| 2712 | memset(cur, 0, sizeof(xmlNode)); |
| 2713 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2714 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2715 | |
| 2716 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2717 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2718 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2719 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2720 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2721 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2722 | return(cur); |
| 2723 | } |
| 2724 | |
| 2725 | /** |
| 2726 | * xmlNewDocComment: |
| 2727 | * @doc: the document |
| 2728 | * @content: the comment content |
| 2729 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2730 | * Creation of a new node containing a comment within a document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2731 | * Returns a pointer to the new node object. |
| 2732 | */ |
| 2733 | xmlNodePtr |
| 2734 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 2735 | xmlNodePtr cur; |
| 2736 | |
| 2737 | cur = xmlNewComment(content); |
| 2738 | if (cur != NULL) cur->doc = doc; |
| 2739 | return(cur); |
| 2740 | } |
| 2741 | |
| 2742 | /** |
| 2743 | * xmlSetTreeDoc: |
| 2744 | * @tree: the top element |
| 2745 | * @doc: the document |
| 2746 | * |
| 2747 | * update all nodes under the tree to point to the right document |
| 2748 | */ |
| 2749 | void |
| 2750 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2751 | xmlAttrPtr prop; |
| 2752 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2753 | if (tree == NULL) |
| 2754 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2755 | if (tree->doc != doc) { |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2756 | if(tree->type == XML_ELEMENT_NODE) { |
| 2757 | prop = tree->properties; |
| 2758 | while (prop != NULL) { |
| 2759 | prop->doc = doc; |
| 2760 | xmlSetListDoc(prop->children, doc); |
| 2761 | prop = prop->next; |
| 2762 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2763 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2764 | if (tree->children != NULL) |
| 2765 | xmlSetListDoc(tree->children, doc); |
| 2766 | tree->doc = doc; |
| 2767 | } |
| 2768 | } |
| 2769 | |
| 2770 | /** |
| 2771 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2772 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2773 | * @doc: the document |
| 2774 | * |
| 2775 | * update all nodes in the list to point to the right document |
| 2776 | */ |
| 2777 | void |
| 2778 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2779 | xmlNodePtr cur; |
| 2780 | |
| 2781 | if (list == NULL) |
| 2782 | return; |
| 2783 | cur = list; |
| 2784 | while (cur != NULL) { |
| 2785 | if (cur->doc != doc) |
| 2786 | xmlSetTreeDoc(cur, doc); |
| 2787 | cur = cur->next; |
| 2788 | } |
| 2789 | } |
| 2790 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 2791 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2792 | /** |
| 2793 | * xmlNewChild: |
| 2794 | * @parent: the parent node |
| 2795 | * @ns: a namespace if any |
| 2796 | * @name: the name of the child |
| 2797 | * @content: the XML content of the child if any. |
| 2798 | * |
| 2799 | * 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] | 2800 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2801 | * created element inherits the namespace of @parent. If @content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2802 | * 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] | 2803 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 2804 | * references. XML special chars must be escaped first by using |
| 2805 | * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2806 | * |
| 2807 | * Returns a pointer to the new node object. |
| 2808 | */ |
| 2809 | xmlNodePtr |
| 2810 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2811 | const xmlChar *name, const xmlChar *content) { |
| 2812 | xmlNodePtr cur, prev; |
| 2813 | |
| 2814 | if (parent == NULL) { |
| 2815 | #ifdef DEBUG_TREE |
| 2816 | xmlGenericError(xmlGenericErrorContext, |
| 2817 | "xmlNewChild : parent == NULL\n"); |
| 2818 | #endif |
| 2819 | return(NULL); |
| 2820 | } |
| 2821 | |
| 2822 | if (name == NULL) { |
| 2823 | #ifdef DEBUG_TREE |
| 2824 | xmlGenericError(xmlGenericErrorContext, |
| 2825 | "xmlNewChild : name == NULL\n"); |
| 2826 | #endif |
| 2827 | return(NULL); |
| 2828 | } |
| 2829 | |
| 2830 | /* |
| 2831 | * Allocate a new node |
| 2832 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2833 | if (parent->type == XML_ELEMENT_NODE) { |
| 2834 | if (ns == NULL) |
| 2835 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2836 | else |
| 2837 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2838 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2839 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2840 | if (ns == NULL) |
| 2841 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2842 | else |
| 2843 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2844 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2845 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2846 | } else { |
| 2847 | return(NULL); |
| 2848 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2849 | if (cur == NULL) return(NULL); |
| 2850 | |
| 2851 | /* |
| 2852 | * add the new element at the end of the children list. |
| 2853 | */ |
| 2854 | cur->type = XML_ELEMENT_NODE; |
| 2855 | cur->parent = parent; |
| 2856 | cur->doc = parent->doc; |
| 2857 | if (parent->children == NULL) { |
| 2858 | parent->children = cur; |
| 2859 | parent->last = cur; |
| 2860 | } else { |
| 2861 | prev = parent->last; |
| 2862 | prev->next = cur; |
| 2863 | cur->prev = prev; |
| 2864 | parent->last = cur; |
| 2865 | } |
| 2866 | |
| 2867 | return(cur); |
| 2868 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2869 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2870 | |
| 2871 | /** |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2872 | * xmlAddPropSibling: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2873 | * @prev: the attribute to which @prop is added after |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2874 | * @cur: the base attribute passed to calling function |
| 2875 | * @prop: the new attribute |
| 2876 | * |
| 2877 | * Add a new attribute after @prev using @cur as base attribute. |
| 2878 | * When inserting before @cur, @prev is passed as @cur->prev. |
| 2879 | * When inserting after @cur, @prev is passed as @cur. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2880 | * If an existing attribute is found it is detroyed prior to adding @prop. |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2881 | * |
| 2882 | * Returns the attribute being inserted or NULL in case of error. |
| 2883 | */ |
| 2884 | static xmlNodePtr |
| 2885 | xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) { |
| 2886 | xmlAttrPtr attr; |
| 2887 | |
| 2888 | if (cur->type != XML_ATTRIBUTE_NODE) |
| 2889 | return(NULL); |
| 2890 | |
| 2891 | /* check if an attribute with the same name exists */ |
| 2892 | if (prop->ns == NULL) |
| 2893 | attr = xmlHasNsProp(cur->parent, prop->name, NULL); |
| 2894 | else |
| 2895 | attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href); |
| 2896 | |
| 2897 | if (prop->doc != cur->doc) { |
| 2898 | xmlSetTreeDoc(prop, cur->doc); |
| 2899 | } |
| 2900 | prop->parent = cur->parent; |
| 2901 | prop->prev = prev; |
| 2902 | if (prev != NULL) { |
| 2903 | prop->next = prev->next; |
| 2904 | prev->next = prop; |
| 2905 | if (prop->next) |
| 2906 | prop->next->prev = prop; |
| 2907 | } else { |
| 2908 | prop->next = cur; |
| 2909 | cur->prev = prop; |
| 2910 | } |
| 2911 | if (prop->prev == NULL && prop->parent != NULL) |
| 2912 | prop->parent->properties = (xmlAttrPtr) prop; |
| 2913 | if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) { |
| 2914 | /* different instance, destroy it (attributes must be unique) */ |
| 2915 | xmlRemoveProp((xmlAttrPtr) attr); |
| 2916 | } |
| 2917 | return prop; |
| 2918 | } |
| 2919 | |
| 2920 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2921 | * xmlAddNextSibling: |
| 2922 | * @cur: the child node |
| 2923 | * @elem: the new node |
| 2924 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2925 | * Add a new node @elem as the next sibling of @cur |
| 2926 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2927 | * first unlinked from its existing context. |
| 2928 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2929 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2930 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2931 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2932 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2933 | */ |
| 2934 | xmlNodePtr |
| 2935 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2936 | if (cur == NULL) { |
| 2937 | #ifdef DEBUG_TREE |
| 2938 | xmlGenericError(xmlGenericErrorContext, |
| 2939 | "xmlAddNextSibling : cur == NULL\n"); |
| 2940 | #endif |
| 2941 | return(NULL); |
| 2942 | } |
| 2943 | if (elem == NULL) { |
| 2944 | #ifdef DEBUG_TREE |
| 2945 | xmlGenericError(xmlGenericErrorContext, |
| 2946 | "xmlAddNextSibling : elem == NULL\n"); |
| 2947 | #endif |
| 2948 | return(NULL); |
| 2949 | } |
| 2950 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2951 | if (cur == elem) { |
| 2952 | #ifdef DEBUG_TREE |
| 2953 | xmlGenericError(xmlGenericErrorContext, |
| 2954 | "xmlAddNextSibling : cur == elem\n"); |
| 2955 | #endif |
| 2956 | return(NULL); |
| 2957 | } |
| 2958 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2959 | xmlUnlinkNode(elem); |
| 2960 | |
| 2961 | if (elem->type == XML_TEXT_NODE) { |
| 2962 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2963 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2964 | xmlFreeNode(elem); |
| 2965 | return(cur); |
| 2966 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2967 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 2968 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2969 | xmlChar *tmp; |
| 2970 | |
| 2971 | tmp = xmlStrdup(elem->content); |
| 2972 | tmp = xmlStrcat(tmp, cur->next->content); |
| 2973 | xmlNodeSetContent(cur->next, tmp); |
| 2974 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2975 | xmlFreeNode(elem); |
| 2976 | return(cur->next); |
| 2977 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2978 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2979 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | if (elem->doc != cur->doc) { |
| 2983 | xmlSetTreeDoc(elem, cur->doc); |
| 2984 | } |
| 2985 | elem->parent = cur->parent; |
| 2986 | elem->prev = cur; |
| 2987 | elem->next = cur->next; |
| 2988 | cur->next = elem; |
| 2989 | if (elem->next != NULL) |
| 2990 | elem->next->prev = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2991 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2992 | elem->parent->last = elem; |
| 2993 | return(elem); |
| 2994 | } |
| 2995 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 2996 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 2997 | defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2998 | /** |
| 2999 | * xmlAddPrevSibling: |
| 3000 | * @cur: the child node |
| 3001 | * @elem: the new node |
| 3002 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3003 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3004 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3005 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3006 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3007 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3008 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3009 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3010 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3011 | */ |
| 3012 | xmlNodePtr |
| 3013 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 3014 | if (cur == NULL) { |
| 3015 | #ifdef DEBUG_TREE |
| 3016 | xmlGenericError(xmlGenericErrorContext, |
| 3017 | "xmlAddPrevSibling : cur == NULL\n"); |
| 3018 | #endif |
| 3019 | return(NULL); |
| 3020 | } |
| 3021 | if (elem == NULL) { |
| 3022 | #ifdef DEBUG_TREE |
| 3023 | xmlGenericError(xmlGenericErrorContext, |
| 3024 | "xmlAddPrevSibling : elem == NULL\n"); |
| 3025 | #endif |
| 3026 | return(NULL); |
| 3027 | } |
| 3028 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3029 | if (cur == elem) { |
| 3030 | #ifdef DEBUG_TREE |
| 3031 | xmlGenericError(xmlGenericErrorContext, |
| 3032 | "xmlAddPrevSibling : cur == elem\n"); |
| 3033 | #endif |
| 3034 | return(NULL); |
| 3035 | } |
| 3036 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3037 | xmlUnlinkNode(elem); |
| 3038 | |
| 3039 | if (elem->type == XML_TEXT_NODE) { |
| 3040 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3041 | xmlChar *tmp; |
| 3042 | |
| 3043 | tmp = xmlStrdup(elem->content); |
| 3044 | tmp = xmlStrcat(tmp, cur->content); |
| 3045 | xmlNodeSetContent(cur, tmp); |
| 3046 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3047 | xmlFreeNode(elem); |
| 3048 | return(cur); |
| 3049 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 3050 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 3051 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3052 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3053 | xmlFreeNode(elem); |
| 3054 | return(cur->prev); |
| 3055 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3056 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3057 | return xmlAddPropSibling(cur->prev, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | if (elem->doc != cur->doc) { |
| 3061 | xmlSetTreeDoc(elem, cur->doc); |
| 3062 | } |
| 3063 | elem->parent = cur->parent; |
| 3064 | elem->next = cur; |
| 3065 | elem->prev = cur->prev; |
| 3066 | cur->prev = elem; |
| 3067 | if (elem->prev != NULL) |
| 3068 | elem->prev->next = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3069 | if ((elem->parent != NULL) && (elem->parent->children == cur)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3070 | elem->parent->children = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3071 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3072 | return(elem); |
| 3073 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3074 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3075 | |
| 3076 | /** |
| 3077 | * xmlAddSibling: |
| 3078 | * @cur: the child node |
| 3079 | * @elem: the new node |
| 3080 | * |
| 3081 | * Add a new element @elem to the list of siblings of @cur |
| 3082 | * merging adjacent TEXT nodes (@elem may be freed) |
| 3083 | * If the new element was already inserted in a document it is |
| 3084 | * first unlinked from its existing context. |
| 3085 | * |
| 3086 | * Returns the new element or NULL in case of error. |
| 3087 | */ |
| 3088 | xmlNodePtr |
| 3089 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 3090 | xmlNodePtr parent; |
| 3091 | |
| 3092 | if (cur == NULL) { |
| 3093 | #ifdef DEBUG_TREE |
| 3094 | xmlGenericError(xmlGenericErrorContext, |
| 3095 | "xmlAddSibling : cur == NULL\n"); |
| 3096 | #endif |
| 3097 | return(NULL); |
| 3098 | } |
| 3099 | |
| 3100 | if (elem == NULL) { |
| 3101 | #ifdef DEBUG_TREE |
| 3102 | xmlGenericError(xmlGenericErrorContext, |
| 3103 | "xmlAddSibling : elem == NULL\n"); |
| 3104 | #endif |
| 3105 | return(NULL); |
| 3106 | } |
| 3107 | |
Daniel Veillard | 43bc89c | 2009-03-23 19:32:04 +0000 | [diff] [blame] | 3108 | if (cur == elem) { |
| 3109 | #ifdef DEBUG_TREE |
| 3110 | xmlGenericError(xmlGenericErrorContext, |
| 3111 | "xmlAddSibling : cur == elem\n"); |
| 3112 | #endif |
| 3113 | return(NULL); |
| 3114 | } |
| 3115 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3116 | /* |
| 3117 | * Constant time is we can rely on the ->parent->last to find |
| 3118 | * the last sibling. |
| 3119 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3120 | if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3121 | (cur->parent->children != NULL) && |
| 3122 | (cur->parent->last != NULL) && |
| 3123 | (cur->parent->last->next == NULL)) { |
| 3124 | cur = cur->parent->last; |
| 3125 | } else { |
| 3126 | while (cur->next != NULL) cur = cur->next; |
| 3127 | } |
| 3128 | |
| 3129 | xmlUnlinkNode(elem); |
| 3130 | |
Daniel Veillard | e22dd5c | 2003-10-29 12:53:27 +0000 | [diff] [blame] | 3131 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) && |
| 3132 | (cur->name == elem->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3133 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3134 | xmlFreeNode(elem); |
| 3135 | return(cur); |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3136 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 3137 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | if (elem->doc != cur->doc) { |
| 3141 | xmlSetTreeDoc(elem, cur->doc); |
| 3142 | } |
| 3143 | parent = cur->parent; |
| 3144 | elem->prev = cur; |
| 3145 | elem->next = NULL; |
| 3146 | elem->parent = parent; |
| 3147 | cur->next = elem; |
| 3148 | if (parent != NULL) |
| 3149 | parent->last = elem; |
| 3150 | |
| 3151 | return(elem); |
| 3152 | } |
| 3153 | |
| 3154 | /** |
| 3155 | * xmlAddChildList: |
| 3156 | * @parent: the parent node |
| 3157 | * @cur: the first node in the list |
| 3158 | * |
| 3159 | * Add a list of node at the end of the child list of the parent |
| 3160 | * merging adjacent TEXT nodes (@cur may be freed) |
| 3161 | * |
| 3162 | * Returns the last child or NULL in case of error. |
| 3163 | */ |
| 3164 | xmlNodePtr |
| 3165 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 3166 | xmlNodePtr prev; |
| 3167 | |
| 3168 | if (parent == NULL) { |
| 3169 | #ifdef DEBUG_TREE |
| 3170 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3171 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3172 | #endif |
| 3173 | return(NULL); |
| 3174 | } |
| 3175 | |
| 3176 | if (cur == NULL) { |
| 3177 | #ifdef DEBUG_TREE |
| 3178 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3179 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3180 | #endif |
| 3181 | return(NULL); |
| 3182 | } |
| 3183 | |
| 3184 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 3185 | (cur->doc != parent->doc)) { |
| 3186 | #ifdef DEBUG_TREE |
| 3187 | xmlGenericError(xmlGenericErrorContext, |
| 3188 | "Elements moved to a different document\n"); |
| 3189 | #endif |
| 3190 | } |
| 3191 | |
| 3192 | /* |
| 3193 | * add the first element at the end of the children list. |
| 3194 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3195 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3196 | if (parent->children == NULL) { |
| 3197 | parent->children = cur; |
| 3198 | } else { |
| 3199 | /* |
| 3200 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 3201 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3202 | if ((cur->type == XML_TEXT_NODE) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3203 | (parent->last->type == XML_TEXT_NODE) && |
| 3204 | (cur->name == parent->last->name)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3205 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3206 | /* |
| 3207 | * if it's the only child, nothing more to be done. |
| 3208 | */ |
| 3209 | if (cur->next == NULL) { |
| 3210 | xmlFreeNode(cur); |
| 3211 | return(parent->last); |
| 3212 | } |
| 3213 | prev = cur; |
| 3214 | cur = cur->next; |
| 3215 | xmlFreeNode(prev); |
| 3216 | } |
| 3217 | prev = parent->last; |
| 3218 | prev->next = cur; |
| 3219 | cur->prev = prev; |
| 3220 | } |
| 3221 | while (cur->next != NULL) { |
| 3222 | cur->parent = parent; |
| 3223 | if (cur->doc != parent->doc) { |
| 3224 | xmlSetTreeDoc(cur, parent->doc); |
| 3225 | } |
| 3226 | cur = cur->next; |
| 3227 | } |
| 3228 | cur->parent = parent; |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3229 | /* the parent may not be linked to a doc ! */ |
| 3230 | if (cur->doc != parent->doc) { |
| 3231 | xmlSetTreeDoc(cur, parent->doc); |
| 3232 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3233 | parent->last = cur; |
| 3234 | |
| 3235 | return(cur); |
| 3236 | } |
| 3237 | |
| 3238 | /** |
| 3239 | * xmlAddChild: |
| 3240 | * @parent: the parent node |
| 3241 | * @cur: the child node |
| 3242 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3243 | * 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] | 3244 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3245 | * If the new node is ATTRIBUTE, it is added into properties instead of children. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3246 | * If there is an attribute with equal name, it is first destroyed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3247 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3248 | * Returns the child or NULL in case of error. |
| 3249 | */ |
| 3250 | xmlNodePtr |
| 3251 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 3252 | xmlNodePtr prev; |
| 3253 | |
| 3254 | if (parent == NULL) { |
| 3255 | #ifdef DEBUG_TREE |
| 3256 | xmlGenericError(xmlGenericErrorContext, |
| 3257 | "xmlAddChild : parent == NULL\n"); |
| 3258 | #endif |
| 3259 | return(NULL); |
| 3260 | } |
| 3261 | |
| 3262 | if (cur == NULL) { |
| 3263 | #ifdef DEBUG_TREE |
| 3264 | xmlGenericError(xmlGenericErrorContext, |
| 3265 | "xmlAddChild : child == NULL\n"); |
| 3266 | #endif |
| 3267 | return(NULL); |
| 3268 | } |
| 3269 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3270 | if (parent == cur) { |
| 3271 | #ifdef DEBUG_TREE |
| 3272 | xmlGenericError(xmlGenericErrorContext, |
| 3273 | "xmlAddChild : parent == cur\n"); |
| 3274 | #endif |
| 3275 | return(NULL); |
| 3276 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3277 | /* |
| 3278 | * 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] | 3279 | * cur is then freed. |
| 3280 | */ |
| 3281 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3282 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3283 | (parent->content != NULL) && |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3284 | (parent->name == cur->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3285 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3286 | xmlFreeNode(cur); |
| 3287 | return(parent); |
| 3288 | } |
| 3289 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3290 | (parent->last->name == cur->name) && |
| 3291 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3292 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3293 | xmlFreeNode(cur); |
| 3294 | return(parent->last); |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | /* |
| 3299 | * add the new element at the end of the children list. |
| 3300 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3301 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3302 | cur->parent = parent; |
| 3303 | if (cur->doc != parent->doc) { |
| 3304 | xmlSetTreeDoc(cur, parent->doc); |
| 3305 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3306 | /* this check prevents a loop on tree-traversions if a developer |
| 3307 | * tries to add a node to its parent multiple times |
| 3308 | */ |
| 3309 | if (prev == parent) |
| 3310 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3311 | |
| 3312 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3313 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3314 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3315 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3316 | (parent->content != NULL) && |
| 3317 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3318 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3319 | xmlFreeNode(cur); |
| 3320 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3321 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3322 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3323 | if (parent->type != XML_ELEMENT_NODE) |
| 3324 | return(NULL); |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3325 | if (parent->properties != NULL) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3326 | /* check if an attribute with the same name exists */ |
| 3327 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3328 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3329 | if (cur->ns == NULL) |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3330 | lastattr = xmlHasNsProp(parent, cur->name, NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3331 | else |
| 3332 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3333 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3334 | /* different instance, destroy it (attributes must be unique) */ |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3335 | xmlUnlinkNode((xmlNodePtr) lastattr); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3336 | xmlFreeProp(lastattr); |
| 3337 | } |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3338 | if (lastattr == (xmlAttrPtr) cur) |
| 3339 | return(cur); |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3340 | |
| 3341 | } |
| 3342 | if (parent->properties == NULL) { |
| 3343 | parent->properties = (xmlAttrPtr) cur; |
| 3344 | } else { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3345 | /* find the end */ |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3346 | xmlAttrPtr lastattr = parent->properties; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3347 | while (lastattr->next != NULL) { |
| 3348 | lastattr = lastattr->next; |
| 3349 | } |
| 3350 | lastattr->next = (xmlAttrPtr) cur; |
| 3351 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3352 | } |
| 3353 | } else { |
| 3354 | if (parent->children == NULL) { |
| 3355 | parent->children = cur; |
| 3356 | parent->last = cur; |
| 3357 | } else { |
| 3358 | prev = parent->last; |
| 3359 | prev->next = cur; |
| 3360 | cur->prev = prev; |
| 3361 | parent->last = cur; |
| 3362 | } |
| 3363 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3364 | return(cur); |
| 3365 | } |
| 3366 | |
| 3367 | /** |
| 3368 | * xmlGetLastChild: |
| 3369 | * @parent: the parent node |
| 3370 | * |
| 3371 | * Search the last child of a node. |
| 3372 | * Returns the last child or NULL if none. |
| 3373 | */ |
| 3374 | xmlNodePtr |
| 3375 | xmlGetLastChild(xmlNodePtr parent) { |
| 3376 | if (parent == NULL) { |
| 3377 | #ifdef DEBUG_TREE |
| 3378 | xmlGenericError(xmlGenericErrorContext, |
| 3379 | "xmlGetLastChild : parent == NULL\n"); |
| 3380 | #endif |
| 3381 | return(NULL); |
| 3382 | } |
| 3383 | return(parent->last); |
| 3384 | } |
| 3385 | |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3386 | #ifdef LIBXML_TREE_ENABLED |
| 3387 | /* |
| 3388 | * 5 interfaces from DOM ElementTraversal |
| 3389 | */ |
| 3390 | |
| 3391 | /** |
| 3392 | * xmlChildElementCount: |
| 3393 | * @parent: the parent node |
| 3394 | * |
| 3395 | * Finds the current number of child nodes of that element which are |
| 3396 | * element nodes. |
| 3397 | * Note the handling of entities references is different than in |
| 3398 | * the W3C DOM element traversal spec since we don't have back reference |
| 3399 | * from entities content to entities references. |
| 3400 | * |
| 3401 | * Returns the count of element child or 0 if not available |
| 3402 | */ |
| 3403 | unsigned long |
| 3404 | xmlChildElementCount(xmlNodePtr parent) { |
| 3405 | unsigned long ret = 0; |
| 3406 | xmlNodePtr cur = NULL; |
| 3407 | |
| 3408 | if (parent == NULL) |
| 3409 | return(0); |
| 3410 | switch (parent->type) { |
| 3411 | case XML_ELEMENT_NODE: |
| 3412 | case XML_ENTITY_NODE: |
| 3413 | case XML_DOCUMENT_NODE: |
| 3414 | case XML_HTML_DOCUMENT_NODE: |
| 3415 | cur = parent->children; |
| 3416 | break; |
| 3417 | default: |
| 3418 | return(0); |
| 3419 | } |
| 3420 | while (cur != NULL) { |
| 3421 | if (cur->type == XML_ELEMENT_NODE) |
| 3422 | ret++; |
| 3423 | cur = cur->next; |
| 3424 | } |
| 3425 | return(ret); |
| 3426 | } |
| 3427 | |
| 3428 | /** |
| 3429 | * xmlFirstElementChild: |
| 3430 | * @parent: the parent node |
| 3431 | * |
| 3432 | * Finds the first child node of that element which is a Element node |
| 3433 | * Note the handling of entities references is different than in |
| 3434 | * the W3C DOM element traversal spec since we don't have back reference |
| 3435 | * from entities content to entities references. |
| 3436 | * |
| 3437 | * Returns the first element child or NULL if not available |
| 3438 | */ |
| 3439 | xmlNodePtr |
| 3440 | xmlFirstElementChild(xmlNodePtr parent) { |
| 3441 | xmlNodePtr cur = NULL; |
| 3442 | |
| 3443 | if (parent == NULL) |
| 3444 | return(NULL); |
| 3445 | switch (parent->type) { |
| 3446 | case XML_ELEMENT_NODE: |
| 3447 | case XML_ENTITY_NODE: |
| 3448 | case XML_DOCUMENT_NODE: |
| 3449 | case XML_HTML_DOCUMENT_NODE: |
| 3450 | cur = parent->children; |
| 3451 | break; |
| 3452 | default: |
| 3453 | return(NULL); |
| 3454 | } |
| 3455 | while (cur != NULL) { |
| 3456 | if (cur->type == XML_ELEMENT_NODE) |
| 3457 | return(cur); |
| 3458 | cur = cur->next; |
| 3459 | } |
| 3460 | return(NULL); |
| 3461 | } |
| 3462 | |
| 3463 | /** |
| 3464 | * xmlLastElementChild: |
| 3465 | * @parent: the parent node |
| 3466 | * |
| 3467 | * Finds the last child node of that element which is a Element node |
| 3468 | * Note the handling of entities references is different than in |
| 3469 | * the W3C DOM element traversal spec since we don't have back reference |
| 3470 | * from entities content to entities references. |
| 3471 | * |
| 3472 | * Returns the last element child or NULL if not available |
| 3473 | */ |
| 3474 | xmlNodePtr |
| 3475 | xmlLastElementChild(xmlNodePtr parent) { |
| 3476 | xmlNodePtr cur = NULL; |
| 3477 | |
| 3478 | if (parent == NULL) |
| 3479 | return(NULL); |
| 3480 | switch (parent->type) { |
| 3481 | case XML_ELEMENT_NODE: |
| 3482 | case XML_ENTITY_NODE: |
| 3483 | case XML_DOCUMENT_NODE: |
| 3484 | case XML_HTML_DOCUMENT_NODE: |
| 3485 | cur = parent->last; |
| 3486 | break; |
| 3487 | default: |
| 3488 | return(NULL); |
| 3489 | } |
| 3490 | while (cur != NULL) { |
| 3491 | if (cur->type == XML_ELEMENT_NODE) |
| 3492 | return(cur); |
| 3493 | cur = cur->prev; |
| 3494 | } |
| 3495 | return(NULL); |
| 3496 | } |
| 3497 | |
| 3498 | /** |
| 3499 | * xmlPreviousElementSibling: |
| 3500 | * @node: the current node |
| 3501 | * |
| 3502 | * Finds the first closest previous sibling of the node which is an |
| 3503 | * element node. |
| 3504 | * Note the handling of entities references is different than in |
| 3505 | * the W3C DOM element traversal spec since we don't have back reference |
| 3506 | * from entities content to entities references. |
| 3507 | * |
| 3508 | * Returns the previous element sibling or NULL if not available |
| 3509 | */ |
| 3510 | xmlNodePtr |
| 3511 | xmlPreviousElementSibling(xmlNodePtr node) { |
| 3512 | if (node == NULL) |
| 3513 | return(NULL); |
| 3514 | switch (node->type) { |
| 3515 | case XML_ELEMENT_NODE: |
| 3516 | case XML_TEXT_NODE: |
| 3517 | case XML_CDATA_SECTION_NODE: |
| 3518 | case XML_ENTITY_REF_NODE: |
| 3519 | case XML_ENTITY_NODE: |
| 3520 | case XML_PI_NODE: |
| 3521 | case XML_COMMENT_NODE: |
| 3522 | case XML_XINCLUDE_START: |
| 3523 | case XML_XINCLUDE_END: |
| 3524 | node = node->prev; |
| 3525 | break; |
| 3526 | default: |
| 3527 | return(NULL); |
| 3528 | } |
| 3529 | while (node != NULL) { |
| 3530 | if (node->type == XML_ELEMENT_NODE) |
| 3531 | return(node); |
| 3532 | node = node->next; |
| 3533 | } |
| 3534 | return(NULL); |
| 3535 | } |
| 3536 | |
| 3537 | /** |
| 3538 | * xmlNextElementSibling: |
| 3539 | * @node: the current node |
| 3540 | * |
| 3541 | * Finds the first closest next sibling of the node which is an |
| 3542 | * element node. |
| 3543 | * Note the handling of entities references is different than in |
| 3544 | * the W3C DOM element traversal spec since we don't have back reference |
| 3545 | * from entities content to entities references. |
| 3546 | * |
| 3547 | * Returns the next element sibling or NULL if not available |
| 3548 | */ |
| 3549 | xmlNodePtr |
| 3550 | xmlNextElementSibling(xmlNodePtr node) { |
| 3551 | if (node == NULL) |
| 3552 | return(NULL); |
| 3553 | switch (node->type) { |
| 3554 | case XML_ELEMENT_NODE: |
| 3555 | case XML_TEXT_NODE: |
| 3556 | case XML_CDATA_SECTION_NODE: |
| 3557 | case XML_ENTITY_REF_NODE: |
| 3558 | case XML_ENTITY_NODE: |
| 3559 | case XML_PI_NODE: |
| 3560 | case XML_COMMENT_NODE: |
| 3561 | case XML_DTD_NODE: |
| 3562 | case XML_XINCLUDE_START: |
| 3563 | case XML_XINCLUDE_END: |
| 3564 | node = node->next; |
| 3565 | break; |
| 3566 | default: |
| 3567 | return(NULL); |
| 3568 | } |
| 3569 | while (node != NULL) { |
| 3570 | if (node->type == XML_ELEMENT_NODE) |
| 3571 | return(node); |
| 3572 | node = node->next; |
| 3573 | } |
| 3574 | return(NULL); |
| 3575 | } |
| 3576 | |
| 3577 | #endif /* LIBXML_TREE_ENABLED */ |
| 3578 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3579 | /** |
| 3580 | * xmlFreeNodeList: |
| 3581 | * @cur: the first node in the list |
| 3582 | * |
| 3583 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3584 | * the children are freed too. |
| 3585 | */ |
| 3586 | void |
| 3587 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3588 | xmlNodePtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3589 | xmlDictPtr dict = NULL; |
| 3590 | |
| 3591 | if (cur == NULL) return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3592 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3593 | xmlFreeNsList((xmlNsPtr) cur); |
| 3594 | return; |
| 3595 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3596 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3597 | #ifdef LIBXML_DOCB_ENABLED |
| 3598 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3599 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3600 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3601 | xmlFreeDoc((xmlDocPtr) cur); |
| 3602 | return; |
| 3603 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3604 | if (cur->doc != NULL) dict = cur->doc->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3605 | while (cur != NULL) { |
| 3606 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3607 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3608 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3609 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3610 | xmlDeregisterNodeDefaultValue(cur); |
| 3611 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3612 | if ((cur->children != NULL) && |
| 3613 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3614 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3615 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3616 | (cur->type == XML_XINCLUDE_START) || |
| 3617 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3618 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3619 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3620 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3621 | (cur->type != XML_XINCLUDE_START) && |
| 3622 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3623 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3624 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3625 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3626 | } |
| 3627 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3628 | (cur->type == XML_XINCLUDE_START) || |
| 3629 | (cur->type == XML_XINCLUDE_END)) && |
| 3630 | (cur->nsDef != NULL)) |
| 3631 | xmlFreeNsList(cur->nsDef); |
| 3632 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3633 | /* |
| 3634 | * When a node is a text node or a comment, it uses a global static |
| 3635 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3636 | * Otherwise the node name might come from the document's |
| 3637 | * dictionnary |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3638 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3639 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3640 | (cur->type != XML_TEXT_NODE) && |
| 3641 | (cur->type != XML_COMMENT_NODE)) |
| 3642 | DICT_FREE(cur->name) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3643 | xmlFree(cur); |
| 3644 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3645 | cur = next; |
| 3646 | } |
| 3647 | } |
| 3648 | |
| 3649 | /** |
| 3650 | * xmlFreeNode: |
| 3651 | * @cur: the node |
| 3652 | * |
| 3653 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3654 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3655 | */ |
| 3656 | void |
| 3657 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3658 | xmlDictPtr dict = NULL; |
| 3659 | |
| 3660 | if (cur == NULL) return; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3661 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3662 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3663 | if (cur->type == XML_DTD_NODE) { |
| 3664 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3665 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3666 | } |
| 3667 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3668 | xmlFreeNs((xmlNsPtr) cur); |
| 3669 | return; |
| 3670 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3671 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3672 | xmlFreeProp((xmlAttrPtr) cur); |
| 3673 | return; |
| 3674 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3675 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3676 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3677 | xmlDeregisterNodeDefaultValue(cur); |
| 3678 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3679 | if (cur->doc != NULL) dict = cur->doc->dict; |
| 3680 | |
Daniel Veillard | 2cba415 | 2008-08-27 11:45:41 +0000 | [diff] [blame] | 3681 | if (cur->type == XML_ENTITY_DECL) { |
| 3682 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 3683 | DICT_FREE(ent->SystemID); |
| 3684 | DICT_FREE(ent->ExternalID); |
| 3685 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3686 | if ((cur->children != NULL) && |
| 3687 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3688 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3689 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3690 | (cur->type == XML_XINCLUDE_START) || |
| 3691 | (cur->type == XML_XINCLUDE_END)) && |
| 3692 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3693 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3694 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3695 | (cur->content != NULL) && |
| 3696 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3697 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3698 | (cur->type != XML_XINCLUDE_START) && |
| 3699 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3700 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3703 | /* |
| 3704 | * When a node is a text node or a comment, it uses a global static |
| 3705 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3706 | * Otherwise the node name might come from the document's dictionnary |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3707 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3708 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3709 | (cur->type != XML_TEXT_NODE) && |
| 3710 | (cur->type != XML_COMMENT_NODE)) |
| 3711 | DICT_FREE(cur->name) |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3712 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3713 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3714 | (cur->type == XML_XINCLUDE_START) || |
| 3715 | (cur->type == XML_XINCLUDE_END)) && |
| 3716 | (cur->nsDef != NULL)) |
| 3717 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3718 | xmlFree(cur); |
| 3719 | } |
| 3720 | |
| 3721 | /** |
| 3722 | * xmlUnlinkNode: |
| 3723 | * @cur: the node |
| 3724 | * |
| 3725 | * Unlink a node from it's current context, the node is not freed |
| 3726 | */ |
| 3727 | void |
| 3728 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3729 | if (cur == NULL) { |
| 3730 | #ifdef DEBUG_TREE |
| 3731 | xmlGenericError(xmlGenericErrorContext, |
| 3732 | "xmlUnlinkNode : node == NULL\n"); |
| 3733 | #endif |
| 3734 | return; |
| 3735 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3736 | if (cur->type == XML_DTD_NODE) { |
| 3737 | xmlDocPtr doc; |
| 3738 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3739 | if (doc != NULL) { |
| 3740 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3741 | doc->intSubset = NULL; |
| 3742 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3743 | doc->extSubset = NULL; |
| 3744 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3745 | } |
Daniel Veillard | 2cba415 | 2008-08-27 11:45:41 +0000 | [diff] [blame] | 3746 | if (cur->type == XML_ENTITY_DECL) { |
| 3747 | xmlDocPtr doc; |
| 3748 | doc = cur->doc; |
| 3749 | if (doc != NULL) { |
| 3750 | if (doc->intSubset != NULL) { |
| 3751 | if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur) |
| 3752 | xmlHashRemoveEntry(doc->intSubset->entities, cur->name, |
| 3753 | NULL); |
| 3754 | if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur) |
| 3755 | xmlHashRemoveEntry(doc->intSubset->pentities, cur->name, |
| 3756 | NULL); |
| 3757 | } |
| 3758 | if (doc->extSubset != NULL) { |
| 3759 | if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur) |
| 3760 | xmlHashRemoveEntry(doc->extSubset->entities, cur->name, |
| 3761 | NULL); |
| 3762 | if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur) |
| 3763 | xmlHashRemoveEntry(doc->extSubset->pentities, cur->name, |
| 3764 | NULL); |
| 3765 | } |
| 3766 | } |
| 3767 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3768 | if (cur->parent != NULL) { |
| 3769 | xmlNodePtr parent; |
| 3770 | parent = cur->parent; |
| 3771 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3772 | if (parent->properties == (xmlAttrPtr) cur) |
| 3773 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3774 | } else { |
| 3775 | if (parent->children == cur) |
| 3776 | parent->children = cur->next; |
| 3777 | if (parent->last == cur) |
| 3778 | parent->last = cur->prev; |
| 3779 | } |
| 3780 | cur->parent = NULL; |
| 3781 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3782 | if (cur->next != NULL) |
| 3783 | cur->next->prev = cur->prev; |
| 3784 | if (cur->prev != NULL) |
| 3785 | cur->prev->next = cur->next; |
| 3786 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 3789 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3790 | /** |
| 3791 | * xmlReplaceNode: |
| 3792 | * @old: the old node |
| 3793 | * @cur: the node |
| 3794 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 3795 | * Unlink the old node from its current context, prune the new one |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3796 | * 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] | 3797 | * first unlinked from its existing context. |
| 3798 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3799 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3800 | */ |
| 3801 | xmlNodePtr |
| 3802 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 3803 | if (old == cur) return(NULL); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3804 | if ((old == NULL) || (old->parent == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3805 | #ifdef DEBUG_TREE |
| 3806 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3807 | "xmlReplaceNode : old == NULL or without parent\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3808 | #endif |
| 3809 | return(NULL); |
| 3810 | } |
| 3811 | if (cur == NULL) { |
| 3812 | xmlUnlinkNode(old); |
| 3813 | return(old); |
| 3814 | } |
| 3815 | if (cur == old) { |
| 3816 | return(old); |
| 3817 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3818 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3819 | #ifdef DEBUG_TREE |
| 3820 | xmlGenericError(xmlGenericErrorContext, |
| 3821 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3822 | #endif |
| 3823 | return(old); |
| 3824 | } |
| 3825 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3826 | #ifdef DEBUG_TREE |
| 3827 | xmlGenericError(xmlGenericErrorContext, |
| 3828 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3829 | #endif |
| 3830 | return(old); |
| 3831 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3832 | xmlUnlinkNode(cur); |
Daniel Veillard | 64d7d12 | 2005-05-11 18:03:42 +0000 | [diff] [blame] | 3833 | xmlSetTreeDoc(cur, old->doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3834 | cur->parent = old->parent; |
| 3835 | cur->next = old->next; |
| 3836 | if (cur->next != NULL) |
| 3837 | cur->next->prev = cur; |
| 3838 | cur->prev = old->prev; |
| 3839 | if (cur->prev != NULL) |
| 3840 | cur->prev->next = cur; |
| 3841 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3842 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3843 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3844 | cur->parent->properties = ((xmlAttrPtr) cur); |
| 3845 | } else { |
| 3846 | if (cur->parent->children == old) |
| 3847 | cur->parent->children = cur; |
| 3848 | if (cur->parent->last == old) |
| 3849 | cur->parent->last = cur; |
| 3850 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3851 | } |
| 3852 | old->next = old->prev = NULL; |
| 3853 | old->parent = NULL; |
| 3854 | return(old); |
| 3855 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3856 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3857 | |
| 3858 | /************************************************************************ |
| 3859 | * * |
| 3860 | * Copy operations * |
| 3861 | * * |
| 3862 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3863 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3864 | /** |
| 3865 | * xmlCopyNamespace: |
| 3866 | * @cur: the namespace |
| 3867 | * |
| 3868 | * Do a copy of the namespace. |
| 3869 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3870 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3871 | */ |
| 3872 | xmlNsPtr |
| 3873 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3874 | xmlNsPtr ret; |
| 3875 | |
| 3876 | if (cur == NULL) return(NULL); |
| 3877 | switch (cur->type) { |
| 3878 | case XML_LOCAL_NAMESPACE: |
| 3879 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3880 | break; |
| 3881 | default: |
| 3882 | #ifdef DEBUG_TREE |
| 3883 | xmlGenericError(xmlGenericErrorContext, |
| 3884 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3885 | #endif |
| 3886 | return(NULL); |
| 3887 | } |
| 3888 | return(ret); |
| 3889 | } |
| 3890 | |
| 3891 | /** |
| 3892 | * xmlCopyNamespaceList: |
| 3893 | * @cur: the first namespace |
| 3894 | * |
| 3895 | * Do a copy of an namespace list. |
| 3896 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3897 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3898 | */ |
| 3899 | xmlNsPtr |
| 3900 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3901 | xmlNsPtr ret = NULL; |
| 3902 | xmlNsPtr p = NULL,q; |
| 3903 | |
| 3904 | while (cur != NULL) { |
| 3905 | q = xmlCopyNamespace(cur); |
| 3906 | if (p == NULL) { |
| 3907 | ret = p = q; |
| 3908 | } else { |
| 3909 | p->next = q; |
| 3910 | p = q; |
| 3911 | } |
| 3912 | cur = cur->next; |
| 3913 | } |
| 3914 | return(ret); |
| 3915 | } |
| 3916 | |
| 3917 | static xmlNodePtr |
| 3918 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3919 | |
| 3920 | static xmlAttrPtr |
| 3921 | xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3922 | xmlAttrPtr ret; |
| 3923 | |
| 3924 | if (cur == NULL) return(NULL); |
| 3925 | if (target != NULL) |
| 3926 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3927 | else if (doc != NULL) |
| 3928 | ret = xmlNewDocProp(doc, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3929 | else if (cur->parent != NULL) |
| 3930 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 3931 | else if (cur->children != NULL) |
| 3932 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 3933 | else |
| 3934 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 3935 | if (ret == NULL) return(NULL); |
| 3936 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3937 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3938 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3939 | xmlNsPtr ns; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3940 | |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3941 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3942 | if (ns == NULL) { |
| 3943 | /* |
| 3944 | * Humm, we are copying an element whose namespace is defined |
| 3945 | * out of the new tree scope. Search it in the original tree |
| 3946 | * and add it at the top of the new tree |
| 3947 | */ |
| 3948 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 3949 | if (ns != NULL) { |
| 3950 | xmlNodePtr root = target; |
| 3951 | xmlNodePtr pred = NULL; |
| 3952 | |
| 3953 | while (root->parent != NULL) { |
| 3954 | pred = root; |
| 3955 | root = root->parent; |
| 3956 | } |
| 3957 | if (root == (xmlNodePtr) target->doc) { |
| 3958 | /* correct possibly cycling above the document elt */ |
| 3959 | root = pred; |
| 3960 | } |
| 3961 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 3962 | } |
| 3963 | } else { |
| 3964 | /* |
| 3965 | * we have to find something appropriate here since |
| 3966 | * we cant be sure, that the namespce we found is identified |
| 3967 | * by the prefix |
| 3968 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3969 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3970 | /* this is the nice case */ |
| 3971 | ret->ns = ns; |
| 3972 | } else { |
| 3973 | /* |
| 3974 | * we are in trouble: we need a new reconcilied namespace. |
| 3975 | * This is expensive |
| 3976 | */ |
| 3977 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 3978 | } |
| 3979 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3980 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3981 | } else |
| 3982 | ret->ns = NULL; |
| 3983 | |
| 3984 | if (cur->children != NULL) { |
| 3985 | xmlNodePtr tmp; |
| 3986 | |
| 3987 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 3988 | ret->last = NULL; |
| 3989 | tmp = ret->children; |
| 3990 | while (tmp != NULL) { |
| 3991 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 3992 | if (tmp->next == NULL) |
| 3993 | ret->last = tmp; |
| 3994 | tmp = tmp->next; |
| 3995 | } |
| 3996 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3997 | /* |
| 3998 | * Try to handle IDs |
| 3999 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 4000 | if ((target!= NULL) && (cur!= NULL) && |
| 4001 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 4002 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 4003 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 4004 | xmlChar *id; |
| 4005 | |
| 4006 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 4007 | if (id != NULL) { |
| 4008 | xmlAddID(NULL, target->doc, id, ret); |
| 4009 | xmlFree(id); |
| 4010 | } |
| 4011 | } |
| 4012 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4013 | return(ret); |
| 4014 | } |
| 4015 | |
| 4016 | /** |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 4017 | * xmlCopyProp: |
| 4018 | * @target: the element where the attribute will be grafted |
| 4019 | * @cur: the attribute |
| 4020 | * |
| 4021 | * Do a copy of the attribute. |
| 4022 | * |
| 4023 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
| 4024 | */ |
| 4025 | xmlAttrPtr |
| 4026 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 4027 | return xmlCopyPropInternal(NULL, target, cur); |
| 4028 | } |
| 4029 | |
| 4030 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4031 | * xmlCopyPropList: |
| 4032 | * @target: the element where the attributes will be grafted |
| 4033 | * @cur: the first attribute |
| 4034 | * |
| 4035 | * Do a copy of an attribute list. |
| 4036 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4037 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4038 | */ |
| 4039 | xmlAttrPtr |
| 4040 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 4041 | xmlAttrPtr ret = NULL; |
| 4042 | xmlAttrPtr p = NULL,q; |
| 4043 | |
| 4044 | while (cur != NULL) { |
| 4045 | q = xmlCopyProp(target, cur); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4046 | if (q == NULL) |
| 4047 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4048 | if (p == NULL) { |
| 4049 | ret = p = q; |
| 4050 | } else { |
| 4051 | p->next = q; |
| 4052 | q->prev = p; |
| 4053 | p = q; |
| 4054 | } |
| 4055 | cur = cur->next; |
| 4056 | } |
| 4057 | return(ret); |
| 4058 | } |
| 4059 | |
| 4060 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4061 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4062 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4063 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4064 | * tricky reason: namespaces. Doing a direct copy of a node |
| 4065 | * say RPM:Copyright without changing the namespace pointer to |
| 4066 | * something else can produce stale links. One way to do it is |
| 4067 | * to keep a reference counter but this doesn't work as soon |
| 4068 | * as one move the element or the subtree out of the scope of |
| 4069 | * the existing namespace. The actual solution seems to add |
| 4070 | * a copy of the namespace at the top of the copied tree if |
| 4071 | * not available in the subtree. |
| 4072 | * Hence two functions, the public front-end call the inner ones |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4073 | * The argument "recursive" normally indicates a recursive copy |
| 4074 | * of the node with values 0 (no) and 1 (yes). For XInclude, |
| 4075 | * however, we allow a value of 2 to indicate copy properties and |
| 4076 | * namespace info, but don't recurse on children. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4077 | */ |
| 4078 | |
| 4079 | static xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 4080 | xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4081 | int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4082 | xmlNodePtr ret; |
| 4083 | |
| 4084 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4085 | switch (node->type) { |
| 4086 | case XML_TEXT_NODE: |
| 4087 | case XML_CDATA_SECTION_NODE: |
| 4088 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 4089 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4090 | case XML_ENTITY_REF_NODE: |
| 4091 | case XML_ENTITY_NODE: |
| 4092 | case XML_PI_NODE: |
| 4093 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4094 | case XML_XINCLUDE_START: |
| 4095 | case XML_XINCLUDE_END: |
| 4096 | break; |
| 4097 | case XML_ATTRIBUTE_NODE: |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 4098 | return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node)); |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4099 | case XML_NAMESPACE_DECL: |
| 4100 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4101 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4102 | case XML_DOCUMENT_NODE: |
| 4103 | case XML_HTML_DOCUMENT_NODE: |
| 4104 | #ifdef LIBXML_DOCB_ENABLED |
| 4105 | case XML_DOCB_DOCUMENT_NODE: |
| 4106 | #endif |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4107 | #ifdef LIBXML_TREE_ENABLED |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4108 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended)); |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4109 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4110 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4111 | case XML_NOTATION_NODE: |
| 4112 | case XML_DTD_NODE: |
| 4113 | case XML_ELEMENT_DECL: |
| 4114 | case XML_ATTRIBUTE_DECL: |
| 4115 | case XML_ENTITY_DECL: |
| 4116 | return(NULL); |
| 4117 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4118 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4119 | /* |
| 4120 | * Allocate a new node and fill the fields. |
| 4121 | */ |
| 4122 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 4123 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4124 | xmlTreeErrMemory("copying node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4125 | return(NULL); |
| 4126 | } |
| 4127 | memset(ret, 0, sizeof(xmlNode)); |
| 4128 | ret->type = node->type; |
| 4129 | |
| 4130 | ret->doc = doc; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4131 | ret->parent = parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4132 | if (node->name == xmlStringText) |
| 4133 | ret->name = xmlStringText; |
| 4134 | else if (node->name == xmlStringTextNoenc) |
| 4135 | ret->name = xmlStringTextNoenc; |
| 4136 | else if (node->name == xmlStringComment) |
| 4137 | ret->name = xmlStringComment; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4138 | else if (node->name != NULL) { |
| 4139 | if ((doc != NULL) && (doc->dict != NULL)) |
| 4140 | ret->name = xmlDictLookup(doc->dict, node->name, -1); |
| 4141 | else |
| 4142 | ret->name = xmlStrdup(node->name); |
| 4143 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 4144 | if ((node->type != XML_ELEMENT_NODE) && |
| 4145 | (node->content != NULL) && |
| 4146 | (node->type != XML_ENTITY_REF_NODE) && |
| 4147 | (node->type != XML_XINCLUDE_END) && |
| 4148 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4149 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 4150 | }else{ |
| 4151 | if (node->type == XML_ELEMENT_NODE) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 4152 | ret->line = node->line; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4153 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4154 | if (parent != NULL) { |
| 4155 | xmlNodePtr tmp; |
| 4156 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4157 | /* |
| 4158 | * this is a tricky part for the node register thing: |
| 4159 | * in case ret does get coalesced in xmlAddChild |
| 4160 | * the deregister-node callback is called; so we register ret now already |
| 4161 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 4162 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4163 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 4164 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4165 | tmp = xmlAddChild(parent, ret); |
| 4166 | /* node could have coalesced */ |
| 4167 | if (tmp != ret) |
| 4168 | return(tmp); |
| 4169 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4170 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4171 | if (!extended) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4172 | goto out; |
Petr Pajas | 2afca4a | 2009-07-30 17:47:32 +0200 | [diff] [blame] | 4173 | if (((node->type == XML_ELEMENT_NODE) || |
| 4174 | (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4175 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 4176 | |
| 4177 | if (node->ns != NULL) { |
| 4178 | xmlNsPtr ns; |
| 4179 | |
| 4180 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 4181 | if (ns == NULL) { |
| 4182 | /* |
| 4183 | * Humm, we are copying an element whose namespace is defined |
| 4184 | * out of the new tree scope. Search it in the original tree |
| 4185 | * and add it at the top of the new tree |
| 4186 | */ |
| 4187 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 4188 | if (ns != NULL) { |
| 4189 | xmlNodePtr root = ret; |
| 4190 | |
| 4191 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 4192 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4193 | } |
| 4194 | } else { |
| 4195 | /* |
| 4196 | * reference the existing namespace definition in our own tree. |
| 4197 | */ |
| 4198 | ret->ns = ns; |
| 4199 | } |
| 4200 | } |
Petr Pajas | 2afca4a | 2009-07-30 17:47:32 +0200 | [diff] [blame] | 4201 | if (((node->type == XML_ELEMENT_NODE) || |
| 4202 | (node->type == XML_XINCLUDE_START)) && (node->properties != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4203 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4204 | if (node->type == XML_ENTITY_REF_NODE) { |
| 4205 | if ((doc == NULL) || (node->doc != doc)) { |
| 4206 | /* |
| 4207 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4208 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4209 | * we cannot keep the reference. Try to find it in the |
| 4210 | * target document. |
| 4211 | */ |
| 4212 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 4213 | } else { |
| 4214 | ret->children = node->children; |
| 4215 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 4216 | ret->last = ret->children; |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4217 | } else if ((node->children != NULL) && (extended != 2)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4218 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 4219 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 4220 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4221 | |
| 4222 | out: |
| 4223 | /* if parent != NULL we already registered the node above */ |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 4224 | if ((parent == NULL) && |
| 4225 | ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4226 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4227 | return(ret); |
| 4228 | } |
| 4229 | |
| 4230 | static xmlNodePtr |
| 4231 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 4232 | xmlNodePtr ret = NULL; |
| 4233 | xmlNodePtr p = NULL,q; |
| 4234 | |
| 4235 | while (node != NULL) { |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4236 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4237 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 4238 | if (doc == NULL) { |
| 4239 | node = node->next; |
| 4240 | continue; |
| 4241 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4242 | if (doc->intSubset == NULL) { |
| 4243 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
| 4244 | q->doc = doc; |
| 4245 | q->parent = parent; |
| 4246 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4247 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4248 | } else { |
| 4249 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4250 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4251 | } |
| 4252 | } else |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4253 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4254 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4255 | if (ret == NULL) { |
| 4256 | q->prev = NULL; |
| 4257 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4258 | } else if (p != q) { |
| 4259 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4260 | p->next = q; |
| 4261 | q->prev = p; |
| 4262 | p = q; |
| 4263 | } |
| 4264 | node = node->next; |
| 4265 | } |
| 4266 | return(ret); |
| 4267 | } |
| 4268 | |
| 4269 | /** |
| 4270 | * xmlCopyNode: |
| 4271 | * @node: the node |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4272 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4273 | * when applicable) |
| 4274 | * if 2 copy properties and namespaces (when applicable) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4275 | * |
| 4276 | * Do a copy of the node. |
| 4277 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4278 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4279 | */ |
| 4280 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4281 | xmlCopyNode(const xmlNodePtr node, int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4282 | xmlNodePtr ret; |
| 4283 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4284 | ret = xmlStaticCopyNode(node, NULL, NULL, extended); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4285 | return(ret); |
| 4286 | } |
| 4287 | |
| 4288 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4289 | * xmlDocCopyNode: |
| 4290 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4291 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4292 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4293 | * when applicable) |
| 4294 | * if 2 copy properties and namespaces (when applicable) |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4295 | * |
| 4296 | * Do a copy of the node to a given document. |
| 4297 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4298 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4299 | */ |
| 4300 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4301 | xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4302 | xmlNodePtr ret; |
| 4303 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4304 | ret = xmlStaticCopyNode(node, doc, NULL, extended); |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4305 | return(ret); |
| 4306 | } |
| 4307 | |
| 4308 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4309 | * xmlDocCopyNodeList: |
| 4310 | * @doc: the target document |
| 4311 | * @node: the first node in the list. |
| 4312 | * |
| 4313 | * Do a recursive copy of the node list. |
| 4314 | * |
| 4315 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
| 4316 | */ |
| 4317 | xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) { |
| 4318 | xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL); |
| 4319 | return(ret); |
| 4320 | } |
| 4321 | |
| 4322 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4323 | * xmlCopyNodeList: |
| 4324 | * @node: the first node in the list. |
| 4325 | * |
| 4326 | * Do a recursive copy of the node list. |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4327 | * Use xmlDocCopyNodeList() if possible to ensure string interning. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4328 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4329 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4330 | */ |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 4331 | xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4332 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 4333 | return(ret); |
| 4334 | } |
| 4335 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4336 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4337 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4338 | * xmlCopyDtd: |
| 4339 | * @dtd: the dtd |
| 4340 | * |
| 4341 | * Do a copy of the dtd. |
| 4342 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4343 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4344 | */ |
| 4345 | xmlDtdPtr |
| 4346 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 4347 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4348 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4349 | |
| 4350 | if (dtd == NULL) return(NULL); |
| 4351 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 4352 | if (ret == NULL) return(NULL); |
| 4353 | if (dtd->entities != NULL) |
| 4354 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 4355 | (xmlEntitiesTablePtr) dtd->entities); |
| 4356 | if (dtd->notations != NULL) |
| 4357 | ret->notations = (void *) xmlCopyNotationTable( |
| 4358 | (xmlNotationTablePtr) dtd->notations); |
| 4359 | if (dtd->elements != NULL) |
| 4360 | ret->elements = (void *) xmlCopyElementTable( |
| 4361 | (xmlElementTablePtr) dtd->elements); |
| 4362 | if (dtd->attributes != NULL) |
| 4363 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 4364 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4365 | if (dtd->pentities != NULL) |
| 4366 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 4367 | (xmlEntitiesTablePtr) dtd->pentities); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4368 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4369 | cur = dtd->children; |
| 4370 | while (cur != NULL) { |
| 4371 | q = NULL; |
| 4372 | |
| 4373 | if (cur->type == XML_ENTITY_DECL) { |
| 4374 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 4375 | switch (tmp->etype) { |
| 4376 | case XML_INTERNAL_GENERAL_ENTITY: |
| 4377 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 4378 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 4379 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 4380 | break; |
| 4381 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 4382 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4383 | q = (xmlNodePtr) |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4384 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 4385 | break; |
| 4386 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 4387 | break; |
| 4388 | } |
| 4389 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 4390 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 4391 | q = (xmlNodePtr) |
| 4392 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 4393 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4394 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4395 | q = (xmlNodePtr) |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4396 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 4397 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4398 | q = xmlCopyNode(cur, 0); |
| 4399 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4400 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4401 | if (q == NULL) { |
| 4402 | cur = cur->next; |
| 4403 | continue; |
| 4404 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4405 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4406 | if (p == NULL) |
| 4407 | ret->children = q; |
| 4408 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4409 | p->next = q; |
| 4410 | |
| 4411 | q->prev = p; |
| 4412 | q->parent = (xmlNodePtr) ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4413 | q->next = NULL; |
| 4414 | ret->last = q; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4415 | p = q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4416 | cur = cur->next; |
| 4417 | } |
| 4418 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4419 | return(ret); |
| 4420 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4421 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4422 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4423 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4424 | /** |
| 4425 | * xmlCopyDoc: |
| 4426 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4427 | * @recursive: if not zero do a recursive copy. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4428 | * |
| 4429 | * 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] | 4430 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4431 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4432 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4433 | */ |
| 4434 | xmlDocPtr |
| 4435 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 4436 | xmlDocPtr ret; |
| 4437 | |
| 4438 | if (doc == NULL) return(NULL); |
| 4439 | ret = xmlNewDoc(doc->version); |
| 4440 | if (ret == NULL) return(NULL); |
| 4441 | if (doc->name != NULL) |
| 4442 | ret->name = xmlMemStrdup(doc->name); |
| 4443 | if (doc->encoding != NULL) |
| 4444 | ret->encoding = xmlStrdup(doc->encoding); |
Daniel Veillard | f59507d | 2005-01-27 17:26:49 +0000 | [diff] [blame] | 4445 | if (doc->URL != NULL) |
| 4446 | ret->URL = xmlStrdup(doc->URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4447 | ret->charset = doc->charset; |
| 4448 | ret->compression = doc->compression; |
| 4449 | ret->standalone = doc->standalone; |
| 4450 | if (!recursive) return(ret); |
| 4451 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4452 | ret->last = NULL; |
| 4453 | ret->children = NULL; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4454 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4455 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4456 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4457 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4458 | ret->intSubset->parent = ret; |
| 4459 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4460 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4461 | if (doc->oldNs != NULL) |
| 4462 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 4463 | if (doc->children != NULL) { |
| 4464 | xmlNodePtr tmp; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4465 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4466 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 4467 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4468 | ret->last = NULL; |
| 4469 | tmp = ret->children; |
| 4470 | while (tmp != NULL) { |
| 4471 | if (tmp->next == NULL) |
| 4472 | ret->last = tmp; |
| 4473 | tmp = tmp->next; |
| 4474 | } |
| 4475 | } |
| 4476 | return(ret); |
| 4477 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4478 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4479 | |
| 4480 | /************************************************************************ |
| 4481 | * * |
| 4482 | * Content access functions * |
| 4483 | * * |
| 4484 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4485 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4486 | /** |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4487 | * xmlGetLineNo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4488 | * @node: valid node |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4489 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 4490 | * Get line number of @node. This requires activation of this option |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4491 | * before invoking the parser by calling xmlLineNumbersDefault(1) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4492 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4493 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4494 | */ |
| 4495 | long |
| 4496 | xmlGetLineNo(xmlNodePtr node) |
| 4497 | { |
| 4498 | long result = -1; |
| 4499 | |
| 4500 | if (!node) |
| 4501 | return result; |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4502 | if ((node->type == XML_ELEMENT_NODE) || |
| 4503 | (node->type == XML_TEXT_NODE) || |
| 4504 | (node->type == XML_COMMENT_NODE) || |
| 4505 | (node->type == XML_PI_NODE)) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 4506 | result = (long) node->line; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4507 | else if ((node->prev != NULL) && |
| 4508 | ((node->prev->type == XML_ELEMENT_NODE) || |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4509 | (node->prev->type == XML_TEXT_NODE) || |
| 4510 | (node->prev->type == XML_COMMENT_NODE) || |
| 4511 | (node->prev->type == XML_PI_NODE))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4512 | result = xmlGetLineNo(node->prev); |
| 4513 | else if ((node->parent != NULL) && |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4514 | (node->parent->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4515 | result = xmlGetLineNo(node->parent); |
| 4516 | |
| 4517 | return result; |
| 4518 | } |
| 4519 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4520 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4521 | /** |
| 4522 | * xmlGetNodePath: |
| 4523 | * @node: a node |
| 4524 | * |
| 4525 | * Build a structure based Path for the given node |
| 4526 | * |
| 4527 | * Returns the new path or NULL in case of error. The caller must free |
| 4528 | * the returned string |
| 4529 | */ |
| 4530 | xmlChar * |
| 4531 | xmlGetNodePath(xmlNodePtr node) |
| 4532 | { |
| 4533 | xmlNodePtr cur, tmp, next; |
| 4534 | xmlChar *buffer = NULL, *temp; |
| 4535 | size_t buf_len; |
| 4536 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4537 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4538 | const char *name; |
| 4539 | char nametemp[100]; |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4540 | int occur = 0, generic; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4541 | |
| 4542 | if (node == NULL) |
| 4543 | return (NULL); |
| 4544 | |
| 4545 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4546 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4547 | if (buffer == NULL) { |
| 4548 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4549 | return (NULL); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4550 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4551 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4552 | if (buf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4553 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4554 | xmlFree(buffer); |
| 4555 | return (NULL); |
| 4556 | } |
| 4557 | |
| 4558 | buffer[0] = 0; |
| 4559 | cur = node; |
| 4560 | do { |
| 4561 | name = ""; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4562 | sep = "?"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4563 | occur = 0; |
| 4564 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4565 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4566 | if (buffer[0] == '/') |
| 4567 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4568 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4569 | next = NULL; |
| 4570 | } else if (cur->type == XML_ELEMENT_NODE) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4571 | generic = 0; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4572 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4573 | name = (const char *) cur->name; |
| 4574 | if (cur->ns) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4575 | if (cur->ns->prefix != NULL) { |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4576 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4577 | (char *)cur->ns->prefix, (char *)cur->name); |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4578 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4579 | name = nametemp; |
| 4580 | } else { |
| 4581 | /* |
| 4582 | * We cannot express named elements in the default |
| 4583 | * namespace, so use "*". |
| 4584 | */ |
| 4585 | generic = 1; |
| 4586 | name = "*"; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4587 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4588 | } |
| 4589 | next = cur->parent; |
| 4590 | |
| 4591 | /* |
| 4592 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4593 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4594 | */ |
| 4595 | tmp = cur->prev; |
| 4596 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4597 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4598 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4599 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4600 | ((tmp->ns == cur->ns) || |
| 4601 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4602 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4603 | occur++; |
| 4604 | tmp = tmp->prev; |
| 4605 | } |
| 4606 | if (occur == 0) { |
| 4607 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4608 | while (tmp != NULL && occur == 0) { |
| 4609 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4610 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4611 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4612 | ((tmp->ns == cur->ns) || |
| 4613 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4614 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4615 | occur++; |
| 4616 | tmp = tmp->next; |
| 4617 | } |
| 4618 | if (occur != 0) |
| 4619 | occur = 1; |
| 4620 | } else |
| 4621 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4622 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4623 | sep = "/"; |
| 4624 | name = "comment()"; |
| 4625 | next = cur->parent; |
| 4626 | |
| 4627 | /* |
| 4628 | * Thumbler index computation |
| 4629 | */ |
| 4630 | tmp = cur->prev; |
| 4631 | while (tmp != NULL) { |
| 4632 | if (tmp->type == XML_COMMENT_NODE) |
| 4633 | occur++; |
| 4634 | tmp = tmp->prev; |
| 4635 | } |
| 4636 | if (occur == 0) { |
| 4637 | tmp = cur->next; |
| 4638 | while (tmp != NULL && occur == 0) { |
| 4639 | if (tmp->type == XML_COMMENT_NODE) |
| 4640 | occur++; |
| 4641 | tmp = tmp->next; |
| 4642 | } |
| 4643 | if (occur != 0) |
| 4644 | occur = 1; |
| 4645 | } else |
| 4646 | occur++; |
| 4647 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4648 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4649 | sep = "/"; |
| 4650 | name = "text()"; |
| 4651 | next = cur->parent; |
| 4652 | |
| 4653 | /* |
| 4654 | * Thumbler index computation |
| 4655 | */ |
| 4656 | tmp = cur->prev; |
| 4657 | while (tmp != NULL) { |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4658 | if ((tmp->type == XML_TEXT_NODE) || |
| 4659 | (tmp->type == XML_CDATA_SECTION_NODE)) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4660 | occur++; |
| 4661 | tmp = tmp->prev; |
| 4662 | } |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4663 | /* |
| 4664 | * Evaluate if this is the only text- or CDATA-section-node; |
| 4665 | * if yes, then we'll get "text()", otherwise "text()[1]". |
| 4666 | */ |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4667 | if (occur == 0) { |
| 4668 | tmp = cur->next; |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4669 | while (tmp != NULL) { |
| 4670 | if ((tmp->type == XML_TEXT_NODE) || |
| 4671 | (tmp->type == XML_CDATA_SECTION_NODE)) |
| 4672 | { |
| 4673 | occur = 1; |
| 4674 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4675 | } |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4676 | tmp = tmp->next; |
| 4677 | } |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4678 | } else |
| 4679 | occur++; |
| 4680 | } else if (cur->type == XML_PI_NODE) { |
| 4681 | sep = "/"; |
| 4682 | snprintf(nametemp, sizeof(nametemp) - 1, |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4683 | "processing-instruction('%s')", (char *)cur->name); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4684 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4685 | name = nametemp; |
| 4686 | |
| 4687 | next = cur->parent; |
| 4688 | |
| 4689 | /* |
| 4690 | * Thumbler index computation |
| 4691 | */ |
| 4692 | tmp = cur->prev; |
| 4693 | while (tmp != NULL) { |
| 4694 | if ((tmp->type == XML_PI_NODE) && |
| 4695 | (xmlStrEqual(cur->name, tmp->name))) |
| 4696 | occur++; |
| 4697 | tmp = tmp->prev; |
| 4698 | } |
| 4699 | if (occur == 0) { |
| 4700 | tmp = cur->next; |
| 4701 | while (tmp != NULL && occur == 0) { |
| 4702 | if ((tmp->type == XML_PI_NODE) && |
| 4703 | (xmlStrEqual(cur->name, tmp->name))) |
| 4704 | occur++; |
| 4705 | tmp = tmp->next; |
| 4706 | } |
| 4707 | if (occur != 0) |
| 4708 | occur = 1; |
| 4709 | } else |
| 4710 | occur++; |
| 4711 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4712 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4713 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4714 | name = (const char *) (((xmlAttrPtr) cur)->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4715 | if (cur->ns) { |
| 4716 | if (cur->ns->prefix != NULL) |
| 4717 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4718 | (char *)cur->ns->prefix, (char *)cur->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4719 | else |
| 4720 | snprintf(nametemp, sizeof(nametemp) - 1, "%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4721 | (char *)cur->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4722 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4723 | name = nametemp; |
| 4724 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4725 | next = ((xmlAttrPtr) cur)->parent; |
| 4726 | } else { |
| 4727 | next = cur->parent; |
| 4728 | } |
| 4729 | |
| 4730 | /* |
| 4731 | * Make sure there is enough room |
| 4732 | */ |
| 4733 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4734 | buf_len = |
| 4735 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4736 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4737 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4738 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4739 | xmlFree(buf); |
| 4740 | xmlFree(buffer); |
| 4741 | return (NULL); |
| 4742 | } |
| 4743 | buffer = temp; |
| 4744 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4745 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4746 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4747 | xmlFree(buf); |
| 4748 | xmlFree(buffer); |
| 4749 | return (NULL); |
| 4750 | } |
| 4751 | buf = temp; |
| 4752 | } |
| 4753 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4754 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4755 | sep, name, (char *) buffer); |
| 4756 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4757 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4758 | sep, name, occur, (char *) buffer); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4759 | snprintf((char *) buffer, buf_len, "%s", (char *)buf); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4760 | cur = next; |
| 4761 | } while (cur != NULL); |
| 4762 | xmlFree(buf); |
| 4763 | return (buffer); |
| 4764 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4765 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4766 | |
| 4767 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4768 | * xmlDocGetRootElement: |
| 4769 | * @doc: the document |
| 4770 | * |
| 4771 | * Get the root element of the document (doc->children is a list |
| 4772 | * containing possibly comments, PIs, etc ...). |
| 4773 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4774 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4775 | */ |
| 4776 | xmlNodePtr |
| 4777 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 4778 | xmlNodePtr ret; |
| 4779 | |
| 4780 | if (doc == NULL) return(NULL); |
| 4781 | ret = doc->children; |
| 4782 | while (ret != NULL) { |
| 4783 | if (ret->type == XML_ELEMENT_NODE) |
| 4784 | return(ret); |
| 4785 | ret = ret->next; |
| 4786 | } |
| 4787 | return(ret); |
| 4788 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4789 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4790 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4791 | /** |
| 4792 | * xmlDocSetRootElement: |
| 4793 | * @doc: the document |
Daniel Veillard | 26a45c8 | 2006-10-20 12:55:34 +0000 | [diff] [blame] | 4794 | * @root: the new document root element, if root is NULL no action is taken, |
| 4795 | * to remove a node from a document use xmlUnlinkNode(root) instead. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4796 | * |
| 4797 | * Set the root element of the document (doc->children is a list |
| 4798 | * containing possibly comments, PIs, etc ...). |
| 4799 | * |
Daniel Veillard | 26a45c8 | 2006-10-20 12:55:34 +0000 | [diff] [blame] | 4800 | * Returns the old root element if any was found, NULL if root was NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4801 | */ |
| 4802 | xmlNodePtr |
| 4803 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4804 | xmlNodePtr old = NULL; |
| 4805 | |
| 4806 | if (doc == NULL) return(NULL); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4807 | if (root == NULL) |
| 4808 | return(NULL); |
| 4809 | xmlUnlinkNode(root); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 4810 | xmlSetTreeDoc(root, doc); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4811 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4812 | old = doc->children; |
| 4813 | while (old != NULL) { |
| 4814 | if (old->type == XML_ELEMENT_NODE) |
| 4815 | break; |
| 4816 | old = old->next; |
| 4817 | } |
| 4818 | if (old == NULL) { |
| 4819 | if (doc->children == NULL) { |
| 4820 | doc->children = root; |
| 4821 | doc->last = root; |
| 4822 | } else { |
| 4823 | xmlAddSibling(doc->children, root); |
| 4824 | } |
| 4825 | } else { |
| 4826 | xmlReplaceNode(old, root); |
| 4827 | } |
| 4828 | return(old); |
| 4829 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4830 | #endif |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4831 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4832 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4833 | /** |
| 4834 | * xmlNodeSetLang: |
| 4835 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4836 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4837 | * |
| 4838 | * Set the language of a node, i.e. the values of the xml:lang |
| 4839 | * attribute. |
| 4840 | */ |
| 4841 | void |
| 4842 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4843 | xmlNsPtr ns; |
| 4844 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4845 | if (cur == NULL) return; |
| 4846 | switch(cur->type) { |
| 4847 | case XML_TEXT_NODE: |
| 4848 | case XML_CDATA_SECTION_NODE: |
| 4849 | case XML_COMMENT_NODE: |
| 4850 | case XML_DOCUMENT_NODE: |
| 4851 | case XML_DOCUMENT_TYPE_NODE: |
| 4852 | case XML_DOCUMENT_FRAG_NODE: |
| 4853 | case XML_NOTATION_NODE: |
| 4854 | case XML_HTML_DOCUMENT_NODE: |
| 4855 | case XML_DTD_NODE: |
| 4856 | case XML_ELEMENT_DECL: |
| 4857 | case XML_ATTRIBUTE_DECL: |
| 4858 | case XML_ENTITY_DECL: |
| 4859 | case XML_PI_NODE: |
| 4860 | case XML_ENTITY_REF_NODE: |
| 4861 | case XML_ENTITY_NODE: |
| 4862 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4863 | #ifdef LIBXML_DOCB_ENABLED |
| 4864 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4865 | #endif |
| 4866 | case XML_XINCLUDE_START: |
| 4867 | case XML_XINCLUDE_END: |
| 4868 | return; |
| 4869 | case XML_ELEMENT_NODE: |
| 4870 | case XML_ATTRIBUTE_NODE: |
| 4871 | break; |
| 4872 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4873 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4874 | if (ns == NULL) |
| 4875 | return; |
| 4876 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4877 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4878 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4879 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4880 | /** |
| 4881 | * xmlNodeGetLang: |
| 4882 | * @cur: the node being checked |
| 4883 | * |
| 4884 | * Searches the language of a node, i.e. the values of the xml:lang |
| 4885 | * attribute or the one carried by the nearest ancestor. |
| 4886 | * |
| 4887 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4888 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4889 | */ |
| 4890 | xmlChar * |
| 4891 | xmlNodeGetLang(xmlNodePtr cur) { |
| 4892 | xmlChar *lang; |
| 4893 | |
| 4894 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 4895 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4896 | if (lang != NULL) |
| 4897 | return(lang); |
| 4898 | cur = cur->parent; |
| 4899 | } |
| 4900 | return(NULL); |
| 4901 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4902 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4903 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4904 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4905 | /** |
| 4906 | * xmlNodeSetSpacePreserve: |
| 4907 | * @cur: the node being changed |
| 4908 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 4909 | * |
| 4910 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 4911 | * value of the xml:space attribute. |
| 4912 | */ |
| 4913 | void |
| 4914 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4915 | xmlNsPtr ns; |
| 4916 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4917 | if (cur == NULL) return; |
| 4918 | switch(cur->type) { |
| 4919 | case XML_TEXT_NODE: |
| 4920 | case XML_CDATA_SECTION_NODE: |
| 4921 | case XML_COMMENT_NODE: |
| 4922 | case XML_DOCUMENT_NODE: |
| 4923 | case XML_DOCUMENT_TYPE_NODE: |
| 4924 | case XML_DOCUMENT_FRAG_NODE: |
| 4925 | case XML_NOTATION_NODE: |
| 4926 | case XML_HTML_DOCUMENT_NODE: |
| 4927 | case XML_DTD_NODE: |
| 4928 | case XML_ELEMENT_DECL: |
| 4929 | case XML_ATTRIBUTE_DECL: |
| 4930 | case XML_ENTITY_DECL: |
| 4931 | case XML_PI_NODE: |
| 4932 | case XML_ENTITY_REF_NODE: |
| 4933 | case XML_ENTITY_NODE: |
| 4934 | case XML_NAMESPACE_DECL: |
| 4935 | case XML_XINCLUDE_START: |
| 4936 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4937 | #ifdef LIBXML_DOCB_ENABLED |
| 4938 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4939 | #endif |
| 4940 | return; |
| 4941 | case XML_ELEMENT_NODE: |
| 4942 | case XML_ATTRIBUTE_NODE: |
| 4943 | break; |
| 4944 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4945 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4946 | if (ns == NULL) |
| 4947 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4948 | switch (val) { |
| 4949 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4950 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4951 | break; |
| 4952 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4953 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4954 | break; |
| 4955 | } |
| 4956 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4957 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4958 | |
| 4959 | /** |
| 4960 | * xmlNodeGetSpacePreserve: |
| 4961 | * @cur: the node being checked |
| 4962 | * |
| 4963 | * Searches the space preserving behaviour of a node, i.e. the values |
| 4964 | * of the xml:space attribute or the one carried by the nearest |
| 4965 | * ancestor. |
| 4966 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4967 | * 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] | 4968 | */ |
| 4969 | int |
| 4970 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 4971 | xmlChar *space; |
| 4972 | |
| 4973 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4974 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4975 | if (space != NULL) { |
| 4976 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 4977 | xmlFree(space); |
| 4978 | return(1); |
| 4979 | } |
| 4980 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 4981 | xmlFree(space); |
| 4982 | return(0); |
| 4983 | } |
| 4984 | xmlFree(space); |
| 4985 | } |
| 4986 | cur = cur->parent; |
| 4987 | } |
| 4988 | return(-1); |
| 4989 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4990 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4991 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4992 | /** |
| 4993 | * xmlNodeSetName: |
| 4994 | * @cur: the node being changed |
| 4995 | * @name: the new tag name |
| 4996 | * |
| 4997 | * Set (or reset) the name of a node. |
| 4998 | */ |
| 4999 | void |
| 5000 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5001 | xmlDocPtr doc; |
| 5002 | xmlDictPtr dict; |
| 5003 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5004 | if (cur == NULL) return; |
| 5005 | if (name == NULL) return; |
| 5006 | switch(cur->type) { |
| 5007 | case XML_TEXT_NODE: |
| 5008 | case XML_CDATA_SECTION_NODE: |
| 5009 | case XML_COMMENT_NODE: |
| 5010 | case XML_DOCUMENT_TYPE_NODE: |
| 5011 | case XML_DOCUMENT_FRAG_NODE: |
| 5012 | case XML_NOTATION_NODE: |
| 5013 | case XML_HTML_DOCUMENT_NODE: |
| 5014 | case XML_NAMESPACE_DECL: |
| 5015 | case XML_XINCLUDE_START: |
| 5016 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5017 | #ifdef LIBXML_DOCB_ENABLED |
| 5018 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5019 | #endif |
| 5020 | return; |
| 5021 | case XML_ELEMENT_NODE: |
| 5022 | case XML_ATTRIBUTE_NODE: |
| 5023 | case XML_PI_NODE: |
| 5024 | case XML_ENTITY_REF_NODE: |
| 5025 | case XML_ENTITY_NODE: |
| 5026 | case XML_DTD_NODE: |
| 5027 | case XML_DOCUMENT_NODE: |
| 5028 | case XML_ELEMENT_DECL: |
| 5029 | case XML_ATTRIBUTE_DECL: |
| 5030 | case XML_ENTITY_DECL: |
| 5031 | break; |
| 5032 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5033 | doc = cur->doc; |
| 5034 | if (doc != NULL) |
| 5035 | dict = doc->dict; |
| 5036 | else |
| 5037 | dict = NULL; |
| 5038 | if (dict != NULL) { |
| 5039 | if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name))) |
| 5040 | xmlFree((xmlChar *) cur->name); |
| 5041 | cur->name = xmlDictLookup(dict, name, -1); |
| 5042 | } else { |
| 5043 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 5044 | cur->name = xmlStrdup(name); |
| 5045 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5046 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 5047 | #endif |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5048 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 5049 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5050 | /** |
| 5051 | * xmlNodeSetBase: |
| 5052 | * @cur: the node being changed |
| 5053 | * @uri: the new base URI |
| 5054 | * |
| 5055 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 5056 | * xml:base attribute. |
| 5057 | */ |
| 5058 | void |
Daniel Veillard | f85ce8e | 2003-09-22 10:24:45 +0000 | [diff] [blame] | 5059 | xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5060 | xmlNsPtr ns; |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5061 | const xmlChar* fixed; |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5062 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5063 | if (cur == NULL) return; |
| 5064 | switch(cur->type) { |
| 5065 | case XML_TEXT_NODE: |
| 5066 | case XML_CDATA_SECTION_NODE: |
| 5067 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5068 | case XML_DOCUMENT_TYPE_NODE: |
| 5069 | case XML_DOCUMENT_FRAG_NODE: |
| 5070 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5071 | case XML_DTD_NODE: |
| 5072 | case XML_ELEMENT_DECL: |
| 5073 | case XML_ATTRIBUTE_DECL: |
| 5074 | case XML_ENTITY_DECL: |
| 5075 | case XML_PI_NODE: |
| 5076 | case XML_ENTITY_REF_NODE: |
| 5077 | case XML_ENTITY_NODE: |
| 5078 | case XML_NAMESPACE_DECL: |
| 5079 | case XML_XINCLUDE_START: |
| 5080 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5081 | return; |
| 5082 | case XML_ELEMENT_NODE: |
| 5083 | case XML_ATTRIBUTE_NODE: |
| 5084 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 5085 | case XML_DOCUMENT_NODE: |
| 5086 | #ifdef LIBXML_DOCB_ENABLED |
| 5087 | case XML_DOCB_DOCUMENT_NODE: |
| 5088 | #endif |
| 5089 | case XML_HTML_DOCUMENT_NODE: { |
| 5090 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 5091 | |
| 5092 | if (doc->URL != NULL) |
| 5093 | xmlFree((xmlChar *) doc->URL); |
| 5094 | if (uri == NULL) |
| 5095 | doc->URL = NULL; |
| 5096 | else |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5097 | doc->URL = xmlPathToURI(uri); |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 5098 | return; |
| 5099 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5100 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5101 | |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5102 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 5103 | if (ns == NULL) |
| 5104 | return; |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5105 | fixed = xmlPathToURI(uri); |
| 5106 | if (fixed != NULL) { |
| 5107 | xmlSetNsProp(cur, ns, BAD_CAST "base", fixed); |
William M. Brack | 38d452a | 2007-05-22 16:00:06 +0000 | [diff] [blame] | 5108 | xmlFree((xmlChar *)fixed); |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5109 | } else { |
| 5110 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
| 5111 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5112 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5113 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5114 | |
| 5115 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5116 | * xmlNodeGetBase: |
| 5117 | * @doc: the document the node pertains to |
| 5118 | * @cur: the node being checked |
| 5119 | * |
| 5120 | * Searches for the BASE URL. The code should work on both XML |
| 5121 | * and HTML document even if base mechanisms are completely different. |
| 5122 | * It returns the base as defined in RFC 2396 sections |
| 5123 | * 5.1.1. Base URI within Document Content |
| 5124 | * and |
| 5125 | * 5.1.2. Base URI from the Encapsulating Entity |
| 5126 | * However it does not return the document base (5.1.3), use |
| 5127 | * xmlDocumentGetBase() for this |
| 5128 | * |
| 5129 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5130 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5131 | */ |
| 5132 | xmlChar * |
| 5133 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5134 | xmlChar *oldbase = NULL; |
| 5135 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5136 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5137 | if ((cur == NULL) && (doc == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5138 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5139 | if (doc == NULL) doc = cur->doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5140 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 5141 | cur = doc->children; |
| 5142 | while ((cur != NULL) && (cur->name != NULL)) { |
| 5143 | if (cur->type != XML_ELEMENT_NODE) { |
| 5144 | cur = cur->next; |
| 5145 | continue; |
| 5146 | } |
| 5147 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 5148 | cur = cur->children; |
| 5149 | continue; |
| 5150 | } |
| 5151 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 5152 | cur = cur->children; |
| 5153 | continue; |
| 5154 | } |
| 5155 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 5156 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 5157 | } |
| 5158 | cur = cur->next; |
| 5159 | } |
| 5160 | return(NULL); |
| 5161 | } |
| 5162 | while (cur != NULL) { |
| 5163 | if (cur->type == XML_ENTITY_DECL) { |
| 5164 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 5165 | return(xmlStrdup(ent->URI)); |
| 5166 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5167 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5168 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5169 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5170 | if (oldbase != NULL) { |
| 5171 | newbase = xmlBuildURI(oldbase, base); |
| 5172 | if (newbase != NULL) { |
| 5173 | xmlFree(oldbase); |
| 5174 | xmlFree(base); |
| 5175 | oldbase = newbase; |
| 5176 | } else { |
| 5177 | xmlFree(oldbase); |
| 5178 | xmlFree(base); |
| 5179 | return(NULL); |
| 5180 | } |
| 5181 | } else { |
| 5182 | oldbase = base; |
| 5183 | } |
| 5184 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 5185 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 5186 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 5187 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5188 | } |
| 5189 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5190 | cur = cur->parent; |
| 5191 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5192 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 5193 | if (oldbase == NULL) |
| 5194 | return(xmlStrdup(doc->URL)); |
| 5195 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 5196 | xmlFree(oldbase); |
| 5197 | return(newbase); |
| 5198 | } |
| 5199 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5200 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5201 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5202 | /** |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5203 | * xmlNodeBufGetContent: |
| 5204 | * @buffer: a buffer |
| 5205 | * @cur: the node being read |
| 5206 | * |
| 5207 | * Read the value of a node @cur, this can be either the text carried |
| 5208 | * directly by this node if it's a TEXT node or the aggregate string |
| 5209 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 5210 | * Entity references are substituted. |
| 5211 | * Fills up the buffer @buffer with this value |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5212 | * |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5213 | * Returns 0 in case of success and -1 in case of error. |
| 5214 | */ |
| 5215 | int |
| 5216 | xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur) |
| 5217 | { |
| 5218 | if ((cur == NULL) || (buffer == NULL)) return(-1); |
| 5219 | switch (cur->type) { |
| 5220 | case XML_CDATA_SECTION_NODE: |
| 5221 | case XML_TEXT_NODE: |
| 5222 | xmlBufferCat(buffer, cur->content); |
| 5223 | break; |
| 5224 | case XML_DOCUMENT_FRAG_NODE: |
| 5225 | case XML_ELEMENT_NODE:{ |
| 5226 | xmlNodePtr tmp = cur; |
| 5227 | |
| 5228 | while (tmp != NULL) { |
| 5229 | switch (tmp->type) { |
| 5230 | case XML_CDATA_SECTION_NODE: |
| 5231 | case XML_TEXT_NODE: |
| 5232 | if (tmp->content != NULL) |
| 5233 | xmlBufferCat(buffer, tmp->content); |
| 5234 | break; |
| 5235 | case XML_ENTITY_REF_NODE: |
Rob Richards | 77b92ff | 2005-12-20 15:55:14 +0000 | [diff] [blame] | 5236 | xmlNodeBufGetContent(buffer, tmp); |
| 5237 | break; |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5238 | default: |
| 5239 | break; |
| 5240 | } |
| 5241 | /* |
| 5242 | * Skip to next node |
| 5243 | */ |
| 5244 | if (tmp->children != NULL) { |
| 5245 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 5246 | tmp = tmp->children; |
| 5247 | continue; |
| 5248 | } |
| 5249 | } |
| 5250 | if (tmp == cur) |
| 5251 | break; |
| 5252 | |
| 5253 | if (tmp->next != NULL) { |
| 5254 | tmp = tmp->next; |
| 5255 | continue; |
| 5256 | } |
| 5257 | |
| 5258 | do { |
| 5259 | tmp = tmp->parent; |
| 5260 | if (tmp == NULL) |
| 5261 | break; |
| 5262 | if (tmp == cur) { |
| 5263 | tmp = NULL; |
| 5264 | break; |
| 5265 | } |
| 5266 | if (tmp->next != NULL) { |
| 5267 | tmp = tmp->next; |
| 5268 | break; |
| 5269 | } |
| 5270 | } while (tmp != NULL); |
| 5271 | } |
| 5272 | break; |
| 5273 | } |
| 5274 | case XML_ATTRIBUTE_NODE:{ |
| 5275 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 5276 | xmlNodePtr tmp = attr->children; |
| 5277 | |
| 5278 | while (tmp != NULL) { |
| 5279 | if (tmp->type == XML_TEXT_NODE) |
| 5280 | xmlBufferCat(buffer, tmp->content); |
| 5281 | else |
| 5282 | xmlNodeBufGetContent(buffer, tmp); |
| 5283 | tmp = tmp->next; |
| 5284 | } |
| 5285 | break; |
| 5286 | } |
| 5287 | case XML_COMMENT_NODE: |
| 5288 | case XML_PI_NODE: |
| 5289 | xmlBufferCat(buffer, cur->content); |
| 5290 | break; |
| 5291 | case XML_ENTITY_REF_NODE:{ |
| 5292 | xmlEntityPtr ent; |
| 5293 | xmlNodePtr tmp; |
| 5294 | |
| 5295 | /* lookup entity declaration */ |
| 5296 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5297 | if (ent == NULL) |
| 5298 | return(-1); |
| 5299 | |
| 5300 | /* an entity content can be any "well balanced chunk", |
| 5301 | * i.e. the result of the content [43] production: |
| 5302 | * http://www.w3.org/TR/REC-xml#NT-content |
| 5303 | * -> we iterate through child nodes and recursive call |
| 5304 | * xmlNodeGetContent() which handles all possible node types */ |
| 5305 | tmp = ent->children; |
| 5306 | while (tmp) { |
| 5307 | xmlNodeBufGetContent(buffer, tmp); |
| 5308 | tmp = tmp->next; |
| 5309 | } |
| 5310 | break; |
| 5311 | } |
| 5312 | case XML_ENTITY_NODE: |
| 5313 | case XML_DOCUMENT_TYPE_NODE: |
| 5314 | case XML_NOTATION_NODE: |
| 5315 | case XML_DTD_NODE: |
| 5316 | case XML_XINCLUDE_START: |
| 5317 | case XML_XINCLUDE_END: |
| 5318 | break; |
| 5319 | case XML_DOCUMENT_NODE: |
| 5320 | #ifdef LIBXML_DOCB_ENABLED |
| 5321 | case XML_DOCB_DOCUMENT_NODE: |
| 5322 | #endif |
| 5323 | case XML_HTML_DOCUMENT_NODE: |
| 5324 | cur = cur->children; |
| 5325 | while (cur!= NULL) { |
| 5326 | if ((cur->type == XML_ELEMENT_NODE) || |
| 5327 | (cur->type == XML_TEXT_NODE) || |
| 5328 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 5329 | xmlNodeBufGetContent(buffer, cur); |
| 5330 | } |
| 5331 | cur = cur->next; |
| 5332 | } |
| 5333 | break; |
| 5334 | case XML_NAMESPACE_DECL: |
| 5335 | xmlBufferCat(buffer, ((xmlNsPtr) cur)->href); |
| 5336 | break; |
| 5337 | case XML_ELEMENT_DECL: |
| 5338 | case XML_ATTRIBUTE_DECL: |
| 5339 | case XML_ENTITY_DECL: |
| 5340 | break; |
| 5341 | } |
| 5342 | return(0); |
| 5343 | } |
| 5344 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5345 | * xmlNodeGetContent: |
| 5346 | * @cur: the node being read |
| 5347 | * |
| 5348 | * Read the value of a node, this can be either the text carried |
| 5349 | * directly by this node if it's a TEXT node or the aggregate string |
| 5350 | * 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] | 5351 | * Entity references are substituted. |
| 5352 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5353 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5354 | */ |
| 5355 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5356 | xmlNodeGetContent(xmlNodePtr cur) |
| 5357 | { |
| 5358 | if (cur == NULL) |
| 5359 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5360 | switch (cur->type) { |
| 5361 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5362 | case XML_ELEMENT_NODE:{ |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5363 | xmlBufferPtr buffer; |
| 5364 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5365 | |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 5366 | buffer = xmlBufferCreateSize(64); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5367 | if (buffer == NULL) |
| 5368 | return (NULL); |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5369 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5370 | ret = buffer->content; |
| 5371 | buffer->content = NULL; |
| 5372 | xmlBufferFree(buffer); |
| 5373 | return (ret); |
| 5374 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5375 | case XML_ATTRIBUTE_NODE: |
| 5376 | return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5377 | case XML_COMMENT_NODE: |
| 5378 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5379 | if (cur->content != NULL) |
| 5380 | return (xmlStrdup(cur->content)); |
| 5381 | return (NULL); |
| 5382 | case XML_ENTITY_REF_NODE:{ |
| 5383 | xmlEntityPtr ent; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5384 | xmlBufferPtr buffer; |
| 5385 | xmlChar *ret; |
| 5386 | |
| 5387 | /* lookup entity declaration */ |
| 5388 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5389 | if (ent == NULL) |
| 5390 | return (NULL); |
| 5391 | |
| 5392 | buffer = xmlBufferCreate(); |
| 5393 | if (buffer == NULL) |
| 5394 | return (NULL); |
| 5395 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5396 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5397 | |
| 5398 | ret = buffer->content; |
| 5399 | buffer->content = NULL; |
| 5400 | xmlBufferFree(buffer); |
| 5401 | return (ret); |
| 5402 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5403 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5404 | case XML_DOCUMENT_TYPE_NODE: |
| 5405 | case XML_NOTATION_NODE: |
| 5406 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5407 | case XML_XINCLUDE_START: |
| 5408 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5409 | return (NULL); |
| 5410 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5411 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5412 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5413 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5414 | case XML_HTML_DOCUMENT_NODE: { |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5415 | xmlBufferPtr buffer; |
| 5416 | xmlChar *ret; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5417 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5418 | buffer = xmlBufferCreate(); |
| 5419 | if (buffer == NULL) |
| 5420 | return (NULL); |
| 5421 | |
| 5422 | xmlNodeBufGetContent(buffer, (xmlNodePtr) cur); |
| 5423 | |
| 5424 | ret = buffer->content; |
| 5425 | buffer->content = NULL; |
| 5426 | xmlBufferFree(buffer); |
| 5427 | return (ret); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5428 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 5429 | case XML_NAMESPACE_DECL: { |
| 5430 | xmlChar *tmp; |
| 5431 | |
| 5432 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 5433 | return (tmp); |
| 5434 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5435 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5436 | /* TODO !!! */ |
| 5437 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5438 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5439 | /* TODO !!! */ |
| 5440 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5441 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5442 | /* TODO !!! */ |
| 5443 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5444 | case XML_CDATA_SECTION_NODE: |
| 5445 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5446 | if (cur->content != NULL) |
| 5447 | return (xmlStrdup(cur->content)); |
| 5448 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5449 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5450 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5451 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5452 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5453 | /** |
| 5454 | * xmlNodeSetContent: |
| 5455 | * @cur: the node being modified |
| 5456 | * @content: the new value of the content |
| 5457 | * |
| 5458 | * Replace the content of a node. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5459 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 5460 | * references, but XML special chars need to be escaped first by using |
| 5461 | * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5462 | */ |
| 5463 | void |
| 5464 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 5465 | if (cur == NULL) { |
| 5466 | #ifdef DEBUG_TREE |
| 5467 | xmlGenericError(xmlGenericErrorContext, |
| 5468 | "xmlNodeSetContent : node == NULL\n"); |
| 5469 | #endif |
| 5470 | return; |
| 5471 | } |
| 5472 | switch (cur->type) { |
| 5473 | case XML_DOCUMENT_FRAG_NODE: |
| 5474 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5475 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5476 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5477 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 5478 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5479 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5480 | case XML_TEXT_NODE: |
| 5481 | case XML_CDATA_SECTION_NODE: |
| 5482 | case XML_ENTITY_REF_NODE: |
| 5483 | case XML_ENTITY_NODE: |
| 5484 | case XML_PI_NODE: |
| 5485 | case XML_COMMENT_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5486 | if ((cur->content != NULL) && |
| 5487 | (cur->content != (xmlChar *) &(cur->properties))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5488 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
Daniel Veillard | c587bce | 2005-05-10 15:28:08 +0000 | [diff] [blame] | 5489 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5490 | xmlFree(cur->content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5491 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5492 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5493 | cur->last = cur->children = NULL; |
| 5494 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5495 | cur->content = xmlStrdup(content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5496 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5497 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5498 | cur->properties = NULL; |
| 5499 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5500 | break; |
| 5501 | case XML_DOCUMENT_NODE: |
| 5502 | case XML_HTML_DOCUMENT_NODE: |
| 5503 | case XML_DOCUMENT_TYPE_NODE: |
| 5504 | case XML_XINCLUDE_START: |
| 5505 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5506 | #ifdef LIBXML_DOCB_ENABLED |
| 5507 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5508 | #endif |
| 5509 | break; |
| 5510 | case XML_NOTATION_NODE: |
| 5511 | break; |
| 5512 | case XML_DTD_NODE: |
| 5513 | break; |
| 5514 | case XML_NAMESPACE_DECL: |
| 5515 | break; |
| 5516 | case XML_ELEMENT_DECL: |
| 5517 | /* TODO !!! */ |
| 5518 | break; |
| 5519 | case XML_ATTRIBUTE_DECL: |
| 5520 | /* TODO !!! */ |
| 5521 | break; |
| 5522 | case XML_ENTITY_DECL: |
| 5523 | /* TODO !!! */ |
| 5524 | break; |
| 5525 | } |
| 5526 | } |
| 5527 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5528 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5529 | /** |
| 5530 | * xmlNodeSetContentLen: |
| 5531 | * @cur: the node being modified |
| 5532 | * @content: the new value of the content |
| 5533 | * @len: the size of @content |
| 5534 | * |
| 5535 | * Replace the content of a node. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5536 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 5537 | * references, but XML special chars need to be escaped first by using |
| 5538 | * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5539 | */ |
| 5540 | void |
| 5541 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5542 | if (cur == NULL) { |
| 5543 | #ifdef DEBUG_TREE |
| 5544 | xmlGenericError(xmlGenericErrorContext, |
| 5545 | "xmlNodeSetContentLen : node == NULL\n"); |
| 5546 | #endif |
| 5547 | return; |
| 5548 | } |
| 5549 | switch (cur->type) { |
| 5550 | case XML_DOCUMENT_FRAG_NODE: |
| 5551 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5552 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5553 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5554 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 5555 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5556 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5557 | case XML_TEXT_NODE: |
| 5558 | case XML_CDATA_SECTION_NODE: |
| 5559 | case XML_ENTITY_REF_NODE: |
| 5560 | case XML_ENTITY_NODE: |
| 5561 | case XML_PI_NODE: |
| 5562 | case XML_COMMENT_NODE: |
| 5563 | case XML_NOTATION_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5564 | if ((cur->content != NULL) && |
| 5565 | (cur->content != (xmlChar *) &(cur->properties))) { |
| 5566 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5567 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
| 5568 | xmlFree(cur->content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5569 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5570 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5571 | cur->children = cur->last = NULL; |
| 5572 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5573 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5574 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5575 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5576 | cur->properties = NULL; |
| 5577 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5578 | break; |
| 5579 | case XML_DOCUMENT_NODE: |
| 5580 | case XML_DTD_NODE: |
| 5581 | case XML_HTML_DOCUMENT_NODE: |
| 5582 | case XML_DOCUMENT_TYPE_NODE: |
| 5583 | case XML_NAMESPACE_DECL: |
| 5584 | case XML_XINCLUDE_START: |
| 5585 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5586 | #ifdef LIBXML_DOCB_ENABLED |
| 5587 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5588 | #endif |
| 5589 | break; |
| 5590 | case XML_ELEMENT_DECL: |
| 5591 | /* TODO !!! */ |
| 5592 | break; |
| 5593 | case XML_ATTRIBUTE_DECL: |
| 5594 | /* TODO !!! */ |
| 5595 | break; |
| 5596 | case XML_ENTITY_DECL: |
| 5597 | /* TODO !!! */ |
| 5598 | break; |
| 5599 | } |
| 5600 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5601 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5602 | |
| 5603 | /** |
| 5604 | * xmlNodeAddContentLen: |
| 5605 | * @cur: the node being modified |
| 5606 | * @content: extra content |
| 5607 | * @len: the size of @content |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5608 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5609 | * Append the extra substring to the node content. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5610 | * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be |
| 5611 | * raw text, so unescaped XML special chars are allowed, entity |
| 5612 | * references are not supported. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5613 | */ |
| 5614 | void |
| 5615 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5616 | if (cur == NULL) { |
| 5617 | #ifdef DEBUG_TREE |
| 5618 | xmlGenericError(xmlGenericErrorContext, |
| 5619 | "xmlNodeAddContentLen : node == NULL\n"); |
| 5620 | #endif |
| 5621 | return; |
| 5622 | } |
| 5623 | if (len <= 0) return; |
| 5624 | switch (cur->type) { |
| 5625 | case XML_DOCUMENT_FRAG_NODE: |
| 5626 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5627 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5628 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 5629 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5630 | newNode = xmlNewTextLen(content, len); |
| 5631 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5632 | tmp = xmlAddChild(cur, newNode); |
| 5633 | if (tmp != newNode) |
| 5634 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5635 | if ((last != NULL) && (last->next == newNode)) { |
| 5636 | xmlTextMerge(last, newNode); |
| 5637 | } |
| 5638 | } |
| 5639 | break; |
| 5640 | } |
| 5641 | case XML_ATTRIBUTE_NODE: |
| 5642 | break; |
| 5643 | case XML_TEXT_NODE: |
| 5644 | case XML_CDATA_SECTION_NODE: |
| 5645 | case XML_ENTITY_REF_NODE: |
| 5646 | case XML_ENTITY_NODE: |
| 5647 | case XML_PI_NODE: |
| 5648 | case XML_COMMENT_NODE: |
| 5649 | case XML_NOTATION_NODE: |
| 5650 | if (content != NULL) { |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5651 | if ((cur->content == (xmlChar *) &(cur->properties)) || |
| 5652 | ((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5653 | xmlDictOwns(cur->doc->dict, cur->content))) { |
| 5654 | cur->content = xmlStrncatNew(cur->content, content, len); |
| 5655 | cur->properties = NULL; |
| 5656 | cur->nsDef = NULL; |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5657 | break; |
| 5658 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5659 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5660 | } |
| 5661 | case XML_DOCUMENT_NODE: |
| 5662 | case XML_DTD_NODE: |
| 5663 | case XML_HTML_DOCUMENT_NODE: |
| 5664 | case XML_DOCUMENT_TYPE_NODE: |
| 5665 | case XML_NAMESPACE_DECL: |
| 5666 | case XML_XINCLUDE_START: |
| 5667 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5668 | #ifdef LIBXML_DOCB_ENABLED |
| 5669 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5670 | #endif |
| 5671 | break; |
| 5672 | case XML_ELEMENT_DECL: |
| 5673 | case XML_ATTRIBUTE_DECL: |
| 5674 | case XML_ENTITY_DECL: |
| 5675 | break; |
| 5676 | } |
| 5677 | } |
| 5678 | |
| 5679 | /** |
| 5680 | * xmlNodeAddContent: |
| 5681 | * @cur: the node being modified |
| 5682 | * @content: extra content |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5683 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5684 | * Append the extra substring to the node content. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5685 | * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be |
| 5686 | * raw text, so unescaped XML special chars are allowed, entity |
| 5687 | * references are not supported. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5688 | */ |
| 5689 | void |
| 5690 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5691 | int len; |
| 5692 | |
| 5693 | if (cur == NULL) { |
| 5694 | #ifdef DEBUG_TREE |
| 5695 | xmlGenericError(xmlGenericErrorContext, |
| 5696 | "xmlNodeAddContent : node == NULL\n"); |
| 5697 | #endif |
| 5698 | return; |
| 5699 | } |
| 5700 | if (content == NULL) return; |
| 5701 | len = xmlStrlen(content); |
| 5702 | xmlNodeAddContentLen(cur, content, len); |
| 5703 | } |
| 5704 | |
| 5705 | /** |
| 5706 | * xmlTextMerge: |
| 5707 | * @first: the first text node |
| 5708 | * @second: the second text node being merged |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5709 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5710 | * Merge two text nodes into one |
| 5711 | * Returns the first text node augmented |
| 5712 | */ |
| 5713 | xmlNodePtr |
| 5714 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5715 | if (first == NULL) return(second); |
| 5716 | if (second == NULL) return(first); |
| 5717 | if (first->type != XML_TEXT_NODE) return(first); |
| 5718 | if (second->type != XML_TEXT_NODE) return(first); |
| 5719 | if (second->name != first->name) |
| 5720 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5721 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5722 | xmlUnlinkNode(second); |
| 5723 | xmlFreeNode(second); |
| 5724 | return(first); |
| 5725 | } |
| 5726 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 5727 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5728 | /** |
| 5729 | * xmlGetNsList: |
| 5730 | * @doc: the document |
| 5731 | * @node: the current node |
| 5732 | * |
| 5733 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5734 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5735 | * that need to be freed by the caller or NULL if no |
| 5736 | * namespace if defined |
| 5737 | */ |
| 5738 | xmlNsPtr * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5739 | xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) |
| 5740 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5741 | xmlNsPtr cur; |
| 5742 | xmlNsPtr *ret = NULL; |
| 5743 | int nbns = 0; |
| 5744 | int maxns = 10; |
| 5745 | int i; |
| 5746 | |
| 5747 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5748 | if (node->type == XML_ELEMENT_NODE) { |
| 5749 | cur = node->nsDef; |
| 5750 | while (cur != NULL) { |
| 5751 | if (ret == NULL) { |
| 5752 | ret = |
| 5753 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5754 | sizeof(xmlNsPtr)); |
| 5755 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5756 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5757 | return (NULL); |
| 5758 | } |
| 5759 | ret[nbns] = NULL; |
| 5760 | } |
| 5761 | for (i = 0; i < nbns; i++) { |
| 5762 | if ((cur->prefix == ret[i]->prefix) || |
| 5763 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5764 | break; |
| 5765 | } |
| 5766 | if (i >= nbns) { |
| 5767 | if (nbns >= maxns) { |
| 5768 | maxns *= 2; |
| 5769 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5770 | (maxns + |
| 5771 | 1) * |
| 5772 | sizeof(xmlNsPtr)); |
| 5773 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5774 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5775 | return (NULL); |
| 5776 | } |
| 5777 | } |
| 5778 | ret[nbns++] = cur; |
| 5779 | ret[nbns] = NULL; |
| 5780 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5781 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5782 | cur = cur->next; |
| 5783 | } |
| 5784 | } |
| 5785 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5786 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5787 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5788 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5789 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5790 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5791 | /* |
| 5792 | * xmlTreeEnsureXMLDecl: |
| 5793 | * @doc: the doc |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5794 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5795 | * Ensures that there is an XML namespace declaration on the doc. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5796 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5797 | * Returns the XML ns-struct or NULL on API and internal errors. |
| 5798 | */ |
| 5799 | static xmlNsPtr |
| 5800 | xmlTreeEnsureXMLDecl(xmlDocPtr doc) |
| 5801 | { |
| 5802 | if (doc == NULL) |
| 5803 | return (NULL); |
| 5804 | if (doc->oldNs != NULL) |
| 5805 | return (doc->oldNs); |
| 5806 | { |
| 5807 | xmlNsPtr ns; |
| 5808 | ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5809 | if (ns == NULL) { |
| 5810 | xmlTreeErrMemory( |
| 5811 | "allocating the XML namespace"); |
| 5812 | return (NULL); |
| 5813 | } |
| 5814 | memset(ns, 0, sizeof(xmlNs)); |
| 5815 | ns->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5816 | ns->href = xmlStrdup(XML_XML_NAMESPACE); |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5817 | ns->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5818 | doc->oldNs = ns; |
| 5819 | return (ns); |
| 5820 | } |
| 5821 | } |
| 5822 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5823 | /** |
| 5824 | * xmlSearchNs: |
| 5825 | * @doc: the document |
| 5826 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5827 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5828 | * |
| 5829 | * Search a Ns registered under a given name space for a document. |
| 5830 | * recurse on the parents until it finds the defined namespace |
| 5831 | * or return NULL otherwise. |
| 5832 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5833 | * We don't allow to cross entities boundaries. If you don't declare |
| 5834 | * the namespace within those you will be in troubles !!! A warning |
| 5835 | * is generated to cover this case. |
| 5836 | * |
| 5837 | * Returns the namespace pointer or NULL. |
| 5838 | */ |
| 5839 | xmlNsPtr |
| 5840 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5841 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5842 | xmlNsPtr cur; |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5843 | xmlNodePtr orig = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5844 | |
| 5845 | if (node == NULL) return(NULL); |
| 5846 | if ((nameSpace != NULL) && |
| 5847 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5848 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5849 | /* |
| 5850 | * The XML-1.0 namespace is normally held on the root |
| 5851 | * element. In this case exceptionally create it on the |
| 5852 | * node element. |
| 5853 | */ |
| 5854 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5855 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5856 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5857 | return(NULL); |
| 5858 | } |
| 5859 | memset(cur, 0, sizeof(xmlNs)); |
| 5860 | cur->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5861 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5862 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5863 | cur->next = node->nsDef; |
| 5864 | node->nsDef = cur; |
| 5865 | return(cur); |
| 5866 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5867 | if (doc == NULL) { |
| 5868 | doc = node->doc; |
| 5869 | if (doc == NULL) |
| 5870 | return(NULL); |
| 5871 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5872 | /* |
| 5873 | * Return the XML namespace declaration held by the doc. |
| 5874 | */ |
| 5875 | if (doc->oldNs == NULL) |
| 5876 | return(xmlTreeEnsureXMLDecl(doc)); |
| 5877 | else |
| 5878 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5879 | } |
| 5880 | while (node != NULL) { |
| 5881 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5882 | (node->type == XML_ENTITY_NODE) || |
| 5883 | (node->type == XML_ENTITY_DECL)) |
| 5884 | return(NULL); |
| 5885 | if (node->type == XML_ELEMENT_NODE) { |
| 5886 | cur = node->nsDef; |
| 5887 | while (cur != NULL) { |
| 5888 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5889 | (cur->href != NULL)) |
| 5890 | return(cur); |
| 5891 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5892 | (cur->href != NULL) && |
| 5893 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5894 | return(cur); |
| 5895 | cur = cur->next; |
| 5896 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5897 | if (orig != node) { |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5898 | cur = node->ns; |
| 5899 | if (cur != NULL) { |
| 5900 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5901 | (cur->href != NULL)) |
| 5902 | return(cur); |
| 5903 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5904 | (cur->href != NULL) && |
| 5905 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5906 | return(cur); |
| 5907 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5908 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5909 | } |
| 5910 | node = node->parent; |
| 5911 | } |
| 5912 | return(NULL); |
| 5913 | } |
| 5914 | |
| 5915 | /** |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5916 | * xmlNsInScope: |
| 5917 | * @doc: the document |
| 5918 | * @node: the current node |
| 5919 | * @ancestor: the ancestor carrying the namespace |
| 5920 | * @prefix: the namespace prefix |
| 5921 | * |
| 5922 | * Verify that the given namespace held on @ancestor is still in scope |
| 5923 | * on node. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5924 | * |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5925 | * Returns 1 if true, 0 if false and -1 in case of error. |
| 5926 | */ |
| 5927 | static int |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5928 | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 5929 | xmlNodePtr ancestor, const xmlChar * prefix) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5930 | { |
| 5931 | xmlNsPtr tst; |
| 5932 | |
| 5933 | while ((node != NULL) && (node != ancestor)) { |
| 5934 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5935 | (node->type == XML_ENTITY_NODE) || |
| 5936 | (node->type == XML_ENTITY_DECL)) |
| 5937 | return (-1); |
| 5938 | if (node->type == XML_ELEMENT_NODE) { |
| 5939 | tst = node->nsDef; |
| 5940 | while (tst != NULL) { |
| 5941 | if ((tst->prefix == NULL) |
| 5942 | && (prefix == NULL)) |
| 5943 | return (0); |
| 5944 | if ((tst->prefix != NULL) |
| 5945 | && (prefix != NULL) |
| 5946 | && (xmlStrEqual(tst->prefix, prefix))) |
| 5947 | return (0); |
| 5948 | tst = tst->next; |
| 5949 | } |
| 5950 | } |
| 5951 | node = node->parent; |
| 5952 | } |
| 5953 | if (node != ancestor) |
| 5954 | return (-1); |
| 5955 | return (1); |
| 5956 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5957 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5958 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5959 | * xmlSearchNsByHref: |
| 5960 | * @doc: the document |
| 5961 | * @node: the current node |
| 5962 | * @href: the namespace value |
| 5963 | * |
| 5964 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 5965 | * the defined namespace or return NULL otherwise. |
| 5966 | * Returns the namespace pointer or NULL. |
| 5967 | */ |
| 5968 | xmlNsPtr |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5969 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) |
| 5970 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5971 | xmlNsPtr cur; |
| 5972 | xmlNodePtr orig = node; |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5973 | int is_attr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5974 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5975 | if ((node == NULL) || (href == NULL)) |
| 5976 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5977 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5978 | /* |
| 5979 | * Only the document can hold the XML spec namespace. |
| 5980 | */ |
| 5981 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5982 | /* |
| 5983 | * The XML-1.0 namespace is normally held on the root |
| 5984 | * element. In this case exceptionally create it on the |
| 5985 | * node element. |
| 5986 | */ |
| 5987 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5988 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5989 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5990 | return (NULL); |
| 5991 | } |
| 5992 | memset(cur, 0, sizeof(xmlNs)); |
| 5993 | cur->type = XML_LOCAL_NAMESPACE; |
| 5994 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5995 | cur->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5996 | cur->next = node->nsDef; |
| 5997 | node->nsDef = cur; |
| 5998 | return (cur); |
| 5999 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6000 | if (doc == NULL) { |
| 6001 | doc = node->doc; |
| 6002 | if (doc == NULL) |
| 6003 | return(NULL); |
| 6004 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 6005 | /* |
| 6006 | * Return the XML namespace declaration held by the doc. |
| 6007 | */ |
| 6008 | if (doc->oldNs == NULL) |
| 6009 | return(xmlTreeEnsureXMLDecl(doc)); |
| 6010 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6011 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6012 | } |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6013 | is_attr = (node->type == XML_ATTRIBUTE_NODE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6014 | while (node != NULL) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6015 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 6016 | (node->type == XML_ENTITY_NODE) || |
| 6017 | (node->type == XML_ENTITY_DECL)) |
| 6018 | return (NULL); |
| 6019 | if (node->type == XML_ELEMENT_NODE) { |
| 6020 | cur = node->nsDef; |
| 6021 | while (cur != NULL) { |
| 6022 | if ((cur->href != NULL) && (href != NULL) && |
| 6023 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6024 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 6025 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6026 | return (cur); |
| 6027 | } |
| 6028 | cur = cur->next; |
| 6029 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 6030 | if (orig != node) { |
| 6031 | cur = node->ns; |
| 6032 | if (cur != NULL) { |
| 6033 | if ((cur->href != NULL) && (href != NULL) && |
| 6034 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6035 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 6036 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 6037 | return (cur); |
| 6038 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6039 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6040 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6041 | } |
| 6042 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6043 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6044 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6045 | } |
| 6046 | |
| 6047 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6048 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6049 | * @doc: the document |
| 6050 | * @tree: a node expected to hold the new namespace |
| 6051 | * @ns: the original namespace |
| 6052 | * |
| 6053 | * This function tries to locate a namespace definition in a tree |
| 6054 | * ancestors, or create a new namespace definition node similar to |
| 6055 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 6056 | * null (default namespace) or reused within the subtree defined by |
| 6057 | * @tree or on one of its ancestors then a new prefix is generated. |
| 6058 | * Returns the (new) namespace definition or NULL in case of error |
| 6059 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 6060 | static xmlNsPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6061 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 6062 | xmlNsPtr def; |
| 6063 | xmlChar prefix[50]; |
| 6064 | int counter = 1; |
| 6065 | |
| 6066 | if (tree == NULL) { |
| 6067 | #ifdef DEBUG_TREE |
| 6068 | xmlGenericError(xmlGenericErrorContext, |
| 6069 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 6070 | #endif |
| 6071 | return(NULL); |
| 6072 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 6073 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6074 | #ifdef DEBUG_TREE |
| 6075 | xmlGenericError(xmlGenericErrorContext, |
| 6076 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 6077 | #endif |
| 6078 | return(NULL); |
| 6079 | } |
| 6080 | /* |
| 6081 | * Search an existing namespace definition inherited. |
| 6082 | */ |
| 6083 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 6084 | if (def != NULL) |
| 6085 | return(def); |
| 6086 | |
| 6087 | /* |
| 6088 | * Find a close prefix which is not already in use. |
| 6089 | * Let's strip namespace prefixes longer than 20 chars ! |
| 6090 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6091 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6092 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6093 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 6094 | snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6095 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6096 | def = xmlSearchNs(doc, tree, prefix); |
| 6097 | while (def != NULL) { |
| 6098 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6099 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6100 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6101 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 6102 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6103 | (char *)ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6104 | def = xmlSearchNs(doc, tree, prefix); |
| 6105 | } |
| 6106 | |
| 6107 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6108 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6109 | */ |
| 6110 | def = xmlNewNs(tree, ns->href, prefix); |
| 6111 | return(def); |
| 6112 | } |
| 6113 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6114 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6115 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6116 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6117 | * @doc: the document |
| 6118 | * @tree: a node defining the subtree to reconciliate |
| 6119 | * |
| 6120 | * This function checks that all the namespaces declared within the given |
| 6121 | * tree are properly declared. This is needed for example after Copy or Cut |
| 6122 | * and then paste operations. The subtree may still hold pointers to |
| 6123 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6124 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6125 | * the new environment. If not possible the new namespaces are redeclared |
| 6126 | * on @tree at the top of the given subtree. |
| 6127 | * Returns the number of namespace declarations created or -1 in case of error. |
| 6128 | */ |
| 6129 | int |
| 6130 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 6131 | xmlNsPtr *oldNs = NULL; |
| 6132 | xmlNsPtr *newNs = NULL; |
| 6133 | int sizeCache = 0; |
| 6134 | int nbCache = 0; |
| 6135 | |
| 6136 | xmlNsPtr n; |
| 6137 | xmlNodePtr node = tree; |
| 6138 | xmlAttrPtr attr; |
| 6139 | int ret = 0, i; |
| 6140 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 6141 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); |
| 6142 | if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1); |
| 6143 | if (node->doc != doc) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6144 | while (node != NULL) { |
| 6145 | /* |
| 6146 | * Reconciliate the node namespace |
| 6147 | */ |
| 6148 | if (node->ns != NULL) { |
| 6149 | /* |
| 6150 | * initialize the cache if needed |
| 6151 | */ |
| 6152 | if (sizeCache == 0) { |
| 6153 | sizeCache = 10; |
| 6154 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6155 | sizeof(xmlNsPtr)); |
| 6156 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6157 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6158 | return(-1); |
| 6159 | } |
| 6160 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6161 | sizeof(xmlNsPtr)); |
| 6162 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6163 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6164 | xmlFree(oldNs); |
| 6165 | return(-1); |
| 6166 | } |
| 6167 | } |
| 6168 | for (i = 0;i < nbCache;i++) { |
| 6169 | if (oldNs[i] == node->ns) { |
| 6170 | node->ns = newNs[i]; |
| 6171 | break; |
| 6172 | } |
| 6173 | } |
| 6174 | if (i == nbCache) { |
| 6175 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6176 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6177 | */ |
| 6178 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 6179 | if (n != NULL) { /* :-( what if else ??? */ |
| 6180 | /* |
| 6181 | * check if we need to grow the cache buffers. |
| 6182 | */ |
| 6183 | if (sizeCache <= nbCache) { |
| 6184 | sizeCache *= 2; |
| 6185 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 6186 | sizeof(xmlNsPtr)); |
| 6187 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6188 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6189 | xmlFree(newNs); |
| 6190 | return(-1); |
| 6191 | } |
| 6192 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 6193 | sizeof(xmlNsPtr)); |
| 6194 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6195 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6196 | xmlFree(oldNs); |
| 6197 | return(-1); |
| 6198 | } |
| 6199 | } |
| 6200 | newNs[nbCache] = n; |
| 6201 | oldNs[nbCache++] = node->ns; |
| 6202 | node->ns = n; |
| 6203 | } |
| 6204 | } |
| 6205 | } |
| 6206 | /* |
| 6207 | * now check for namespace hold by attributes on the node. |
| 6208 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6209 | if (node->type == XML_ELEMENT_NODE) { |
| 6210 | attr = node->properties; |
| 6211 | while (attr != NULL) { |
| 6212 | if (attr->ns != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6213 | /* |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6214 | * initialize the cache if needed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6215 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6216 | if (sizeCache == 0) { |
| 6217 | sizeCache = 10; |
| 6218 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6219 | sizeof(xmlNsPtr)); |
| 6220 | if (oldNs == NULL) { |
| 6221 | xmlTreeErrMemory("fixing namespaces"); |
| 6222 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6223 | } |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6224 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6225 | sizeof(xmlNsPtr)); |
| 6226 | if (newNs == NULL) { |
| 6227 | xmlTreeErrMemory("fixing namespaces"); |
| 6228 | xmlFree(oldNs); |
| 6229 | return(-1); |
| 6230 | } |
| 6231 | } |
| 6232 | for (i = 0;i < nbCache;i++) { |
| 6233 | if (oldNs[i] == attr->ns) { |
| 6234 | attr->ns = newNs[i]; |
| 6235 | break; |
| 6236 | } |
| 6237 | } |
| 6238 | if (i == nbCache) { |
| 6239 | /* |
| 6240 | * OK we need to recreate a new namespace definition |
| 6241 | */ |
| 6242 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 6243 | if (n != NULL) { /* :-( what if else ??? */ |
| 6244 | /* |
| 6245 | * check if we need to grow the cache buffers. |
| 6246 | */ |
| 6247 | if (sizeCache <= nbCache) { |
| 6248 | sizeCache *= 2; |
| 6249 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, |
| 6250 | sizeCache * sizeof(xmlNsPtr)); |
| 6251 | if (oldNs == NULL) { |
| 6252 | xmlTreeErrMemory("fixing namespaces"); |
| 6253 | xmlFree(newNs); |
| 6254 | return(-1); |
| 6255 | } |
| 6256 | newNs = (xmlNsPtr *) xmlRealloc(newNs, |
| 6257 | sizeCache * sizeof(xmlNsPtr)); |
| 6258 | if (newNs == NULL) { |
| 6259 | xmlTreeErrMemory("fixing namespaces"); |
| 6260 | xmlFree(oldNs); |
| 6261 | return(-1); |
| 6262 | } |
| 6263 | } |
| 6264 | newNs[nbCache] = n; |
| 6265 | oldNs[nbCache++] = attr->ns; |
| 6266 | attr->ns = n; |
| 6267 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6268 | } |
| 6269 | } |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6270 | attr = attr->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6271 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6272 | } |
| 6273 | |
| 6274 | /* |
| 6275 | * Browse the full subtree, deep first |
| 6276 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6277 | if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6278 | /* deep first */ |
| 6279 | node = node->children; |
| 6280 | } else if ((node != tree) && (node->next != NULL)) { |
| 6281 | /* then siblings */ |
| 6282 | node = node->next; |
| 6283 | } else if (node != tree) { |
| 6284 | /* go up to parents->next if needed */ |
| 6285 | while (node != tree) { |
| 6286 | if (node->parent != NULL) |
| 6287 | node = node->parent; |
| 6288 | if ((node != tree) && (node->next != NULL)) { |
| 6289 | node = node->next; |
| 6290 | break; |
| 6291 | } |
| 6292 | if (node->parent == NULL) { |
| 6293 | node = NULL; |
| 6294 | break; |
| 6295 | } |
| 6296 | } |
| 6297 | /* exit condition */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6298 | if (node == tree) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6299 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 6300 | } else |
| 6301 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6302 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6303 | if (oldNs != NULL) |
| 6304 | xmlFree(oldNs); |
| 6305 | if (newNs != NULL) |
| 6306 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6307 | return(ret); |
| 6308 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6309 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6310 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6311 | static xmlAttrPtr |
| 6312 | xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name, |
| 6313 | const xmlChar *nsName, int useDTD) |
| 6314 | { |
| 6315 | xmlAttrPtr prop; |
| 6316 | |
| 6317 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6318 | return(NULL); |
| 6319 | |
| 6320 | if (node->properties != NULL) { |
| 6321 | prop = node->properties; |
| 6322 | if (nsName == NULL) { |
| 6323 | /* |
| 6324 | * We want the attr to be in no namespace. |
| 6325 | */ |
| 6326 | do { |
| 6327 | if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) { |
| 6328 | return(prop); |
| 6329 | } |
| 6330 | prop = prop->next; |
| 6331 | } while (prop != NULL); |
| 6332 | } else { |
| 6333 | /* |
| 6334 | * We want the attr to be in the specified namespace. |
| 6335 | */ |
| 6336 | do { |
| 6337 | if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) && |
| 6338 | ((prop->ns->href == nsName) || |
| 6339 | xmlStrEqual(prop->ns->href, nsName))) |
| 6340 | { |
| 6341 | return(prop); |
| 6342 | } |
| 6343 | prop = prop->next; |
| 6344 | } while (prop != NULL); |
| 6345 | } |
| 6346 | } |
| 6347 | |
| 6348 | #ifdef LIBXML_TREE_ENABLED |
| 6349 | if (! useDTD) |
| 6350 | return(NULL); |
| 6351 | /* |
| 6352 | * Check if there is a default/fixed attribute declaration in |
| 6353 | * the internal or external subset. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6354 | */ |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6355 | if ((node->doc != NULL) && (node->doc->intSubset != NULL)) { |
| 6356 | xmlDocPtr doc = node->doc; |
| 6357 | xmlAttributePtr attrDecl = NULL; |
| 6358 | xmlChar *elemQName, *tmpstr = NULL; |
| 6359 | |
| 6360 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6361 | * We need the QName of the element for the DTD-lookup. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6362 | */ |
| 6363 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 6364 | tmpstr = xmlStrdup(node->ns->prefix); |
| 6365 | tmpstr = xmlStrcat(tmpstr, BAD_CAST ":"); |
| 6366 | tmpstr = xmlStrcat(tmpstr, node->name); |
| 6367 | if (tmpstr == NULL) |
| 6368 | return(NULL); |
| 6369 | elemQName = tmpstr; |
| 6370 | } else |
| 6371 | elemQName = (xmlChar *) node->name; |
| 6372 | if (nsName == NULL) { |
| 6373 | /* |
| 6374 | * The common and nice case: Attr in no namespace. |
| 6375 | */ |
| 6376 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, |
| 6377 | elemQName, name, NULL); |
| 6378 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 6379 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, |
| 6380 | elemQName, name, NULL); |
| 6381 | } |
| 6382 | } else { |
| 6383 | xmlNsPtr *nsList, *cur; |
| 6384 | |
| 6385 | /* |
| 6386 | * The ugly case: Search using the prefixes of in-scope |
| 6387 | * ns-decls corresponding to @nsName. |
| 6388 | */ |
| 6389 | nsList = xmlGetNsList(node->doc, node); |
| 6390 | if (nsList == NULL) { |
| 6391 | if (tmpstr != NULL) |
| 6392 | xmlFree(tmpstr); |
| 6393 | return(NULL); |
| 6394 | } |
| 6395 | cur = nsList; |
| 6396 | while (*cur != NULL) { |
| 6397 | if (xmlStrEqual((*cur)->href, nsName)) { |
| 6398 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName, |
| 6399 | name, (*cur)->prefix); |
| 6400 | if (attrDecl) |
| 6401 | break; |
| 6402 | if (doc->extSubset != NULL) { |
| 6403 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName, |
| 6404 | name, (*cur)->prefix); |
| 6405 | if (attrDecl) |
| 6406 | break; |
| 6407 | } |
| 6408 | } |
| 6409 | cur++; |
| 6410 | } |
| 6411 | xmlFree(nsList); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6412 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6413 | if (tmpstr != NULL) |
| 6414 | xmlFree(tmpstr); |
| 6415 | /* |
| 6416 | * Only default/fixed attrs are relevant. |
| 6417 | */ |
| 6418 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6419 | return((xmlAttrPtr) attrDecl); |
| 6420 | } |
| 6421 | #endif /* LIBXML_TREE_ENABLED */ |
| 6422 | return(NULL); |
| 6423 | } |
| 6424 | |
| 6425 | static xmlChar* |
| 6426 | xmlGetPropNodeValueInternal(xmlAttrPtr prop) |
| 6427 | { |
| 6428 | if (prop == NULL) |
| 6429 | return(NULL); |
| 6430 | if (prop->type == XML_ATTRIBUTE_NODE) { |
| 6431 | /* |
| 6432 | * Note that we return at least the empty string. |
| 6433 | * TODO: Do we really always want that? |
| 6434 | */ |
| 6435 | if (prop->children != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 6436 | if ((prop->children->next == NULL) && |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6437 | ((prop->children->type == XML_TEXT_NODE) || |
| 6438 | (prop->children->type == XML_CDATA_SECTION_NODE))) |
| 6439 | { |
| 6440 | /* |
| 6441 | * Optimization for the common case: only 1 text node. |
| 6442 | */ |
| 6443 | return(xmlStrdup(prop->children->content)); |
| 6444 | } else { |
| 6445 | xmlChar *ret; |
| 6446 | |
| 6447 | ret = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 6448 | if (ret != NULL) |
| 6449 | return(ret); |
| 6450 | } |
| 6451 | } |
| 6452 | return(xmlStrdup((xmlChar *)"")); |
| 6453 | } else if (prop->type == XML_ATTRIBUTE_DECL) { |
| 6454 | return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue)); |
| 6455 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6456 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6457 | } |
| 6458 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6459 | /** |
| 6460 | * xmlHasProp: |
| 6461 | * @node: the node |
| 6462 | * @name: the attribute name |
| 6463 | * |
| 6464 | * Search an attribute associated to a node |
| 6465 | * This function also looks in DTD attribute declaration for #FIXED or |
| 6466 | * default declaration values unless DTD use has been turned off. |
| 6467 | * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6468 | * Returns the attribute or the attribute declaration or NULL if |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6469 | * neither was found. |
| 6470 | */ |
| 6471 | xmlAttrPtr |
| 6472 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 6473 | xmlAttrPtr prop; |
| 6474 | xmlDocPtr doc; |
| 6475 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6476 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6477 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6478 | /* |
| 6479 | * Check on the properties attached to the node |
| 6480 | */ |
| 6481 | prop = node->properties; |
| 6482 | while (prop != NULL) { |
| 6483 | if (xmlStrEqual(prop->name, name)) { |
| 6484 | return(prop); |
| 6485 | } |
| 6486 | prop = prop->next; |
| 6487 | } |
| 6488 | if (!xmlCheckDTD) return(NULL); |
| 6489 | |
| 6490 | /* |
| 6491 | * Check if there is a default declaration in the internal |
| 6492 | * or external subsets |
| 6493 | */ |
| 6494 | doc = node->doc; |
| 6495 | if (doc != NULL) { |
| 6496 | xmlAttributePtr attrDecl; |
| 6497 | if (doc->intSubset != NULL) { |
| 6498 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6499 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6500 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6501 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6502 | /* return attribute declaration only if a default value is given |
| 6503 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6504 | return((xmlAttrPtr) attrDecl); |
| 6505 | } |
| 6506 | } |
| 6507 | return(NULL); |
| 6508 | } |
| 6509 | |
| 6510 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6511 | * xmlHasNsProp: |
| 6512 | * @node: the node |
| 6513 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6514 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6515 | * |
| 6516 | * Search for an attribute associated to a node |
| 6517 | * This attribute has to be anchored in the namespace specified. |
| 6518 | * This does the entity substitution. |
| 6519 | * This function looks in DTD attribute declaration for #FIXED or |
| 6520 | * default declaration values unless DTD use has been turned off. |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6521 | * Note that a namespace of NULL indicates to use the default namespace. |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6522 | * |
| 6523 | * Returns the attribute or the attribute declaration or NULL |
| 6524 | * if neither was found. |
| 6525 | */ |
| 6526 | xmlAttrPtr |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6527 | xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6528 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6529 | return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD)); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6530 | } |
| 6531 | |
| 6532 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6533 | * xmlGetProp: |
| 6534 | * @node: the node |
| 6535 | * @name: the attribute name |
| 6536 | * |
| 6537 | * Search and get the value of an attribute associated to a node |
| 6538 | * This does the entity substitution. |
| 6539 | * This function looks in DTD attribute declaration for #FIXED or |
| 6540 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 6541 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6542 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 6543 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6544 | * |
| 6545 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6546 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6547 | */ |
| 6548 | xmlChar * |
| 6549 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6550 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6551 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6552 | prop = xmlHasProp(node, name); |
| 6553 | if (prop == NULL) |
| 6554 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6555 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
| 6558 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6559 | * xmlGetNoNsProp: |
| 6560 | * @node: the node |
| 6561 | * @name: the attribute name |
| 6562 | * |
| 6563 | * Search and get the value of an attribute associated to a node |
| 6564 | * This does the entity substitution. |
| 6565 | * This function looks in DTD attribute declaration for #FIXED or |
| 6566 | * default declaration values unless DTD use has been turned off. |
| 6567 | * This function is similar to xmlGetProp except it will accept only |
| 6568 | * an attribute in no namespace. |
| 6569 | * |
| 6570 | * Returns the attribute value or NULL if not found. |
| 6571 | * It's up to the caller to free the memory with xmlFree(). |
| 6572 | */ |
| 6573 | xmlChar * |
| 6574 | xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) { |
| 6575 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6576 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6577 | prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD); |
| 6578 | if (prop == NULL) |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6579 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6580 | return(xmlGetPropNodeValueInternal(prop)); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6581 | } |
| 6582 | |
| 6583 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6584 | * xmlGetNsProp: |
| 6585 | * @node: the node |
| 6586 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6587 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6588 | * |
| 6589 | * Search and get the value of an attribute associated to a node |
| 6590 | * This attribute has to be anchored in the namespace specified. |
| 6591 | * This does the entity substitution. |
| 6592 | * This function looks in DTD attribute declaration for #FIXED or |
| 6593 | * default declaration values unless DTD use has been turned off. |
| 6594 | * |
| 6595 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6596 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6597 | */ |
| 6598 | xmlChar * |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6599 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6600 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6601 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6602 | prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD); |
| 6603 | if (prop == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6604 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6605 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6606 | } |
| 6607 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6608 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
| 6609 | /** |
| 6610 | * xmlUnsetProp: |
| 6611 | * @node: the node |
| 6612 | * @name: the attribute name |
| 6613 | * |
| 6614 | * Remove an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6615 | * This handles only attributes in no namespace. |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6616 | * Returns 0 if successful, -1 if not found |
| 6617 | */ |
| 6618 | int |
| 6619 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6620 | xmlAttrPtr prop; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6621 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6622 | prop = xmlGetPropNodeInternal(node, name, NULL, 0); |
| 6623 | if (prop == NULL) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6624 | return(-1); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6625 | xmlUnlinkNode((xmlNodePtr) prop); |
| 6626 | xmlFreeProp(prop); |
| 6627 | return(0); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6628 | } |
| 6629 | |
| 6630 | /** |
| 6631 | * xmlUnsetNsProp: |
| 6632 | * @node: the node |
| 6633 | * @ns: the namespace definition |
| 6634 | * @name: the attribute name |
| 6635 | * |
| 6636 | * Remove an attribute carried by a node. |
| 6637 | * Returns 0 if successful, -1 if not found |
| 6638 | */ |
| 6639 | int |
| 6640 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6641 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6642 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6643 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6644 | if (prop == NULL) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6645 | return(-1); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6646 | xmlUnlinkNode((xmlNodePtr) prop); |
| 6647 | xmlFreeProp(prop); |
| 6648 | return(0); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6649 | } |
| 6650 | #endif |
| 6651 | |
| 6652 | #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] | 6653 | /** |
| 6654 | * xmlSetProp: |
| 6655 | * @node: the node |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6656 | * @name: the attribute name (a QName) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6657 | * @value: the attribute value |
| 6658 | * |
| 6659 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6660 | * If @name has a prefix, then the corresponding |
| 6661 | * namespace-binding will be used, if in scope; it is an |
| 6662 | * error it there's no such ns-binding for the prefix in |
| 6663 | * scope. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6664 | * Returns the attribute pointer. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6665 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6666 | */ |
| 6667 | xmlAttrPtr |
| 6668 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6669 | int len; |
| 6670 | const xmlChar *nqname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6671 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6672 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6673 | return(NULL); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6674 | |
| 6675 | /* |
| 6676 | * handle QNames |
| 6677 | */ |
| 6678 | nqname = xmlSplitQName3(name, &len); |
| 6679 | if (nqname != NULL) { |
| 6680 | xmlNsPtr ns; |
| 6681 | xmlChar *prefix = xmlStrndup(name, len); |
| 6682 | ns = xmlSearchNs(node->doc, node, prefix); |
| 6683 | if (prefix != NULL) |
| 6684 | xmlFree(prefix); |
| 6685 | if (ns != NULL) |
| 6686 | return(xmlSetNsProp(node, ns, nqname, value)); |
| 6687 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6688 | return(xmlSetNsProp(node, NULL, name, value)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6689 | } |
| 6690 | |
| 6691 | /** |
| 6692 | * xmlSetNsProp: |
| 6693 | * @node: the node |
| 6694 | * @ns: the namespace definition |
| 6695 | * @name: the attribute name |
| 6696 | * @value: the attribute value |
| 6697 | * |
| 6698 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6699 | * The ns structure must be in scope, this is not checked |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6700 | * |
| 6701 | * Returns the attribute pointer. |
| 6702 | */ |
| 6703 | xmlAttrPtr |
| 6704 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6705 | const xmlChar *value) |
| 6706 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6707 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6708 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6709 | if (ns && (ns->href == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6710 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6711 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6712 | if (prop != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6713 | /* |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6714 | * Modify the attribute's value. |
| 6715 | */ |
| 6716 | if (prop->atype == XML_ATTRIBUTE_ID) { |
| 6717 | xmlRemoveID(node->doc, prop); |
| 6718 | prop->atype = XML_ATTRIBUTE_ID; |
| 6719 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6720 | if (prop->children != NULL) |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6721 | xmlFreeNodeList(prop->children); |
| 6722 | prop->children = NULL; |
| 6723 | prop->last = NULL; |
| 6724 | prop->ns = ns; |
| 6725 | if (value != NULL) { |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6726 | xmlNodePtr tmp; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6727 | |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame] | 6728 | if(!xmlCheckUTF8(value)) { |
| 6729 | xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc, |
| 6730 | NULL); |
| 6731 | if (node->doc != NULL) |
| 6732 | node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 6733 | } |
| 6734 | prop->children = xmlNewDocText(node->doc, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6735 | prop->last = NULL; |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6736 | tmp = prop->children; |
| 6737 | while (tmp != NULL) { |
| 6738 | tmp->parent = (xmlNodePtr) prop; |
| 6739 | if (tmp->next == NULL) |
| 6740 | prop->last = tmp; |
| 6741 | tmp = tmp->next; |
| 6742 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6743 | } |
| 6744 | if (prop->atype == XML_ATTRIBUTE_ID) |
| 6745 | xmlAddID(NULL, node->doc, value, prop); |
| 6746 | return(prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6747 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6748 | /* |
| 6749 | * No equal attr found; create a new one. |
| 6750 | */ |
| 6751 | return(xmlNewPropInternal(node, ns, name, value, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6752 | } |
| 6753 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6754 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6755 | |
| 6756 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6757 | * xmlNodeIsText: |
| 6758 | * @node: the node |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6759 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6760 | * Is this node a Text node ? |
| 6761 | * Returns 1 yes, 0 no |
| 6762 | */ |
| 6763 | int |
| 6764 | xmlNodeIsText(xmlNodePtr node) { |
| 6765 | if (node == NULL) return(0); |
| 6766 | |
| 6767 | if (node->type == XML_TEXT_NODE) return(1); |
| 6768 | return(0); |
| 6769 | } |
| 6770 | |
| 6771 | /** |
| 6772 | * xmlIsBlankNode: |
| 6773 | * @node: the node |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6774 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6775 | * Checks whether this node is an empty or whitespace only |
| 6776 | * (and possibly ignorable) text-node. |
| 6777 | * |
| 6778 | * Returns 1 yes, 0 no |
| 6779 | */ |
| 6780 | int |
| 6781 | xmlIsBlankNode(xmlNodePtr node) { |
| 6782 | const xmlChar *cur; |
| 6783 | if (node == NULL) return(0); |
| 6784 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6785 | if ((node->type != XML_TEXT_NODE) && |
| 6786 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6787 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6788 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6789 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6790 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6791 | if (!IS_BLANK_CH(*cur)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6792 | cur++; |
| 6793 | } |
| 6794 | |
| 6795 | return(1); |
| 6796 | } |
| 6797 | |
| 6798 | /** |
| 6799 | * xmlTextConcat: |
| 6800 | * @node: the node |
| 6801 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6802 | * @len: @content length |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6803 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6804 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6805 | * |
| 6806 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6807 | */ |
| 6808 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6809 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6810 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6811 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6812 | |
| 6813 | if ((node->type != XML_TEXT_NODE) && |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 6814 | (node->type != XML_CDATA_SECTION_NODE) && |
| 6815 | (node->type != XML_COMMENT_NODE) && |
| 6816 | (node->type != XML_PI_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6817 | #ifdef DEBUG_TREE |
| 6818 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6819 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6820 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6821 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6822 | } |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6823 | /* need to check if content is currently in the dictionary */ |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6824 | if ((node->content == (xmlChar *) &(node->properties)) || |
| 6825 | ((node->doc != NULL) && (node->doc->dict != NULL) && |
| 6826 | xmlDictOwns(node->doc->dict, node->content))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6827 | node->content = xmlStrncatNew(node->content, content, len); |
| 6828 | } else { |
| 6829 | node->content = xmlStrncat(node->content, content, len); |
| 6830 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6831 | node->properties = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6832 | if (node->content == NULL) |
| 6833 | return(-1); |
| 6834 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6835 | } |
| 6836 | |
| 6837 | /************************************************************************ |
| 6838 | * * |
| 6839 | * Output : to a FILE or in memory * |
| 6840 | * * |
| 6841 | ************************************************************************/ |
| 6842 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6843 | /** |
| 6844 | * xmlBufferCreate: |
| 6845 | * |
| 6846 | * routine to create an XML buffer. |
| 6847 | * returns the new structure. |
| 6848 | */ |
| 6849 | xmlBufferPtr |
| 6850 | xmlBufferCreate(void) { |
| 6851 | xmlBufferPtr ret; |
| 6852 | |
| 6853 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6854 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6855 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6856 | return(NULL); |
| 6857 | } |
| 6858 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 6859 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6860 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6861 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6862 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6863 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6864 | xmlFree(ret); |
| 6865 | return(NULL); |
| 6866 | } |
| 6867 | ret->content[0] = 0; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 6868 | ret->contentIO = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6869 | return(ret); |
| 6870 | } |
| 6871 | |
| 6872 | /** |
| 6873 | * xmlBufferCreateSize: |
| 6874 | * @size: initial size of buffer |
| 6875 | * |
| 6876 | * routine to create an XML buffer. |
| 6877 | * returns the new structure. |
| 6878 | */ |
| 6879 | xmlBufferPtr |
| 6880 | xmlBufferCreateSize(size_t size) { |
| 6881 | xmlBufferPtr ret; |
| 6882 | |
| 6883 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6884 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6885 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6886 | return(NULL); |
| 6887 | } |
| 6888 | ret->use = 0; |
| 6889 | ret->alloc = xmlBufferAllocScheme; |
| 6890 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 6891 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6892 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6893 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6894 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6895 | xmlFree(ret); |
| 6896 | return(NULL); |
| 6897 | } |
| 6898 | ret->content[0] = 0; |
| 6899 | } else |
| 6900 | ret->content = NULL; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 6901 | ret->contentIO = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6902 | return(ret); |
| 6903 | } |
| 6904 | |
| 6905 | /** |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6906 | * xmlBufferCreateStatic: |
| 6907 | * @mem: the memory area |
| 6908 | * @size: the size in byte |
| 6909 | * |
MST 2003 John Fleck | a0e7e93 | 2003-12-19 03:13:47 +0000 | [diff] [blame] | 6910 | * routine to create an XML buffer from an immutable memory area. |
| 6911 | * 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] | 6912 | * present until the end of the buffer lifetime. |
| 6913 | * |
| 6914 | * returns the new structure. |
| 6915 | */ |
| 6916 | xmlBufferPtr |
| 6917 | xmlBufferCreateStatic(void *mem, size_t size) { |
| 6918 | xmlBufferPtr ret; |
| 6919 | |
| 6920 | if ((mem == NULL) || (size == 0)) |
| 6921 | return(NULL); |
| 6922 | |
| 6923 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6924 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6925 | xmlTreeErrMemory("creating buffer"); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6926 | return(NULL); |
| 6927 | } |
| 6928 | ret->use = size; |
| 6929 | ret->size = size; |
| 6930 | ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; |
| 6931 | ret->content = (xmlChar *) mem; |
| 6932 | return(ret); |
| 6933 | } |
| 6934 | |
| 6935 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6936 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6937 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6938 | * @scheme: allocation scheme to use |
| 6939 | * |
| 6940 | * Sets the allocation scheme for this buffer |
| 6941 | */ |
| 6942 | void |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6943 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6944 | xmlBufferAllocationScheme scheme) { |
| 6945 | if (buf == NULL) { |
| 6946 | #ifdef DEBUG_BUFFER |
| 6947 | xmlGenericError(xmlGenericErrorContext, |
| 6948 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 6949 | #endif |
| 6950 | return; |
| 6951 | } |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 6952 | if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || |
| 6953 | (buf->alloc == XML_BUFFER_ALLOC_IO)) return; |
| 6954 | if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) || |
| 6955 | (scheme == XML_BUFFER_ALLOC_EXACT) || |
| 6956 | (scheme == XML_BUFFER_ALLOC_IMMUTABLE)) |
| 6957 | buf->alloc = scheme; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6958 | } |
| 6959 | |
| 6960 | /** |
| 6961 | * xmlBufferFree: |
| 6962 | * @buf: the buffer to free |
| 6963 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 6964 | * Frees an XML buffer. It frees both the content and the structure which |
| 6965 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6966 | */ |
| 6967 | void |
| 6968 | xmlBufferFree(xmlBufferPtr buf) { |
| 6969 | if (buf == NULL) { |
| 6970 | #ifdef DEBUG_BUFFER |
| 6971 | xmlGenericError(xmlGenericErrorContext, |
| 6972 | "xmlBufferFree: buf == NULL\n"); |
| 6973 | #endif |
| 6974 | return; |
| 6975 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6976 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 6977 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && |
| 6978 | (buf->contentIO != NULL)) { |
| 6979 | xmlFree(buf->contentIO); |
| 6980 | } else if ((buf->content != NULL) && |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6981 | (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6982 | xmlFree(buf->content); |
| 6983 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6984 | xmlFree(buf); |
| 6985 | } |
| 6986 | |
| 6987 | /** |
| 6988 | * xmlBufferEmpty: |
| 6989 | * @buf: the buffer |
| 6990 | * |
| 6991 | * empty a buffer. |
| 6992 | */ |
| 6993 | void |
| 6994 | xmlBufferEmpty(xmlBufferPtr buf) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6995 | if (buf == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6996 | if (buf->content == NULL) return; |
| 6997 | buf->use = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6998 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 6999 | buf->content = BAD_CAST ""; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7000 | } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) && |
| 7001 | (buf->contentIO != NULL)) { |
| 7002 | size_t start_buf = buf->content - buf->contentIO; |
| 7003 | |
| 7004 | buf->size += start_buf; |
| 7005 | buf->content = buf->contentIO; |
| 7006 | buf->content[0] = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7007 | } else { |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7008 | buf->content[0] = 0; |
| 7009 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7010 | } |
| 7011 | |
| 7012 | /** |
| 7013 | * xmlBufferShrink: |
| 7014 | * @buf: the buffer to dump |
| 7015 | * @len: the number of xmlChar to remove |
| 7016 | * |
| 7017 | * Remove the beginning of an XML buffer. |
| 7018 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7019 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7020 | */ |
| 7021 | int |
| 7022 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 7023 | if (buf == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7024 | if (len == 0) return(0); |
| 7025 | if (len > buf->use) return(-1); |
| 7026 | |
| 7027 | buf->use -= len; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7028 | if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || |
| 7029 | ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) { |
| 7030 | /* |
| 7031 | * we just move the content pointer, but also make sure |
| 7032 | * the perceived buffer size has shrinked accordingly |
| 7033 | */ |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7034 | buf->content += len; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7035 | buf->size -= len; |
| 7036 | |
| 7037 | /* |
| 7038 | * sometimes though it maybe be better to really shrink |
| 7039 | * on IO buffers |
| 7040 | */ |
| 7041 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7042 | size_t start_buf = buf->content - buf->contentIO; |
| 7043 | if (start_buf >= buf->size) { |
| 7044 | memmove(buf->contentIO, &buf->content[0], buf->use); |
| 7045 | buf->content = buf->contentIO; |
| 7046 | buf->content[buf->use] = 0; |
| 7047 | buf->size += start_buf; |
| 7048 | } |
| 7049 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7050 | } else { |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7051 | memmove(buf->content, &buf->content[len], buf->use); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7052 | buf->content[buf->use] = 0; |
| 7053 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7054 | return(len); |
| 7055 | } |
| 7056 | |
| 7057 | /** |
| 7058 | * xmlBufferGrow: |
| 7059 | * @buf: the buffer |
| 7060 | * @len: the minimum free size to allocate |
| 7061 | * |
| 7062 | * Grow the available space of an XML buffer. |
| 7063 | * |
| 7064 | * Returns the new available space or -1 in case of error |
| 7065 | */ |
| 7066 | int |
| 7067 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 7068 | int size; |
| 7069 | xmlChar *newbuf; |
| 7070 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 7071 | if (buf == NULL) return(-1); |
| 7072 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7073 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7074 | if (len + buf->use < buf->size) return(0); |
| 7075 | |
Daniel Veillard | ee20cd7 | 2009-08-22 15:18:31 +0200 | [diff] [blame^] | 7076 | /* |
| 7077 | * Windows has a BIG problem on realloc timing, so we try to double |
| 7078 | * the buffer size (if that's enough) (bug 146697) |
| 7079 | * Apparently BSD too, and it's probably best for linux too |
| 7080 | * On an embedded system this may be something to change |
| 7081 | */ |
| 7082 | #if 1 |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 7083 | if (buf->size > len) |
| 7084 | size = buf->size * 2; |
| 7085 | else |
| 7086 | size = buf->use + len + 100; |
| 7087 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7088 | size = buf->use + len + 100; |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 7089 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7090 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7091 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7092 | size_t start_buf = buf->content - buf->contentIO; |
| 7093 | |
| 7094 | newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size); |
| 7095 | if (newbuf == NULL) { |
| 7096 | xmlTreeErrMemory("growing buffer"); |
| 7097 | return(-1); |
| 7098 | } |
| 7099 | buf->contentIO = newbuf; |
| 7100 | buf->content = newbuf + start_buf; |
| 7101 | } else { |
| 7102 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
| 7103 | if (newbuf == NULL) { |
| 7104 | xmlTreeErrMemory("growing buffer"); |
| 7105 | return(-1); |
| 7106 | } |
| 7107 | buf->content = newbuf; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7108 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7109 | buf->size = size; |
| 7110 | return(buf->size - buf->use); |
| 7111 | } |
| 7112 | |
| 7113 | /** |
| 7114 | * xmlBufferDump: |
| 7115 | * @file: the file output |
| 7116 | * @buf: the buffer to dump |
| 7117 | * |
| 7118 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7119 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7120 | */ |
| 7121 | int |
| 7122 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 7123 | int ret; |
| 7124 | |
| 7125 | if (buf == NULL) { |
| 7126 | #ifdef DEBUG_BUFFER |
| 7127 | xmlGenericError(xmlGenericErrorContext, |
| 7128 | "xmlBufferDump: buf == NULL\n"); |
| 7129 | #endif |
| 7130 | return(0); |
| 7131 | } |
| 7132 | if (buf->content == NULL) { |
| 7133 | #ifdef DEBUG_BUFFER |
| 7134 | xmlGenericError(xmlGenericErrorContext, |
| 7135 | "xmlBufferDump: buf->content == NULL\n"); |
| 7136 | #endif |
| 7137 | return(0); |
| 7138 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 7139 | if (file == NULL) |
| 7140 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7141 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 7142 | return(ret); |
| 7143 | } |
| 7144 | |
| 7145 | /** |
| 7146 | * xmlBufferContent: |
| 7147 | * @buf: the buffer |
| 7148 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7149 | * Function to extract the content of a buffer |
| 7150 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7151 | * Returns the internal content |
| 7152 | */ |
| 7153 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7154 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7155 | xmlBufferContent(const xmlBufferPtr buf) |
| 7156 | { |
| 7157 | if(!buf) |
| 7158 | return NULL; |
| 7159 | |
| 7160 | return buf->content; |
| 7161 | } |
| 7162 | |
| 7163 | /** |
| 7164 | * xmlBufferLength: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7165 | * @buf: the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7166 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7167 | * Function to get the length of a buffer |
| 7168 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7169 | * Returns the length of data in the internal content |
| 7170 | */ |
| 7171 | |
| 7172 | int |
| 7173 | xmlBufferLength(const xmlBufferPtr buf) |
| 7174 | { |
| 7175 | if(!buf) |
| 7176 | return 0; |
| 7177 | |
| 7178 | return buf->use; |
| 7179 | } |
| 7180 | |
| 7181 | /** |
| 7182 | * xmlBufferResize: |
| 7183 | * @buf: the buffer to resize |
| 7184 | * @size: the desired size |
| 7185 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7186 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7187 | * |
| 7188 | * Returns 0 in case of problems, 1 otherwise |
| 7189 | */ |
| 7190 | int |
| 7191 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 7192 | { |
| 7193 | unsigned int newSize; |
| 7194 | xmlChar* rebuf = NULL; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7195 | size_t start_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7196 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7197 | if (buf == NULL) |
| 7198 | return(0); |
| 7199 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7200 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
| 7201 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7202 | /* Don't resize if we don't have to */ |
| 7203 | if (size < buf->size) |
| 7204 | return 1; |
| 7205 | |
| 7206 | /* figure out new size */ |
| 7207 | switch (buf->alloc){ |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7208 | case XML_BUFFER_ALLOC_IO: |
| 7209 | case XML_BUFFER_ALLOC_DOUBLEIT: |
| 7210 | /*take care of empty case*/ |
| 7211 | newSize = (buf->size ? buf->size*2 : size + 10); |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 7212 | while (size > newSize) { |
| 7213 | if (newSize > UINT_MAX / 2) { |
| 7214 | xmlTreeErrMemory("growing buffer"); |
| 7215 | return 0; |
| 7216 | } |
| 7217 | newSize *= 2; |
| 7218 | } |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7219 | break; |
| 7220 | case XML_BUFFER_ALLOC_EXACT: |
| 7221 | newSize = size+10; |
| 7222 | break; |
| 7223 | default: |
| 7224 | newSize = size+10; |
| 7225 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7226 | } |
| 7227 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7228 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7229 | start_buf = buf->content - buf->contentIO; |
| 7230 | |
| 7231 | if (start_buf > newSize) { |
| 7232 | /* move data back to start */ |
| 7233 | memmove(buf->contentIO, buf->content, buf->use); |
| 7234 | buf->content = buf->contentIO; |
| 7235 | buf->content[buf->use] = 0; |
| 7236 | buf->size += start_buf; |
| 7237 | } else { |
| 7238 | rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize); |
| 7239 | if (rebuf == NULL) { |
| 7240 | xmlTreeErrMemory("growing buffer"); |
| 7241 | return 0; |
| 7242 | } |
| 7243 | buf->contentIO = rebuf; |
| 7244 | buf->content = rebuf + start_buf; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7245 | } |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7246 | } else { |
| 7247 | if (buf->content == NULL) { |
| 7248 | rebuf = (xmlChar *) xmlMallocAtomic(newSize); |
| 7249 | } else if (buf->size - buf->use < 100) { |
| 7250 | rebuf = (xmlChar *) xmlRealloc(buf->content, newSize); |
| 7251 | } else { |
| 7252 | /* |
| 7253 | * if we are reallocating a buffer far from being full, it's |
| 7254 | * better to make a new allocation and copy only the used range |
| 7255 | * and free the old one. |
| 7256 | */ |
| 7257 | rebuf = (xmlChar *) xmlMallocAtomic(newSize); |
| 7258 | if (rebuf != NULL) { |
| 7259 | memcpy(rebuf, buf->content, buf->use); |
| 7260 | xmlFree(buf->content); |
| 7261 | rebuf[buf->use] = 0; |
| 7262 | } |
| 7263 | } |
| 7264 | if (rebuf == NULL) { |
| 7265 | xmlTreeErrMemory("growing buffer"); |
| 7266 | return 0; |
| 7267 | } |
| 7268 | buf->content = rebuf; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7269 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7270 | buf->size = newSize; |
| 7271 | |
| 7272 | return 1; |
| 7273 | } |
| 7274 | |
| 7275 | /** |
| 7276 | * xmlBufferAdd: |
| 7277 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7278 | * @str: the #xmlChar string |
| 7279 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7280 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7281 | * 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] | 7282 | * str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7283 | * |
| 7284 | * Returns 0 successful, a positive error code number otherwise |
| 7285 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7286 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7287 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7288 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 7289 | unsigned int needSize; |
| 7290 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7291 | if ((str == NULL) || (buf == NULL)) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7292 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7293 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7294 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7295 | if (len < -1) { |
| 7296 | #ifdef DEBUG_BUFFER |
| 7297 | xmlGenericError(xmlGenericErrorContext, |
| 7298 | "xmlBufferAdd: len < 0\n"); |
| 7299 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7300 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7301 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7302 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7303 | |
| 7304 | if (len < 0) |
| 7305 | len = xmlStrlen(str); |
| 7306 | |
Daniel Veillard | c992332 | 2007-04-24 18:12:06 +0000 | [diff] [blame] | 7307 | if (len < 0) return -1; |
| 7308 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7309 | |
| 7310 | needSize = buf->use + len + 2; |
| 7311 | if (needSize > buf->size){ |
| 7312 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7313 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7314 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7315 | } |
| 7316 | } |
| 7317 | |
| 7318 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 7319 | buf->use += len; |
| 7320 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7321 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7322 | } |
| 7323 | |
| 7324 | /** |
| 7325 | * xmlBufferAddHead: |
| 7326 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7327 | * @str: the #xmlChar string |
| 7328 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7329 | * |
| 7330 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7331 | * if len == -1, the length of @str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7332 | * |
| 7333 | * Returns 0 successful, a positive error code number otherwise |
| 7334 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7335 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7336 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7337 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 7338 | unsigned int needSize; |
| 7339 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7340 | if (buf == NULL) |
| 7341 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7342 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7343 | if (str == NULL) { |
| 7344 | #ifdef DEBUG_BUFFER |
| 7345 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7346 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7347 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7348 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7349 | } |
| 7350 | if (len < -1) { |
| 7351 | #ifdef DEBUG_BUFFER |
| 7352 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7353 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7354 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7355 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7356 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7357 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7358 | |
| 7359 | if (len < 0) |
| 7360 | len = xmlStrlen(str); |
| 7361 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7362 | if (len <= 0) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7363 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7364 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7365 | size_t start_buf = buf->content - buf->contentIO; |
| 7366 | |
| 7367 | if (start_buf > (unsigned int) len) { |
| 7368 | /* |
| 7369 | * We can add it in the space previously shrinked |
| 7370 | */ |
| 7371 | buf->content -= len; |
| 7372 | memmove(&buf->content[0], str, len); |
| 7373 | buf->use += len; |
| 7374 | buf->size += len; |
| 7375 | return(0); |
| 7376 | } |
| 7377 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7378 | needSize = buf->use + len + 2; |
| 7379 | if (needSize > buf->size){ |
| 7380 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7381 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7382 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7383 | } |
| 7384 | } |
| 7385 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7386 | memmove(&buf->content[len], &buf->content[0], buf->use); |
| 7387 | memmove(&buf->content[0], str, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7388 | buf->use += len; |
| 7389 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7390 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7391 | } |
| 7392 | |
| 7393 | /** |
| 7394 | * xmlBufferCat: |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7395 | * @buf: the buffer to add to |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7396 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7397 | * |
| 7398 | * Append a zero terminated string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7399 | * |
| 7400 | * Returns 0 successful, a positive error code number otherwise |
| 7401 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7402 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7403 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7404 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7405 | if (buf == NULL) |
| 7406 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7407 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
| 7408 | if (str == NULL) return -1; |
| 7409 | return xmlBufferAdd(buf, str, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7410 | } |
| 7411 | |
| 7412 | /** |
| 7413 | * xmlBufferCCat: |
| 7414 | * @buf: the buffer to dump |
| 7415 | * @str: the C char string |
| 7416 | * |
| 7417 | * Append a zero terminated C string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7418 | * |
| 7419 | * Returns 0 successful, a positive error code number otherwise |
| 7420 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7421 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7422 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7423 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 7424 | const char *cur; |
| 7425 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7426 | if (buf == NULL) |
| 7427 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7428 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7429 | if (str == NULL) { |
| 7430 | #ifdef DEBUG_BUFFER |
| 7431 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7432 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7433 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7434 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7435 | } |
| 7436 | for (cur = str;*cur != 0;cur++) { |
| 7437 | if (buf->use + 10 >= buf->size) { |
| 7438 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7439 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7440 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7441 | } |
| 7442 | } |
| 7443 | buf->content[buf->use++] = *cur; |
| 7444 | } |
| 7445 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7446 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7447 | } |
| 7448 | |
| 7449 | /** |
| 7450 | * xmlBufferWriteCHAR: |
| 7451 | * @buf: the XML buffer |
| 7452 | * @string: the string to add |
| 7453 | * |
| 7454 | * routine which manages and grows an output buffer. This one adds |
| 7455 | * xmlChars at the end of the buffer. |
| 7456 | */ |
| 7457 | void |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7458 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7459 | if (buf == NULL) |
| 7460 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7461 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7462 | xmlBufferCat(buf, string); |
| 7463 | } |
| 7464 | |
| 7465 | /** |
| 7466 | * xmlBufferWriteChar: |
| 7467 | * @buf: the XML buffer output |
| 7468 | * @string: the string to add |
| 7469 | * |
| 7470 | * routine which manage and grows an output buffer. This one add |
| 7471 | * C chars at the end of the array. |
| 7472 | */ |
| 7473 | void |
| 7474 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7475 | if (buf == NULL) |
| 7476 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7477 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7478 | xmlBufferCCat(buf, string); |
| 7479 | } |
| 7480 | |
| 7481 | |
| 7482 | /** |
| 7483 | * xmlBufferWriteQuotedString: |
| 7484 | * @buf: the XML buffer output |
| 7485 | * @string: the string to add |
| 7486 | * |
| 7487 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7488 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7489 | * quote or double-quotes internally |
| 7490 | */ |
| 7491 | void |
| 7492 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7493 | const xmlChar *cur, *base; |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7494 | if (buf == NULL) |
| 7495 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7496 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7497 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 7498 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7499 | #ifdef DEBUG_BUFFER |
| 7500 | xmlGenericError(xmlGenericErrorContext, |
| 7501 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 7502 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7503 | xmlBufferCCat(buf, "\""); |
| 7504 | base = cur = string; |
| 7505 | while(*cur != 0){ |
| 7506 | if(*cur == '"'){ |
| 7507 | if (base != cur) |
| 7508 | xmlBufferAdd(buf, base, cur - base); |
| 7509 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 7510 | cur++; |
| 7511 | base = cur; |
| 7512 | } |
| 7513 | else { |
| 7514 | cur++; |
| 7515 | } |
| 7516 | } |
| 7517 | if (base != cur) |
| 7518 | xmlBufferAdd(buf, base, cur - base); |
| 7519 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7520 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7521 | else{ |
| 7522 | xmlBufferCCat(buf, "\'"); |
| 7523 | xmlBufferCat(buf, string); |
| 7524 | xmlBufferCCat(buf, "\'"); |
| 7525 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7526 | } else { |
| 7527 | xmlBufferCCat(buf, "\""); |
| 7528 | xmlBufferCat(buf, string); |
| 7529 | xmlBufferCCat(buf, "\""); |
| 7530 | } |
| 7531 | } |
| 7532 | |
| 7533 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7534 | /** |
| 7535 | * xmlGetDocCompressMode: |
| 7536 | * @doc: the document |
| 7537 | * |
| 7538 | * get the compression ratio for a document, ZLIB based |
| 7539 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7540 | */ |
| 7541 | int |
| 7542 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 7543 | if (doc == NULL) return(-1); |
| 7544 | return(doc->compression); |
| 7545 | } |
| 7546 | |
| 7547 | /** |
| 7548 | * xmlSetDocCompressMode: |
| 7549 | * @doc: the document |
| 7550 | * @mode: the compression ratio |
| 7551 | * |
| 7552 | * set the compression ratio for a document, ZLIB based |
| 7553 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7554 | */ |
| 7555 | void |
| 7556 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 7557 | if (doc == NULL) return; |
| 7558 | if (mode < 0) doc->compression = 0; |
| 7559 | else if (mode > 9) doc->compression = 9; |
| 7560 | else doc->compression = mode; |
| 7561 | } |
| 7562 | |
| 7563 | /** |
| 7564 | * xmlGetCompressMode: |
| 7565 | * |
| 7566 | * get the default compression mode used, ZLIB based. |
| 7567 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7568 | */ |
| 7569 | int |
| 7570 | xmlGetCompressMode(void) |
| 7571 | { |
| 7572 | return (xmlCompressMode); |
| 7573 | } |
| 7574 | |
| 7575 | /** |
| 7576 | * xmlSetCompressMode: |
| 7577 | * @mode: the compression ratio |
| 7578 | * |
| 7579 | * set the default compression mode used, ZLIB based |
| 7580 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7581 | */ |
| 7582 | void |
| 7583 | xmlSetCompressMode(int mode) { |
| 7584 | if (mode < 0) xmlCompressMode = 0; |
| 7585 | else if (mode > 9) xmlCompressMode = 9; |
| 7586 | else xmlCompressMode = mode; |
| 7587 | } |
| 7588 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7589 | #define XML_TREE_NSMAP_PARENT -1 |
| 7590 | #define XML_TREE_NSMAP_XML -2 |
| 7591 | #define XML_TREE_NSMAP_DOC -3 |
| 7592 | #define XML_TREE_NSMAP_CUSTOM -4 |
| 7593 | |
| 7594 | typedef struct xmlNsMapItem *xmlNsMapItemPtr; |
| 7595 | struct xmlNsMapItem { |
| 7596 | xmlNsMapItemPtr next; |
| 7597 | xmlNsMapItemPtr prev; |
| 7598 | xmlNsPtr oldNs; /* old ns decl reference */ |
| 7599 | xmlNsPtr newNs; /* new ns decl reference */ |
| 7600 | int shadowDepth; /* Shadowed at this depth */ |
| 7601 | /* |
| 7602 | * depth: |
| 7603 | * >= 0 == @node's ns-decls |
| 7604 | * -1 == @parent's ns-decls |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7605 | * -2 == the doc->oldNs XML ns-decl |
| 7606 | * -3 == the doc->oldNs storage ns-decls |
| 7607 | * -4 == ns-decls provided via custom ns-handling |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7608 | */ |
| 7609 | int depth; |
| 7610 | }; |
| 7611 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7612 | typedef struct xmlNsMap *xmlNsMapPtr; |
| 7613 | struct xmlNsMap { |
| 7614 | xmlNsMapItemPtr first; |
| 7615 | xmlNsMapItemPtr last; |
| 7616 | xmlNsMapItemPtr pool; |
| 7617 | }; |
| 7618 | |
| 7619 | #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL)) |
| 7620 | #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next) |
| 7621 | #define XML_NSMAP_POP(m, i) \ |
| 7622 | i = (m)->last; \ |
| 7623 | (m)->last = (i)->prev; \ |
| 7624 | if ((m)->last == NULL) \ |
| 7625 | (m)->first = NULL; \ |
| 7626 | else \ |
| 7627 | (m)->last->next = NULL; \ |
| 7628 | (i)->next = (m)->pool; \ |
| 7629 | (m)->pool = i; |
| 7630 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7631 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7632 | * xmlDOMWrapNsMapFree: |
| 7633 | * @map: the ns-map |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7634 | * |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7635 | * Frees the ns-map |
| 7636 | */ |
| 7637 | static void |
| 7638 | xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap) |
| 7639 | { |
| 7640 | xmlNsMapItemPtr cur, tmp; |
| 7641 | |
| 7642 | if (nsmap == NULL) |
| 7643 | return; |
| 7644 | cur = nsmap->pool; |
| 7645 | while (cur != NULL) { |
| 7646 | tmp = cur; |
| 7647 | cur = cur->next; |
| 7648 | xmlFree(tmp); |
| 7649 | } |
| 7650 | cur = nsmap->first; |
| 7651 | while (cur != NULL) { |
| 7652 | tmp = cur; |
| 7653 | cur = cur->next; |
| 7654 | xmlFree(tmp); |
| 7655 | } |
| 7656 | xmlFree(nsmap); |
| 7657 | } |
| 7658 | |
| 7659 | /* |
| 7660 | * xmlDOMWrapNsMapAddItem: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7661 | * @map: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7662 | * @oldNs: the old ns-struct |
| 7663 | * @newNs: the new ns-struct |
| 7664 | * @depth: depth and ns-kind information |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7665 | * |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7666 | * Adds an ns-mapping item. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7667 | */ |
| 7668 | static xmlNsMapItemPtr |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7669 | xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7670 | xmlNsPtr oldNs, xmlNsPtr newNs, int depth) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7671 | { |
| 7672 | xmlNsMapItemPtr ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7673 | xmlNsMapPtr map; |
| 7674 | |
| 7675 | if (nsmap == NULL) |
| 7676 | return(NULL); |
| 7677 | if ((position != -1) && (position != 0)) |
| 7678 | return(NULL); |
| 7679 | map = *nsmap; |
| 7680 | |
| 7681 | if (map == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7682 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7683 | * Create the ns-map. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7684 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7685 | map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap)); |
| 7686 | if (map == NULL) { |
| 7687 | xmlTreeErrMemory("allocating namespace map"); |
| 7688 | return (NULL); |
| 7689 | } |
| 7690 | memset(map, 0, sizeof(struct xmlNsMap)); |
| 7691 | *nsmap = map; |
| 7692 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7693 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7694 | if (map->pool != NULL) { |
| 7695 | /* |
| 7696 | * Reuse an item from the pool. |
| 7697 | */ |
| 7698 | ret = map->pool; |
| 7699 | map->pool = ret->next; |
| 7700 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7701 | } else { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7702 | /* |
| 7703 | * Create a new item. |
| 7704 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7705 | ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem)); |
| 7706 | if (ret == NULL) { |
| 7707 | xmlTreeErrMemory("allocating namespace map item"); |
| 7708 | return (NULL); |
| 7709 | } |
| 7710 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7711 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7712 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7713 | if (map->first == NULL) { |
| 7714 | /* |
| 7715 | * First ever. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7716 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7717 | map->first = ret; |
| 7718 | map->last = ret; |
| 7719 | } else if (position == -1) { |
| 7720 | /* |
| 7721 | * Append. |
| 7722 | */ |
| 7723 | ret->prev = map->last; |
| 7724 | map->last->next = ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7725 | map->last = ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7726 | } else if (position == 0) { |
| 7727 | /* |
| 7728 | * Set on first position. |
| 7729 | */ |
| 7730 | map->first->prev = ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7731 | ret->next = map->first; |
| 7732 | map->first = ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7733 | } else |
| 7734 | return(NULL); |
| 7735 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7736 | ret->oldNs = oldNs; |
| 7737 | ret->newNs = newNs; |
| 7738 | ret->shadowDepth = -1; |
| 7739 | ret->depth = depth; |
| 7740 | return (ret); |
| 7741 | } |
| 7742 | |
| 7743 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7744 | * xmlDOMWrapStoreNs: |
| 7745 | * @doc: the doc |
| 7746 | * @nsName: the namespace name |
| 7747 | * @prefix: the prefix |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7748 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7749 | * Creates or reuses an xmlNs struct on doc->oldNs with |
| 7750 | * the given prefix and namespace name. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7751 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7752 | * Returns the aquired ns struct or NULL in case of an API |
| 7753 | * or internal error. |
| 7754 | */ |
| 7755 | static xmlNsPtr |
| 7756 | xmlDOMWrapStoreNs(xmlDocPtr doc, |
| 7757 | const xmlChar *nsName, |
| 7758 | const xmlChar *prefix) |
| 7759 | { |
| 7760 | xmlNsPtr ns; |
| 7761 | |
| 7762 | if (doc == NULL) |
| 7763 | return (NULL); |
| 7764 | ns = xmlTreeEnsureXMLDecl(doc); |
| 7765 | if (ns == NULL) |
| 7766 | return (NULL); |
| 7767 | if (ns->next != NULL) { |
| 7768 | /* Reuse. */ |
| 7769 | ns = ns->next; |
| 7770 | while (ns != NULL) { |
| 7771 | if (((ns->prefix == prefix) || |
| 7772 | xmlStrEqual(ns->prefix, prefix)) && |
| 7773 | xmlStrEqual(ns->href, nsName)) { |
| 7774 | return (ns); |
| 7775 | } |
| 7776 | if (ns->next == NULL) |
| 7777 | break; |
| 7778 | ns = ns->next; |
| 7779 | } |
| 7780 | } |
| 7781 | /* Create. */ |
| 7782 | ns->next = xmlNewNs(NULL, nsName, prefix); |
| 7783 | return (ns->next); |
| 7784 | } |
| 7785 | |
| 7786 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7787 | * xmlDOMWrapNewCtxt: |
| 7788 | * |
| 7789 | * Allocates and initializes a new DOM-wrapper context. |
| 7790 | * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7791 | * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7792 | */ |
| 7793 | xmlDOMWrapCtxtPtr |
| 7794 | xmlDOMWrapNewCtxt(void) |
| 7795 | { |
| 7796 | xmlDOMWrapCtxtPtr ret; |
| 7797 | |
| 7798 | ret = xmlMalloc(sizeof(xmlDOMWrapCtxt)); |
| 7799 | if (ret == NULL) { |
| 7800 | xmlTreeErrMemory("allocating DOM-wrapper context"); |
| 7801 | return (NULL); |
| 7802 | } |
| 7803 | memset(ret, 0, sizeof(xmlDOMWrapCtxt)); |
| 7804 | return (ret); |
| 7805 | } |
| 7806 | |
| 7807 | /* |
| 7808 | * xmlDOMWrapFreeCtxt: |
| 7809 | * @ctxt: the DOM-wrapper context |
| 7810 | * |
| 7811 | * Frees the DOM-wrapper context. |
| 7812 | */ |
| 7813 | void |
| 7814 | xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt) |
| 7815 | { |
| 7816 | if (ctxt == NULL) |
| 7817 | return; |
| 7818 | if (ctxt->namespaceMap != NULL) |
| 7819 | xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap); |
| 7820 | /* |
| 7821 | * TODO: Store the namespace map in the context. |
| 7822 | */ |
| 7823 | xmlFree(ctxt); |
| 7824 | } |
| 7825 | |
| 7826 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7827 | * xmlTreeLookupNsListByPrefix: |
| 7828 | * @nsList: a list of ns-structs |
| 7829 | * @prefix: the searched prefix |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7830 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7831 | * Searches for a ns-decl with the given prefix in @nsList. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7832 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7833 | * Returns the ns-decl if found, NULL if not found and on |
| 7834 | * API errors. |
| 7835 | */ |
| 7836 | static xmlNsPtr |
| 7837 | xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix) |
| 7838 | { |
| 7839 | if (nsList == NULL) |
| 7840 | return (NULL); |
| 7841 | { |
| 7842 | xmlNsPtr ns; |
| 7843 | ns = nsList; |
| 7844 | do { |
| 7845 | if ((prefix == ns->prefix) || |
| 7846 | xmlStrEqual(prefix, ns->prefix)) { |
| 7847 | return (ns); |
| 7848 | } |
| 7849 | ns = ns->next; |
| 7850 | } while (ns != NULL); |
| 7851 | } |
| 7852 | return (NULL); |
| 7853 | } |
| 7854 | |
| 7855 | /* |
| 7856 | * |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7857 | * xmlDOMWrapNSNormGatherInScopeNs: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7858 | * @map: the namespace map |
| 7859 | * @node: the node to start with |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7860 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7861 | * Puts in-scope namespaces into the ns-map. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7862 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7863 | * Returns 0 on success, -1 on API or internal errors. |
| 7864 | */ |
| 7865 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7866 | xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7867 | xmlNodePtr node) |
| 7868 | { |
| 7869 | xmlNodePtr cur; |
| 7870 | xmlNsPtr ns; |
| 7871 | xmlNsMapItemPtr mi; |
| 7872 | int shadowed; |
| 7873 | |
| 7874 | if ((map == NULL) || (*map != NULL)) |
| 7875 | return (-1); |
| 7876 | /* |
| 7877 | * Get in-scope ns-decls of @parent. |
| 7878 | */ |
| 7879 | cur = node; |
| 7880 | while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) { |
| 7881 | if (cur->type == XML_ELEMENT_NODE) { |
| 7882 | if (cur->nsDef != NULL) { |
| 7883 | ns = cur->nsDef; |
| 7884 | do { |
| 7885 | shadowed = 0; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7886 | if (XML_NSMAP_NOTEMPTY(*map)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7887 | /* |
| 7888 | * Skip shadowed prefixes. |
| 7889 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7890 | XML_NSMAP_FOREACH(*map, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7891 | if ((ns->prefix == mi->newNs->prefix) || |
| 7892 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) { |
| 7893 | shadowed = 1; |
| 7894 | break; |
| 7895 | } |
| 7896 | } |
| 7897 | } |
| 7898 | /* |
| 7899 | * Insert mapping. |
| 7900 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7901 | mi = xmlDOMWrapNsMapAddItem(map, 0, NULL, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7902 | ns, XML_TREE_NSMAP_PARENT); |
| 7903 | if (mi == NULL) |
| 7904 | return (-1); |
| 7905 | if (shadowed) |
| 7906 | mi->shadowDepth = 0; |
| 7907 | ns = ns->next; |
| 7908 | } while (ns != NULL); |
| 7909 | } |
| 7910 | } |
| 7911 | cur = cur->parent; |
| 7912 | } |
| 7913 | return (0); |
| 7914 | } |
| 7915 | |
| 7916 | /* |
| 7917 | * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict; |
| 7918 | * otherwise copy it, when it was in the source-dict. |
| 7919 | */ |
| 7920 | #define XML_TREE_ADOPT_STR(str) \ |
| 7921 | if (adoptStr && (str != NULL)) { \ |
| 7922 | if (destDoc->dict) { \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7923 | const xmlChar *old = str; \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7924 | str = xmlDictLookup(destDoc->dict, str, -1); \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7925 | if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \ |
| 7926 | (!xmlDictOwns(sourceDoc->dict, old))) \ |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 7927 | xmlFree((char *)old); \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7928 | } else if ((sourceDoc) && (sourceDoc->dict) && \ |
| 7929 | xmlDictOwns(sourceDoc->dict, str)) { \ |
| 7930 | str = BAD_CAST xmlStrdup(str); \ |
| 7931 | } \ |
| 7932 | } |
| 7933 | |
| 7934 | /* |
| 7935 | * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then |
| 7936 | * put it in dest-dict or copy it. |
| 7937 | */ |
| 7938 | #define XML_TREE_ADOPT_STR_2(str) \ |
| 7939 | if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \ |
| 7940 | (sourceDoc->dict != NULL) && \ |
| 7941 | xmlDictOwns(sourceDoc->dict, cur->content)) { \ |
| 7942 | if (destDoc->dict) \ |
| 7943 | cur->content = (xmlChar *) \ |
| 7944 | xmlDictLookup(destDoc->dict, cur->content, -1); \ |
| 7945 | else \ |
| 7946 | cur->content = xmlStrdup(BAD_CAST cur->content); \ |
| 7947 | } |
| 7948 | |
| 7949 | /* |
| 7950 | * xmlDOMWrapNSNormAddNsMapItem2: |
| 7951 | * |
| 7952 | * For internal use. Adds a ns-decl mapping. |
| 7953 | * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7954 | * Returns 0 on success, -1 on internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7955 | */ |
| 7956 | static int |
| 7957 | xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number, |
| 7958 | xmlNsPtr oldNs, xmlNsPtr newNs) |
| 7959 | { |
| 7960 | if (*list == NULL) { |
| 7961 | *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr)); |
| 7962 | if (*list == NULL) { |
| 7963 | xmlTreeErrMemory("alloc ns map item"); |
| 7964 | return(-1); |
| 7965 | } |
| 7966 | *size = 3; |
| 7967 | *number = 0; |
| 7968 | } else if ((*number) >= (*size)) { |
| 7969 | *size *= 2; |
| 7970 | *list = (xmlNsPtr *) xmlRealloc(*list, |
| 7971 | (*size) * 2 * sizeof(xmlNsPtr)); |
| 7972 | if (*list == NULL) { |
| 7973 | xmlTreeErrMemory("realloc ns map item"); |
| 7974 | return(-1); |
| 7975 | } |
| 7976 | } |
| 7977 | (*list)[2 * (*number)] = oldNs; |
| 7978 | (*list)[2 * (*number) +1] = newNs; |
| 7979 | (*number)++; |
| 7980 | return (0); |
| 7981 | } |
| 7982 | |
| 7983 | /* |
| 7984 | * xmlDOMWrapRemoveNode: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7985 | * @ctxt: a DOM wrapper context |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7986 | * @doc: the doc |
| 7987 | * @node: the node to be removed. |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7988 | * @options: set of options, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7989 | * |
| 7990 | * Unlinks the given node from its owner. |
| 7991 | * This will substitute ns-references to node->nsDef for |
| 7992 | * ns-references to doc->oldNs, thus ensuring the removed |
| 7993 | * branch to be autark wrt ns-references. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7994 | * |
| 7995 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7996 | * |
| 7997 | * Returns 0 on success, 1 if the node is not supported, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7998 | * -1 on API and internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7999 | */ |
| 8000 | int |
| 8001 | xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc, |
| 8002 | xmlNodePtr node, int options ATTRIBUTE_UNUSED) |
| 8003 | { |
| 8004 | xmlNsPtr *list = NULL; |
| 8005 | int sizeList, nbList, i, j; |
| 8006 | xmlNsPtr ns; |
| 8007 | |
| 8008 | if ((node == NULL) || (doc == NULL) || (node->doc != doc)) |
| 8009 | return (-1); |
| 8010 | |
| 8011 | /* TODO: 0 or -1 ? */ |
| 8012 | if (node->parent == NULL) |
| 8013 | return (0); |
| 8014 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8015 | switch (node->type) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8016 | case XML_TEXT_NODE: |
| 8017 | case XML_CDATA_SECTION_NODE: |
| 8018 | case XML_ENTITY_REF_NODE: |
| 8019 | case XML_PI_NODE: |
| 8020 | case XML_COMMENT_NODE: |
| 8021 | xmlUnlinkNode(node); |
| 8022 | return (0); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8023 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8024 | case XML_ATTRIBUTE_NODE: |
| 8025 | break; |
| 8026 | default: |
| 8027 | return (1); |
| 8028 | } |
| 8029 | xmlUnlinkNode(node); |
| 8030 | /* |
| 8031 | * Save out-of-scope ns-references in doc->oldNs. |
| 8032 | */ |
| 8033 | do { |
| 8034 | switch (node->type) { |
| 8035 | case XML_ELEMENT_NODE: |
| 8036 | if ((ctxt == NULL) && (node->nsDef != NULL)) { |
| 8037 | ns = node->nsDef; |
| 8038 | do { |
| 8039 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 8040 | &nbList, ns, ns) == -1) |
| 8041 | goto internal_error; |
| 8042 | ns = ns->next; |
| 8043 | } while (ns != NULL); |
| 8044 | } |
| 8045 | /* No break on purpose. */ |
| 8046 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8047 | if (node->ns != NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8048 | /* |
| 8049 | * Find a mapping. |
| 8050 | */ |
| 8051 | if (list != NULL) { |
| 8052 | for (i = 0, j = 0; i < nbList; i++, j += 2) { |
| 8053 | if (node->ns == list[j]) { |
| 8054 | node->ns = list[++j]; |
| 8055 | goto next_node; |
| 8056 | } |
| 8057 | } |
| 8058 | } |
| 8059 | ns = NULL; |
| 8060 | if (ctxt != NULL) { |
| 8061 | /* |
| 8062 | * User defined. |
| 8063 | */ |
| 8064 | } else { |
| 8065 | /* |
| 8066 | * Add to doc's oldNs. |
| 8067 | */ |
| 8068 | ns = xmlDOMWrapStoreNs(doc, node->ns->href, |
| 8069 | node->ns->prefix); |
| 8070 | if (ns == NULL) |
| 8071 | goto internal_error; |
| 8072 | } |
| 8073 | if (ns != NULL) { |
| 8074 | /* |
| 8075 | * Add mapping. |
| 8076 | */ |
| 8077 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 8078 | &nbList, node->ns, ns) == -1) |
| 8079 | goto internal_error; |
| 8080 | } |
| 8081 | node->ns = ns; |
| 8082 | } |
| 8083 | if ((node->type == XML_ELEMENT_NODE) && |
| 8084 | (node->properties != NULL)) { |
| 8085 | node = (xmlNodePtr) node->properties; |
| 8086 | continue; |
| 8087 | } |
| 8088 | break; |
| 8089 | default: |
| 8090 | goto next_sibling; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8091 | } |
| 8092 | next_node: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8093 | if ((node->type == XML_ELEMENT_NODE) && |
| 8094 | (node->children != NULL)) { |
| 8095 | node = node->children; |
| 8096 | continue; |
| 8097 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8098 | next_sibling: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8099 | if (node == NULL) |
| 8100 | break; |
| 8101 | if (node->next != NULL) |
| 8102 | node = node->next; |
| 8103 | else { |
| 8104 | node = node->parent; |
| 8105 | goto next_sibling; |
| 8106 | } |
| 8107 | } while (node != NULL); |
| 8108 | |
| 8109 | if (list != NULL) |
| 8110 | xmlFree(list); |
| 8111 | return (0); |
| 8112 | |
| 8113 | internal_error: |
| 8114 | if (list != NULL) |
| 8115 | xmlFree(list); |
| 8116 | return (-1); |
| 8117 | } |
| 8118 | |
| 8119 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8120 | * xmlSearchNsByNamespaceStrict: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8121 | * @doc: the document |
| 8122 | * @node: the start node |
| 8123 | * @nsName: the searched namespace name |
| 8124 | * @retNs: the resulting ns-decl |
| 8125 | * @prefixed: if the found ns-decl must have a prefix (for attributes) |
| 8126 | * |
| 8127 | * Dynamically searches for a ns-declaration which matches |
| 8128 | * the given @nsName in the ancestor-or-self axis of @node. |
| 8129 | * |
| 8130 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8131 | * and internal errors. |
| 8132 | */ |
| 8133 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8134 | xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node, |
| 8135 | const xmlChar* nsName, |
| 8136 | xmlNsPtr *retNs, int prefixed) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8137 | { |
| 8138 | xmlNodePtr cur, prev = NULL, out = NULL; |
| 8139 | xmlNsPtr ns, prevns; |
| 8140 | |
| 8141 | if ((doc == NULL) || (nsName == NULL) || (retNs == NULL)) |
| 8142 | return (-1); |
| 8143 | |
| 8144 | *retNs = NULL; |
| 8145 | if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
| 8146 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8147 | if (*retNs == NULL) |
| 8148 | return (-1); |
| 8149 | return (1); |
| 8150 | } |
| 8151 | cur = node; |
| 8152 | do { |
| 8153 | if (cur->type == XML_ELEMENT_NODE) { |
| 8154 | if (cur->nsDef != NULL) { |
| 8155 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8156 | if (prefixed && (ns->prefix == NULL)) |
| 8157 | continue; |
| 8158 | if (prev != NULL) { |
| 8159 | /* |
| 8160 | * Check the last level of ns-decls for a |
| 8161 | * shadowing prefix. |
| 8162 | */ |
| 8163 | prevns = prev->nsDef; |
| 8164 | do { |
| 8165 | if ((prevns->prefix == ns->prefix) || |
| 8166 | ((prevns->prefix != NULL) && |
| 8167 | (ns->prefix != NULL) && |
| 8168 | xmlStrEqual(prevns->prefix, ns->prefix))) { |
| 8169 | /* |
| 8170 | * Shadowed. |
| 8171 | */ |
| 8172 | break; |
| 8173 | } |
| 8174 | prevns = prevns->next; |
| 8175 | } while (prevns != NULL); |
| 8176 | if (prevns != NULL) |
| 8177 | continue; |
| 8178 | } |
| 8179 | /* |
| 8180 | * Ns-name comparison. |
| 8181 | */ |
| 8182 | if ((nsName == ns->href) || |
| 8183 | xmlStrEqual(nsName, ns->href)) { |
| 8184 | /* |
| 8185 | * At this point the prefix can only be shadowed, |
| 8186 | * if we are the the (at least) 3rd level of |
| 8187 | * ns-decls. |
| 8188 | */ |
| 8189 | if (out) { |
| 8190 | int ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8191 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8192 | ret = xmlNsInScope(doc, node, prev, ns->prefix); |
| 8193 | if (ret < 0) |
| 8194 | return (-1); |
| 8195 | /* |
| 8196 | * TODO: Should we try to find a matching ns-name |
| 8197 | * only once? This here keeps on searching. |
| 8198 | * I think we should try further since, there might |
| 8199 | * be an other matching ns-decl with an unshadowed |
| 8200 | * prefix. |
| 8201 | */ |
| 8202 | if (! ret) |
| 8203 | continue; |
| 8204 | } |
| 8205 | *retNs = ns; |
| 8206 | return (1); |
| 8207 | } |
| 8208 | } |
| 8209 | out = prev; |
| 8210 | prev = cur; |
| 8211 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8212 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 8213 | (cur->type == XML_ENTITY_DECL)) |
| 8214 | return (0); |
| 8215 | cur = cur->parent; |
| 8216 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 8217 | return (0); |
| 8218 | } |
| 8219 | |
| 8220 | /* |
| 8221 | * xmlSearchNsByPrefixStrict: |
| 8222 | * @doc: the document |
| 8223 | * @node: the start node |
| 8224 | * @prefix: the searched namespace prefix |
| 8225 | * @retNs: the resulting ns-decl |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8226 | * |
| 8227 | * Dynamically searches for a ns-declaration which matches |
| 8228 | * the given @nsName in the ancestor-or-self axis of @node. |
| 8229 | * |
| 8230 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8231 | * and internal errors. |
| 8232 | */ |
| 8233 | static int |
| 8234 | xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node, |
| 8235 | const xmlChar* prefix, |
| 8236 | xmlNsPtr *retNs) |
| 8237 | { |
| 8238 | xmlNodePtr cur; |
| 8239 | xmlNsPtr ns; |
| 8240 | |
| 8241 | if ((doc == NULL) || (node == NULL)) |
| 8242 | return (-1); |
| 8243 | |
| 8244 | if (retNs) |
| 8245 | *retNs = NULL; |
| 8246 | if (IS_STR_XML(prefix)) { |
| 8247 | if (retNs) { |
| 8248 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8249 | if (*retNs == NULL) |
| 8250 | return (-1); |
| 8251 | } |
| 8252 | return (1); |
| 8253 | } |
| 8254 | cur = node; |
| 8255 | do { |
| 8256 | if (cur->type == XML_ELEMENT_NODE) { |
| 8257 | if (cur->nsDef != NULL) { |
| 8258 | ns = cur->nsDef; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8259 | do { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8260 | if ((prefix == ns->prefix) || |
| 8261 | xmlStrEqual(prefix, ns->prefix)) |
| 8262 | { |
| 8263 | /* |
| 8264 | * Disabled namespaces, e.g. xmlns:abc="". |
| 8265 | */ |
| 8266 | if (ns->href == NULL) |
| 8267 | return(0); |
| 8268 | if (retNs) |
| 8269 | *retNs = ns; |
| 8270 | return (1); |
| 8271 | } |
| 8272 | ns = ns->next; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8273 | } while (ns != NULL); |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8274 | } |
| 8275 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 8276 | (cur->type == XML_ENTITY_DECL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8277 | return (0); |
| 8278 | cur = cur->parent; |
| 8279 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 8280 | return (0); |
| 8281 | } |
| 8282 | |
| 8283 | /* |
| 8284 | * xmlDOMWrapNSNormDeclareNsForced: |
| 8285 | * @doc: the doc |
| 8286 | * @elem: the element-node to declare on |
| 8287 | * @nsName: the namespace-name of the ns-decl |
| 8288 | * @prefix: the preferred prefix of the ns-decl |
| 8289 | * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls |
| 8290 | * |
| 8291 | * Declares a new namespace on @elem. It tries to use the |
| 8292 | * given @prefix; if a ns-decl with the given prefix is already existent |
| 8293 | * on @elem, it will generate an other prefix. |
| 8294 | * |
| 8295 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8296 | * and internal errors. |
| 8297 | */ |
| 8298 | static xmlNsPtr |
| 8299 | xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc, |
| 8300 | xmlNodePtr elem, |
| 8301 | const xmlChar *nsName, |
| 8302 | const xmlChar *prefix, |
| 8303 | int checkShadow) |
| 8304 | { |
| 8305 | |
| 8306 | xmlNsPtr ret; |
| 8307 | char buf[50]; |
| 8308 | const xmlChar *pref; |
| 8309 | int counter = 0; |
| 8310 | /* |
| 8311 | * Create a ns-decl on @anchor. |
| 8312 | */ |
| 8313 | pref = prefix; |
| 8314 | while (1) { |
| 8315 | /* |
| 8316 | * Lookup whether the prefix is unused in elem's ns-decls. |
| 8317 | */ |
| 8318 | if ((elem->nsDef != NULL) && |
| 8319 | (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL)) |
| 8320 | goto ns_next_prefix; |
| 8321 | if (checkShadow && elem->parent && |
| 8322 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8323 | /* |
| 8324 | * Does it shadow ancestor ns-decls? |
| 8325 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8326 | if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8327 | goto ns_next_prefix; |
| 8328 | } |
| 8329 | ret = xmlNewNs(NULL, nsName, pref); |
| 8330 | if (ret == NULL) |
| 8331 | return (NULL); |
| 8332 | if (elem->nsDef == NULL) |
| 8333 | elem->nsDef = ret; |
| 8334 | else { |
| 8335 | xmlNsPtr ns2 = elem->nsDef; |
| 8336 | while (ns2->next != NULL) |
| 8337 | ns2 = ns2->next; |
| 8338 | ns2->next = ret; |
| 8339 | } |
| 8340 | return (ret); |
| 8341 | ns_next_prefix: |
| 8342 | counter++; |
| 8343 | if (counter > 1000) |
| 8344 | return (NULL); |
| 8345 | if (prefix == NULL) { |
| 8346 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8347 | "ns_%d", counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8348 | } else |
| 8349 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8350 | "%.30s_%d", (char *)prefix, counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8351 | pref = BAD_CAST buf; |
| 8352 | } |
| 8353 | } |
| 8354 | |
| 8355 | /* |
| 8356 | * xmlDOMWrapNSNormAquireNormalizedNs: |
| 8357 | * @doc: the doc |
| 8358 | * @elem: the element-node to declare namespaces on |
| 8359 | * @ns: the ns-struct to use for the search |
| 8360 | * @retNs: the found/created ns-struct |
| 8361 | * @nsMap: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8362 | * @depth: the current tree depth |
| 8363 | * @ancestorsOnly: search in ancestor ns-decls only |
| 8364 | * @prefixed: if the searched ns-decl must have a prefix (for attributes) |
| 8365 | * |
| 8366 | * Searches for a matching ns-name in the ns-decls of @nsMap, if not |
| 8367 | * found it will either declare it on @elem, or store it in doc->oldNs. |
| 8368 | * If a new ns-decl needs to be declared on @elem, it tries to use the |
| 8369 | * @ns->prefix for it, if this prefix is already in use on @elem, it will |
| 8370 | * change the prefix or the new ns-decl. |
| 8371 | * |
| 8372 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8373 | */ |
| 8374 | static int |
| 8375 | xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc, |
| 8376 | xmlNodePtr elem, |
| 8377 | xmlNsPtr ns, |
| 8378 | xmlNsPtr *retNs, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8379 | xmlNsMapPtr *nsMap, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8380 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8381 | int depth, |
| 8382 | int ancestorsOnly, |
| 8383 | int prefixed) |
| 8384 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8385 | xmlNsMapItemPtr mi; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8386 | |
| 8387 | if ((doc == NULL) || (ns == NULL) || (retNs == NULL) || |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8388 | (nsMap == NULL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8389 | return (-1); |
| 8390 | |
| 8391 | *retNs = NULL; |
| 8392 | /* |
| 8393 | * Handle XML namespace. |
| 8394 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8395 | if (IS_STR_XML(ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8396 | /* |
| 8397 | * Insert XML namespace mapping. |
| 8398 | */ |
| 8399 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8400 | if (*retNs == NULL) |
| 8401 | return (-1); |
| 8402 | return (0); |
| 8403 | } |
| 8404 | /* |
| 8405 | * If the search should be done in ancestors only and no |
| 8406 | * @elem (the first ancestor) was specified, then skip the search. |
| 8407 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8408 | if ((XML_NSMAP_NOTEMPTY(*nsMap)) && |
| 8409 | (! (ancestorsOnly && (elem == NULL)))) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8410 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8411 | /* |
| 8412 | * Try to find an equal ns-name in in-scope ns-decls. |
| 8413 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8414 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8415 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8416 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8417 | * ancestorsOnly: This should be turned on to gain speed, |
| 8418 | * if one knows that the branch itself was already |
| 8419 | * ns-wellformed and no stale references existed. |
| 8420 | * I.e. it searches in the ancestor axis only. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8421 | */ |
| 8422 | ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) && |
| 8423 | /* Skip shadowed prefixes. */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8424 | (mi->shadowDepth == -1) && |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8425 | /* Skip xmlns="" or xmlns:foo="". */ |
| 8426 | ((mi->newNs->href != NULL) && |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8427 | (mi->newNs->href[0] != 0)) && |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8428 | /* Ensure a prefix if wanted. */ |
| 8429 | ((! prefixed) || (mi->newNs->prefix != NULL)) && |
| 8430 | /* Equal ns name */ |
| 8431 | ((mi->newNs->href == ns->href) || |
| 8432 | xmlStrEqual(mi->newNs->href, ns->href))) { |
| 8433 | /* Set the mapping. */ |
| 8434 | mi->oldNs = ns; |
| 8435 | *retNs = mi->newNs; |
| 8436 | return (0); |
| 8437 | } |
| 8438 | } |
| 8439 | } |
| 8440 | /* |
| 8441 | * No luck, the namespace is out of scope or shadowed. |
| 8442 | */ |
| 8443 | if (elem == NULL) { |
| 8444 | xmlNsPtr tmpns; |
| 8445 | |
| 8446 | /* |
| 8447 | * Store ns-decls in "oldNs" of the document-node. |
| 8448 | */ |
| 8449 | tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix); |
| 8450 | if (tmpns == NULL) |
| 8451 | return (-1); |
| 8452 | /* |
| 8453 | * Insert mapping. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8454 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8455 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8456 | tmpns, XML_TREE_NSMAP_DOC) == NULL) { |
| 8457 | xmlFreeNs(tmpns); |
| 8458 | return (-1); |
| 8459 | } |
| 8460 | *retNs = tmpns; |
| 8461 | } else { |
| 8462 | xmlNsPtr tmpns; |
| 8463 | |
| 8464 | tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href, |
| 8465 | ns->prefix, 0); |
| 8466 | if (tmpns == NULL) |
| 8467 | return (-1); |
| 8468 | |
| 8469 | if (*nsMap != NULL) { |
| 8470 | /* |
| 8471 | * Does it shadow ancestor ns-decls? |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8472 | */ |
| 8473 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8474 | if ((mi->depth < depth) && |
| 8475 | (mi->shadowDepth == -1) && |
| 8476 | ((ns->prefix == mi->newNs->prefix) || |
| 8477 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8478 | /* |
| 8479 | * Shadows. |
| 8480 | */ |
| 8481 | mi->shadowDepth = depth; |
| 8482 | break; |
| 8483 | } |
| 8484 | } |
| 8485 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8486 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8487 | xmlFreeNs(tmpns); |
| 8488 | return (-1); |
| 8489 | } |
| 8490 | *retNs = tmpns; |
| 8491 | } |
| 8492 | return (0); |
| 8493 | } |
| 8494 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8495 | typedef enum { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8496 | XML_DOM_RECONNS_REMOVEREDUND = 1<<0 |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8497 | } xmlDOMReconcileNSOptions; |
| 8498 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8499 | /* |
| 8500 | * xmlDOMWrapReconcileNamespaces: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8501 | * @ctxt: DOM wrapper context, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8502 | * @elem: the element-node |
| 8503 | * @options: option flags |
| 8504 | * |
| 8505 | * Ensures that ns-references point to ns-decls hold on element-nodes. |
| 8506 | * Ensures that the tree is namespace wellformed by creating additional |
| 8507 | * ns-decls where needed. Note that, since prefixes of already existent |
| 8508 | * ns-decls can be shadowed by this process, it could break QNames in |
| 8509 | * attribute values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8510 | * |
| 8511 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8512 | * |
| 8513 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8514 | */ |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8515 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8516 | int |
| 8517 | xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8518 | xmlNodePtr elem, |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8519 | int options) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8520 | { |
| 8521 | int depth = -1, adoptns = 0, parnsdone = 0; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8522 | xmlNsPtr ns, prevns; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8523 | xmlDocPtr doc; |
| 8524 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8525 | xmlNsMapPtr nsMap = NULL; |
| 8526 | xmlNsMapItemPtr /* topmi = NULL, */ mi; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8527 | /* @ancestorsOnly should be set by an option flag. */ |
| 8528 | int ancestorsOnly = 0; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8529 | int optRemoveRedundantNS = |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8530 | ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0; |
| 8531 | xmlNsPtr *listRedund = NULL; |
| 8532 | int sizeRedund = 0, nbRedund = 0, ret, i, j; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8533 | |
| 8534 | if ((elem == NULL) || (elem->doc == NULL) || |
| 8535 | (elem->type != XML_ELEMENT_NODE)) |
| 8536 | return (-1); |
| 8537 | |
| 8538 | doc = elem->doc; |
| 8539 | cur = elem; |
| 8540 | do { |
| 8541 | switch (cur->type) { |
| 8542 | case XML_ELEMENT_NODE: |
| 8543 | adoptns = 1; |
| 8544 | curElem = cur; |
| 8545 | depth++; |
| 8546 | /* |
| 8547 | * Namespace declarations. |
| 8548 | */ |
| 8549 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8550 | prevns = NULL; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8551 | ns = cur->nsDef; |
| 8552 | while (ns != NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8553 | if (! parnsdone) { |
| 8554 | if ((elem->parent) && |
| 8555 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8556 | /* |
| 8557 | * Gather ancestor in-scope ns-decls. |
| 8558 | */ |
| 8559 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8560 | elem->parent) == -1) |
| 8561 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8562 | } |
| 8563 | parnsdone = 1; |
| 8564 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8565 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8566 | /* |
| 8567 | * Lookup the ns ancestor-axis for equal ns-decls in scope. |
| 8568 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8569 | if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8570 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8571 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8572 | (mi->shadowDepth == -1) && |
| 8573 | ((ns->prefix == mi->newNs->prefix) || |
| 8574 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) && |
| 8575 | ((ns->href == mi->newNs->href) || |
| 8576 | xmlStrEqual(ns->href, mi->newNs->href))) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8577 | { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8578 | /* |
| 8579 | * A redundant ns-decl was found. |
| 8580 | * Add it to the list of redundant ns-decls. |
| 8581 | */ |
| 8582 | if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund, |
| 8583 | &sizeRedund, &nbRedund, ns, mi->newNs) == -1) |
| 8584 | goto internal_error; |
| 8585 | /* |
| 8586 | * Remove the ns-decl from the element-node. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8587 | */ |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8588 | if (prevns) |
| 8589 | prevns->next = ns->next; |
| 8590 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8591 | cur->nsDef = ns->next; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8592 | goto next_ns_decl; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8593 | } |
| 8594 | } |
| 8595 | } |
| 8596 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8597 | /* |
| 8598 | * Skip ns-references handling if the referenced |
| 8599 | * ns-decl is declared on the same element. |
| 8600 | */ |
| 8601 | if ((cur->ns != NULL) && adoptns && (cur->ns == ns)) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8602 | adoptns = 0; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8603 | /* |
| 8604 | * Does it shadow any ns-decl? |
| 8605 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8606 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
| 8607 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8608 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8609 | (mi->shadowDepth == -1) && |
| 8610 | ((ns->prefix == mi->newNs->prefix) || |
| 8611 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8612 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8613 | mi->shadowDepth = depth; |
| 8614 | } |
| 8615 | } |
| 8616 | } |
| 8617 | /* |
| 8618 | * Push mapping. |
| 8619 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8620 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8621 | depth) == NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8622 | goto internal_error; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8623 | |
| 8624 | prevns = ns; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8625 | next_ns_decl: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8626 | ns = ns->next; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8627 | } |
| 8628 | } |
| 8629 | if (! adoptns) |
| 8630 | goto ns_end; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8631 | /* No break on purpose. */ |
| 8632 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8633 | /* No ns, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8634 | if (cur->ns == NULL) |
| 8635 | goto ns_end; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8636 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8637 | if (! parnsdone) { |
| 8638 | if ((elem->parent) && |
| 8639 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8640 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8641 | elem->parent) == -1) |
| 8642 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8643 | } |
| 8644 | parnsdone = 1; |
| 8645 | } |
| 8646 | /* |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8647 | * Adjust the reference if this was a redundant ns-decl. |
| 8648 | */ |
| 8649 | if (listRedund) { |
| 8650 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8651 | if (cur->ns == listRedund[j]) { |
| 8652 | cur->ns = listRedund[++j]; |
| 8653 | break; |
| 8654 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8655 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8656 | } |
| 8657 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8658 | * Adopt ns-references. |
| 8659 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8660 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8661 | /* |
| 8662 | * Search for a mapping. |
| 8663 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8664 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8665 | if ((mi->shadowDepth == -1) && |
| 8666 | (cur->ns == mi->oldNs)) { |
| 8667 | |
| 8668 | cur->ns = mi->newNs; |
| 8669 | goto ns_end; |
| 8670 | } |
| 8671 | } |
| 8672 | } |
| 8673 | /* |
| 8674 | * Aquire a normalized ns-decl and add it to the map. |
| 8675 | */ |
| 8676 | if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem, |
| 8677 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8678 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8679 | ancestorsOnly, |
| 8680 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8681 | goto internal_error; |
| 8682 | cur->ns = ns; |
| 8683 | |
| 8684 | ns_end: |
| 8685 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8686 | (cur->properties != NULL)) { |
| 8687 | /* |
| 8688 | * Process attributes. |
| 8689 | */ |
| 8690 | cur = (xmlNodePtr) cur->properties; |
| 8691 | continue; |
| 8692 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8693 | break; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8694 | default: |
| 8695 | goto next_sibling; |
| 8696 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8697 | into_content: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8698 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8699 | (cur->children != NULL)) { |
| 8700 | /* |
| 8701 | * Process content of element-nodes only. |
| 8702 | */ |
| 8703 | cur = cur->children; |
| 8704 | continue; |
| 8705 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8706 | next_sibling: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8707 | if (cur == elem) |
| 8708 | break; |
| 8709 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8710 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8711 | /* |
| 8712 | * Pop mappings. |
| 8713 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8714 | while ((nsMap->last != NULL) && |
| 8715 | (nsMap->last->depth >= depth)) |
| 8716 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8717 | XML_NSMAP_POP(nsMap, mi) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8718 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8719 | /* |
| 8720 | * Unshadow. |
| 8721 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8722 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8723 | if (mi->shadowDepth >= depth) |
| 8724 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8725 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8726 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8727 | depth--; |
| 8728 | } |
| 8729 | if (cur->next != NULL) |
| 8730 | cur = cur->next; |
| 8731 | else { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8732 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 8733 | cur = cur->parent; |
| 8734 | goto into_content; |
| 8735 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8736 | cur = cur->parent; |
| 8737 | goto next_sibling; |
| 8738 | } |
| 8739 | } while (cur != NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8740 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8741 | ret = 0; |
| 8742 | goto exit; |
| 8743 | internal_error: |
| 8744 | ret = -1; |
| 8745 | exit: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8746 | if (listRedund) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8747 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8748 | xmlFreeNs(listRedund[j]); |
| 8749 | } |
| 8750 | xmlFree(listRedund); |
| 8751 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8752 | if (nsMap != NULL) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8753 | xmlDOMWrapNsMapFree(nsMap); |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8754 | return (ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8755 | } |
| 8756 | |
| 8757 | /* |
| 8758 | * xmlDOMWrapAdoptBranch: |
| 8759 | * @ctxt: the optional context for custom processing |
| 8760 | * @sourceDoc: the optional sourceDoc |
| 8761 | * @node: the element-node to start with |
| 8762 | * @destDoc: the destination doc for adoption |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8763 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8764 | * @options: option flags |
| 8765 | * |
| 8766 | * Ensures that ns-references point to @destDoc: either to |
| 8767 | * elements->nsDef entries if @destParent is given, or to |
| 8768 | * @destDoc->oldNs otherwise. |
| 8769 | * If @destParent is given, it ensures that the tree is namespace |
| 8770 | * wellformed by creating additional ns-decls where needed. |
| 8771 | * Note that, since prefixes of already existent ns-decls can be |
| 8772 | * shadowed by this process, it could break QNames in attribute |
| 8773 | * values or element content. |
| 8774 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8775 | * NOTE: This function was not intensively tested. |
| 8776 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8777 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8778 | */ |
| 8779 | static int |
| 8780 | xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt, |
| 8781 | xmlDocPtr sourceDoc, |
| 8782 | xmlNodePtr node, |
| 8783 | xmlDocPtr destDoc, |
| 8784 | xmlNodePtr destParent, |
| 8785 | int options ATTRIBUTE_UNUSED) |
| 8786 | { |
| 8787 | int ret = 0; |
| 8788 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8789 | xmlNsMapPtr nsMap = NULL; |
| 8790 | xmlNsMapItemPtr mi; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 8791 | xmlNsPtr ns = NULL; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8792 | int depth = -1, adoptStr = 1; |
| 8793 | /* gather @parent's ns-decls. */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8794 | int parnsdone; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8795 | /* @ancestorsOnly should be set per option. */ |
| 8796 | int ancestorsOnly = 0; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8797 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8798 | /* |
| 8799 | * Optimize string adoption for equal or none dicts. |
| 8800 | */ |
| 8801 | if ((sourceDoc != NULL) && |
| 8802 | (sourceDoc->dict == destDoc->dict)) |
| 8803 | adoptStr = 0; |
| 8804 | else |
| 8805 | adoptStr = 1; |
| 8806 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8807 | /* |
| 8808 | * Get the ns-map from the context if available. |
| 8809 | */ |
| 8810 | if (ctxt) |
| 8811 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 8812 | /* |
| 8813 | * Disable search for ns-decls in the parent-axis of the |
| 8814 | * desination element, if: |
| 8815 | * 1) there's no destination parent |
| 8816 | * 2) custom ns-reference handling is used |
| 8817 | */ |
| 8818 | if ((destParent == NULL) || |
| 8819 | (ctxt && ctxt->getNsForNodeFunc)) |
| 8820 | { |
| 8821 | parnsdone = 1; |
| 8822 | } else |
| 8823 | parnsdone = 0; |
| 8824 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8825 | cur = node; |
| 8826 | while (cur != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8827 | /* |
| 8828 | * Paranoid source-doc sanity check. |
| 8829 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8830 | if (cur->doc != sourceDoc) { |
| 8831 | /* |
| 8832 | * We'll assume XIncluded nodes if the doc differs. |
| 8833 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 8834 | * This here skips XIncluded nodes and tries to handle |
| 8835 | * broken sequences. |
| 8836 | */ |
| 8837 | if (cur->next == NULL) |
| 8838 | goto leave_node; |
| 8839 | do { |
| 8840 | cur = cur->next; |
| 8841 | if ((cur->type == XML_XINCLUDE_END) || |
| 8842 | (cur->doc == node->doc)) |
| 8843 | break; |
| 8844 | } while (cur->next != NULL); |
| 8845 | |
| 8846 | if (cur->doc != node->doc) |
| 8847 | goto leave_node; |
| 8848 | } |
| 8849 | cur->doc = destDoc; |
| 8850 | switch (cur->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8851 | case XML_XINCLUDE_START: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8852 | case XML_XINCLUDE_END: |
| 8853 | /* |
| 8854 | * TODO |
| 8855 | */ |
| 8856 | return (-1); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8857 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8858 | curElem = cur; |
| 8859 | depth++; |
| 8860 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8861 | * Namespace declarations. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8862 | * - ns->href and ns->prefix are never in the dict, so |
| 8863 | * we need not move the values over to the destination dict. |
| 8864 | * - Note that for custom handling of ns-references, |
| 8865 | * the ns-decls need not be stored in the ns-map, |
| 8866 | * since they won't be referenced by node->ns. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8867 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8868 | if ((cur->nsDef) && |
| 8869 | ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL))) |
| 8870 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8871 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8872 | /* |
| 8873 | * Gather @parent's in-scope ns-decls. |
| 8874 | */ |
| 8875 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8876 | destParent) == -1) |
| 8877 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8878 | parnsdone = 1; |
| 8879 | } |
| 8880 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8881 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8882 | * NOTE: ns->prefix and ns->href are never in the dict. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8883 | * XML_TREE_ADOPT_STR(ns->prefix) |
| 8884 | * XML_TREE_ADOPT_STR(ns->href) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8885 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8886 | /* |
| 8887 | * Does it shadow any ns-decl? |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8888 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8889 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8890 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8891 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8892 | (mi->shadowDepth == -1) && |
| 8893 | ((ns->prefix == mi->newNs->prefix) || |
| 8894 | xmlStrEqual(ns->prefix, |
| 8895 | mi->newNs->prefix))) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8896 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8897 | mi->shadowDepth = depth; |
| 8898 | } |
| 8899 | } |
| 8900 | } |
| 8901 | /* |
| 8902 | * Push mapping. |
| 8903 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8904 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8905 | ns, ns, depth) == NULL) |
| 8906 | goto internal_error; |
| 8907 | } |
| 8908 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8909 | /* No break on purpose. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8910 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8911 | /* No namespace, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8912 | if (cur->ns == NULL) |
| 8913 | goto ns_end; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8914 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8915 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8916 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8917 | destParent) == -1) |
| 8918 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8919 | parnsdone = 1; |
| 8920 | } |
| 8921 | /* |
| 8922 | * Adopt ns-references. |
| 8923 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8924 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8925 | /* |
| 8926 | * Search for a mapping. |
| 8927 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8928 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8929 | if ((mi->shadowDepth == -1) && |
| 8930 | (cur->ns == mi->oldNs)) { |
| 8931 | |
| 8932 | cur->ns = mi->newNs; |
| 8933 | goto ns_end; |
| 8934 | } |
| 8935 | } |
| 8936 | } |
| 8937 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8938 | * No matching namespace in scope. We need a new one. |
| 8939 | */ |
| 8940 | if ((ctxt) && (ctxt->getNsForNodeFunc)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8941 | /* |
| 8942 | * User-defined behaviour. |
| 8943 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8944 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 8945 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8946 | /* |
| 8947 | * Insert mapping if ns is available; it's the users fault |
| 8948 | * if not. |
| 8949 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8950 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8951 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8952 | goto internal_error; |
| 8953 | cur->ns = ns; |
| 8954 | } else { |
| 8955 | /* |
| 8956 | * Aquire a normalized ns-decl and add it to the map. |
| 8957 | */ |
| 8958 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8959 | /* ns-decls on curElem or on destDoc->oldNs */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8960 | destParent ? curElem : NULL, |
| 8961 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8962 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8963 | ancestorsOnly, |
| 8964 | /* ns-decls must be prefixed for attributes. */ |
| 8965 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8966 | goto internal_error; |
| 8967 | cur->ns = ns; |
| 8968 | } |
| 8969 | ns_end: |
| 8970 | /* |
| 8971 | * Further node properties. |
| 8972 | * TODO: Is this all? |
| 8973 | */ |
| 8974 | XML_TREE_ADOPT_STR(cur->name) |
| 8975 | if (cur->type == XML_ELEMENT_NODE) { |
| 8976 | cur->psvi = NULL; |
| 8977 | cur->line = 0; |
| 8978 | cur->extra = 0; |
| 8979 | /* |
| 8980 | * Walk attributes. |
| 8981 | */ |
| 8982 | if (cur->properties != NULL) { |
| 8983 | /* |
| 8984 | * Process first attribute node. |
| 8985 | */ |
| 8986 | cur = (xmlNodePtr) cur->properties; |
| 8987 | continue; |
| 8988 | } |
| 8989 | } else { |
| 8990 | /* |
| 8991 | * Attributes. |
| 8992 | */ |
| 8993 | if ((sourceDoc != NULL) && |
| 8994 | (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID)) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8995 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8996 | xmlRemoveID(sourceDoc, (xmlAttrPtr) cur); |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8997 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8998 | ((xmlAttrPtr) cur)->atype = 0; |
| 8999 | ((xmlAttrPtr) cur)->psvi = NULL; |
| 9000 | } |
| 9001 | break; |
| 9002 | case XML_TEXT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9003 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9004 | /* |
| 9005 | * This puts the content in the dest dict, only if |
| 9006 | * it was previously in the source dict. |
| 9007 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9008 | XML_TREE_ADOPT_STR_2(cur->content) |
| 9009 | goto leave_node; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9010 | case XML_ENTITY_REF_NODE: |
| 9011 | /* |
| 9012 | * Remove reference to the entitity-node. |
| 9013 | */ |
| 9014 | cur->content = NULL; |
| 9015 | cur->children = NULL; |
| 9016 | cur->last = NULL; |
| 9017 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9018 | xmlEntityPtr ent; |
| 9019 | /* |
| 9020 | * Assign new entity-node if available. |
| 9021 | */ |
| 9022 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9023 | if (ent != NULL) { |
| 9024 | cur->content = ent->content; |
| 9025 | cur->children = (xmlNodePtr) ent; |
| 9026 | cur->last = (xmlNodePtr) ent; |
| 9027 | } |
| 9028 | } |
| 9029 | goto leave_node; |
| 9030 | case XML_PI_NODE: |
| 9031 | XML_TREE_ADOPT_STR(cur->name) |
| 9032 | XML_TREE_ADOPT_STR_2(cur->content) |
| 9033 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9034 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9035 | break; |
| 9036 | default: |
| 9037 | goto internal_error; |
| 9038 | } |
| 9039 | /* |
| 9040 | * Walk the tree. |
| 9041 | */ |
| 9042 | if (cur->children != NULL) { |
| 9043 | cur = cur->children; |
| 9044 | continue; |
| 9045 | } |
| 9046 | |
| 9047 | leave_node: |
| 9048 | if (cur == node) |
| 9049 | break; |
| 9050 | if ((cur->type == XML_ELEMENT_NODE) || |
| 9051 | (cur->type == XML_XINCLUDE_START) || |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9052 | (cur->type == XML_XINCLUDE_END)) |
| 9053 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9054 | /* |
| 9055 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 9056 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9057 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9058 | /* |
| 9059 | * Pop mappings. |
| 9060 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9061 | while ((nsMap->last != NULL) && |
| 9062 | (nsMap->last->depth >= depth)) |
| 9063 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9064 | XML_NSMAP_POP(nsMap, mi) |
| 9065 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9066 | /* |
| 9067 | * Unshadow. |
| 9068 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9069 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9070 | if (mi->shadowDepth >= depth) |
| 9071 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9072 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9073 | } |
| 9074 | depth--; |
| 9075 | } |
| 9076 | if (cur->next != NULL) |
| 9077 | cur = cur->next; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9078 | else if ((cur->type == XML_ATTRIBUTE_NODE) && |
| 9079 | (cur->parent->children != NULL)) |
| 9080 | { |
| 9081 | cur = cur->parent->children; |
| 9082 | } else { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9083 | cur = cur->parent; |
| 9084 | goto leave_node; |
| 9085 | } |
| 9086 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9087 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9088 | goto exit; |
| 9089 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9090 | internal_error: |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9091 | ret = -1; |
| 9092 | |
| 9093 | exit: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9094 | /* |
| 9095 | * Cleanup. |
| 9096 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9097 | if (nsMap != NULL) { |
| 9098 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 9099 | /* |
| 9100 | * Just cleanup the map but don't free. |
| 9101 | */ |
| 9102 | if (nsMap->first) { |
| 9103 | if (nsMap->pool) |
| 9104 | nsMap->last->next = nsMap->pool; |
| 9105 | nsMap->pool = nsMap->first; |
| 9106 | nsMap->first = NULL; |
| 9107 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9108 | } else |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9109 | xmlDOMWrapNsMapFree(nsMap); |
| 9110 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9111 | return(ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9112 | } |
| 9113 | |
| 9114 | /* |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9115 | * xmlDOMWrapCloneNode: |
| 9116 | * @ctxt: the optional context for custom processing |
| 9117 | * @sourceDoc: the optional sourceDoc |
| 9118 | * @node: the node to start with |
| 9119 | * @resNode: the clone of the given @node |
| 9120 | * @destDoc: the destination doc |
| 9121 | * @destParent: the optional new parent of @node in @destDoc |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 9122 | * @deep: descend into child if set |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9123 | * @options: option flags |
| 9124 | * |
| 9125 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 9126 | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9127 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used. |
| 9128 | * This is the case when you don't know already where the cloned branch |
| 9129 | * will be added to. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9130 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9131 | * If @destParent is given, it ensures that the tree is namespace |
| 9132 | * wellformed by creating additional ns-decls where needed. |
| 9133 | * Note that, since prefixes of already existent ns-decls can be |
| 9134 | * shadowed by this process, it could break QNames in attribute |
| 9135 | * values or element content. |
| 9136 | * TODO: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9137 | * 1) What to do with XInclude? Currently this returns an error for XInclude. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9138 | * |
| 9139 | * Returns 0 if the operation succeeded, |
| 9140 | * 1 if a node of unsupported (or not yet supported) type was given, |
| 9141 | * -1 on API/internal errors. |
| 9142 | */ |
| 9143 | |
| 9144 | int |
| 9145 | xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt, |
| 9146 | xmlDocPtr sourceDoc, |
| 9147 | xmlNodePtr node, |
| 9148 | xmlNodePtr *resNode, |
| 9149 | xmlDocPtr destDoc, |
| 9150 | xmlNodePtr destParent, |
| 9151 | int deep, |
| 9152 | int options ATTRIBUTE_UNUSED) |
| 9153 | { |
| 9154 | int ret = 0; |
| 9155 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9156 | xmlNsMapPtr nsMap = NULL; |
| 9157 | xmlNsMapItemPtr mi; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9158 | xmlNsPtr ns; |
| 9159 | int depth = -1; |
| 9160 | /* int adoptStr = 1; */ |
| 9161 | /* gather @parent's ns-decls. */ |
| 9162 | int parnsdone = 0; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9163 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9164 | * @ancestorsOnly: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9165 | * TODO: @ancestorsOnly should be set per option. |
| 9166 | * |
| 9167 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9168 | int ancestorsOnly = 0; |
| 9169 | xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9170 | xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL; |
| 9171 | xmlDictPtr dict; /* The destination dict */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9172 | |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9173 | if ((node == NULL) || (resNode == NULL) || (destDoc == NULL)) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9174 | return(-1); |
| 9175 | /* |
| 9176 | * TODO: Initially we support only element-nodes. |
| 9177 | */ |
| 9178 | if (node->type != XML_ELEMENT_NODE) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9179 | return(1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9180 | /* |
| 9181 | * Check node->doc sanity. |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9182 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9183 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 9184 | (node->doc != sourceDoc)) { |
| 9185 | /* |
| 9186 | * Might be an XIncluded node. |
| 9187 | */ |
| 9188 | return (-1); |
| 9189 | } |
| 9190 | if (sourceDoc == NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9191 | sourceDoc = node->doc; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9192 | if (sourceDoc == NULL) |
| 9193 | return (-1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9194 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9195 | dict = destDoc->dict; |
| 9196 | /* |
| 9197 | * Reuse the namespace map of the context. |
| 9198 | */ |
| 9199 | if (ctxt) |
| 9200 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 9201 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9202 | *resNode = NULL; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9203 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9204 | cur = node; |
| 9205 | while (cur != NULL) { |
| 9206 | if (cur->doc != sourceDoc) { |
| 9207 | /* |
| 9208 | * We'll assume XIncluded nodes if the doc differs. |
| 9209 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 9210 | * TODO: This here returns -1 in this case. |
| 9211 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9212 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9213 | } |
| 9214 | /* |
| 9215 | * Create a new node. |
| 9216 | */ |
| 9217 | switch (cur->type) { |
| 9218 | case XML_XINCLUDE_START: |
| 9219 | case XML_XINCLUDE_END: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9220 | /* |
| 9221 | * TODO: What to do with XInclude? |
| 9222 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9223 | goto internal_error; |
| 9224 | break; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9225 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9226 | case XML_TEXT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9227 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9228 | case XML_COMMENT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9229 | case XML_PI_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9230 | case XML_DOCUMENT_FRAG_NODE: |
| 9231 | case XML_ENTITY_REF_NODE: |
| 9232 | case XML_ENTITY_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9233 | /* |
| 9234 | * Nodes of xmlNode structure. |
| 9235 | */ |
| 9236 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 9237 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9238 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9239 | goto internal_error; |
| 9240 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9241 | memset(clone, 0, sizeof(xmlNode)); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9242 | /* |
| 9243 | * Set hierachical links. |
| 9244 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9245 | if (resultClone != NULL) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9246 | clone->parent = parentClone; |
| 9247 | if (prevClone) { |
| 9248 | prevClone->next = clone; |
| 9249 | clone->prev = prevClone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9250 | } else |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9251 | parentClone->children = clone; |
| 9252 | } else |
| 9253 | resultClone = clone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9254 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9255 | break; |
| 9256 | case XML_ATTRIBUTE_NODE: |
| 9257 | /* |
| 9258 | * Attributes (xmlAttr). |
| 9259 | */ |
| 9260 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr)); |
| 9261 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9262 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9263 | goto internal_error; |
| 9264 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9265 | memset(clone, 0, sizeof(xmlAttr)); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9266 | /* |
| 9267 | * Set hierachical links. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9268 | * TODO: Change this to add to the end of attributes. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9269 | */ |
| 9270 | if (resultClone != NULL) { |
| 9271 | clone->parent = parentClone; |
| 9272 | if (prevClone) { |
| 9273 | prevClone->next = clone; |
| 9274 | clone->prev = prevClone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9275 | } else |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9276 | parentClone->properties = (xmlAttrPtr) clone; |
| 9277 | } else |
| 9278 | resultClone = clone; |
| 9279 | break; |
| 9280 | default: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9281 | /* |
| 9282 | * TODO QUESTION: Any other nodes expected? |
| 9283 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9284 | goto internal_error; |
| 9285 | } |
| 9286 | |
| 9287 | clone->type = cur->type; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9288 | clone->doc = destDoc; |
| 9289 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9290 | /* |
| 9291 | * Clone the name of the node if any. |
| 9292 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9293 | if (cur->name == xmlStringText) |
| 9294 | clone->name = xmlStringText; |
| 9295 | else if (cur->name == xmlStringTextNoenc) |
| 9296 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9297 | * NOTE: Although xmlStringTextNoenc is never assigned to a node |
| 9298 | * in tree.c, it might be set in Libxslt via |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9299 | * "xsl:disable-output-escaping". |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9300 | */ |
| 9301 | clone->name = xmlStringTextNoenc; |
| 9302 | else if (cur->name == xmlStringComment) |
| 9303 | clone->name = xmlStringComment; |
| 9304 | else if (cur->name != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9305 | DICT_CONST_COPY(cur->name, clone->name); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9306 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9307 | |
| 9308 | switch (cur->type) { |
| 9309 | case XML_XINCLUDE_START: |
| 9310 | case XML_XINCLUDE_END: |
| 9311 | /* |
| 9312 | * TODO |
| 9313 | */ |
| 9314 | return (-1); |
| 9315 | case XML_ELEMENT_NODE: |
| 9316 | curElem = cur; |
| 9317 | depth++; |
| 9318 | /* |
| 9319 | * Namespace declarations. |
| 9320 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9321 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9322 | if (! parnsdone) { |
| 9323 | if (destParent && (ctxt == NULL)) { |
| 9324 | /* |
| 9325 | * Gather @parent's in-scope ns-decls. |
| 9326 | */ |
| 9327 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 9328 | destParent) == -1) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9329 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9330 | } |
| 9331 | parnsdone = 1; |
| 9332 | } |
| 9333 | /* |
| 9334 | * Clone namespace declarations. |
| 9335 | */ |
| 9336 | cloneNsDefSlot = &(clone->nsDef); |
| 9337 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 9338 | /* |
| 9339 | * Create a new xmlNs. |
| 9340 | */ |
| 9341 | cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 9342 | if (cloneNs == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9343 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): " |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9344 | "allocating namespace"); |
| 9345 | return(-1); |
| 9346 | } |
| 9347 | memset(cloneNs, 0, sizeof(xmlNs)); |
| 9348 | cloneNs->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9349 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9350 | if (ns->href != NULL) |
| 9351 | cloneNs->href = xmlStrdup(ns->href); |
| 9352 | if (ns->prefix != NULL) |
| 9353 | cloneNs->prefix = xmlStrdup(ns->prefix); |
| 9354 | |
| 9355 | *cloneNsDefSlot = cloneNs; |
| 9356 | cloneNsDefSlot = &(cloneNs->next); |
| 9357 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9358 | /* |
| 9359 | * Note that for custom handling of ns-references, |
| 9360 | * the ns-decls need not be stored in the ns-map, |
| 9361 | * since they won't be referenced by node->ns. |
| 9362 | */ |
| 9363 | if ((ctxt == NULL) || |
| 9364 | (ctxt->getNsForNodeFunc == NULL)) |
| 9365 | { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9366 | /* |
| 9367 | * Does it shadow any ns-decl? |
| 9368 | */ |
| 9369 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9370 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9371 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 9372 | (mi->shadowDepth == -1) && |
| 9373 | ((ns->prefix == mi->newNs->prefix) || |
| 9374 | xmlStrEqual(ns->prefix, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9375 | mi->newNs->prefix))) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9376 | /* |
| 9377 | * Mark as shadowed at the current |
| 9378 | * depth. |
| 9379 | */ |
| 9380 | mi->shadowDepth = depth; |
| 9381 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9382 | } |
| 9383 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9384 | /* |
| 9385 | * Push mapping. |
| 9386 | */ |
| 9387 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
| 9388 | ns, cloneNs, depth) == NULL) |
| 9389 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9390 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9391 | } |
| 9392 | } |
| 9393 | /* cur->ns will be processed further down. */ |
| 9394 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9395 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9396 | /* IDs will be processed further down. */ |
| 9397 | /* cur->ns will be processed further down. */ |
| 9398 | break; |
| 9399 | case XML_TEXT_NODE: |
| 9400 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9401 | /* |
| 9402 | * Note that this will also cover the values of attributes. |
| 9403 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9404 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9405 | goto leave_node; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9406 | case XML_ENTITY_NODE: |
| 9407 | /* TODO: What to do here? */ |
| 9408 | goto leave_node; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9409 | case XML_ENTITY_REF_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9410 | if (sourceDoc != destDoc) { |
| 9411 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9412 | xmlEntityPtr ent; |
| 9413 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9414 | * Different doc: Assign new entity-node if available. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9415 | */ |
| 9416 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9417 | if (ent != NULL) { |
| 9418 | clone->content = ent->content; |
| 9419 | clone->children = (xmlNodePtr) ent; |
| 9420 | clone->last = (xmlNodePtr) ent; |
| 9421 | } |
| 9422 | } |
| 9423 | } else { |
| 9424 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9425 | * Same doc: Use the current node's entity declaration |
| 9426 | * and value. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9427 | */ |
| 9428 | clone->content = cur->content; |
| 9429 | clone->children = cur->children; |
| 9430 | clone->last = cur->last; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9431 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9432 | goto leave_node; |
| 9433 | case XML_PI_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9434 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9435 | goto leave_node; |
| 9436 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9437 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9438 | goto leave_node; |
| 9439 | default: |
| 9440 | goto internal_error; |
| 9441 | } |
| 9442 | |
| 9443 | if (cur->ns == NULL) |
| 9444 | goto end_ns_reference; |
| 9445 | |
| 9446 | /* handle_ns_reference: */ |
| 9447 | /* |
| 9448 | ** The following will take care of references to ns-decls ******** |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9449 | ** and is intended only for element- and attribute-nodes. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9450 | ** |
| 9451 | */ |
| 9452 | if (! parnsdone) { |
| 9453 | if (destParent && (ctxt == NULL)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9454 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9455 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9456 | } |
| 9457 | parnsdone = 1; |
| 9458 | } |
| 9459 | /* |
| 9460 | * Adopt ns-references. |
| 9461 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9462 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9463 | /* |
| 9464 | * Search for a mapping. |
| 9465 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9466 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9467 | if ((mi->shadowDepth == -1) && |
| 9468 | (cur->ns == mi->oldNs)) { |
| 9469 | /* |
| 9470 | * This is the nice case: a mapping was found. |
| 9471 | */ |
| 9472 | clone->ns = mi->newNs; |
| 9473 | goto end_ns_reference; |
| 9474 | } |
| 9475 | } |
| 9476 | } |
| 9477 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9478 | * No matching namespace in scope. We need a new one. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9479 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9480 | if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9481 | /* |
| 9482 | * User-defined behaviour. |
| 9483 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9484 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 9485 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9486 | /* |
| 9487 | * Add user's mapping. |
| 9488 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9489 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9490 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
| 9491 | goto internal_error; |
| 9492 | clone->ns = ns; |
| 9493 | } else { |
| 9494 | /* |
| 9495 | * Aquire a normalized ns-decl and add it to the map. |
| 9496 | */ |
| 9497 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9498 | /* ns-decls on curElem or on destDoc->oldNs */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9499 | destParent ? curElem : NULL, |
| 9500 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9501 | &nsMap, depth, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9502 | /* if we need to search only in the ancestor-axis */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9503 | ancestorsOnly, |
| 9504 | /* ns-decls must be prefixed for attributes. */ |
| 9505 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 9506 | goto internal_error; |
| 9507 | clone->ns = ns; |
| 9508 | } |
| 9509 | |
| 9510 | end_ns_reference: |
| 9511 | |
| 9512 | /* |
| 9513 | * Some post-processing. |
| 9514 | * |
| 9515 | * Handle ID attributes. |
| 9516 | */ |
| 9517 | if ((clone->type == XML_ATTRIBUTE_NODE) && |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9518 | (clone->parent != NULL)) |
| 9519 | { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9520 | if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9521 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9522 | xmlChar *idVal; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9523 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9524 | idVal = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 9525 | if (idVal != NULL) { |
| 9526 | if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) { |
| 9527 | /* TODO: error message. */ |
| 9528 | xmlFree(idVal); |
| 9529 | goto internal_error; |
| 9530 | } |
| 9531 | xmlFree(idVal); |
| 9532 | } |
| 9533 | } |
| 9534 | } |
| 9535 | /* |
| 9536 | ** |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9537 | ** The following will traverse the tree ************************** |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9538 | ** |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9539 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9540 | * Walk the element's attributes before descending into child-nodes. |
| 9541 | */ |
| 9542 | if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) { |
| 9543 | prevClone = NULL; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9544 | parentClone = clone; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9545 | cur = (xmlNodePtr) cur->properties; |
| 9546 | continue; |
| 9547 | } |
| 9548 | into_content: |
| 9549 | /* |
| 9550 | * Descend into child-nodes. |
| 9551 | */ |
| 9552 | if (cur->children != NULL) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9553 | if (deep || (cur->type == XML_ATTRIBUTE_NODE)) { |
| 9554 | prevClone = NULL; |
| 9555 | parentClone = clone; |
| 9556 | cur = cur->children; |
| 9557 | continue; |
| 9558 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9559 | } |
| 9560 | |
| 9561 | leave_node: |
| 9562 | /* |
| 9563 | * At this point we are done with the node, its content |
| 9564 | * and an element-nodes's attribute-nodes. |
| 9565 | */ |
| 9566 | if (cur == node) |
| 9567 | break; |
| 9568 | if ((cur->type == XML_ELEMENT_NODE) || |
| 9569 | (cur->type == XML_XINCLUDE_START) || |
| 9570 | (cur->type == XML_XINCLUDE_END)) { |
| 9571 | /* |
| 9572 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 9573 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9574 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9575 | /* |
| 9576 | * Pop mappings. |
| 9577 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9578 | while ((nsMap->last != NULL) && |
| 9579 | (nsMap->last->depth >= depth)) |
| 9580 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9581 | XML_NSMAP_POP(nsMap, mi) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9582 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9583 | /* |
| 9584 | * Unshadow. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9585 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9586 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9587 | if (mi->shadowDepth >= depth) |
| 9588 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9589 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9590 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9591 | depth--; |
| 9592 | } |
| 9593 | if (cur->next != NULL) { |
| 9594 | prevClone = clone; |
| 9595 | cur = cur->next; |
| 9596 | } else if (cur->type != XML_ATTRIBUTE_NODE) { |
| 9597 | /* |
| 9598 | * Set clone->last. |
| 9599 | */ |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9600 | if (clone->parent != NULL) |
| 9601 | clone->parent->last = clone; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9602 | clone = clone->parent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9603 | parentClone = clone->parent; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9604 | /* |
| 9605 | * Process parent --> next; |
| 9606 | */ |
| 9607 | cur = cur->parent; |
| 9608 | goto leave_node; |
| 9609 | } else { |
| 9610 | /* This is for attributes only. */ |
| 9611 | clone = clone->parent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9612 | parentClone = clone->parent; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9613 | /* |
| 9614 | * Process parent-element --> children. |
| 9615 | */ |
| 9616 | cur = cur->parent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9617 | goto into_content; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9618 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9619 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9620 | goto exit; |
| 9621 | |
| 9622 | internal_error: |
| 9623 | ret = -1; |
| 9624 | |
| 9625 | exit: |
| 9626 | /* |
| 9627 | * Cleanup. |
| 9628 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9629 | if (nsMap != NULL) { |
| 9630 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 9631 | /* |
| 9632 | * Just cleanup the map but don't free. |
| 9633 | */ |
| 9634 | if (nsMap->first) { |
| 9635 | if (nsMap->pool) |
| 9636 | nsMap->last->next = nsMap->pool; |
| 9637 | nsMap->pool = nsMap->first; |
| 9638 | nsMap->first = NULL; |
| 9639 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9640 | } else |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9641 | xmlDOMWrapNsMapFree(nsMap); |
| 9642 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9643 | /* |
| 9644 | * TODO: Should we try a cleanup of the cloned node in case of a |
| 9645 | * fatal error? |
| 9646 | */ |
| 9647 | *resNode = resultClone; |
| 9648 | return (ret); |
| 9649 | } |
| 9650 | |
| 9651 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9652 | * xmlDOMWrapAdoptAttr: |
| 9653 | * @ctxt: the optional context for custom processing |
| 9654 | * @sourceDoc: the optional source document of attr |
| 9655 | * @attr: the attribute-node to be adopted |
| 9656 | * @destDoc: the destination doc for adoption |
| 9657 | * @destParent: the optional new parent of @attr in @destDoc |
| 9658 | * @options: option flags |
| 9659 | * |
| 9660 | * @attr is adopted by @destDoc. |
| 9661 | * Ensures that ns-references point to @destDoc: either to |
| 9662 | * elements->nsDef entries if @destParent is given, or to |
| 9663 | * @destDoc->oldNs otherwise. |
| 9664 | * |
| 9665 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 9666 | */ |
| 9667 | static int |
| 9668 | xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt, |
| 9669 | xmlDocPtr sourceDoc, |
| 9670 | xmlAttrPtr attr, |
| 9671 | xmlDocPtr destDoc, |
| 9672 | xmlNodePtr destParent, |
| 9673 | int options ATTRIBUTE_UNUSED) |
| 9674 | { |
| 9675 | xmlNodePtr cur; |
| 9676 | int adoptStr = 1; |
| 9677 | |
| 9678 | if ((attr == NULL) || (destDoc == NULL)) |
| 9679 | return (-1); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9680 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9681 | attr->doc = destDoc; |
| 9682 | if (attr->ns != NULL) { |
| 9683 | xmlNsPtr ns = NULL; |
| 9684 | |
| 9685 | if (ctxt != NULL) { |
| 9686 | /* TODO: User defined. */ |
| 9687 | } |
| 9688 | /* XML Namespace. */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9689 | if (IS_STR_XML(attr->ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9690 | ns = xmlTreeEnsureXMLDecl(destDoc); |
| 9691 | } else if (destParent == NULL) { |
| 9692 | /* |
| 9693 | * Store in @destDoc->oldNs. |
| 9694 | */ |
| 9695 | ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix); |
| 9696 | } else { |
| 9697 | /* |
| 9698 | * Declare on @destParent. |
| 9699 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9700 | if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9701 | &ns, 1) == -1) |
| 9702 | goto internal_error; |
| 9703 | if (ns == NULL) { |
| 9704 | ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent, |
| 9705 | attr->ns->href, attr->ns->prefix, 1); |
| 9706 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9707 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9708 | if (ns == NULL) |
| 9709 | goto internal_error; |
| 9710 | attr->ns = ns; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9711 | } |
| 9712 | |
| 9713 | XML_TREE_ADOPT_STR(attr->name); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9714 | attr->atype = 0; |
| 9715 | attr->psvi = NULL; |
| 9716 | /* |
| 9717 | * Walk content. |
| 9718 | */ |
| 9719 | if (attr->children == NULL) |
| 9720 | return (0); |
| 9721 | cur = attr->children; |
| 9722 | while (cur != NULL) { |
| 9723 | cur->doc = destDoc; |
| 9724 | switch (cur->type) { |
| 9725 | case XML_TEXT_NODE: |
| 9726 | case XML_CDATA_SECTION_NODE: |
| 9727 | XML_TREE_ADOPT_STR_2(cur->content) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9728 | break; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9729 | case XML_ENTITY_REF_NODE: |
| 9730 | /* |
| 9731 | * Remove reference to the entitity-node. |
| 9732 | */ |
| 9733 | cur->content = NULL; |
| 9734 | cur->children = NULL; |
| 9735 | cur->last = NULL; |
| 9736 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9737 | xmlEntityPtr ent; |
| 9738 | /* |
| 9739 | * Assign new entity-node if available. |
| 9740 | */ |
| 9741 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9742 | if (ent != NULL) { |
| 9743 | cur->content = ent->content; |
| 9744 | cur->children = (xmlNodePtr) ent; |
| 9745 | cur->last = (xmlNodePtr) ent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9746 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9747 | } |
| 9748 | break; |
| 9749 | default: |
| 9750 | break; |
| 9751 | } |
| 9752 | if (cur->children != NULL) { |
| 9753 | cur = cur->children; |
| 9754 | continue; |
| 9755 | } |
| 9756 | next_sibling: |
| 9757 | if (cur == (xmlNodePtr) attr) |
| 9758 | break; |
| 9759 | if (cur->next != NULL) |
| 9760 | cur = cur->next; |
| 9761 | else { |
| 9762 | cur = cur->parent; |
| 9763 | goto next_sibling; |
| 9764 | } |
| 9765 | } |
| 9766 | return (0); |
| 9767 | internal_error: |
| 9768 | return (-1); |
| 9769 | } |
| 9770 | |
| 9771 | /* |
| 9772 | * xmlDOMWrapAdoptNode: |
| 9773 | * @ctxt: the optional context for custom processing |
| 9774 | * @sourceDoc: the optional sourceDoc |
| 9775 | * @node: the node to start with |
| 9776 | * @destDoc: the destination doc |
Kasimier T. Buchcik | 4d9c948 | 2005-06-27 15:04:46 +0000 | [diff] [blame] | 9777 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9778 | * @options: option flags |
| 9779 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9780 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 9781 | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
| 9782 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used |
| 9783 | * This is the case when you have an unliked node and just want to move it |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9784 | * to the context of |
| 9785 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9786 | * If @destParent is given, it ensures that the tree is namespace |
| 9787 | * wellformed by creating additional ns-decls where needed. |
| 9788 | * Note that, since prefixes of already existent ns-decls can be |
| 9789 | * shadowed by this process, it could break QNames in attribute |
| 9790 | * values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9791 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9792 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9793 | * Returns 0 if the operation succeeded, |
| 9794 | * 1 if a node of unsupported type was given, |
| 9795 | * 2 if a node of not yet supported type was given and |
| 9796 | * -1 on API/internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9797 | */ |
| 9798 | int |
| 9799 | xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt, |
| 9800 | xmlDocPtr sourceDoc, |
| 9801 | xmlNodePtr node, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9802 | xmlDocPtr destDoc, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9803 | xmlNodePtr destParent, |
| 9804 | int options) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9805 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9806 | if ((node == NULL) || (destDoc == NULL) || |
| 9807 | ((destParent != NULL) && (destParent->doc != destDoc))) |
| 9808 | return(-1); |
| 9809 | /* |
| 9810 | * Check node->doc sanity. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9811 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9812 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 9813 | (node->doc != sourceDoc)) { |
| 9814 | /* |
| 9815 | * Might be an XIncluded node. |
| 9816 | */ |
| 9817 | return (-1); |
| 9818 | } |
| 9819 | if (sourceDoc == NULL) |
| 9820 | sourceDoc = node->doc; |
| 9821 | if (sourceDoc == destDoc) |
| 9822 | return (-1); |
| 9823 | switch (node->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9824 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9825 | case XML_ATTRIBUTE_NODE: |
| 9826 | case XML_TEXT_NODE: |
| 9827 | case XML_CDATA_SECTION_NODE: |
| 9828 | case XML_ENTITY_REF_NODE: |
| 9829 | case XML_PI_NODE: |
| 9830 | case XML_COMMENT_NODE: |
| 9831 | break; |
| 9832 | case XML_DOCUMENT_FRAG_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9833 | /* TODO: Support document-fragment-nodes. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9834 | return (2); |
| 9835 | default: |
| 9836 | return (1); |
| 9837 | } |
| 9838 | /* |
| 9839 | * Unlink only if @node was not already added to @destParent. |
| 9840 | */ |
| 9841 | if ((node->parent != NULL) && (destParent != node->parent)) |
| 9842 | xmlUnlinkNode(node); |
| 9843 | |
| 9844 | if (node->type == XML_ELEMENT_NODE) { |
| 9845 | return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node, |
| 9846 | destDoc, destParent, options)); |
| 9847 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
| 9848 | return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc, |
| 9849 | (xmlAttrPtr) node, destDoc, destParent, options)); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9850 | } else { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9851 | xmlNodePtr cur = node; |
| 9852 | int adoptStr = 1; |
| 9853 | |
| 9854 | cur->doc = destDoc; |
| 9855 | /* |
| 9856 | * Optimize string adoption. |
| 9857 | */ |
| 9858 | if ((sourceDoc != NULL) && |
| 9859 | (sourceDoc->dict == destDoc->dict)) |
| 9860 | adoptStr = 0; |
| 9861 | switch (node->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9862 | case XML_TEXT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9863 | case XML_CDATA_SECTION_NODE: |
| 9864 | XML_TREE_ADOPT_STR_2(node->content) |
| 9865 | break; |
| 9866 | case XML_ENTITY_REF_NODE: |
| 9867 | /* |
| 9868 | * Remove reference to the entitity-node. |
| 9869 | */ |
| 9870 | node->content = NULL; |
| 9871 | node->children = NULL; |
| 9872 | node->last = NULL; |
| 9873 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9874 | xmlEntityPtr ent; |
| 9875 | /* |
| 9876 | * Assign new entity-node if available. |
| 9877 | */ |
| 9878 | ent = xmlGetDocEntity(destDoc, node->name); |
| 9879 | if (ent != NULL) { |
| 9880 | node->content = ent->content; |
| 9881 | node->children = (xmlNodePtr) ent; |
| 9882 | node->last = (xmlNodePtr) ent; |
| 9883 | } |
| 9884 | } |
| 9885 | XML_TREE_ADOPT_STR(node->name) |
| 9886 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 9887 | case XML_PI_NODE: { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9888 | XML_TREE_ADOPT_STR(node->name) |
| 9889 | XML_TREE_ADOPT_STR_2(node->content) |
| 9890 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 9891 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9892 | default: |
| 9893 | break; |
| 9894 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9895 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9896 | return (0); |
| 9897 | } |
| 9898 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 9899 | #define bottom_tree |
| 9900 | #include "elfgcchack.h" |