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) { |
Daniel Veillard | 594e5df | 2009-09-07 14:58:47 +0200 | [diff] [blame] | 1424 | ret = node; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1425 | } else { |
Daniel Veillard | 594e5df | 2009-09-07 14:58:47 +0200 | [diff] [blame] | 1426 | 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 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1593 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1594 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1595 | cur++; |
| 1596 | } |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 1597 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1598 | /* |
| 1599 | * Handle the last piece of text. |
| 1600 | */ |
| 1601 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 1602 | xmlNodeAddContentLen(last, q, cur - q); |
| 1603 | } else { |
| 1604 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 1605 | if (node == NULL) return(ret); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1606 | if (last == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1607 | last = ret = node; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1608 | } else { |
| 1609 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1610 | } |
| 1611 | } |
| 1612 | } |
| 1613 | return(ret); |
| 1614 | } |
| 1615 | |
| 1616 | /** |
| 1617 | * xmlNodeListGetString: |
| 1618 | * @doc: the document |
| 1619 | * @list: a Node list |
| 1620 | * @inLine: should we replace entity contents or show their external form |
| 1621 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1622 | * Build the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1623 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1624 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1625 | * 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] | 1626 | */ |
| 1627 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1628 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1629 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1630 | xmlNodePtr node = list; |
| 1631 | xmlChar *ret = NULL; |
| 1632 | xmlEntityPtr ent; |
| 1633 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1634 | if (list == NULL) |
| 1635 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1636 | |
| 1637 | while (node != NULL) { |
| 1638 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1639 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1640 | if (inLine) { |
| 1641 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1642 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1643 | xmlChar *buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1644 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1645 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
| 1646 | if (buffer != NULL) { |
| 1647 | ret = xmlStrcat(ret, buffer); |
| 1648 | xmlFree(buffer); |
| 1649 | } |
| 1650 | } |
| 1651 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1652 | if (inLine) { |
| 1653 | ent = xmlGetDocEntity(doc, node->name); |
| 1654 | if (ent != NULL) { |
| 1655 | xmlChar *buffer; |
| 1656 | |
| 1657 | /* an entity content can be any "well balanced chunk", |
| 1658 | * i.e. the result of the content [43] production: |
| 1659 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1660 | * So it can contain text, CDATA section or nested |
| 1661 | * entity reference nodes (among others). |
| 1662 | * -> we recursive call xmlNodeListGetString() |
| 1663 | * which handles these types */ |
| 1664 | buffer = xmlNodeListGetString(doc, ent->children, 1); |
| 1665 | if (buffer != NULL) { |
| 1666 | ret = xmlStrcat(ret, buffer); |
| 1667 | xmlFree(buffer); |
| 1668 | } |
| 1669 | } else { |
| 1670 | ret = xmlStrcat(ret, node->content); |
| 1671 | } |
| 1672 | } else { |
| 1673 | xmlChar buf[2]; |
| 1674 | |
| 1675 | buf[0] = '&'; |
| 1676 | buf[1] = 0; |
| 1677 | ret = xmlStrncat(ret, buf, 1); |
| 1678 | ret = xmlStrcat(ret, node->name); |
| 1679 | buf[0] = ';'; |
| 1680 | buf[1] = 0; |
| 1681 | ret = xmlStrncat(ret, buf, 1); |
| 1682 | } |
| 1683 | } |
| 1684 | #if 0 |
| 1685 | else { |
| 1686 | xmlGenericError(xmlGenericErrorContext, |
| 1687 | "xmlGetNodeListString : invalid node type %d\n", |
| 1688 | node->type); |
| 1689 | } |
| 1690 | #endif |
| 1691 | node = node->next; |
| 1692 | } |
| 1693 | return (ret); |
| 1694 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1695 | |
| 1696 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1697 | /** |
| 1698 | * xmlNodeListGetRawString: |
| 1699 | * @doc: the document |
| 1700 | * @list: a Node list |
| 1701 | * @inLine: should we replace entity contents or show their external form |
| 1702 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1703 | * Builds the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1704 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 1705 | * this function doesn't do any character encoding handling. |
| 1706 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1707 | * 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] | 1708 | */ |
| 1709 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1710 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1711 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1712 | xmlNodePtr node = list; |
| 1713 | xmlChar *ret = NULL; |
| 1714 | xmlEntityPtr ent; |
| 1715 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1716 | if (list == NULL) |
| 1717 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1718 | |
| 1719 | while (node != NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1720 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1721 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1722 | if (inLine) { |
| 1723 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1724 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1725 | xmlChar *buffer; |
| 1726 | |
| 1727 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 1728 | if (buffer != NULL) { |
| 1729 | ret = xmlStrcat(ret, buffer); |
| 1730 | xmlFree(buffer); |
| 1731 | } |
| 1732 | } |
| 1733 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1734 | if (inLine) { |
| 1735 | ent = xmlGetDocEntity(doc, node->name); |
| 1736 | if (ent != NULL) { |
| 1737 | xmlChar *buffer; |
| 1738 | |
| 1739 | /* an entity content can be any "well balanced chunk", |
| 1740 | * i.e. the result of the content [43] production: |
| 1741 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1742 | * So it can contain text, CDATA section or nested |
| 1743 | * entity reference nodes (among others). |
| 1744 | * -> we recursive call xmlNodeListGetRawString() |
| 1745 | * which handles these types */ |
| 1746 | buffer = |
| 1747 | xmlNodeListGetRawString(doc, ent->children, 1); |
| 1748 | if (buffer != NULL) { |
| 1749 | ret = xmlStrcat(ret, buffer); |
| 1750 | xmlFree(buffer); |
| 1751 | } |
| 1752 | } else { |
| 1753 | ret = xmlStrcat(ret, node->content); |
| 1754 | } |
| 1755 | } else { |
| 1756 | xmlChar buf[2]; |
| 1757 | |
| 1758 | buf[0] = '&'; |
| 1759 | buf[1] = 0; |
| 1760 | ret = xmlStrncat(ret, buf, 1); |
| 1761 | ret = xmlStrcat(ret, node->name); |
| 1762 | buf[0] = ';'; |
| 1763 | buf[1] = 0; |
| 1764 | ret = xmlStrncat(ret, buf, 1); |
| 1765 | } |
| 1766 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1767 | #if 0 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1768 | else { |
| 1769 | xmlGenericError(xmlGenericErrorContext, |
| 1770 | "xmlGetNodeListString : invalid node type %d\n", |
| 1771 | node->type); |
| 1772 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1773 | #endif |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1774 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1775 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1776 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1777 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1778 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1779 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1780 | static xmlAttrPtr |
| 1781 | xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns, |
| 1782 | const xmlChar * name, const xmlChar * value, |
| 1783 | int eatname) |
| 1784 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1785 | xmlAttrPtr cur; |
| 1786 | xmlDocPtr doc = NULL; |
| 1787 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1788 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) { |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 1789 | if ((eatname == 1) && |
| 1790 | ((node->doc == NULL) || |
Daniel Veillard | ed939f8 | 2008-04-08 08:20:08 +0000 | [diff] [blame] | 1791 | (!(xmlDictOwns(node->doc->dict, name))))) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1792 | xmlFree((xmlChar *) name); |
| 1793 | return (NULL); |
| 1794 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1795 | |
| 1796 | /* |
| 1797 | * Allocate a new property and fill the fields. |
| 1798 | */ |
| 1799 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1800 | if (cur == NULL) { |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 1801 | if ((eatname == 1) && |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 1802 | ((node == NULL) || (node->doc == NULL) || |
Daniel Veillard | ed939f8 | 2008-04-08 08:20:08 +0000 | [diff] [blame] | 1803 | (!(xmlDictOwns(node->doc->dict, name))))) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1804 | xmlFree((xmlChar *) name); |
| 1805 | xmlTreeErrMemory("building attribute"); |
| 1806 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1807 | } |
| 1808 | memset(cur, 0, sizeof(xmlAttr)); |
| 1809 | cur->type = XML_ATTRIBUTE_NODE; |
| 1810 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1811 | cur->parent = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1812 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1813 | doc = node->doc; |
| 1814 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1815 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1816 | cur->ns = ns; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1817 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1818 | if (eatname == 0) { |
| 1819 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1820 | cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1); |
| 1821 | else |
| 1822 | cur->name = xmlStrdup(name); |
| 1823 | } else |
| 1824 | cur->name = name; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1825 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1826 | if (value != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1827 | xmlNodePtr tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1828 | |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame] | 1829 | if(!xmlCheckUTF8(value)) { |
| 1830 | xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc, |
| 1831 | NULL); |
| 1832 | if (doc != NULL) |
| 1833 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 1834 | } |
| 1835 | cur->children = xmlNewDocText(doc, value); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1836 | cur->last = NULL; |
| 1837 | tmp = cur->children; |
| 1838 | while (tmp != NULL) { |
| 1839 | tmp->parent = (xmlNodePtr) cur; |
| 1840 | if (tmp->next == NULL) |
| 1841 | cur->last = tmp; |
| 1842 | tmp = tmp->next; |
| 1843 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1844 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1845 | |
| 1846 | /* |
| 1847 | * Add it at the end to preserve parsing order ... |
| 1848 | */ |
| 1849 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1850 | if (node->properties == NULL) { |
| 1851 | node->properties = cur; |
| 1852 | } else { |
| 1853 | xmlAttrPtr prev = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1854 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1855 | while (prev->next != NULL) |
| 1856 | prev = prev->next; |
| 1857 | prev->next = cur; |
| 1858 | cur->prev = prev; |
| 1859 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1860 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1861 | |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 1862 | if ((value != NULL) && (node != NULL) && |
| 1863 | (xmlIsID(node->doc, node, cur) == 1)) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1864 | xmlAddID(NULL, node->doc, value, cur); |
| 1865 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1866 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1867 | xmlRegisterNodeDefaultValue((xmlNodePtr) cur); |
| 1868 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1869 | } |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1870 | |
| 1871 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 1872 | defined(LIBXML_SCHEMAS_ENABLED) |
| 1873 | /** |
| 1874 | * xmlNewProp: |
| 1875 | * @node: the holding node |
| 1876 | * @name: the name of the attribute |
| 1877 | * @value: the value of the attribute |
| 1878 | * |
| 1879 | * Create a new property carried by a node. |
| 1880 | * Returns a pointer to the attribute |
| 1881 | */ |
| 1882 | xmlAttrPtr |
| 1883 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 1884 | |
| 1885 | if (name == NULL) { |
| 1886 | #ifdef DEBUG_TREE |
| 1887 | xmlGenericError(xmlGenericErrorContext, |
| 1888 | "xmlNewProp : name == NULL\n"); |
| 1889 | #endif |
| 1890 | return(NULL); |
| 1891 | } |
| 1892 | |
| 1893 | return xmlNewPropInternal(node, NULL, name, value, 0); |
| 1894 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1895 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1896 | |
| 1897 | /** |
| 1898 | * xmlNewNsProp: |
| 1899 | * @node: the holding node |
| 1900 | * @ns: the namespace |
| 1901 | * @name: the name of the attribute |
| 1902 | * @value: the value of the attribute |
| 1903 | * |
| 1904 | * Create a new property tagged with a namespace and carried by a node. |
| 1905 | * Returns a pointer to the attribute |
| 1906 | */ |
| 1907 | xmlAttrPtr |
| 1908 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 1909 | const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1910 | |
| 1911 | if (name == NULL) { |
| 1912 | #ifdef DEBUG_TREE |
| 1913 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1914 | "xmlNewNsProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1915 | #endif |
| 1916 | return(NULL); |
| 1917 | } |
| 1918 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1919 | return xmlNewPropInternal(node, ns, name, value, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1923 | * xmlNewNsPropEatName: |
| 1924 | * @node: the holding node |
| 1925 | * @ns: the namespace |
| 1926 | * @name: the name of the attribute |
| 1927 | * @value: the value of the attribute |
| 1928 | * |
| 1929 | * Create a new property tagged with a namespace and carried by a node. |
| 1930 | * Returns a pointer to the attribute |
| 1931 | */ |
| 1932 | xmlAttrPtr |
| 1933 | xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name, |
| 1934 | const xmlChar *value) { |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1935 | |
| 1936 | if (name == NULL) { |
| 1937 | #ifdef DEBUG_TREE |
| 1938 | xmlGenericError(xmlGenericErrorContext, |
| 1939 | "xmlNewNsPropEatName : name == NULL\n"); |
| 1940 | #endif |
| 1941 | return(NULL); |
| 1942 | } |
| 1943 | |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 1944 | return xmlNewPropInternal(node, ns, name, value, 1); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1948 | * xmlNewDocProp: |
| 1949 | * @doc: the document |
| 1950 | * @name: the name of the attribute |
| 1951 | * @value: the value of the attribute |
| 1952 | * |
| 1953 | * Create a new property carried by a document. |
| 1954 | * Returns a pointer to the attribute |
| 1955 | */ |
| 1956 | xmlAttrPtr |
| 1957 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
| 1958 | xmlAttrPtr cur; |
| 1959 | |
| 1960 | if (name == NULL) { |
| 1961 | #ifdef DEBUG_TREE |
| 1962 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1963 | "xmlNewDocProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1964 | #endif |
| 1965 | return(NULL); |
| 1966 | } |
| 1967 | |
| 1968 | /* |
| 1969 | * Allocate a new property and fill the fields. |
| 1970 | */ |
| 1971 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1972 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1973 | xmlTreeErrMemory("building attribute"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1974 | return(NULL); |
| 1975 | } |
| 1976 | memset(cur, 0, sizeof(xmlAttr)); |
| 1977 | cur->type = XML_ATTRIBUTE_NODE; |
| 1978 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 1979 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1980 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 1981 | else |
| 1982 | cur->name = xmlStrdup(name); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1983 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1984 | if (value != NULL) { |
| 1985 | xmlNodePtr tmp; |
| 1986 | |
| 1987 | cur->children = xmlStringGetNodeList(doc, value); |
| 1988 | cur->last = NULL; |
| 1989 | |
| 1990 | tmp = cur->children; |
| 1991 | while (tmp != NULL) { |
| 1992 | tmp->parent = (xmlNodePtr) cur; |
| 1993 | if (tmp->next == NULL) |
| 1994 | cur->last = tmp; |
| 1995 | tmp = tmp->next; |
| 1996 | } |
| 1997 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1998 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1999 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2000 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2001 | return(cur); |
| 2002 | } |
| 2003 | |
| 2004 | /** |
| 2005 | * xmlFreePropList: |
| 2006 | * @cur: the first property in the list |
| 2007 | * |
| 2008 | * Free a property and all its siblings, all the children are freed too. |
| 2009 | */ |
| 2010 | void |
| 2011 | xmlFreePropList(xmlAttrPtr cur) { |
| 2012 | xmlAttrPtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2013 | if (cur == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2014 | while (cur != NULL) { |
| 2015 | next = cur->next; |
| 2016 | xmlFreeProp(cur); |
| 2017 | cur = next; |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | /** |
| 2022 | * xmlFreeProp: |
| 2023 | * @cur: an attribute |
| 2024 | * |
| 2025 | * Free one attribute, all the content is freed too |
| 2026 | */ |
| 2027 | void |
| 2028 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2029 | xmlDictPtr dict = NULL; |
| 2030 | if (cur == NULL) return; |
| 2031 | |
| 2032 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2033 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2034 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2035 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 2036 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2037 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | da6f4af | 2005-06-20 17:17:54 +0000 | [diff] [blame] | 2038 | if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) { |
| 2039 | xmlRemoveID(cur->doc, cur); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2040 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2041 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2042 | DICT_FREE(cur->name) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2043 | xmlFree(cur); |
| 2044 | } |
| 2045 | |
| 2046 | /** |
| 2047 | * xmlRemoveProp: |
| 2048 | * @cur: an attribute |
| 2049 | * |
| 2050 | * Unlink and free one attribute, all the content is freed too |
| 2051 | * Note this doesn't work for namespace definition attributes |
| 2052 | * |
| 2053 | * Returns 0 if success and -1 in case of error. |
| 2054 | */ |
| 2055 | int |
| 2056 | xmlRemoveProp(xmlAttrPtr cur) { |
| 2057 | xmlAttrPtr tmp; |
| 2058 | if (cur == NULL) { |
| 2059 | #ifdef DEBUG_TREE |
| 2060 | xmlGenericError(xmlGenericErrorContext, |
| 2061 | "xmlRemoveProp : cur == NULL\n"); |
| 2062 | #endif |
| 2063 | return(-1); |
| 2064 | } |
| 2065 | if (cur->parent == NULL) { |
| 2066 | #ifdef DEBUG_TREE |
| 2067 | xmlGenericError(xmlGenericErrorContext, |
| 2068 | "xmlRemoveProp : cur->parent == NULL\n"); |
| 2069 | #endif |
| 2070 | return(-1); |
| 2071 | } |
| 2072 | tmp = cur->parent->properties; |
| 2073 | if (tmp == cur) { |
| 2074 | cur->parent->properties = cur->next; |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2075 | if (cur->next != NULL) |
| 2076 | cur->next->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2077 | xmlFreeProp(cur); |
| 2078 | return(0); |
| 2079 | } |
| 2080 | while (tmp != NULL) { |
| 2081 | if (tmp->next == cur) { |
| 2082 | tmp->next = cur->next; |
| 2083 | if (tmp->next != NULL) |
| 2084 | tmp->next->prev = tmp; |
| 2085 | xmlFreeProp(cur); |
| 2086 | return(0); |
| 2087 | } |
| 2088 | tmp = tmp->next; |
| 2089 | } |
| 2090 | #ifdef DEBUG_TREE |
| 2091 | xmlGenericError(xmlGenericErrorContext, |
| 2092 | "xmlRemoveProp : attribute not owned by its node\n"); |
| 2093 | #endif |
| 2094 | return(-1); |
| 2095 | } |
| 2096 | |
| 2097 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2098 | * xmlNewDocPI: |
| 2099 | * @doc: the target document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2100 | * @name: the processing instruction name |
| 2101 | * @content: the PI content |
| 2102 | * |
| 2103 | * Creation of a processing instruction element. |
| 2104 | * Returns a pointer to the new node object. |
| 2105 | */ |
| 2106 | xmlNodePtr |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2107 | xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2108 | xmlNodePtr cur; |
| 2109 | |
| 2110 | if (name == NULL) { |
| 2111 | #ifdef DEBUG_TREE |
| 2112 | xmlGenericError(xmlGenericErrorContext, |
| 2113 | "xmlNewPI : name == NULL\n"); |
| 2114 | #endif |
| 2115 | return(NULL); |
| 2116 | } |
| 2117 | |
| 2118 | /* |
| 2119 | * Allocate a new node and fill the fields. |
| 2120 | */ |
| 2121 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2122 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2123 | xmlTreeErrMemory("building PI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2124 | return(NULL); |
| 2125 | } |
| 2126 | memset(cur, 0, sizeof(xmlNode)); |
| 2127 | cur->type = XML_PI_NODE; |
| 2128 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2129 | if ((doc != NULL) && (doc->dict != NULL)) |
| 2130 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 2131 | else |
| 2132 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2133 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2134 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2135 | } |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 2136 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2137 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2138 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2139 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2140 | return(cur); |
| 2141 | } |
| 2142 | |
| 2143 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2144 | * xmlNewPI: |
| 2145 | * @name: the processing instruction name |
| 2146 | * @content: the PI content |
| 2147 | * |
| 2148 | * Creation of a processing instruction element. |
| 2149 | * Use xmlDocNewPI preferably to get string interning |
| 2150 | * |
| 2151 | * Returns a pointer to the new node object. |
| 2152 | */ |
| 2153 | xmlNodePtr |
| 2154 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
| 2155 | return(xmlNewDocPI(NULL, name, content)); |
| 2156 | } |
| 2157 | |
| 2158 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2159 | * xmlNewNode: |
| 2160 | * @ns: namespace if any |
| 2161 | * @name: the node name |
| 2162 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2163 | * Creation of a new node element. @ns is optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2164 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2165 | * Returns a pointer to the new node object. Uses xmlStrdup() to make |
| 2166 | * copy of @name. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2167 | */ |
| 2168 | xmlNodePtr |
| 2169 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 2170 | xmlNodePtr cur; |
| 2171 | |
| 2172 | if (name == NULL) { |
| 2173 | #ifdef DEBUG_TREE |
| 2174 | xmlGenericError(xmlGenericErrorContext, |
| 2175 | "xmlNewNode : name == NULL\n"); |
| 2176 | #endif |
| 2177 | return(NULL); |
| 2178 | } |
| 2179 | |
| 2180 | /* |
| 2181 | * Allocate a new node and fill the fields. |
| 2182 | */ |
| 2183 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2184 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2185 | xmlTreeErrMemory("building node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2186 | return(NULL); |
| 2187 | } |
| 2188 | memset(cur, 0, sizeof(xmlNode)); |
| 2189 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2190 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2191 | cur->name = xmlStrdup(name); |
| 2192 | cur->ns = ns; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2193 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2194 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2195 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2196 | return(cur); |
| 2197 | } |
| 2198 | |
| 2199 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2200 | * xmlNewNodeEatName: |
| 2201 | * @ns: namespace if any |
| 2202 | * @name: the node name |
| 2203 | * |
| 2204 | * Creation of a new node element. @ns is optional (NULL). |
| 2205 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2206 | * Returns a pointer to the new node object, with pointer @name as |
| 2207 | * new node's name. Use xmlNewNode() if a copy of @name string is |
| 2208 | * is needed as new node's name. |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2209 | */ |
| 2210 | xmlNodePtr |
| 2211 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
| 2212 | xmlNodePtr cur; |
| 2213 | |
| 2214 | if (name == NULL) { |
| 2215 | #ifdef DEBUG_TREE |
| 2216 | xmlGenericError(xmlGenericErrorContext, |
| 2217 | "xmlNewNode : name == NULL\n"); |
| 2218 | #endif |
| 2219 | return(NULL); |
| 2220 | } |
| 2221 | |
| 2222 | /* |
| 2223 | * Allocate a new node and fill the fields. |
| 2224 | */ |
| 2225 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2226 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2227 | xmlTreeErrMemory("building node"); |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 2228 | /* we can't check here that name comes from the doc dictionnary */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2229 | return(NULL); |
| 2230 | } |
| 2231 | memset(cur, 0, sizeof(xmlNode)); |
| 2232 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2233 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2234 | cur->name = name; |
| 2235 | cur->ns = ns; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2236 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2237 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2238 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2239 | return(cur); |
| 2240 | } |
| 2241 | |
| 2242 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2243 | * xmlNewDocNode: |
| 2244 | * @doc: the document |
| 2245 | * @ns: namespace if any |
| 2246 | * @name: the node name |
| 2247 | * @content: the XML text content if any |
| 2248 | * |
| 2249 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2250 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2251 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2252 | * references, but XML special chars need to be escaped first by using |
| 2253 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2254 | * need entities support. |
| 2255 | * |
| 2256 | * Returns a pointer to the new node object. |
| 2257 | */ |
| 2258 | xmlNodePtr |
| 2259 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2260 | const xmlChar *name, const xmlChar *content) { |
| 2261 | xmlNodePtr cur; |
| 2262 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2263 | if ((doc != NULL) && (doc->dict != NULL)) |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2264 | cur = xmlNewNodeEatName(ns, (xmlChar *) |
| 2265 | xmlDictLookup(doc->dict, name, -1)); |
| 2266 | else |
| 2267 | cur = xmlNewNode(ns, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2268 | if (cur != NULL) { |
| 2269 | cur->doc = doc; |
| 2270 | if (content != NULL) { |
| 2271 | cur->children = xmlStringGetNodeList(doc, content); |
| 2272 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2273 | } |
| 2274 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2275 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2276 | return(cur); |
| 2277 | } |
| 2278 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2279 | /** |
| 2280 | * xmlNewDocNodeEatName: |
| 2281 | * @doc: the document |
| 2282 | * @ns: namespace if any |
| 2283 | * @name: the node name |
| 2284 | * @content: the XML text content if any |
| 2285 | * |
| 2286 | * Creation of a new node element within a document. @ns and @content |
| 2287 | * are optional (NULL). |
| 2288 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2289 | * references, but XML special chars need to be escaped first by using |
| 2290 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2291 | * need entities support. |
| 2292 | * |
| 2293 | * Returns a pointer to the new node object. |
| 2294 | */ |
| 2295 | xmlNodePtr |
| 2296 | xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns, |
| 2297 | xmlChar *name, const xmlChar *content) { |
| 2298 | xmlNodePtr cur; |
| 2299 | |
| 2300 | cur = xmlNewNodeEatName(ns, name); |
| 2301 | if (cur != NULL) { |
| 2302 | cur->doc = doc; |
| 2303 | if (content != NULL) { |
| 2304 | cur->children = xmlStringGetNodeList(doc, content); |
| 2305 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2306 | } |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 2307 | } else { |
| 2308 | /* if name don't come from the doc dictionnary free it here */ |
| 2309 | if ((name != NULL) && (doc != NULL) && |
| 2310 | (!(xmlDictOwns(doc->dict, name)))) |
| 2311 | xmlFree(name); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2312 | } |
| 2313 | return(cur); |
| 2314 | } |
| 2315 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2316 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2317 | /** |
| 2318 | * xmlNewDocRawNode: |
| 2319 | * @doc: the document |
| 2320 | * @ns: namespace if any |
| 2321 | * @name: the node name |
| 2322 | * @content: the text content if any |
| 2323 | * |
| 2324 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2325 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2326 | * |
| 2327 | * Returns a pointer to the new node object. |
| 2328 | */ |
| 2329 | xmlNodePtr |
| 2330 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2331 | const xmlChar *name, const xmlChar *content) { |
| 2332 | xmlNodePtr cur; |
| 2333 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2334 | cur = xmlNewDocNode(doc, ns, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2335 | if (cur != NULL) { |
| 2336 | cur->doc = doc; |
| 2337 | if (content != NULL) { |
| 2338 | cur->children = xmlNewDocText(doc, content); |
| 2339 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2340 | } |
| 2341 | } |
| 2342 | return(cur); |
| 2343 | } |
| 2344 | |
| 2345 | /** |
| 2346 | * xmlNewDocFragment: |
| 2347 | * @doc: the document owning the fragment |
| 2348 | * |
| 2349 | * Creation of a new Fragment node. |
| 2350 | * Returns a pointer to the new node object. |
| 2351 | */ |
| 2352 | xmlNodePtr |
| 2353 | xmlNewDocFragment(xmlDocPtr doc) { |
| 2354 | xmlNodePtr cur; |
| 2355 | |
| 2356 | /* |
| 2357 | * Allocate a new DocumentFragment node and fill the fields. |
| 2358 | */ |
| 2359 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2360 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2361 | xmlTreeErrMemory("building fragment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2362 | return(NULL); |
| 2363 | } |
| 2364 | memset(cur, 0, sizeof(xmlNode)); |
| 2365 | cur->type = XML_DOCUMENT_FRAG_NODE; |
| 2366 | |
| 2367 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2368 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2369 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2370 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2371 | return(cur); |
| 2372 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2373 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2374 | |
| 2375 | /** |
| 2376 | * xmlNewText: |
| 2377 | * @content: the text content |
| 2378 | * |
| 2379 | * Creation of a new text node. |
| 2380 | * Returns a pointer to the new node object. |
| 2381 | */ |
| 2382 | xmlNodePtr |
| 2383 | xmlNewText(const xmlChar *content) { |
| 2384 | xmlNodePtr cur; |
| 2385 | |
| 2386 | /* |
| 2387 | * Allocate a new node and fill the fields. |
| 2388 | */ |
| 2389 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2390 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2391 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2392 | return(NULL); |
| 2393 | } |
| 2394 | memset(cur, 0, sizeof(xmlNode)); |
| 2395 | cur->type = XML_TEXT_NODE; |
| 2396 | |
| 2397 | cur->name = xmlStringText; |
| 2398 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2399 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2400 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2401 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2402 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2403 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2404 | return(cur); |
| 2405 | } |
| 2406 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2407 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2408 | /** |
| 2409 | * xmlNewTextChild: |
| 2410 | * @parent: the parent node |
| 2411 | * @ns: a namespace if any |
| 2412 | * @name: the name of the child |
| 2413 | * @content: the text content of the child if any. |
| 2414 | * |
| 2415 | * 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] | 2416 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2417 | * 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] | 2418 | * 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] | 2419 | * NOTE: Use xmlNewChild() if @content will contain entities that need to be |
| 2420 | * preserved. Use this function, xmlNewTextChild(), if you need to ensure that |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2421 | * reserved XML chars that might appear in @content, such as the ampersand, |
| 2422 | * greater-than or less-than signs, are automatically replaced by their XML |
| 2423 | * escaped entity representations. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2424 | * |
| 2425 | * Returns a pointer to the new node object. |
| 2426 | */ |
| 2427 | xmlNodePtr |
| 2428 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2429 | const xmlChar *name, const xmlChar *content) { |
| 2430 | xmlNodePtr cur, prev; |
| 2431 | |
| 2432 | if (parent == NULL) { |
| 2433 | #ifdef DEBUG_TREE |
| 2434 | xmlGenericError(xmlGenericErrorContext, |
| 2435 | "xmlNewTextChild : parent == NULL\n"); |
| 2436 | #endif |
| 2437 | return(NULL); |
| 2438 | } |
| 2439 | |
| 2440 | if (name == NULL) { |
| 2441 | #ifdef DEBUG_TREE |
| 2442 | xmlGenericError(xmlGenericErrorContext, |
| 2443 | "xmlNewTextChild : name == NULL\n"); |
| 2444 | #endif |
| 2445 | return(NULL); |
| 2446 | } |
| 2447 | |
| 2448 | /* |
| 2449 | * Allocate a new node |
| 2450 | */ |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 2451 | if (parent->type == XML_ELEMENT_NODE) { |
| 2452 | if (ns == NULL) |
| 2453 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 2454 | else |
| 2455 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 2456 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2457 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2458 | if (ns == NULL) |
| 2459 | cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content); |
| 2460 | else |
| 2461 | cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content); |
| 2462 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2463 | cur = xmlNewDocRawNode( parent->doc, ns, name, content); |
| 2464 | } else { |
| 2465 | return(NULL); |
| 2466 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2467 | if (cur == NULL) return(NULL); |
| 2468 | |
| 2469 | /* |
| 2470 | * add the new element at the end of the children list. |
| 2471 | */ |
| 2472 | cur->type = XML_ELEMENT_NODE; |
| 2473 | cur->parent = parent; |
| 2474 | cur->doc = parent->doc; |
| 2475 | if (parent->children == NULL) { |
| 2476 | parent->children = cur; |
| 2477 | parent->last = cur; |
| 2478 | } else { |
| 2479 | prev = parent->last; |
| 2480 | prev->next = cur; |
| 2481 | cur->prev = prev; |
| 2482 | parent->last = cur; |
| 2483 | } |
| 2484 | |
| 2485 | return(cur); |
| 2486 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2487 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2488 | |
| 2489 | /** |
| 2490 | * xmlNewCharRef: |
| 2491 | * @doc: the document |
| 2492 | * @name: the char ref string, starting with # or "&# ... ;" |
| 2493 | * |
| 2494 | * Creation of a new character reference node. |
| 2495 | * Returns a pointer to the new node object. |
| 2496 | */ |
| 2497 | xmlNodePtr |
| 2498 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 2499 | xmlNodePtr cur; |
| 2500 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2501 | if (name == NULL) |
| 2502 | return(NULL); |
| 2503 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2504 | /* |
| 2505 | * Allocate a new node and fill the fields. |
| 2506 | */ |
| 2507 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2508 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2509 | xmlTreeErrMemory("building character reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2510 | return(NULL); |
| 2511 | } |
| 2512 | memset(cur, 0, sizeof(xmlNode)); |
| 2513 | cur->type = XML_ENTITY_REF_NODE; |
| 2514 | |
| 2515 | cur->doc = doc; |
| 2516 | if (name[0] == '&') { |
| 2517 | int len; |
| 2518 | name++; |
| 2519 | len = xmlStrlen(name); |
| 2520 | if (name[len - 1] == ';') |
| 2521 | cur->name = xmlStrndup(name, len - 1); |
| 2522 | else |
| 2523 | cur->name = xmlStrndup(name, len); |
| 2524 | } else |
| 2525 | cur->name = xmlStrdup(name); |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2526 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2527 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2528 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2529 | return(cur); |
| 2530 | } |
| 2531 | |
| 2532 | /** |
| 2533 | * xmlNewReference: |
| 2534 | * @doc: the document |
| 2535 | * @name: the reference name, or the reference string with & and ; |
| 2536 | * |
| 2537 | * Creation of a new reference node. |
| 2538 | * Returns a pointer to the new node object. |
| 2539 | */ |
| 2540 | xmlNodePtr |
| 2541 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 2542 | xmlNodePtr cur; |
| 2543 | xmlEntityPtr ent; |
| 2544 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2545 | if (name == NULL) |
| 2546 | return(NULL); |
| 2547 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2548 | /* |
| 2549 | * Allocate a new node and fill the fields. |
| 2550 | */ |
| 2551 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2552 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2553 | xmlTreeErrMemory("building reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2554 | return(NULL); |
| 2555 | } |
| 2556 | memset(cur, 0, sizeof(xmlNode)); |
| 2557 | cur->type = XML_ENTITY_REF_NODE; |
| 2558 | |
| 2559 | cur->doc = doc; |
| 2560 | if (name[0] == '&') { |
| 2561 | int len; |
| 2562 | name++; |
| 2563 | len = xmlStrlen(name); |
| 2564 | if (name[len - 1] == ';') |
| 2565 | cur->name = xmlStrndup(name, len - 1); |
| 2566 | else |
| 2567 | cur->name = xmlStrndup(name, len); |
| 2568 | } else |
| 2569 | cur->name = xmlStrdup(name); |
| 2570 | |
| 2571 | ent = xmlGetDocEntity(doc, cur->name); |
| 2572 | if (ent != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2573 | cur->content = ent->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2574 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2575 | * 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] | 2576 | * updated. Not sure if this is 100% correct. |
| 2577 | * -George |
| 2578 | */ |
| 2579 | cur->children = (xmlNodePtr) ent; |
| 2580 | cur->last = (xmlNodePtr) ent; |
| 2581 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2582 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2583 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2584 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2585 | return(cur); |
| 2586 | } |
| 2587 | |
| 2588 | /** |
| 2589 | * xmlNewDocText: |
| 2590 | * @doc: the document |
| 2591 | * @content: the text content |
| 2592 | * |
| 2593 | * Creation of a new text node within a document. |
| 2594 | * Returns a pointer to the new node object. |
| 2595 | */ |
| 2596 | xmlNodePtr |
| 2597 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 2598 | xmlNodePtr cur; |
| 2599 | |
| 2600 | cur = xmlNewText(content); |
| 2601 | if (cur != NULL) cur->doc = doc; |
| 2602 | return(cur); |
| 2603 | } |
| 2604 | |
| 2605 | /** |
| 2606 | * xmlNewTextLen: |
| 2607 | * @content: the text content |
| 2608 | * @len: the text len. |
| 2609 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2610 | * 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] | 2611 | * Returns a pointer to the new node object. |
| 2612 | */ |
| 2613 | xmlNodePtr |
| 2614 | xmlNewTextLen(const xmlChar *content, int len) { |
| 2615 | xmlNodePtr cur; |
| 2616 | |
| 2617 | /* |
| 2618 | * Allocate a new node and fill the fields. |
| 2619 | */ |
| 2620 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2621 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2622 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2623 | return(NULL); |
| 2624 | } |
| 2625 | memset(cur, 0, sizeof(xmlNode)); |
| 2626 | cur->type = XML_TEXT_NODE; |
| 2627 | |
| 2628 | cur->name = xmlStringText; |
| 2629 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2630 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2631 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2632 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2633 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2634 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2635 | return(cur); |
| 2636 | } |
| 2637 | |
| 2638 | /** |
| 2639 | * xmlNewDocTextLen: |
| 2640 | * @doc: the document |
| 2641 | * @content: the text content |
| 2642 | * @len: the text len. |
| 2643 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2644 | * 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] | 2645 | * text node pertain to a given document. |
| 2646 | * Returns a pointer to the new node object. |
| 2647 | */ |
| 2648 | xmlNodePtr |
| 2649 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2650 | xmlNodePtr cur; |
| 2651 | |
| 2652 | cur = xmlNewTextLen(content, len); |
| 2653 | if (cur != NULL) cur->doc = doc; |
| 2654 | return(cur); |
| 2655 | } |
| 2656 | |
| 2657 | /** |
| 2658 | * xmlNewComment: |
| 2659 | * @content: the comment content |
| 2660 | * |
| 2661 | * Creation of a new node containing a comment. |
| 2662 | * Returns a pointer to the new node object. |
| 2663 | */ |
| 2664 | xmlNodePtr |
| 2665 | xmlNewComment(const xmlChar *content) { |
| 2666 | xmlNodePtr cur; |
| 2667 | |
| 2668 | /* |
| 2669 | * Allocate a new node and fill the fields. |
| 2670 | */ |
| 2671 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2672 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2673 | xmlTreeErrMemory("building comment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2674 | return(NULL); |
| 2675 | } |
| 2676 | memset(cur, 0, sizeof(xmlNode)); |
| 2677 | cur->type = XML_COMMENT_NODE; |
| 2678 | |
| 2679 | cur->name = xmlStringComment; |
| 2680 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2681 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2682 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2683 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2684 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2685 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2686 | return(cur); |
| 2687 | } |
| 2688 | |
| 2689 | /** |
| 2690 | * xmlNewCDataBlock: |
| 2691 | * @doc: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2692 | * @content: the CDATA block content content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2693 | * @len: the length of the block |
| 2694 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2695 | * Creation of a new node containing a CDATA block. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2696 | * Returns a pointer to the new node object. |
| 2697 | */ |
| 2698 | xmlNodePtr |
| 2699 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2700 | xmlNodePtr cur; |
| 2701 | |
| 2702 | /* |
| 2703 | * Allocate a new node and fill the fields. |
| 2704 | */ |
| 2705 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2706 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2707 | xmlTreeErrMemory("building CDATA"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2708 | return(NULL); |
| 2709 | } |
| 2710 | memset(cur, 0, sizeof(xmlNode)); |
| 2711 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2712 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2713 | |
| 2714 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2715 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2716 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2717 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2718 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2719 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2720 | return(cur); |
| 2721 | } |
| 2722 | |
| 2723 | /** |
| 2724 | * xmlNewDocComment: |
| 2725 | * @doc: the document |
| 2726 | * @content: the comment content |
| 2727 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2728 | * Creation of a new node containing a comment within a document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2729 | * Returns a pointer to the new node object. |
| 2730 | */ |
| 2731 | xmlNodePtr |
| 2732 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 2733 | xmlNodePtr cur; |
| 2734 | |
| 2735 | cur = xmlNewComment(content); |
| 2736 | if (cur != NULL) cur->doc = doc; |
| 2737 | return(cur); |
| 2738 | } |
| 2739 | |
| 2740 | /** |
| 2741 | * xmlSetTreeDoc: |
| 2742 | * @tree: the top element |
| 2743 | * @doc: the document |
| 2744 | * |
| 2745 | * update all nodes under the tree to point to the right document |
| 2746 | */ |
| 2747 | void |
| 2748 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2749 | xmlAttrPtr prop; |
| 2750 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2751 | if (tree == NULL) |
| 2752 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2753 | if (tree->doc != doc) { |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2754 | if(tree->type == XML_ELEMENT_NODE) { |
| 2755 | prop = tree->properties; |
| 2756 | while (prop != NULL) { |
| 2757 | prop->doc = doc; |
| 2758 | xmlSetListDoc(prop->children, doc); |
| 2759 | prop = prop->next; |
| 2760 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2761 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2762 | if (tree->children != NULL) |
| 2763 | xmlSetListDoc(tree->children, doc); |
| 2764 | tree->doc = doc; |
| 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | /** |
| 2769 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2770 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2771 | * @doc: the document |
| 2772 | * |
| 2773 | * update all nodes in the list to point to the right document |
| 2774 | */ |
| 2775 | void |
| 2776 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2777 | xmlNodePtr cur; |
| 2778 | |
| 2779 | if (list == NULL) |
| 2780 | return; |
| 2781 | cur = list; |
| 2782 | while (cur != NULL) { |
| 2783 | if (cur->doc != doc) |
| 2784 | xmlSetTreeDoc(cur, doc); |
| 2785 | cur = cur->next; |
| 2786 | } |
| 2787 | } |
| 2788 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 2789 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2790 | /** |
| 2791 | * xmlNewChild: |
| 2792 | * @parent: the parent node |
| 2793 | * @ns: a namespace if any |
| 2794 | * @name: the name of the child |
| 2795 | * @content: the XML content of the child if any. |
| 2796 | * |
| 2797 | * 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] | 2798 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2799 | * created element inherits the namespace of @parent. If @content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2800 | * 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] | 2801 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 2802 | * references. XML special chars must be escaped first by using |
| 2803 | * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2804 | * |
| 2805 | * Returns a pointer to the new node object. |
| 2806 | */ |
| 2807 | xmlNodePtr |
| 2808 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2809 | const xmlChar *name, const xmlChar *content) { |
| 2810 | xmlNodePtr cur, prev; |
| 2811 | |
| 2812 | if (parent == NULL) { |
| 2813 | #ifdef DEBUG_TREE |
| 2814 | xmlGenericError(xmlGenericErrorContext, |
| 2815 | "xmlNewChild : parent == NULL\n"); |
| 2816 | #endif |
| 2817 | return(NULL); |
| 2818 | } |
| 2819 | |
| 2820 | if (name == NULL) { |
| 2821 | #ifdef DEBUG_TREE |
| 2822 | xmlGenericError(xmlGenericErrorContext, |
| 2823 | "xmlNewChild : name == NULL\n"); |
| 2824 | #endif |
| 2825 | return(NULL); |
| 2826 | } |
| 2827 | |
| 2828 | /* |
| 2829 | * Allocate a new node |
| 2830 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2831 | if (parent->type == XML_ELEMENT_NODE) { |
| 2832 | if (ns == NULL) |
| 2833 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2834 | else |
| 2835 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2836 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2837 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2838 | if (ns == NULL) |
| 2839 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2840 | else |
| 2841 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2842 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2843 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2844 | } else { |
| 2845 | return(NULL); |
| 2846 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2847 | if (cur == NULL) return(NULL); |
| 2848 | |
| 2849 | /* |
| 2850 | * add the new element at the end of the children list. |
| 2851 | */ |
| 2852 | cur->type = XML_ELEMENT_NODE; |
| 2853 | cur->parent = parent; |
| 2854 | cur->doc = parent->doc; |
| 2855 | if (parent->children == NULL) { |
| 2856 | parent->children = cur; |
| 2857 | parent->last = cur; |
| 2858 | } else { |
| 2859 | prev = parent->last; |
| 2860 | prev->next = cur; |
| 2861 | cur->prev = prev; |
| 2862 | parent->last = cur; |
| 2863 | } |
| 2864 | |
| 2865 | return(cur); |
| 2866 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2867 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2868 | |
| 2869 | /** |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2870 | * xmlAddPropSibling: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2871 | * @prev: the attribute to which @prop is added after |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2872 | * @cur: the base attribute passed to calling function |
| 2873 | * @prop: the new attribute |
| 2874 | * |
| 2875 | * Add a new attribute after @prev using @cur as base attribute. |
| 2876 | * When inserting before @cur, @prev is passed as @cur->prev. |
| 2877 | * When inserting after @cur, @prev is passed as @cur. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2878 | * 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] | 2879 | * |
| 2880 | * Returns the attribute being inserted or NULL in case of error. |
| 2881 | */ |
| 2882 | static xmlNodePtr |
| 2883 | xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) { |
| 2884 | xmlAttrPtr attr; |
| 2885 | |
| 2886 | if (cur->type != XML_ATTRIBUTE_NODE) |
| 2887 | return(NULL); |
| 2888 | |
| 2889 | /* check if an attribute with the same name exists */ |
| 2890 | if (prop->ns == NULL) |
| 2891 | attr = xmlHasNsProp(cur->parent, prop->name, NULL); |
| 2892 | else |
| 2893 | attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href); |
| 2894 | |
| 2895 | if (prop->doc != cur->doc) { |
| 2896 | xmlSetTreeDoc(prop, cur->doc); |
| 2897 | } |
| 2898 | prop->parent = cur->parent; |
| 2899 | prop->prev = prev; |
| 2900 | if (prev != NULL) { |
| 2901 | prop->next = prev->next; |
| 2902 | prev->next = prop; |
| 2903 | if (prop->next) |
| 2904 | prop->next->prev = prop; |
| 2905 | } else { |
| 2906 | prop->next = cur; |
| 2907 | cur->prev = prop; |
| 2908 | } |
| 2909 | if (prop->prev == NULL && prop->parent != NULL) |
| 2910 | prop->parent->properties = (xmlAttrPtr) prop; |
| 2911 | if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) { |
| 2912 | /* different instance, destroy it (attributes must be unique) */ |
| 2913 | xmlRemoveProp((xmlAttrPtr) attr); |
| 2914 | } |
| 2915 | return prop; |
| 2916 | } |
| 2917 | |
| 2918 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2919 | * xmlAddNextSibling: |
| 2920 | * @cur: the child node |
| 2921 | * @elem: the new node |
| 2922 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2923 | * Add a new node @elem as the next sibling of @cur |
| 2924 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2925 | * first unlinked from its existing context. |
| 2926 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2927 | * 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] | 2928 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2929 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2930 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2931 | */ |
| 2932 | xmlNodePtr |
| 2933 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 2934 | if (cur == NULL) { |
| 2935 | #ifdef DEBUG_TREE |
| 2936 | xmlGenericError(xmlGenericErrorContext, |
| 2937 | "xmlAddNextSibling : cur == NULL\n"); |
| 2938 | #endif |
| 2939 | return(NULL); |
| 2940 | } |
| 2941 | if (elem == NULL) { |
| 2942 | #ifdef DEBUG_TREE |
| 2943 | xmlGenericError(xmlGenericErrorContext, |
| 2944 | "xmlAddNextSibling : elem == NULL\n"); |
| 2945 | #endif |
| 2946 | return(NULL); |
| 2947 | } |
| 2948 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2949 | if (cur == elem) { |
| 2950 | #ifdef DEBUG_TREE |
| 2951 | xmlGenericError(xmlGenericErrorContext, |
| 2952 | "xmlAddNextSibling : cur == elem\n"); |
| 2953 | #endif |
| 2954 | return(NULL); |
| 2955 | } |
| 2956 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2957 | xmlUnlinkNode(elem); |
| 2958 | |
| 2959 | if (elem->type == XML_TEXT_NODE) { |
| 2960 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2961 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2962 | xmlFreeNode(elem); |
| 2963 | return(cur); |
| 2964 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 2965 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 2966 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2967 | xmlChar *tmp; |
| 2968 | |
| 2969 | tmp = xmlStrdup(elem->content); |
| 2970 | tmp = xmlStrcat(tmp, cur->next->content); |
| 2971 | xmlNodeSetContent(cur->next, tmp); |
| 2972 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2973 | xmlFreeNode(elem); |
| 2974 | return(cur->next); |
| 2975 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2976 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2977 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | if (elem->doc != cur->doc) { |
| 2981 | xmlSetTreeDoc(elem, cur->doc); |
| 2982 | } |
| 2983 | elem->parent = cur->parent; |
| 2984 | elem->prev = cur; |
| 2985 | elem->next = cur->next; |
| 2986 | cur->next = elem; |
| 2987 | if (elem->next != NULL) |
| 2988 | elem->next->prev = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2989 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2990 | elem->parent->last = elem; |
| 2991 | return(elem); |
| 2992 | } |
| 2993 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 2994 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 2995 | defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2996 | /** |
| 2997 | * xmlAddPrevSibling: |
| 2998 | * @cur: the child node |
| 2999 | * @elem: the new node |
| 3000 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3001 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3002 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3003 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3004 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3005 | * 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] | 3006 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3007 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3008 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3009 | */ |
| 3010 | xmlNodePtr |
| 3011 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 3012 | if (cur == NULL) { |
| 3013 | #ifdef DEBUG_TREE |
| 3014 | xmlGenericError(xmlGenericErrorContext, |
| 3015 | "xmlAddPrevSibling : cur == NULL\n"); |
| 3016 | #endif |
| 3017 | return(NULL); |
| 3018 | } |
| 3019 | if (elem == NULL) { |
| 3020 | #ifdef DEBUG_TREE |
| 3021 | xmlGenericError(xmlGenericErrorContext, |
| 3022 | "xmlAddPrevSibling : elem == NULL\n"); |
| 3023 | #endif |
| 3024 | return(NULL); |
| 3025 | } |
| 3026 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3027 | if (cur == elem) { |
| 3028 | #ifdef DEBUG_TREE |
| 3029 | xmlGenericError(xmlGenericErrorContext, |
| 3030 | "xmlAddPrevSibling : cur == elem\n"); |
| 3031 | #endif |
| 3032 | return(NULL); |
| 3033 | } |
| 3034 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3035 | xmlUnlinkNode(elem); |
| 3036 | |
| 3037 | if (elem->type == XML_TEXT_NODE) { |
| 3038 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3039 | xmlChar *tmp; |
| 3040 | |
| 3041 | tmp = xmlStrdup(elem->content); |
| 3042 | tmp = xmlStrcat(tmp, cur->content); |
| 3043 | xmlNodeSetContent(cur, tmp); |
| 3044 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3045 | xmlFreeNode(elem); |
| 3046 | return(cur); |
| 3047 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 3048 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 3049 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3050 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3051 | xmlFreeNode(elem); |
| 3052 | return(cur->prev); |
| 3053 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3054 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3055 | return xmlAddPropSibling(cur->prev, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | if (elem->doc != cur->doc) { |
| 3059 | xmlSetTreeDoc(elem, cur->doc); |
| 3060 | } |
| 3061 | elem->parent = cur->parent; |
| 3062 | elem->next = cur; |
| 3063 | elem->prev = cur->prev; |
| 3064 | cur->prev = elem; |
| 3065 | if (elem->prev != NULL) |
| 3066 | elem->prev->next = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3067 | if ((elem->parent != NULL) && (elem->parent->children == cur)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3068 | elem->parent->children = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3069 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3070 | return(elem); |
| 3071 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3072 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3073 | |
| 3074 | /** |
| 3075 | * xmlAddSibling: |
| 3076 | * @cur: the child node |
| 3077 | * @elem: the new node |
| 3078 | * |
| 3079 | * Add a new element @elem to the list of siblings of @cur |
| 3080 | * merging adjacent TEXT nodes (@elem may be freed) |
| 3081 | * If the new element was already inserted in a document it is |
| 3082 | * first unlinked from its existing context. |
| 3083 | * |
| 3084 | * Returns the new element or NULL in case of error. |
| 3085 | */ |
| 3086 | xmlNodePtr |
| 3087 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 3088 | xmlNodePtr parent; |
| 3089 | |
| 3090 | if (cur == NULL) { |
| 3091 | #ifdef DEBUG_TREE |
| 3092 | xmlGenericError(xmlGenericErrorContext, |
| 3093 | "xmlAddSibling : cur == NULL\n"); |
| 3094 | #endif |
| 3095 | return(NULL); |
| 3096 | } |
| 3097 | |
| 3098 | if (elem == NULL) { |
| 3099 | #ifdef DEBUG_TREE |
| 3100 | xmlGenericError(xmlGenericErrorContext, |
| 3101 | "xmlAddSibling : elem == NULL\n"); |
| 3102 | #endif |
| 3103 | return(NULL); |
| 3104 | } |
| 3105 | |
Daniel Veillard | 43bc89c | 2009-03-23 19:32:04 +0000 | [diff] [blame] | 3106 | if (cur == elem) { |
| 3107 | #ifdef DEBUG_TREE |
| 3108 | xmlGenericError(xmlGenericErrorContext, |
| 3109 | "xmlAddSibling : cur == elem\n"); |
| 3110 | #endif |
| 3111 | return(NULL); |
| 3112 | } |
| 3113 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3114 | /* |
| 3115 | * Constant time is we can rely on the ->parent->last to find |
| 3116 | * the last sibling. |
| 3117 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3118 | if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3119 | (cur->parent->children != NULL) && |
| 3120 | (cur->parent->last != NULL) && |
| 3121 | (cur->parent->last->next == NULL)) { |
| 3122 | cur = cur->parent->last; |
| 3123 | } else { |
| 3124 | while (cur->next != NULL) cur = cur->next; |
| 3125 | } |
| 3126 | |
| 3127 | xmlUnlinkNode(elem); |
| 3128 | |
Daniel Veillard | e22dd5c | 2003-10-29 12:53:27 +0000 | [diff] [blame] | 3129 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) && |
| 3130 | (cur->name == elem->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3131 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3132 | xmlFreeNode(elem); |
| 3133 | return(cur); |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3134 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 3135 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | if (elem->doc != cur->doc) { |
| 3139 | xmlSetTreeDoc(elem, cur->doc); |
| 3140 | } |
| 3141 | parent = cur->parent; |
| 3142 | elem->prev = cur; |
| 3143 | elem->next = NULL; |
| 3144 | elem->parent = parent; |
| 3145 | cur->next = elem; |
| 3146 | if (parent != NULL) |
| 3147 | parent->last = elem; |
| 3148 | |
| 3149 | return(elem); |
| 3150 | } |
| 3151 | |
| 3152 | /** |
| 3153 | * xmlAddChildList: |
| 3154 | * @parent: the parent node |
| 3155 | * @cur: the first node in the list |
| 3156 | * |
| 3157 | * Add a list of node at the end of the child list of the parent |
| 3158 | * merging adjacent TEXT nodes (@cur may be freed) |
| 3159 | * |
| 3160 | * Returns the last child or NULL in case of error. |
| 3161 | */ |
| 3162 | xmlNodePtr |
| 3163 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 3164 | xmlNodePtr prev; |
| 3165 | |
| 3166 | if (parent == NULL) { |
| 3167 | #ifdef DEBUG_TREE |
| 3168 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3169 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3170 | #endif |
| 3171 | return(NULL); |
| 3172 | } |
| 3173 | |
| 3174 | if (cur == NULL) { |
| 3175 | #ifdef DEBUG_TREE |
| 3176 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3177 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3178 | #endif |
| 3179 | return(NULL); |
| 3180 | } |
| 3181 | |
| 3182 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 3183 | (cur->doc != parent->doc)) { |
| 3184 | #ifdef DEBUG_TREE |
| 3185 | xmlGenericError(xmlGenericErrorContext, |
| 3186 | "Elements moved to a different document\n"); |
| 3187 | #endif |
| 3188 | } |
| 3189 | |
| 3190 | /* |
| 3191 | * add the first element at the end of the children list. |
| 3192 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3193 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3194 | if (parent->children == NULL) { |
| 3195 | parent->children = cur; |
| 3196 | } else { |
| 3197 | /* |
| 3198 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 3199 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3200 | if ((cur->type == XML_TEXT_NODE) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3201 | (parent->last->type == XML_TEXT_NODE) && |
| 3202 | (cur->name == parent->last->name)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3203 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3204 | /* |
| 3205 | * if it's the only child, nothing more to be done. |
| 3206 | */ |
| 3207 | if (cur->next == NULL) { |
| 3208 | xmlFreeNode(cur); |
| 3209 | return(parent->last); |
| 3210 | } |
| 3211 | prev = cur; |
| 3212 | cur = cur->next; |
| 3213 | xmlFreeNode(prev); |
| 3214 | } |
| 3215 | prev = parent->last; |
| 3216 | prev->next = cur; |
| 3217 | cur->prev = prev; |
| 3218 | } |
| 3219 | while (cur->next != NULL) { |
| 3220 | cur->parent = parent; |
| 3221 | if (cur->doc != parent->doc) { |
| 3222 | xmlSetTreeDoc(cur, parent->doc); |
| 3223 | } |
| 3224 | cur = cur->next; |
| 3225 | } |
| 3226 | cur->parent = parent; |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3227 | /* the parent may not be linked to a doc ! */ |
| 3228 | if (cur->doc != parent->doc) { |
| 3229 | xmlSetTreeDoc(cur, parent->doc); |
| 3230 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3231 | parent->last = cur; |
| 3232 | |
| 3233 | return(cur); |
| 3234 | } |
| 3235 | |
| 3236 | /** |
| 3237 | * xmlAddChild: |
| 3238 | * @parent: the parent node |
| 3239 | * @cur: the child node |
| 3240 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3241 | * 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] | 3242 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3243 | * 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] | 3244 | * If there is an attribute with equal name, it is first destroyed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3245 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3246 | * Returns the child or NULL in case of error. |
| 3247 | */ |
| 3248 | xmlNodePtr |
| 3249 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 3250 | xmlNodePtr prev; |
| 3251 | |
| 3252 | if (parent == NULL) { |
| 3253 | #ifdef DEBUG_TREE |
| 3254 | xmlGenericError(xmlGenericErrorContext, |
| 3255 | "xmlAddChild : parent == NULL\n"); |
| 3256 | #endif |
| 3257 | return(NULL); |
| 3258 | } |
| 3259 | |
| 3260 | if (cur == NULL) { |
| 3261 | #ifdef DEBUG_TREE |
| 3262 | xmlGenericError(xmlGenericErrorContext, |
| 3263 | "xmlAddChild : child == NULL\n"); |
| 3264 | #endif |
| 3265 | return(NULL); |
| 3266 | } |
| 3267 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3268 | if (parent == cur) { |
| 3269 | #ifdef DEBUG_TREE |
| 3270 | xmlGenericError(xmlGenericErrorContext, |
| 3271 | "xmlAddChild : parent == cur\n"); |
| 3272 | #endif |
| 3273 | return(NULL); |
| 3274 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3275 | /* |
| 3276 | * 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] | 3277 | * cur is then freed. |
| 3278 | */ |
| 3279 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3280 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3281 | (parent->content != NULL) && |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3282 | (parent->name == cur->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3283 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3284 | xmlFreeNode(cur); |
| 3285 | return(parent); |
| 3286 | } |
| 3287 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3288 | (parent->last->name == cur->name) && |
| 3289 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3290 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3291 | xmlFreeNode(cur); |
| 3292 | return(parent->last); |
| 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | /* |
| 3297 | * add the new element at the end of the children list. |
| 3298 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3299 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3300 | cur->parent = parent; |
| 3301 | if (cur->doc != parent->doc) { |
| 3302 | xmlSetTreeDoc(cur, parent->doc); |
| 3303 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3304 | /* this check prevents a loop on tree-traversions if a developer |
| 3305 | * tries to add a node to its parent multiple times |
| 3306 | */ |
| 3307 | if (prev == parent) |
| 3308 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3309 | |
| 3310 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3311 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3312 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3313 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3314 | (parent->content != NULL) && |
| 3315 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3316 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3317 | xmlFreeNode(cur); |
| 3318 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3319 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3320 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3321 | if (parent->type != XML_ELEMENT_NODE) |
| 3322 | return(NULL); |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3323 | if (parent->properties != NULL) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3324 | /* check if an attribute with the same name exists */ |
| 3325 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3326 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3327 | if (cur->ns == NULL) |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3328 | lastattr = xmlHasNsProp(parent, cur->name, NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3329 | else |
| 3330 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3331 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3332 | /* different instance, destroy it (attributes must be unique) */ |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3333 | xmlUnlinkNode((xmlNodePtr) lastattr); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3334 | xmlFreeProp(lastattr); |
| 3335 | } |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3336 | if (lastattr == (xmlAttrPtr) cur) |
| 3337 | return(cur); |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3338 | |
| 3339 | } |
| 3340 | if (parent->properties == NULL) { |
| 3341 | parent->properties = (xmlAttrPtr) cur; |
| 3342 | } else { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3343 | /* find the end */ |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3344 | xmlAttrPtr lastattr = parent->properties; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3345 | while (lastattr->next != NULL) { |
| 3346 | lastattr = lastattr->next; |
| 3347 | } |
| 3348 | lastattr->next = (xmlAttrPtr) cur; |
| 3349 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3350 | } |
| 3351 | } else { |
| 3352 | if (parent->children == NULL) { |
| 3353 | parent->children = cur; |
| 3354 | parent->last = cur; |
| 3355 | } else { |
| 3356 | prev = parent->last; |
| 3357 | prev->next = cur; |
| 3358 | cur->prev = prev; |
| 3359 | parent->last = cur; |
| 3360 | } |
| 3361 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3362 | return(cur); |
| 3363 | } |
| 3364 | |
| 3365 | /** |
| 3366 | * xmlGetLastChild: |
| 3367 | * @parent: the parent node |
| 3368 | * |
| 3369 | * Search the last child of a node. |
| 3370 | * Returns the last child or NULL if none. |
| 3371 | */ |
| 3372 | xmlNodePtr |
| 3373 | xmlGetLastChild(xmlNodePtr parent) { |
| 3374 | if (parent == NULL) { |
| 3375 | #ifdef DEBUG_TREE |
| 3376 | xmlGenericError(xmlGenericErrorContext, |
| 3377 | "xmlGetLastChild : parent == NULL\n"); |
| 3378 | #endif |
| 3379 | return(NULL); |
| 3380 | } |
| 3381 | return(parent->last); |
| 3382 | } |
| 3383 | |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3384 | #ifdef LIBXML_TREE_ENABLED |
| 3385 | /* |
| 3386 | * 5 interfaces from DOM ElementTraversal |
| 3387 | */ |
| 3388 | |
| 3389 | /** |
| 3390 | * xmlChildElementCount: |
| 3391 | * @parent: the parent node |
| 3392 | * |
| 3393 | * Finds the current number of child nodes of that element which are |
| 3394 | * element nodes. |
| 3395 | * Note the handling of entities references is different than in |
| 3396 | * the W3C DOM element traversal spec since we don't have back reference |
| 3397 | * from entities content to entities references. |
| 3398 | * |
| 3399 | * Returns the count of element child or 0 if not available |
| 3400 | */ |
| 3401 | unsigned long |
| 3402 | xmlChildElementCount(xmlNodePtr parent) { |
| 3403 | unsigned long ret = 0; |
| 3404 | xmlNodePtr cur = NULL; |
| 3405 | |
| 3406 | if (parent == NULL) |
| 3407 | return(0); |
| 3408 | switch (parent->type) { |
| 3409 | case XML_ELEMENT_NODE: |
| 3410 | case XML_ENTITY_NODE: |
| 3411 | case XML_DOCUMENT_NODE: |
| 3412 | case XML_HTML_DOCUMENT_NODE: |
| 3413 | cur = parent->children; |
| 3414 | break; |
| 3415 | default: |
| 3416 | return(0); |
| 3417 | } |
| 3418 | while (cur != NULL) { |
| 3419 | if (cur->type == XML_ELEMENT_NODE) |
| 3420 | ret++; |
| 3421 | cur = cur->next; |
| 3422 | } |
| 3423 | return(ret); |
| 3424 | } |
| 3425 | |
| 3426 | /** |
| 3427 | * xmlFirstElementChild: |
| 3428 | * @parent: the parent node |
| 3429 | * |
| 3430 | * Finds the first child node of that element which is a Element node |
| 3431 | * Note the handling of entities references is different than in |
| 3432 | * the W3C DOM element traversal spec since we don't have back reference |
| 3433 | * from entities content to entities references. |
| 3434 | * |
| 3435 | * Returns the first element child or NULL if not available |
| 3436 | */ |
| 3437 | xmlNodePtr |
| 3438 | xmlFirstElementChild(xmlNodePtr parent) { |
| 3439 | xmlNodePtr cur = NULL; |
| 3440 | |
| 3441 | if (parent == NULL) |
| 3442 | return(NULL); |
| 3443 | switch (parent->type) { |
| 3444 | case XML_ELEMENT_NODE: |
| 3445 | case XML_ENTITY_NODE: |
| 3446 | case XML_DOCUMENT_NODE: |
| 3447 | case XML_HTML_DOCUMENT_NODE: |
| 3448 | cur = parent->children; |
| 3449 | break; |
| 3450 | default: |
| 3451 | return(NULL); |
| 3452 | } |
| 3453 | while (cur != NULL) { |
| 3454 | if (cur->type == XML_ELEMENT_NODE) |
| 3455 | return(cur); |
| 3456 | cur = cur->next; |
| 3457 | } |
| 3458 | return(NULL); |
| 3459 | } |
| 3460 | |
| 3461 | /** |
| 3462 | * xmlLastElementChild: |
| 3463 | * @parent: the parent node |
| 3464 | * |
| 3465 | * Finds the last child node of that element which is a Element node |
| 3466 | * Note the handling of entities references is different than in |
| 3467 | * the W3C DOM element traversal spec since we don't have back reference |
| 3468 | * from entities content to entities references. |
| 3469 | * |
| 3470 | * Returns the last element child or NULL if not available |
| 3471 | */ |
| 3472 | xmlNodePtr |
| 3473 | xmlLastElementChild(xmlNodePtr parent) { |
| 3474 | xmlNodePtr cur = NULL; |
| 3475 | |
| 3476 | if (parent == NULL) |
| 3477 | return(NULL); |
| 3478 | switch (parent->type) { |
| 3479 | case XML_ELEMENT_NODE: |
| 3480 | case XML_ENTITY_NODE: |
| 3481 | case XML_DOCUMENT_NODE: |
| 3482 | case XML_HTML_DOCUMENT_NODE: |
| 3483 | cur = parent->last; |
| 3484 | break; |
| 3485 | default: |
| 3486 | return(NULL); |
| 3487 | } |
| 3488 | while (cur != NULL) { |
| 3489 | if (cur->type == XML_ELEMENT_NODE) |
| 3490 | return(cur); |
| 3491 | cur = cur->prev; |
| 3492 | } |
| 3493 | return(NULL); |
| 3494 | } |
| 3495 | |
| 3496 | /** |
| 3497 | * xmlPreviousElementSibling: |
| 3498 | * @node: the current node |
| 3499 | * |
| 3500 | * Finds the first closest previous sibling of the node which is an |
| 3501 | * element node. |
| 3502 | * Note the handling of entities references is different than in |
| 3503 | * the W3C DOM element traversal spec since we don't have back reference |
| 3504 | * from entities content to entities references. |
| 3505 | * |
| 3506 | * Returns the previous element sibling or NULL if not available |
| 3507 | */ |
| 3508 | xmlNodePtr |
| 3509 | xmlPreviousElementSibling(xmlNodePtr node) { |
| 3510 | if (node == NULL) |
| 3511 | return(NULL); |
| 3512 | switch (node->type) { |
| 3513 | case XML_ELEMENT_NODE: |
| 3514 | case XML_TEXT_NODE: |
| 3515 | case XML_CDATA_SECTION_NODE: |
| 3516 | case XML_ENTITY_REF_NODE: |
| 3517 | case XML_ENTITY_NODE: |
| 3518 | case XML_PI_NODE: |
| 3519 | case XML_COMMENT_NODE: |
| 3520 | case XML_XINCLUDE_START: |
| 3521 | case XML_XINCLUDE_END: |
| 3522 | node = node->prev; |
| 3523 | break; |
| 3524 | default: |
| 3525 | return(NULL); |
| 3526 | } |
| 3527 | while (node != NULL) { |
| 3528 | if (node->type == XML_ELEMENT_NODE) |
| 3529 | return(node); |
| 3530 | node = node->next; |
| 3531 | } |
| 3532 | return(NULL); |
| 3533 | } |
| 3534 | |
| 3535 | /** |
| 3536 | * xmlNextElementSibling: |
| 3537 | * @node: the current node |
| 3538 | * |
| 3539 | * Finds the first closest next sibling of the node which is an |
| 3540 | * element node. |
| 3541 | * Note the handling of entities references is different than in |
| 3542 | * the W3C DOM element traversal spec since we don't have back reference |
| 3543 | * from entities content to entities references. |
| 3544 | * |
| 3545 | * Returns the next element sibling or NULL if not available |
| 3546 | */ |
| 3547 | xmlNodePtr |
| 3548 | xmlNextElementSibling(xmlNodePtr node) { |
| 3549 | if (node == NULL) |
| 3550 | return(NULL); |
| 3551 | switch (node->type) { |
| 3552 | case XML_ELEMENT_NODE: |
| 3553 | case XML_TEXT_NODE: |
| 3554 | case XML_CDATA_SECTION_NODE: |
| 3555 | case XML_ENTITY_REF_NODE: |
| 3556 | case XML_ENTITY_NODE: |
| 3557 | case XML_PI_NODE: |
| 3558 | case XML_COMMENT_NODE: |
| 3559 | case XML_DTD_NODE: |
| 3560 | case XML_XINCLUDE_START: |
| 3561 | case XML_XINCLUDE_END: |
| 3562 | node = node->next; |
| 3563 | break; |
| 3564 | default: |
| 3565 | return(NULL); |
| 3566 | } |
| 3567 | while (node != NULL) { |
| 3568 | if (node->type == XML_ELEMENT_NODE) |
| 3569 | return(node); |
| 3570 | node = node->next; |
| 3571 | } |
| 3572 | return(NULL); |
| 3573 | } |
| 3574 | |
| 3575 | #endif /* LIBXML_TREE_ENABLED */ |
| 3576 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3577 | /** |
| 3578 | * xmlFreeNodeList: |
| 3579 | * @cur: the first node in the list |
| 3580 | * |
| 3581 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3582 | * the children are freed too. |
| 3583 | */ |
| 3584 | void |
| 3585 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3586 | xmlNodePtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3587 | xmlDictPtr dict = NULL; |
| 3588 | |
| 3589 | if (cur == NULL) return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3590 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3591 | xmlFreeNsList((xmlNsPtr) cur); |
| 3592 | return; |
| 3593 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3594 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3595 | #ifdef LIBXML_DOCB_ENABLED |
| 3596 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3597 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3598 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3599 | xmlFreeDoc((xmlDocPtr) cur); |
| 3600 | return; |
| 3601 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3602 | if (cur->doc != NULL) dict = cur->doc->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3603 | while (cur != NULL) { |
| 3604 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3605 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3606 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3607 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3608 | xmlDeregisterNodeDefaultValue(cur); |
| 3609 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3610 | if ((cur->children != NULL) && |
| 3611 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3612 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3613 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3614 | (cur->type == XML_XINCLUDE_START) || |
| 3615 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3616 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3617 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3618 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3619 | (cur->type != XML_XINCLUDE_START) && |
| 3620 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3621 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3622 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3623 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3624 | } |
| 3625 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3626 | (cur->type == XML_XINCLUDE_START) || |
| 3627 | (cur->type == XML_XINCLUDE_END)) && |
| 3628 | (cur->nsDef != NULL)) |
| 3629 | xmlFreeNsList(cur->nsDef); |
| 3630 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3631 | /* |
| 3632 | * When a node is a text node or a comment, it uses a global static |
| 3633 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3634 | * Otherwise the node name might come from the document's |
| 3635 | * dictionnary |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3636 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3637 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3638 | (cur->type != XML_TEXT_NODE) && |
| 3639 | (cur->type != XML_COMMENT_NODE)) |
| 3640 | DICT_FREE(cur->name) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3641 | xmlFree(cur); |
| 3642 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3643 | cur = next; |
| 3644 | } |
| 3645 | } |
| 3646 | |
| 3647 | /** |
| 3648 | * xmlFreeNode: |
| 3649 | * @cur: the node |
| 3650 | * |
| 3651 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3652 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3653 | */ |
| 3654 | void |
| 3655 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3656 | xmlDictPtr dict = NULL; |
| 3657 | |
| 3658 | if (cur == NULL) return; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3659 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3660 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3661 | if (cur->type == XML_DTD_NODE) { |
| 3662 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3663 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3664 | } |
| 3665 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3666 | xmlFreeNs((xmlNsPtr) cur); |
| 3667 | return; |
| 3668 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3669 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3670 | xmlFreeProp((xmlAttrPtr) cur); |
| 3671 | return; |
| 3672 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3673 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3674 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3675 | xmlDeregisterNodeDefaultValue(cur); |
| 3676 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3677 | if (cur->doc != NULL) dict = cur->doc->dict; |
| 3678 | |
Daniel Veillard | 2cba415 | 2008-08-27 11:45:41 +0000 | [diff] [blame] | 3679 | if (cur->type == XML_ENTITY_DECL) { |
| 3680 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 3681 | DICT_FREE(ent->SystemID); |
| 3682 | DICT_FREE(ent->ExternalID); |
| 3683 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3684 | if ((cur->children != NULL) && |
| 3685 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3686 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3687 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3688 | (cur->type == XML_XINCLUDE_START) || |
| 3689 | (cur->type == XML_XINCLUDE_END)) && |
| 3690 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3691 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3692 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3693 | (cur->content != NULL) && |
| 3694 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3695 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3696 | (cur->type != XML_XINCLUDE_START) && |
| 3697 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3698 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3701 | /* |
| 3702 | * When a node is a text node or a comment, it uses a global static |
| 3703 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3704 | * Otherwise the node name might come from the document's dictionnary |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3705 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3706 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3707 | (cur->type != XML_TEXT_NODE) && |
| 3708 | (cur->type != XML_COMMENT_NODE)) |
| 3709 | DICT_FREE(cur->name) |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3710 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3711 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3712 | (cur->type == XML_XINCLUDE_START) || |
| 3713 | (cur->type == XML_XINCLUDE_END)) && |
| 3714 | (cur->nsDef != NULL)) |
| 3715 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3716 | xmlFree(cur); |
| 3717 | } |
| 3718 | |
| 3719 | /** |
| 3720 | * xmlUnlinkNode: |
| 3721 | * @cur: the node |
| 3722 | * |
| 3723 | * Unlink a node from it's current context, the node is not freed |
| 3724 | */ |
| 3725 | void |
| 3726 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3727 | if (cur == NULL) { |
| 3728 | #ifdef DEBUG_TREE |
| 3729 | xmlGenericError(xmlGenericErrorContext, |
| 3730 | "xmlUnlinkNode : node == NULL\n"); |
| 3731 | #endif |
| 3732 | return; |
| 3733 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3734 | if (cur->type == XML_DTD_NODE) { |
| 3735 | xmlDocPtr doc; |
| 3736 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3737 | if (doc != NULL) { |
| 3738 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3739 | doc->intSubset = NULL; |
| 3740 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3741 | doc->extSubset = NULL; |
| 3742 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3743 | } |
Daniel Veillard | 2cba415 | 2008-08-27 11:45:41 +0000 | [diff] [blame] | 3744 | if (cur->type == XML_ENTITY_DECL) { |
| 3745 | xmlDocPtr doc; |
| 3746 | doc = cur->doc; |
| 3747 | if (doc != NULL) { |
| 3748 | if (doc->intSubset != NULL) { |
| 3749 | if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur) |
| 3750 | xmlHashRemoveEntry(doc->intSubset->entities, cur->name, |
| 3751 | NULL); |
| 3752 | if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur) |
| 3753 | xmlHashRemoveEntry(doc->intSubset->pentities, cur->name, |
| 3754 | NULL); |
| 3755 | } |
| 3756 | if (doc->extSubset != NULL) { |
| 3757 | if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur) |
| 3758 | xmlHashRemoveEntry(doc->extSubset->entities, cur->name, |
| 3759 | NULL); |
| 3760 | if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur) |
| 3761 | xmlHashRemoveEntry(doc->extSubset->pentities, cur->name, |
| 3762 | NULL); |
| 3763 | } |
| 3764 | } |
| 3765 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3766 | if (cur->parent != NULL) { |
| 3767 | xmlNodePtr parent; |
| 3768 | parent = cur->parent; |
| 3769 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3770 | if (parent->properties == (xmlAttrPtr) cur) |
| 3771 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3772 | } else { |
| 3773 | if (parent->children == cur) |
| 3774 | parent->children = cur->next; |
| 3775 | if (parent->last == cur) |
| 3776 | parent->last = cur->prev; |
| 3777 | } |
| 3778 | cur->parent = NULL; |
| 3779 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3780 | if (cur->next != NULL) |
| 3781 | cur->next->prev = cur->prev; |
| 3782 | if (cur->prev != NULL) |
| 3783 | cur->prev->next = cur->next; |
| 3784 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 3787 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3788 | /** |
| 3789 | * xmlReplaceNode: |
| 3790 | * @old: the old node |
| 3791 | * @cur: the node |
| 3792 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 3793 | * Unlink the old node from its current context, prune the new one |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3794 | * 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] | 3795 | * first unlinked from its existing context. |
| 3796 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3797 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3798 | */ |
| 3799 | xmlNodePtr |
| 3800 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 3801 | if (old == cur) return(NULL); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3802 | if ((old == NULL) || (old->parent == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3803 | #ifdef DEBUG_TREE |
| 3804 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3805 | "xmlReplaceNode : old == NULL or without parent\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3806 | #endif |
| 3807 | return(NULL); |
| 3808 | } |
| 3809 | if (cur == NULL) { |
| 3810 | xmlUnlinkNode(old); |
| 3811 | return(old); |
| 3812 | } |
| 3813 | if (cur == old) { |
| 3814 | return(old); |
| 3815 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3816 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3817 | #ifdef DEBUG_TREE |
| 3818 | xmlGenericError(xmlGenericErrorContext, |
| 3819 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3820 | #endif |
| 3821 | return(old); |
| 3822 | } |
| 3823 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3824 | #ifdef DEBUG_TREE |
| 3825 | xmlGenericError(xmlGenericErrorContext, |
| 3826 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3827 | #endif |
| 3828 | return(old); |
| 3829 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3830 | xmlUnlinkNode(cur); |
Daniel Veillard | 64d7d12 | 2005-05-11 18:03:42 +0000 | [diff] [blame] | 3831 | xmlSetTreeDoc(cur, old->doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3832 | cur->parent = old->parent; |
| 3833 | cur->next = old->next; |
| 3834 | if (cur->next != NULL) |
| 3835 | cur->next->prev = cur; |
| 3836 | cur->prev = old->prev; |
| 3837 | if (cur->prev != NULL) |
| 3838 | cur->prev->next = cur; |
| 3839 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3840 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3841 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3842 | cur->parent->properties = ((xmlAttrPtr) cur); |
| 3843 | } else { |
| 3844 | if (cur->parent->children == old) |
| 3845 | cur->parent->children = cur; |
| 3846 | if (cur->parent->last == old) |
| 3847 | cur->parent->last = cur; |
| 3848 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3849 | } |
| 3850 | old->next = old->prev = NULL; |
| 3851 | old->parent = NULL; |
| 3852 | return(old); |
| 3853 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3854 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3855 | |
| 3856 | /************************************************************************ |
| 3857 | * * |
| 3858 | * Copy operations * |
| 3859 | * * |
| 3860 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3861 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3862 | /** |
| 3863 | * xmlCopyNamespace: |
| 3864 | * @cur: the namespace |
| 3865 | * |
| 3866 | * Do a copy of the namespace. |
| 3867 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3868 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3869 | */ |
| 3870 | xmlNsPtr |
| 3871 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3872 | xmlNsPtr ret; |
| 3873 | |
| 3874 | if (cur == NULL) return(NULL); |
| 3875 | switch (cur->type) { |
| 3876 | case XML_LOCAL_NAMESPACE: |
| 3877 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3878 | break; |
| 3879 | default: |
| 3880 | #ifdef DEBUG_TREE |
| 3881 | xmlGenericError(xmlGenericErrorContext, |
| 3882 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3883 | #endif |
| 3884 | return(NULL); |
| 3885 | } |
| 3886 | return(ret); |
| 3887 | } |
| 3888 | |
| 3889 | /** |
| 3890 | * xmlCopyNamespaceList: |
| 3891 | * @cur: the first namespace |
| 3892 | * |
| 3893 | * Do a copy of an namespace list. |
| 3894 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3895 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3896 | */ |
| 3897 | xmlNsPtr |
| 3898 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3899 | xmlNsPtr ret = NULL; |
| 3900 | xmlNsPtr p = NULL,q; |
| 3901 | |
| 3902 | while (cur != NULL) { |
| 3903 | q = xmlCopyNamespace(cur); |
| 3904 | if (p == NULL) { |
| 3905 | ret = p = q; |
| 3906 | } else { |
| 3907 | p->next = q; |
| 3908 | p = q; |
| 3909 | } |
| 3910 | cur = cur->next; |
| 3911 | } |
| 3912 | return(ret); |
| 3913 | } |
| 3914 | |
| 3915 | static xmlNodePtr |
| 3916 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3917 | |
| 3918 | static xmlAttrPtr |
| 3919 | xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3920 | xmlAttrPtr ret; |
| 3921 | |
| 3922 | if (cur == NULL) return(NULL); |
| 3923 | if (target != NULL) |
| 3924 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3925 | else if (doc != NULL) |
| 3926 | ret = xmlNewDocProp(doc, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3927 | else if (cur->parent != NULL) |
| 3928 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 3929 | else if (cur->children != NULL) |
| 3930 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 3931 | else |
| 3932 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 3933 | if (ret == NULL) return(NULL); |
| 3934 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3935 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3936 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3937 | xmlNsPtr ns; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3938 | |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3939 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3940 | if (ns == NULL) { |
| 3941 | /* |
| 3942 | * Humm, we are copying an element whose namespace is defined |
| 3943 | * out of the new tree scope. Search it in the original tree |
| 3944 | * and add it at the top of the new tree |
| 3945 | */ |
| 3946 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 3947 | if (ns != NULL) { |
| 3948 | xmlNodePtr root = target; |
| 3949 | xmlNodePtr pred = NULL; |
| 3950 | |
| 3951 | while (root->parent != NULL) { |
| 3952 | pred = root; |
| 3953 | root = root->parent; |
| 3954 | } |
| 3955 | if (root == (xmlNodePtr) target->doc) { |
| 3956 | /* correct possibly cycling above the document elt */ |
| 3957 | root = pred; |
| 3958 | } |
| 3959 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 3960 | } |
| 3961 | } else { |
| 3962 | /* |
| 3963 | * we have to find something appropriate here since |
| 3964 | * we cant be sure, that the namespce we found is identified |
| 3965 | * by the prefix |
| 3966 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 3967 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3968 | /* this is the nice case */ |
| 3969 | ret->ns = ns; |
| 3970 | } else { |
| 3971 | /* |
| 3972 | * we are in trouble: we need a new reconcilied namespace. |
| 3973 | * This is expensive |
| 3974 | */ |
| 3975 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 3976 | } |
| 3977 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3978 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3979 | } else |
| 3980 | ret->ns = NULL; |
| 3981 | |
| 3982 | if (cur->children != NULL) { |
| 3983 | xmlNodePtr tmp; |
| 3984 | |
| 3985 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 3986 | ret->last = NULL; |
| 3987 | tmp = ret->children; |
| 3988 | while (tmp != NULL) { |
| 3989 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 3990 | if (tmp->next == NULL) |
| 3991 | ret->last = tmp; |
| 3992 | tmp = tmp->next; |
| 3993 | } |
| 3994 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 3995 | /* |
| 3996 | * Try to handle IDs |
| 3997 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 3998 | if ((target!= NULL) && (cur!= NULL) && |
| 3999 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 4000 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 4001 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 4002 | xmlChar *id; |
| 4003 | |
| 4004 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 4005 | if (id != NULL) { |
| 4006 | xmlAddID(NULL, target->doc, id, ret); |
| 4007 | xmlFree(id); |
| 4008 | } |
| 4009 | } |
| 4010 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4011 | return(ret); |
| 4012 | } |
| 4013 | |
| 4014 | /** |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 4015 | * xmlCopyProp: |
| 4016 | * @target: the element where the attribute will be grafted |
| 4017 | * @cur: the attribute |
| 4018 | * |
| 4019 | * Do a copy of the attribute. |
| 4020 | * |
| 4021 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
| 4022 | */ |
| 4023 | xmlAttrPtr |
| 4024 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 4025 | return xmlCopyPropInternal(NULL, target, cur); |
| 4026 | } |
| 4027 | |
| 4028 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4029 | * xmlCopyPropList: |
| 4030 | * @target: the element where the attributes will be grafted |
| 4031 | * @cur: the first attribute |
| 4032 | * |
| 4033 | * Do a copy of an attribute list. |
| 4034 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4035 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4036 | */ |
| 4037 | xmlAttrPtr |
| 4038 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 4039 | xmlAttrPtr ret = NULL; |
| 4040 | xmlAttrPtr p = NULL,q; |
| 4041 | |
| 4042 | while (cur != NULL) { |
| 4043 | q = xmlCopyProp(target, cur); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4044 | if (q == NULL) |
| 4045 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4046 | if (p == NULL) { |
| 4047 | ret = p = q; |
| 4048 | } else { |
| 4049 | p->next = q; |
| 4050 | q->prev = p; |
| 4051 | p = q; |
| 4052 | } |
| 4053 | cur = cur->next; |
| 4054 | } |
| 4055 | return(ret); |
| 4056 | } |
| 4057 | |
| 4058 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4059 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4060 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4061 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4062 | * tricky reason: namespaces. Doing a direct copy of a node |
| 4063 | * say RPM:Copyright without changing the namespace pointer to |
| 4064 | * something else can produce stale links. One way to do it is |
| 4065 | * to keep a reference counter but this doesn't work as soon |
| 4066 | * as one move the element or the subtree out of the scope of |
| 4067 | * the existing namespace. The actual solution seems to add |
| 4068 | * a copy of the namespace at the top of the copied tree if |
| 4069 | * not available in the subtree. |
| 4070 | * Hence two functions, the public front-end call the inner ones |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4071 | * The argument "recursive" normally indicates a recursive copy |
| 4072 | * of the node with values 0 (no) and 1 (yes). For XInclude, |
| 4073 | * however, we allow a value of 2 to indicate copy properties and |
| 4074 | * namespace info, but don't recurse on children. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4075 | */ |
| 4076 | |
| 4077 | static xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 4078 | xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4079 | int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4080 | xmlNodePtr ret; |
| 4081 | |
| 4082 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4083 | switch (node->type) { |
| 4084 | case XML_TEXT_NODE: |
| 4085 | case XML_CDATA_SECTION_NODE: |
| 4086 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 4087 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4088 | case XML_ENTITY_REF_NODE: |
| 4089 | case XML_ENTITY_NODE: |
| 4090 | case XML_PI_NODE: |
| 4091 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4092 | case XML_XINCLUDE_START: |
| 4093 | case XML_XINCLUDE_END: |
| 4094 | break; |
| 4095 | case XML_ATTRIBUTE_NODE: |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 4096 | return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node)); |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4097 | case XML_NAMESPACE_DECL: |
| 4098 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4099 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4100 | case XML_DOCUMENT_NODE: |
| 4101 | case XML_HTML_DOCUMENT_NODE: |
| 4102 | #ifdef LIBXML_DOCB_ENABLED |
| 4103 | case XML_DOCB_DOCUMENT_NODE: |
| 4104 | #endif |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4105 | #ifdef LIBXML_TREE_ENABLED |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4106 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended)); |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4107 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4108 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4109 | case XML_NOTATION_NODE: |
| 4110 | case XML_DTD_NODE: |
| 4111 | case XML_ELEMENT_DECL: |
| 4112 | case XML_ATTRIBUTE_DECL: |
| 4113 | case XML_ENTITY_DECL: |
| 4114 | return(NULL); |
| 4115 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4116 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4117 | /* |
| 4118 | * Allocate a new node and fill the fields. |
| 4119 | */ |
| 4120 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 4121 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4122 | xmlTreeErrMemory("copying node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4123 | return(NULL); |
| 4124 | } |
| 4125 | memset(ret, 0, sizeof(xmlNode)); |
| 4126 | ret->type = node->type; |
| 4127 | |
| 4128 | ret->doc = doc; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4129 | ret->parent = parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4130 | if (node->name == xmlStringText) |
| 4131 | ret->name = xmlStringText; |
| 4132 | else if (node->name == xmlStringTextNoenc) |
| 4133 | ret->name = xmlStringTextNoenc; |
| 4134 | else if (node->name == xmlStringComment) |
| 4135 | ret->name = xmlStringComment; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4136 | else if (node->name != NULL) { |
| 4137 | if ((doc != NULL) && (doc->dict != NULL)) |
| 4138 | ret->name = xmlDictLookup(doc->dict, node->name, -1); |
| 4139 | else |
| 4140 | ret->name = xmlStrdup(node->name); |
| 4141 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 4142 | if ((node->type != XML_ELEMENT_NODE) && |
| 4143 | (node->content != NULL) && |
| 4144 | (node->type != XML_ENTITY_REF_NODE) && |
| 4145 | (node->type != XML_XINCLUDE_END) && |
| 4146 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4147 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 4148 | }else{ |
| 4149 | if (node->type == XML_ELEMENT_NODE) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 4150 | ret->line = node->line; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4151 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4152 | if (parent != NULL) { |
| 4153 | xmlNodePtr tmp; |
| 4154 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4155 | /* |
| 4156 | * this is a tricky part for the node register thing: |
| 4157 | * in case ret does get coalesced in xmlAddChild |
| 4158 | * the deregister-node callback is called; so we register ret now already |
| 4159 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 4160 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4161 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 4162 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4163 | tmp = xmlAddChild(parent, ret); |
| 4164 | /* node could have coalesced */ |
| 4165 | if (tmp != ret) |
| 4166 | return(tmp); |
| 4167 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4168 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4169 | if (!extended) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4170 | goto out; |
Petr Pajas | 2afca4a | 2009-07-30 17:47:32 +0200 | [diff] [blame] | 4171 | if (((node->type == XML_ELEMENT_NODE) || |
| 4172 | (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4173 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 4174 | |
| 4175 | if (node->ns != NULL) { |
| 4176 | xmlNsPtr ns; |
| 4177 | |
| 4178 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 4179 | if (ns == NULL) { |
| 4180 | /* |
| 4181 | * Humm, we are copying an element whose namespace is defined |
| 4182 | * out of the new tree scope. Search it in the original tree |
| 4183 | * and add it at the top of the new tree |
| 4184 | */ |
| 4185 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 4186 | if (ns != NULL) { |
| 4187 | xmlNodePtr root = ret; |
| 4188 | |
| 4189 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 4190 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4191 | } |
| 4192 | } else { |
| 4193 | /* |
| 4194 | * reference the existing namespace definition in our own tree. |
| 4195 | */ |
| 4196 | ret->ns = ns; |
| 4197 | } |
| 4198 | } |
Petr Pajas | 2afca4a | 2009-07-30 17:47:32 +0200 | [diff] [blame] | 4199 | if (((node->type == XML_ELEMENT_NODE) || |
| 4200 | (node->type == XML_XINCLUDE_START)) && (node->properties != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4201 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4202 | if (node->type == XML_ENTITY_REF_NODE) { |
| 4203 | if ((doc == NULL) || (node->doc != doc)) { |
| 4204 | /* |
| 4205 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4206 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4207 | * we cannot keep the reference. Try to find it in the |
| 4208 | * target document. |
| 4209 | */ |
| 4210 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 4211 | } else { |
| 4212 | ret->children = node->children; |
| 4213 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 4214 | ret->last = ret->children; |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4215 | } else if ((node->children != NULL) && (extended != 2)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4216 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 4217 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 4218 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4219 | |
| 4220 | out: |
| 4221 | /* if parent != NULL we already registered the node above */ |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 4222 | if ((parent == NULL) && |
| 4223 | ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4224 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4225 | return(ret); |
| 4226 | } |
| 4227 | |
| 4228 | static xmlNodePtr |
| 4229 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 4230 | xmlNodePtr ret = NULL; |
| 4231 | xmlNodePtr p = NULL,q; |
| 4232 | |
| 4233 | while (node != NULL) { |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4234 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4235 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 4236 | if (doc == NULL) { |
| 4237 | node = node->next; |
| 4238 | continue; |
| 4239 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4240 | if (doc->intSubset == NULL) { |
| 4241 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
| 4242 | q->doc = doc; |
| 4243 | q->parent = parent; |
| 4244 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4245 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4246 | } else { |
| 4247 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4248 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4249 | } |
| 4250 | } else |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4251 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4252 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4253 | if (ret == NULL) { |
| 4254 | q->prev = NULL; |
| 4255 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4256 | } else if (p != q) { |
| 4257 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4258 | p->next = q; |
| 4259 | q->prev = p; |
| 4260 | p = q; |
| 4261 | } |
| 4262 | node = node->next; |
| 4263 | } |
| 4264 | return(ret); |
| 4265 | } |
| 4266 | |
| 4267 | /** |
| 4268 | * xmlCopyNode: |
| 4269 | * @node: the node |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4270 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4271 | * when applicable) |
| 4272 | * if 2 copy properties and namespaces (when applicable) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4273 | * |
| 4274 | * Do a copy of the node. |
| 4275 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4276 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4277 | */ |
| 4278 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4279 | xmlCopyNode(const xmlNodePtr node, int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4280 | xmlNodePtr ret; |
| 4281 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4282 | ret = xmlStaticCopyNode(node, NULL, NULL, extended); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4283 | return(ret); |
| 4284 | } |
| 4285 | |
| 4286 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4287 | * xmlDocCopyNode: |
| 4288 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4289 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4290 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4291 | * when applicable) |
| 4292 | * if 2 copy properties and namespaces (when applicable) |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4293 | * |
| 4294 | * Do a copy of the node to a given document. |
| 4295 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4296 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4297 | */ |
| 4298 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4299 | xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4300 | xmlNodePtr ret; |
| 4301 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4302 | ret = xmlStaticCopyNode(node, doc, NULL, extended); |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4303 | return(ret); |
| 4304 | } |
| 4305 | |
| 4306 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4307 | * xmlDocCopyNodeList: |
| 4308 | * @doc: the target document |
| 4309 | * @node: the first node in the list. |
| 4310 | * |
| 4311 | * Do a recursive copy of the node list. |
| 4312 | * |
| 4313 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
| 4314 | */ |
| 4315 | xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) { |
| 4316 | xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL); |
| 4317 | return(ret); |
| 4318 | } |
| 4319 | |
| 4320 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4321 | * xmlCopyNodeList: |
| 4322 | * @node: the first node in the list. |
| 4323 | * |
| 4324 | * Do a recursive copy of the node list. |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4325 | * Use xmlDocCopyNodeList() if possible to ensure string interning. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4326 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4327 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4328 | */ |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 4329 | xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4330 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 4331 | return(ret); |
| 4332 | } |
| 4333 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4334 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4335 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4336 | * xmlCopyDtd: |
| 4337 | * @dtd: the dtd |
| 4338 | * |
| 4339 | * Do a copy of the dtd. |
| 4340 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4341 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4342 | */ |
| 4343 | xmlDtdPtr |
| 4344 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 4345 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4346 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4347 | |
| 4348 | if (dtd == NULL) return(NULL); |
| 4349 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 4350 | if (ret == NULL) return(NULL); |
| 4351 | if (dtd->entities != NULL) |
| 4352 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 4353 | (xmlEntitiesTablePtr) dtd->entities); |
| 4354 | if (dtd->notations != NULL) |
| 4355 | ret->notations = (void *) xmlCopyNotationTable( |
| 4356 | (xmlNotationTablePtr) dtd->notations); |
| 4357 | if (dtd->elements != NULL) |
| 4358 | ret->elements = (void *) xmlCopyElementTable( |
| 4359 | (xmlElementTablePtr) dtd->elements); |
| 4360 | if (dtd->attributes != NULL) |
| 4361 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 4362 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4363 | if (dtd->pentities != NULL) |
| 4364 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 4365 | (xmlEntitiesTablePtr) dtd->pentities); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4366 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4367 | cur = dtd->children; |
| 4368 | while (cur != NULL) { |
| 4369 | q = NULL; |
| 4370 | |
| 4371 | if (cur->type == XML_ENTITY_DECL) { |
| 4372 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 4373 | switch (tmp->etype) { |
| 4374 | case XML_INTERNAL_GENERAL_ENTITY: |
| 4375 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 4376 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 4377 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 4378 | break; |
| 4379 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 4380 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4381 | q = (xmlNodePtr) |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4382 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 4383 | break; |
| 4384 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 4385 | break; |
| 4386 | } |
| 4387 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 4388 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 4389 | q = (xmlNodePtr) |
| 4390 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 4391 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4392 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4393 | q = (xmlNodePtr) |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4394 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 4395 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4396 | q = xmlCopyNode(cur, 0); |
| 4397 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4398 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4399 | if (q == NULL) { |
| 4400 | cur = cur->next; |
| 4401 | continue; |
| 4402 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4403 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4404 | if (p == NULL) |
| 4405 | ret->children = q; |
| 4406 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4407 | p->next = q; |
| 4408 | |
| 4409 | q->prev = p; |
| 4410 | q->parent = (xmlNodePtr) ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4411 | q->next = NULL; |
| 4412 | ret->last = q; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4413 | p = q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4414 | cur = cur->next; |
| 4415 | } |
| 4416 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4417 | return(ret); |
| 4418 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4419 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4420 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4421 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4422 | /** |
| 4423 | * xmlCopyDoc: |
| 4424 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4425 | * @recursive: if not zero do a recursive copy. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4426 | * |
| 4427 | * 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] | 4428 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4429 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4430 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4431 | */ |
| 4432 | xmlDocPtr |
| 4433 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 4434 | xmlDocPtr ret; |
| 4435 | |
| 4436 | if (doc == NULL) return(NULL); |
| 4437 | ret = xmlNewDoc(doc->version); |
| 4438 | if (ret == NULL) return(NULL); |
| 4439 | if (doc->name != NULL) |
| 4440 | ret->name = xmlMemStrdup(doc->name); |
| 4441 | if (doc->encoding != NULL) |
| 4442 | ret->encoding = xmlStrdup(doc->encoding); |
Daniel Veillard | f59507d | 2005-01-27 17:26:49 +0000 | [diff] [blame] | 4443 | if (doc->URL != NULL) |
| 4444 | ret->URL = xmlStrdup(doc->URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4445 | ret->charset = doc->charset; |
| 4446 | ret->compression = doc->compression; |
| 4447 | ret->standalone = doc->standalone; |
| 4448 | if (!recursive) return(ret); |
| 4449 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4450 | ret->last = NULL; |
| 4451 | ret->children = NULL; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4452 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4453 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4454 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4455 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4456 | ret->intSubset->parent = ret; |
| 4457 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4458 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4459 | if (doc->oldNs != NULL) |
| 4460 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 4461 | if (doc->children != NULL) { |
| 4462 | xmlNodePtr tmp; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4463 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4464 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 4465 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4466 | ret->last = NULL; |
| 4467 | tmp = ret->children; |
| 4468 | while (tmp != NULL) { |
| 4469 | if (tmp->next == NULL) |
| 4470 | ret->last = tmp; |
| 4471 | tmp = tmp->next; |
| 4472 | } |
| 4473 | } |
| 4474 | return(ret); |
| 4475 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4476 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4477 | |
| 4478 | /************************************************************************ |
| 4479 | * * |
| 4480 | * Content access functions * |
| 4481 | * * |
| 4482 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4483 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4484 | /** |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4485 | * xmlGetLineNo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4486 | * @node: valid node |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4487 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 4488 | * Get line number of @node. This requires activation of this option |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4489 | * before invoking the parser by calling xmlLineNumbersDefault(1) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4490 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4491 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4492 | */ |
| 4493 | long |
| 4494 | xmlGetLineNo(xmlNodePtr node) |
| 4495 | { |
| 4496 | long result = -1; |
| 4497 | |
| 4498 | if (!node) |
| 4499 | return result; |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4500 | if ((node->type == XML_ELEMENT_NODE) || |
| 4501 | (node->type == XML_TEXT_NODE) || |
| 4502 | (node->type == XML_COMMENT_NODE) || |
| 4503 | (node->type == XML_PI_NODE)) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 4504 | result = (long) node->line; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4505 | else if ((node->prev != NULL) && |
| 4506 | ((node->prev->type == XML_ELEMENT_NODE) || |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4507 | (node->prev->type == XML_TEXT_NODE) || |
| 4508 | (node->prev->type == XML_COMMENT_NODE) || |
| 4509 | (node->prev->type == XML_PI_NODE))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4510 | result = xmlGetLineNo(node->prev); |
| 4511 | else if ((node->parent != NULL) && |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4512 | (node->parent->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4513 | result = xmlGetLineNo(node->parent); |
| 4514 | |
| 4515 | return result; |
| 4516 | } |
| 4517 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4518 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4519 | /** |
| 4520 | * xmlGetNodePath: |
| 4521 | * @node: a node |
| 4522 | * |
| 4523 | * Build a structure based Path for the given node |
| 4524 | * |
| 4525 | * Returns the new path or NULL in case of error. The caller must free |
| 4526 | * the returned string |
| 4527 | */ |
| 4528 | xmlChar * |
| 4529 | xmlGetNodePath(xmlNodePtr node) |
| 4530 | { |
| 4531 | xmlNodePtr cur, tmp, next; |
| 4532 | xmlChar *buffer = NULL, *temp; |
| 4533 | size_t buf_len; |
| 4534 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4535 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4536 | const char *name; |
| 4537 | char nametemp[100]; |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4538 | int occur = 0, generic; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4539 | |
| 4540 | if (node == NULL) |
| 4541 | return (NULL); |
| 4542 | |
| 4543 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4544 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4545 | if (buffer == NULL) { |
| 4546 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4547 | return (NULL); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4548 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4549 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4550 | if (buf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4551 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4552 | xmlFree(buffer); |
| 4553 | return (NULL); |
| 4554 | } |
| 4555 | |
| 4556 | buffer[0] = 0; |
| 4557 | cur = node; |
| 4558 | do { |
| 4559 | name = ""; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4560 | sep = "?"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4561 | occur = 0; |
| 4562 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4563 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4564 | if (buffer[0] == '/') |
| 4565 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4566 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4567 | next = NULL; |
| 4568 | } else if (cur->type == XML_ELEMENT_NODE) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4569 | generic = 0; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4570 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4571 | name = (const char *) cur->name; |
| 4572 | if (cur->ns) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4573 | if (cur->ns->prefix != NULL) { |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4574 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4575 | (char *)cur->ns->prefix, (char *)cur->name); |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4576 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4577 | name = nametemp; |
| 4578 | } else { |
| 4579 | /* |
| 4580 | * We cannot express named elements in the default |
| 4581 | * namespace, so use "*". |
| 4582 | */ |
| 4583 | generic = 1; |
| 4584 | name = "*"; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4585 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4586 | } |
| 4587 | next = cur->parent; |
| 4588 | |
| 4589 | /* |
| 4590 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4591 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4592 | */ |
| 4593 | tmp = cur->prev; |
| 4594 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4595 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4596 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4597 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4598 | ((tmp->ns == cur->ns) || |
| 4599 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4600 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4601 | occur++; |
| 4602 | tmp = tmp->prev; |
| 4603 | } |
| 4604 | if (occur == 0) { |
| 4605 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4606 | while (tmp != NULL && occur == 0) { |
| 4607 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4608 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4609 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4610 | ((tmp->ns == cur->ns) || |
| 4611 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4612 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4613 | occur++; |
| 4614 | tmp = tmp->next; |
| 4615 | } |
| 4616 | if (occur != 0) |
| 4617 | occur = 1; |
| 4618 | } else |
| 4619 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4620 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4621 | sep = "/"; |
| 4622 | name = "comment()"; |
| 4623 | next = cur->parent; |
| 4624 | |
| 4625 | /* |
| 4626 | * Thumbler index computation |
| 4627 | */ |
| 4628 | tmp = cur->prev; |
| 4629 | while (tmp != NULL) { |
| 4630 | if (tmp->type == XML_COMMENT_NODE) |
| 4631 | occur++; |
| 4632 | tmp = tmp->prev; |
| 4633 | } |
| 4634 | if (occur == 0) { |
| 4635 | tmp = cur->next; |
| 4636 | while (tmp != NULL && occur == 0) { |
| 4637 | if (tmp->type == XML_COMMENT_NODE) |
| 4638 | occur++; |
| 4639 | tmp = tmp->next; |
| 4640 | } |
| 4641 | if (occur != 0) |
| 4642 | occur = 1; |
| 4643 | } else |
| 4644 | occur++; |
| 4645 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4646 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4647 | sep = "/"; |
| 4648 | name = "text()"; |
| 4649 | next = cur->parent; |
| 4650 | |
| 4651 | /* |
| 4652 | * Thumbler index computation |
| 4653 | */ |
| 4654 | tmp = cur->prev; |
| 4655 | while (tmp != NULL) { |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4656 | if ((tmp->type == XML_TEXT_NODE) || |
| 4657 | (tmp->type == XML_CDATA_SECTION_NODE)) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4658 | occur++; |
| 4659 | tmp = tmp->prev; |
| 4660 | } |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4661 | /* |
| 4662 | * Evaluate if this is the only text- or CDATA-section-node; |
| 4663 | * if yes, then we'll get "text()", otherwise "text()[1]". |
| 4664 | */ |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4665 | if (occur == 0) { |
| 4666 | tmp = cur->next; |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4667 | while (tmp != NULL) { |
| 4668 | if ((tmp->type == XML_TEXT_NODE) || |
| 4669 | (tmp->type == XML_CDATA_SECTION_NODE)) |
| 4670 | { |
| 4671 | occur = 1; |
| 4672 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4673 | } |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4674 | tmp = tmp->next; |
| 4675 | } |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4676 | } else |
| 4677 | occur++; |
| 4678 | } else if (cur->type == XML_PI_NODE) { |
| 4679 | sep = "/"; |
| 4680 | snprintf(nametemp, sizeof(nametemp) - 1, |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4681 | "processing-instruction('%s')", (char *)cur->name); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4682 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4683 | name = nametemp; |
| 4684 | |
| 4685 | next = cur->parent; |
| 4686 | |
| 4687 | /* |
| 4688 | * Thumbler index computation |
| 4689 | */ |
| 4690 | tmp = cur->prev; |
| 4691 | while (tmp != NULL) { |
| 4692 | if ((tmp->type == XML_PI_NODE) && |
| 4693 | (xmlStrEqual(cur->name, tmp->name))) |
| 4694 | occur++; |
| 4695 | tmp = tmp->prev; |
| 4696 | } |
| 4697 | if (occur == 0) { |
| 4698 | tmp = cur->next; |
| 4699 | while (tmp != NULL && occur == 0) { |
| 4700 | if ((tmp->type == XML_PI_NODE) && |
| 4701 | (xmlStrEqual(cur->name, tmp->name))) |
| 4702 | occur++; |
| 4703 | tmp = tmp->next; |
| 4704 | } |
| 4705 | if (occur != 0) |
| 4706 | occur = 1; |
| 4707 | } else |
| 4708 | occur++; |
| 4709 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4710 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4711 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4712 | name = (const char *) (((xmlAttrPtr) cur)->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4713 | if (cur->ns) { |
| 4714 | if (cur->ns->prefix != NULL) |
| 4715 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4716 | (char *)cur->ns->prefix, (char *)cur->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4717 | else |
| 4718 | snprintf(nametemp, sizeof(nametemp) - 1, "%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4719 | (char *)cur->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4720 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4721 | name = nametemp; |
| 4722 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4723 | next = ((xmlAttrPtr) cur)->parent; |
| 4724 | } else { |
| 4725 | next = cur->parent; |
| 4726 | } |
| 4727 | |
| 4728 | /* |
| 4729 | * Make sure there is enough room |
| 4730 | */ |
| 4731 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4732 | buf_len = |
| 4733 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4734 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4735 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4736 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4737 | xmlFree(buf); |
| 4738 | xmlFree(buffer); |
| 4739 | return (NULL); |
| 4740 | } |
| 4741 | buffer = temp; |
| 4742 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4743 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4744 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4745 | xmlFree(buf); |
| 4746 | xmlFree(buffer); |
| 4747 | return (NULL); |
| 4748 | } |
| 4749 | buf = temp; |
| 4750 | } |
| 4751 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4752 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4753 | sep, name, (char *) buffer); |
| 4754 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4755 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4756 | sep, name, occur, (char *) buffer); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4757 | snprintf((char *) buffer, buf_len, "%s", (char *)buf); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4758 | cur = next; |
| 4759 | } while (cur != NULL); |
| 4760 | xmlFree(buf); |
| 4761 | return (buffer); |
| 4762 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4763 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4764 | |
| 4765 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4766 | * xmlDocGetRootElement: |
| 4767 | * @doc: the document |
| 4768 | * |
| 4769 | * Get the root element of the document (doc->children is a list |
| 4770 | * containing possibly comments, PIs, etc ...). |
| 4771 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4772 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4773 | */ |
| 4774 | xmlNodePtr |
| 4775 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 4776 | xmlNodePtr ret; |
| 4777 | |
| 4778 | if (doc == NULL) return(NULL); |
| 4779 | ret = doc->children; |
| 4780 | while (ret != NULL) { |
| 4781 | if (ret->type == XML_ELEMENT_NODE) |
| 4782 | return(ret); |
| 4783 | ret = ret->next; |
| 4784 | } |
| 4785 | return(ret); |
| 4786 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4787 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4788 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4789 | /** |
| 4790 | * xmlDocSetRootElement: |
| 4791 | * @doc: the document |
Daniel Veillard | 26a45c8 | 2006-10-20 12:55:34 +0000 | [diff] [blame] | 4792 | * @root: the new document root element, if root is NULL no action is taken, |
| 4793 | * to remove a node from a document use xmlUnlinkNode(root) instead. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4794 | * |
| 4795 | * Set the root element of the document (doc->children is a list |
| 4796 | * containing possibly comments, PIs, etc ...). |
| 4797 | * |
Daniel Veillard | 26a45c8 | 2006-10-20 12:55:34 +0000 | [diff] [blame] | 4798 | * 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] | 4799 | */ |
| 4800 | xmlNodePtr |
| 4801 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4802 | xmlNodePtr old = NULL; |
| 4803 | |
| 4804 | if (doc == NULL) return(NULL); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4805 | if (root == NULL) |
| 4806 | return(NULL); |
| 4807 | xmlUnlinkNode(root); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 4808 | xmlSetTreeDoc(root, doc); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4809 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4810 | old = doc->children; |
| 4811 | while (old != NULL) { |
| 4812 | if (old->type == XML_ELEMENT_NODE) |
| 4813 | break; |
| 4814 | old = old->next; |
| 4815 | } |
| 4816 | if (old == NULL) { |
| 4817 | if (doc->children == NULL) { |
| 4818 | doc->children = root; |
| 4819 | doc->last = root; |
| 4820 | } else { |
| 4821 | xmlAddSibling(doc->children, root); |
| 4822 | } |
| 4823 | } else { |
| 4824 | xmlReplaceNode(old, root); |
| 4825 | } |
| 4826 | return(old); |
| 4827 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4828 | #endif |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4829 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4830 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4831 | /** |
| 4832 | * xmlNodeSetLang: |
| 4833 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4834 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4835 | * |
| 4836 | * Set the language of a node, i.e. the values of the xml:lang |
| 4837 | * attribute. |
| 4838 | */ |
| 4839 | void |
| 4840 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4841 | xmlNsPtr ns; |
| 4842 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4843 | if (cur == NULL) return; |
| 4844 | switch(cur->type) { |
| 4845 | case XML_TEXT_NODE: |
| 4846 | case XML_CDATA_SECTION_NODE: |
| 4847 | case XML_COMMENT_NODE: |
| 4848 | case XML_DOCUMENT_NODE: |
| 4849 | case XML_DOCUMENT_TYPE_NODE: |
| 4850 | case XML_DOCUMENT_FRAG_NODE: |
| 4851 | case XML_NOTATION_NODE: |
| 4852 | case XML_HTML_DOCUMENT_NODE: |
| 4853 | case XML_DTD_NODE: |
| 4854 | case XML_ELEMENT_DECL: |
| 4855 | case XML_ATTRIBUTE_DECL: |
| 4856 | case XML_ENTITY_DECL: |
| 4857 | case XML_PI_NODE: |
| 4858 | case XML_ENTITY_REF_NODE: |
| 4859 | case XML_ENTITY_NODE: |
| 4860 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4861 | #ifdef LIBXML_DOCB_ENABLED |
| 4862 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4863 | #endif |
| 4864 | case XML_XINCLUDE_START: |
| 4865 | case XML_XINCLUDE_END: |
| 4866 | return; |
| 4867 | case XML_ELEMENT_NODE: |
| 4868 | case XML_ATTRIBUTE_NODE: |
| 4869 | break; |
| 4870 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4871 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4872 | if (ns == NULL) |
| 4873 | return; |
| 4874 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4875 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4876 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4877 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4878 | /** |
| 4879 | * xmlNodeGetLang: |
| 4880 | * @cur: the node being checked |
| 4881 | * |
| 4882 | * Searches the language of a node, i.e. the values of the xml:lang |
| 4883 | * attribute or the one carried by the nearest ancestor. |
| 4884 | * |
| 4885 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4886 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4887 | */ |
| 4888 | xmlChar * |
| 4889 | xmlNodeGetLang(xmlNodePtr cur) { |
| 4890 | xmlChar *lang; |
| 4891 | |
| 4892 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 4893 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4894 | if (lang != NULL) |
| 4895 | return(lang); |
| 4896 | cur = cur->parent; |
| 4897 | } |
| 4898 | return(NULL); |
| 4899 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4900 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4901 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4902 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4903 | /** |
| 4904 | * xmlNodeSetSpacePreserve: |
| 4905 | * @cur: the node being changed |
| 4906 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 4907 | * |
| 4908 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 4909 | * value of the xml:space attribute. |
| 4910 | */ |
| 4911 | void |
| 4912 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4913 | xmlNsPtr ns; |
| 4914 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4915 | if (cur == NULL) return; |
| 4916 | switch(cur->type) { |
| 4917 | case XML_TEXT_NODE: |
| 4918 | case XML_CDATA_SECTION_NODE: |
| 4919 | case XML_COMMENT_NODE: |
| 4920 | case XML_DOCUMENT_NODE: |
| 4921 | case XML_DOCUMENT_TYPE_NODE: |
| 4922 | case XML_DOCUMENT_FRAG_NODE: |
| 4923 | case XML_NOTATION_NODE: |
| 4924 | case XML_HTML_DOCUMENT_NODE: |
| 4925 | case XML_DTD_NODE: |
| 4926 | case XML_ELEMENT_DECL: |
| 4927 | case XML_ATTRIBUTE_DECL: |
| 4928 | case XML_ENTITY_DECL: |
| 4929 | case XML_PI_NODE: |
| 4930 | case XML_ENTITY_REF_NODE: |
| 4931 | case XML_ENTITY_NODE: |
| 4932 | case XML_NAMESPACE_DECL: |
| 4933 | case XML_XINCLUDE_START: |
| 4934 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4935 | #ifdef LIBXML_DOCB_ENABLED |
| 4936 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4937 | #endif |
| 4938 | return; |
| 4939 | case XML_ELEMENT_NODE: |
| 4940 | case XML_ATTRIBUTE_NODE: |
| 4941 | break; |
| 4942 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4943 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4944 | if (ns == NULL) |
| 4945 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4946 | switch (val) { |
| 4947 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4948 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4949 | break; |
| 4950 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4951 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4952 | break; |
| 4953 | } |
| 4954 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4955 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4956 | |
| 4957 | /** |
| 4958 | * xmlNodeGetSpacePreserve: |
| 4959 | * @cur: the node being checked |
| 4960 | * |
| 4961 | * Searches the space preserving behaviour of a node, i.e. the values |
| 4962 | * of the xml:space attribute or the one carried by the nearest |
| 4963 | * ancestor. |
| 4964 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4965 | * 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] | 4966 | */ |
| 4967 | int |
| 4968 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 4969 | xmlChar *space; |
| 4970 | |
| 4971 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4972 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4973 | if (space != NULL) { |
| 4974 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 4975 | xmlFree(space); |
| 4976 | return(1); |
| 4977 | } |
| 4978 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 4979 | xmlFree(space); |
| 4980 | return(0); |
| 4981 | } |
| 4982 | xmlFree(space); |
| 4983 | } |
| 4984 | cur = cur->parent; |
| 4985 | } |
| 4986 | return(-1); |
| 4987 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4988 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4989 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4990 | /** |
| 4991 | * xmlNodeSetName: |
| 4992 | * @cur: the node being changed |
| 4993 | * @name: the new tag name |
| 4994 | * |
| 4995 | * Set (or reset) the name of a node. |
| 4996 | */ |
| 4997 | void |
| 4998 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 4999 | xmlDocPtr doc; |
| 5000 | xmlDictPtr dict; |
| 5001 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5002 | if (cur == NULL) return; |
| 5003 | if (name == NULL) return; |
| 5004 | switch(cur->type) { |
| 5005 | case XML_TEXT_NODE: |
| 5006 | case XML_CDATA_SECTION_NODE: |
| 5007 | case XML_COMMENT_NODE: |
| 5008 | case XML_DOCUMENT_TYPE_NODE: |
| 5009 | case XML_DOCUMENT_FRAG_NODE: |
| 5010 | case XML_NOTATION_NODE: |
| 5011 | case XML_HTML_DOCUMENT_NODE: |
| 5012 | case XML_NAMESPACE_DECL: |
| 5013 | case XML_XINCLUDE_START: |
| 5014 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5015 | #ifdef LIBXML_DOCB_ENABLED |
| 5016 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5017 | #endif |
| 5018 | return; |
| 5019 | case XML_ELEMENT_NODE: |
| 5020 | case XML_ATTRIBUTE_NODE: |
| 5021 | case XML_PI_NODE: |
| 5022 | case XML_ENTITY_REF_NODE: |
| 5023 | case XML_ENTITY_NODE: |
| 5024 | case XML_DTD_NODE: |
| 5025 | case XML_DOCUMENT_NODE: |
| 5026 | case XML_ELEMENT_DECL: |
| 5027 | case XML_ATTRIBUTE_DECL: |
| 5028 | case XML_ENTITY_DECL: |
| 5029 | break; |
| 5030 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5031 | doc = cur->doc; |
| 5032 | if (doc != NULL) |
| 5033 | dict = doc->dict; |
| 5034 | else |
| 5035 | dict = NULL; |
| 5036 | if (dict != NULL) { |
| 5037 | if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name))) |
| 5038 | xmlFree((xmlChar *) cur->name); |
| 5039 | cur->name = xmlDictLookup(dict, name, -1); |
| 5040 | } else { |
| 5041 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 5042 | cur->name = xmlStrdup(name); |
| 5043 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5044 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 5045 | #endif |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5046 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 5047 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5048 | /** |
| 5049 | * xmlNodeSetBase: |
| 5050 | * @cur: the node being changed |
| 5051 | * @uri: the new base URI |
| 5052 | * |
| 5053 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 5054 | * xml:base attribute. |
| 5055 | */ |
| 5056 | void |
Daniel Veillard | f85ce8e | 2003-09-22 10:24:45 +0000 | [diff] [blame] | 5057 | xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5058 | xmlNsPtr ns; |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5059 | const xmlChar* fixed; |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5060 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5061 | if (cur == NULL) return; |
| 5062 | switch(cur->type) { |
| 5063 | case XML_TEXT_NODE: |
| 5064 | case XML_CDATA_SECTION_NODE: |
| 5065 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5066 | case XML_DOCUMENT_TYPE_NODE: |
| 5067 | case XML_DOCUMENT_FRAG_NODE: |
| 5068 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5069 | case XML_DTD_NODE: |
| 5070 | case XML_ELEMENT_DECL: |
| 5071 | case XML_ATTRIBUTE_DECL: |
| 5072 | case XML_ENTITY_DECL: |
| 5073 | case XML_PI_NODE: |
| 5074 | case XML_ENTITY_REF_NODE: |
| 5075 | case XML_ENTITY_NODE: |
| 5076 | case XML_NAMESPACE_DECL: |
| 5077 | case XML_XINCLUDE_START: |
| 5078 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5079 | return; |
| 5080 | case XML_ELEMENT_NODE: |
| 5081 | case XML_ATTRIBUTE_NODE: |
| 5082 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 5083 | case XML_DOCUMENT_NODE: |
| 5084 | #ifdef LIBXML_DOCB_ENABLED |
| 5085 | case XML_DOCB_DOCUMENT_NODE: |
| 5086 | #endif |
| 5087 | case XML_HTML_DOCUMENT_NODE: { |
| 5088 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 5089 | |
| 5090 | if (doc->URL != NULL) |
| 5091 | xmlFree((xmlChar *) doc->URL); |
| 5092 | if (uri == NULL) |
| 5093 | doc->URL = NULL; |
| 5094 | else |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5095 | doc->URL = xmlPathToURI(uri); |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 5096 | return; |
| 5097 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5098 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5099 | |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5100 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 5101 | if (ns == NULL) |
| 5102 | return; |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5103 | fixed = xmlPathToURI(uri); |
| 5104 | if (fixed != NULL) { |
| 5105 | xmlSetNsProp(cur, ns, BAD_CAST "base", fixed); |
William M. Brack | 38d452a | 2007-05-22 16:00:06 +0000 | [diff] [blame] | 5106 | xmlFree((xmlChar *)fixed); |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5107 | } else { |
| 5108 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
| 5109 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5110 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5111 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5112 | |
| 5113 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5114 | * xmlNodeGetBase: |
| 5115 | * @doc: the document the node pertains to |
| 5116 | * @cur: the node being checked |
| 5117 | * |
| 5118 | * Searches for the BASE URL. The code should work on both XML |
| 5119 | * and HTML document even if base mechanisms are completely different. |
| 5120 | * It returns the base as defined in RFC 2396 sections |
| 5121 | * 5.1.1. Base URI within Document Content |
| 5122 | * and |
| 5123 | * 5.1.2. Base URI from the Encapsulating Entity |
| 5124 | * However it does not return the document base (5.1.3), use |
| 5125 | * xmlDocumentGetBase() for this |
| 5126 | * |
| 5127 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5128 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5129 | */ |
| 5130 | xmlChar * |
| 5131 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5132 | xmlChar *oldbase = NULL; |
| 5133 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5134 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5135 | if ((cur == NULL) && (doc == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5136 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5137 | if (doc == NULL) doc = cur->doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5138 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 5139 | cur = doc->children; |
| 5140 | while ((cur != NULL) && (cur->name != NULL)) { |
| 5141 | if (cur->type != XML_ELEMENT_NODE) { |
| 5142 | cur = cur->next; |
| 5143 | continue; |
| 5144 | } |
| 5145 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 5146 | cur = cur->children; |
| 5147 | continue; |
| 5148 | } |
| 5149 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 5150 | cur = cur->children; |
| 5151 | continue; |
| 5152 | } |
| 5153 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 5154 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 5155 | } |
| 5156 | cur = cur->next; |
| 5157 | } |
| 5158 | return(NULL); |
| 5159 | } |
| 5160 | while (cur != NULL) { |
| 5161 | if (cur->type == XML_ENTITY_DECL) { |
| 5162 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 5163 | return(xmlStrdup(ent->URI)); |
| 5164 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5165 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5166 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5167 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5168 | if (oldbase != NULL) { |
| 5169 | newbase = xmlBuildURI(oldbase, base); |
| 5170 | if (newbase != NULL) { |
| 5171 | xmlFree(oldbase); |
| 5172 | xmlFree(base); |
| 5173 | oldbase = newbase; |
| 5174 | } else { |
| 5175 | xmlFree(oldbase); |
| 5176 | xmlFree(base); |
| 5177 | return(NULL); |
| 5178 | } |
| 5179 | } else { |
| 5180 | oldbase = base; |
| 5181 | } |
| 5182 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 5183 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 5184 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 5185 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5186 | } |
| 5187 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5188 | cur = cur->parent; |
| 5189 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5190 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 5191 | if (oldbase == NULL) |
| 5192 | return(xmlStrdup(doc->URL)); |
| 5193 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 5194 | xmlFree(oldbase); |
| 5195 | return(newbase); |
| 5196 | } |
| 5197 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5198 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5199 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5200 | /** |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5201 | * xmlNodeBufGetContent: |
| 5202 | * @buffer: a buffer |
| 5203 | * @cur: the node being read |
| 5204 | * |
| 5205 | * Read the value of a node @cur, this can be either the text carried |
| 5206 | * directly by this node if it's a TEXT node or the aggregate string |
| 5207 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 5208 | * Entity references are substituted. |
| 5209 | * Fills up the buffer @buffer with this value |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5210 | * |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5211 | * Returns 0 in case of success and -1 in case of error. |
| 5212 | */ |
| 5213 | int |
| 5214 | xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur) |
| 5215 | { |
| 5216 | if ((cur == NULL) || (buffer == NULL)) return(-1); |
| 5217 | switch (cur->type) { |
| 5218 | case XML_CDATA_SECTION_NODE: |
| 5219 | case XML_TEXT_NODE: |
| 5220 | xmlBufferCat(buffer, cur->content); |
| 5221 | break; |
| 5222 | case XML_DOCUMENT_FRAG_NODE: |
| 5223 | case XML_ELEMENT_NODE:{ |
| 5224 | xmlNodePtr tmp = cur; |
| 5225 | |
| 5226 | while (tmp != NULL) { |
| 5227 | switch (tmp->type) { |
| 5228 | case XML_CDATA_SECTION_NODE: |
| 5229 | case XML_TEXT_NODE: |
| 5230 | if (tmp->content != NULL) |
| 5231 | xmlBufferCat(buffer, tmp->content); |
| 5232 | break; |
| 5233 | case XML_ENTITY_REF_NODE: |
Rob Richards | 77b92ff | 2005-12-20 15:55:14 +0000 | [diff] [blame] | 5234 | xmlNodeBufGetContent(buffer, tmp); |
| 5235 | break; |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5236 | default: |
| 5237 | break; |
| 5238 | } |
| 5239 | /* |
| 5240 | * Skip to next node |
| 5241 | */ |
| 5242 | if (tmp->children != NULL) { |
| 5243 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 5244 | tmp = tmp->children; |
| 5245 | continue; |
| 5246 | } |
| 5247 | } |
| 5248 | if (tmp == cur) |
| 5249 | break; |
| 5250 | |
| 5251 | if (tmp->next != NULL) { |
| 5252 | tmp = tmp->next; |
| 5253 | continue; |
| 5254 | } |
| 5255 | |
| 5256 | do { |
| 5257 | tmp = tmp->parent; |
| 5258 | if (tmp == NULL) |
| 5259 | break; |
| 5260 | if (tmp == cur) { |
| 5261 | tmp = NULL; |
| 5262 | break; |
| 5263 | } |
| 5264 | if (tmp->next != NULL) { |
| 5265 | tmp = tmp->next; |
| 5266 | break; |
| 5267 | } |
| 5268 | } while (tmp != NULL); |
| 5269 | } |
| 5270 | break; |
| 5271 | } |
| 5272 | case XML_ATTRIBUTE_NODE:{ |
| 5273 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 5274 | xmlNodePtr tmp = attr->children; |
| 5275 | |
| 5276 | while (tmp != NULL) { |
| 5277 | if (tmp->type == XML_TEXT_NODE) |
| 5278 | xmlBufferCat(buffer, tmp->content); |
| 5279 | else |
| 5280 | xmlNodeBufGetContent(buffer, tmp); |
| 5281 | tmp = tmp->next; |
| 5282 | } |
| 5283 | break; |
| 5284 | } |
| 5285 | case XML_COMMENT_NODE: |
| 5286 | case XML_PI_NODE: |
| 5287 | xmlBufferCat(buffer, cur->content); |
| 5288 | break; |
| 5289 | case XML_ENTITY_REF_NODE:{ |
| 5290 | xmlEntityPtr ent; |
| 5291 | xmlNodePtr tmp; |
| 5292 | |
| 5293 | /* lookup entity declaration */ |
| 5294 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5295 | if (ent == NULL) |
| 5296 | return(-1); |
| 5297 | |
| 5298 | /* an entity content can be any "well balanced chunk", |
| 5299 | * i.e. the result of the content [43] production: |
| 5300 | * http://www.w3.org/TR/REC-xml#NT-content |
| 5301 | * -> we iterate through child nodes and recursive call |
| 5302 | * xmlNodeGetContent() which handles all possible node types */ |
| 5303 | tmp = ent->children; |
| 5304 | while (tmp) { |
| 5305 | xmlNodeBufGetContent(buffer, tmp); |
| 5306 | tmp = tmp->next; |
| 5307 | } |
| 5308 | break; |
| 5309 | } |
| 5310 | case XML_ENTITY_NODE: |
| 5311 | case XML_DOCUMENT_TYPE_NODE: |
| 5312 | case XML_NOTATION_NODE: |
| 5313 | case XML_DTD_NODE: |
| 5314 | case XML_XINCLUDE_START: |
| 5315 | case XML_XINCLUDE_END: |
| 5316 | break; |
| 5317 | case XML_DOCUMENT_NODE: |
| 5318 | #ifdef LIBXML_DOCB_ENABLED |
| 5319 | case XML_DOCB_DOCUMENT_NODE: |
| 5320 | #endif |
| 5321 | case XML_HTML_DOCUMENT_NODE: |
| 5322 | cur = cur->children; |
| 5323 | while (cur!= NULL) { |
| 5324 | if ((cur->type == XML_ELEMENT_NODE) || |
| 5325 | (cur->type == XML_TEXT_NODE) || |
| 5326 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 5327 | xmlNodeBufGetContent(buffer, cur); |
| 5328 | } |
| 5329 | cur = cur->next; |
| 5330 | } |
| 5331 | break; |
| 5332 | case XML_NAMESPACE_DECL: |
| 5333 | xmlBufferCat(buffer, ((xmlNsPtr) cur)->href); |
| 5334 | break; |
| 5335 | case XML_ELEMENT_DECL: |
| 5336 | case XML_ATTRIBUTE_DECL: |
| 5337 | case XML_ENTITY_DECL: |
| 5338 | break; |
| 5339 | } |
| 5340 | return(0); |
| 5341 | } |
| 5342 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5343 | * xmlNodeGetContent: |
| 5344 | * @cur: the node being read |
| 5345 | * |
| 5346 | * Read the value of a node, this can be either the text carried |
| 5347 | * directly by this node if it's a TEXT node or the aggregate string |
| 5348 | * 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] | 5349 | * Entity references are substituted. |
| 5350 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5351 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5352 | */ |
| 5353 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5354 | xmlNodeGetContent(xmlNodePtr cur) |
| 5355 | { |
| 5356 | if (cur == NULL) |
| 5357 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5358 | switch (cur->type) { |
| 5359 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5360 | case XML_ELEMENT_NODE:{ |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5361 | xmlBufferPtr buffer; |
| 5362 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5363 | |
Daniel Veillard | 814a76d | 2003-01-23 18:24:20 +0000 | [diff] [blame] | 5364 | buffer = xmlBufferCreateSize(64); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5365 | if (buffer == NULL) |
| 5366 | return (NULL); |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5367 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5368 | ret = buffer->content; |
| 5369 | buffer->content = NULL; |
| 5370 | xmlBufferFree(buffer); |
| 5371 | return (ret); |
| 5372 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5373 | case XML_ATTRIBUTE_NODE: |
| 5374 | return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5375 | case XML_COMMENT_NODE: |
| 5376 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5377 | if (cur->content != NULL) |
| 5378 | return (xmlStrdup(cur->content)); |
| 5379 | return (NULL); |
| 5380 | case XML_ENTITY_REF_NODE:{ |
| 5381 | xmlEntityPtr ent; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5382 | xmlBufferPtr buffer; |
| 5383 | xmlChar *ret; |
| 5384 | |
| 5385 | /* lookup entity declaration */ |
| 5386 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5387 | if (ent == NULL) |
| 5388 | return (NULL); |
| 5389 | |
| 5390 | buffer = xmlBufferCreate(); |
| 5391 | if (buffer == NULL) |
| 5392 | return (NULL); |
| 5393 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5394 | xmlNodeBufGetContent(buffer, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5395 | |
| 5396 | ret = buffer->content; |
| 5397 | buffer->content = NULL; |
| 5398 | xmlBufferFree(buffer); |
| 5399 | return (ret); |
| 5400 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5401 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5402 | case XML_DOCUMENT_TYPE_NODE: |
| 5403 | case XML_NOTATION_NODE: |
| 5404 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5405 | case XML_XINCLUDE_START: |
| 5406 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5407 | return (NULL); |
| 5408 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5409 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5410 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5411 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5412 | case XML_HTML_DOCUMENT_NODE: { |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5413 | xmlBufferPtr buffer; |
| 5414 | xmlChar *ret; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5415 | |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5416 | buffer = xmlBufferCreate(); |
| 5417 | if (buffer == NULL) |
| 5418 | return (NULL); |
| 5419 | |
| 5420 | xmlNodeBufGetContent(buffer, (xmlNodePtr) cur); |
| 5421 | |
| 5422 | ret = buffer->content; |
| 5423 | buffer->content = NULL; |
| 5424 | xmlBufferFree(buffer); |
| 5425 | return (ret); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5426 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 5427 | case XML_NAMESPACE_DECL: { |
| 5428 | xmlChar *tmp; |
| 5429 | |
| 5430 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 5431 | return (tmp); |
| 5432 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5433 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5434 | /* TODO !!! */ |
| 5435 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5436 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5437 | /* TODO !!! */ |
| 5438 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5439 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5440 | /* TODO !!! */ |
| 5441 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5442 | case XML_CDATA_SECTION_NODE: |
| 5443 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5444 | if (cur->content != NULL) |
| 5445 | return (xmlStrdup(cur->content)); |
| 5446 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5447 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5448 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5449 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5450 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5451 | /** |
| 5452 | * xmlNodeSetContent: |
| 5453 | * @cur: the node being modified |
| 5454 | * @content: the new value of the content |
| 5455 | * |
| 5456 | * Replace the content of a node. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5457 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 5458 | * references, but XML special chars need to be escaped first by using |
| 5459 | * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5460 | */ |
| 5461 | void |
| 5462 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 5463 | if (cur == NULL) { |
| 5464 | #ifdef DEBUG_TREE |
| 5465 | xmlGenericError(xmlGenericErrorContext, |
| 5466 | "xmlNodeSetContent : node == NULL\n"); |
| 5467 | #endif |
| 5468 | return; |
| 5469 | } |
| 5470 | switch (cur->type) { |
| 5471 | case XML_DOCUMENT_FRAG_NODE: |
| 5472 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5473 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5474 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5475 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 5476 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5477 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5478 | case XML_TEXT_NODE: |
| 5479 | case XML_CDATA_SECTION_NODE: |
| 5480 | case XML_ENTITY_REF_NODE: |
| 5481 | case XML_ENTITY_NODE: |
| 5482 | case XML_PI_NODE: |
| 5483 | case XML_COMMENT_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5484 | if ((cur->content != NULL) && |
| 5485 | (cur->content != (xmlChar *) &(cur->properties))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5486 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
Daniel Veillard | c587bce | 2005-05-10 15:28:08 +0000 | [diff] [blame] | 5487 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5488 | xmlFree(cur->content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5489 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5490 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5491 | cur->last = cur->children = NULL; |
| 5492 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5493 | cur->content = xmlStrdup(content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5494 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5495 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5496 | cur->properties = NULL; |
| 5497 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5498 | break; |
| 5499 | case XML_DOCUMENT_NODE: |
| 5500 | case XML_HTML_DOCUMENT_NODE: |
| 5501 | case XML_DOCUMENT_TYPE_NODE: |
| 5502 | case XML_XINCLUDE_START: |
| 5503 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5504 | #ifdef LIBXML_DOCB_ENABLED |
| 5505 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5506 | #endif |
| 5507 | break; |
| 5508 | case XML_NOTATION_NODE: |
| 5509 | break; |
| 5510 | case XML_DTD_NODE: |
| 5511 | break; |
| 5512 | case XML_NAMESPACE_DECL: |
| 5513 | break; |
| 5514 | case XML_ELEMENT_DECL: |
| 5515 | /* TODO !!! */ |
| 5516 | break; |
| 5517 | case XML_ATTRIBUTE_DECL: |
| 5518 | /* TODO !!! */ |
| 5519 | break; |
| 5520 | case XML_ENTITY_DECL: |
| 5521 | /* TODO !!! */ |
| 5522 | break; |
| 5523 | } |
| 5524 | } |
| 5525 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5526 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5527 | /** |
| 5528 | * xmlNodeSetContentLen: |
| 5529 | * @cur: the node being modified |
| 5530 | * @content: the new value of the content |
| 5531 | * @len: the size of @content |
| 5532 | * |
| 5533 | * Replace the content of a node. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5534 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 5535 | * references, but XML special chars need to be escaped first by using |
| 5536 | * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5537 | */ |
| 5538 | void |
| 5539 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5540 | if (cur == NULL) { |
| 5541 | #ifdef DEBUG_TREE |
| 5542 | xmlGenericError(xmlGenericErrorContext, |
| 5543 | "xmlNodeSetContentLen : node == NULL\n"); |
| 5544 | #endif |
| 5545 | return; |
| 5546 | } |
| 5547 | switch (cur->type) { |
| 5548 | case XML_DOCUMENT_FRAG_NODE: |
| 5549 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5550 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5551 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5552 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 5553 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5554 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5555 | case XML_TEXT_NODE: |
| 5556 | case XML_CDATA_SECTION_NODE: |
| 5557 | case XML_ENTITY_REF_NODE: |
| 5558 | case XML_ENTITY_NODE: |
| 5559 | case XML_PI_NODE: |
| 5560 | case XML_COMMENT_NODE: |
| 5561 | case XML_NOTATION_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5562 | if ((cur->content != NULL) && |
| 5563 | (cur->content != (xmlChar *) &(cur->properties))) { |
| 5564 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5565 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
| 5566 | xmlFree(cur->content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5567 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5568 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5569 | cur->children = cur->last = NULL; |
| 5570 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5571 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5572 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5573 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5574 | cur->properties = NULL; |
| 5575 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5576 | break; |
| 5577 | case XML_DOCUMENT_NODE: |
| 5578 | case XML_DTD_NODE: |
| 5579 | case XML_HTML_DOCUMENT_NODE: |
| 5580 | case XML_DOCUMENT_TYPE_NODE: |
| 5581 | case XML_NAMESPACE_DECL: |
| 5582 | case XML_XINCLUDE_START: |
| 5583 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5584 | #ifdef LIBXML_DOCB_ENABLED |
| 5585 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5586 | #endif |
| 5587 | break; |
| 5588 | case XML_ELEMENT_DECL: |
| 5589 | /* TODO !!! */ |
| 5590 | break; |
| 5591 | case XML_ATTRIBUTE_DECL: |
| 5592 | /* TODO !!! */ |
| 5593 | break; |
| 5594 | case XML_ENTITY_DECL: |
| 5595 | /* TODO !!! */ |
| 5596 | break; |
| 5597 | } |
| 5598 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5599 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5600 | |
| 5601 | /** |
| 5602 | * xmlNodeAddContentLen: |
| 5603 | * @cur: the node being modified |
| 5604 | * @content: extra content |
| 5605 | * @len: the size of @content |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5606 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5607 | * Append the extra substring to the node content. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5608 | * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be |
| 5609 | * raw text, so unescaped XML special chars are allowed, entity |
| 5610 | * references are not supported. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5611 | */ |
| 5612 | void |
| 5613 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5614 | if (cur == NULL) { |
| 5615 | #ifdef DEBUG_TREE |
| 5616 | xmlGenericError(xmlGenericErrorContext, |
| 5617 | "xmlNodeAddContentLen : node == NULL\n"); |
| 5618 | #endif |
| 5619 | return; |
| 5620 | } |
| 5621 | if (len <= 0) return; |
| 5622 | switch (cur->type) { |
| 5623 | case XML_DOCUMENT_FRAG_NODE: |
| 5624 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5625 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5626 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 5627 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5628 | newNode = xmlNewTextLen(content, len); |
| 5629 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5630 | tmp = xmlAddChild(cur, newNode); |
| 5631 | if (tmp != newNode) |
| 5632 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5633 | if ((last != NULL) && (last->next == newNode)) { |
| 5634 | xmlTextMerge(last, newNode); |
| 5635 | } |
| 5636 | } |
| 5637 | break; |
| 5638 | } |
| 5639 | case XML_ATTRIBUTE_NODE: |
| 5640 | break; |
| 5641 | case XML_TEXT_NODE: |
| 5642 | case XML_CDATA_SECTION_NODE: |
| 5643 | case XML_ENTITY_REF_NODE: |
| 5644 | case XML_ENTITY_NODE: |
| 5645 | case XML_PI_NODE: |
| 5646 | case XML_COMMENT_NODE: |
| 5647 | case XML_NOTATION_NODE: |
| 5648 | if (content != NULL) { |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5649 | if ((cur->content == (xmlChar *) &(cur->properties)) || |
| 5650 | ((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5651 | xmlDictOwns(cur->doc->dict, cur->content))) { |
| 5652 | cur->content = xmlStrncatNew(cur->content, content, len); |
| 5653 | cur->properties = NULL; |
| 5654 | cur->nsDef = NULL; |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5655 | break; |
| 5656 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5657 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5658 | } |
| 5659 | case XML_DOCUMENT_NODE: |
| 5660 | case XML_DTD_NODE: |
| 5661 | case XML_HTML_DOCUMENT_NODE: |
| 5662 | case XML_DOCUMENT_TYPE_NODE: |
| 5663 | case XML_NAMESPACE_DECL: |
| 5664 | case XML_XINCLUDE_START: |
| 5665 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5666 | #ifdef LIBXML_DOCB_ENABLED |
| 5667 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5668 | #endif |
| 5669 | break; |
| 5670 | case XML_ELEMENT_DECL: |
| 5671 | case XML_ATTRIBUTE_DECL: |
| 5672 | case XML_ENTITY_DECL: |
| 5673 | break; |
| 5674 | } |
| 5675 | } |
| 5676 | |
| 5677 | /** |
| 5678 | * xmlNodeAddContent: |
| 5679 | * @cur: the node being modified |
| 5680 | * @content: extra content |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5681 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5682 | * Append the extra substring to the node content. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5683 | * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be |
| 5684 | * raw text, so unescaped XML special chars are allowed, entity |
| 5685 | * references are not supported. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5686 | */ |
| 5687 | void |
| 5688 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5689 | int len; |
| 5690 | |
| 5691 | if (cur == NULL) { |
| 5692 | #ifdef DEBUG_TREE |
| 5693 | xmlGenericError(xmlGenericErrorContext, |
| 5694 | "xmlNodeAddContent : node == NULL\n"); |
| 5695 | #endif |
| 5696 | return; |
| 5697 | } |
| 5698 | if (content == NULL) return; |
| 5699 | len = xmlStrlen(content); |
| 5700 | xmlNodeAddContentLen(cur, content, len); |
| 5701 | } |
| 5702 | |
| 5703 | /** |
| 5704 | * xmlTextMerge: |
| 5705 | * @first: the first text node |
| 5706 | * @second: the second text node being merged |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5707 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5708 | * Merge two text nodes into one |
| 5709 | * Returns the first text node augmented |
| 5710 | */ |
| 5711 | xmlNodePtr |
| 5712 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5713 | if (first == NULL) return(second); |
| 5714 | if (second == NULL) return(first); |
| 5715 | if (first->type != XML_TEXT_NODE) return(first); |
| 5716 | if (second->type != XML_TEXT_NODE) return(first); |
| 5717 | if (second->name != first->name) |
| 5718 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5719 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5720 | xmlUnlinkNode(second); |
| 5721 | xmlFreeNode(second); |
| 5722 | return(first); |
| 5723 | } |
| 5724 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 5725 | #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] | 5726 | /** |
| 5727 | * xmlGetNsList: |
| 5728 | * @doc: the document |
| 5729 | * @node: the current node |
| 5730 | * |
| 5731 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5732 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5733 | * that need to be freed by the caller or NULL if no |
| 5734 | * namespace if defined |
| 5735 | */ |
| 5736 | xmlNsPtr * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5737 | xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) |
| 5738 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5739 | xmlNsPtr cur; |
| 5740 | xmlNsPtr *ret = NULL; |
| 5741 | int nbns = 0; |
| 5742 | int maxns = 10; |
| 5743 | int i; |
| 5744 | |
| 5745 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5746 | if (node->type == XML_ELEMENT_NODE) { |
| 5747 | cur = node->nsDef; |
| 5748 | while (cur != NULL) { |
| 5749 | if (ret == NULL) { |
| 5750 | ret = |
| 5751 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5752 | sizeof(xmlNsPtr)); |
| 5753 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5754 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5755 | return (NULL); |
| 5756 | } |
| 5757 | ret[nbns] = NULL; |
| 5758 | } |
| 5759 | for (i = 0; i < nbns; i++) { |
| 5760 | if ((cur->prefix == ret[i]->prefix) || |
| 5761 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5762 | break; |
| 5763 | } |
| 5764 | if (i >= nbns) { |
| 5765 | if (nbns >= maxns) { |
| 5766 | maxns *= 2; |
| 5767 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5768 | (maxns + |
| 5769 | 1) * |
| 5770 | sizeof(xmlNsPtr)); |
| 5771 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5772 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5773 | return (NULL); |
| 5774 | } |
| 5775 | } |
| 5776 | ret[nbns++] = cur; |
| 5777 | ret[nbns] = NULL; |
| 5778 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5779 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5780 | cur = cur->next; |
| 5781 | } |
| 5782 | } |
| 5783 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5784 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5785 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5786 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5787 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5788 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5789 | /* |
| 5790 | * xmlTreeEnsureXMLDecl: |
| 5791 | * @doc: the doc |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5792 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5793 | * Ensures that there is an XML namespace declaration on 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 | * Returns the XML ns-struct or NULL on API and internal errors. |
| 5796 | */ |
| 5797 | static xmlNsPtr |
| 5798 | xmlTreeEnsureXMLDecl(xmlDocPtr doc) |
| 5799 | { |
| 5800 | if (doc == NULL) |
| 5801 | return (NULL); |
| 5802 | if (doc->oldNs != NULL) |
| 5803 | return (doc->oldNs); |
| 5804 | { |
| 5805 | xmlNsPtr ns; |
| 5806 | ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5807 | if (ns == NULL) { |
| 5808 | xmlTreeErrMemory( |
| 5809 | "allocating the XML namespace"); |
| 5810 | return (NULL); |
| 5811 | } |
| 5812 | memset(ns, 0, sizeof(xmlNs)); |
| 5813 | ns->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5814 | ns->href = xmlStrdup(XML_XML_NAMESPACE); |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5815 | ns->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5816 | doc->oldNs = ns; |
| 5817 | return (ns); |
| 5818 | } |
| 5819 | } |
| 5820 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5821 | /** |
| 5822 | * xmlSearchNs: |
| 5823 | * @doc: the document |
| 5824 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5825 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5826 | * |
| 5827 | * Search a Ns registered under a given name space for a document. |
| 5828 | * recurse on the parents until it finds the defined namespace |
| 5829 | * or return NULL otherwise. |
| 5830 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5831 | * We don't allow to cross entities boundaries. If you don't declare |
| 5832 | * the namespace within those you will be in troubles !!! A warning |
| 5833 | * is generated to cover this case. |
| 5834 | * |
| 5835 | * Returns the namespace pointer or NULL. |
| 5836 | */ |
| 5837 | xmlNsPtr |
| 5838 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5839 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5840 | xmlNsPtr cur; |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5841 | xmlNodePtr orig = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5842 | |
| 5843 | if (node == NULL) return(NULL); |
| 5844 | if ((nameSpace != NULL) && |
| 5845 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5846 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5847 | /* |
| 5848 | * The XML-1.0 namespace is normally held on the root |
| 5849 | * element. In this case exceptionally create it on the |
| 5850 | * node element. |
| 5851 | */ |
| 5852 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5853 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5854 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5855 | return(NULL); |
| 5856 | } |
| 5857 | memset(cur, 0, sizeof(xmlNs)); |
| 5858 | cur->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5859 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5860 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5861 | cur->next = node->nsDef; |
| 5862 | node->nsDef = cur; |
| 5863 | return(cur); |
| 5864 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5865 | if (doc == NULL) { |
| 5866 | doc = node->doc; |
| 5867 | if (doc == NULL) |
| 5868 | return(NULL); |
| 5869 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5870 | /* |
| 5871 | * Return the XML namespace declaration held by the doc. |
| 5872 | */ |
| 5873 | if (doc->oldNs == NULL) |
| 5874 | return(xmlTreeEnsureXMLDecl(doc)); |
| 5875 | else |
| 5876 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5877 | } |
| 5878 | while (node != NULL) { |
| 5879 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5880 | (node->type == XML_ENTITY_NODE) || |
| 5881 | (node->type == XML_ENTITY_DECL)) |
| 5882 | return(NULL); |
| 5883 | if (node->type == XML_ELEMENT_NODE) { |
| 5884 | cur = node->nsDef; |
| 5885 | while (cur != NULL) { |
| 5886 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5887 | (cur->href != NULL)) |
| 5888 | return(cur); |
| 5889 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5890 | (cur->href != NULL) && |
| 5891 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5892 | return(cur); |
| 5893 | cur = cur->next; |
| 5894 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5895 | if (orig != node) { |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5896 | cur = node->ns; |
| 5897 | if (cur != NULL) { |
| 5898 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 5899 | (cur->href != NULL)) |
| 5900 | return(cur); |
| 5901 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 5902 | (cur->href != NULL) && |
| 5903 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 5904 | return(cur); |
| 5905 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5906 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5907 | } |
| 5908 | node = node->parent; |
| 5909 | } |
| 5910 | return(NULL); |
| 5911 | } |
| 5912 | |
| 5913 | /** |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5914 | * xmlNsInScope: |
| 5915 | * @doc: the document |
| 5916 | * @node: the current node |
| 5917 | * @ancestor: the ancestor carrying the namespace |
| 5918 | * @prefix: the namespace prefix |
| 5919 | * |
| 5920 | * Verify that the given namespace held on @ancestor is still in scope |
| 5921 | * on node. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5922 | * |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5923 | * Returns 1 if true, 0 if false and -1 in case of error. |
| 5924 | */ |
| 5925 | static int |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5926 | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 5927 | xmlNodePtr ancestor, const xmlChar * prefix) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5928 | { |
| 5929 | xmlNsPtr tst; |
| 5930 | |
| 5931 | while ((node != NULL) && (node != ancestor)) { |
| 5932 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 5933 | (node->type == XML_ENTITY_NODE) || |
| 5934 | (node->type == XML_ENTITY_DECL)) |
| 5935 | return (-1); |
| 5936 | if (node->type == XML_ELEMENT_NODE) { |
| 5937 | tst = node->nsDef; |
| 5938 | while (tst != NULL) { |
| 5939 | if ((tst->prefix == NULL) |
| 5940 | && (prefix == NULL)) |
| 5941 | return (0); |
| 5942 | if ((tst->prefix != NULL) |
| 5943 | && (prefix != NULL) |
| 5944 | && (xmlStrEqual(tst->prefix, prefix))) |
| 5945 | return (0); |
| 5946 | tst = tst->next; |
| 5947 | } |
| 5948 | } |
| 5949 | node = node->parent; |
| 5950 | } |
| 5951 | if (node != ancestor) |
| 5952 | return (-1); |
| 5953 | return (1); |
| 5954 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5955 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5956 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5957 | * xmlSearchNsByHref: |
| 5958 | * @doc: the document |
| 5959 | * @node: the current node |
| 5960 | * @href: the namespace value |
| 5961 | * |
| 5962 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 5963 | * the defined namespace or return NULL otherwise. |
| 5964 | * Returns the namespace pointer or NULL. |
| 5965 | */ |
| 5966 | xmlNsPtr |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5967 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) |
| 5968 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5969 | xmlNsPtr cur; |
| 5970 | xmlNodePtr orig = node; |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 5971 | int is_attr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5972 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5973 | if ((node == NULL) || (href == NULL)) |
| 5974 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5975 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5976 | /* |
| 5977 | * Only the document can hold the XML spec namespace. |
| 5978 | */ |
| 5979 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5980 | /* |
| 5981 | * The XML-1.0 namespace is normally held on the root |
| 5982 | * element. In this case exceptionally create it on the |
| 5983 | * node element. |
| 5984 | */ |
| 5985 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5986 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5987 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 5988 | return (NULL); |
| 5989 | } |
| 5990 | memset(cur, 0, sizeof(xmlNs)); |
| 5991 | cur->type = XML_LOCAL_NAMESPACE; |
| 5992 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5993 | cur->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 5994 | cur->next = node->nsDef; |
| 5995 | node->nsDef = cur; |
| 5996 | return (cur); |
| 5997 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5998 | if (doc == NULL) { |
| 5999 | doc = node->doc; |
| 6000 | if (doc == NULL) |
| 6001 | return(NULL); |
| 6002 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 6003 | /* |
| 6004 | * Return the XML namespace declaration held by the doc. |
| 6005 | */ |
| 6006 | if (doc->oldNs == NULL) |
| 6007 | return(xmlTreeEnsureXMLDecl(doc)); |
| 6008 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6009 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6010 | } |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6011 | is_attr = (node->type == XML_ATTRIBUTE_NODE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6012 | while (node != NULL) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6013 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 6014 | (node->type == XML_ENTITY_NODE) || |
| 6015 | (node->type == XML_ENTITY_DECL)) |
| 6016 | return (NULL); |
| 6017 | if (node->type == XML_ELEMENT_NODE) { |
| 6018 | cur = node->nsDef; |
| 6019 | while (cur != NULL) { |
| 6020 | if ((cur->href != NULL) && (href != NULL) && |
| 6021 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6022 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 6023 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6024 | return (cur); |
| 6025 | } |
| 6026 | cur = cur->next; |
| 6027 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 6028 | if (orig != node) { |
| 6029 | cur = node->ns; |
| 6030 | if (cur != NULL) { |
| 6031 | if ((cur->href != NULL) && (href != NULL) && |
| 6032 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6033 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 6034 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 6035 | return (cur); |
| 6036 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6037 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6038 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6039 | } |
| 6040 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6041 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6042 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6043 | } |
| 6044 | |
| 6045 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6046 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6047 | * @doc: the document |
| 6048 | * @tree: a node expected to hold the new namespace |
| 6049 | * @ns: the original namespace |
| 6050 | * |
| 6051 | * This function tries to locate a namespace definition in a tree |
| 6052 | * ancestors, or create a new namespace definition node similar to |
| 6053 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 6054 | * null (default namespace) or reused within the subtree defined by |
| 6055 | * @tree or on one of its ancestors then a new prefix is generated. |
| 6056 | * Returns the (new) namespace definition or NULL in case of error |
| 6057 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 6058 | static xmlNsPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6059 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 6060 | xmlNsPtr def; |
| 6061 | xmlChar prefix[50]; |
| 6062 | int counter = 1; |
| 6063 | |
| 6064 | if (tree == NULL) { |
| 6065 | #ifdef DEBUG_TREE |
| 6066 | xmlGenericError(xmlGenericErrorContext, |
| 6067 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 6068 | #endif |
| 6069 | return(NULL); |
| 6070 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 6071 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6072 | #ifdef DEBUG_TREE |
| 6073 | xmlGenericError(xmlGenericErrorContext, |
| 6074 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 6075 | #endif |
| 6076 | return(NULL); |
| 6077 | } |
| 6078 | /* |
| 6079 | * Search an existing namespace definition inherited. |
| 6080 | */ |
| 6081 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 6082 | if (def != NULL) |
| 6083 | return(def); |
| 6084 | |
| 6085 | /* |
| 6086 | * Find a close prefix which is not already in use. |
| 6087 | * Let's strip namespace prefixes longer than 20 chars ! |
| 6088 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6089 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6090 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6091 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 6092 | snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6093 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6094 | def = xmlSearchNs(doc, tree, prefix); |
| 6095 | while (def != NULL) { |
| 6096 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6097 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6098 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6099 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 6100 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6101 | (char *)ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6102 | def = xmlSearchNs(doc, tree, prefix); |
| 6103 | } |
| 6104 | |
| 6105 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6106 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6107 | */ |
| 6108 | def = xmlNewNs(tree, ns->href, prefix); |
| 6109 | return(def); |
| 6110 | } |
| 6111 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6112 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6113 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6114 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6115 | * @doc: the document |
| 6116 | * @tree: a node defining the subtree to reconciliate |
| 6117 | * |
| 6118 | * This function checks that all the namespaces declared within the given |
| 6119 | * tree are properly declared. This is needed for example after Copy or Cut |
| 6120 | * and then paste operations. The subtree may still hold pointers to |
| 6121 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6122 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6123 | * the new environment. If not possible the new namespaces are redeclared |
| 6124 | * on @tree at the top of the given subtree. |
| 6125 | * Returns the number of namespace declarations created or -1 in case of error. |
| 6126 | */ |
| 6127 | int |
| 6128 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 6129 | xmlNsPtr *oldNs = NULL; |
| 6130 | xmlNsPtr *newNs = NULL; |
| 6131 | int sizeCache = 0; |
| 6132 | int nbCache = 0; |
| 6133 | |
| 6134 | xmlNsPtr n; |
| 6135 | xmlNodePtr node = tree; |
| 6136 | xmlAttrPtr attr; |
| 6137 | int ret = 0, i; |
| 6138 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 6139 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); |
| 6140 | if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1); |
| 6141 | if (node->doc != doc) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6142 | while (node != NULL) { |
| 6143 | /* |
| 6144 | * Reconciliate the node namespace |
| 6145 | */ |
| 6146 | if (node->ns != NULL) { |
| 6147 | /* |
| 6148 | * initialize the cache if needed |
| 6149 | */ |
| 6150 | if (sizeCache == 0) { |
| 6151 | sizeCache = 10; |
| 6152 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6153 | sizeof(xmlNsPtr)); |
| 6154 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6155 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6156 | return(-1); |
| 6157 | } |
| 6158 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6159 | sizeof(xmlNsPtr)); |
| 6160 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6161 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6162 | xmlFree(oldNs); |
| 6163 | return(-1); |
| 6164 | } |
| 6165 | } |
| 6166 | for (i = 0;i < nbCache;i++) { |
| 6167 | if (oldNs[i] == node->ns) { |
| 6168 | node->ns = newNs[i]; |
| 6169 | break; |
| 6170 | } |
| 6171 | } |
| 6172 | if (i == nbCache) { |
| 6173 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6174 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6175 | */ |
| 6176 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 6177 | if (n != NULL) { /* :-( what if else ??? */ |
| 6178 | /* |
| 6179 | * check if we need to grow the cache buffers. |
| 6180 | */ |
| 6181 | if (sizeCache <= nbCache) { |
| 6182 | sizeCache *= 2; |
| 6183 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 6184 | sizeof(xmlNsPtr)); |
| 6185 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6186 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6187 | xmlFree(newNs); |
| 6188 | return(-1); |
| 6189 | } |
| 6190 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 6191 | sizeof(xmlNsPtr)); |
| 6192 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6193 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6194 | xmlFree(oldNs); |
| 6195 | return(-1); |
| 6196 | } |
| 6197 | } |
| 6198 | newNs[nbCache] = n; |
| 6199 | oldNs[nbCache++] = node->ns; |
| 6200 | node->ns = n; |
| 6201 | } |
| 6202 | } |
| 6203 | } |
| 6204 | /* |
| 6205 | * now check for namespace hold by attributes on the node. |
| 6206 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6207 | if (node->type == XML_ELEMENT_NODE) { |
| 6208 | attr = node->properties; |
| 6209 | while (attr != NULL) { |
| 6210 | if (attr->ns != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6211 | /* |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6212 | * initialize the cache if needed |
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 | if (sizeCache == 0) { |
| 6215 | sizeCache = 10; |
| 6216 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6217 | sizeof(xmlNsPtr)); |
| 6218 | if (oldNs == NULL) { |
| 6219 | xmlTreeErrMemory("fixing namespaces"); |
| 6220 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6221 | } |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6222 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6223 | sizeof(xmlNsPtr)); |
| 6224 | if (newNs == NULL) { |
| 6225 | xmlTreeErrMemory("fixing namespaces"); |
| 6226 | xmlFree(oldNs); |
| 6227 | return(-1); |
| 6228 | } |
| 6229 | } |
| 6230 | for (i = 0;i < nbCache;i++) { |
| 6231 | if (oldNs[i] == attr->ns) { |
| 6232 | attr->ns = newNs[i]; |
| 6233 | break; |
| 6234 | } |
| 6235 | } |
| 6236 | if (i == nbCache) { |
| 6237 | /* |
| 6238 | * OK we need to recreate a new namespace definition |
| 6239 | */ |
| 6240 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 6241 | if (n != NULL) { /* :-( what if else ??? */ |
| 6242 | /* |
| 6243 | * check if we need to grow the cache buffers. |
| 6244 | */ |
| 6245 | if (sizeCache <= nbCache) { |
| 6246 | sizeCache *= 2; |
| 6247 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, |
| 6248 | sizeCache * sizeof(xmlNsPtr)); |
| 6249 | if (oldNs == NULL) { |
| 6250 | xmlTreeErrMemory("fixing namespaces"); |
| 6251 | xmlFree(newNs); |
| 6252 | return(-1); |
| 6253 | } |
| 6254 | newNs = (xmlNsPtr *) xmlRealloc(newNs, |
| 6255 | sizeCache * sizeof(xmlNsPtr)); |
| 6256 | if (newNs == NULL) { |
| 6257 | xmlTreeErrMemory("fixing namespaces"); |
| 6258 | xmlFree(oldNs); |
| 6259 | return(-1); |
| 6260 | } |
| 6261 | } |
| 6262 | newNs[nbCache] = n; |
| 6263 | oldNs[nbCache++] = attr->ns; |
| 6264 | attr->ns = n; |
| 6265 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6266 | } |
| 6267 | } |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6268 | attr = attr->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6269 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6270 | } |
| 6271 | |
| 6272 | /* |
| 6273 | * Browse the full subtree, deep first |
| 6274 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6275 | if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6276 | /* deep first */ |
| 6277 | node = node->children; |
| 6278 | } else if ((node != tree) && (node->next != NULL)) { |
| 6279 | /* then siblings */ |
| 6280 | node = node->next; |
| 6281 | } else if (node != tree) { |
| 6282 | /* go up to parents->next if needed */ |
| 6283 | while (node != tree) { |
| 6284 | if (node->parent != NULL) |
| 6285 | node = node->parent; |
| 6286 | if ((node != tree) && (node->next != NULL)) { |
| 6287 | node = node->next; |
| 6288 | break; |
| 6289 | } |
| 6290 | if (node->parent == NULL) { |
| 6291 | node = NULL; |
| 6292 | break; |
| 6293 | } |
| 6294 | } |
| 6295 | /* exit condition */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6296 | if (node == tree) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6297 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 6298 | } else |
| 6299 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6300 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6301 | if (oldNs != NULL) |
| 6302 | xmlFree(oldNs); |
| 6303 | if (newNs != NULL) |
| 6304 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6305 | return(ret); |
| 6306 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6307 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6308 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6309 | static xmlAttrPtr |
| 6310 | xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name, |
| 6311 | const xmlChar *nsName, int useDTD) |
| 6312 | { |
| 6313 | xmlAttrPtr prop; |
| 6314 | |
| 6315 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6316 | return(NULL); |
| 6317 | |
| 6318 | if (node->properties != NULL) { |
| 6319 | prop = node->properties; |
| 6320 | if (nsName == NULL) { |
| 6321 | /* |
| 6322 | * We want the attr to be in no namespace. |
| 6323 | */ |
| 6324 | do { |
| 6325 | if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) { |
| 6326 | return(prop); |
| 6327 | } |
| 6328 | prop = prop->next; |
| 6329 | } while (prop != NULL); |
| 6330 | } else { |
| 6331 | /* |
| 6332 | * We want the attr to be in the specified namespace. |
| 6333 | */ |
| 6334 | do { |
| 6335 | if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) && |
| 6336 | ((prop->ns->href == nsName) || |
| 6337 | xmlStrEqual(prop->ns->href, nsName))) |
| 6338 | { |
| 6339 | return(prop); |
| 6340 | } |
| 6341 | prop = prop->next; |
| 6342 | } while (prop != NULL); |
| 6343 | } |
| 6344 | } |
| 6345 | |
| 6346 | #ifdef LIBXML_TREE_ENABLED |
| 6347 | if (! useDTD) |
| 6348 | return(NULL); |
| 6349 | /* |
| 6350 | * Check if there is a default/fixed attribute declaration in |
| 6351 | * the internal or external subset. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6352 | */ |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6353 | if ((node->doc != NULL) && (node->doc->intSubset != NULL)) { |
| 6354 | xmlDocPtr doc = node->doc; |
| 6355 | xmlAttributePtr attrDecl = NULL; |
| 6356 | xmlChar *elemQName, *tmpstr = NULL; |
| 6357 | |
| 6358 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6359 | * We need the QName of the element for the DTD-lookup. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6360 | */ |
| 6361 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 6362 | tmpstr = xmlStrdup(node->ns->prefix); |
| 6363 | tmpstr = xmlStrcat(tmpstr, BAD_CAST ":"); |
| 6364 | tmpstr = xmlStrcat(tmpstr, node->name); |
| 6365 | if (tmpstr == NULL) |
| 6366 | return(NULL); |
| 6367 | elemQName = tmpstr; |
| 6368 | } else |
| 6369 | elemQName = (xmlChar *) node->name; |
| 6370 | if (nsName == NULL) { |
| 6371 | /* |
| 6372 | * The common and nice case: Attr in no namespace. |
| 6373 | */ |
| 6374 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, |
| 6375 | elemQName, name, NULL); |
| 6376 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 6377 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, |
| 6378 | elemQName, name, NULL); |
| 6379 | } |
| 6380 | } else { |
| 6381 | xmlNsPtr *nsList, *cur; |
| 6382 | |
| 6383 | /* |
| 6384 | * The ugly case: Search using the prefixes of in-scope |
| 6385 | * ns-decls corresponding to @nsName. |
| 6386 | */ |
| 6387 | nsList = xmlGetNsList(node->doc, node); |
| 6388 | if (nsList == NULL) { |
| 6389 | if (tmpstr != NULL) |
| 6390 | xmlFree(tmpstr); |
| 6391 | return(NULL); |
| 6392 | } |
| 6393 | cur = nsList; |
| 6394 | while (*cur != NULL) { |
| 6395 | if (xmlStrEqual((*cur)->href, nsName)) { |
| 6396 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName, |
| 6397 | name, (*cur)->prefix); |
| 6398 | if (attrDecl) |
| 6399 | break; |
| 6400 | if (doc->extSubset != NULL) { |
| 6401 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName, |
| 6402 | name, (*cur)->prefix); |
| 6403 | if (attrDecl) |
| 6404 | break; |
| 6405 | } |
| 6406 | } |
| 6407 | cur++; |
| 6408 | } |
| 6409 | xmlFree(nsList); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6410 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6411 | if (tmpstr != NULL) |
| 6412 | xmlFree(tmpstr); |
| 6413 | /* |
| 6414 | * Only default/fixed attrs are relevant. |
| 6415 | */ |
| 6416 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6417 | return((xmlAttrPtr) attrDecl); |
| 6418 | } |
| 6419 | #endif /* LIBXML_TREE_ENABLED */ |
| 6420 | return(NULL); |
| 6421 | } |
| 6422 | |
| 6423 | static xmlChar* |
| 6424 | xmlGetPropNodeValueInternal(xmlAttrPtr prop) |
| 6425 | { |
| 6426 | if (prop == NULL) |
| 6427 | return(NULL); |
| 6428 | if (prop->type == XML_ATTRIBUTE_NODE) { |
| 6429 | /* |
| 6430 | * Note that we return at least the empty string. |
| 6431 | * TODO: Do we really always want that? |
| 6432 | */ |
| 6433 | if (prop->children != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 6434 | if ((prop->children->next == NULL) && |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6435 | ((prop->children->type == XML_TEXT_NODE) || |
| 6436 | (prop->children->type == XML_CDATA_SECTION_NODE))) |
| 6437 | { |
| 6438 | /* |
| 6439 | * Optimization for the common case: only 1 text node. |
| 6440 | */ |
| 6441 | return(xmlStrdup(prop->children->content)); |
| 6442 | } else { |
| 6443 | xmlChar *ret; |
| 6444 | |
| 6445 | ret = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 6446 | if (ret != NULL) |
| 6447 | return(ret); |
| 6448 | } |
| 6449 | } |
| 6450 | return(xmlStrdup((xmlChar *)"")); |
| 6451 | } else if (prop->type == XML_ATTRIBUTE_DECL) { |
| 6452 | return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue)); |
| 6453 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6454 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6455 | } |
| 6456 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6457 | /** |
| 6458 | * xmlHasProp: |
| 6459 | * @node: the node |
| 6460 | * @name: the attribute name |
| 6461 | * |
| 6462 | * Search an attribute associated to a node |
| 6463 | * This function also looks in DTD attribute declaration for #FIXED or |
| 6464 | * default declaration values unless DTD use has been turned off. |
| 6465 | * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6466 | * Returns the attribute or the attribute declaration or NULL if |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6467 | * neither was found. |
| 6468 | */ |
| 6469 | xmlAttrPtr |
| 6470 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 6471 | xmlAttrPtr prop; |
| 6472 | xmlDocPtr doc; |
| 6473 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6474 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6475 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6476 | /* |
| 6477 | * Check on the properties attached to the node |
| 6478 | */ |
| 6479 | prop = node->properties; |
| 6480 | while (prop != NULL) { |
| 6481 | if (xmlStrEqual(prop->name, name)) { |
| 6482 | return(prop); |
| 6483 | } |
| 6484 | prop = prop->next; |
| 6485 | } |
| 6486 | if (!xmlCheckDTD) return(NULL); |
| 6487 | |
| 6488 | /* |
| 6489 | * Check if there is a default declaration in the internal |
| 6490 | * or external subsets |
| 6491 | */ |
| 6492 | doc = node->doc; |
| 6493 | if (doc != NULL) { |
| 6494 | xmlAttributePtr attrDecl; |
| 6495 | if (doc->intSubset != NULL) { |
| 6496 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6497 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6498 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6499 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6500 | /* return attribute declaration only if a default value is given |
| 6501 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6502 | return((xmlAttrPtr) attrDecl); |
| 6503 | } |
| 6504 | } |
| 6505 | return(NULL); |
| 6506 | } |
| 6507 | |
| 6508 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6509 | * xmlHasNsProp: |
| 6510 | * @node: the node |
| 6511 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6512 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6513 | * |
| 6514 | * Search for an attribute associated to a node |
| 6515 | * This attribute has to be anchored in the namespace specified. |
| 6516 | * This does the entity substitution. |
| 6517 | * This function looks in DTD attribute declaration for #FIXED or |
| 6518 | * default declaration values unless DTD use has been turned off. |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6519 | * Note that a namespace of NULL indicates to use the default namespace. |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6520 | * |
| 6521 | * Returns the attribute or the attribute declaration or NULL |
| 6522 | * if neither was found. |
| 6523 | */ |
| 6524 | xmlAttrPtr |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6525 | xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6526 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6527 | return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD)); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6528 | } |
| 6529 | |
| 6530 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6531 | * xmlGetProp: |
| 6532 | * @node: the node |
| 6533 | * @name: the attribute name |
| 6534 | * |
| 6535 | * Search and get the value of an attribute associated to a node |
| 6536 | * This does the entity substitution. |
| 6537 | * This function looks in DTD attribute declaration for #FIXED or |
| 6538 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 6539 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6540 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 6541 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6542 | * |
| 6543 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6544 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6545 | */ |
| 6546 | xmlChar * |
| 6547 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6548 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6549 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6550 | prop = xmlHasProp(node, name); |
| 6551 | if (prop == NULL) |
| 6552 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6553 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6554 | } |
| 6555 | |
| 6556 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6557 | * xmlGetNoNsProp: |
| 6558 | * @node: the node |
| 6559 | * @name: the attribute name |
| 6560 | * |
| 6561 | * Search and get the value of an attribute associated to a node |
| 6562 | * This does the entity substitution. |
| 6563 | * This function looks in DTD attribute declaration for #FIXED or |
| 6564 | * default declaration values unless DTD use has been turned off. |
| 6565 | * This function is similar to xmlGetProp except it will accept only |
| 6566 | * an attribute in no namespace. |
| 6567 | * |
| 6568 | * Returns the attribute value or NULL if not found. |
| 6569 | * It's up to the caller to free the memory with xmlFree(). |
| 6570 | */ |
| 6571 | xmlChar * |
| 6572 | xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) { |
| 6573 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6574 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6575 | prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD); |
| 6576 | if (prop == NULL) |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6577 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6578 | return(xmlGetPropNodeValueInternal(prop)); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6579 | } |
| 6580 | |
| 6581 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6582 | * xmlGetNsProp: |
| 6583 | * @node: the node |
| 6584 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6585 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6586 | * |
| 6587 | * Search and get the value of an attribute associated to a node |
| 6588 | * This attribute has to be anchored in the namespace specified. |
| 6589 | * This does the entity substitution. |
| 6590 | * This function looks in DTD attribute declaration for #FIXED or |
| 6591 | * default declaration values unless DTD use has been turned off. |
| 6592 | * |
| 6593 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6594 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6595 | */ |
| 6596 | xmlChar * |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6597 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6598 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6599 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6600 | prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD); |
| 6601 | if (prop == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6602 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6603 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6604 | } |
| 6605 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6606 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
| 6607 | /** |
| 6608 | * xmlUnsetProp: |
| 6609 | * @node: the node |
| 6610 | * @name: the attribute name |
| 6611 | * |
| 6612 | * Remove an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6613 | * This handles only attributes in no namespace. |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6614 | * Returns 0 if successful, -1 if not found |
| 6615 | */ |
| 6616 | int |
| 6617 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6618 | xmlAttrPtr prop; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6619 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6620 | prop = xmlGetPropNodeInternal(node, name, NULL, 0); |
| 6621 | if (prop == NULL) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6622 | return(-1); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6623 | xmlUnlinkNode((xmlNodePtr) prop); |
| 6624 | xmlFreeProp(prop); |
| 6625 | return(0); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6626 | } |
| 6627 | |
| 6628 | /** |
| 6629 | * xmlUnsetNsProp: |
| 6630 | * @node: the node |
| 6631 | * @ns: the namespace definition |
| 6632 | * @name: the attribute name |
| 6633 | * |
| 6634 | * Remove an attribute carried by a node. |
| 6635 | * Returns 0 if successful, -1 if not found |
| 6636 | */ |
| 6637 | int |
| 6638 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6639 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6640 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6641 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6642 | if (prop == NULL) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6643 | return(-1); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6644 | xmlUnlinkNode((xmlNodePtr) prop); |
| 6645 | xmlFreeProp(prop); |
| 6646 | return(0); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6647 | } |
| 6648 | #endif |
| 6649 | |
| 6650 | #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] | 6651 | /** |
| 6652 | * xmlSetProp: |
| 6653 | * @node: the node |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6654 | * @name: the attribute name (a QName) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6655 | * @value: the attribute value |
| 6656 | * |
| 6657 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6658 | * If @name has a prefix, then the corresponding |
| 6659 | * namespace-binding will be used, if in scope; it is an |
| 6660 | * error it there's no such ns-binding for the prefix in |
| 6661 | * scope. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6662 | * Returns the attribute pointer. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6663 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6664 | */ |
| 6665 | xmlAttrPtr |
| 6666 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6667 | int len; |
| 6668 | const xmlChar *nqname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6669 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6670 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6671 | return(NULL); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6672 | |
| 6673 | /* |
| 6674 | * handle QNames |
| 6675 | */ |
| 6676 | nqname = xmlSplitQName3(name, &len); |
| 6677 | if (nqname != NULL) { |
| 6678 | xmlNsPtr ns; |
| 6679 | xmlChar *prefix = xmlStrndup(name, len); |
| 6680 | ns = xmlSearchNs(node->doc, node, prefix); |
| 6681 | if (prefix != NULL) |
| 6682 | xmlFree(prefix); |
| 6683 | if (ns != NULL) |
| 6684 | return(xmlSetNsProp(node, ns, nqname, value)); |
| 6685 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6686 | return(xmlSetNsProp(node, NULL, name, value)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6687 | } |
| 6688 | |
| 6689 | /** |
| 6690 | * xmlSetNsProp: |
| 6691 | * @node: the node |
| 6692 | * @ns: the namespace definition |
| 6693 | * @name: the attribute name |
| 6694 | * @value: the attribute value |
| 6695 | * |
| 6696 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6697 | * The ns structure must be in scope, this is not checked |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6698 | * |
| 6699 | * Returns the attribute pointer. |
| 6700 | */ |
| 6701 | xmlAttrPtr |
| 6702 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6703 | const xmlChar *value) |
| 6704 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6705 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6706 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6707 | if (ns && (ns->href == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6708 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6709 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6710 | if (prop != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6711 | /* |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6712 | * Modify the attribute's value. |
| 6713 | */ |
| 6714 | if (prop->atype == XML_ATTRIBUTE_ID) { |
| 6715 | xmlRemoveID(node->doc, prop); |
| 6716 | prop->atype = XML_ATTRIBUTE_ID; |
| 6717 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6718 | if (prop->children != NULL) |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6719 | xmlFreeNodeList(prop->children); |
| 6720 | prop->children = NULL; |
| 6721 | prop->last = NULL; |
| 6722 | prop->ns = ns; |
| 6723 | if (value != NULL) { |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6724 | xmlNodePtr tmp; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6725 | |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame] | 6726 | if(!xmlCheckUTF8(value)) { |
| 6727 | xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc, |
| 6728 | NULL); |
| 6729 | if (node->doc != NULL) |
| 6730 | node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 6731 | } |
| 6732 | prop->children = xmlNewDocText(node->doc, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6733 | prop->last = NULL; |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6734 | tmp = prop->children; |
| 6735 | while (tmp != NULL) { |
| 6736 | tmp->parent = (xmlNodePtr) prop; |
| 6737 | if (tmp->next == NULL) |
| 6738 | prop->last = tmp; |
| 6739 | tmp = tmp->next; |
| 6740 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6741 | } |
| 6742 | if (prop->atype == XML_ATTRIBUTE_ID) |
| 6743 | xmlAddID(NULL, node->doc, value, prop); |
| 6744 | return(prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6745 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6746 | /* |
| 6747 | * No equal attr found; create a new one. |
| 6748 | */ |
| 6749 | return(xmlNewPropInternal(node, ns, name, value, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6750 | } |
| 6751 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6752 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6753 | |
| 6754 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6755 | * xmlNodeIsText: |
| 6756 | * @node: the node |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6757 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6758 | * Is this node a Text node ? |
| 6759 | * Returns 1 yes, 0 no |
| 6760 | */ |
| 6761 | int |
| 6762 | xmlNodeIsText(xmlNodePtr node) { |
| 6763 | if (node == NULL) return(0); |
| 6764 | |
| 6765 | if (node->type == XML_TEXT_NODE) return(1); |
| 6766 | return(0); |
| 6767 | } |
| 6768 | |
| 6769 | /** |
| 6770 | * xmlIsBlankNode: |
| 6771 | * @node: the node |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6772 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6773 | * Checks whether this node is an empty or whitespace only |
| 6774 | * (and possibly ignorable) text-node. |
| 6775 | * |
| 6776 | * Returns 1 yes, 0 no |
| 6777 | */ |
| 6778 | int |
| 6779 | xmlIsBlankNode(xmlNodePtr node) { |
| 6780 | const xmlChar *cur; |
| 6781 | if (node == NULL) return(0); |
| 6782 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6783 | if ((node->type != XML_TEXT_NODE) && |
| 6784 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6785 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6786 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6787 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6788 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6789 | if (!IS_BLANK_CH(*cur)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6790 | cur++; |
| 6791 | } |
| 6792 | |
| 6793 | return(1); |
| 6794 | } |
| 6795 | |
| 6796 | /** |
| 6797 | * xmlTextConcat: |
| 6798 | * @node: the node |
| 6799 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6800 | * @len: @content length |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6801 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6802 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6803 | * |
| 6804 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6805 | */ |
| 6806 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6807 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6808 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6809 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6810 | |
| 6811 | if ((node->type != XML_TEXT_NODE) && |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 6812 | (node->type != XML_CDATA_SECTION_NODE) && |
| 6813 | (node->type != XML_COMMENT_NODE) && |
| 6814 | (node->type != XML_PI_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6815 | #ifdef DEBUG_TREE |
| 6816 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6817 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6818 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6819 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6820 | } |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6821 | /* need to check if content is currently in the dictionary */ |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6822 | if ((node->content == (xmlChar *) &(node->properties)) || |
| 6823 | ((node->doc != NULL) && (node->doc->dict != NULL) && |
| 6824 | xmlDictOwns(node->doc->dict, node->content))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6825 | node->content = xmlStrncatNew(node->content, content, len); |
| 6826 | } else { |
| 6827 | node->content = xmlStrncat(node->content, content, len); |
| 6828 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6829 | node->properties = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6830 | if (node->content == NULL) |
| 6831 | return(-1); |
| 6832 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6833 | } |
| 6834 | |
| 6835 | /************************************************************************ |
| 6836 | * * |
| 6837 | * Output : to a FILE or in memory * |
| 6838 | * * |
| 6839 | ************************************************************************/ |
| 6840 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6841 | /** |
| 6842 | * xmlBufferCreate: |
| 6843 | * |
| 6844 | * routine to create an XML buffer. |
| 6845 | * returns the new structure. |
| 6846 | */ |
| 6847 | xmlBufferPtr |
| 6848 | xmlBufferCreate(void) { |
| 6849 | xmlBufferPtr ret; |
| 6850 | |
| 6851 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6852 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6853 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6854 | return(NULL); |
| 6855 | } |
| 6856 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 6857 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6858 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6859 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6860 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6861 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6862 | xmlFree(ret); |
| 6863 | return(NULL); |
| 6864 | } |
| 6865 | ret->content[0] = 0; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 6866 | ret->contentIO = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6867 | return(ret); |
| 6868 | } |
| 6869 | |
| 6870 | /** |
| 6871 | * xmlBufferCreateSize: |
| 6872 | * @size: initial size of buffer |
| 6873 | * |
| 6874 | * routine to create an XML buffer. |
| 6875 | * returns the new structure. |
| 6876 | */ |
| 6877 | xmlBufferPtr |
| 6878 | xmlBufferCreateSize(size_t size) { |
| 6879 | xmlBufferPtr ret; |
| 6880 | |
| 6881 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6882 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6883 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6884 | return(NULL); |
| 6885 | } |
| 6886 | ret->use = 0; |
| 6887 | ret->alloc = xmlBufferAllocScheme; |
| 6888 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 6889 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6890 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6891 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6892 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6893 | xmlFree(ret); |
| 6894 | return(NULL); |
| 6895 | } |
| 6896 | ret->content[0] = 0; |
| 6897 | } else |
| 6898 | ret->content = NULL; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 6899 | ret->contentIO = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6900 | return(ret); |
| 6901 | } |
| 6902 | |
| 6903 | /** |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6904 | * xmlBufferCreateStatic: |
| 6905 | * @mem: the memory area |
| 6906 | * @size: the size in byte |
| 6907 | * |
MST 2003 John Fleck | a0e7e93 | 2003-12-19 03:13:47 +0000 | [diff] [blame] | 6908 | * routine to create an XML buffer from an immutable memory area. |
| 6909 | * 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] | 6910 | * present until the end of the buffer lifetime. |
| 6911 | * |
| 6912 | * returns the new structure. |
| 6913 | */ |
| 6914 | xmlBufferPtr |
| 6915 | xmlBufferCreateStatic(void *mem, size_t size) { |
| 6916 | xmlBufferPtr ret; |
| 6917 | |
| 6918 | if ((mem == NULL) || (size == 0)) |
| 6919 | return(NULL); |
| 6920 | |
| 6921 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6922 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6923 | xmlTreeErrMemory("creating buffer"); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6924 | return(NULL); |
| 6925 | } |
| 6926 | ret->use = size; |
| 6927 | ret->size = size; |
| 6928 | ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; |
| 6929 | ret->content = (xmlChar *) mem; |
| 6930 | return(ret); |
| 6931 | } |
| 6932 | |
| 6933 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6934 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6935 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6936 | * @scheme: allocation scheme to use |
| 6937 | * |
| 6938 | * Sets the allocation scheme for this buffer |
| 6939 | */ |
| 6940 | void |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6941 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6942 | xmlBufferAllocationScheme scheme) { |
| 6943 | if (buf == NULL) { |
| 6944 | #ifdef DEBUG_BUFFER |
| 6945 | xmlGenericError(xmlGenericErrorContext, |
| 6946 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 6947 | #endif |
| 6948 | return; |
| 6949 | } |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 6950 | if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || |
| 6951 | (buf->alloc == XML_BUFFER_ALLOC_IO)) return; |
| 6952 | if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) || |
| 6953 | (scheme == XML_BUFFER_ALLOC_EXACT) || |
| 6954 | (scheme == XML_BUFFER_ALLOC_IMMUTABLE)) |
| 6955 | buf->alloc = scheme; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6956 | } |
| 6957 | |
| 6958 | /** |
| 6959 | * xmlBufferFree: |
| 6960 | * @buf: the buffer to free |
| 6961 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 6962 | * Frees an XML buffer. It frees both the content and the structure which |
| 6963 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6964 | */ |
| 6965 | void |
| 6966 | xmlBufferFree(xmlBufferPtr buf) { |
| 6967 | if (buf == NULL) { |
| 6968 | #ifdef DEBUG_BUFFER |
| 6969 | xmlGenericError(xmlGenericErrorContext, |
| 6970 | "xmlBufferFree: buf == NULL\n"); |
| 6971 | #endif |
| 6972 | return; |
| 6973 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6974 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 6975 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && |
| 6976 | (buf->contentIO != NULL)) { |
| 6977 | xmlFree(buf->contentIO); |
| 6978 | } else if ((buf->content != NULL) && |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6979 | (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6980 | xmlFree(buf->content); |
| 6981 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6982 | xmlFree(buf); |
| 6983 | } |
| 6984 | |
| 6985 | /** |
| 6986 | * xmlBufferEmpty: |
| 6987 | * @buf: the buffer |
| 6988 | * |
| 6989 | * empty a buffer. |
| 6990 | */ |
| 6991 | void |
| 6992 | xmlBufferEmpty(xmlBufferPtr buf) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 6993 | if (buf == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6994 | if (buf->content == NULL) return; |
| 6995 | buf->use = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 6996 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 6997 | buf->content = BAD_CAST ""; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 6998 | } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) && |
| 6999 | (buf->contentIO != NULL)) { |
| 7000 | size_t start_buf = buf->content - buf->contentIO; |
| 7001 | |
| 7002 | buf->size += start_buf; |
| 7003 | buf->content = buf->contentIO; |
| 7004 | buf->content[0] = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7005 | } else { |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7006 | buf->content[0] = 0; |
| 7007 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7008 | } |
| 7009 | |
| 7010 | /** |
| 7011 | * xmlBufferShrink: |
| 7012 | * @buf: the buffer to dump |
| 7013 | * @len: the number of xmlChar to remove |
| 7014 | * |
| 7015 | * Remove the beginning of an XML buffer. |
| 7016 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7017 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7018 | */ |
| 7019 | int |
| 7020 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 7021 | if (buf == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7022 | if (len == 0) return(0); |
| 7023 | if (len > buf->use) return(-1); |
| 7024 | |
| 7025 | buf->use -= len; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7026 | if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || |
| 7027 | ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) { |
| 7028 | /* |
| 7029 | * we just move the content pointer, but also make sure |
| 7030 | * the perceived buffer size has shrinked accordingly |
| 7031 | */ |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7032 | buf->content += len; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7033 | buf->size -= len; |
| 7034 | |
| 7035 | /* |
| 7036 | * sometimes though it maybe be better to really shrink |
| 7037 | * on IO buffers |
| 7038 | */ |
| 7039 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7040 | size_t start_buf = buf->content - buf->contentIO; |
| 7041 | if (start_buf >= buf->size) { |
| 7042 | memmove(buf->contentIO, &buf->content[0], buf->use); |
| 7043 | buf->content = buf->contentIO; |
| 7044 | buf->content[buf->use] = 0; |
| 7045 | buf->size += start_buf; |
| 7046 | } |
| 7047 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7048 | } else { |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7049 | memmove(buf->content, &buf->content[len], buf->use); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7050 | buf->content[buf->use] = 0; |
| 7051 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7052 | return(len); |
| 7053 | } |
| 7054 | |
| 7055 | /** |
| 7056 | * xmlBufferGrow: |
| 7057 | * @buf: the buffer |
| 7058 | * @len: the minimum free size to allocate |
| 7059 | * |
| 7060 | * Grow the available space of an XML buffer. |
| 7061 | * |
| 7062 | * Returns the new available space or -1 in case of error |
| 7063 | */ |
| 7064 | int |
| 7065 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 7066 | int size; |
| 7067 | xmlChar *newbuf; |
| 7068 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 7069 | if (buf == NULL) return(-1); |
| 7070 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7071 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7072 | if (len + buf->use < buf->size) return(0); |
| 7073 | |
Daniel Veillard | ee20cd7 | 2009-08-22 15:18:31 +0200 | [diff] [blame] | 7074 | /* |
| 7075 | * Windows has a BIG problem on realloc timing, so we try to double |
| 7076 | * the buffer size (if that's enough) (bug 146697) |
| 7077 | * Apparently BSD too, and it's probably best for linux too |
| 7078 | * On an embedded system this may be something to change |
| 7079 | */ |
| 7080 | #if 1 |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 7081 | if (buf->size > len) |
| 7082 | size = buf->size * 2; |
| 7083 | else |
| 7084 | size = buf->use + len + 100; |
| 7085 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7086 | size = buf->use + len + 100; |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 7087 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7088 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7089 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7090 | size_t start_buf = buf->content - buf->contentIO; |
| 7091 | |
| 7092 | newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size); |
| 7093 | if (newbuf == NULL) { |
| 7094 | xmlTreeErrMemory("growing buffer"); |
| 7095 | return(-1); |
| 7096 | } |
| 7097 | buf->contentIO = newbuf; |
| 7098 | buf->content = newbuf + start_buf; |
| 7099 | } else { |
| 7100 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
| 7101 | if (newbuf == NULL) { |
| 7102 | xmlTreeErrMemory("growing buffer"); |
| 7103 | return(-1); |
| 7104 | } |
| 7105 | buf->content = newbuf; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7106 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7107 | buf->size = size; |
| 7108 | return(buf->size - buf->use); |
| 7109 | } |
| 7110 | |
| 7111 | /** |
| 7112 | * xmlBufferDump: |
| 7113 | * @file: the file output |
| 7114 | * @buf: the buffer to dump |
| 7115 | * |
| 7116 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7117 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7118 | */ |
| 7119 | int |
| 7120 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 7121 | int ret; |
| 7122 | |
| 7123 | if (buf == NULL) { |
| 7124 | #ifdef DEBUG_BUFFER |
| 7125 | xmlGenericError(xmlGenericErrorContext, |
| 7126 | "xmlBufferDump: buf == NULL\n"); |
| 7127 | #endif |
| 7128 | return(0); |
| 7129 | } |
| 7130 | if (buf->content == NULL) { |
| 7131 | #ifdef DEBUG_BUFFER |
| 7132 | xmlGenericError(xmlGenericErrorContext, |
| 7133 | "xmlBufferDump: buf->content == NULL\n"); |
| 7134 | #endif |
| 7135 | return(0); |
| 7136 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 7137 | if (file == NULL) |
| 7138 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7139 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 7140 | return(ret); |
| 7141 | } |
| 7142 | |
| 7143 | /** |
| 7144 | * xmlBufferContent: |
| 7145 | * @buf: the buffer |
| 7146 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7147 | * Function to extract the content of a buffer |
| 7148 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7149 | * Returns the internal content |
| 7150 | */ |
| 7151 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7152 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7153 | xmlBufferContent(const xmlBufferPtr buf) |
| 7154 | { |
| 7155 | if(!buf) |
| 7156 | return NULL; |
| 7157 | |
| 7158 | return buf->content; |
| 7159 | } |
| 7160 | |
| 7161 | /** |
| 7162 | * xmlBufferLength: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7163 | * @buf: the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7164 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7165 | * Function to get the length of a buffer |
| 7166 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7167 | * Returns the length of data in the internal content |
| 7168 | */ |
| 7169 | |
| 7170 | int |
| 7171 | xmlBufferLength(const xmlBufferPtr buf) |
| 7172 | { |
| 7173 | if(!buf) |
| 7174 | return 0; |
| 7175 | |
| 7176 | return buf->use; |
| 7177 | } |
| 7178 | |
| 7179 | /** |
| 7180 | * xmlBufferResize: |
| 7181 | * @buf: the buffer to resize |
| 7182 | * @size: the desired size |
| 7183 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7184 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7185 | * |
| 7186 | * Returns 0 in case of problems, 1 otherwise |
| 7187 | */ |
| 7188 | int |
| 7189 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 7190 | { |
| 7191 | unsigned int newSize; |
| 7192 | xmlChar* rebuf = NULL; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7193 | size_t start_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7194 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7195 | if (buf == NULL) |
| 7196 | return(0); |
| 7197 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7198 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
| 7199 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7200 | /* Don't resize if we don't have to */ |
| 7201 | if (size < buf->size) |
| 7202 | return 1; |
| 7203 | |
| 7204 | /* figure out new size */ |
| 7205 | switch (buf->alloc){ |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7206 | case XML_BUFFER_ALLOC_IO: |
| 7207 | case XML_BUFFER_ALLOC_DOUBLEIT: |
| 7208 | /*take care of empty case*/ |
| 7209 | newSize = (buf->size ? buf->size*2 : size + 10); |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 7210 | while (size > newSize) { |
| 7211 | if (newSize > UINT_MAX / 2) { |
| 7212 | xmlTreeErrMemory("growing buffer"); |
| 7213 | return 0; |
| 7214 | } |
| 7215 | newSize *= 2; |
| 7216 | } |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7217 | break; |
| 7218 | case XML_BUFFER_ALLOC_EXACT: |
| 7219 | newSize = size+10; |
| 7220 | break; |
| 7221 | default: |
| 7222 | newSize = size+10; |
| 7223 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7224 | } |
| 7225 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7226 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7227 | start_buf = buf->content - buf->contentIO; |
| 7228 | |
| 7229 | if (start_buf > newSize) { |
| 7230 | /* move data back to start */ |
| 7231 | memmove(buf->contentIO, buf->content, buf->use); |
| 7232 | buf->content = buf->contentIO; |
| 7233 | buf->content[buf->use] = 0; |
| 7234 | buf->size += start_buf; |
| 7235 | } else { |
| 7236 | rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize); |
| 7237 | if (rebuf == NULL) { |
| 7238 | xmlTreeErrMemory("growing buffer"); |
| 7239 | return 0; |
| 7240 | } |
| 7241 | buf->contentIO = rebuf; |
| 7242 | buf->content = rebuf + start_buf; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7243 | } |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7244 | } else { |
| 7245 | if (buf->content == NULL) { |
| 7246 | rebuf = (xmlChar *) xmlMallocAtomic(newSize); |
| 7247 | } else if (buf->size - buf->use < 100) { |
| 7248 | rebuf = (xmlChar *) xmlRealloc(buf->content, newSize); |
| 7249 | } else { |
| 7250 | /* |
| 7251 | * if we are reallocating a buffer far from being full, it's |
| 7252 | * better to make a new allocation and copy only the used range |
| 7253 | * and free the old one. |
| 7254 | */ |
| 7255 | rebuf = (xmlChar *) xmlMallocAtomic(newSize); |
| 7256 | if (rebuf != NULL) { |
| 7257 | memcpy(rebuf, buf->content, buf->use); |
| 7258 | xmlFree(buf->content); |
| 7259 | rebuf[buf->use] = 0; |
| 7260 | } |
| 7261 | } |
| 7262 | if (rebuf == NULL) { |
| 7263 | xmlTreeErrMemory("growing buffer"); |
| 7264 | return 0; |
| 7265 | } |
| 7266 | buf->content = rebuf; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7267 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7268 | buf->size = newSize; |
| 7269 | |
| 7270 | return 1; |
| 7271 | } |
| 7272 | |
| 7273 | /** |
| 7274 | * xmlBufferAdd: |
| 7275 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7276 | * @str: the #xmlChar string |
| 7277 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7278 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7279 | * 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] | 7280 | * str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7281 | * |
| 7282 | * Returns 0 successful, a positive error code number otherwise |
| 7283 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7284 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7285 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7286 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 7287 | unsigned int needSize; |
| 7288 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7289 | if ((str == NULL) || (buf == NULL)) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7290 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7291 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7292 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7293 | if (len < -1) { |
| 7294 | #ifdef DEBUG_BUFFER |
| 7295 | xmlGenericError(xmlGenericErrorContext, |
| 7296 | "xmlBufferAdd: len < 0\n"); |
| 7297 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7298 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7299 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7300 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7301 | |
| 7302 | if (len < 0) |
| 7303 | len = xmlStrlen(str); |
| 7304 | |
Daniel Veillard | c992332 | 2007-04-24 18:12:06 +0000 | [diff] [blame] | 7305 | if (len < 0) return -1; |
| 7306 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7307 | |
| 7308 | needSize = buf->use + len + 2; |
| 7309 | if (needSize > buf->size){ |
| 7310 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7311 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7312 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7313 | } |
| 7314 | } |
| 7315 | |
| 7316 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 7317 | buf->use += len; |
| 7318 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7319 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7320 | } |
| 7321 | |
| 7322 | /** |
| 7323 | * xmlBufferAddHead: |
| 7324 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7325 | * @str: the #xmlChar string |
| 7326 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7327 | * |
| 7328 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7329 | * if len == -1, the length of @str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7330 | * |
| 7331 | * Returns 0 successful, a positive error code number otherwise |
| 7332 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7333 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7334 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7335 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 7336 | unsigned int needSize; |
| 7337 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7338 | if (buf == NULL) |
| 7339 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7340 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7341 | if (str == NULL) { |
| 7342 | #ifdef DEBUG_BUFFER |
| 7343 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7344 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7345 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7346 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7347 | } |
| 7348 | if (len < -1) { |
| 7349 | #ifdef DEBUG_BUFFER |
| 7350 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7351 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7352 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7353 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7354 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7355 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7356 | |
| 7357 | if (len < 0) |
| 7358 | len = xmlStrlen(str); |
| 7359 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7360 | if (len <= 0) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7361 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7362 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7363 | size_t start_buf = buf->content - buf->contentIO; |
| 7364 | |
| 7365 | if (start_buf > (unsigned int) len) { |
| 7366 | /* |
| 7367 | * We can add it in the space previously shrinked |
| 7368 | */ |
| 7369 | buf->content -= len; |
| 7370 | memmove(&buf->content[0], str, len); |
| 7371 | buf->use += len; |
| 7372 | buf->size += len; |
| 7373 | return(0); |
| 7374 | } |
| 7375 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7376 | needSize = buf->use + len + 2; |
| 7377 | if (needSize > buf->size){ |
| 7378 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7379 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7380 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7381 | } |
| 7382 | } |
| 7383 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7384 | memmove(&buf->content[len], &buf->content[0], buf->use); |
| 7385 | memmove(&buf->content[0], str, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7386 | buf->use += len; |
| 7387 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7388 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7389 | } |
| 7390 | |
| 7391 | /** |
| 7392 | * xmlBufferCat: |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7393 | * @buf: the buffer to add to |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7394 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7395 | * |
| 7396 | * Append a zero terminated string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7397 | * |
| 7398 | * Returns 0 successful, a positive error code number otherwise |
| 7399 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7400 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7401 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7402 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7403 | if (buf == NULL) |
| 7404 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7405 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
| 7406 | if (str == NULL) return -1; |
| 7407 | return xmlBufferAdd(buf, str, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7408 | } |
| 7409 | |
| 7410 | /** |
| 7411 | * xmlBufferCCat: |
| 7412 | * @buf: the buffer to dump |
| 7413 | * @str: the C char string |
| 7414 | * |
| 7415 | * Append a zero terminated C string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7416 | * |
| 7417 | * Returns 0 successful, a positive error code number otherwise |
| 7418 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7419 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7420 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7421 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 7422 | const char *cur; |
| 7423 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7424 | if (buf == NULL) |
| 7425 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7426 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7427 | if (str == NULL) { |
| 7428 | #ifdef DEBUG_BUFFER |
| 7429 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7430 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7431 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7432 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7433 | } |
| 7434 | for (cur = str;*cur != 0;cur++) { |
| 7435 | if (buf->use + 10 >= buf->size) { |
| 7436 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7437 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7438 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7439 | } |
| 7440 | } |
| 7441 | buf->content[buf->use++] = *cur; |
| 7442 | } |
| 7443 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7444 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7445 | } |
| 7446 | |
| 7447 | /** |
| 7448 | * xmlBufferWriteCHAR: |
| 7449 | * @buf: the XML buffer |
| 7450 | * @string: the string to add |
| 7451 | * |
| 7452 | * routine which manages and grows an output buffer. This one adds |
| 7453 | * xmlChars at the end of the buffer. |
| 7454 | */ |
| 7455 | void |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7456 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7457 | if (buf == NULL) |
| 7458 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7459 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7460 | xmlBufferCat(buf, string); |
| 7461 | } |
| 7462 | |
| 7463 | /** |
| 7464 | * xmlBufferWriteChar: |
| 7465 | * @buf: the XML buffer output |
| 7466 | * @string: the string to add |
| 7467 | * |
| 7468 | * routine which manage and grows an output buffer. This one add |
| 7469 | * C chars at the end of the array. |
| 7470 | */ |
| 7471 | void |
| 7472 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7473 | if (buf == NULL) |
| 7474 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7475 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7476 | xmlBufferCCat(buf, string); |
| 7477 | } |
| 7478 | |
| 7479 | |
| 7480 | /** |
| 7481 | * xmlBufferWriteQuotedString: |
| 7482 | * @buf: the XML buffer output |
| 7483 | * @string: the string to add |
| 7484 | * |
| 7485 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7486 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7487 | * quote or double-quotes internally |
| 7488 | */ |
| 7489 | void |
| 7490 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7491 | const xmlChar *cur, *base; |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7492 | if (buf == NULL) |
| 7493 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7494 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7495 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 7496 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7497 | #ifdef DEBUG_BUFFER |
| 7498 | xmlGenericError(xmlGenericErrorContext, |
| 7499 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 7500 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7501 | xmlBufferCCat(buf, "\""); |
| 7502 | base = cur = string; |
| 7503 | while(*cur != 0){ |
| 7504 | if(*cur == '"'){ |
| 7505 | if (base != cur) |
| 7506 | xmlBufferAdd(buf, base, cur - base); |
| 7507 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 7508 | cur++; |
| 7509 | base = cur; |
| 7510 | } |
| 7511 | else { |
| 7512 | cur++; |
| 7513 | } |
| 7514 | } |
| 7515 | if (base != cur) |
| 7516 | xmlBufferAdd(buf, base, cur - base); |
| 7517 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7518 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7519 | else{ |
| 7520 | xmlBufferCCat(buf, "\'"); |
| 7521 | xmlBufferCat(buf, string); |
| 7522 | xmlBufferCCat(buf, "\'"); |
| 7523 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7524 | } else { |
| 7525 | xmlBufferCCat(buf, "\""); |
| 7526 | xmlBufferCat(buf, string); |
| 7527 | xmlBufferCCat(buf, "\""); |
| 7528 | } |
| 7529 | } |
| 7530 | |
| 7531 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7532 | /** |
| 7533 | * xmlGetDocCompressMode: |
| 7534 | * @doc: the document |
| 7535 | * |
| 7536 | * get the compression ratio for a document, ZLIB based |
| 7537 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7538 | */ |
| 7539 | int |
| 7540 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 7541 | if (doc == NULL) return(-1); |
| 7542 | return(doc->compression); |
| 7543 | } |
| 7544 | |
| 7545 | /** |
| 7546 | * xmlSetDocCompressMode: |
| 7547 | * @doc: the document |
| 7548 | * @mode: the compression ratio |
| 7549 | * |
| 7550 | * set the compression ratio for a document, ZLIB based |
| 7551 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7552 | */ |
| 7553 | void |
| 7554 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 7555 | if (doc == NULL) return; |
| 7556 | if (mode < 0) doc->compression = 0; |
| 7557 | else if (mode > 9) doc->compression = 9; |
| 7558 | else doc->compression = mode; |
| 7559 | } |
| 7560 | |
| 7561 | /** |
| 7562 | * xmlGetCompressMode: |
| 7563 | * |
| 7564 | * get the default compression mode used, ZLIB based. |
| 7565 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7566 | */ |
| 7567 | int |
| 7568 | xmlGetCompressMode(void) |
| 7569 | { |
| 7570 | return (xmlCompressMode); |
| 7571 | } |
| 7572 | |
| 7573 | /** |
| 7574 | * xmlSetCompressMode: |
| 7575 | * @mode: the compression ratio |
| 7576 | * |
| 7577 | * set the default compression mode used, ZLIB based |
| 7578 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7579 | */ |
| 7580 | void |
| 7581 | xmlSetCompressMode(int mode) { |
| 7582 | if (mode < 0) xmlCompressMode = 0; |
| 7583 | else if (mode > 9) xmlCompressMode = 9; |
| 7584 | else xmlCompressMode = mode; |
| 7585 | } |
| 7586 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7587 | #define XML_TREE_NSMAP_PARENT -1 |
| 7588 | #define XML_TREE_NSMAP_XML -2 |
| 7589 | #define XML_TREE_NSMAP_DOC -3 |
| 7590 | #define XML_TREE_NSMAP_CUSTOM -4 |
| 7591 | |
| 7592 | typedef struct xmlNsMapItem *xmlNsMapItemPtr; |
| 7593 | struct xmlNsMapItem { |
| 7594 | xmlNsMapItemPtr next; |
| 7595 | xmlNsMapItemPtr prev; |
| 7596 | xmlNsPtr oldNs; /* old ns decl reference */ |
| 7597 | xmlNsPtr newNs; /* new ns decl reference */ |
| 7598 | int shadowDepth; /* Shadowed at this depth */ |
| 7599 | /* |
| 7600 | * depth: |
| 7601 | * >= 0 == @node's ns-decls |
| 7602 | * -1 == @parent's ns-decls |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7603 | * -2 == the doc->oldNs XML ns-decl |
| 7604 | * -3 == the doc->oldNs storage ns-decls |
| 7605 | * -4 == ns-decls provided via custom ns-handling |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7606 | */ |
| 7607 | int depth; |
| 7608 | }; |
| 7609 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7610 | typedef struct xmlNsMap *xmlNsMapPtr; |
| 7611 | struct xmlNsMap { |
| 7612 | xmlNsMapItemPtr first; |
| 7613 | xmlNsMapItemPtr last; |
| 7614 | xmlNsMapItemPtr pool; |
| 7615 | }; |
| 7616 | |
| 7617 | #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL)) |
| 7618 | #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next) |
| 7619 | #define XML_NSMAP_POP(m, i) \ |
| 7620 | i = (m)->last; \ |
| 7621 | (m)->last = (i)->prev; \ |
| 7622 | if ((m)->last == NULL) \ |
| 7623 | (m)->first = NULL; \ |
| 7624 | else \ |
| 7625 | (m)->last->next = NULL; \ |
| 7626 | (i)->next = (m)->pool; \ |
| 7627 | (m)->pool = i; |
| 7628 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7629 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7630 | * xmlDOMWrapNsMapFree: |
| 7631 | * @map: the ns-map |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7632 | * |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7633 | * Frees the ns-map |
| 7634 | */ |
| 7635 | static void |
| 7636 | xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap) |
| 7637 | { |
| 7638 | xmlNsMapItemPtr cur, tmp; |
| 7639 | |
| 7640 | if (nsmap == NULL) |
| 7641 | return; |
| 7642 | cur = nsmap->pool; |
| 7643 | while (cur != NULL) { |
| 7644 | tmp = cur; |
| 7645 | cur = cur->next; |
| 7646 | xmlFree(tmp); |
| 7647 | } |
| 7648 | cur = nsmap->first; |
| 7649 | while (cur != NULL) { |
| 7650 | tmp = cur; |
| 7651 | cur = cur->next; |
| 7652 | xmlFree(tmp); |
| 7653 | } |
| 7654 | xmlFree(nsmap); |
| 7655 | } |
| 7656 | |
| 7657 | /* |
| 7658 | * xmlDOMWrapNsMapAddItem: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7659 | * @map: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7660 | * @oldNs: the old ns-struct |
| 7661 | * @newNs: the new ns-struct |
| 7662 | * @depth: depth and ns-kind information |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7663 | * |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7664 | * Adds an ns-mapping item. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7665 | */ |
| 7666 | static xmlNsMapItemPtr |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7667 | xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7668 | xmlNsPtr oldNs, xmlNsPtr newNs, int depth) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7669 | { |
| 7670 | xmlNsMapItemPtr ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7671 | xmlNsMapPtr map; |
| 7672 | |
| 7673 | if (nsmap == NULL) |
| 7674 | return(NULL); |
| 7675 | if ((position != -1) && (position != 0)) |
| 7676 | return(NULL); |
| 7677 | map = *nsmap; |
| 7678 | |
| 7679 | if (map == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7680 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7681 | * Create the ns-map. |
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 | map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap)); |
| 7684 | if (map == NULL) { |
| 7685 | xmlTreeErrMemory("allocating namespace map"); |
| 7686 | return (NULL); |
| 7687 | } |
| 7688 | memset(map, 0, sizeof(struct xmlNsMap)); |
| 7689 | *nsmap = map; |
| 7690 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7691 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7692 | if (map->pool != NULL) { |
| 7693 | /* |
| 7694 | * Reuse an item from the pool. |
| 7695 | */ |
| 7696 | ret = map->pool; |
| 7697 | map->pool = ret->next; |
| 7698 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7699 | } else { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7700 | /* |
| 7701 | * Create a new item. |
| 7702 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7703 | ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem)); |
| 7704 | if (ret == NULL) { |
| 7705 | xmlTreeErrMemory("allocating namespace map item"); |
| 7706 | return (NULL); |
| 7707 | } |
| 7708 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7709 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7710 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7711 | if (map->first == NULL) { |
| 7712 | /* |
| 7713 | * First ever. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7714 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7715 | map->first = ret; |
| 7716 | map->last = ret; |
| 7717 | } else if (position == -1) { |
| 7718 | /* |
| 7719 | * Append. |
| 7720 | */ |
| 7721 | ret->prev = map->last; |
| 7722 | map->last->next = ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7723 | map->last = ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7724 | } else if (position == 0) { |
| 7725 | /* |
| 7726 | * Set on first position. |
| 7727 | */ |
| 7728 | map->first->prev = ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7729 | ret->next = map->first; |
| 7730 | map->first = ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7731 | } else |
| 7732 | return(NULL); |
| 7733 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7734 | ret->oldNs = oldNs; |
| 7735 | ret->newNs = newNs; |
| 7736 | ret->shadowDepth = -1; |
| 7737 | ret->depth = depth; |
| 7738 | return (ret); |
| 7739 | } |
| 7740 | |
| 7741 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7742 | * xmlDOMWrapStoreNs: |
| 7743 | * @doc: the doc |
| 7744 | * @nsName: the namespace name |
| 7745 | * @prefix: the prefix |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7746 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7747 | * Creates or reuses an xmlNs struct on doc->oldNs with |
| 7748 | * the given prefix and namespace name. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7749 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7750 | * Returns the aquired ns struct or NULL in case of an API |
| 7751 | * or internal error. |
| 7752 | */ |
| 7753 | static xmlNsPtr |
| 7754 | xmlDOMWrapStoreNs(xmlDocPtr doc, |
| 7755 | const xmlChar *nsName, |
| 7756 | const xmlChar *prefix) |
| 7757 | { |
| 7758 | xmlNsPtr ns; |
| 7759 | |
| 7760 | if (doc == NULL) |
| 7761 | return (NULL); |
| 7762 | ns = xmlTreeEnsureXMLDecl(doc); |
| 7763 | if (ns == NULL) |
| 7764 | return (NULL); |
| 7765 | if (ns->next != NULL) { |
| 7766 | /* Reuse. */ |
| 7767 | ns = ns->next; |
| 7768 | while (ns != NULL) { |
| 7769 | if (((ns->prefix == prefix) || |
| 7770 | xmlStrEqual(ns->prefix, prefix)) && |
| 7771 | xmlStrEqual(ns->href, nsName)) { |
| 7772 | return (ns); |
| 7773 | } |
| 7774 | if (ns->next == NULL) |
| 7775 | break; |
| 7776 | ns = ns->next; |
| 7777 | } |
| 7778 | } |
| 7779 | /* Create. */ |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 7780 | if (ns != NULL) { |
| 7781 | ns->next = xmlNewNs(NULL, nsName, prefix); |
| 7782 | return (ns->next); |
| 7783 | } |
| 7784 | return(NULL); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7785 | } |
| 7786 | |
| 7787 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7788 | * xmlDOMWrapNewCtxt: |
| 7789 | * |
| 7790 | * Allocates and initializes a new DOM-wrapper context. |
| 7791 | * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7792 | * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7793 | */ |
| 7794 | xmlDOMWrapCtxtPtr |
| 7795 | xmlDOMWrapNewCtxt(void) |
| 7796 | { |
| 7797 | xmlDOMWrapCtxtPtr ret; |
| 7798 | |
| 7799 | ret = xmlMalloc(sizeof(xmlDOMWrapCtxt)); |
| 7800 | if (ret == NULL) { |
| 7801 | xmlTreeErrMemory("allocating DOM-wrapper context"); |
| 7802 | return (NULL); |
| 7803 | } |
| 7804 | memset(ret, 0, sizeof(xmlDOMWrapCtxt)); |
| 7805 | return (ret); |
| 7806 | } |
| 7807 | |
| 7808 | /* |
| 7809 | * xmlDOMWrapFreeCtxt: |
| 7810 | * @ctxt: the DOM-wrapper context |
| 7811 | * |
| 7812 | * Frees the DOM-wrapper context. |
| 7813 | */ |
| 7814 | void |
| 7815 | xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt) |
| 7816 | { |
| 7817 | if (ctxt == NULL) |
| 7818 | return; |
| 7819 | if (ctxt->namespaceMap != NULL) |
| 7820 | xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap); |
| 7821 | /* |
| 7822 | * TODO: Store the namespace map in the context. |
| 7823 | */ |
| 7824 | xmlFree(ctxt); |
| 7825 | } |
| 7826 | |
| 7827 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7828 | * xmlTreeLookupNsListByPrefix: |
| 7829 | * @nsList: a list of ns-structs |
| 7830 | * @prefix: the searched prefix |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7831 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7832 | * Searches for a ns-decl with the given prefix in @nsList. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7833 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7834 | * Returns the ns-decl if found, NULL if not found and on |
| 7835 | * API errors. |
| 7836 | */ |
| 7837 | static xmlNsPtr |
| 7838 | xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix) |
| 7839 | { |
| 7840 | if (nsList == NULL) |
| 7841 | return (NULL); |
| 7842 | { |
| 7843 | xmlNsPtr ns; |
| 7844 | ns = nsList; |
| 7845 | do { |
| 7846 | if ((prefix == ns->prefix) || |
| 7847 | xmlStrEqual(prefix, ns->prefix)) { |
| 7848 | return (ns); |
| 7849 | } |
| 7850 | ns = ns->next; |
| 7851 | } while (ns != NULL); |
| 7852 | } |
| 7853 | return (NULL); |
| 7854 | } |
| 7855 | |
| 7856 | /* |
| 7857 | * |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7858 | * xmlDOMWrapNSNormGatherInScopeNs: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7859 | * @map: the namespace map |
| 7860 | * @node: the node to start with |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7861 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7862 | * Puts in-scope namespaces into the ns-map. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7863 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7864 | * Returns 0 on success, -1 on API or internal errors. |
| 7865 | */ |
| 7866 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7867 | xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7868 | xmlNodePtr node) |
| 7869 | { |
| 7870 | xmlNodePtr cur; |
| 7871 | xmlNsPtr ns; |
| 7872 | xmlNsMapItemPtr mi; |
| 7873 | int shadowed; |
| 7874 | |
| 7875 | if ((map == NULL) || (*map != NULL)) |
| 7876 | return (-1); |
| 7877 | /* |
| 7878 | * Get in-scope ns-decls of @parent. |
| 7879 | */ |
| 7880 | cur = node; |
| 7881 | while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) { |
| 7882 | if (cur->type == XML_ELEMENT_NODE) { |
| 7883 | if (cur->nsDef != NULL) { |
| 7884 | ns = cur->nsDef; |
| 7885 | do { |
| 7886 | shadowed = 0; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7887 | if (XML_NSMAP_NOTEMPTY(*map)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7888 | /* |
| 7889 | * Skip shadowed prefixes. |
| 7890 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7891 | XML_NSMAP_FOREACH(*map, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7892 | if ((ns->prefix == mi->newNs->prefix) || |
| 7893 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) { |
| 7894 | shadowed = 1; |
| 7895 | break; |
| 7896 | } |
| 7897 | } |
| 7898 | } |
| 7899 | /* |
| 7900 | * Insert mapping. |
| 7901 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7902 | mi = xmlDOMWrapNsMapAddItem(map, 0, NULL, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7903 | ns, XML_TREE_NSMAP_PARENT); |
| 7904 | if (mi == NULL) |
| 7905 | return (-1); |
| 7906 | if (shadowed) |
| 7907 | mi->shadowDepth = 0; |
| 7908 | ns = ns->next; |
| 7909 | } while (ns != NULL); |
| 7910 | } |
| 7911 | } |
| 7912 | cur = cur->parent; |
| 7913 | } |
| 7914 | return (0); |
| 7915 | } |
| 7916 | |
| 7917 | /* |
| 7918 | * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict; |
| 7919 | * otherwise copy it, when it was in the source-dict. |
| 7920 | */ |
| 7921 | #define XML_TREE_ADOPT_STR(str) \ |
| 7922 | if (adoptStr && (str != NULL)) { \ |
| 7923 | if (destDoc->dict) { \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7924 | const xmlChar *old = str; \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7925 | str = xmlDictLookup(destDoc->dict, str, -1); \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 7926 | if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \ |
| 7927 | (!xmlDictOwns(sourceDoc->dict, old))) \ |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 7928 | xmlFree((char *)old); \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7929 | } else if ((sourceDoc) && (sourceDoc->dict) && \ |
| 7930 | xmlDictOwns(sourceDoc->dict, str)) { \ |
| 7931 | str = BAD_CAST xmlStrdup(str); \ |
| 7932 | } \ |
| 7933 | } |
| 7934 | |
| 7935 | /* |
| 7936 | * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then |
| 7937 | * put it in dest-dict or copy it. |
| 7938 | */ |
| 7939 | #define XML_TREE_ADOPT_STR_2(str) \ |
| 7940 | if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \ |
| 7941 | (sourceDoc->dict != NULL) && \ |
| 7942 | xmlDictOwns(sourceDoc->dict, cur->content)) { \ |
| 7943 | if (destDoc->dict) \ |
| 7944 | cur->content = (xmlChar *) \ |
| 7945 | xmlDictLookup(destDoc->dict, cur->content, -1); \ |
| 7946 | else \ |
| 7947 | cur->content = xmlStrdup(BAD_CAST cur->content); \ |
| 7948 | } |
| 7949 | |
| 7950 | /* |
| 7951 | * xmlDOMWrapNSNormAddNsMapItem2: |
| 7952 | * |
| 7953 | * For internal use. Adds a ns-decl mapping. |
| 7954 | * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7955 | * Returns 0 on success, -1 on internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7956 | */ |
| 7957 | static int |
| 7958 | xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number, |
| 7959 | xmlNsPtr oldNs, xmlNsPtr newNs) |
| 7960 | { |
| 7961 | if (*list == NULL) { |
| 7962 | *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr)); |
| 7963 | if (*list == NULL) { |
| 7964 | xmlTreeErrMemory("alloc ns map item"); |
| 7965 | return(-1); |
| 7966 | } |
| 7967 | *size = 3; |
| 7968 | *number = 0; |
| 7969 | } else if ((*number) >= (*size)) { |
| 7970 | *size *= 2; |
| 7971 | *list = (xmlNsPtr *) xmlRealloc(*list, |
| 7972 | (*size) * 2 * sizeof(xmlNsPtr)); |
| 7973 | if (*list == NULL) { |
| 7974 | xmlTreeErrMemory("realloc ns map item"); |
| 7975 | return(-1); |
| 7976 | } |
| 7977 | } |
| 7978 | (*list)[2 * (*number)] = oldNs; |
| 7979 | (*list)[2 * (*number) +1] = newNs; |
| 7980 | (*number)++; |
| 7981 | return (0); |
| 7982 | } |
| 7983 | |
| 7984 | /* |
| 7985 | * xmlDOMWrapRemoveNode: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7986 | * @ctxt: a DOM wrapper context |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7987 | * @doc: the doc |
| 7988 | * @node: the node to be removed. |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 7989 | * @options: set of options, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7990 | * |
| 7991 | * Unlinks the given node from its owner. |
| 7992 | * This will substitute ns-references to node->nsDef for |
| 7993 | * ns-references to doc->oldNs, thus ensuring the removed |
| 7994 | * branch to be autark wrt ns-references. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7995 | * |
| 7996 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7997 | * |
| 7998 | * Returns 0 on success, 1 if the node is not supported, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7999 | * -1 on API and internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8000 | */ |
| 8001 | int |
| 8002 | xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc, |
| 8003 | xmlNodePtr node, int options ATTRIBUTE_UNUSED) |
| 8004 | { |
| 8005 | xmlNsPtr *list = NULL; |
| 8006 | int sizeList, nbList, i, j; |
| 8007 | xmlNsPtr ns; |
| 8008 | |
| 8009 | if ((node == NULL) || (doc == NULL) || (node->doc != doc)) |
| 8010 | return (-1); |
| 8011 | |
| 8012 | /* TODO: 0 or -1 ? */ |
| 8013 | if (node->parent == NULL) |
| 8014 | return (0); |
| 8015 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8016 | switch (node->type) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8017 | case XML_TEXT_NODE: |
| 8018 | case XML_CDATA_SECTION_NODE: |
| 8019 | case XML_ENTITY_REF_NODE: |
| 8020 | case XML_PI_NODE: |
| 8021 | case XML_COMMENT_NODE: |
| 8022 | xmlUnlinkNode(node); |
| 8023 | return (0); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8024 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8025 | case XML_ATTRIBUTE_NODE: |
| 8026 | break; |
| 8027 | default: |
| 8028 | return (1); |
| 8029 | } |
| 8030 | xmlUnlinkNode(node); |
| 8031 | /* |
| 8032 | * Save out-of-scope ns-references in doc->oldNs. |
| 8033 | */ |
| 8034 | do { |
| 8035 | switch (node->type) { |
| 8036 | case XML_ELEMENT_NODE: |
| 8037 | if ((ctxt == NULL) && (node->nsDef != NULL)) { |
| 8038 | ns = node->nsDef; |
| 8039 | do { |
| 8040 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 8041 | &nbList, ns, ns) == -1) |
| 8042 | goto internal_error; |
| 8043 | ns = ns->next; |
| 8044 | } while (ns != NULL); |
| 8045 | } |
| 8046 | /* No break on purpose. */ |
| 8047 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8048 | if (node->ns != NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8049 | /* |
| 8050 | * Find a mapping. |
| 8051 | */ |
| 8052 | if (list != NULL) { |
| 8053 | for (i = 0, j = 0; i < nbList; i++, j += 2) { |
| 8054 | if (node->ns == list[j]) { |
| 8055 | node->ns = list[++j]; |
| 8056 | goto next_node; |
| 8057 | } |
| 8058 | } |
| 8059 | } |
| 8060 | ns = NULL; |
| 8061 | if (ctxt != NULL) { |
| 8062 | /* |
| 8063 | * User defined. |
| 8064 | */ |
| 8065 | } else { |
| 8066 | /* |
| 8067 | * Add to doc's oldNs. |
| 8068 | */ |
| 8069 | ns = xmlDOMWrapStoreNs(doc, node->ns->href, |
| 8070 | node->ns->prefix); |
| 8071 | if (ns == NULL) |
| 8072 | goto internal_error; |
| 8073 | } |
| 8074 | if (ns != NULL) { |
| 8075 | /* |
| 8076 | * Add mapping. |
| 8077 | */ |
| 8078 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 8079 | &nbList, node->ns, ns) == -1) |
| 8080 | goto internal_error; |
| 8081 | } |
| 8082 | node->ns = ns; |
| 8083 | } |
| 8084 | if ((node->type == XML_ELEMENT_NODE) && |
| 8085 | (node->properties != NULL)) { |
| 8086 | node = (xmlNodePtr) node->properties; |
| 8087 | continue; |
| 8088 | } |
| 8089 | break; |
| 8090 | default: |
| 8091 | goto next_sibling; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8092 | } |
| 8093 | next_node: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8094 | if ((node->type == XML_ELEMENT_NODE) && |
| 8095 | (node->children != NULL)) { |
| 8096 | node = node->children; |
| 8097 | continue; |
| 8098 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8099 | next_sibling: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8100 | if (node == NULL) |
| 8101 | break; |
| 8102 | if (node->next != NULL) |
| 8103 | node = node->next; |
| 8104 | else { |
| 8105 | node = node->parent; |
| 8106 | goto next_sibling; |
| 8107 | } |
| 8108 | } while (node != NULL); |
| 8109 | |
| 8110 | if (list != NULL) |
| 8111 | xmlFree(list); |
| 8112 | return (0); |
| 8113 | |
| 8114 | internal_error: |
| 8115 | if (list != NULL) |
| 8116 | xmlFree(list); |
| 8117 | return (-1); |
| 8118 | } |
| 8119 | |
| 8120 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8121 | * xmlSearchNsByNamespaceStrict: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8122 | * @doc: the document |
| 8123 | * @node: the start node |
| 8124 | * @nsName: the searched namespace name |
| 8125 | * @retNs: the resulting ns-decl |
| 8126 | * @prefixed: if the found ns-decl must have a prefix (for attributes) |
| 8127 | * |
| 8128 | * Dynamically searches for a ns-declaration which matches |
| 8129 | * the given @nsName in the ancestor-or-self axis of @node. |
| 8130 | * |
| 8131 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8132 | * and internal errors. |
| 8133 | */ |
| 8134 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8135 | xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node, |
| 8136 | const xmlChar* nsName, |
| 8137 | xmlNsPtr *retNs, int prefixed) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8138 | { |
| 8139 | xmlNodePtr cur, prev = NULL, out = NULL; |
| 8140 | xmlNsPtr ns, prevns; |
| 8141 | |
| 8142 | if ((doc == NULL) || (nsName == NULL) || (retNs == NULL)) |
| 8143 | return (-1); |
| 8144 | |
| 8145 | *retNs = NULL; |
| 8146 | if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
| 8147 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8148 | if (*retNs == NULL) |
| 8149 | return (-1); |
| 8150 | return (1); |
| 8151 | } |
| 8152 | cur = node; |
| 8153 | do { |
| 8154 | if (cur->type == XML_ELEMENT_NODE) { |
| 8155 | if (cur->nsDef != NULL) { |
| 8156 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8157 | if (prefixed && (ns->prefix == NULL)) |
| 8158 | continue; |
| 8159 | if (prev != NULL) { |
| 8160 | /* |
| 8161 | * Check the last level of ns-decls for a |
| 8162 | * shadowing prefix. |
| 8163 | */ |
| 8164 | prevns = prev->nsDef; |
| 8165 | do { |
| 8166 | if ((prevns->prefix == ns->prefix) || |
| 8167 | ((prevns->prefix != NULL) && |
| 8168 | (ns->prefix != NULL) && |
| 8169 | xmlStrEqual(prevns->prefix, ns->prefix))) { |
| 8170 | /* |
| 8171 | * Shadowed. |
| 8172 | */ |
| 8173 | break; |
| 8174 | } |
| 8175 | prevns = prevns->next; |
| 8176 | } while (prevns != NULL); |
| 8177 | if (prevns != NULL) |
| 8178 | continue; |
| 8179 | } |
| 8180 | /* |
| 8181 | * Ns-name comparison. |
| 8182 | */ |
| 8183 | if ((nsName == ns->href) || |
| 8184 | xmlStrEqual(nsName, ns->href)) { |
| 8185 | /* |
| 8186 | * At this point the prefix can only be shadowed, |
| 8187 | * if we are the the (at least) 3rd level of |
| 8188 | * ns-decls. |
| 8189 | */ |
| 8190 | if (out) { |
| 8191 | int ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8192 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8193 | ret = xmlNsInScope(doc, node, prev, ns->prefix); |
| 8194 | if (ret < 0) |
| 8195 | return (-1); |
| 8196 | /* |
| 8197 | * TODO: Should we try to find a matching ns-name |
| 8198 | * only once? This here keeps on searching. |
| 8199 | * I think we should try further since, there might |
| 8200 | * be an other matching ns-decl with an unshadowed |
| 8201 | * prefix. |
| 8202 | */ |
| 8203 | if (! ret) |
| 8204 | continue; |
| 8205 | } |
| 8206 | *retNs = ns; |
| 8207 | return (1); |
| 8208 | } |
| 8209 | } |
| 8210 | out = prev; |
| 8211 | prev = cur; |
| 8212 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8213 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 8214 | (cur->type == XML_ENTITY_DECL)) |
| 8215 | return (0); |
| 8216 | cur = cur->parent; |
| 8217 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 8218 | return (0); |
| 8219 | } |
| 8220 | |
| 8221 | /* |
| 8222 | * xmlSearchNsByPrefixStrict: |
| 8223 | * @doc: the document |
| 8224 | * @node: the start node |
| 8225 | * @prefix: the searched namespace prefix |
| 8226 | * @retNs: the resulting ns-decl |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8227 | * |
| 8228 | * Dynamically searches for a ns-declaration which matches |
| 8229 | * the given @nsName in the ancestor-or-self axis of @node. |
| 8230 | * |
| 8231 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8232 | * and internal errors. |
| 8233 | */ |
| 8234 | static int |
| 8235 | xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node, |
| 8236 | const xmlChar* prefix, |
| 8237 | xmlNsPtr *retNs) |
| 8238 | { |
| 8239 | xmlNodePtr cur; |
| 8240 | xmlNsPtr ns; |
| 8241 | |
| 8242 | if ((doc == NULL) || (node == NULL)) |
| 8243 | return (-1); |
| 8244 | |
| 8245 | if (retNs) |
| 8246 | *retNs = NULL; |
| 8247 | if (IS_STR_XML(prefix)) { |
| 8248 | if (retNs) { |
| 8249 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8250 | if (*retNs == NULL) |
| 8251 | return (-1); |
| 8252 | } |
| 8253 | return (1); |
| 8254 | } |
| 8255 | cur = node; |
| 8256 | do { |
| 8257 | if (cur->type == XML_ELEMENT_NODE) { |
| 8258 | if (cur->nsDef != NULL) { |
| 8259 | ns = cur->nsDef; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8260 | do { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8261 | if ((prefix == ns->prefix) || |
| 8262 | xmlStrEqual(prefix, ns->prefix)) |
| 8263 | { |
| 8264 | /* |
| 8265 | * Disabled namespaces, e.g. xmlns:abc="". |
| 8266 | */ |
| 8267 | if (ns->href == NULL) |
| 8268 | return(0); |
| 8269 | if (retNs) |
| 8270 | *retNs = ns; |
| 8271 | return (1); |
| 8272 | } |
| 8273 | ns = ns->next; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8274 | } while (ns != NULL); |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8275 | } |
| 8276 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 8277 | (cur->type == XML_ENTITY_DECL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8278 | return (0); |
| 8279 | cur = cur->parent; |
| 8280 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 8281 | return (0); |
| 8282 | } |
| 8283 | |
| 8284 | /* |
| 8285 | * xmlDOMWrapNSNormDeclareNsForced: |
| 8286 | * @doc: the doc |
| 8287 | * @elem: the element-node to declare on |
| 8288 | * @nsName: the namespace-name of the ns-decl |
| 8289 | * @prefix: the preferred prefix of the ns-decl |
| 8290 | * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls |
| 8291 | * |
| 8292 | * Declares a new namespace on @elem. It tries to use the |
| 8293 | * given @prefix; if a ns-decl with the given prefix is already existent |
| 8294 | * on @elem, it will generate an other prefix. |
| 8295 | * |
| 8296 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8297 | * and internal errors. |
| 8298 | */ |
| 8299 | static xmlNsPtr |
| 8300 | xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc, |
| 8301 | xmlNodePtr elem, |
| 8302 | const xmlChar *nsName, |
| 8303 | const xmlChar *prefix, |
| 8304 | int checkShadow) |
| 8305 | { |
| 8306 | |
| 8307 | xmlNsPtr ret; |
| 8308 | char buf[50]; |
| 8309 | const xmlChar *pref; |
| 8310 | int counter = 0; |
| 8311 | /* |
| 8312 | * Create a ns-decl on @anchor. |
| 8313 | */ |
| 8314 | pref = prefix; |
| 8315 | while (1) { |
| 8316 | /* |
| 8317 | * Lookup whether the prefix is unused in elem's ns-decls. |
| 8318 | */ |
| 8319 | if ((elem->nsDef != NULL) && |
| 8320 | (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL)) |
| 8321 | goto ns_next_prefix; |
| 8322 | if (checkShadow && elem->parent && |
| 8323 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8324 | /* |
| 8325 | * Does it shadow ancestor ns-decls? |
| 8326 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8327 | if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8328 | goto ns_next_prefix; |
| 8329 | } |
| 8330 | ret = xmlNewNs(NULL, nsName, pref); |
| 8331 | if (ret == NULL) |
| 8332 | return (NULL); |
| 8333 | if (elem->nsDef == NULL) |
| 8334 | elem->nsDef = ret; |
| 8335 | else { |
| 8336 | xmlNsPtr ns2 = elem->nsDef; |
| 8337 | while (ns2->next != NULL) |
| 8338 | ns2 = ns2->next; |
| 8339 | ns2->next = ret; |
| 8340 | } |
| 8341 | return (ret); |
| 8342 | ns_next_prefix: |
| 8343 | counter++; |
| 8344 | if (counter > 1000) |
| 8345 | return (NULL); |
| 8346 | if (prefix == NULL) { |
| 8347 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8348 | "ns_%d", counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8349 | } else |
| 8350 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8351 | "%.30s_%d", (char *)prefix, counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8352 | pref = BAD_CAST buf; |
| 8353 | } |
| 8354 | } |
| 8355 | |
| 8356 | /* |
| 8357 | * xmlDOMWrapNSNormAquireNormalizedNs: |
| 8358 | * @doc: the doc |
| 8359 | * @elem: the element-node to declare namespaces on |
| 8360 | * @ns: the ns-struct to use for the search |
| 8361 | * @retNs: the found/created ns-struct |
| 8362 | * @nsMap: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8363 | * @depth: the current tree depth |
| 8364 | * @ancestorsOnly: search in ancestor ns-decls only |
| 8365 | * @prefixed: if the searched ns-decl must have a prefix (for attributes) |
| 8366 | * |
| 8367 | * Searches for a matching ns-name in the ns-decls of @nsMap, if not |
| 8368 | * found it will either declare it on @elem, or store it in doc->oldNs. |
| 8369 | * If a new ns-decl needs to be declared on @elem, it tries to use the |
| 8370 | * @ns->prefix for it, if this prefix is already in use on @elem, it will |
| 8371 | * change the prefix or the new ns-decl. |
| 8372 | * |
| 8373 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8374 | */ |
| 8375 | static int |
| 8376 | xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc, |
| 8377 | xmlNodePtr elem, |
| 8378 | xmlNsPtr ns, |
| 8379 | xmlNsPtr *retNs, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8380 | xmlNsMapPtr *nsMap, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8381 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8382 | int depth, |
| 8383 | int ancestorsOnly, |
| 8384 | int prefixed) |
| 8385 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8386 | xmlNsMapItemPtr mi; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8387 | |
| 8388 | if ((doc == NULL) || (ns == NULL) || (retNs == NULL) || |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8389 | (nsMap == NULL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8390 | return (-1); |
| 8391 | |
| 8392 | *retNs = NULL; |
| 8393 | /* |
| 8394 | * Handle XML namespace. |
| 8395 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8396 | if (IS_STR_XML(ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8397 | /* |
| 8398 | * Insert XML namespace mapping. |
| 8399 | */ |
| 8400 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8401 | if (*retNs == NULL) |
| 8402 | return (-1); |
| 8403 | return (0); |
| 8404 | } |
| 8405 | /* |
| 8406 | * If the search should be done in ancestors only and no |
| 8407 | * @elem (the first ancestor) was specified, then skip the search. |
| 8408 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8409 | if ((XML_NSMAP_NOTEMPTY(*nsMap)) && |
| 8410 | (! (ancestorsOnly && (elem == NULL)))) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8411 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8412 | /* |
| 8413 | * Try to find an equal ns-name in in-scope ns-decls. |
| 8414 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8415 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8416 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8417 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8418 | * ancestorsOnly: This should be turned on to gain speed, |
| 8419 | * if one knows that the branch itself was already |
| 8420 | * ns-wellformed and no stale references existed. |
| 8421 | * I.e. it searches in the ancestor axis only. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8422 | */ |
| 8423 | ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) && |
| 8424 | /* Skip shadowed prefixes. */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8425 | (mi->shadowDepth == -1) && |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8426 | /* Skip xmlns="" or xmlns:foo="". */ |
| 8427 | ((mi->newNs->href != NULL) && |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8428 | (mi->newNs->href[0] != 0)) && |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8429 | /* Ensure a prefix if wanted. */ |
| 8430 | ((! prefixed) || (mi->newNs->prefix != NULL)) && |
| 8431 | /* Equal ns name */ |
| 8432 | ((mi->newNs->href == ns->href) || |
| 8433 | xmlStrEqual(mi->newNs->href, ns->href))) { |
| 8434 | /* Set the mapping. */ |
| 8435 | mi->oldNs = ns; |
| 8436 | *retNs = mi->newNs; |
| 8437 | return (0); |
| 8438 | } |
| 8439 | } |
| 8440 | } |
| 8441 | /* |
| 8442 | * No luck, the namespace is out of scope or shadowed. |
| 8443 | */ |
| 8444 | if (elem == NULL) { |
| 8445 | xmlNsPtr tmpns; |
| 8446 | |
| 8447 | /* |
| 8448 | * Store ns-decls in "oldNs" of the document-node. |
| 8449 | */ |
| 8450 | tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix); |
| 8451 | if (tmpns == NULL) |
| 8452 | return (-1); |
| 8453 | /* |
| 8454 | * Insert mapping. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8455 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8456 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8457 | tmpns, XML_TREE_NSMAP_DOC) == NULL) { |
| 8458 | xmlFreeNs(tmpns); |
| 8459 | return (-1); |
| 8460 | } |
| 8461 | *retNs = tmpns; |
| 8462 | } else { |
| 8463 | xmlNsPtr tmpns; |
| 8464 | |
| 8465 | tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href, |
| 8466 | ns->prefix, 0); |
| 8467 | if (tmpns == NULL) |
| 8468 | return (-1); |
| 8469 | |
| 8470 | if (*nsMap != NULL) { |
| 8471 | /* |
| 8472 | * Does it shadow ancestor ns-decls? |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8473 | */ |
| 8474 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8475 | if ((mi->depth < depth) && |
| 8476 | (mi->shadowDepth == -1) && |
| 8477 | ((ns->prefix == mi->newNs->prefix) || |
| 8478 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8479 | /* |
| 8480 | * Shadows. |
| 8481 | */ |
| 8482 | mi->shadowDepth = depth; |
| 8483 | break; |
| 8484 | } |
| 8485 | } |
| 8486 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8487 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8488 | xmlFreeNs(tmpns); |
| 8489 | return (-1); |
| 8490 | } |
| 8491 | *retNs = tmpns; |
| 8492 | } |
| 8493 | return (0); |
| 8494 | } |
| 8495 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8496 | typedef enum { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8497 | XML_DOM_RECONNS_REMOVEREDUND = 1<<0 |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8498 | } xmlDOMReconcileNSOptions; |
| 8499 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8500 | /* |
| 8501 | * xmlDOMWrapReconcileNamespaces: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8502 | * @ctxt: DOM wrapper context, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8503 | * @elem: the element-node |
| 8504 | * @options: option flags |
| 8505 | * |
| 8506 | * Ensures that ns-references point to ns-decls hold on element-nodes. |
| 8507 | * Ensures that the tree is namespace wellformed by creating additional |
| 8508 | * ns-decls where needed. Note that, since prefixes of already existent |
| 8509 | * ns-decls can be shadowed by this process, it could break QNames in |
| 8510 | * attribute values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8511 | * |
| 8512 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8513 | * |
| 8514 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8515 | */ |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8516 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8517 | int |
| 8518 | xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8519 | xmlNodePtr elem, |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8520 | int options) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8521 | { |
| 8522 | int depth = -1, adoptns = 0, parnsdone = 0; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8523 | xmlNsPtr ns, prevns; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8524 | xmlDocPtr doc; |
| 8525 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8526 | xmlNsMapPtr nsMap = NULL; |
| 8527 | xmlNsMapItemPtr /* topmi = NULL, */ mi; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8528 | /* @ancestorsOnly should be set by an option flag. */ |
| 8529 | int ancestorsOnly = 0; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8530 | int optRemoveRedundantNS = |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8531 | ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0; |
| 8532 | xmlNsPtr *listRedund = NULL; |
| 8533 | int sizeRedund = 0, nbRedund = 0, ret, i, j; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8534 | |
| 8535 | if ((elem == NULL) || (elem->doc == NULL) || |
| 8536 | (elem->type != XML_ELEMENT_NODE)) |
| 8537 | return (-1); |
| 8538 | |
| 8539 | doc = elem->doc; |
| 8540 | cur = elem; |
| 8541 | do { |
| 8542 | switch (cur->type) { |
| 8543 | case XML_ELEMENT_NODE: |
| 8544 | adoptns = 1; |
| 8545 | curElem = cur; |
| 8546 | depth++; |
| 8547 | /* |
| 8548 | * Namespace declarations. |
| 8549 | */ |
| 8550 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8551 | prevns = NULL; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8552 | ns = cur->nsDef; |
| 8553 | while (ns != NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8554 | if (! parnsdone) { |
| 8555 | if ((elem->parent) && |
| 8556 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8557 | /* |
| 8558 | * Gather ancestor in-scope ns-decls. |
| 8559 | */ |
| 8560 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8561 | elem->parent) == -1) |
| 8562 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8563 | } |
| 8564 | parnsdone = 1; |
| 8565 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8566 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8567 | /* |
| 8568 | * Lookup the ns ancestor-axis for equal ns-decls in scope. |
| 8569 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8570 | if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8571 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8572 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8573 | (mi->shadowDepth == -1) && |
| 8574 | ((ns->prefix == mi->newNs->prefix) || |
| 8575 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) && |
| 8576 | ((ns->href == mi->newNs->href) || |
| 8577 | xmlStrEqual(ns->href, mi->newNs->href))) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8578 | { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8579 | /* |
| 8580 | * A redundant ns-decl was found. |
| 8581 | * Add it to the list of redundant ns-decls. |
| 8582 | */ |
| 8583 | if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund, |
| 8584 | &sizeRedund, &nbRedund, ns, mi->newNs) == -1) |
| 8585 | goto internal_error; |
| 8586 | /* |
| 8587 | * Remove the ns-decl from the element-node. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8588 | */ |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8589 | if (prevns) |
| 8590 | prevns->next = ns->next; |
| 8591 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8592 | cur->nsDef = ns->next; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8593 | goto next_ns_decl; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8594 | } |
| 8595 | } |
| 8596 | } |
| 8597 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8598 | /* |
| 8599 | * Skip ns-references handling if the referenced |
| 8600 | * ns-decl is declared on the same element. |
| 8601 | */ |
| 8602 | if ((cur->ns != NULL) && adoptns && (cur->ns == ns)) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8603 | adoptns = 0; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8604 | /* |
| 8605 | * Does it shadow any ns-decl? |
| 8606 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8607 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
| 8608 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8609 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8610 | (mi->shadowDepth == -1) && |
| 8611 | ((ns->prefix == mi->newNs->prefix) || |
| 8612 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8613 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8614 | mi->shadowDepth = depth; |
| 8615 | } |
| 8616 | } |
| 8617 | } |
| 8618 | /* |
| 8619 | * Push mapping. |
| 8620 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8621 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8622 | depth) == NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8623 | goto internal_error; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8624 | |
| 8625 | prevns = ns; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8626 | next_ns_decl: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8627 | ns = ns->next; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8628 | } |
| 8629 | } |
| 8630 | if (! adoptns) |
| 8631 | goto ns_end; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8632 | /* No break on purpose. */ |
| 8633 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8634 | /* No ns, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8635 | if (cur->ns == NULL) |
| 8636 | goto ns_end; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8637 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8638 | if (! parnsdone) { |
| 8639 | if ((elem->parent) && |
| 8640 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8641 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8642 | elem->parent) == -1) |
| 8643 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8644 | } |
| 8645 | parnsdone = 1; |
| 8646 | } |
| 8647 | /* |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8648 | * Adjust the reference if this was a redundant ns-decl. |
| 8649 | */ |
| 8650 | if (listRedund) { |
| 8651 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8652 | if (cur->ns == listRedund[j]) { |
| 8653 | cur->ns = listRedund[++j]; |
| 8654 | break; |
| 8655 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8656 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8657 | } |
| 8658 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8659 | * Adopt ns-references. |
| 8660 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8661 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8662 | /* |
| 8663 | * Search for a mapping. |
| 8664 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8665 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8666 | if ((mi->shadowDepth == -1) && |
| 8667 | (cur->ns == mi->oldNs)) { |
| 8668 | |
| 8669 | cur->ns = mi->newNs; |
| 8670 | goto ns_end; |
| 8671 | } |
| 8672 | } |
| 8673 | } |
| 8674 | /* |
| 8675 | * Aquire a normalized ns-decl and add it to the map. |
| 8676 | */ |
| 8677 | if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem, |
| 8678 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8679 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8680 | ancestorsOnly, |
| 8681 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8682 | goto internal_error; |
| 8683 | cur->ns = ns; |
| 8684 | |
| 8685 | ns_end: |
| 8686 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8687 | (cur->properties != NULL)) { |
| 8688 | /* |
| 8689 | * Process attributes. |
| 8690 | */ |
| 8691 | cur = (xmlNodePtr) cur->properties; |
| 8692 | continue; |
| 8693 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8694 | break; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8695 | default: |
| 8696 | goto next_sibling; |
| 8697 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8698 | into_content: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8699 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8700 | (cur->children != NULL)) { |
| 8701 | /* |
| 8702 | * Process content of element-nodes only. |
| 8703 | */ |
| 8704 | cur = cur->children; |
| 8705 | continue; |
| 8706 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8707 | next_sibling: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8708 | if (cur == elem) |
| 8709 | break; |
| 8710 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8711 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8712 | /* |
| 8713 | * Pop mappings. |
| 8714 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8715 | while ((nsMap->last != NULL) && |
| 8716 | (nsMap->last->depth >= depth)) |
| 8717 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8718 | XML_NSMAP_POP(nsMap, mi) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8719 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8720 | /* |
| 8721 | * Unshadow. |
| 8722 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8723 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8724 | if (mi->shadowDepth >= depth) |
| 8725 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8726 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8727 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8728 | depth--; |
| 8729 | } |
| 8730 | if (cur->next != NULL) |
| 8731 | cur = cur->next; |
| 8732 | else { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8733 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 8734 | cur = cur->parent; |
| 8735 | goto into_content; |
| 8736 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8737 | cur = cur->parent; |
| 8738 | goto next_sibling; |
| 8739 | } |
| 8740 | } while (cur != NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8741 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8742 | ret = 0; |
| 8743 | goto exit; |
| 8744 | internal_error: |
| 8745 | ret = -1; |
| 8746 | exit: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8747 | if (listRedund) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8748 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8749 | xmlFreeNs(listRedund[j]); |
| 8750 | } |
| 8751 | xmlFree(listRedund); |
| 8752 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8753 | if (nsMap != NULL) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8754 | xmlDOMWrapNsMapFree(nsMap); |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8755 | return (ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8756 | } |
| 8757 | |
| 8758 | /* |
| 8759 | * xmlDOMWrapAdoptBranch: |
| 8760 | * @ctxt: the optional context for custom processing |
| 8761 | * @sourceDoc: the optional sourceDoc |
| 8762 | * @node: the element-node to start with |
| 8763 | * @destDoc: the destination doc for adoption |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8764 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8765 | * @options: option flags |
| 8766 | * |
| 8767 | * Ensures that ns-references point to @destDoc: either to |
| 8768 | * elements->nsDef entries if @destParent is given, or to |
| 8769 | * @destDoc->oldNs otherwise. |
| 8770 | * If @destParent is given, it ensures that the tree is namespace |
| 8771 | * wellformed by creating additional ns-decls where needed. |
| 8772 | * Note that, since prefixes of already existent ns-decls can be |
| 8773 | * shadowed by this process, it could break QNames in attribute |
| 8774 | * values or element content. |
| 8775 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8776 | * NOTE: This function was not intensively tested. |
| 8777 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8778 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8779 | */ |
| 8780 | static int |
| 8781 | xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt, |
| 8782 | xmlDocPtr sourceDoc, |
| 8783 | xmlNodePtr node, |
| 8784 | xmlDocPtr destDoc, |
| 8785 | xmlNodePtr destParent, |
| 8786 | int options ATTRIBUTE_UNUSED) |
| 8787 | { |
| 8788 | int ret = 0; |
| 8789 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8790 | xmlNsMapPtr nsMap = NULL; |
| 8791 | xmlNsMapItemPtr mi; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 8792 | xmlNsPtr ns = NULL; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8793 | int depth = -1, adoptStr = 1; |
| 8794 | /* gather @parent's ns-decls. */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8795 | int parnsdone; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8796 | /* @ancestorsOnly should be set per option. */ |
| 8797 | int ancestorsOnly = 0; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8798 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8799 | /* |
| 8800 | * Optimize string adoption for equal or none dicts. |
| 8801 | */ |
| 8802 | if ((sourceDoc != NULL) && |
| 8803 | (sourceDoc->dict == destDoc->dict)) |
| 8804 | adoptStr = 0; |
| 8805 | else |
| 8806 | adoptStr = 1; |
| 8807 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8808 | /* |
| 8809 | * Get the ns-map from the context if available. |
| 8810 | */ |
| 8811 | if (ctxt) |
| 8812 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 8813 | /* |
| 8814 | * Disable search for ns-decls in the parent-axis of the |
| 8815 | * desination element, if: |
| 8816 | * 1) there's no destination parent |
| 8817 | * 2) custom ns-reference handling is used |
| 8818 | */ |
| 8819 | if ((destParent == NULL) || |
| 8820 | (ctxt && ctxt->getNsForNodeFunc)) |
| 8821 | { |
| 8822 | parnsdone = 1; |
| 8823 | } else |
| 8824 | parnsdone = 0; |
| 8825 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8826 | cur = node; |
| 8827 | while (cur != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8828 | /* |
| 8829 | * Paranoid source-doc sanity check. |
| 8830 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8831 | if (cur->doc != sourceDoc) { |
| 8832 | /* |
| 8833 | * We'll assume XIncluded nodes if the doc differs. |
| 8834 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 8835 | * This here skips XIncluded nodes and tries to handle |
| 8836 | * broken sequences. |
| 8837 | */ |
| 8838 | if (cur->next == NULL) |
| 8839 | goto leave_node; |
| 8840 | do { |
| 8841 | cur = cur->next; |
| 8842 | if ((cur->type == XML_XINCLUDE_END) || |
| 8843 | (cur->doc == node->doc)) |
| 8844 | break; |
| 8845 | } while (cur->next != NULL); |
| 8846 | |
| 8847 | if (cur->doc != node->doc) |
| 8848 | goto leave_node; |
| 8849 | } |
| 8850 | cur->doc = destDoc; |
| 8851 | switch (cur->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8852 | case XML_XINCLUDE_START: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8853 | case XML_XINCLUDE_END: |
| 8854 | /* |
| 8855 | * TODO |
| 8856 | */ |
| 8857 | return (-1); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8858 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8859 | curElem = cur; |
| 8860 | depth++; |
| 8861 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8862 | * Namespace declarations. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8863 | * - ns->href and ns->prefix are never in the dict, so |
| 8864 | * we need not move the values over to the destination dict. |
| 8865 | * - Note that for custom handling of ns-references, |
| 8866 | * the ns-decls need not be stored in the ns-map, |
| 8867 | * since they won't be referenced by node->ns. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8868 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8869 | if ((cur->nsDef) && |
| 8870 | ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL))) |
| 8871 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8872 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8873 | /* |
| 8874 | * Gather @parent's in-scope ns-decls. |
| 8875 | */ |
| 8876 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8877 | destParent) == -1) |
| 8878 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8879 | parnsdone = 1; |
| 8880 | } |
| 8881 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8882 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8883 | * NOTE: ns->prefix and ns->href are never in the dict. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8884 | * XML_TREE_ADOPT_STR(ns->prefix) |
| 8885 | * XML_TREE_ADOPT_STR(ns->href) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8886 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8887 | /* |
| 8888 | * Does it shadow any ns-decl? |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8889 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8890 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8891 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8892 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8893 | (mi->shadowDepth == -1) && |
| 8894 | ((ns->prefix == mi->newNs->prefix) || |
| 8895 | xmlStrEqual(ns->prefix, |
| 8896 | mi->newNs->prefix))) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8897 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8898 | mi->shadowDepth = depth; |
| 8899 | } |
| 8900 | } |
| 8901 | } |
| 8902 | /* |
| 8903 | * Push mapping. |
| 8904 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8905 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8906 | ns, ns, depth) == NULL) |
| 8907 | goto internal_error; |
| 8908 | } |
| 8909 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8910 | /* No break on purpose. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8911 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8912 | /* No namespace, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8913 | if (cur->ns == NULL) |
| 8914 | goto ns_end; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8915 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8916 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8917 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8918 | destParent) == -1) |
| 8919 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8920 | parnsdone = 1; |
| 8921 | } |
| 8922 | /* |
| 8923 | * Adopt ns-references. |
| 8924 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8925 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8926 | /* |
| 8927 | * Search for a mapping. |
| 8928 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8929 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8930 | if ((mi->shadowDepth == -1) && |
| 8931 | (cur->ns == mi->oldNs)) { |
| 8932 | |
| 8933 | cur->ns = mi->newNs; |
| 8934 | goto ns_end; |
| 8935 | } |
| 8936 | } |
| 8937 | } |
| 8938 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8939 | * No matching namespace in scope. We need a new one. |
| 8940 | */ |
| 8941 | if ((ctxt) && (ctxt->getNsForNodeFunc)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8942 | /* |
| 8943 | * User-defined behaviour. |
| 8944 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8945 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 8946 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8947 | /* |
| 8948 | * Insert mapping if ns is available; it's the users fault |
| 8949 | * if not. |
| 8950 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8951 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8952 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8953 | goto internal_error; |
| 8954 | cur->ns = ns; |
| 8955 | } else { |
| 8956 | /* |
| 8957 | * Aquire a normalized ns-decl and add it to the map. |
| 8958 | */ |
| 8959 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8960 | /* ns-decls on curElem or on destDoc->oldNs */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8961 | destParent ? curElem : NULL, |
| 8962 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8963 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8964 | ancestorsOnly, |
| 8965 | /* ns-decls must be prefixed for attributes. */ |
| 8966 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8967 | goto internal_error; |
| 8968 | cur->ns = ns; |
| 8969 | } |
| 8970 | ns_end: |
| 8971 | /* |
| 8972 | * Further node properties. |
| 8973 | * TODO: Is this all? |
| 8974 | */ |
| 8975 | XML_TREE_ADOPT_STR(cur->name) |
| 8976 | if (cur->type == XML_ELEMENT_NODE) { |
| 8977 | cur->psvi = NULL; |
| 8978 | cur->line = 0; |
| 8979 | cur->extra = 0; |
| 8980 | /* |
| 8981 | * Walk attributes. |
| 8982 | */ |
| 8983 | if (cur->properties != NULL) { |
| 8984 | /* |
| 8985 | * Process first attribute node. |
| 8986 | */ |
| 8987 | cur = (xmlNodePtr) cur->properties; |
| 8988 | continue; |
| 8989 | } |
| 8990 | } else { |
| 8991 | /* |
| 8992 | * Attributes. |
| 8993 | */ |
| 8994 | if ((sourceDoc != NULL) && |
| 8995 | (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID)) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8996 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8997 | xmlRemoveID(sourceDoc, (xmlAttrPtr) cur); |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8998 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8999 | ((xmlAttrPtr) cur)->atype = 0; |
| 9000 | ((xmlAttrPtr) cur)->psvi = NULL; |
| 9001 | } |
| 9002 | break; |
| 9003 | case XML_TEXT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9004 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9005 | /* |
| 9006 | * This puts the content in the dest dict, only if |
| 9007 | * it was previously in the source dict. |
| 9008 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9009 | XML_TREE_ADOPT_STR_2(cur->content) |
| 9010 | goto leave_node; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9011 | case XML_ENTITY_REF_NODE: |
| 9012 | /* |
| 9013 | * Remove reference to the entitity-node. |
| 9014 | */ |
| 9015 | cur->content = NULL; |
| 9016 | cur->children = NULL; |
| 9017 | cur->last = NULL; |
| 9018 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9019 | xmlEntityPtr ent; |
| 9020 | /* |
| 9021 | * Assign new entity-node if available. |
| 9022 | */ |
| 9023 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9024 | if (ent != NULL) { |
| 9025 | cur->content = ent->content; |
| 9026 | cur->children = (xmlNodePtr) ent; |
| 9027 | cur->last = (xmlNodePtr) ent; |
| 9028 | } |
| 9029 | } |
| 9030 | goto leave_node; |
| 9031 | case XML_PI_NODE: |
| 9032 | XML_TREE_ADOPT_STR(cur->name) |
| 9033 | XML_TREE_ADOPT_STR_2(cur->content) |
| 9034 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9035 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9036 | break; |
| 9037 | default: |
| 9038 | goto internal_error; |
| 9039 | } |
| 9040 | /* |
| 9041 | * Walk the tree. |
| 9042 | */ |
| 9043 | if (cur->children != NULL) { |
| 9044 | cur = cur->children; |
| 9045 | continue; |
| 9046 | } |
| 9047 | |
| 9048 | leave_node: |
| 9049 | if (cur == node) |
| 9050 | break; |
| 9051 | if ((cur->type == XML_ELEMENT_NODE) || |
| 9052 | (cur->type == XML_XINCLUDE_START) || |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9053 | (cur->type == XML_XINCLUDE_END)) |
| 9054 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9055 | /* |
| 9056 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 9057 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9058 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9059 | /* |
| 9060 | * Pop mappings. |
| 9061 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9062 | while ((nsMap->last != NULL) && |
| 9063 | (nsMap->last->depth >= depth)) |
| 9064 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9065 | XML_NSMAP_POP(nsMap, mi) |
| 9066 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9067 | /* |
| 9068 | * Unshadow. |
| 9069 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9070 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9071 | if (mi->shadowDepth >= depth) |
| 9072 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9073 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9074 | } |
| 9075 | depth--; |
| 9076 | } |
| 9077 | if (cur->next != NULL) |
| 9078 | cur = cur->next; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9079 | else if ((cur->type == XML_ATTRIBUTE_NODE) && |
| 9080 | (cur->parent->children != NULL)) |
| 9081 | { |
| 9082 | cur = cur->parent->children; |
| 9083 | } else { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9084 | cur = cur->parent; |
| 9085 | goto leave_node; |
| 9086 | } |
| 9087 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9088 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9089 | goto exit; |
| 9090 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9091 | internal_error: |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9092 | ret = -1; |
| 9093 | |
| 9094 | exit: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9095 | /* |
| 9096 | * Cleanup. |
| 9097 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9098 | if (nsMap != NULL) { |
| 9099 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 9100 | /* |
| 9101 | * Just cleanup the map but don't free. |
| 9102 | */ |
| 9103 | if (nsMap->first) { |
| 9104 | if (nsMap->pool) |
| 9105 | nsMap->last->next = nsMap->pool; |
| 9106 | nsMap->pool = nsMap->first; |
| 9107 | nsMap->first = NULL; |
| 9108 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9109 | } else |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9110 | xmlDOMWrapNsMapFree(nsMap); |
| 9111 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9112 | return(ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9113 | } |
| 9114 | |
| 9115 | /* |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9116 | * xmlDOMWrapCloneNode: |
| 9117 | * @ctxt: the optional context for custom processing |
| 9118 | * @sourceDoc: the optional sourceDoc |
| 9119 | * @node: the node to start with |
| 9120 | * @resNode: the clone of the given @node |
| 9121 | * @destDoc: the destination doc |
| 9122 | * @destParent: the optional new parent of @node in @destDoc |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 9123 | * @deep: descend into child if set |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9124 | * @options: option flags |
| 9125 | * |
| 9126 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 9127 | * 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] | 9128 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used. |
| 9129 | * This is the case when you don't know already where the cloned branch |
| 9130 | * will be added to. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9131 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9132 | * If @destParent is given, it ensures that the tree is namespace |
| 9133 | * wellformed by creating additional ns-decls where needed. |
| 9134 | * Note that, since prefixes of already existent ns-decls can be |
| 9135 | * shadowed by this process, it could break QNames in attribute |
| 9136 | * values or element content. |
| 9137 | * TODO: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9138 | * 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] | 9139 | * |
| 9140 | * Returns 0 if the operation succeeded, |
| 9141 | * 1 if a node of unsupported (or not yet supported) type was given, |
| 9142 | * -1 on API/internal errors. |
| 9143 | */ |
| 9144 | |
| 9145 | int |
| 9146 | xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt, |
| 9147 | xmlDocPtr sourceDoc, |
| 9148 | xmlNodePtr node, |
| 9149 | xmlNodePtr *resNode, |
| 9150 | xmlDocPtr destDoc, |
| 9151 | xmlNodePtr destParent, |
| 9152 | int deep, |
| 9153 | int options ATTRIBUTE_UNUSED) |
| 9154 | { |
| 9155 | int ret = 0; |
| 9156 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9157 | xmlNsMapPtr nsMap = NULL; |
| 9158 | xmlNsMapItemPtr mi; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9159 | xmlNsPtr ns; |
| 9160 | int depth = -1; |
| 9161 | /* int adoptStr = 1; */ |
| 9162 | /* gather @parent's ns-decls. */ |
| 9163 | int parnsdone = 0; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9164 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9165 | * @ancestorsOnly: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9166 | * TODO: @ancestorsOnly should be set per option. |
| 9167 | * |
| 9168 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9169 | int ancestorsOnly = 0; |
| 9170 | xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9171 | xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL; |
| 9172 | xmlDictPtr dict; /* The destination dict */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9173 | |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9174 | if ((node == NULL) || (resNode == NULL) || (destDoc == NULL)) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9175 | return(-1); |
| 9176 | /* |
| 9177 | * TODO: Initially we support only element-nodes. |
| 9178 | */ |
| 9179 | if (node->type != XML_ELEMENT_NODE) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9180 | return(1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9181 | /* |
| 9182 | * Check node->doc sanity. |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9183 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9184 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 9185 | (node->doc != sourceDoc)) { |
| 9186 | /* |
| 9187 | * Might be an XIncluded node. |
| 9188 | */ |
| 9189 | return (-1); |
| 9190 | } |
| 9191 | if (sourceDoc == NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9192 | sourceDoc = node->doc; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9193 | if (sourceDoc == NULL) |
| 9194 | return (-1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9195 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9196 | dict = destDoc->dict; |
| 9197 | /* |
| 9198 | * Reuse the namespace map of the context. |
| 9199 | */ |
| 9200 | if (ctxt) |
| 9201 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 9202 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9203 | *resNode = NULL; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9204 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9205 | cur = node; |
| 9206 | while (cur != NULL) { |
| 9207 | if (cur->doc != sourceDoc) { |
| 9208 | /* |
| 9209 | * We'll assume XIncluded nodes if the doc differs. |
| 9210 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 9211 | * TODO: This here returns -1 in this case. |
| 9212 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9213 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9214 | } |
| 9215 | /* |
| 9216 | * Create a new node. |
| 9217 | */ |
| 9218 | switch (cur->type) { |
| 9219 | case XML_XINCLUDE_START: |
| 9220 | case XML_XINCLUDE_END: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9221 | /* |
| 9222 | * TODO: What to do with XInclude? |
| 9223 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9224 | goto internal_error; |
| 9225 | break; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9226 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9227 | case XML_TEXT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9228 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9229 | case XML_COMMENT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9230 | case XML_PI_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9231 | case XML_DOCUMENT_FRAG_NODE: |
| 9232 | case XML_ENTITY_REF_NODE: |
| 9233 | case XML_ENTITY_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9234 | /* |
| 9235 | * Nodes of xmlNode structure. |
| 9236 | */ |
| 9237 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 9238 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9239 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9240 | goto internal_error; |
| 9241 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9242 | memset(clone, 0, sizeof(xmlNode)); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9243 | /* |
| 9244 | * Set hierachical links. |
| 9245 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9246 | if (resultClone != NULL) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9247 | clone->parent = parentClone; |
| 9248 | if (prevClone) { |
| 9249 | prevClone->next = clone; |
| 9250 | clone->prev = prevClone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9251 | } else |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9252 | parentClone->children = clone; |
| 9253 | } else |
| 9254 | resultClone = clone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9255 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9256 | break; |
| 9257 | case XML_ATTRIBUTE_NODE: |
| 9258 | /* |
| 9259 | * Attributes (xmlAttr). |
| 9260 | */ |
| 9261 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr)); |
| 9262 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9263 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9264 | goto internal_error; |
| 9265 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9266 | memset(clone, 0, sizeof(xmlAttr)); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9267 | /* |
| 9268 | * Set hierachical links. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9269 | * TODO: Change this to add to the end of attributes. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9270 | */ |
| 9271 | if (resultClone != NULL) { |
| 9272 | clone->parent = parentClone; |
| 9273 | if (prevClone) { |
| 9274 | prevClone->next = clone; |
| 9275 | clone->prev = prevClone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9276 | } else |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9277 | parentClone->properties = (xmlAttrPtr) clone; |
| 9278 | } else |
| 9279 | resultClone = clone; |
| 9280 | break; |
| 9281 | default: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9282 | /* |
| 9283 | * TODO QUESTION: Any other nodes expected? |
| 9284 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9285 | goto internal_error; |
| 9286 | } |
| 9287 | |
| 9288 | clone->type = cur->type; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9289 | clone->doc = destDoc; |
| 9290 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9291 | /* |
| 9292 | * Clone the name of the node if any. |
| 9293 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9294 | if (cur->name == xmlStringText) |
| 9295 | clone->name = xmlStringText; |
| 9296 | else if (cur->name == xmlStringTextNoenc) |
| 9297 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9298 | * NOTE: Although xmlStringTextNoenc is never assigned to a node |
| 9299 | * in tree.c, it might be set in Libxslt via |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9300 | * "xsl:disable-output-escaping". |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9301 | */ |
| 9302 | clone->name = xmlStringTextNoenc; |
| 9303 | else if (cur->name == xmlStringComment) |
| 9304 | clone->name = xmlStringComment; |
| 9305 | else if (cur->name != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9306 | DICT_CONST_COPY(cur->name, clone->name); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9307 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9308 | |
| 9309 | switch (cur->type) { |
| 9310 | case XML_XINCLUDE_START: |
| 9311 | case XML_XINCLUDE_END: |
| 9312 | /* |
| 9313 | * TODO |
| 9314 | */ |
| 9315 | return (-1); |
| 9316 | case XML_ELEMENT_NODE: |
| 9317 | curElem = cur; |
| 9318 | depth++; |
| 9319 | /* |
| 9320 | * Namespace declarations. |
| 9321 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9322 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9323 | if (! parnsdone) { |
| 9324 | if (destParent && (ctxt == NULL)) { |
| 9325 | /* |
| 9326 | * Gather @parent's in-scope ns-decls. |
| 9327 | */ |
| 9328 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 9329 | destParent) == -1) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9330 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9331 | } |
| 9332 | parnsdone = 1; |
| 9333 | } |
| 9334 | /* |
| 9335 | * Clone namespace declarations. |
| 9336 | */ |
| 9337 | cloneNsDefSlot = &(clone->nsDef); |
| 9338 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 9339 | /* |
| 9340 | * Create a new xmlNs. |
| 9341 | */ |
| 9342 | cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 9343 | if (cloneNs == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9344 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): " |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9345 | "allocating namespace"); |
| 9346 | return(-1); |
| 9347 | } |
| 9348 | memset(cloneNs, 0, sizeof(xmlNs)); |
| 9349 | cloneNs->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9350 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9351 | if (ns->href != NULL) |
| 9352 | cloneNs->href = xmlStrdup(ns->href); |
| 9353 | if (ns->prefix != NULL) |
| 9354 | cloneNs->prefix = xmlStrdup(ns->prefix); |
| 9355 | |
| 9356 | *cloneNsDefSlot = cloneNs; |
| 9357 | cloneNsDefSlot = &(cloneNs->next); |
| 9358 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9359 | /* |
| 9360 | * Note that for custom handling of ns-references, |
| 9361 | * the ns-decls need not be stored in the ns-map, |
| 9362 | * since they won't be referenced by node->ns. |
| 9363 | */ |
| 9364 | if ((ctxt == NULL) || |
| 9365 | (ctxt->getNsForNodeFunc == NULL)) |
| 9366 | { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9367 | /* |
| 9368 | * Does it shadow any ns-decl? |
| 9369 | */ |
| 9370 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9371 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9372 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 9373 | (mi->shadowDepth == -1) && |
| 9374 | ((ns->prefix == mi->newNs->prefix) || |
| 9375 | xmlStrEqual(ns->prefix, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9376 | mi->newNs->prefix))) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9377 | /* |
| 9378 | * Mark as shadowed at the current |
| 9379 | * depth. |
| 9380 | */ |
| 9381 | mi->shadowDepth = depth; |
| 9382 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9383 | } |
| 9384 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9385 | /* |
| 9386 | * Push mapping. |
| 9387 | */ |
| 9388 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
| 9389 | ns, cloneNs, depth) == NULL) |
| 9390 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9391 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9392 | } |
| 9393 | } |
| 9394 | /* cur->ns will be processed further down. */ |
| 9395 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9396 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9397 | /* IDs will be processed further down. */ |
| 9398 | /* cur->ns will be processed further down. */ |
| 9399 | break; |
| 9400 | case XML_TEXT_NODE: |
| 9401 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9402 | /* |
| 9403 | * Note that this will also cover the values of attributes. |
| 9404 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9405 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9406 | goto leave_node; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9407 | case XML_ENTITY_NODE: |
| 9408 | /* TODO: What to do here? */ |
| 9409 | goto leave_node; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9410 | case XML_ENTITY_REF_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9411 | if (sourceDoc != destDoc) { |
| 9412 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9413 | xmlEntityPtr ent; |
| 9414 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9415 | * Different doc: Assign new entity-node if available. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9416 | */ |
| 9417 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9418 | if (ent != NULL) { |
| 9419 | clone->content = ent->content; |
| 9420 | clone->children = (xmlNodePtr) ent; |
| 9421 | clone->last = (xmlNodePtr) ent; |
| 9422 | } |
| 9423 | } |
| 9424 | } else { |
| 9425 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9426 | * Same doc: Use the current node's entity declaration |
| 9427 | * and value. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9428 | */ |
| 9429 | clone->content = cur->content; |
| 9430 | clone->children = cur->children; |
| 9431 | clone->last = cur->last; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9432 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9433 | goto leave_node; |
| 9434 | case XML_PI_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9435 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9436 | goto leave_node; |
| 9437 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9438 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9439 | goto leave_node; |
| 9440 | default: |
| 9441 | goto internal_error; |
| 9442 | } |
| 9443 | |
| 9444 | if (cur->ns == NULL) |
| 9445 | goto end_ns_reference; |
| 9446 | |
| 9447 | /* handle_ns_reference: */ |
| 9448 | /* |
| 9449 | ** The following will take care of references to ns-decls ******** |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9450 | ** and is intended only for element- and attribute-nodes. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9451 | ** |
| 9452 | */ |
| 9453 | if (! parnsdone) { |
| 9454 | if (destParent && (ctxt == NULL)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9455 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9456 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9457 | } |
| 9458 | parnsdone = 1; |
| 9459 | } |
| 9460 | /* |
| 9461 | * Adopt ns-references. |
| 9462 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9463 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9464 | /* |
| 9465 | * Search for a mapping. |
| 9466 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9467 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9468 | if ((mi->shadowDepth == -1) && |
| 9469 | (cur->ns == mi->oldNs)) { |
| 9470 | /* |
| 9471 | * This is the nice case: a mapping was found. |
| 9472 | */ |
| 9473 | clone->ns = mi->newNs; |
| 9474 | goto end_ns_reference; |
| 9475 | } |
| 9476 | } |
| 9477 | } |
| 9478 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9479 | * No matching namespace in scope. We need a new one. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9480 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9481 | if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9482 | /* |
| 9483 | * User-defined behaviour. |
| 9484 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9485 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 9486 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9487 | /* |
| 9488 | * Add user's mapping. |
| 9489 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9490 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9491 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
| 9492 | goto internal_error; |
| 9493 | clone->ns = ns; |
| 9494 | } else { |
| 9495 | /* |
| 9496 | * Aquire a normalized ns-decl and add it to the map. |
| 9497 | */ |
| 9498 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9499 | /* ns-decls on curElem or on destDoc->oldNs */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9500 | destParent ? curElem : NULL, |
| 9501 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9502 | &nsMap, depth, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9503 | /* if we need to search only in the ancestor-axis */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9504 | ancestorsOnly, |
| 9505 | /* ns-decls must be prefixed for attributes. */ |
| 9506 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 9507 | goto internal_error; |
| 9508 | clone->ns = ns; |
| 9509 | } |
| 9510 | |
| 9511 | end_ns_reference: |
| 9512 | |
| 9513 | /* |
| 9514 | * Some post-processing. |
| 9515 | * |
| 9516 | * Handle ID attributes. |
| 9517 | */ |
| 9518 | if ((clone->type == XML_ATTRIBUTE_NODE) && |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9519 | (clone->parent != NULL)) |
| 9520 | { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9521 | if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9522 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9523 | xmlChar *idVal; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9524 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9525 | idVal = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 9526 | if (idVal != NULL) { |
| 9527 | if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) { |
| 9528 | /* TODO: error message. */ |
| 9529 | xmlFree(idVal); |
| 9530 | goto internal_error; |
| 9531 | } |
| 9532 | xmlFree(idVal); |
| 9533 | } |
| 9534 | } |
| 9535 | } |
| 9536 | /* |
| 9537 | ** |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9538 | ** The following will traverse the tree ************************** |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9539 | ** |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9540 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9541 | * Walk the element's attributes before descending into child-nodes. |
| 9542 | */ |
| 9543 | if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) { |
| 9544 | prevClone = NULL; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9545 | parentClone = clone; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9546 | cur = (xmlNodePtr) cur->properties; |
| 9547 | continue; |
| 9548 | } |
| 9549 | into_content: |
| 9550 | /* |
| 9551 | * Descend into child-nodes. |
| 9552 | */ |
| 9553 | if (cur->children != NULL) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9554 | if (deep || (cur->type == XML_ATTRIBUTE_NODE)) { |
| 9555 | prevClone = NULL; |
| 9556 | parentClone = clone; |
| 9557 | cur = cur->children; |
| 9558 | continue; |
| 9559 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9560 | } |
| 9561 | |
| 9562 | leave_node: |
| 9563 | /* |
| 9564 | * At this point we are done with the node, its content |
| 9565 | * and an element-nodes's attribute-nodes. |
| 9566 | */ |
| 9567 | if (cur == node) |
| 9568 | break; |
| 9569 | if ((cur->type == XML_ELEMENT_NODE) || |
| 9570 | (cur->type == XML_XINCLUDE_START) || |
| 9571 | (cur->type == XML_XINCLUDE_END)) { |
| 9572 | /* |
| 9573 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 9574 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9575 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9576 | /* |
| 9577 | * Pop mappings. |
| 9578 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9579 | while ((nsMap->last != NULL) && |
| 9580 | (nsMap->last->depth >= depth)) |
| 9581 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9582 | XML_NSMAP_POP(nsMap, mi) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9583 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9584 | /* |
| 9585 | * Unshadow. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9586 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9587 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9588 | if (mi->shadowDepth >= depth) |
| 9589 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9590 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9591 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9592 | depth--; |
| 9593 | } |
| 9594 | if (cur->next != NULL) { |
| 9595 | prevClone = clone; |
| 9596 | cur = cur->next; |
| 9597 | } else if (cur->type != XML_ATTRIBUTE_NODE) { |
| 9598 | /* |
| 9599 | * Set clone->last. |
| 9600 | */ |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9601 | if (clone->parent != NULL) |
| 9602 | clone->parent->last = clone; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9603 | clone = clone->parent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9604 | parentClone = clone->parent; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9605 | /* |
| 9606 | * Process parent --> next; |
| 9607 | */ |
| 9608 | cur = cur->parent; |
| 9609 | goto leave_node; |
| 9610 | } else { |
| 9611 | /* This is for attributes only. */ |
| 9612 | clone = clone->parent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9613 | parentClone = clone->parent; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9614 | /* |
| 9615 | * Process parent-element --> children. |
| 9616 | */ |
| 9617 | cur = cur->parent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9618 | goto into_content; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9619 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9620 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9621 | goto exit; |
| 9622 | |
| 9623 | internal_error: |
| 9624 | ret = -1; |
| 9625 | |
| 9626 | exit: |
| 9627 | /* |
| 9628 | * Cleanup. |
| 9629 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9630 | if (nsMap != NULL) { |
| 9631 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 9632 | /* |
| 9633 | * Just cleanup the map but don't free. |
| 9634 | */ |
| 9635 | if (nsMap->first) { |
| 9636 | if (nsMap->pool) |
| 9637 | nsMap->last->next = nsMap->pool; |
| 9638 | nsMap->pool = nsMap->first; |
| 9639 | nsMap->first = NULL; |
| 9640 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9641 | } else |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9642 | xmlDOMWrapNsMapFree(nsMap); |
| 9643 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9644 | /* |
| 9645 | * TODO: Should we try a cleanup of the cloned node in case of a |
| 9646 | * fatal error? |
| 9647 | */ |
| 9648 | *resNode = resultClone; |
| 9649 | return (ret); |
| 9650 | } |
| 9651 | |
| 9652 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9653 | * xmlDOMWrapAdoptAttr: |
| 9654 | * @ctxt: the optional context for custom processing |
| 9655 | * @sourceDoc: the optional source document of attr |
| 9656 | * @attr: the attribute-node to be adopted |
| 9657 | * @destDoc: the destination doc for adoption |
| 9658 | * @destParent: the optional new parent of @attr in @destDoc |
| 9659 | * @options: option flags |
| 9660 | * |
| 9661 | * @attr is adopted by @destDoc. |
| 9662 | * Ensures that ns-references point to @destDoc: either to |
| 9663 | * elements->nsDef entries if @destParent is given, or to |
| 9664 | * @destDoc->oldNs otherwise. |
| 9665 | * |
| 9666 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 9667 | */ |
| 9668 | static int |
| 9669 | xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt, |
| 9670 | xmlDocPtr sourceDoc, |
| 9671 | xmlAttrPtr attr, |
| 9672 | xmlDocPtr destDoc, |
| 9673 | xmlNodePtr destParent, |
| 9674 | int options ATTRIBUTE_UNUSED) |
| 9675 | { |
| 9676 | xmlNodePtr cur; |
| 9677 | int adoptStr = 1; |
| 9678 | |
| 9679 | if ((attr == NULL) || (destDoc == NULL)) |
| 9680 | return (-1); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9681 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9682 | attr->doc = destDoc; |
| 9683 | if (attr->ns != NULL) { |
| 9684 | xmlNsPtr ns = NULL; |
| 9685 | |
| 9686 | if (ctxt != NULL) { |
| 9687 | /* TODO: User defined. */ |
| 9688 | } |
| 9689 | /* XML Namespace. */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9690 | if (IS_STR_XML(attr->ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9691 | ns = xmlTreeEnsureXMLDecl(destDoc); |
| 9692 | } else if (destParent == NULL) { |
| 9693 | /* |
| 9694 | * Store in @destDoc->oldNs. |
| 9695 | */ |
| 9696 | ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix); |
| 9697 | } else { |
| 9698 | /* |
| 9699 | * Declare on @destParent. |
| 9700 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9701 | if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9702 | &ns, 1) == -1) |
| 9703 | goto internal_error; |
| 9704 | if (ns == NULL) { |
| 9705 | ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent, |
| 9706 | attr->ns->href, attr->ns->prefix, 1); |
| 9707 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9708 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9709 | if (ns == NULL) |
| 9710 | goto internal_error; |
| 9711 | attr->ns = ns; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9712 | } |
| 9713 | |
| 9714 | XML_TREE_ADOPT_STR(attr->name); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9715 | attr->atype = 0; |
| 9716 | attr->psvi = NULL; |
| 9717 | /* |
| 9718 | * Walk content. |
| 9719 | */ |
| 9720 | if (attr->children == NULL) |
| 9721 | return (0); |
| 9722 | cur = attr->children; |
| 9723 | while (cur != NULL) { |
| 9724 | cur->doc = destDoc; |
| 9725 | switch (cur->type) { |
| 9726 | case XML_TEXT_NODE: |
| 9727 | case XML_CDATA_SECTION_NODE: |
| 9728 | XML_TREE_ADOPT_STR_2(cur->content) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9729 | break; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9730 | case XML_ENTITY_REF_NODE: |
| 9731 | /* |
| 9732 | * Remove reference to the entitity-node. |
| 9733 | */ |
| 9734 | cur->content = NULL; |
| 9735 | cur->children = NULL; |
| 9736 | cur->last = NULL; |
| 9737 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9738 | xmlEntityPtr ent; |
| 9739 | /* |
| 9740 | * Assign new entity-node if available. |
| 9741 | */ |
| 9742 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9743 | if (ent != NULL) { |
| 9744 | cur->content = ent->content; |
| 9745 | cur->children = (xmlNodePtr) ent; |
| 9746 | cur->last = (xmlNodePtr) ent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9747 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9748 | } |
| 9749 | break; |
| 9750 | default: |
| 9751 | break; |
| 9752 | } |
| 9753 | if (cur->children != NULL) { |
| 9754 | cur = cur->children; |
| 9755 | continue; |
| 9756 | } |
| 9757 | next_sibling: |
| 9758 | if (cur == (xmlNodePtr) attr) |
| 9759 | break; |
| 9760 | if (cur->next != NULL) |
| 9761 | cur = cur->next; |
| 9762 | else { |
| 9763 | cur = cur->parent; |
| 9764 | goto next_sibling; |
| 9765 | } |
| 9766 | } |
| 9767 | return (0); |
| 9768 | internal_error: |
| 9769 | return (-1); |
| 9770 | } |
| 9771 | |
| 9772 | /* |
| 9773 | * xmlDOMWrapAdoptNode: |
| 9774 | * @ctxt: the optional context for custom processing |
| 9775 | * @sourceDoc: the optional sourceDoc |
| 9776 | * @node: the node to start with |
| 9777 | * @destDoc: the destination doc |
Kasimier T. Buchcik | 4d9c948 | 2005-06-27 15:04:46 +0000 | [diff] [blame] | 9778 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9779 | * @options: option flags |
| 9780 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9781 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 9782 | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
| 9783 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used |
| 9784 | * 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] | 9785 | * to the context of |
| 9786 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9787 | * If @destParent is given, it ensures that the tree is namespace |
| 9788 | * wellformed by creating additional ns-decls where needed. |
| 9789 | * Note that, since prefixes of already existent ns-decls can be |
| 9790 | * shadowed by this process, it could break QNames in attribute |
| 9791 | * values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9792 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9793 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9794 | * Returns 0 if the operation succeeded, |
| 9795 | * 1 if a node of unsupported type was given, |
| 9796 | * 2 if a node of not yet supported type was given and |
| 9797 | * -1 on API/internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9798 | */ |
| 9799 | int |
| 9800 | xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt, |
| 9801 | xmlDocPtr sourceDoc, |
| 9802 | xmlNodePtr node, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9803 | xmlDocPtr destDoc, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9804 | xmlNodePtr destParent, |
| 9805 | int options) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9806 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9807 | if ((node == NULL) || (destDoc == NULL) || |
| 9808 | ((destParent != NULL) && (destParent->doc != destDoc))) |
| 9809 | return(-1); |
| 9810 | /* |
| 9811 | * Check node->doc sanity. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9812 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9813 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 9814 | (node->doc != sourceDoc)) { |
| 9815 | /* |
| 9816 | * Might be an XIncluded node. |
| 9817 | */ |
| 9818 | return (-1); |
| 9819 | } |
| 9820 | if (sourceDoc == NULL) |
| 9821 | sourceDoc = node->doc; |
| 9822 | if (sourceDoc == destDoc) |
| 9823 | return (-1); |
| 9824 | switch (node->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9825 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9826 | case XML_ATTRIBUTE_NODE: |
| 9827 | case XML_TEXT_NODE: |
| 9828 | case XML_CDATA_SECTION_NODE: |
| 9829 | case XML_ENTITY_REF_NODE: |
| 9830 | case XML_PI_NODE: |
| 9831 | case XML_COMMENT_NODE: |
| 9832 | break; |
| 9833 | case XML_DOCUMENT_FRAG_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9834 | /* TODO: Support document-fragment-nodes. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9835 | return (2); |
| 9836 | default: |
| 9837 | return (1); |
| 9838 | } |
| 9839 | /* |
| 9840 | * Unlink only if @node was not already added to @destParent. |
| 9841 | */ |
| 9842 | if ((node->parent != NULL) && (destParent != node->parent)) |
| 9843 | xmlUnlinkNode(node); |
| 9844 | |
| 9845 | if (node->type == XML_ELEMENT_NODE) { |
| 9846 | return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node, |
| 9847 | destDoc, destParent, options)); |
| 9848 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
| 9849 | return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc, |
| 9850 | (xmlAttrPtr) node, destDoc, destParent, options)); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9851 | } else { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9852 | xmlNodePtr cur = node; |
| 9853 | int adoptStr = 1; |
| 9854 | |
| 9855 | cur->doc = destDoc; |
| 9856 | /* |
| 9857 | * Optimize string adoption. |
| 9858 | */ |
| 9859 | if ((sourceDoc != NULL) && |
| 9860 | (sourceDoc->dict == destDoc->dict)) |
| 9861 | adoptStr = 0; |
| 9862 | switch (node->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9863 | case XML_TEXT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9864 | case XML_CDATA_SECTION_NODE: |
| 9865 | XML_TREE_ADOPT_STR_2(node->content) |
| 9866 | break; |
| 9867 | case XML_ENTITY_REF_NODE: |
| 9868 | /* |
| 9869 | * Remove reference to the entitity-node. |
| 9870 | */ |
| 9871 | node->content = NULL; |
| 9872 | node->children = NULL; |
| 9873 | node->last = NULL; |
| 9874 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9875 | xmlEntityPtr ent; |
| 9876 | /* |
| 9877 | * Assign new entity-node if available. |
| 9878 | */ |
| 9879 | ent = xmlGetDocEntity(destDoc, node->name); |
| 9880 | if (ent != NULL) { |
| 9881 | node->content = ent->content; |
| 9882 | node->children = (xmlNodePtr) ent; |
| 9883 | node->last = (xmlNodePtr) ent; |
| 9884 | } |
| 9885 | } |
| 9886 | XML_TREE_ADOPT_STR(node->name) |
| 9887 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 9888 | case XML_PI_NODE: { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9889 | XML_TREE_ADOPT_STR(node->name) |
| 9890 | XML_TREE_ADOPT_STR_2(node->content) |
| 9891 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 9892 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9893 | default: |
| 9894 | break; |
| 9895 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9896 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9897 | return (0); |
| 9898 | } |
| 9899 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 9900 | #define bottom_tree |
| 9901 | #include "elfgcchack.h" |