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 | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 44 | #include "buf.h" |
Daniel Veillard | 7d4c529 | 2012-09-05 11:45:32 +0800 | [diff] [blame] | 45 | #include "save.h" |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 46 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 47 | int __xmlRegisterCallbacks = 0; |
| 48 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 49 | /************************************************************************ |
| 50 | * * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 51 | * Forward declarations * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 52 | * * |
| 53 | ************************************************************************/ |
| 54 | |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 55 | static xmlNsPtr |
| 56 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 57 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 58 | static xmlChar* xmlGetPropNodeValueInternal(xmlAttrPtr prop); |
| 59 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 60 | /************************************************************************ |
| 61 | * * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 62 | * Tree memory error handler * |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 63 | * * |
| 64 | ************************************************************************/ |
| 65 | /** |
| 66 | * xmlTreeErrMemory: |
| 67 | * @extra: extra informations |
| 68 | * |
| 69 | * Handle an out of memory condition |
| 70 | */ |
| 71 | static void |
| 72 | xmlTreeErrMemory(const char *extra) |
| 73 | { |
| 74 | __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * xmlTreeErr: |
| 79 | * @code: the error number |
| 80 | * @extra: extra informations |
| 81 | * |
| 82 | * Handle an out of memory condition |
| 83 | */ |
| 84 | static void |
| 85 | xmlTreeErr(int code, xmlNodePtr node, const char *extra) |
| 86 | { |
| 87 | const char *msg = NULL; |
| 88 | |
| 89 | switch(code) { |
| 90 | case XML_TREE_INVALID_HEX: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 91 | msg = "invalid hexadecimal character value\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 92 | break; |
| 93 | case XML_TREE_INVALID_DEC: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 94 | msg = "invalid decimal character value\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 95 | break; |
| 96 | case XML_TREE_UNTERMINATED_ENTITY: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 97 | msg = "unterminated entity reference %15s\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 98 | break; |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame] | 99 | case XML_TREE_NOT_UTF8: |
| 100 | msg = "string is not in UTF-8\n"; |
| 101 | break; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 102 | default: |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 103 | msg = "unexpected error number\n"; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 104 | } |
| 105 | __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra); |
| 106 | } |
| 107 | |
| 108 | /************************************************************************ |
| 109 | * * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 110 | * A few static variables and macros * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 111 | * * |
| 112 | ************************************************************************/ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 113 | /* #undef xmlStringText */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 114 | const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 115 | /* #undef xmlStringTextNoenc */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 116 | const xmlChar xmlStringTextNoenc[] = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 117 | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 118 | /* #undef xmlStringComment */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 119 | const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
| 120 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 121 | static int xmlCompressMode = 0; |
| 122 | static int xmlCheckDTD = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 123 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 124 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
| 125 | xmlNodePtr ulccur = (n)->children; \ |
| 126 | if (ulccur == NULL) { \ |
| 127 | (n)->last = NULL; \ |
| 128 | } else { \ |
| 129 | while (ulccur->next != NULL) { \ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 130 | ulccur->parent = (n); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 131 | ulccur = ulccur->next; \ |
| 132 | } \ |
| 133 | ulccur->parent = (n); \ |
| 134 | (n)->last = ulccur; \ |
| 135 | }} |
| 136 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 137 | #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \ |
| 138 | (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0)) |
| 139 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 140 | /* #define DEBUG_BUFFER */ |
| 141 | /* #define DEBUG_TREE */ |
| 142 | |
| 143 | /************************************************************************ |
| 144 | * * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 145 | * Functions to move to entities.c once the * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 146 | * API freeze is smoothen and they can be made public. * |
| 147 | * * |
| 148 | ************************************************************************/ |
| 149 | #include <libxml/hash.h> |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 150 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 151 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 152 | /** |
| 153 | * xmlGetEntityFromDtd: |
| 154 | * @dtd: A pointer to the DTD to search |
| 155 | * @name: The entity name |
| 156 | * |
| 157 | * Do an entity lookup in the DTD entity hash table and |
| 158 | * return the corresponding entity, if found. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 159 | * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 160 | * Returns A pointer to the entity structure or NULL if not found. |
| 161 | */ |
| 162 | static xmlEntityPtr |
| 163 | xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 164 | xmlEntitiesTablePtr table; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 165 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 166 | if((dtd != NULL) && (dtd->entities != NULL)) { |
| 167 | table = (xmlEntitiesTablePtr) dtd->entities; |
| 168 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 169 | /* return(xmlGetEntityFromTable(table, name)); */ |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 170 | } |
| 171 | return(NULL); |
| 172 | } |
| 173 | /** |
| 174 | * xmlGetParameterEntityFromDtd: |
| 175 | * @dtd: A pointer to the DTD to search |
| 176 | * @name: The entity name |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 177 | * |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 178 | * Do an entity lookup in the DTD pararmeter entity hash table and |
| 179 | * return the corresponding entity, if found. |
| 180 | * |
| 181 | * Returns A pointer to the entity structure or NULL if not found. |
| 182 | */ |
| 183 | static xmlEntityPtr |
| 184 | xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) { |
| 185 | xmlEntitiesTablePtr table; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 186 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 187 | if ((dtd != NULL) && (dtd->pentities != NULL)) { |
| 188 | table = (xmlEntitiesTablePtr) dtd->pentities; |
| 189 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 190 | /* return(xmlGetEntityFromTable(table, name)); */ |
| 191 | } |
| 192 | return(NULL); |
| 193 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 194 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 195 | |
| 196 | /************************************************************************ |
| 197 | * * |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 198 | * QName handling helper * |
| 199 | * * |
| 200 | ************************************************************************/ |
| 201 | |
| 202 | /** |
| 203 | * xmlBuildQName: |
| 204 | * @ncname: the Name |
| 205 | * @prefix: the prefix |
| 206 | * @memory: preallocated memory |
| 207 | * @len: preallocated memory length |
| 208 | * |
| 209 | * Builds the QName @prefix:@ncname in @memory if there is enough space |
| 210 | * and prefix is not NULL nor empty, otherwise allocate a new string. |
| 211 | * If prefix is NULL or empty it returns ncname. |
| 212 | * |
| 213 | * Returns the new string which must be freed by the caller if different from |
| 214 | * @memory and @ncname or NULL in case of error |
| 215 | */ |
| 216 | xmlChar * |
| 217 | xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix, |
| 218 | xmlChar *memory, int len) { |
| 219 | int lenn, lenp; |
| 220 | xmlChar *ret; |
| 221 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 222 | if (ncname == NULL) return(NULL); |
| 223 | if (prefix == NULL) return((xmlChar *) ncname); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 224 | |
| 225 | lenn = strlen((char *) ncname); |
| 226 | lenp = strlen((char *) prefix); |
| 227 | |
| 228 | if ((memory == NULL) || (len < lenn + lenp + 2)) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 229 | ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 230 | if (ret == NULL) { |
| 231 | xmlTreeErrMemory("building QName"); |
| 232 | return(NULL); |
| 233 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 234 | } else { |
| 235 | ret = memory; |
| 236 | } |
| 237 | memcpy(&ret[0], prefix, lenp); |
| 238 | ret[lenp] = ':'; |
| 239 | memcpy(&ret[lenp + 1], ncname, lenn); |
| 240 | ret[lenn + lenp + 1] = 0; |
| 241 | return(ret); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * xmlSplitQName2: |
| 246 | * @name: the full QName |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 247 | * @prefix: a xmlChar ** |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 248 | * |
| 249 | * parse an XML qualified name string |
| 250 | * |
| 251 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 252 | * |
| 253 | * [NS 6] Prefix ::= NCName |
| 254 | * |
| 255 | * [NS 7] LocalPart ::= NCName |
| 256 | * |
| 257 | * Returns NULL if not a QName, otherwise the local part, and prefix |
| 258 | * is updated to get the Prefix if any. |
| 259 | */ |
| 260 | |
| 261 | xmlChar * |
| 262 | xmlSplitQName2(const xmlChar *name, xmlChar **prefix) { |
| 263 | int len = 0; |
| 264 | xmlChar *ret = NULL; |
| 265 | |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 266 | if (prefix == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 267 | *prefix = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 268 | if (name == NULL) return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 269 | |
| 270 | #ifndef XML_XML_NAMESPACE |
| 271 | /* xml: prefix is not really a namespace */ |
| 272 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 273 | (name[2] == 'l') && (name[3] == ':')) |
| 274 | return(NULL); |
| 275 | #endif |
| 276 | |
| 277 | /* nasty but valid */ |
| 278 | if (name[0] == ':') |
| 279 | return(NULL); |
| 280 | |
| 281 | /* |
| 282 | * we are not trying to validate but just to cut, and yes it will |
| 283 | * work even if this is as set of UTF-8 encoded chars |
| 284 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 285 | while ((name[len] != 0) && (name[len] != ':')) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 286 | len++; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 287 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 288 | if (name[len] == 0) |
| 289 | return(NULL); |
| 290 | |
| 291 | *prefix = xmlStrndup(name, len); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 292 | if (*prefix == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 293 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 294 | return(NULL); |
| 295 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 296 | ret = xmlStrdup(&name[len + 1]); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 297 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 298 | xmlTreeErrMemory("QName split"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 299 | if (*prefix != NULL) { |
| 300 | xmlFree(*prefix); |
| 301 | *prefix = NULL; |
| 302 | } |
| 303 | return(NULL); |
| 304 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 305 | |
| 306 | return(ret); |
| 307 | } |
| 308 | |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 309 | /** |
| 310 | * xmlSplitQName3: |
| 311 | * @name: the full QName |
| 312 | * @len: an int * |
| 313 | * |
| 314 | * parse an XML qualified name string,i |
| 315 | * |
| 316 | * returns NULL if it is not a Qualified Name, otherwise, update len |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 317 | * with the length in byte of the prefix and return a pointer |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 318 | * to the start of the name without the prefix |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 319 | */ |
| 320 | |
| 321 | const xmlChar * |
| 322 | xmlSplitQName3(const xmlChar *name, int *len) { |
| 323 | int l = 0; |
| 324 | |
| 325 | if (name == NULL) return(NULL); |
| 326 | if (len == NULL) return(NULL); |
| 327 | |
| 328 | /* nasty but valid */ |
| 329 | if (name[0] == ':') |
| 330 | return(NULL); |
| 331 | |
| 332 | /* |
| 333 | * we are not trying to validate but just to cut, and yes it will |
| 334 | * work even if this is as set of UTF-8 encoded chars |
| 335 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 336 | while ((name[l] != 0) && (name[l] != ':')) |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 337 | l++; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 338 | |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 339 | if (name[l] == 0) |
| 340 | return(NULL); |
| 341 | |
| 342 | *len = l; |
| 343 | |
| 344 | return(&name[l+1]); |
| 345 | } |
| 346 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 347 | /************************************************************************ |
| 348 | * * |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 349 | * Check Name, NCName and QName strings * |
| 350 | * * |
| 351 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 352 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 353 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l) |
| 354 | |
Nicolas Le Cam | 77b5b46 | 2014-02-10 10:32:45 +0800 | [diff] [blame] | 355 | #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) || defined(LIBXML_LEGACY_ENABLED) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 356 | /** |
| 357 | * xmlValidateNCName: |
| 358 | * @value: the value to check |
| 359 | * @space: allow spaces in front and end of the string |
| 360 | * |
| 361 | * Check that a value conforms to the lexical space of NCName |
| 362 | * |
| 363 | * Returns 0 if this validates, a positive error code number otherwise |
| 364 | * and -1 in case of internal or API error. |
| 365 | */ |
| 366 | int |
| 367 | xmlValidateNCName(const xmlChar *value, int space) { |
| 368 | const xmlChar *cur = value; |
| 369 | int c,l; |
| 370 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 371 | if (value == NULL) |
| 372 | return(-1); |
| 373 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 374 | /* |
| 375 | * First quick algorithm for ASCII range |
| 376 | */ |
| 377 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 378 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 379 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 380 | (*cur == '_')) |
| 381 | cur++; |
| 382 | else |
| 383 | goto try_complex; |
| 384 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 385 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 386 | ((*cur >= '0') && (*cur <= '9')) || |
| 387 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 388 | cur++; |
| 389 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 390 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 391 | if (*cur == 0) |
| 392 | return(0); |
| 393 | |
| 394 | try_complex: |
| 395 | /* |
| 396 | * Second check for chars outside the ASCII range |
| 397 | */ |
| 398 | cur = value; |
| 399 | c = CUR_SCHAR(cur, l); |
| 400 | if (space) { |
| 401 | while (IS_BLANK(c)) { |
| 402 | cur += l; |
| 403 | c = CUR_SCHAR(cur, l); |
| 404 | } |
| 405 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 406 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 407 | return(1); |
| 408 | cur += l; |
| 409 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 410 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 411 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 412 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 413 | cur += l; |
| 414 | c = CUR_SCHAR(cur, l); |
| 415 | } |
| 416 | if (space) { |
| 417 | while (IS_BLANK(c)) { |
| 418 | cur += l; |
| 419 | c = CUR_SCHAR(cur, l); |
| 420 | } |
| 421 | } |
| 422 | if (c != 0) |
| 423 | return(1); |
| 424 | |
| 425 | return(0); |
| 426 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 427 | #endif |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 428 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 429 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 430 | /** |
| 431 | * xmlValidateQName: |
| 432 | * @value: the value to check |
| 433 | * @space: allow spaces in front and end of the string |
| 434 | * |
| 435 | * Check that a value conforms to the lexical space of QName |
| 436 | * |
| 437 | * Returns 0 if this validates, a positive error code number otherwise |
| 438 | * and -1 in case of internal or API error. |
| 439 | */ |
| 440 | int |
| 441 | xmlValidateQName(const xmlChar *value, int space) { |
| 442 | const xmlChar *cur = value; |
| 443 | int c,l; |
| 444 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 445 | if (value == NULL) |
| 446 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 447 | /* |
| 448 | * First quick algorithm for ASCII range |
| 449 | */ |
| 450 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 451 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 452 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 453 | (*cur == '_')) |
| 454 | cur++; |
| 455 | else |
| 456 | goto try_complex; |
| 457 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 458 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 459 | ((*cur >= '0') && (*cur <= '9')) || |
| 460 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 461 | cur++; |
| 462 | if (*cur == ':') { |
| 463 | cur++; |
| 464 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 465 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 466 | (*cur == '_')) |
| 467 | cur++; |
| 468 | else |
| 469 | goto try_complex; |
| 470 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 471 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 472 | ((*cur >= '0') && (*cur <= '9')) || |
| 473 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
| 474 | cur++; |
| 475 | } |
| 476 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 477 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 478 | if (*cur == 0) |
| 479 | return(0); |
| 480 | |
| 481 | try_complex: |
| 482 | /* |
| 483 | * Second check for chars outside the ASCII range |
| 484 | */ |
| 485 | cur = value; |
| 486 | c = CUR_SCHAR(cur, l); |
| 487 | if (space) { |
| 488 | while (IS_BLANK(c)) { |
| 489 | cur += l; |
| 490 | c = CUR_SCHAR(cur, l); |
| 491 | } |
| 492 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 493 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 494 | return(1); |
| 495 | cur += l; |
| 496 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 497 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 498 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 499 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 500 | cur += l; |
| 501 | c = CUR_SCHAR(cur, l); |
| 502 | } |
| 503 | if (c == ':') { |
| 504 | cur += l; |
| 505 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 506 | if ((!IS_LETTER(c)) && (c != '_')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 507 | return(1); |
| 508 | cur += l; |
| 509 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 510 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
| 511 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
| 512 | IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 513 | cur += l; |
| 514 | c = CUR_SCHAR(cur, l); |
| 515 | } |
| 516 | } |
| 517 | if (space) { |
| 518 | while (IS_BLANK(c)) { |
| 519 | cur += l; |
| 520 | c = CUR_SCHAR(cur, l); |
| 521 | } |
| 522 | } |
| 523 | if (c != 0) |
| 524 | return(1); |
| 525 | return(0); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * xmlValidateName: |
| 530 | * @value: the value to check |
| 531 | * @space: allow spaces in front and end of the string |
| 532 | * |
| 533 | * Check that a value conforms to the lexical space of Name |
| 534 | * |
| 535 | * Returns 0 if this validates, a positive error code number otherwise |
| 536 | * and -1 in case of internal or API error. |
| 537 | */ |
| 538 | int |
| 539 | xmlValidateName(const xmlChar *value, int space) { |
| 540 | const xmlChar *cur = value; |
| 541 | int c,l; |
| 542 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 543 | if (value == NULL) |
| 544 | return(-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 545 | /* |
| 546 | * First quick algorithm for ASCII range |
| 547 | */ |
| 548 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 549 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 550 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
| 551 | (*cur == '_') || (*cur == ':')) |
| 552 | cur++; |
| 553 | else |
| 554 | goto try_complex; |
| 555 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 556 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 557 | ((*cur >= '0') && (*cur <= '9')) || |
| 558 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 559 | cur++; |
| 560 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 561 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 562 | if (*cur == 0) |
| 563 | return(0); |
| 564 | |
| 565 | try_complex: |
| 566 | /* |
| 567 | * Second check for chars outside the ASCII range |
| 568 | */ |
| 569 | cur = value; |
| 570 | c = CUR_SCHAR(cur, l); |
| 571 | if (space) { |
| 572 | while (IS_BLANK(c)) { |
| 573 | cur += l; |
| 574 | c = CUR_SCHAR(cur, l); |
| 575 | } |
| 576 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 577 | if ((!IS_LETTER(c)) && (c != '_') && (c != ':')) |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 578 | return(1); |
| 579 | cur += l; |
| 580 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 581 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 582 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 583 | cur += l; |
| 584 | c = CUR_SCHAR(cur, l); |
| 585 | } |
| 586 | if (space) { |
| 587 | while (IS_BLANK(c)) { |
| 588 | cur += l; |
| 589 | c = CUR_SCHAR(cur, l); |
| 590 | } |
| 591 | } |
| 592 | if (c != 0) |
| 593 | return(1); |
| 594 | return(0); |
| 595 | } |
| 596 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 597 | /** |
| 598 | * xmlValidateNMToken: |
| 599 | * @value: the value to check |
| 600 | * @space: allow spaces in front and end of the string |
| 601 | * |
| 602 | * Check that a value conforms to the lexical space of NMToken |
| 603 | * |
| 604 | * Returns 0 if this validates, a positive error code number otherwise |
| 605 | * and -1 in case of internal or API error. |
| 606 | */ |
| 607 | int |
| 608 | xmlValidateNMToken(const xmlChar *value, int space) { |
| 609 | const xmlChar *cur = value; |
| 610 | int c,l; |
| 611 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 612 | if (value == NULL) |
| 613 | return(-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 614 | /* |
| 615 | * First quick algorithm for ASCII range |
| 616 | */ |
| 617 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 618 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 619 | if (((*cur >= 'a') && (*cur <= 'z')) || |
| 620 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 621 | ((*cur >= '0') && (*cur <= '9')) || |
| 622 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 623 | cur++; |
| 624 | else |
| 625 | goto try_complex; |
| 626 | while (((*cur >= 'a') && (*cur <= 'z')) || |
| 627 | ((*cur >= 'A') && (*cur <= 'Z')) || |
| 628 | ((*cur >= '0') && (*cur <= '9')) || |
| 629 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
| 630 | cur++; |
| 631 | if (space) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 632 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 633 | if (*cur == 0) |
| 634 | return(0); |
| 635 | |
| 636 | try_complex: |
| 637 | /* |
| 638 | * Second check for chars outside the ASCII range |
| 639 | */ |
| 640 | cur = value; |
| 641 | c = CUR_SCHAR(cur, l); |
| 642 | if (space) { |
| 643 | while (IS_BLANK(c)) { |
| 644 | cur += l; |
| 645 | c = CUR_SCHAR(cur, l); |
| 646 | } |
| 647 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 648 | if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 649 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c))) |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 650 | return(1); |
| 651 | cur += l; |
| 652 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 653 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
| 654 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 655 | cur += l; |
| 656 | c = CUR_SCHAR(cur, l); |
| 657 | } |
| 658 | if (space) { |
| 659 | while (IS_BLANK(c)) { |
| 660 | cur += l; |
| 661 | c = CUR_SCHAR(cur, l); |
| 662 | } |
| 663 | } |
| 664 | if (c != 0) |
| 665 | return(1); |
| 666 | return(0); |
| 667 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 668 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 669 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 670 | /************************************************************************ |
| 671 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 672 | * Allocation and deallocation of basic structures * |
| 673 | * * |
| 674 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 675 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 676 | /** |
| 677 | * xmlSetBufferAllocationScheme: |
| 678 | * @scheme: allocation method to use |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 679 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 680 | * Set the buffer allocation method. Types are |
| 681 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 682 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 683 | * improves performance |
| 684 | */ |
| 685 | void |
| 686 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 687 | if ((scheme == XML_BUFFER_ALLOC_EXACT) || |
Conrad Irwin | 7d0d2a5 | 2012-05-14 14:18:58 +0800 | [diff] [blame] | 688 | (scheme == XML_BUFFER_ALLOC_DOUBLEIT) || |
| 689 | (scheme == XML_BUFFER_ALLOC_HYBRID)) |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 690 | xmlBufferAllocScheme = scheme; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
| 694 | * xmlGetBufferAllocationScheme: |
| 695 | * |
| 696 | * Types are |
| 697 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 698 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 699 | * improves performance |
Conrad Irwin | 7d0d2a5 | 2012-05-14 14:18:58 +0800 | [diff] [blame] | 700 | * XML_BUFFER_ALLOC_HYBRID - use exact sizes on small strings to keep memory usage tight |
| 701 | * in normal usage, and doubleit on large strings to avoid |
| 702 | * pathological performance. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 703 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 704 | * Returns the current allocation scheme |
| 705 | */ |
| 706 | xmlBufferAllocationScheme |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 707 | xmlGetBufferAllocationScheme(void) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 708 | return(xmlBufferAllocScheme); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /** |
| 712 | * xmlNewNs: |
| 713 | * @node: the element carrying the namespace |
| 714 | * @href: the URI associated |
| 715 | * @prefix: the prefix for the namespace |
| 716 | * |
| 717 | * Creation of a new Namespace. This function will refuse to create |
| 718 | * a namespace with a similar prefix than an existing one present on this |
| 719 | * node. |
Daniel Veillard | 81b9617 | 2013-07-22 13:01:11 +0800 | [diff] [blame] | 720 | * Note that for a default namespace, @prefix should be NULL. |
| 721 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 722 | * We use href==NULL in the case of an element creation where the namespace |
| 723 | * was not defined. |
Daniel Veillard | 81b9617 | 2013-07-22 13:01:11 +0800 | [diff] [blame] | 724 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 725 | * Returns a new namespace pointer or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 726 | */ |
| 727 | xmlNsPtr |
| 728 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
| 729 | xmlNsPtr cur; |
| 730 | |
| 731 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 732 | return(NULL); |
| 733 | |
Daniel Veillard | aa54d37 | 2010-09-09 18:17:47 +0200 | [diff] [blame] | 734 | if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) { |
| 735 | /* xml namespace is predefined, no need to add it */ |
| 736 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) |
| 737 | return(NULL); |
| 738 | |
| 739 | /* |
| 740 | * Problem, this is an attempt to bind xml prefix to a wrong |
| 741 | * namespace, which breaks |
| 742 | * Namespace constraint: Reserved Prefixes and Namespace Names |
| 743 | * from XML namespace. But documents authors may not care in |
| 744 | * their context so let's proceed. |
| 745 | */ |
| 746 | } |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 747 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 748 | /* |
| 749 | * Allocate a new Namespace and fill the fields. |
| 750 | */ |
| 751 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 752 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 753 | xmlTreeErrMemory("building namespace"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 754 | return(NULL); |
| 755 | } |
| 756 | memset(cur, 0, sizeof(xmlNs)); |
| 757 | cur->type = XML_LOCAL_NAMESPACE; |
| 758 | |
| 759 | if (href != NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 760 | cur->href = xmlStrdup(href); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 761 | if (prefix != NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 762 | cur->prefix = xmlStrdup(prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 763 | |
| 764 | /* |
| 765 | * Add it at the end to preserve parsing order ... |
| 766 | * and checks for existing use of the prefix |
| 767 | */ |
| 768 | if (node != NULL) { |
| 769 | if (node->nsDef == NULL) { |
| 770 | node->nsDef = cur; |
| 771 | } else { |
| 772 | xmlNsPtr prev = node->nsDef; |
| 773 | |
| 774 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 775 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 776 | xmlFreeNs(cur); |
| 777 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 778 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 779 | while (prev->next != NULL) { |
| 780 | prev = prev->next; |
| 781 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
| 782 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
| 783 | xmlFreeNs(cur); |
| 784 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 785 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 786 | } |
| 787 | prev->next = cur; |
| 788 | } |
| 789 | } |
| 790 | return(cur); |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * xmlSetNs: |
| 795 | * @node: a node in the document |
| 796 | * @ns: a namespace pointer |
| 797 | * |
| 798 | * Associate a namespace to a node, a posteriori. |
| 799 | */ |
| 800 | void |
| 801 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
| 802 | if (node == NULL) { |
| 803 | #ifdef DEBUG_TREE |
| 804 | xmlGenericError(xmlGenericErrorContext, |
| 805 | "xmlSetNs: node == NULL\n"); |
| 806 | #endif |
| 807 | return; |
| 808 | } |
Daniel Veillard | 81b9617 | 2013-07-22 13:01:11 +0800 | [diff] [blame] | 809 | if ((node->type == XML_ELEMENT_NODE) || |
| 810 | (node->type == XML_ATTRIBUTE_NODE)) |
| 811 | node->ns = ns; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /** |
| 815 | * xmlFreeNs: |
| 816 | * @cur: the namespace pointer |
| 817 | * |
| 818 | * Free up the structures associated to a namespace |
| 819 | */ |
| 820 | void |
| 821 | xmlFreeNs(xmlNsPtr cur) { |
| 822 | if (cur == NULL) { |
| 823 | #ifdef DEBUG_TREE |
| 824 | xmlGenericError(xmlGenericErrorContext, |
| 825 | "xmlFreeNs : ns == NULL\n"); |
| 826 | #endif |
| 827 | return; |
| 828 | } |
| 829 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 830 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 831 | xmlFree(cur); |
| 832 | } |
| 833 | |
| 834 | /** |
| 835 | * xmlFreeNsList: |
| 836 | * @cur: the first namespace pointer |
| 837 | * |
| 838 | * Free up all the structures associated to the chained namespaces. |
| 839 | */ |
| 840 | void |
| 841 | xmlFreeNsList(xmlNsPtr cur) { |
| 842 | xmlNsPtr next; |
| 843 | if (cur == NULL) { |
| 844 | #ifdef DEBUG_TREE |
| 845 | xmlGenericError(xmlGenericErrorContext, |
| 846 | "xmlFreeNsList : ns == NULL\n"); |
| 847 | #endif |
| 848 | return; |
| 849 | } |
| 850 | while (cur != NULL) { |
| 851 | next = cur->next; |
| 852 | xmlFreeNs(cur); |
| 853 | cur = next; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * xmlNewDtd: |
| 859 | * @doc: the document pointer |
| 860 | * @name: the DTD name |
| 861 | * @ExternalID: the external ID |
| 862 | * @SystemID: the system ID |
| 863 | * |
| 864 | * Creation of a new DTD for the external subset. To create an |
| 865 | * internal subset, use xmlCreateIntSubset(). |
| 866 | * |
| 867 | * Returns a pointer to the new DTD structure |
| 868 | */ |
| 869 | xmlDtdPtr |
| 870 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 871 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 872 | xmlDtdPtr cur; |
| 873 | |
| 874 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
| 875 | #ifdef DEBUG_TREE |
| 876 | xmlGenericError(xmlGenericErrorContext, |
| 877 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
| 878 | /* !!! */ (char *) name, doc->name, |
| 879 | /* !!! */ (char *)doc->extSubset->name); |
| 880 | #endif |
| 881 | return(NULL); |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Allocate a new DTD and fill the fields. |
| 886 | */ |
| 887 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 888 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 889 | xmlTreeErrMemory("building DTD"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 890 | return(NULL); |
| 891 | } |
| 892 | memset(cur, 0 , sizeof(xmlDtd)); |
| 893 | cur->type = XML_DTD_NODE; |
| 894 | |
| 895 | if (name != NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 896 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 897 | if (ExternalID != NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 898 | cur->ExternalID = xmlStrdup(ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 899 | if (SystemID != NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 900 | cur->SystemID = xmlStrdup(SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 901 | if (doc != NULL) |
| 902 | doc->extSubset = cur; |
| 903 | cur->doc = doc; |
| 904 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 905 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 906 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 907 | return(cur); |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * xmlGetIntSubset: |
| 912 | * @doc: the document pointer |
| 913 | * |
| 914 | * Get the internal subset of a document |
| 915 | * Returns a pointer to the DTD structure or NULL if not found |
| 916 | */ |
| 917 | |
| 918 | xmlDtdPtr |
| 919 | xmlGetIntSubset(xmlDocPtr doc) { |
| 920 | xmlNodePtr cur; |
| 921 | |
| 922 | if (doc == NULL) |
| 923 | return(NULL); |
| 924 | cur = doc->children; |
| 925 | while (cur != NULL) { |
| 926 | if (cur->type == XML_DTD_NODE) |
| 927 | return((xmlDtdPtr) cur); |
| 928 | cur = cur->next; |
| 929 | } |
| 930 | return((xmlDtdPtr) doc->intSubset); |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * xmlCreateIntSubset: |
| 935 | * @doc: the document pointer |
| 936 | * @name: the DTD name |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 937 | * @ExternalID: the external (PUBLIC) ID |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 938 | * @SystemID: the system ID |
| 939 | * |
| 940 | * Create the internal subset of a document |
| 941 | * Returns a pointer to the new DTD structure |
| 942 | */ |
| 943 | xmlDtdPtr |
| 944 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 945 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 946 | xmlDtdPtr cur; |
| 947 | |
| 948 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
| 949 | #ifdef DEBUG_TREE |
| 950 | xmlGenericError(xmlGenericErrorContext, |
| 951 | |
| 952 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 953 | doc->name); |
| 954 | #endif |
| 955 | return(NULL); |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Allocate a new DTD and fill the fields. |
| 960 | */ |
| 961 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
| 962 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 963 | xmlTreeErrMemory("building internal subset"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 964 | return(NULL); |
| 965 | } |
| 966 | memset(cur, 0, sizeof(xmlDtd)); |
| 967 | cur->type = XML_DTD_NODE; |
| 968 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 969 | if (name != NULL) { |
| 970 | cur->name = xmlStrdup(name); |
| 971 | if (cur->name == NULL) { |
| 972 | xmlTreeErrMemory("building internal subset"); |
| 973 | xmlFree(cur); |
| 974 | return(NULL); |
| 975 | } |
| 976 | } |
| 977 | if (ExternalID != NULL) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 978 | cur->ExternalID = xmlStrdup(ExternalID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 979 | if (cur->ExternalID == NULL) { |
| 980 | xmlTreeErrMemory("building internal subset"); |
| 981 | if (cur->name != NULL) |
| 982 | xmlFree((char *)cur->name); |
| 983 | xmlFree(cur); |
| 984 | return(NULL); |
| 985 | } |
| 986 | } |
| 987 | if (SystemID != NULL) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 988 | cur->SystemID = xmlStrdup(SystemID); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 989 | if (cur->SystemID == NULL) { |
| 990 | xmlTreeErrMemory("building internal subset"); |
| 991 | if (cur->name != NULL) |
| 992 | xmlFree((char *)cur->name); |
| 993 | if (cur->ExternalID != NULL) |
| 994 | xmlFree((char *)cur->ExternalID); |
| 995 | xmlFree(cur); |
| 996 | return(NULL); |
| 997 | } |
| 998 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 999 | if (doc != NULL) { |
| 1000 | doc->intSubset = cur; |
| 1001 | cur->parent = doc; |
| 1002 | cur->doc = doc; |
| 1003 | if (doc->children == NULL) { |
| 1004 | doc->children = (xmlNodePtr) cur; |
| 1005 | doc->last = (xmlNodePtr) cur; |
| 1006 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1007 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 1008 | xmlNodePtr prev; |
| 1009 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1010 | prev = doc->children; |
| 1011 | prev->prev = (xmlNodePtr) cur; |
| 1012 | cur->next = prev; |
| 1013 | doc->children = (xmlNodePtr) cur; |
| 1014 | } else { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 1015 | xmlNodePtr next; |
| 1016 | |
| 1017 | next = doc->children; |
| 1018 | while ((next != NULL) && (next->type != XML_ELEMENT_NODE)) |
| 1019 | next = next->next; |
| 1020 | if (next == NULL) { |
| 1021 | cur->prev = doc->last; |
| 1022 | cur->prev->next = (xmlNodePtr) cur; |
| 1023 | cur->next = NULL; |
| 1024 | doc->last = (xmlNodePtr) cur; |
| 1025 | } else { |
| 1026 | cur->next = next; |
| 1027 | cur->prev = next->prev; |
| 1028 | if (cur->prev == NULL) |
| 1029 | doc->children = (xmlNodePtr) cur; |
| 1030 | else |
| 1031 | cur->prev->next = (xmlNodePtr) cur; |
| 1032 | next->prev = (xmlNodePtr) cur; |
| 1033 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1037 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1038 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1039 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1040 | return(cur); |
| 1041 | } |
| 1042 | |
| 1043 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1044 | * DICT_FREE: |
| 1045 | * @str: a string |
| 1046 | * |
| 1047 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 1048 | * current scope |
| 1049 | */ |
| 1050 | #define DICT_FREE(str) \ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1051 | if ((str) && ((!dict) || \ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1052 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 1053 | xmlFree((char *)(str)); |
| 1054 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 1055 | |
| 1056 | /** |
| 1057 | * DICT_COPY: |
| 1058 | * @str: a string |
| 1059 | * |
| 1060 | * Copy a string using a "dict" dictionnary in the current scope, |
| 1061 | * if availabe. |
| 1062 | */ |
| 1063 | #define DICT_COPY(str, cpy) \ |
| 1064 | if (str) { \ |
| 1065 | if (dict) { \ |
| 1066 | if (xmlDictOwns(dict, (const xmlChar *)(str))) \ |
| 1067 | cpy = (xmlChar *) (str); \ |
| 1068 | else \ |
| 1069 | cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \ |
| 1070 | } else \ |
| 1071 | cpy = xmlStrdup((const xmlChar *)(str)); } |
| 1072 | |
| 1073 | /** |
| 1074 | * DICT_CONST_COPY: |
| 1075 | * @str: a string |
| 1076 | * |
| 1077 | * Copy a string using a "dict" dictionnary in the current scope, |
| 1078 | * if availabe. |
| 1079 | */ |
| 1080 | #define DICT_CONST_COPY(str, cpy) \ |
| 1081 | if (str) { \ |
| 1082 | if (dict) { \ |
| 1083 | if (xmlDictOwns(dict, (const xmlChar *)(str))) \ |
| 1084 | cpy = (const xmlChar *) (str); \ |
| 1085 | else \ |
| 1086 | cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \ |
| 1087 | } else \ |
| 1088 | cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); } |
| 1089 | |
| 1090 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1091 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1092 | * xmlFreeDtd: |
| 1093 | * @cur: the DTD structure to free up |
| 1094 | * |
| 1095 | * Free a DTD structure. |
| 1096 | */ |
| 1097 | void |
| 1098 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1099 | xmlDictPtr dict = NULL; |
| 1100 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1101 | if (cur == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1102 | return; |
| 1103 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1104 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1105 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1106 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1107 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1108 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1109 | if (cur->children != NULL) { |
| 1110 | xmlNodePtr next, c = cur->children; |
| 1111 | |
| 1112 | /* |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1113 | * Cleanup all nodes which are not part of the specific lists |
| 1114 | * of notations, elements, attributes and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1115 | */ |
| 1116 | while (c != NULL) { |
| 1117 | next = c->next; |
William M. Brack | 18a04f2 | 2004-08-03 16:42:37 +0000 | [diff] [blame] | 1118 | if ((c->type != XML_NOTATION_NODE) && |
| 1119 | (c->type != XML_ELEMENT_DECL) && |
| 1120 | (c->type != XML_ATTRIBUTE_DECL) && |
| 1121 | (c->type != XML_ENTITY_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1122 | xmlUnlinkNode(c); |
| 1123 | xmlFreeNode(c); |
| 1124 | } |
| 1125 | c = next; |
| 1126 | } |
| 1127 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1128 | DICT_FREE(cur->name) |
| 1129 | DICT_FREE(cur->SystemID) |
| 1130 | DICT_FREE(cur->ExternalID) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1131 | /* TODO !!! */ |
| 1132 | if (cur->notations != NULL) |
| 1133 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1134 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1135 | if (cur->elements != NULL) |
| 1136 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
| 1137 | if (cur->attributes != NULL) |
| 1138 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
| 1139 | if (cur->entities != NULL) |
| 1140 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
| 1141 | if (cur->pentities != NULL) |
| 1142 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
| 1143 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1144 | xmlFree(cur); |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * xmlNewDoc: |
| 1149 | * @version: xmlChar string giving the version of XML "1.0" |
| 1150 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1151 | * Creates a new XML document |
| 1152 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1153 | * Returns a new document |
| 1154 | */ |
| 1155 | xmlDocPtr |
| 1156 | xmlNewDoc(const xmlChar *version) { |
| 1157 | xmlDocPtr cur; |
| 1158 | |
| 1159 | if (version == NULL) |
| 1160 | version = (const xmlChar *) "1.0"; |
| 1161 | |
| 1162 | /* |
| 1163 | * Allocate a new document and fill the fields. |
| 1164 | */ |
| 1165 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
| 1166 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1167 | xmlTreeErrMemory("building doc"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1168 | return(NULL); |
| 1169 | } |
| 1170 | memset(cur, 0, sizeof(xmlDoc)); |
| 1171 | cur->type = XML_DOCUMENT_NODE; |
| 1172 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1173 | cur->version = xmlStrdup(version); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 1174 | if (cur->version == NULL) { |
| 1175 | xmlTreeErrMemory("building doc"); |
| 1176 | xmlFree(cur); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1177 | return(NULL); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 1178 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1179 | cur->standalone = -1; |
| 1180 | cur->compression = -1; /* not initialized */ |
| 1181 | cur->doc = cur; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1182 | cur->parseFlags = 0; |
| 1183 | cur->properties = XML_DOC_USERBUILT; |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 1184 | /* |
| 1185 | * The in memory encoding is always UTF8 |
| 1186 | * This field will never change and would |
| 1187 | * be obsolete if not for binary compatibility. |
| 1188 | */ |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 1189 | cur->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1190 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1191 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1192 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1193 | return(cur); |
| 1194 | } |
| 1195 | |
| 1196 | /** |
| 1197 | * xmlFreeDoc: |
| 1198 | * @cur: pointer to the document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1199 | * |
| 1200 | * Free up all the structures used by a document, tree included. |
| 1201 | */ |
| 1202 | void |
| 1203 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1204 | xmlDtdPtr extSubset, intSubset; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1205 | xmlDictPtr dict = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1206 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1207 | if (cur == NULL) { |
| 1208 | #ifdef DEBUG_TREE |
| 1209 | xmlGenericError(xmlGenericErrorContext, |
| 1210 | "xmlFreeDoc : document == NULL\n"); |
| 1211 | #endif |
| 1212 | return; |
| 1213 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1214 | #ifdef LIBXML_DEBUG_RUNTIME |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1215 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1216 | xmlDebugCheckDocument(stderr, cur); |
| 1217 | #endif |
Daniel Veillard | bca3ad2 | 2005-08-23 22:14:02 +0000 | [diff] [blame] | 1218 | #endif |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1219 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1220 | if (cur != NULL) dict = cur->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1221 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1222 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1223 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 1224 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1225 | /* |
| 1226 | * Do this before freeing the children list to avoid ID lookups |
| 1227 | */ |
| 1228 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
| 1229 | cur->ids = NULL; |
| 1230 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
| 1231 | cur->refs = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1232 | extSubset = cur->extSubset; |
| 1233 | intSubset = cur->intSubset; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 1234 | if (intSubset == extSubset) |
| 1235 | extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1236 | if (extSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1237 | xmlUnlinkNode((xmlNodePtr) cur->extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1238 | cur->extSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1239 | xmlFreeDtd(extSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1240 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1241 | if (intSubset != NULL) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1242 | xmlUnlinkNode((xmlNodePtr) cur->intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1243 | cur->intSubset = NULL; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1244 | xmlFreeDtd(intSubset); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1248 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1249 | |
| 1250 | DICT_FREE(cur->version) |
| 1251 | DICT_FREE(cur->name) |
| 1252 | DICT_FREE(cur->encoding) |
| 1253 | DICT_FREE(cur->URL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1254 | xmlFree(cur); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 1255 | if (dict) xmlDictFree(dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | /** |
| 1259 | * xmlStringLenGetNodeList: |
| 1260 | * @doc: the document |
| 1261 | * @value: the value of the text |
| 1262 | * @len: the length of the string value |
| 1263 | * |
| 1264 | * Parse the value string and build the node list associated. Should |
| 1265 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1266 | * Returns a pointer to the first child |
| 1267 | */ |
| 1268 | xmlNodePtr |
| 1269 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
| 1270 | xmlNodePtr ret = NULL, last = NULL; |
| 1271 | xmlNodePtr node; |
| 1272 | xmlChar *val; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1273 | const xmlChar *cur = value, *end = cur + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1274 | const xmlChar *q; |
| 1275 | xmlEntityPtr ent; |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1276 | xmlBufPtr buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1277 | |
| 1278 | if (value == NULL) return(NULL); |
| 1279 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1280 | buf = xmlBufCreateSize(0); |
| 1281 | if (buf == NULL) return(NULL); |
| 1282 | xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1283 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1284 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1285 | while ((cur < end) && (*cur != 0)) { |
| 1286 | if (cur[0] == '&') { |
| 1287 | int charval = 0; |
| 1288 | xmlChar tmp; |
| 1289 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1290 | /* |
| 1291 | * Save the current text. |
| 1292 | */ |
| 1293 | if (cur != q) { |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1294 | if (xmlBufAdd(buf, q, cur - q)) |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1295 | goto out; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1296 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1297 | q = cur; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1298 | if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) { |
| 1299 | cur += 3; |
| 1300 | if (cur < end) |
| 1301 | tmp = *cur; |
| 1302 | else |
| 1303 | tmp = 0; |
| 1304 | while (tmp != ';') { /* Non input consuming loop */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1305 | if ((tmp >= '0') && (tmp <= '9')) |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1306 | charval = charval * 16 + (tmp - '0'); |
| 1307 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1308 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1309 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1310 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1311 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1312 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1313 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1314 | charval = 0; |
| 1315 | break; |
| 1316 | } |
| 1317 | cur++; |
| 1318 | if (cur < end) |
| 1319 | tmp = *cur; |
| 1320 | else |
| 1321 | tmp = 0; |
| 1322 | } |
| 1323 | if (tmp == ';') |
| 1324 | cur++; |
| 1325 | q = cur; |
| 1326 | } else if ((cur + 1 < end) && (cur[1] == '#')) { |
| 1327 | cur += 2; |
| 1328 | if (cur < end) |
| 1329 | tmp = *cur; |
| 1330 | else |
| 1331 | tmp = 0; |
| 1332 | while (tmp != ';') { /* Non input consuming loops */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1333 | if ((tmp >= '0') && (tmp <= '9')) |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1334 | charval = charval * 10 + (tmp - '0'); |
| 1335 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1336 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1337 | NULL); |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1338 | charval = 0; |
| 1339 | break; |
| 1340 | } |
| 1341 | cur++; |
| 1342 | if (cur < end) |
| 1343 | tmp = *cur; |
| 1344 | else |
| 1345 | tmp = 0; |
| 1346 | } |
| 1347 | if (tmp == ';') |
| 1348 | cur++; |
| 1349 | q = cur; |
| 1350 | } else { |
| 1351 | /* |
| 1352 | * Read the entity string |
| 1353 | */ |
| 1354 | cur++; |
| 1355 | q = cur; |
| 1356 | while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++; |
| 1357 | if ((cur >= end) || (*cur == 0)) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1358 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc, |
| 1359 | (const char *) q); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1360 | goto out; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1361 | } |
| 1362 | if (cur != q) { |
| 1363 | /* |
| 1364 | * Predefined entities don't generate nodes |
| 1365 | */ |
| 1366 | val = xmlStrndup(q, cur - q); |
| 1367 | ent = xmlGetDocEntity(doc, val); |
| 1368 | if ((ent != NULL) && |
| 1369 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1370 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1371 | if (xmlBufCat(buf, ent->content)) |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1372 | goto out; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1373 | |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1374 | } else { |
| 1375 | /* |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1376 | * Flush buffer so far |
| 1377 | */ |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1378 | if (!xmlBufIsEmpty(buf)) { |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1379 | node = xmlNewDocText(doc, NULL); |
| 1380 | if (node == NULL) { |
| 1381 | if (val != NULL) xmlFree(val); |
| 1382 | goto out; |
| 1383 | } |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1384 | node->content = xmlBufDetach(buf); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1385 | |
| 1386 | if (last == NULL) { |
| 1387 | last = ret = node; |
| 1388 | } else { |
| 1389 | last = xmlAddNextSibling(last, node); |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | /* |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1394 | * Create a new REFERENCE_REF node |
| 1395 | */ |
| 1396 | node = xmlNewReference(doc, val); |
| 1397 | if (node == NULL) { |
| 1398 | if (val != NULL) xmlFree(val); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1399 | goto out; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1400 | } |
| 1401 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1402 | xmlNodePtr temp; |
| 1403 | |
| 1404 | ent->children = xmlStringGetNodeList(doc, |
| 1405 | (const xmlChar*)node->content); |
| 1406 | ent->owner = 1; |
| 1407 | temp = ent->children; |
| 1408 | while (temp) { |
| 1409 | temp->parent = (xmlNodePtr)ent; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1410 | ent->last = temp; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1411 | temp = temp->next; |
| 1412 | } |
| 1413 | } |
| 1414 | if (last == NULL) { |
| 1415 | last = ret = node; |
| 1416 | } else { |
| 1417 | last = xmlAddNextSibling(last, node); |
| 1418 | } |
| 1419 | } |
| 1420 | xmlFree(val); |
| 1421 | } |
| 1422 | cur++; |
| 1423 | q = cur; |
| 1424 | } |
| 1425 | if (charval != 0) { |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1426 | xmlChar buffer[10]; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1427 | int l; |
| 1428 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1429 | l = xmlCopyCharMultiByte(buffer, charval); |
| 1430 | buffer[l] = 0; |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1431 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1432 | if (xmlBufCat(buf, buffer)) |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1433 | goto out; |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1434 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1435 | } |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 1436 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1437 | cur++; |
| 1438 | } |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1439 | |
| 1440 | if (cur != q) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1441 | /* |
| 1442 | * Handle the last piece of text. |
| 1443 | */ |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1444 | if (xmlBufAdd(buf, q, cur - q)) |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1445 | goto out; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1446 | } |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1447 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1448 | if (!xmlBufIsEmpty(buf)) { |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1449 | node = xmlNewDocText(doc, NULL); |
| 1450 | if (node == NULL) goto out; |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1451 | node->content = xmlBufDetach(buf); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1452 | |
| 1453 | if (last == NULL) { |
| 1454 | last = ret = node; |
| 1455 | } else { |
| 1456 | last = xmlAddNextSibling(last, node); |
| 1457 | } |
| 1458 | } else if (ret == NULL) { |
| 1459 | ret = xmlNewDocText(doc, BAD_CAST ""); |
| 1460 | } |
| 1461 | |
| 1462 | out: |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1463 | xmlBufFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1464 | return(ret); |
| 1465 | } |
| 1466 | |
| 1467 | /** |
| 1468 | * xmlStringGetNodeList: |
| 1469 | * @doc: the document |
| 1470 | * @value: the value of the attribute |
| 1471 | * |
| 1472 | * Parse the value string and build the node list associated. Should |
| 1473 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
| 1474 | * Returns a pointer to the first child |
| 1475 | */ |
| 1476 | xmlNodePtr |
| 1477 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
| 1478 | xmlNodePtr ret = NULL, last = NULL; |
| 1479 | xmlNodePtr node; |
| 1480 | xmlChar *val; |
| 1481 | const xmlChar *cur = value; |
| 1482 | const xmlChar *q; |
| 1483 | xmlEntityPtr ent; |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1484 | xmlBufPtr buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1485 | |
| 1486 | if (value == NULL) return(NULL); |
| 1487 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1488 | buf = xmlBufCreateSize(0); |
| 1489 | if (buf == NULL) return(NULL); |
| 1490 | xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1491 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1492 | q = cur; |
| 1493 | while (*cur != 0) { |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1494 | if (cur[0] == '&') { |
| 1495 | int charval = 0; |
| 1496 | xmlChar tmp; |
| 1497 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1498 | /* |
| 1499 | * Save the current text. |
| 1500 | */ |
| 1501 | if (cur != q) { |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1502 | if (xmlBufAdd(buf, q, cur - q)) |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1503 | goto out; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1504 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1505 | q = cur; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1506 | if ((cur[1] == '#') && (cur[2] == 'x')) { |
| 1507 | cur += 3; |
| 1508 | tmp = *cur; |
| 1509 | while (tmp != ';') { /* Non input consuming loop */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1510 | if ((tmp >= '0') && (tmp <= '9')) |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1511 | charval = charval * 16 + (tmp - '0'); |
| 1512 | else if ((tmp >= 'a') && (tmp <= 'f')) |
| 1513 | charval = charval * 16 + (tmp - 'a') + 10; |
| 1514 | else if ((tmp >= 'A') && (tmp <= 'F')) |
| 1515 | charval = charval * 16 + (tmp - 'A') + 10; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1516 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1517 | xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc, |
| 1518 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1519 | charval = 0; |
| 1520 | break; |
| 1521 | } |
| 1522 | cur++; |
| 1523 | tmp = *cur; |
| 1524 | } |
| 1525 | if (tmp == ';') |
| 1526 | cur++; |
| 1527 | q = cur; |
| 1528 | } else if (cur[1] == '#') { |
| 1529 | cur += 2; |
| 1530 | tmp = *cur; |
| 1531 | while (tmp != ';') { /* Non input consuming loops */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1532 | if ((tmp >= '0') && (tmp <= '9')) |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1533 | charval = charval * 10 + (tmp - '0'); |
| 1534 | else { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1535 | xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc, |
| 1536 | NULL); |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1537 | charval = 0; |
| 1538 | break; |
| 1539 | } |
| 1540 | cur++; |
| 1541 | tmp = *cur; |
| 1542 | } |
| 1543 | if (tmp == ';') |
| 1544 | cur++; |
| 1545 | q = cur; |
| 1546 | } else { |
| 1547 | /* |
| 1548 | * Read the entity string |
| 1549 | */ |
| 1550 | cur++; |
| 1551 | q = cur; |
| 1552 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 1553 | if (*cur == 0) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 1554 | xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, |
| 1555 | (xmlNodePtr) doc, (const char *) q); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1556 | goto out; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1557 | } |
| 1558 | if (cur != q) { |
| 1559 | /* |
| 1560 | * Predefined entities don't generate nodes |
| 1561 | */ |
| 1562 | val = xmlStrndup(q, cur - q); |
| 1563 | ent = xmlGetDocEntity(doc, val); |
| 1564 | if ((ent != NULL) && |
| 1565 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1566 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1567 | if (xmlBufCat(buf, ent->content)) |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1568 | goto out; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 1569 | |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1570 | } else { |
| 1571 | /* |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1572 | * Flush buffer so far |
| 1573 | */ |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1574 | if (!xmlBufIsEmpty(buf)) { |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1575 | node = xmlNewDocText(doc, NULL); |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1576 | node->content = xmlBufDetach(buf); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1577 | |
| 1578 | if (last == NULL) { |
| 1579 | last = ret = node; |
| 1580 | } else { |
| 1581 | last = xmlAddNextSibling(last, node); |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | /* |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1586 | * Create a new REFERENCE_REF node |
| 1587 | */ |
| 1588 | node = xmlNewReference(doc, val); |
| 1589 | if (node == NULL) { |
| 1590 | if (val != NULL) xmlFree(val); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1591 | goto out; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1592 | } |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1593 | else if ((ent != NULL) && (ent->children == NULL)) { |
| 1594 | xmlNodePtr temp; |
| 1595 | |
| 1596 | ent->children = xmlStringGetNodeList(doc, |
| 1597 | (const xmlChar*)node->content); |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 1598 | ent->owner = 1; |
Daniel Veillard | bf8dae8 | 2002-04-18 16:39:10 +0000 | [diff] [blame] | 1599 | temp = ent->children; |
| 1600 | while (temp) { |
| 1601 | temp->parent = (xmlNodePtr)ent; |
| 1602 | temp = temp->next; |
| 1603 | } |
| 1604 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1605 | if (last == NULL) { |
| 1606 | last = ret = node; |
| 1607 | } else { |
| 1608 | last = xmlAddNextSibling(last, node); |
| 1609 | } |
| 1610 | } |
| 1611 | xmlFree(val); |
| 1612 | } |
| 1613 | cur++; |
| 1614 | q = cur; |
| 1615 | } |
| 1616 | if (charval != 0) { |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1617 | xmlChar buffer[10]; |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1618 | int len; |
| 1619 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1620 | len = xmlCopyCharMultiByte(buffer, charval); |
| 1621 | buffer[len] = 0; |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1622 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1623 | if (xmlBufCat(buf, buffer)) |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1624 | goto out; |
| 1625 | charval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1626 | } |
Daniel Veillard | bdb9ba7 | 2001-04-11 11:28:06 +0000 | [diff] [blame] | 1627 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1628 | cur++; |
| 1629 | } |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 1630 | if ((cur != q) || (ret == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1631 | /* |
| 1632 | * Handle the last piece of text. |
| 1633 | */ |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1634 | xmlBufAdd(buf, q, cur - q); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1637 | if (!xmlBufIsEmpty(buf)) { |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1638 | node = xmlNewDocText(doc, NULL); |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1639 | node->content = xmlBufDetach(buf); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1640 | |
| 1641 | if (last == NULL) { |
| 1642 | last = ret = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1643 | } else { |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1644 | last = xmlAddNextSibling(last, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1645 | } |
| 1646 | } |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1647 | |
| 1648 | out: |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 1649 | xmlBufFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1650 | return(ret); |
| 1651 | } |
| 1652 | |
| 1653 | /** |
| 1654 | * xmlNodeListGetString: |
| 1655 | * @doc: the document |
| 1656 | * @list: a Node list |
| 1657 | * @inLine: should we replace entity contents or show their external form |
| 1658 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1659 | * Build the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1660 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1661 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1662 | * 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] | 1663 | */ |
| 1664 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1665 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1666 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1667 | xmlNodePtr node = list; |
| 1668 | xmlChar *ret = NULL; |
| 1669 | xmlEntityPtr ent; |
Daniel Veillard | 7d4c529 | 2012-09-05 11:45:32 +0800 | [diff] [blame] | 1670 | int attr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1671 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1672 | if (list == NULL) |
| 1673 | return (NULL); |
Daniel Veillard | 7d4c529 | 2012-09-05 11:45:32 +0800 | [diff] [blame] | 1674 | if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE)) |
| 1675 | attr = 1; |
| 1676 | else |
| 1677 | attr = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1678 | |
| 1679 | while (node != NULL) { |
| 1680 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1681 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1682 | if (inLine) { |
| 1683 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1684 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1685 | xmlChar *buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1686 | |
Daniel Veillard | 7d4c529 | 2012-09-05 11:45:32 +0800 | [diff] [blame] | 1687 | if (attr) |
| 1688 | buffer = xmlEncodeAttributeEntities(doc, node->content); |
| 1689 | else |
| 1690 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1691 | if (buffer != NULL) { |
| 1692 | ret = xmlStrcat(ret, buffer); |
| 1693 | xmlFree(buffer); |
| 1694 | } |
| 1695 | } |
| 1696 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1697 | if (inLine) { |
| 1698 | ent = xmlGetDocEntity(doc, node->name); |
| 1699 | if (ent != NULL) { |
| 1700 | xmlChar *buffer; |
| 1701 | |
| 1702 | /* an entity content can be any "well balanced chunk", |
| 1703 | * i.e. the result of the content [43] production: |
| 1704 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1705 | * So it can contain text, CDATA section or nested |
| 1706 | * entity reference nodes (among others). |
| 1707 | * -> we recursive call xmlNodeListGetString() |
| 1708 | * which handles these types */ |
| 1709 | buffer = xmlNodeListGetString(doc, ent->children, 1); |
| 1710 | if (buffer != NULL) { |
| 1711 | ret = xmlStrcat(ret, buffer); |
| 1712 | xmlFree(buffer); |
| 1713 | } |
| 1714 | } else { |
| 1715 | ret = xmlStrcat(ret, node->content); |
| 1716 | } |
| 1717 | } else { |
| 1718 | xmlChar buf[2]; |
| 1719 | |
| 1720 | buf[0] = '&'; |
| 1721 | buf[1] = 0; |
| 1722 | ret = xmlStrncat(ret, buf, 1); |
| 1723 | ret = xmlStrcat(ret, node->name); |
| 1724 | buf[0] = ';'; |
| 1725 | buf[1] = 0; |
| 1726 | ret = xmlStrncat(ret, buf, 1); |
| 1727 | } |
| 1728 | } |
| 1729 | #if 0 |
| 1730 | else { |
| 1731 | xmlGenericError(xmlGenericErrorContext, |
| 1732 | "xmlGetNodeListString : invalid node type %d\n", |
| 1733 | node->type); |
| 1734 | } |
| 1735 | #endif |
| 1736 | node = node->next; |
| 1737 | } |
| 1738 | return (ret); |
| 1739 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1740 | |
| 1741 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1742 | /** |
| 1743 | * xmlNodeListGetRawString: |
| 1744 | * @doc: the document |
| 1745 | * @list: a Node list |
| 1746 | * @inLine: should we replace entity contents or show their external form |
| 1747 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1748 | * Builds the string equivalent to the text contained in the Node list |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1749 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 1750 | * this function doesn't do any character encoding handling. |
| 1751 | * |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 1752 | * 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] | 1753 | */ |
| 1754 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1755 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) |
| 1756 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1757 | xmlNodePtr node = list; |
| 1758 | xmlChar *ret = NULL; |
| 1759 | xmlEntityPtr ent; |
| 1760 | |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1761 | if (list == NULL) |
| 1762 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1763 | |
| 1764 | while (node != NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1765 | if ((node->type == XML_TEXT_NODE) || |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1766 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 1767 | if (inLine) { |
| 1768 | ret = xmlStrcat(ret, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1769 | } else { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1770 | xmlChar *buffer; |
| 1771 | |
| 1772 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 1773 | if (buffer != NULL) { |
| 1774 | ret = xmlStrcat(ret, buffer); |
| 1775 | xmlFree(buffer); |
| 1776 | } |
| 1777 | } |
| 1778 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 1779 | if (inLine) { |
| 1780 | ent = xmlGetDocEntity(doc, node->name); |
| 1781 | if (ent != NULL) { |
| 1782 | xmlChar *buffer; |
| 1783 | |
| 1784 | /* an entity content can be any "well balanced chunk", |
| 1785 | * i.e. the result of the content [43] production: |
| 1786 | * http://www.w3.org/TR/REC-xml#NT-content. |
| 1787 | * So it can contain text, CDATA section or nested |
| 1788 | * entity reference nodes (among others). |
| 1789 | * -> we recursive call xmlNodeListGetRawString() |
| 1790 | * which handles these types */ |
| 1791 | buffer = |
| 1792 | xmlNodeListGetRawString(doc, ent->children, 1); |
| 1793 | if (buffer != NULL) { |
| 1794 | ret = xmlStrcat(ret, buffer); |
| 1795 | xmlFree(buffer); |
| 1796 | } |
| 1797 | } else { |
| 1798 | ret = xmlStrcat(ret, node->content); |
| 1799 | } |
| 1800 | } else { |
| 1801 | xmlChar buf[2]; |
| 1802 | |
| 1803 | buf[0] = '&'; |
| 1804 | buf[1] = 0; |
| 1805 | ret = xmlStrncat(ret, buf, 1); |
| 1806 | ret = xmlStrcat(ret, node->name); |
| 1807 | buf[0] = ';'; |
| 1808 | buf[1] = 0; |
| 1809 | ret = xmlStrncat(ret, buf, 1); |
| 1810 | } |
| 1811 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1812 | #if 0 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1813 | else { |
| 1814 | xmlGenericError(xmlGenericErrorContext, |
| 1815 | "xmlGetNodeListString : invalid node type %d\n", |
| 1816 | node->type); |
| 1817 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1818 | #endif |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1819 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1820 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1821 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1822 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1823 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1824 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1825 | static xmlAttrPtr |
| 1826 | xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns, |
| 1827 | const xmlChar * name, const xmlChar * value, |
| 1828 | int eatname) |
| 1829 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1830 | xmlAttrPtr cur; |
| 1831 | xmlDocPtr doc = NULL; |
| 1832 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1833 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) { |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 1834 | if ((eatname == 1) && |
| 1835 | ((node->doc == NULL) || |
Daniel Veillard | ed939f8 | 2008-04-08 08:20:08 +0000 | [diff] [blame] | 1836 | (!(xmlDictOwns(node->doc->dict, name))))) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1837 | xmlFree((xmlChar *) name); |
| 1838 | return (NULL); |
| 1839 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1840 | |
| 1841 | /* |
| 1842 | * Allocate a new property and fill the fields. |
| 1843 | */ |
| 1844 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 1845 | if (cur == NULL) { |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 1846 | if ((eatname == 1) && |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 1847 | ((node == NULL) || (node->doc == NULL) || |
Daniel Veillard | ed939f8 | 2008-04-08 08:20:08 +0000 | [diff] [blame] | 1848 | (!(xmlDictOwns(node->doc->dict, name))))) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1849 | xmlFree((xmlChar *) name); |
| 1850 | xmlTreeErrMemory("building attribute"); |
| 1851 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1852 | } |
| 1853 | memset(cur, 0, sizeof(xmlAttr)); |
| 1854 | cur->type = XML_ATTRIBUTE_NODE; |
| 1855 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1856 | cur->parent = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1857 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1858 | doc = node->doc; |
| 1859 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1860 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1861 | cur->ns = ns; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1862 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1863 | if (eatname == 0) { |
| 1864 | if ((doc != NULL) && (doc->dict != NULL)) |
| 1865 | cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1); |
| 1866 | else |
| 1867 | cur->name = xmlStrdup(name); |
| 1868 | } else |
| 1869 | cur->name = name; |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1870 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1871 | if (value != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1872 | xmlNodePtr tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1873 | |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame] | 1874 | if(!xmlCheckUTF8(value)) { |
| 1875 | xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc, |
| 1876 | NULL); |
| 1877 | if (doc != NULL) |
| 1878 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 1879 | } |
| 1880 | cur->children = xmlNewDocText(doc, value); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1881 | cur->last = NULL; |
| 1882 | tmp = cur->children; |
| 1883 | while (tmp != NULL) { |
| 1884 | tmp->parent = (xmlNodePtr) cur; |
| 1885 | if (tmp->next == NULL) |
| 1886 | cur->last = tmp; |
| 1887 | tmp = tmp->next; |
| 1888 | } |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1889 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1890 | |
| 1891 | /* |
| 1892 | * Add it at the end to preserve parsing order ... |
| 1893 | */ |
| 1894 | if (node != NULL) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1895 | if (node->properties == NULL) { |
| 1896 | node->properties = cur; |
| 1897 | } else { |
| 1898 | xmlAttrPtr prev = node->properties; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1899 | |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1900 | while (prev->next != NULL) |
| 1901 | prev = prev->next; |
| 1902 | prev->next = cur; |
| 1903 | cur->prev = prev; |
| 1904 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1905 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 1906 | |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 1907 | if ((value != NULL) && (node != NULL) && |
| 1908 | (xmlIsID(node->doc, node, cur) == 1)) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1909 | xmlAddID(NULL, node->doc, value, cur); |
| 1910 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1911 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 1912 | xmlRegisterNodeDefaultValue((xmlNodePtr) cur); |
| 1913 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1914 | } |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1915 | |
| 1916 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
| 1917 | defined(LIBXML_SCHEMAS_ENABLED) |
| 1918 | /** |
| 1919 | * xmlNewProp: |
| 1920 | * @node: the holding node |
| 1921 | * @name: the name of the attribute |
| 1922 | * @value: the value of the attribute |
| 1923 | * |
| 1924 | * Create a new property carried by a node. |
| 1925 | * Returns a pointer to the attribute |
| 1926 | */ |
| 1927 | xmlAttrPtr |
| 1928 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
| 1929 | |
| 1930 | if (name == NULL) { |
| 1931 | #ifdef DEBUG_TREE |
| 1932 | xmlGenericError(xmlGenericErrorContext, |
| 1933 | "xmlNewProp : name == NULL\n"); |
| 1934 | #endif |
| 1935 | return(NULL); |
| 1936 | } |
| 1937 | |
| 1938 | return xmlNewPropInternal(node, NULL, name, value, 0); |
| 1939 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1940 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1941 | |
| 1942 | /** |
| 1943 | * xmlNewNsProp: |
| 1944 | * @node: the holding node |
| 1945 | * @ns: the namespace |
| 1946 | * @name: the name of the attribute |
| 1947 | * @value: the value of the attribute |
| 1948 | * |
| 1949 | * Create a new property tagged with a namespace and carried by a node. |
| 1950 | * Returns a pointer to the attribute |
| 1951 | */ |
| 1952 | xmlAttrPtr |
| 1953 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 1954 | const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1955 | |
| 1956 | if (name == NULL) { |
| 1957 | #ifdef DEBUG_TREE |
| 1958 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 1959 | "xmlNewNsProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1960 | #endif |
| 1961 | return(NULL); |
| 1962 | } |
| 1963 | |
Daniel Veillard | 5cd3e8c | 2005-03-27 11:25:28 +0000 | [diff] [blame] | 1964 | return xmlNewPropInternal(node, ns, name, value, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1968 | * xmlNewNsPropEatName: |
| 1969 | * @node: the holding node |
| 1970 | * @ns: the namespace |
| 1971 | * @name: the name of the attribute |
| 1972 | * @value: the value of the attribute |
| 1973 | * |
| 1974 | * Create a new property tagged with a namespace and carried by a node. |
| 1975 | * Returns a pointer to the attribute |
| 1976 | */ |
| 1977 | xmlAttrPtr |
| 1978 | xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name, |
| 1979 | const xmlChar *value) { |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1980 | |
| 1981 | if (name == NULL) { |
| 1982 | #ifdef DEBUG_TREE |
| 1983 | xmlGenericError(xmlGenericErrorContext, |
| 1984 | "xmlNewNsPropEatName : name == NULL\n"); |
| 1985 | #endif |
| 1986 | return(NULL); |
| 1987 | } |
| 1988 | |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 1989 | return xmlNewPropInternal(node, ns, name, value, 1); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1993 | * xmlNewDocProp: |
| 1994 | * @doc: the document |
| 1995 | * @name: the name of the attribute |
| 1996 | * @value: the value of the attribute |
| 1997 | * |
| 1998 | * Create a new property carried by a document. |
| 1999 | * Returns a pointer to the attribute |
| 2000 | */ |
| 2001 | xmlAttrPtr |
| 2002 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
| 2003 | xmlAttrPtr cur; |
| 2004 | |
| 2005 | if (name == NULL) { |
| 2006 | #ifdef DEBUG_TREE |
| 2007 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2008 | "xmlNewDocProp : name == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2009 | #endif |
| 2010 | return(NULL); |
| 2011 | } |
| 2012 | |
| 2013 | /* |
| 2014 | * Allocate a new property and fill the fields. |
| 2015 | */ |
| 2016 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
| 2017 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2018 | xmlTreeErrMemory("building attribute"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2019 | return(NULL); |
| 2020 | } |
| 2021 | memset(cur, 0, sizeof(xmlAttr)); |
| 2022 | cur->type = XML_ATTRIBUTE_NODE; |
| 2023 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2024 | if ((doc != NULL) && (doc->dict != NULL)) |
| 2025 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 2026 | else |
| 2027 | cur->name = xmlStrdup(name); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2028 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2029 | if (value != NULL) { |
| 2030 | xmlNodePtr tmp; |
| 2031 | |
| 2032 | cur->children = xmlStringGetNodeList(doc, value); |
| 2033 | cur->last = NULL; |
| 2034 | |
| 2035 | tmp = cur->children; |
| 2036 | while (tmp != NULL) { |
| 2037 | tmp->parent = (xmlNodePtr) cur; |
| 2038 | if (tmp->next == NULL) |
| 2039 | cur->last = tmp; |
| 2040 | tmp = tmp->next; |
| 2041 | } |
| 2042 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2043 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2044 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2045 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2046 | return(cur); |
| 2047 | } |
| 2048 | |
| 2049 | /** |
| 2050 | * xmlFreePropList: |
| 2051 | * @cur: the first property in the list |
| 2052 | * |
| 2053 | * Free a property and all its siblings, all the children are freed too. |
| 2054 | */ |
| 2055 | void |
| 2056 | xmlFreePropList(xmlAttrPtr cur) { |
| 2057 | xmlAttrPtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2058 | if (cur == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2059 | while (cur != NULL) { |
| 2060 | next = cur->next; |
| 2061 | xmlFreeProp(cur); |
| 2062 | cur = next; |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | /** |
| 2067 | * xmlFreeProp: |
| 2068 | * @cur: an attribute |
| 2069 | * |
| 2070 | * Free one attribute, all the content is freed too |
| 2071 | */ |
| 2072 | void |
| 2073 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2074 | xmlDictPtr dict = NULL; |
| 2075 | if (cur == NULL) return; |
| 2076 | |
| 2077 | if (cur->doc != NULL) dict = cur->doc->dict; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2078 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2079 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2080 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
| 2081 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2082 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | da6f4af | 2005-06-20 17:17:54 +0000 | [diff] [blame] | 2083 | if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) { |
| 2084 | xmlRemoveID(cur->doc, cur); |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2085 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2086 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 2087 | DICT_FREE(cur->name) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2088 | xmlFree(cur); |
| 2089 | } |
| 2090 | |
| 2091 | /** |
| 2092 | * xmlRemoveProp: |
| 2093 | * @cur: an attribute |
| 2094 | * |
| 2095 | * Unlink and free one attribute, all the content is freed too |
| 2096 | * Note this doesn't work for namespace definition attributes |
| 2097 | * |
| 2098 | * Returns 0 if success and -1 in case of error. |
| 2099 | */ |
| 2100 | int |
| 2101 | xmlRemoveProp(xmlAttrPtr cur) { |
| 2102 | xmlAttrPtr tmp; |
| 2103 | if (cur == NULL) { |
| 2104 | #ifdef DEBUG_TREE |
| 2105 | xmlGenericError(xmlGenericErrorContext, |
| 2106 | "xmlRemoveProp : cur == NULL\n"); |
| 2107 | #endif |
| 2108 | return(-1); |
| 2109 | } |
| 2110 | if (cur->parent == NULL) { |
| 2111 | #ifdef DEBUG_TREE |
| 2112 | xmlGenericError(xmlGenericErrorContext, |
| 2113 | "xmlRemoveProp : cur->parent == NULL\n"); |
| 2114 | #endif |
| 2115 | return(-1); |
| 2116 | } |
| 2117 | tmp = cur->parent->properties; |
| 2118 | if (tmp == cur) { |
| 2119 | cur->parent->properties = cur->next; |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2120 | if (cur->next != NULL) |
| 2121 | cur->next->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2122 | xmlFreeProp(cur); |
| 2123 | return(0); |
| 2124 | } |
| 2125 | while (tmp != NULL) { |
| 2126 | if (tmp->next == cur) { |
| 2127 | tmp->next = cur->next; |
| 2128 | if (tmp->next != NULL) |
| 2129 | tmp->next->prev = tmp; |
| 2130 | xmlFreeProp(cur); |
| 2131 | return(0); |
| 2132 | } |
| 2133 | tmp = tmp->next; |
| 2134 | } |
| 2135 | #ifdef DEBUG_TREE |
| 2136 | xmlGenericError(xmlGenericErrorContext, |
| 2137 | "xmlRemoveProp : attribute not owned by its node\n"); |
| 2138 | #endif |
| 2139 | return(-1); |
| 2140 | } |
| 2141 | |
| 2142 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2143 | * xmlNewDocPI: |
| 2144 | * @doc: the target document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2145 | * @name: the processing instruction name |
| 2146 | * @content: the PI content |
| 2147 | * |
| 2148 | * Creation of a processing instruction element. |
| 2149 | * Returns a pointer to the new node object. |
| 2150 | */ |
| 2151 | xmlNodePtr |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2152 | xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2153 | xmlNodePtr cur; |
| 2154 | |
| 2155 | if (name == NULL) { |
| 2156 | #ifdef DEBUG_TREE |
| 2157 | xmlGenericError(xmlGenericErrorContext, |
| 2158 | "xmlNewPI : name == NULL\n"); |
| 2159 | #endif |
| 2160 | return(NULL); |
| 2161 | } |
| 2162 | |
| 2163 | /* |
| 2164 | * Allocate a new node and fill the fields. |
| 2165 | */ |
| 2166 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2167 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2168 | xmlTreeErrMemory("building PI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2169 | return(NULL); |
| 2170 | } |
| 2171 | memset(cur, 0, sizeof(xmlNode)); |
| 2172 | cur->type = XML_PI_NODE; |
| 2173 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2174 | if ((doc != NULL) && (doc->dict != NULL)) |
| 2175 | cur->name = xmlDictLookup(doc->dict, name, -1); |
| 2176 | else |
| 2177 | cur->name = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2178 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2179 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2180 | } |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 2181 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2182 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2183 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2184 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2185 | return(cur); |
| 2186 | } |
| 2187 | |
| 2188 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2189 | * xmlNewPI: |
| 2190 | * @name: the processing instruction name |
| 2191 | * @content: the PI content |
| 2192 | * |
| 2193 | * Creation of a processing instruction element. |
| 2194 | * Use xmlDocNewPI preferably to get string interning |
| 2195 | * |
| 2196 | * Returns a pointer to the new node object. |
| 2197 | */ |
| 2198 | xmlNodePtr |
| 2199 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
| 2200 | return(xmlNewDocPI(NULL, name, content)); |
| 2201 | } |
| 2202 | |
| 2203 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2204 | * xmlNewNode: |
| 2205 | * @ns: namespace if any |
| 2206 | * @name: the node name |
| 2207 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2208 | * Creation of a new node element. @ns is optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2209 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2210 | * Returns a pointer to the new node object. Uses xmlStrdup() to make |
| 2211 | * copy of @name. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2212 | */ |
| 2213 | xmlNodePtr |
| 2214 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
| 2215 | xmlNodePtr cur; |
| 2216 | |
| 2217 | if (name == NULL) { |
| 2218 | #ifdef DEBUG_TREE |
| 2219 | xmlGenericError(xmlGenericErrorContext, |
| 2220 | "xmlNewNode : name == NULL\n"); |
| 2221 | #endif |
| 2222 | return(NULL); |
| 2223 | } |
| 2224 | |
| 2225 | /* |
| 2226 | * Allocate a new node and fill the fields. |
| 2227 | */ |
| 2228 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2229 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2230 | xmlTreeErrMemory("building node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2231 | return(NULL); |
| 2232 | } |
| 2233 | memset(cur, 0, sizeof(xmlNode)); |
| 2234 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2235 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2236 | cur->name = xmlStrdup(name); |
| 2237 | cur->ns = ns; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2238 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2239 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2240 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2241 | return(cur); |
| 2242 | } |
| 2243 | |
| 2244 | /** |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2245 | * xmlNewNodeEatName: |
| 2246 | * @ns: namespace if any |
| 2247 | * @name: the node name |
| 2248 | * |
| 2249 | * Creation of a new node element. @ns is optional (NULL). |
| 2250 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 2251 | * Returns a pointer to the new node object, with pointer @name as |
| 2252 | * new node's name. Use xmlNewNode() if a copy of @name string is |
| 2253 | * is needed as new node's name. |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2254 | */ |
| 2255 | xmlNodePtr |
| 2256 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
| 2257 | xmlNodePtr cur; |
| 2258 | |
| 2259 | if (name == NULL) { |
| 2260 | #ifdef DEBUG_TREE |
| 2261 | xmlGenericError(xmlGenericErrorContext, |
| 2262 | "xmlNewNode : name == NULL\n"); |
| 2263 | #endif |
| 2264 | return(NULL); |
| 2265 | } |
| 2266 | |
| 2267 | /* |
| 2268 | * Allocate a new node and fill the fields. |
| 2269 | */ |
| 2270 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2271 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2272 | xmlTreeErrMemory("building node"); |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 2273 | /* we can't check here that name comes from the doc dictionnary */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2274 | return(NULL); |
| 2275 | } |
| 2276 | memset(cur, 0, sizeof(xmlNode)); |
| 2277 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2278 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2279 | cur->name = name; |
| 2280 | cur->ns = ns; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2281 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2282 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 2283 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2284 | return(cur); |
| 2285 | } |
| 2286 | |
| 2287 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2288 | * xmlNewDocNode: |
| 2289 | * @doc: the document |
| 2290 | * @ns: namespace if any |
| 2291 | * @name: the node name |
| 2292 | * @content: the XML text content if any |
| 2293 | * |
| 2294 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2295 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2296 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2297 | * references, but XML special chars need to be escaped first by using |
| 2298 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2299 | * need entities support. |
| 2300 | * |
| 2301 | * Returns a pointer to the new node object. |
| 2302 | */ |
| 2303 | xmlNodePtr |
| 2304 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2305 | const xmlChar *name, const xmlChar *content) { |
| 2306 | xmlNodePtr cur; |
| 2307 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2308 | if ((doc != NULL) && (doc->dict != NULL)) |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 2309 | cur = xmlNewNodeEatName(ns, (xmlChar *) |
| 2310 | xmlDictLookup(doc->dict, name, -1)); |
| 2311 | else |
| 2312 | cur = xmlNewNode(ns, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2313 | if (cur != NULL) { |
| 2314 | cur->doc = doc; |
| 2315 | if (content != NULL) { |
| 2316 | cur->children = xmlStringGetNodeList(doc, content); |
| 2317 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2318 | } |
| 2319 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2320 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2321 | return(cur); |
| 2322 | } |
| 2323 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2324 | /** |
| 2325 | * xmlNewDocNodeEatName: |
| 2326 | * @doc: the document |
| 2327 | * @ns: namespace if any |
| 2328 | * @name: the node name |
| 2329 | * @content: the XML text content if any |
| 2330 | * |
| 2331 | * Creation of a new node element within a document. @ns and @content |
| 2332 | * are optional (NULL). |
| 2333 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 2334 | * references, but XML special chars need to be escaped first by using |
| 2335 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 2336 | * need entities support. |
| 2337 | * |
| 2338 | * Returns a pointer to the new node object. |
| 2339 | */ |
| 2340 | xmlNodePtr |
| 2341 | xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns, |
| 2342 | xmlChar *name, const xmlChar *content) { |
| 2343 | xmlNodePtr cur; |
| 2344 | |
| 2345 | cur = xmlNewNodeEatName(ns, name); |
| 2346 | if (cur != NULL) { |
| 2347 | cur->doc = doc; |
| 2348 | if (content != NULL) { |
| 2349 | cur->children = xmlStringGetNodeList(doc, content); |
| 2350 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2351 | } |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 2352 | } else { |
| 2353 | /* if name don't come from the doc dictionnary free it here */ |
| 2354 | if ((name != NULL) && (doc != NULL) && |
| 2355 | (!(xmlDictOwns(doc->dict, name)))) |
| 2356 | xmlFree(name); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2357 | } |
| 2358 | return(cur); |
| 2359 | } |
| 2360 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2361 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2362 | /** |
| 2363 | * xmlNewDocRawNode: |
| 2364 | * @doc: the document |
| 2365 | * @ns: namespace if any |
| 2366 | * @name: the node name |
| 2367 | * @content: the text content if any |
| 2368 | * |
| 2369 | * Creation of a new node element within a document. @ns and @content |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2370 | * are optional (NULL). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2371 | * |
| 2372 | * Returns a pointer to the new node object. |
| 2373 | */ |
| 2374 | xmlNodePtr |
| 2375 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 2376 | const xmlChar *name, const xmlChar *content) { |
| 2377 | xmlNodePtr cur; |
| 2378 | |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 2379 | cur = xmlNewDocNode(doc, ns, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2380 | if (cur != NULL) { |
| 2381 | cur->doc = doc; |
| 2382 | if (content != NULL) { |
| 2383 | cur->children = xmlNewDocText(doc, content); |
| 2384 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 2385 | } |
| 2386 | } |
| 2387 | return(cur); |
| 2388 | } |
| 2389 | |
| 2390 | /** |
| 2391 | * xmlNewDocFragment: |
| 2392 | * @doc: the document owning the fragment |
| 2393 | * |
| 2394 | * Creation of a new Fragment node. |
| 2395 | * Returns a pointer to the new node object. |
| 2396 | */ |
| 2397 | xmlNodePtr |
| 2398 | xmlNewDocFragment(xmlDocPtr doc) { |
| 2399 | xmlNodePtr cur; |
| 2400 | |
| 2401 | /* |
| 2402 | * Allocate a new DocumentFragment node and fill the fields. |
| 2403 | */ |
| 2404 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2405 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2406 | xmlTreeErrMemory("building fragment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2407 | return(NULL); |
| 2408 | } |
| 2409 | memset(cur, 0, sizeof(xmlNode)); |
| 2410 | cur->type = XML_DOCUMENT_FRAG_NODE; |
| 2411 | |
| 2412 | cur->doc = doc; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2413 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2414 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2415 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2416 | return(cur); |
| 2417 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2418 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2419 | |
| 2420 | /** |
| 2421 | * xmlNewText: |
| 2422 | * @content: the text content |
| 2423 | * |
| 2424 | * Creation of a new text node. |
| 2425 | * Returns a pointer to the new node object. |
| 2426 | */ |
| 2427 | xmlNodePtr |
| 2428 | xmlNewText(const xmlChar *content) { |
| 2429 | xmlNodePtr cur; |
| 2430 | |
| 2431 | /* |
| 2432 | * Allocate a new node and fill the fields. |
| 2433 | */ |
| 2434 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2435 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2436 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2437 | return(NULL); |
| 2438 | } |
| 2439 | memset(cur, 0, sizeof(xmlNode)); |
| 2440 | cur->type = XML_TEXT_NODE; |
| 2441 | |
| 2442 | cur->name = xmlStringText; |
| 2443 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2444 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2445 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2446 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2447 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2448 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2449 | return(cur); |
| 2450 | } |
| 2451 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2452 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2453 | /** |
| 2454 | * xmlNewTextChild: |
| 2455 | * @parent: the parent node |
| 2456 | * @ns: a namespace if any |
| 2457 | * @name: the name of the child |
| 2458 | * @content: the text content of the child if any. |
| 2459 | * |
| 2460 | * 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] | 2461 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2462 | * 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] | 2463 | * 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] | 2464 | * NOTE: Use xmlNewChild() if @content will contain entities that need to be |
| 2465 | * preserved. Use this function, xmlNewTextChild(), if you need to ensure that |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2466 | * reserved XML chars that might appear in @content, such as the ampersand, |
| 2467 | * greater-than or less-than signs, are automatically replaced by their XML |
| 2468 | * escaped entity representations. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2469 | * |
| 2470 | * Returns a pointer to the new node object. |
| 2471 | */ |
| 2472 | xmlNodePtr |
| 2473 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2474 | const xmlChar *name, const xmlChar *content) { |
| 2475 | xmlNodePtr cur, prev; |
| 2476 | |
| 2477 | if (parent == NULL) { |
| 2478 | #ifdef DEBUG_TREE |
| 2479 | xmlGenericError(xmlGenericErrorContext, |
| 2480 | "xmlNewTextChild : parent == NULL\n"); |
| 2481 | #endif |
| 2482 | return(NULL); |
| 2483 | } |
| 2484 | |
| 2485 | if (name == NULL) { |
| 2486 | #ifdef DEBUG_TREE |
| 2487 | xmlGenericError(xmlGenericErrorContext, |
| 2488 | "xmlNewTextChild : name == NULL\n"); |
| 2489 | #endif |
| 2490 | return(NULL); |
| 2491 | } |
| 2492 | |
| 2493 | /* |
| 2494 | * Allocate a new node |
| 2495 | */ |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 2496 | if (parent->type == XML_ELEMENT_NODE) { |
| 2497 | if (ns == NULL) |
| 2498 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 2499 | else |
| 2500 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 2501 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2502 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2503 | if (ns == NULL) |
| 2504 | cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content); |
| 2505 | else |
| 2506 | cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content); |
| 2507 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2508 | cur = xmlNewDocRawNode( parent->doc, ns, name, content); |
| 2509 | } else { |
| 2510 | return(NULL); |
| 2511 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2512 | if (cur == NULL) return(NULL); |
| 2513 | |
| 2514 | /* |
| 2515 | * add the new element at the end of the children list. |
| 2516 | */ |
| 2517 | cur->type = XML_ELEMENT_NODE; |
| 2518 | cur->parent = parent; |
| 2519 | cur->doc = parent->doc; |
| 2520 | if (parent->children == NULL) { |
| 2521 | parent->children = cur; |
| 2522 | parent->last = cur; |
| 2523 | } else { |
| 2524 | prev = parent->last; |
| 2525 | prev->next = cur; |
| 2526 | cur->prev = prev; |
| 2527 | parent->last = cur; |
| 2528 | } |
| 2529 | |
| 2530 | return(cur); |
| 2531 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2532 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2533 | |
| 2534 | /** |
| 2535 | * xmlNewCharRef: |
| 2536 | * @doc: the document |
| 2537 | * @name: the char ref string, starting with # or "&# ... ;" |
| 2538 | * |
| 2539 | * Creation of a new character reference node. |
| 2540 | * Returns a pointer to the new node object. |
| 2541 | */ |
| 2542 | xmlNodePtr |
| 2543 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 2544 | xmlNodePtr cur; |
| 2545 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2546 | if (name == NULL) |
| 2547 | return(NULL); |
| 2548 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2549 | /* |
| 2550 | * Allocate a new node and fill the fields. |
| 2551 | */ |
| 2552 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2553 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2554 | xmlTreeErrMemory("building character reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2555 | return(NULL); |
| 2556 | } |
| 2557 | memset(cur, 0, sizeof(xmlNode)); |
| 2558 | cur->type = XML_ENTITY_REF_NODE; |
| 2559 | |
| 2560 | cur->doc = doc; |
| 2561 | if (name[0] == '&') { |
| 2562 | int len; |
| 2563 | name++; |
| 2564 | len = xmlStrlen(name); |
| 2565 | if (name[len - 1] == ';') |
| 2566 | cur->name = xmlStrndup(name, len - 1); |
| 2567 | else |
| 2568 | cur->name = xmlStrndup(name, len); |
| 2569 | } else |
| 2570 | cur->name = xmlStrdup(name); |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2571 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2572 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2573 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2574 | return(cur); |
| 2575 | } |
| 2576 | |
| 2577 | /** |
| 2578 | * xmlNewReference: |
| 2579 | * @doc: the document |
| 2580 | * @name: the reference name, or the reference string with & and ; |
| 2581 | * |
| 2582 | * Creation of a new reference node. |
| 2583 | * Returns a pointer to the new node object. |
| 2584 | */ |
| 2585 | xmlNodePtr |
| 2586 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
| 2587 | xmlNodePtr cur; |
| 2588 | xmlEntityPtr ent; |
| 2589 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 2590 | if (name == NULL) |
| 2591 | return(NULL); |
| 2592 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2593 | /* |
| 2594 | * Allocate a new node and fill the fields. |
| 2595 | */ |
| 2596 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2597 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2598 | xmlTreeErrMemory("building reference"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2599 | return(NULL); |
| 2600 | } |
| 2601 | memset(cur, 0, sizeof(xmlNode)); |
| 2602 | cur->type = XML_ENTITY_REF_NODE; |
| 2603 | |
| 2604 | cur->doc = doc; |
| 2605 | if (name[0] == '&') { |
| 2606 | int len; |
| 2607 | name++; |
| 2608 | len = xmlStrlen(name); |
| 2609 | if (name[len - 1] == ';') |
| 2610 | cur->name = xmlStrndup(name, len - 1); |
| 2611 | else |
| 2612 | cur->name = xmlStrndup(name, len); |
| 2613 | } else |
| 2614 | cur->name = xmlStrdup(name); |
| 2615 | |
| 2616 | ent = xmlGetDocEntity(doc, cur->name); |
| 2617 | if (ent != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2618 | cur->content = ent->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2619 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2620 | * 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] | 2621 | * updated. Not sure if this is 100% correct. |
| 2622 | * -George |
| 2623 | */ |
| 2624 | cur->children = (xmlNodePtr) ent; |
| 2625 | cur->last = (xmlNodePtr) ent; |
| 2626 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2627 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2628 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2629 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2630 | return(cur); |
| 2631 | } |
| 2632 | |
| 2633 | /** |
| 2634 | * xmlNewDocText: |
| 2635 | * @doc: the document |
| 2636 | * @content: the text content |
| 2637 | * |
| 2638 | * Creation of a new text node within a document. |
| 2639 | * Returns a pointer to the new node object. |
| 2640 | */ |
| 2641 | xmlNodePtr |
| 2642 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
| 2643 | xmlNodePtr cur; |
| 2644 | |
| 2645 | cur = xmlNewText(content); |
| 2646 | if (cur != NULL) cur->doc = doc; |
| 2647 | return(cur); |
| 2648 | } |
| 2649 | |
| 2650 | /** |
| 2651 | * xmlNewTextLen: |
| 2652 | * @content: the text content |
| 2653 | * @len: the text len. |
| 2654 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2655 | * 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] | 2656 | * Returns a pointer to the new node object. |
| 2657 | */ |
| 2658 | xmlNodePtr |
| 2659 | xmlNewTextLen(const xmlChar *content, int len) { |
| 2660 | xmlNodePtr cur; |
| 2661 | |
| 2662 | /* |
| 2663 | * Allocate a new node and fill the fields. |
| 2664 | */ |
| 2665 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2666 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2667 | xmlTreeErrMemory("building text"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2668 | return(NULL); |
| 2669 | } |
| 2670 | memset(cur, 0, sizeof(xmlNode)); |
| 2671 | cur->type = XML_TEXT_NODE; |
| 2672 | |
| 2673 | cur->name = xmlStringText; |
| 2674 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2675 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2676 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2677 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2678 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2679 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2680 | return(cur); |
| 2681 | } |
| 2682 | |
| 2683 | /** |
| 2684 | * xmlNewDocTextLen: |
| 2685 | * @doc: the document |
| 2686 | * @content: the text content |
| 2687 | * @len: the text len. |
| 2688 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 2689 | * 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] | 2690 | * text node pertain to a given document. |
| 2691 | * Returns a pointer to the new node object. |
| 2692 | */ |
| 2693 | xmlNodePtr |
| 2694 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2695 | xmlNodePtr cur; |
| 2696 | |
| 2697 | cur = xmlNewTextLen(content, len); |
| 2698 | if (cur != NULL) cur->doc = doc; |
| 2699 | return(cur); |
| 2700 | } |
| 2701 | |
| 2702 | /** |
| 2703 | * xmlNewComment: |
| 2704 | * @content: the comment content |
| 2705 | * |
| 2706 | * Creation of a new node containing a comment. |
| 2707 | * Returns a pointer to the new node object. |
| 2708 | */ |
| 2709 | xmlNodePtr |
| 2710 | xmlNewComment(const xmlChar *content) { |
| 2711 | xmlNodePtr cur; |
| 2712 | |
| 2713 | /* |
| 2714 | * Allocate a new node and fill the fields. |
| 2715 | */ |
| 2716 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2717 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2718 | xmlTreeErrMemory("building comment"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2719 | return(NULL); |
| 2720 | } |
| 2721 | memset(cur, 0, sizeof(xmlNode)); |
| 2722 | cur->type = XML_COMMENT_NODE; |
| 2723 | |
| 2724 | cur->name = xmlStringComment; |
| 2725 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2726 | cur->content = xmlStrdup(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2727 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2728 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2729 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2730 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2731 | return(cur); |
| 2732 | } |
| 2733 | |
| 2734 | /** |
| 2735 | * xmlNewCDataBlock: |
| 2736 | * @doc: the document |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2737 | * @content: the CDATA block content content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2738 | * @len: the length of the block |
| 2739 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2740 | * Creation of a new node containing a CDATA block. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2741 | * Returns a pointer to the new node object. |
| 2742 | */ |
| 2743 | xmlNodePtr |
| 2744 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
| 2745 | xmlNodePtr cur; |
| 2746 | |
| 2747 | /* |
| 2748 | * Allocate a new node and fill the fields. |
| 2749 | */ |
| 2750 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 2751 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 2752 | xmlTreeErrMemory("building CDATA"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2753 | return(NULL); |
| 2754 | } |
| 2755 | memset(cur, 0, sizeof(xmlNode)); |
| 2756 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2757 | cur->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2758 | |
| 2759 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2760 | cur->content = xmlStrndup(content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2761 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2762 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2763 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 2764 | xmlRegisterNodeDefaultValue(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2765 | return(cur); |
| 2766 | } |
| 2767 | |
| 2768 | /** |
| 2769 | * xmlNewDocComment: |
| 2770 | * @doc: the document |
| 2771 | * @content: the comment content |
| 2772 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2773 | * Creation of a new node containing a comment within a document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2774 | * Returns a pointer to the new node object. |
| 2775 | */ |
| 2776 | xmlNodePtr |
| 2777 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
| 2778 | xmlNodePtr cur; |
| 2779 | |
| 2780 | cur = xmlNewComment(content); |
| 2781 | if (cur != NULL) cur->doc = doc; |
| 2782 | return(cur); |
| 2783 | } |
| 2784 | |
| 2785 | /** |
| 2786 | * xmlSetTreeDoc: |
| 2787 | * @tree: the top element |
| 2788 | * @doc: the document |
| 2789 | * |
| 2790 | * update all nodes under the tree to point to the right document |
| 2791 | */ |
| 2792 | void |
| 2793 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2794 | xmlAttrPtr prop; |
| 2795 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 2796 | if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2797 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2798 | if (tree->doc != doc) { |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2799 | if(tree->type == XML_ELEMENT_NODE) { |
| 2800 | prop = tree->properties; |
| 2801 | while (prop != NULL) { |
| 2802 | prop->doc = doc; |
| 2803 | xmlSetListDoc(prop->children, doc); |
| 2804 | prop = prop->next; |
| 2805 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2806 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2807 | if (tree->children != NULL) |
| 2808 | xmlSetListDoc(tree->children, doc); |
| 2809 | tree->doc = doc; |
| 2810 | } |
| 2811 | } |
| 2812 | |
| 2813 | /** |
| 2814 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2815 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2816 | * @doc: the document |
| 2817 | * |
| 2818 | * update all nodes in the list to point to the right document |
| 2819 | */ |
| 2820 | void |
| 2821 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2822 | xmlNodePtr cur; |
| 2823 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 2824 | if ((list == NULL) || (list->type == XML_NAMESPACE_DECL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2825 | return; |
| 2826 | cur = list; |
| 2827 | while (cur != NULL) { |
| 2828 | if (cur->doc != doc) |
| 2829 | xmlSetTreeDoc(cur, doc); |
| 2830 | cur = cur->next; |
| 2831 | } |
| 2832 | } |
| 2833 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 2834 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2835 | /** |
| 2836 | * xmlNewChild: |
| 2837 | * @parent: the parent node |
| 2838 | * @ns: a namespace if any |
| 2839 | * @name: the name of the child |
| 2840 | * @content: the XML content of the child if any. |
| 2841 | * |
| 2842 | * 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] | 2843 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2844 | * created element inherits the namespace of @parent. If @content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2845 | * 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] | 2846 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 2847 | * references. XML special chars must be escaped first by using |
| 2848 | * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2849 | * |
| 2850 | * Returns a pointer to the new node object. |
| 2851 | */ |
| 2852 | xmlNodePtr |
| 2853 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2854 | const xmlChar *name, const xmlChar *content) { |
| 2855 | xmlNodePtr cur, prev; |
| 2856 | |
| 2857 | if (parent == NULL) { |
| 2858 | #ifdef DEBUG_TREE |
| 2859 | xmlGenericError(xmlGenericErrorContext, |
| 2860 | "xmlNewChild : parent == NULL\n"); |
| 2861 | #endif |
| 2862 | return(NULL); |
| 2863 | } |
| 2864 | |
| 2865 | if (name == NULL) { |
| 2866 | #ifdef DEBUG_TREE |
| 2867 | xmlGenericError(xmlGenericErrorContext, |
| 2868 | "xmlNewChild : name == NULL\n"); |
| 2869 | #endif |
| 2870 | return(NULL); |
| 2871 | } |
| 2872 | |
| 2873 | /* |
| 2874 | * Allocate a new node |
| 2875 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2876 | if (parent->type == XML_ELEMENT_NODE) { |
| 2877 | if (ns == NULL) |
| 2878 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2879 | else |
| 2880 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2881 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2882 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2883 | if (ns == NULL) |
| 2884 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2885 | else |
| 2886 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2887 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2888 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2889 | } else { |
| 2890 | return(NULL); |
| 2891 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2892 | if (cur == NULL) return(NULL); |
| 2893 | |
| 2894 | /* |
| 2895 | * add the new element at the end of the children list. |
| 2896 | */ |
| 2897 | cur->type = XML_ELEMENT_NODE; |
| 2898 | cur->parent = parent; |
| 2899 | cur->doc = parent->doc; |
| 2900 | if (parent->children == NULL) { |
| 2901 | parent->children = cur; |
| 2902 | parent->last = cur; |
| 2903 | } else { |
| 2904 | prev = parent->last; |
| 2905 | prev->next = cur; |
| 2906 | cur->prev = prev; |
| 2907 | parent->last = cur; |
| 2908 | } |
| 2909 | |
| 2910 | return(cur); |
| 2911 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2912 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2913 | |
| 2914 | /** |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2915 | * xmlAddPropSibling: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2916 | * @prev: the attribute to which @prop is added after |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2917 | * @cur: the base attribute passed to calling function |
| 2918 | * @prop: the new attribute |
| 2919 | * |
| 2920 | * Add a new attribute after @prev using @cur as base attribute. |
| 2921 | * When inserting before @cur, @prev is passed as @cur->prev. |
| 2922 | * When inserting after @cur, @prev is passed as @cur. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2923 | * 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] | 2924 | * |
| 2925 | * Returns the attribute being inserted or NULL in case of error. |
| 2926 | */ |
| 2927 | static xmlNodePtr |
| 2928 | xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) { |
| 2929 | xmlAttrPtr attr; |
| 2930 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 2931 | if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) || |
| 2932 | (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) || |
| 2933 | ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE))) |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2934 | return(NULL); |
| 2935 | |
| 2936 | /* check if an attribute with the same name exists */ |
| 2937 | if (prop->ns == NULL) |
| 2938 | attr = xmlHasNsProp(cur->parent, prop->name, NULL); |
| 2939 | else |
| 2940 | attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href); |
| 2941 | |
| 2942 | if (prop->doc != cur->doc) { |
| 2943 | xmlSetTreeDoc(prop, cur->doc); |
| 2944 | } |
| 2945 | prop->parent = cur->parent; |
| 2946 | prop->prev = prev; |
| 2947 | if (prev != NULL) { |
| 2948 | prop->next = prev->next; |
| 2949 | prev->next = prop; |
| 2950 | if (prop->next) |
| 2951 | prop->next->prev = prop; |
| 2952 | } else { |
| 2953 | prop->next = cur; |
| 2954 | cur->prev = prop; |
| 2955 | } |
| 2956 | if (prop->prev == NULL && prop->parent != NULL) |
| 2957 | prop->parent->properties = (xmlAttrPtr) prop; |
| 2958 | if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) { |
| 2959 | /* different instance, destroy it (attributes must be unique) */ |
| 2960 | xmlRemoveProp((xmlAttrPtr) attr); |
| 2961 | } |
| 2962 | return prop; |
| 2963 | } |
| 2964 | |
| 2965 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2966 | * xmlAddNextSibling: |
| 2967 | * @cur: the child node |
| 2968 | * @elem: the new node |
| 2969 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2970 | * Add a new node @elem as the next sibling of @cur |
| 2971 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2972 | * first unlinked from its existing context. |
| 2973 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2974 | * 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] | 2975 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2976 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2977 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2978 | */ |
| 2979 | xmlNodePtr |
| 2980 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 2981 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2982 | #ifdef DEBUG_TREE |
| 2983 | xmlGenericError(xmlGenericErrorContext, |
| 2984 | "xmlAddNextSibling : cur == NULL\n"); |
| 2985 | #endif |
| 2986 | return(NULL); |
| 2987 | } |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 2988 | if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2989 | #ifdef DEBUG_TREE |
| 2990 | xmlGenericError(xmlGenericErrorContext, |
| 2991 | "xmlAddNextSibling : elem == NULL\n"); |
| 2992 | #endif |
| 2993 | return(NULL); |
| 2994 | } |
| 2995 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 2996 | if (cur == elem) { |
| 2997 | #ifdef DEBUG_TREE |
| 2998 | xmlGenericError(xmlGenericErrorContext, |
| 2999 | "xmlAddNextSibling : cur == elem\n"); |
| 3000 | #endif |
| 3001 | return(NULL); |
| 3002 | } |
| 3003 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3004 | xmlUnlinkNode(elem); |
| 3005 | |
| 3006 | if (elem->type == XML_TEXT_NODE) { |
| 3007 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3008 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3009 | xmlFreeNode(elem); |
| 3010 | return(cur); |
| 3011 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 3012 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 3013 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3014 | xmlChar *tmp; |
| 3015 | |
| 3016 | tmp = xmlStrdup(elem->content); |
| 3017 | tmp = xmlStrcat(tmp, cur->next->content); |
| 3018 | xmlNodeSetContent(cur->next, tmp); |
| 3019 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3020 | xmlFreeNode(elem); |
| 3021 | return(cur->next); |
| 3022 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3023 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3024 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | if (elem->doc != cur->doc) { |
| 3028 | xmlSetTreeDoc(elem, cur->doc); |
| 3029 | } |
| 3030 | elem->parent = cur->parent; |
| 3031 | elem->prev = cur; |
| 3032 | elem->next = cur->next; |
| 3033 | cur->next = elem; |
| 3034 | if (elem->next != NULL) |
| 3035 | elem->next->prev = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3036 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3037 | elem->parent->last = elem; |
| 3038 | return(elem); |
| 3039 | } |
| 3040 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 3041 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
Nicolas Le Cam | 41586ca | 2013-06-17 13:01:33 +0200 | [diff] [blame] | 3042 | defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3043 | /** |
| 3044 | * xmlAddPrevSibling: |
| 3045 | * @cur: the child node |
| 3046 | * @elem: the new node |
| 3047 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3048 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3049 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3050 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3051 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3052 | * 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] | 3053 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3054 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3055 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3056 | */ |
| 3057 | xmlNodePtr |
| 3058 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3059 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3060 | #ifdef DEBUG_TREE |
| 3061 | xmlGenericError(xmlGenericErrorContext, |
| 3062 | "xmlAddPrevSibling : cur == NULL\n"); |
| 3063 | #endif |
| 3064 | return(NULL); |
| 3065 | } |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3066 | if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3067 | #ifdef DEBUG_TREE |
| 3068 | xmlGenericError(xmlGenericErrorContext, |
| 3069 | "xmlAddPrevSibling : elem == NULL\n"); |
| 3070 | #endif |
| 3071 | return(NULL); |
| 3072 | } |
| 3073 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3074 | if (cur == elem) { |
| 3075 | #ifdef DEBUG_TREE |
| 3076 | xmlGenericError(xmlGenericErrorContext, |
| 3077 | "xmlAddPrevSibling : cur == elem\n"); |
| 3078 | #endif |
| 3079 | return(NULL); |
| 3080 | } |
| 3081 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3082 | xmlUnlinkNode(elem); |
| 3083 | |
| 3084 | if (elem->type == XML_TEXT_NODE) { |
| 3085 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3086 | xmlChar *tmp; |
| 3087 | |
| 3088 | tmp = xmlStrdup(elem->content); |
| 3089 | tmp = xmlStrcat(tmp, cur->content); |
| 3090 | xmlNodeSetContent(cur, tmp); |
| 3091 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3092 | xmlFreeNode(elem); |
| 3093 | return(cur); |
| 3094 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 3095 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 3096 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3097 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3098 | xmlFreeNode(elem); |
| 3099 | return(cur->prev); |
| 3100 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3101 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3102 | return xmlAddPropSibling(cur->prev, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | if (elem->doc != cur->doc) { |
| 3106 | xmlSetTreeDoc(elem, cur->doc); |
| 3107 | } |
| 3108 | elem->parent = cur->parent; |
| 3109 | elem->next = cur; |
| 3110 | elem->prev = cur->prev; |
| 3111 | cur->prev = elem; |
| 3112 | if (elem->prev != NULL) |
| 3113 | elem->prev->next = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3114 | if ((elem->parent != NULL) && (elem->parent->children == cur)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3115 | elem->parent->children = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3116 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3117 | return(elem); |
| 3118 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3119 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3120 | |
| 3121 | /** |
| 3122 | * xmlAddSibling: |
| 3123 | * @cur: the child node |
| 3124 | * @elem: the new node |
| 3125 | * |
| 3126 | * Add a new element @elem to the list of siblings of @cur |
| 3127 | * merging adjacent TEXT nodes (@elem may be freed) |
| 3128 | * If the new element was already inserted in a document it is |
| 3129 | * first unlinked from its existing context. |
| 3130 | * |
| 3131 | * Returns the new element or NULL in case of error. |
| 3132 | */ |
| 3133 | xmlNodePtr |
| 3134 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 3135 | xmlNodePtr parent; |
| 3136 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3137 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3138 | #ifdef DEBUG_TREE |
| 3139 | xmlGenericError(xmlGenericErrorContext, |
| 3140 | "xmlAddSibling : cur == NULL\n"); |
| 3141 | #endif |
| 3142 | return(NULL); |
| 3143 | } |
| 3144 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3145 | if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3146 | #ifdef DEBUG_TREE |
| 3147 | xmlGenericError(xmlGenericErrorContext, |
| 3148 | "xmlAddSibling : elem == NULL\n"); |
| 3149 | #endif |
| 3150 | return(NULL); |
| 3151 | } |
| 3152 | |
Daniel Veillard | 43bc89c | 2009-03-23 19:32:04 +0000 | [diff] [blame] | 3153 | if (cur == elem) { |
| 3154 | #ifdef DEBUG_TREE |
| 3155 | xmlGenericError(xmlGenericErrorContext, |
| 3156 | "xmlAddSibling : cur == elem\n"); |
| 3157 | #endif |
| 3158 | return(NULL); |
| 3159 | } |
| 3160 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3161 | /* |
| 3162 | * Constant time is we can rely on the ->parent->last to find |
| 3163 | * the last sibling. |
| 3164 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3165 | if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3166 | (cur->parent->children != NULL) && |
| 3167 | (cur->parent->last != NULL) && |
| 3168 | (cur->parent->last->next == NULL)) { |
| 3169 | cur = cur->parent->last; |
| 3170 | } else { |
| 3171 | while (cur->next != NULL) cur = cur->next; |
| 3172 | } |
| 3173 | |
| 3174 | xmlUnlinkNode(elem); |
| 3175 | |
Daniel Veillard | e22dd5c | 2003-10-29 12:53:27 +0000 | [diff] [blame] | 3176 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) && |
| 3177 | (cur->name == elem->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3178 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3179 | xmlFreeNode(elem); |
| 3180 | return(cur); |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3181 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 3182 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | if (elem->doc != cur->doc) { |
| 3186 | xmlSetTreeDoc(elem, cur->doc); |
| 3187 | } |
| 3188 | parent = cur->parent; |
| 3189 | elem->prev = cur; |
| 3190 | elem->next = NULL; |
| 3191 | elem->parent = parent; |
| 3192 | cur->next = elem; |
| 3193 | if (parent != NULL) |
| 3194 | parent->last = elem; |
| 3195 | |
| 3196 | return(elem); |
| 3197 | } |
| 3198 | |
| 3199 | /** |
| 3200 | * xmlAddChildList: |
| 3201 | * @parent: the parent node |
| 3202 | * @cur: the first node in the list |
| 3203 | * |
| 3204 | * Add a list of node at the end of the child list of the parent |
| 3205 | * merging adjacent TEXT nodes (@cur may be freed) |
| 3206 | * |
| 3207 | * Returns the last child or NULL in case of error. |
| 3208 | */ |
| 3209 | xmlNodePtr |
| 3210 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 3211 | xmlNodePtr prev; |
| 3212 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3213 | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3214 | #ifdef DEBUG_TREE |
| 3215 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3216 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3217 | #endif |
| 3218 | return(NULL); |
| 3219 | } |
| 3220 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3221 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3222 | #ifdef DEBUG_TREE |
| 3223 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3224 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3225 | #endif |
| 3226 | return(NULL); |
| 3227 | } |
| 3228 | |
| 3229 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 3230 | (cur->doc != parent->doc)) { |
| 3231 | #ifdef DEBUG_TREE |
| 3232 | xmlGenericError(xmlGenericErrorContext, |
| 3233 | "Elements moved to a different document\n"); |
| 3234 | #endif |
| 3235 | } |
| 3236 | |
| 3237 | /* |
| 3238 | * add the first element at the end of the children list. |
| 3239 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3240 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3241 | if (parent->children == NULL) { |
| 3242 | parent->children = cur; |
| 3243 | } else { |
| 3244 | /* |
| 3245 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 3246 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3247 | if ((cur->type == XML_TEXT_NODE) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3248 | (parent->last->type == XML_TEXT_NODE) && |
| 3249 | (cur->name == parent->last->name)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3250 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3251 | /* |
| 3252 | * if it's the only child, nothing more to be done. |
| 3253 | */ |
| 3254 | if (cur->next == NULL) { |
| 3255 | xmlFreeNode(cur); |
| 3256 | return(parent->last); |
| 3257 | } |
| 3258 | prev = cur; |
| 3259 | cur = cur->next; |
| 3260 | xmlFreeNode(prev); |
| 3261 | } |
| 3262 | prev = parent->last; |
| 3263 | prev->next = cur; |
| 3264 | cur->prev = prev; |
| 3265 | } |
| 3266 | while (cur->next != NULL) { |
| 3267 | cur->parent = parent; |
| 3268 | if (cur->doc != parent->doc) { |
| 3269 | xmlSetTreeDoc(cur, parent->doc); |
| 3270 | } |
| 3271 | cur = cur->next; |
| 3272 | } |
| 3273 | cur->parent = parent; |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3274 | /* the parent may not be linked to a doc ! */ |
| 3275 | if (cur->doc != parent->doc) { |
| 3276 | xmlSetTreeDoc(cur, parent->doc); |
| 3277 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3278 | parent->last = cur; |
| 3279 | |
| 3280 | return(cur); |
| 3281 | } |
| 3282 | |
| 3283 | /** |
| 3284 | * xmlAddChild: |
| 3285 | * @parent: the parent node |
| 3286 | * @cur: the child node |
| 3287 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3288 | * 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] | 3289 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3290 | * 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] | 3291 | * If there is an attribute with equal name, it is first destroyed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3292 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3293 | * Returns the child or NULL in case of error. |
| 3294 | */ |
| 3295 | xmlNodePtr |
| 3296 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 3297 | xmlNodePtr prev; |
| 3298 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3299 | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3300 | #ifdef DEBUG_TREE |
| 3301 | xmlGenericError(xmlGenericErrorContext, |
| 3302 | "xmlAddChild : parent == NULL\n"); |
| 3303 | #endif |
| 3304 | return(NULL); |
| 3305 | } |
| 3306 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3307 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3308 | #ifdef DEBUG_TREE |
| 3309 | xmlGenericError(xmlGenericErrorContext, |
| 3310 | "xmlAddChild : child == NULL\n"); |
| 3311 | #endif |
| 3312 | return(NULL); |
| 3313 | } |
| 3314 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3315 | if (parent == cur) { |
| 3316 | #ifdef DEBUG_TREE |
| 3317 | xmlGenericError(xmlGenericErrorContext, |
| 3318 | "xmlAddChild : parent == cur\n"); |
| 3319 | #endif |
| 3320 | return(NULL); |
| 3321 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3322 | /* |
| 3323 | * 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] | 3324 | * cur is then freed. |
| 3325 | */ |
| 3326 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3327 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3328 | (parent->content != NULL) && |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3329 | (parent->name == cur->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3330 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3331 | xmlFreeNode(cur); |
| 3332 | return(parent); |
| 3333 | } |
| 3334 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3335 | (parent->last->name == cur->name) && |
| 3336 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3337 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3338 | xmlFreeNode(cur); |
| 3339 | return(parent->last); |
| 3340 | } |
| 3341 | } |
| 3342 | |
| 3343 | /* |
| 3344 | * add the new element at the end of the children list. |
| 3345 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3346 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3347 | cur->parent = parent; |
| 3348 | if (cur->doc != parent->doc) { |
| 3349 | xmlSetTreeDoc(cur, parent->doc); |
| 3350 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3351 | /* this check prevents a loop on tree-traversions if a developer |
| 3352 | * tries to add a node to its parent multiple times |
| 3353 | */ |
| 3354 | if (prev == parent) |
| 3355 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3356 | |
| 3357 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3358 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3359 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3360 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3361 | (parent->content != NULL) && |
| 3362 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3363 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3364 | xmlFreeNode(cur); |
| 3365 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3366 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3367 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3368 | if (parent->type != XML_ELEMENT_NODE) |
| 3369 | return(NULL); |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3370 | if (parent->properties != NULL) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3371 | /* check if an attribute with the same name exists */ |
| 3372 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3373 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3374 | if (cur->ns == NULL) |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3375 | lastattr = xmlHasNsProp(parent, cur->name, NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3376 | else |
| 3377 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3378 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3379 | /* different instance, destroy it (attributes must be unique) */ |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3380 | xmlUnlinkNode((xmlNodePtr) lastattr); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3381 | xmlFreeProp(lastattr); |
| 3382 | } |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3383 | if (lastattr == (xmlAttrPtr) cur) |
| 3384 | return(cur); |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3385 | |
| 3386 | } |
| 3387 | if (parent->properties == NULL) { |
| 3388 | parent->properties = (xmlAttrPtr) cur; |
| 3389 | } else { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3390 | /* find the end */ |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3391 | xmlAttrPtr lastattr = parent->properties; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3392 | while (lastattr->next != NULL) { |
| 3393 | lastattr = lastattr->next; |
| 3394 | } |
| 3395 | lastattr->next = (xmlAttrPtr) cur; |
| 3396 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3397 | } |
| 3398 | } else { |
| 3399 | if (parent->children == NULL) { |
| 3400 | parent->children = cur; |
| 3401 | parent->last = cur; |
| 3402 | } else { |
| 3403 | prev = parent->last; |
| 3404 | prev->next = cur; |
| 3405 | cur->prev = prev; |
| 3406 | parent->last = cur; |
| 3407 | } |
| 3408 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3409 | return(cur); |
| 3410 | } |
| 3411 | |
| 3412 | /** |
| 3413 | * xmlGetLastChild: |
| 3414 | * @parent: the parent node |
| 3415 | * |
| 3416 | * Search the last child of a node. |
| 3417 | * Returns the last child or NULL if none. |
| 3418 | */ |
| 3419 | xmlNodePtr |
| 3420 | xmlGetLastChild(xmlNodePtr parent) { |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3421 | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3422 | #ifdef DEBUG_TREE |
| 3423 | xmlGenericError(xmlGenericErrorContext, |
| 3424 | "xmlGetLastChild : parent == NULL\n"); |
| 3425 | #endif |
| 3426 | return(NULL); |
| 3427 | } |
| 3428 | return(parent->last); |
| 3429 | } |
| 3430 | |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3431 | #ifdef LIBXML_TREE_ENABLED |
| 3432 | /* |
| 3433 | * 5 interfaces from DOM ElementTraversal |
| 3434 | */ |
| 3435 | |
| 3436 | /** |
| 3437 | * xmlChildElementCount: |
| 3438 | * @parent: the parent node |
| 3439 | * |
| 3440 | * Finds the current number of child nodes of that element which are |
| 3441 | * element nodes. |
| 3442 | * Note the handling of entities references is different than in |
| 3443 | * the W3C DOM element traversal spec since we don't have back reference |
| 3444 | * from entities content to entities references. |
| 3445 | * |
| 3446 | * Returns the count of element child or 0 if not available |
| 3447 | */ |
| 3448 | unsigned long |
| 3449 | xmlChildElementCount(xmlNodePtr parent) { |
| 3450 | unsigned long ret = 0; |
| 3451 | xmlNodePtr cur = NULL; |
| 3452 | |
| 3453 | if (parent == NULL) |
| 3454 | return(0); |
| 3455 | switch (parent->type) { |
| 3456 | case XML_ELEMENT_NODE: |
| 3457 | case XML_ENTITY_NODE: |
| 3458 | case XML_DOCUMENT_NODE: |
| 3459 | case XML_HTML_DOCUMENT_NODE: |
| 3460 | cur = parent->children; |
| 3461 | break; |
| 3462 | default: |
| 3463 | return(0); |
| 3464 | } |
| 3465 | while (cur != NULL) { |
| 3466 | if (cur->type == XML_ELEMENT_NODE) |
| 3467 | ret++; |
| 3468 | cur = cur->next; |
| 3469 | } |
| 3470 | return(ret); |
| 3471 | } |
| 3472 | |
| 3473 | /** |
| 3474 | * xmlFirstElementChild: |
| 3475 | * @parent: the parent node |
| 3476 | * |
| 3477 | * Finds the first child node of that element which is a Element node |
| 3478 | * Note the handling of entities references is different than in |
| 3479 | * the W3C DOM element traversal spec since we don't have back reference |
| 3480 | * from entities content to entities references. |
| 3481 | * |
| 3482 | * Returns the first element child or NULL if not available |
| 3483 | */ |
| 3484 | xmlNodePtr |
| 3485 | xmlFirstElementChild(xmlNodePtr parent) { |
| 3486 | xmlNodePtr cur = NULL; |
| 3487 | |
| 3488 | if (parent == NULL) |
| 3489 | return(NULL); |
| 3490 | switch (parent->type) { |
| 3491 | case XML_ELEMENT_NODE: |
| 3492 | case XML_ENTITY_NODE: |
| 3493 | case XML_DOCUMENT_NODE: |
| 3494 | case XML_HTML_DOCUMENT_NODE: |
| 3495 | cur = parent->children; |
| 3496 | break; |
| 3497 | default: |
| 3498 | return(NULL); |
| 3499 | } |
| 3500 | while (cur != NULL) { |
| 3501 | if (cur->type == XML_ELEMENT_NODE) |
| 3502 | return(cur); |
| 3503 | cur = cur->next; |
| 3504 | } |
| 3505 | return(NULL); |
| 3506 | } |
| 3507 | |
| 3508 | /** |
| 3509 | * xmlLastElementChild: |
| 3510 | * @parent: the parent node |
| 3511 | * |
| 3512 | * Finds the last child node of that element which is a Element node |
| 3513 | * Note the handling of entities references is different than in |
| 3514 | * the W3C DOM element traversal spec since we don't have back reference |
| 3515 | * from entities content to entities references. |
| 3516 | * |
| 3517 | * Returns the last element child or NULL if not available |
| 3518 | */ |
| 3519 | xmlNodePtr |
| 3520 | xmlLastElementChild(xmlNodePtr parent) { |
| 3521 | xmlNodePtr cur = NULL; |
| 3522 | |
| 3523 | if (parent == NULL) |
| 3524 | return(NULL); |
| 3525 | switch (parent->type) { |
| 3526 | case XML_ELEMENT_NODE: |
| 3527 | case XML_ENTITY_NODE: |
| 3528 | case XML_DOCUMENT_NODE: |
| 3529 | case XML_HTML_DOCUMENT_NODE: |
| 3530 | cur = parent->last; |
| 3531 | break; |
| 3532 | default: |
| 3533 | return(NULL); |
| 3534 | } |
| 3535 | while (cur != NULL) { |
| 3536 | if (cur->type == XML_ELEMENT_NODE) |
| 3537 | return(cur); |
| 3538 | cur = cur->prev; |
| 3539 | } |
| 3540 | return(NULL); |
| 3541 | } |
| 3542 | |
| 3543 | /** |
| 3544 | * xmlPreviousElementSibling: |
| 3545 | * @node: the current node |
| 3546 | * |
| 3547 | * Finds the first closest previous sibling of the node which is an |
| 3548 | * element node. |
| 3549 | * Note the handling of entities references is different than in |
| 3550 | * the W3C DOM element traversal spec since we don't have back reference |
| 3551 | * from entities content to entities references. |
| 3552 | * |
| 3553 | * Returns the previous element sibling or NULL if not available |
| 3554 | */ |
| 3555 | xmlNodePtr |
| 3556 | xmlPreviousElementSibling(xmlNodePtr node) { |
| 3557 | if (node == NULL) |
| 3558 | return(NULL); |
| 3559 | switch (node->type) { |
| 3560 | case XML_ELEMENT_NODE: |
| 3561 | case XML_TEXT_NODE: |
| 3562 | case XML_CDATA_SECTION_NODE: |
| 3563 | case XML_ENTITY_REF_NODE: |
| 3564 | case XML_ENTITY_NODE: |
| 3565 | case XML_PI_NODE: |
| 3566 | case XML_COMMENT_NODE: |
| 3567 | case XML_XINCLUDE_START: |
| 3568 | case XML_XINCLUDE_END: |
| 3569 | node = node->prev; |
| 3570 | break; |
| 3571 | default: |
| 3572 | return(NULL); |
| 3573 | } |
| 3574 | while (node != NULL) { |
| 3575 | if (node->type == XML_ELEMENT_NODE) |
| 3576 | return(node); |
François Delyon | 2f70090 | 2010-02-03 17:32:37 +0100 | [diff] [blame] | 3577 | node = node->prev; |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3578 | } |
| 3579 | return(NULL); |
| 3580 | } |
| 3581 | |
| 3582 | /** |
| 3583 | * xmlNextElementSibling: |
| 3584 | * @node: the current node |
| 3585 | * |
| 3586 | * Finds the first closest next sibling of the node which is an |
| 3587 | * element node. |
| 3588 | * Note the handling of entities references is different than in |
| 3589 | * the W3C DOM element traversal spec since we don't have back reference |
| 3590 | * from entities content to entities references. |
| 3591 | * |
| 3592 | * Returns the next element sibling or NULL if not available |
| 3593 | */ |
| 3594 | xmlNodePtr |
| 3595 | xmlNextElementSibling(xmlNodePtr node) { |
| 3596 | if (node == NULL) |
| 3597 | return(NULL); |
| 3598 | switch (node->type) { |
| 3599 | case XML_ELEMENT_NODE: |
| 3600 | case XML_TEXT_NODE: |
| 3601 | case XML_CDATA_SECTION_NODE: |
| 3602 | case XML_ENTITY_REF_NODE: |
| 3603 | case XML_ENTITY_NODE: |
| 3604 | case XML_PI_NODE: |
| 3605 | case XML_COMMENT_NODE: |
| 3606 | case XML_DTD_NODE: |
| 3607 | case XML_XINCLUDE_START: |
| 3608 | case XML_XINCLUDE_END: |
| 3609 | node = node->next; |
| 3610 | break; |
| 3611 | default: |
| 3612 | return(NULL); |
| 3613 | } |
| 3614 | while (node != NULL) { |
| 3615 | if (node->type == XML_ELEMENT_NODE) |
| 3616 | return(node); |
| 3617 | node = node->next; |
| 3618 | } |
| 3619 | return(NULL); |
| 3620 | } |
| 3621 | |
| 3622 | #endif /* LIBXML_TREE_ENABLED */ |
| 3623 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3624 | /** |
| 3625 | * xmlFreeNodeList: |
| 3626 | * @cur: the first node in the list |
| 3627 | * |
| 3628 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3629 | * the children are freed too. |
| 3630 | */ |
| 3631 | void |
| 3632 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3633 | xmlNodePtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3634 | xmlDictPtr dict = NULL; |
| 3635 | |
| 3636 | if (cur == NULL) return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3637 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3638 | xmlFreeNsList((xmlNsPtr) cur); |
| 3639 | return; |
| 3640 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3641 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3642 | #ifdef LIBXML_DOCB_ENABLED |
| 3643 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3644 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3645 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3646 | xmlFreeDoc((xmlDocPtr) cur); |
| 3647 | return; |
| 3648 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3649 | if (cur->doc != NULL) dict = cur->doc->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3650 | while (cur != NULL) { |
| 3651 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3652 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3653 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3654 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3655 | xmlDeregisterNodeDefaultValue(cur); |
| 3656 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3657 | if ((cur->children != NULL) && |
| 3658 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3659 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3660 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3661 | (cur->type == XML_XINCLUDE_START) || |
| 3662 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3663 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3664 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3665 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3666 | (cur->type != XML_XINCLUDE_START) && |
| 3667 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3668 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3669 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3670 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3671 | } |
| 3672 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3673 | (cur->type == XML_XINCLUDE_START) || |
| 3674 | (cur->type == XML_XINCLUDE_END)) && |
| 3675 | (cur->nsDef != NULL)) |
| 3676 | xmlFreeNsList(cur->nsDef); |
| 3677 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3678 | /* |
| 3679 | * When a node is a text node or a comment, it uses a global static |
| 3680 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3681 | * Otherwise the node name might come from the document's |
| 3682 | * dictionnary |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3683 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3684 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3685 | (cur->type != XML_TEXT_NODE) && |
| 3686 | (cur->type != XML_COMMENT_NODE)) |
| 3687 | DICT_FREE(cur->name) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3688 | xmlFree(cur); |
| 3689 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3690 | cur = next; |
| 3691 | } |
| 3692 | } |
| 3693 | |
| 3694 | /** |
| 3695 | * xmlFreeNode: |
| 3696 | * @cur: the node |
| 3697 | * |
| 3698 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3699 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3700 | */ |
| 3701 | void |
| 3702 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3703 | xmlDictPtr dict = NULL; |
| 3704 | |
| 3705 | if (cur == NULL) return; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3706 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3707 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3708 | if (cur->type == XML_DTD_NODE) { |
| 3709 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3710 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3711 | } |
| 3712 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3713 | xmlFreeNs((xmlNsPtr) cur); |
| 3714 | return; |
| 3715 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3716 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3717 | xmlFreeProp((xmlAttrPtr) cur); |
| 3718 | return; |
| 3719 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3720 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3721 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3722 | xmlDeregisterNodeDefaultValue(cur); |
| 3723 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3724 | if (cur->doc != NULL) dict = cur->doc->dict; |
| 3725 | |
Daniel Veillard | 2cba415 | 2008-08-27 11:45:41 +0000 | [diff] [blame] | 3726 | if (cur->type == XML_ENTITY_DECL) { |
| 3727 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 3728 | DICT_FREE(ent->SystemID); |
| 3729 | DICT_FREE(ent->ExternalID); |
| 3730 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3731 | if ((cur->children != NULL) && |
| 3732 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3733 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3734 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3735 | (cur->type == XML_XINCLUDE_START) || |
| 3736 | (cur->type == XML_XINCLUDE_END)) && |
| 3737 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3738 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3739 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3740 | (cur->content != NULL) && |
| 3741 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3742 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3743 | (cur->type != XML_XINCLUDE_START) && |
| 3744 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3745 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3748 | /* |
| 3749 | * When a node is a text node or a comment, it uses a global static |
| 3750 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3751 | * Otherwise the node name might come from the document's dictionnary |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3752 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3753 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3754 | (cur->type != XML_TEXT_NODE) && |
| 3755 | (cur->type != XML_COMMENT_NODE)) |
| 3756 | DICT_FREE(cur->name) |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3757 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3758 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3759 | (cur->type == XML_XINCLUDE_START) || |
| 3760 | (cur->type == XML_XINCLUDE_END)) && |
| 3761 | (cur->nsDef != NULL)) |
| 3762 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3763 | xmlFree(cur); |
| 3764 | } |
| 3765 | |
| 3766 | /** |
| 3767 | * xmlUnlinkNode: |
| 3768 | * @cur: the node |
| 3769 | * |
| 3770 | * Unlink a node from it's current context, the node is not freed |
Daniel Veillard | 39d027c | 2012-05-11 12:38:23 +0800 | [diff] [blame] | 3771 | * If one need to free the node, use xmlFreeNode() routine after the |
| 3772 | * unlink to discard it. |
Daniel Veillard | 6ca24a3 | 2012-08-08 15:31:55 +0800 | [diff] [blame] | 3773 | * Note that namespace nodes can't be unlinked as they do not have |
| 3774 | * pointer to their parent. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3775 | */ |
| 3776 | void |
| 3777 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3778 | if (cur == NULL) { |
| 3779 | #ifdef DEBUG_TREE |
| 3780 | xmlGenericError(xmlGenericErrorContext, |
| 3781 | "xmlUnlinkNode : node == NULL\n"); |
| 3782 | #endif |
| 3783 | return; |
| 3784 | } |
Daniel Veillard | 6ca24a3 | 2012-08-08 15:31:55 +0800 | [diff] [blame] | 3785 | if (cur->type == XML_NAMESPACE_DECL) |
| 3786 | return; |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3787 | if (cur->type == XML_DTD_NODE) { |
| 3788 | xmlDocPtr doc; |
| 3789 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3790 | if (doc != NULL) { |
| 3791 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3792 | doc->intSubset = NULL; |
| 3793 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3794 | doc->extSubset = NULL; |
| 3795 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3796 | } |
Daniel Veillard | 2cba415 | 2008-08-27 11:45:41 +0000 | [diff] [blame] | 3797 | if (cur->type == XML_ENTITY_DECL) { |
| 3798 | xmlDocPtr doc; |
| 3799 | doc = cur->doc; |
| 3800 | if (doc != NULL) { |
| 3801 | if (doc->intSubset != NULL) { |
| 3802 | if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur) |
| 3803 | xmlHashRemoveEntry(doc->intSubset->entities, cur->name, |
| 3804 | NULL); |
| 3805 | if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur) |
| 3806 | xmlHashRemoveEntry(doc->intSubset->pentities, cur->name, |
| 3807 | NULL); |
| 3808 | } |
| 3809 | if (doc->extSubset != NULL) { |
| 3810 | if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur) |
| 3811 | xmlHashRemoveEntry(doc->extSubset->entities, cur->name, |
| 3812 | NULL); |
| 3813 | if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur) |
| 3814 | xmlHashRemoveEntry(doc->extSubset->pentities, cur->name, |
| 3815 | NULL); |
| 3816 | } |
| 3817 | } |
| 3818 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3819 | if (cur->parent != NULL) { |
| 3820 | xmlNodePtr parent; |
| 3821 | parent = cur->parent; |
| 3822 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3823 | if (parent->properties == (xmlAttrPtr) cur) |
| 3824 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3825 | } else { |
| 3826 | if (parent->children == cur) |
| 3827 | parent->children = cur->next; |
| 3828 | if (parent->last == cur) |
| 3829 | parent->last = cur->prev; |
| 3830 | } |
| 3831 | cur->parent = NULL; |
| 3832 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3833 | if (cur->next != NULL) |
| 3834 | cur->next->prev = cur->prev; |
| 3835 | if (cur->prev != NULL) |
| 3836 | cur->prev->next = cur->next; |
| 3837 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 3840 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3841 | /** |
| 3842 | * xmlReplaceNode: |
| 3843 | * @old: the old node |
| 3844 | * @cur: the node |
| 3845 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 3846 | * Unlink the old node from its current context, prune the new one |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3847 | * 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] | 3848 | * first unlinked from its existing context. |
| 3849 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3850 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3851 | */ |
| 3852 | xmlNodePtr |
| 3853 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 3854 | if (old == cur) return(NULL); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3855 | if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) || |
| 3856 | (old->parent == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3857 | #ifdef DEBUG_TREE |
| 3858 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3859 | "xmlReplaceNode : old == NULL or without parent\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3860 | #endif |
| 3861 | return(NULL); |
| 3862 | } |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3863 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3864 | xmlUnlinkNode(old); |
| 3865 | return(old); |
| 3866 | } |
| 3867 | if (cur == old) { |
| 3868 | return(old); |
| 3869 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3870 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3871 | #ifdef DEBUG_TREE |
| 3872 | xmlGenericError(xmlGenericErrorContext, |
| 3873 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3874 | #endif |
| 3875 | return(old); |
| 3876 | } |
| 3877 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3878 | #ifdef DEBUG_TREE |
| 3879 | xmlGenericError(xmlGenericErrorContext, |
| 3880 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3881 | #endif |
| 3882 | return(old); |
| 3883 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3884 | xmlUnlinkNode(cur); |
Daniel Veillard | 64d7d12 | 2005-05-11 18:03:42 +0000 | [diff] [blame] | 3885 | xmlSetTreeDoc(cur, old->doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3886 | cur->parent = old->parent; |
| 3887 | cur->next = old->next; |
| 3888 | if (cur->next != NULL) |
| 3889 | cur->next->prev = cur; |
| 3890 | cur->prev = old->prev; |
| 3891 | if (cur->prev != NULL) |
| 3892 | cur->prev->next = cur; |
| 3893 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3894 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3895 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3896 | cur->parent->properties = ((xmlAttrPtr) cur); |
| 3897 | } else { |
| 3898 | if (cur->parent->children == old) |
| 3899 | cur->parent->children = cur; |
| 3900 | if (cur->parent->last == old) |
| 3901 | cur->parent->last = cur; |
| 3902 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3903 | } |
| 3904 | old->next = old->prev = NULL; |
| 3905 | old->parent = NULL; |
| 3906 | return(old); |
| 3907 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3908 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3909 | |
| 3910 | /************************************************************************ |
| 3911 | * * |
| 3912 | * Copy operations * |
| 3913 | * * |
| 3914 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3915 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3916 | /** |
| 3917 | * xmlCopyNamespace: |
| 3918 | * @cur: the namespace |
| 3919 | * |
| 3920 | * Do a copy of the namespace. |
| 3921 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3922 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3923 | */ |
| 3924 | xmlNsPtr |
| 3925 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3926 | xmlNsPtr ret; |
| 3927 | |
| 3928 | if (cur == NULL) return(NULL); |
| 3929 | switch (cur->type) { |
| 3930 | case XML_LOCAL_NAMESPACE: |
| 3931 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3932 | break; |
| 3933 | default: |
| 3934 | #ifdef DEBUG_TREE |
| 3935 | xmlGenericError(xmlGenericErrorContext, |
| 3936 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3937 | #endif |
| 3938 | return(NULL); |
| 3939 | } |
| 3940 | return(ret); |
| 3941 | } |
| 3942 | |
| 3943 | /** |
| 3944 | * xmlCopyNamespaceList: |
| 3945 | * @cur: the first namespace |
| 3946 | * |
| 3947 | * Do a copy of an namespace list. |
| 3948 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3949 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3950 | */ |
| 3951 | xmlNsPtr |
| 3952 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3953 | xmlNsPtr ret = NULL; |
| 3954 | xmlNsPtr p = NULL,q; |
| 3955 | |
| 3956 | while (cur != NULL) { |
| 3957 | q = xmlCopyNamespace(cur); |
| 3958 | if (p == NULL) { |
| 3959 | ret = p = q; |
| 3960 | } else { |
| 3961 | p->next = q; |
| 3962 | p = q; |
| 3963 | } |
| 3964 | cur = cur->next; |
| 3965 | } |
| 3966 | return(ret); |
| 3967 | } |
| 3968 | |
| 3969 | static xmlNodePtr |
| 3970 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3971 | |
| 3972 | static xmlAttrPtr |
| 3973 | xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3974 | xmlAttrPtr ret; |
| 3975 | |
| 3976 | if (cur == NULL) return(NULL); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3977 | if ((target != NULL) && (target->type != XML_ELEMENT_NODE)) |
| 3978 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3979 | if (target != NULL) |
| 3980 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3981 | else if (doc != NULL) |
| 3982 | ret = xmlNewDocProp(doc, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3983 | else if (cur->parent != NULL) |
| 3984 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 3985 | else if (cur->children != NULL) |
| 3986 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 3987 | else |
| 3988 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 3989 | if (ret == NULL) return(NULL); |
| 3990 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3991 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3992 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 3993 | xmlNsPtr ns; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3994 | |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 3995 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 3996 | if (ns == NULL) { |
| 3997 | /* |
| 3998 | * Humm, we are copying an element whose namespace is defined |
| 3999 | * out of the new tree scope. Search it in the original tree |
| 4000 | * and add it at the top of the new tree |
| 4001 | */ |
| 4002 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 4003 | if (ns != NULL) { |
| 4004 | xmlNodePtr root = target; |
| 4005 | xmlNodePtr pred = NULL; |
| 4006 | |
| 4007 | while (root->parent != NULL) { |
| 4008 | pred = root; |
| 4009 | root = root->parent; |
| 4010 | } |
| 4011 | if (root == (xmlNodePtr) target->doc) { |
| 4012 | /* correct possibly cycling above the document elt */ |
| 4013 | root = pred; |
| 4014 | } |
| 4015 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 4016 | } |
| 4017 | } else { |
| 4018 | /* |
| 4019 | * we have to find something appropriate here since |
| 4020 | * we cant be sure, that the namespce we found is identified |
| 4021 | * by the prefix |
| 4022 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4023 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 4024 | /* this is the nice case */ |
| 4025 | ret->ns = ns; |
| 4026 | } else { |
| 4027 | /* |
| 4028 | * we are in trouble: we need a new reconcilied namespace. |
| 4029 | * This is expensive |
| 4030 | */ |
| 4031 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 4032 | } |
| 4033 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4034 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4035 | } else |
| 4036 | ret->ns = NULL; |
| 4037 | |
| 4038 | if (cur->children != NULL) { |
| 4039 | xmlNodePtr tmp; |
| 4040 | |
| 4041 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 4042 | ret->last = NULL; |
| 4043 | tmp = ret->children; |
| 4044 | while (tmp != NULL) { |
| 4045 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 4046 | if (tmp->next == NULL) |
| 4047 | ret->last = tmp; |
| 4048 | tmp = tmp->next; |
| 4049 | } |
| 4050 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 4051 | /* |
| 4052 | * Try to handle IDs |
| 4053 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 4054 | if ((target!= NULL) && (cur!= NULL) && |
| 4055 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 4056 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 4057 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 4058 | xmlChar *id; |
| 4059 | |
| 4060 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 4061 | if (id != NULL) { |
| 4062 | xmlAddID(NULL, target->doc, id, ret); |
| 4063 | xmlFree(id); |
| 4064 | } |
| 4065 | } |
| 4066 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4067 | return(ret); |
| 4068 | } |
| 4069 | |
| 4070 | /** |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 4071 | * xmlCopyProp: |
| 4072 | * @target: the element where the attribute will be grafted |
| 4073 | * @cur: the attribute |
| 4074 | * |
| 4075 | * Do a copy of the attribute. |
| 4076 | * |
| 4077 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
| 4078 | */ |
| 4079 | xmlAttrPtr |
| 4080 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 4081 | return xmlCopyPropInternal(NULL, target, cur); |
| 4082 | } |
| 4083 | |
| 4084 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4085 | * xmlCopyPropList: |
| 4086 | * @target: the element where the attributes will be grafted |
| 4087 | * @cur: the first attribute |
| 4088 | * |
| 4089 | * Do a copy of an attribute list. |
| 4090 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4091 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4092 | */ |
| 4093 | xmlAttrPtr |
| 4094 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 4095 | xmlAttrPtr ret = NULL; |
| 4096 | xmlAttrPtr p = NULL,q; |
| 4097 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 4098 | if ((target != NULL) && (target->type != XML_ELEMENT_NODE)) |
| 4099 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4100 | while (cur != NULL) { |
| 4101 | q = xmlCopyProp(target, cur); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4102 | if (q == NULL) |
| 4103 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4104 | if (p == NULL) { |
| 4105 | ret = p = q; |
| 4106 | } else { |
| 4107 | p->next = q; |
| 4108 | q->prev = p; |
| 4109 | p = q; |
| 4110 | } |
| 4111 | cur = cur->next; |
| 4112 | } |
| 4113 | return(ret); |
| 4114 | } |
| 4115 | |
| 4116 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4117 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4118 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4119 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4120 | * tricky reason: namespaces. Doing a direct copy of a node |
| 4121 | * say RPM:Copyright without changing the namespace pointer to |
| 4122 | * something else can produce stale links. One way to do it is |
| 4123 | * to keep a reference counter but this doesn't work as soon |
| 4124 | * as one move the element or the subtree out of the scope of |
| 4125 | * the existing namespace. The actual solution seems to add |
| 4126 | * a copy of the namespace at the top of the copied tree if |
| 4127 | * not available in the subtree. |
| 4128 | * Hence two functions, the public front-end call the inner ones |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4129 | * The argument "recursive" normally indicates a recursive copy |
| 4130 | * of the node with values 0 (no) and 1 (yes). For XInclude, |
| 4131 | * however, we allow a value of 2 to indicate copy properties and |
| 4132 | * namespace info, but don't recurse on children. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4133 | */ |
| 4134 | |
| 4135 | static xmlNodePtr |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 4136 | xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4137 | int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4138 | xmlNodePtr ret; |
| 4139 | |
| 4140 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4141 | switch (node->type) { |
| 4142 | case XML_TEXT_NODE: |
| 4143 | case XML_CDATA_SECTION_NODE: |
| 4144 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 4145 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4146 | case XML_ENTITY_REF_NODE: |
| 4147 | case XML_ENTITY_NODE: |
| 4148 | case XML_PI_NODE: |
| 4149 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4150 | case XML_XINCLUDE_START: |
| 4151 | case XML_XINCLUDE_END: |
| 4152 | break; |
| 4153 | case XML_ATTRIBUTE_NODE: |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 4154 | return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node)); |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4155 | case XML_NAMESPACE_DECL: |
| 4156 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4157 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4158 | case XML_DOCUMENT_NODE: |
| 4159 | case XML_HTML_DOCUMENT_NODE: |
| 4160 | #ifdef LIBXML_DOCB_ENABLED |
| 4161 | case XML_DOCB_DOCUMENT_NODE: |
| 4162 | #endif |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4163 | #ifdef LIBXML_TREE_ENABLED |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4164 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended)); |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4165 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4166 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4167 | case XML_NOTATION_NODE: |
| 4168 | case XML_DTD_NODE: |
| 4169 | case XML_ELEMENT_DECL: |
| 4170 | case XML_ATTRIBUTE_DECL: |
| 4171 | case XML_ENTITY_DECL: |
| 4172 | return(NULL); |
| 4173 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4174 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4175 | /* |
| 4176 | * Allocate a new node and fill the fields. |
| 4177 | */ |
| 4178 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 4179 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4180 | xmlTreeErrMemory("copying node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4181 | return(NULL); |
| 4182 | } |
| 4183 | memset(ret, 0, sizeof(xmlNode)); |
| 4184 | ret->type = node->type; |
| 4185 | |
| 4186 | ret->doc = doc; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4187 | ret->parent = parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4188 | if (node->name == xmlStringText) |
| 4189 | ret->name = xmlStringText; |
| 4190 | else if (node->name == xmlStringTextNoenc) |
| 4191 | ret->name = xmlStringTextNoenc; |
| 4192 | else if (node->name == xmlStringComment) |
| 4193 | ret->name = xmlStringComment; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4194 | else if (node->name != NULL) { |
| 4195 | if ((doc != NULL) && (doc->dict != NULL)) |
| 4196 | ret->name = xmlDictLookup(doc->dict, node->name, -1); |
| 4197 | else |
| 4198 | ret->name = xmlStrdup(node->name); |
| 4199 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 4200 | if ((node->type != XML_ELEMENT_NODE) && |
| 4201 | (node->content != NULL) && |
| 4202 | (node->type != XML_ENTITY_REF_NODE) && |
| 4203 | (node->type != XML_XINCLUDE_END) && |
| 4204 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4205 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 4206 | }else{ |
| 4207 | if (node->type == XML_ELEMENT_NODE) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 4208 | ret->line = node->line; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4209 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4210 | if (parent != NULL) { |
| 4211 | xmlNodePtr tmp; |
| 4212 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4213 | /* |
| 4214 | * this is a tricky part for the node register thing: |
| 4215 | * in case ret does get coalesced in xmlAddChild |
| 4216 | * the deregister-node callback is called; so we register ret now already |
| 4217 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 4218 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4219 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 4220 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4221 | tmp = xmlAddChild(parent, ret); |
| 4222 | /* node could have coalesced */ |
| 4223 | if (tmp != ret) |
| 4224 | return(tmp); |
| 4225 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4226 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4227 | if (!extended) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4228 | goto out; |
Petr Pajas | 2afca4a | 2009-07-30 17:47:32 +0200 | [diff] [blame] | 4229 | if (((node->type == XML_ELEMENT_NODE) || |
| 4230 | (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4231 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 4232 | |
| 4233 | if (node->ns != NULL) { |
| 4234 | xmlNsPtr ns; |
| 4235 | |
| 4236 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 4237 | if (ns == NULL) { |
| 4238 | /* |
| 4239 | * Humm, we are copying an element whose namespace is defined |
| 4240 | * out of the new tree scope. Search it in the original tree |
| 4241 | * and add it at the top of the new tree |
| 4242 | */ |
| 4243 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 4244 | if (ns != NULL) { |
| 4245 | xmlNodePtr root = ret; |
| 4246 | |
| 4247 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 4248 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Rob Richards | ddb01cb | 2010-01-29 13:32:12 -0500 | [diff] [blame] | 4249 | } else { |
| 4250 | ret->ns = xmlNewReconciliedNs(doc, ret, node->ns); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4251 | } |
| 4252 | } else { |
| 4253 | /* |
| 4254 | * reference the existing namespace definition in our own tree. |
| 4255 | */ |
| 4256 | ret->ns = ns; |
| 4257 | } |
| 4258 | } |
Petr Pajas | 2afca4a | 2009-07-30 17:47:32 +0200 | [diff] [blame] | 4259 | if (((node->type == XML_ELEMENT_NODE) || |
| 4260 | (node->type == XML_XINCLUDE_START)) && (node->properties != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4261 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4262 | if (node->type == XML_ENTITY_REF_NODE) { |
| 4263 | if ((doc == NULL) || (node->doc != doc)) { |
| 4264 | /* |
| 4265 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4266 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4267 | * we cannot keep the reference. Try to find it in the |
| 4268 | * target document. |
| 4269 | */ |
| 4270 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 4271 | } else { |
| 4272 | ret->children = node->children; |
| 4273 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 4274 | ret->last = ret->children; |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4275 | } else if ((node->children != NULL) && (extended != 2)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4276 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 4277 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 4278 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4279 | |
| 4280 | out: |
| 4281 | /* if parent != NULL we already registered the node above */ |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 4282 | if ((parent == NULL) && |
| 4283 | ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4284 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4285 | return(ret); |
| 4286 | } |
| 4287 | |
| 4288 | static xmlNodePtr |
| 4289 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 4290 | xmlNodePtr ret = NULL; |
| 4291 | xmlNodePtr p = NULL,q; |
| 4292 | |
| 4293 | while (node != NULL) { |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4294 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4295 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 4296 | if (doc == NULL) { |
| 4297 | node = node->next; |
| 4298 | continue; |
| 4299 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4300 | if (doc->intSubset == NULL) { |
| 4301 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
Gaurav | 98a4e71 | 2013-11-29 23:28:21 +0800 | [diff] [blame] | 4302 | if (q == NULL) return(NULL); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4303 | q->doc = doc; |
| 4304 | q->parent = parent; |
| 4305 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4306 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4307 | } else { |
| 4308 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4309 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4310 | } |
| 4311 | } else |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4312 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4313 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Gaurav | 98a4e71 | 2013-11-29 23:28:21 +0800 | [diff] [blame] | 4314 | if (q == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4315 | if (ret == NULL) { |
| 4316 | q->prev = NULL; |
| 4317 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4318 | } else if (p != q) { |
| 4319 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4320 | p->next = q; |
| 4321 | q->prev = p; |
| 4322 | p = q; |
| 4323 | } |
| 4324 | node = node->next; |
| 4325 | } |
| 4326 | return(ret); |
| 4327 | } |
| 4328 | |
| 4329 | /** |
| 4330 | * xmlCopyNode: |
| 4331 | * @node: the node |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4332 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4333 | * when applicable) |
| 4334 | * if 2 copy properties and namespaces (when applicable) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4335 | * |
| 4336 | * Do a copy of the node. |
| 4337 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4338 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4339 | */ |
| 4340 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4341 | xmlCopyNode(const xmlNodePtr node, int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4342 | xmlNodePtr ret; |
| 4343 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4344 | ret = xmlStaticCopyNode(node, NULL, NULL, extended); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4345 | return(ret); |
| 4346 | } |
| 4347 | |
| 4348 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4349 | * xmlDocCopyNode: |
| 4350 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4351 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4352 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4353 | * when applicable) |
| 4354 | * if 2 copy properties and namespaces (when applicable) |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4355 | * |
| 4356 | * Do a copy of the node to a given document. |
| 4357 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4358 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4359 | */ |
| 4360 | xmlNodePtr |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4361 | xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4362 | xmlNodePtr ret; |
| 4363 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4364 | ret = xmlStaticCopyNode(node, doc, NULL, extended); |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4365 | return(ret); |
| 4366 | } |
| 4367 | |
| 4368 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4369 | * xmlDocCopyNodeList: |
| 4370 | * @doc: the target document |
| 4371 | * @node: the first node in the list. |
| 4372 | * |
| 4373 | * Do a recursive copy of the node list. |
| 4374 | * |
| 4375 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
| 4376 | */ |
| 4377 | xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) { |
| 4378 | xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL); |
| 4379 | return(ret); |
| 4380 | } |
| 4381 | |
| 4382 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4383 | * xmlCopyNodeList: |
| 4384 | * @node: the first node in the list. |
| 4385 | * |
| 4386 | * Do a recursive copy of the node list. |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4387 | * Use xmlDocCopyNodeList() if possible to ensure string interning. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4388 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4389 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4390 | */ |
Daniel Veillard | 3ec4c61 | 2001-08-28 20:39:49 +0000 | [diff] [blame] | 4391 | xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4392 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 4393 | return(ret); |
| 4394 | } |
| 4395 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4396 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4397 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4398 | * xmlCopyDtd: |
| 4399 | * @dtd: the dtd |
| 4400 | * |
| 4401 | * Do a copy of the dtd. |
| 4402 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4403 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4404 | */ |
| 4405 | xmlDtdPtr |
| 4406 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 4407 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4408 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4409 | |
| 4410 | if (dtd == NULL) return(NULL); |
| 4411 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 4412 | if (ret == NULL) return(NULL); |
| 4413 | if (dtd->entities != NULL) |
| 4414 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 4415 | (xmlEntitiesTablePtr) dtd->entities); |
| 4416 | if (dtd->notations != NULL) |
| 4417 | ret->notations = (void *) xmlCopyNotationTable( |
| 4418 | (xmlNotationTablePtr) dtd->notations); |
| 4419 | if (dtd->elements != NULL) |
| 4420 | ret->elements = (void *) xmlCopyElementTable( |
| 4421 | (xmlElementTablePtr) dtd->elements); |
| 4422 | if (dtd->attributes != NULL) |
| 4423 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 4424 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4425 | if (dtd->pentities != NULL) |
| 4426 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 4427 | (xmlEntitiesTablePtr) dtd->pentities); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4428 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4429 | cur = dtd->children; |
| 4430 | while (cur != NULL) { |
| 4431 | q = NULL; |
| 4432 | |
| 4433 | if (cur->type == XML_ENTITY_DECL) { |
| 4434 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 4435 | switch (tmp->etype) { |
| 4436 | case XML_INTERNAL_GENERAL_ENTITY: |
| 4437 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 4438 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 4439 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 4440 | break; |
| 4441 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 4442 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4443 | q = (xmlNodePtr) |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4444 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 4445 | break; |
| 4446 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 4447 | break; |
| 4448 | } |
| 4449 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 4450 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 4451 | q = (xmlNodePtr) |
| 4452 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 4453 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4454 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4455 | q = (xmlNodePtr) |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4456 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 4457 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4458 | q = xmlCopyNode(cur, 0); |
| 4459 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4460 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4461 | if (q == NULL) { |
| 4462 | cur = cur->next; |
| 4463 | continue; |
| 4464 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4465 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4466 | if (p == NULL) |
| 4467 | ret->children = q; |
| 4468 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4469 | p->next = q; |
| 4470 | |
| 4471 | q->prev = p; |
| 4472 | q->parent = (xmlNodePtr) ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4473 | q->next = NULL; |
| 4474 | ret->last = q; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4475 | p = q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4476 | cur = cur->next; |
| 4477 | } |
| 4478 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4479 | return(ret); |
| 4480 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4481 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4482 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4483 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4484 | /** |
| 4485 | * xmlCopyDoc: |
| 4486 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4487 | * @recursive: if not zero do a recursive copy. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4488 | * |
| 4489 | * 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] | 4490 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4491 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4492 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4493 | */ |
| 4494 | xmlDocPtr |
| 4495 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 4496 | xmlDocPtr ret; |
| 4497 | |
| 4498 | if (doc == NULL) return(NULL); |
| 4499 | ret = xmlNewDoc(doc->version); |
| 4500 | if (ret == NULL) return(NULL); |
| 4501 | if (doc->name != NULL) |
| 4502 | ret->name = xmlMemStrdup(doc->name); |
| 4503 | if (doc->encoding != NULL) |
| 4504 | ret->encoding = xmlStrdup(doc->encoding); |
Daniel Veillard | f59507d | 2005-01-27 17:26:49 +0000 | [diff] [blame] | 4505 | if (doc->URL != NULL) |
| 4506 | ret->URL = xmlStrdup(doc->URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4507 | ret->charset = doc->charset; |
| 4508 | ret->compression = doc->compression; |
| 4509 | ret->standalone = doc->standalone; |
| 4510 | if (!recursive) return(ret); |
| 4511 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4512 | ret->last = NULL; |
| 4513 | ret->children = NULL; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4514 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4515 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4516 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4517 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4518 | ret->intSubset->parent = ret; |
| 4519 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4520 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4521 | if (doc->oldNs != NULL) |
| 4522 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 4523 | if (doc->children != NULL) { |
| 4524 | xmlNodePtr tmp; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4525 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4526 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 4527 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4528 | ret->last = NULL; |
| 4529 | tmp = ret->children; |
| 4530 | while (tmp != NULL) { |
| 4531 | if (tmp->next == NULL) |
| 4532 | ret->last = tmp; |
| 4533 | tmp = tmp->next; |
| 4534 | } |
| 4535 | } |
| 4536 | return(ret); |
| 4537 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4538 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4539 | |
| 4540 | /************************************************************************ |
| 4541 | * * |
| 4542 | * Content access functions * |
| 4543 | * * |
| 4544 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4545 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4546 | /** |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4547 | * xmlGetLineNoInternal: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4548 | * @node: valid node |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4549 | * @depth: used to limit any risk of recursion |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4550 | * |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4551 | * Get line number of @node. |
| 4552 | * Try to override the limitation of lines being store in 16 bits ints |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4553 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4554 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4555 | */ |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4556 | static long |
| 4557 | xmlGetLineNoInternal(xmlNodePtr node, int depth) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4558 | { |
| 4559 | long result = -1; |
| 4560 | |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4561 | if (depth >= 5) |
| 4562 | return(-1); |
| 4563 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4564 | if (!node) |
| 4565 | return result; |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4566 | if ((node->type == XML_ELEMENT_NODE) || |
| 4567 | (node->type == XML_TEXT_NODE) || |
| 4568 | (node->type == XML_COMMENT_NODE) || |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4569 | (node->type == XML_PI_NODE)) { |
| 4570 | if (node->line == 65535) { |
| 4571 | if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL)) |
| 4572 | result = (long) node->psvi; |
| 4573 | else if ((node->type == XML_ELEMENT_NODE) && |
| 4574 | (node->children != NULL)) |
| 4575 | result = xmlGetLineNoInternal(node->children, depth + 1); |
| 4576 | else if (node->next != NULL) |
| 4577 | result = xmlGetLineNoInternal(node->next, depth + 1); |
| 4578 | else if (node->prev != NULL) |
| 4579 | result = xmlGetLineNoInternal(node->prev, depth + 1); |
| 4580 | } |
| 4581 | if ((result == -1) || (result == 65535)) |
| 4582 | result = (long) node->line; |
| 4583 | } else if ((node->prev != NULL) && |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4584 | ((node->prev->type == XML_ELEMENT_NODE) || |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4585 | (node->prev->type == XML_TEXT_NODE) || |
| 4586 | (node->prev->type == XML_COMMENT_NODE) || |
| 4587 | (node->prev->type == XML_PI_NODE))) |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4588 | result = xmlGetLineNoInternal(node->prev, depth + 1); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4589 | else if ((node->parent != NULL) && |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4590 | (node->parent->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4591 | result = xmlGetLineNoInternal(node->parent, depth + 1); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4592 | |
| 4593 | return result; |
| 4594 | } |
| 4595 | |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4596 | /** |
| 4597 | * xmlGetLineNo: |
| 4598 | * @node: valid node |
| 4599 | * |
| 4600 | * Get line number of @node. |
| 4601 | * Try to override the limitation of lines being store in 16 bits ints |
| 4602 | * if XML_PARSE_BIG_LINES parser option was used |
| 4603 | * |
| 4604 | * Returns the line number if successful, -1 otherwise |
| 4605 | */ |
| 4606 | long |
| 4607 | xmlGetLineNo(xmlNodePtr node) |
| 4608 | { |
| 4609 | return(xmlGetLineNoInternal(node, 0)); |
| 4610 | } |
| 4611 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4612 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4613 | /** |
| 4614 | * xmlGetNodePath: |
| 4615 | * @node: a node |
| 4616 | * |
| 4617 | * Build a structure based Path for the given node |
| 4618 | * |
| 4619 | * Returns the new path or NULL in case of error. The caller must free |
| 4620 | * the returned string |
| 4621 | */ |
| 4622 | xmlChar * |
| 4623 | xmlGetNodePath(xmlNodePtr node) |
| 4624 | { |
| 4625 | xmlNodePtr cur, tmp, next; |
| 4626 | xmlChar *buffer = NULL, *temp; |
| 4627 | size_t buf_len; |
| 4628 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4629 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4630 | const char *name; |
| 4631 | char nametemp[100]; |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4632 | int occur = 0, generic; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4633 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 4634 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4635 | return (NULL); |
| 4636 | |
| 4637 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4638 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4639 | if (buffer == NULL) { |
| 4640 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4641 | return (NULL); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4642 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4643 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4644 | if (buf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4645 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4646 | xmlFree(buffer); |
| 4647 | return (NULL); |
| 4648 | } |
| 4649 | |
| 4650 | buffer[0] = 0; |
| 4651 | cur = node; |
| 4652 | do { |
| 4653 | name = ""; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4654 | sep = "?"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4655 | occur = 0; |
| 4656 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4657 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4658 | if (buffer[0] == '/') |
| 4659 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4660 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4661 | next = NULL; |
| 4662 | } else if (cur->type == XML_ELEMENT_NODE) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4663 | generic = 0; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4664 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4665 | name = (const char *) cur->name; |
| 4666 | if (cur->ns) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4667 | if (cur->ns->prefix != NULL) { |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4668 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4669 | (char *)cur->ns->prefix, (char *)cur->name); |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4670 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4671 | name = nametemp; |
| 4672 | } else { |
| 4673 | /* |
| 4674 | * We cannot express named elements in the default |
| 4675 | * namespace, so use "*". |
| 4676 | */ |
| 4677 | generic = 1; |
| 4678 | name = "*"; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4679 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4680 | } |
| 4681 | next = cur->parent; |
| 4682 | |
| 4683 | /* |
| 4684 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4685 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4686 | */ |
| 4687 | tmp = cur->prev; |
| 4688 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4689 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4690 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4691 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4692 | ((tmp->ns == cur->ns) || |
| 4693 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4694 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4695 | occur++; |
| 4696 | tmp = tmp->prev; |
| 4697 | } |
| 4698 | if (occur == 0) { |
| 4699 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4700 | while (tmp != NULL && occur == 0) { |
| 4701 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4702 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4703 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4704 | ((tmp->ns == cur->ns) || |
| 4705 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4706 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4707 | occur++; |
| 4708 | tmp = tmp->next; |
| 4709 | } |
| 4710 | if (occur != 0) |
| 4711 | occur = 1; |
| 4712 | } else |
| 4713 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4714 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4715 | sep = "/"; |
| 4716 | name = "comment()"; |
| 4717 | next = cur->parent; |
| 4718 | |
| 4719 | /* |
| 4720 | * Thumbler index computation |
| 4721 | */ |
| 4722 | tmp = cur->prev; |
| 4723 | while (tmp != NULL) { |
| 4724 | if (tmp->type == XML_COMMENT_NODE) |
| 4725 | occur++; |
| 4726 | tmp = tmp->prev; |
| 4727 | } |
| 4728 | if (occur == 0) { |
| 4729 | tmp = cur->next; |
| 4730 | while (tmp != NULL && occur == 0) { |
| 4731 | if (tmp->type == XML_COMMENT_NODE) |
| 4732 | occur++; |
| 4733 | tmp = tmp->next; |
| 4734 | } |
| 4735 | if (occur != 0) |
| 4736 | occur = 1; |
| 4737 | } else |
| 4738 | occur++; |
| 4739 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4740 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4741 | sep = "/"; |
| 4742 | name = "text()"; |
| 4743 | next = cur->parent; |
| 4744 | |
| 4745 | /* |
| 4746 | * Thumbler index computation |
| 4747 | */ |
| 4748 | tmp = cur->prev; |
| 4749 | while (tmp != NULL) { |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4750 | if ((tmp->type == XML_TEXT_NODE) || |
| 4751 | (tmp->type == XML_CDATA_SECTION_NODE)) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4752 | occur++; |
| 4753 | tmp = tmp->prev; |
| 4754 | } |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4755 | /* |
| 4756 | * Evaluate if this is the only text- or CDATA-section-node; |
| 4757 | * if yes, then we'll get "text()", otherwise "text()[1]". |
| 4758 | */ |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4759 | if (occur == 0) { |
| 4760 | tmp = cur->next; |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4761 | while (tmp != NULL) { |
| 4762 | if ((tmp->type == XML_TEXT_NODE) || |
| 4763 | (tmp->type == XML_CDATA_SECTION_NODE)) |
| 4764 | { |
| 4765 | occur = 1; |
| 4766 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4767 | } |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4768 | tmp = tmp->next; |
| 4769 | } |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4770 | } else |
| 4771 | occur++; |
| 4772 | } else if (cur->type == XML_PI_NODE) { |
| 4773 | sep = "/"; |
| 4774 | snprintf(nametemp, sizeof(nametemp) - 1, |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4775 | "processing-instruction('%s')", (char *)cur->name); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4776 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4777 | name = nametemp; |
| 4778 | |
| 4779 | next = cur->parent; |
| 4780 | |
| 4781 | /* |
| 4782 | * Thumbler index computation |
| 4783 | */ |
| 4784 | tmp = cur->prev; |
| 4785 | while (tmp != NULL) { |
| 4786 | if ((tmp->type == XML_PI_NODE) && |
| 4787 | (xmlStrEqual(cur->name, tmp->name))) |
| 4788 | occur++; |
| 4789 | tmp = tmp->prev; |
| 4790 | } |
| 4791 | if (occur == 0) { |
| 4792 | tmp = cur->next; |
| 4793 | while (tmp != NULL && occur == 0) { |
| 4794 | if ((tmp->type == XML_PI_NODE) && |
| 4795 | (xmlStrEqual(cur->name, tmp->name))) |
| 4796 | occur++; |
| 4797 | tmp = tmp->next; |
| 4798 | } |
| 4799 | if (occur != 0) |
| 4800 | occur = 1; |
| 4801 | } else |
| 4802 | occur++; |
| 4803 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4804 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4805 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4806 | name = (const char *) (((xmlAttrPtr) cur)->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4807 | if (cur->ns) { |
| 4808 | if (cur->ns->prefix != NULL) |
| 4809 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4810 | (char *)cur->ns->prefix, (char *)cur->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4811 | else |
| 4812 | snprintf(nametemp, sizeof(nametemp) - 1, "%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4813 | (char *)cur->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4814 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4815 | name = nametemp; |
| 4816 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4817 | next = ((xmlAttrPtr) cur)->parent; |
| 4818 | } else { |
| 4819 | next = cur->parent; |
| 4820 | } |
| 4821 | |
| 4822 | /* |
| 4823 | * Make sure there is enough room |
| 4824 | */ |
| 4825 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4826 | buf_len = |
| 4827 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4828 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4829 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4830 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4831 | xmlFree(buf); |
| 4832 | xmlFree(buffer); |
| 4833 | return (NULL); |
| 4834 | } |
| 4835 | buffer = temp; |
| 4836 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4837 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4838 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4839 | xmlFree(buf); |
| 4840 | xmlFree(buffer); |
| 4841 | return (NULL); |
| 4842 | } |
| 4843 | buf = temp; |
| 4844 | } |
| 4845 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4846 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4847 | sep, name, (char *) buffer); |
| 4848 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4849 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4850 | sep, name, occur, (char *) buffer); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4851 | snprintf((char *) buffer, buf_len, "%s", (char *)buf); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4852 | cur = next; |
| 4853 | } while (cur != NULL); |
| 4854 | xmlFree(buf); |
| 4855 | return (buffer); |
| 4856 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4857 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4858 | |
| 4859 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4860 | * xmlDocGetRootElement: |
| 4861 | * @doc: the document |
| 4862 | * |
| 4863 | * Get the root element of the document (doc->children is a list |
| 4864 | * containing possibly comments, PIs, etc ...). |
| 4865 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4866 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4867 | */ |
| 4868 | xmlNodePtr |
| 4869 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 4870 | xmlNodePtr ret; |
| 4871 | |
| 4872 | if (doc == NULL) return(NULL); |
| 4873 | ret = doc->children; |
| 4874 | while (ret != NULL) { |
| 4875 | if (ret->type == XML_ELEMENT_NODE) |
| 4876 | return(ret); |
| 4877 | ret = ret->next; |
| 4878 | } |
| 4879 | return(ret); |
| 4880 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4881 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4882 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4883 | /** |
| 4884 | * xmlDocSetRootElement: |
| 4885 | * @doc: the document |
Daniel Veillard | 26a45c8 | 2006-10-20 12:55:34 +0000 | [diff] [blame] | 4886 | * @root: the new document root element, if root is NULL no action is taken, |
| 4887 | * to remove a node from a document use xmlUnlinkNode(root) instead. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4888 | * |
| 4889 | * Set the root element of the document (doc->children is a list |
| 4890 | * containing possibly comments, PIs, etc ...). |
| 4891 | * |
Daniel Veillard | 26a45c8 | 2006-10-20 12:55:34 +0000 | [diff] [blame] | 4892 | * 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] | 4893 | */ |
| 4894 | xmlNodePtr |
| 4895 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4896 | xmlNodePtr old = NULL; |
| 4897 | |
| 4898 | if (doc == NULL) return(NULL); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 4899 | if ((root == NULL) || (root->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4900 | return(NULL); |
| 4901 | xmlUnlinkNode(root); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 4902 | xmlSetTreeDoc(root, doc); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4903 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4904 | old = doc->children; |
| 4905 | while (old != NULL) { |
| 4906 | if (old->type == XML_ELEMENT_NODE) |
| 4907 | break; |
| 4908 | old = old->next; |
| 4909 | } |
| 4910 | if (old == NULL) { |
| 4911 | if (doc->children == NULL) { |
| 4912 | doc->children = root; |
| 4913 | doc->last = root; |
| 4914 | } else { |
| 4915 | xmlAddSibling(doc->children, root); |
| 4916 | } |
| 4917 | } else { |
| 4918 | xmlReplaceNode(old, root); |
| 4919 | } |
| 4920 | return(old); |
| 4921 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4922 | #endif |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4923 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4924 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4925 | /** |
| 4926 | * xmlNodeSetLang: |
| 4927 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4928 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4929 | * |
| 4930 | * Set the language of a node, i.e. the values of the xml:lang |
| 4931 | * attribute. |
| 4932 | */ |
| 4933 | void |
| 4934 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4935 | xmlNsPtr ns; |
| 4936 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4937 | if (cur == NULL) return; |
| 4938 | switch(cur->type) { |
| 4939 | case XML_TEXT_NODE: |
| 4940 | case XML_CDATA_SECTION_NODE: |
| 4941 | case XML_COMMENT_NODE: |
| 4942 | case XML_DOCUMENT_NODE: |
| 4943 | case XML_DOCUMENT_TYPE_NODE: |
| 4944 | case XML_DOCUMENT_FRAG_NODE: |
| 4945 | case XML_NOTATION_NODE: |
| 4946 | case XML_HTML_DOCUMENT_NODE: |
| 4947 | case XML_DTD_NODE: |
| 4948 | case XML_ELEMENT_DECL: |
| 4949 | case XML_ATTRIBUTE_DECL: |
| 4950 | case XML_ENTITY_DECL: |
| 4951 | case XML_PI_NODE: |
| 4952 | case XML_ENTITY_REF_NODE: |
| 4953 | case XML_ENTITY_NODE: |
| 4954 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4955 | #ifdef LIBXML_DOCB_ENABLED |
| 4956 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4957 | #endif |
| 4958 | case XML_XINCLUDE_START: |
| 4959 | case XML_XINCLUDE_END: |
| 4960 | return; |
| 4961 | case XML_ELEMENT_NODE: |
| 4962 | case XML_ATTRIBUTE_NODE: |
| 4963 | break; |
| 4964 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4965 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4966 | if (ns == NULL) |
| 4967 | return; |
| 4968 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4969 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4970 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4971 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4972 | /** |
| 4973 | * xmlNodeGetLang: |
| 4974 | * @cur: the node being checked |
| 4975 | * |
| 4976 | * Searches the language of a node, i.e. the values of the xml:lang |
| 4977 | * attribute or the one carried by the nearest ancestor. |
| 4978 | * |
| 4979 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4980 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4981 | */ |
| 4982 | xmlChar * |
| 4983 | xmlNodeGetLang(xmlNodePtr cur) { |
| 4984 | xmlChar *lang; |
| 4985 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 4986 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) |
| 4987 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4988 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 4989 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4990 | if (lang != NULL) |
| 4991 | return(lang); |
| 4992 | cur = cur->parent; |
| 4993 | } |
| 4994 | return(NULL); |
| 4995 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4996 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4997 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4998 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4999 | /** |
| 5000 | * xmlNodeSetSpacePreserve: |
| 5001 | * @cur: the node being changed |
| 5002 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 5003 | * |
| 5004 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 5005 | * value of the xml:space attribute. |
| 5006 | */ |
| 5007 | void |
| 5008 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5009 | xmlNsPtr ns; |
| 5010 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5011 | if (cur == NULL) return; |
| 5012 | switch(cur->type) { |
| 5013 | case XML_TEXT_NODE: |
| 5014 | case XML_CDATA_SECTION_NODE: |
| 5015 | case XML_COMMENT_NODE: |
| 5016 | case XML_DOCUMENT_NODE: |
| 5017 | case XML_DOCUMENT_TYPE_NODE: |
| 5018 | case XML_DOCUMENT_FRAG_NODE: |
| 5019 | case XML_NOTATION_NODE: |
| 5020 | case XML_HTML_DOCUMENT_NODE: |
| 5021 | case XML_DTD_NODE: |
| 5022 | case XML_ELEMENT_DECL: |
| 5023 | case XML_ATTRIBUTE_DECL: |
| 5024 | case XML_ENTITY_DECL: |
| 5025 | case XML_PI_NODE: |
| 5026 | case XML_ENTITY_REF_NODE: |
| 5027 | case XML_ENTITY_NODE: |
| 5028 | case XML_NAMESPACE_DECL: |
| 5029 | case XML_XINCLUDE_START: |
| 5030 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5031 | #ifdef LIBXML_DOCB_ENABLED |
| 5032 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5033 | #endif |
| 5034 | return; |
| 5035 | case XML_ELEMENT_NODE: |
| 5036 | case XML_ATTRIBUTE_NODE: |
| 5037 | break; |
| 5038 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5039 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 5040 | if (ns == NULL) |
| 5041 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5042 | switch (val) { |
| 5043 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5044 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5045 | break; |
| 5046 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5047 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5048 | break; |
| 5049 | } |
| 5050 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5051 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5052 | |
| 5053 | /** |
| 5054 | * xmlNodeGetSpacePreserve: |
| 5055 | * @cur: the node being checked |
| 5056 | * |
| 5057 | * Searches the space preserving behaviour of a node, i.e. the values |
| 5058 | * of the xml:space attribute or the one carried by the nearest |
| 5059 | * ancestor. |
| 5060 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5061 | * 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] | 5062 | */ |
| 5063 | int |
| 5064 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 5065 | xmlChar *space; |
| 5066 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 5067 | if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) |
| 5068 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5069 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5070 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5071 | if (space != NULL) { |
| 5072 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 5073 | xmlFree(space); |
| 5074 | return(1); |
| 5075 | } |
| 5076 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 5077 | xmlFree(space); |
| 5078 | return(0); |
| 5079 | } |
| 5080 | xmlFree(space); |
| 5081 | } |
| 5082 | cur = cur->parent; |
| 5083 | } |
| 5084 | return(-1); |
| 5085 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5086 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5087 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5088 | /** |
| 5089 | * xmlNodeSetName: |
| 5090 | * @cur: the node being changed |
| 5091 | * @name: the new tag name |
| 5092 | * |
| 5093 | * Set (or reset) the name of a node. |
| 5094 | */ |
| 5095 | void |
| 5096 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5097 | xmlDocPtr doc; |
| 5098 | xmlDictPtr dict; |
| 5099 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5100 | if (cur == NULL) return; |
| 5101 | if (name == NULL) return; |
| 5102 | switch(cur->type) { |
| 5103 | case XML_TEXT_NODE: |
| 5104 | case XML_CDATA_SECTION_NODE: |
| 5105 | case XML_COMMENT_NODE: |
| 5106 | case XML_DOCUMENT_TYPE_NODE: |
| 5107 | case XML_DOCUMENT_FRAG_NODE: |
| 5108 | case XML_NOTATION_NODE: |
| 5109 | case XML_HTML_DOCUMENT_NODE: |
| 5110 | case XML_NAMESPACE_DECL: |
| 5111 | case XML_XINCLUDE_START: |
| 5112 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5113 | #ifdef LIBXML_DOCB_ENABLED |
| 5114 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5115 | #endif |
| 5116 | return; |
| 5117 | case XML_ELEMENT_NODE: |
| 5118 | case XML_ATTRIBUTE_NODE: |
| 5119 | case XML_PI_NODE: |
| 5120 | case XML_ENTITY_REF_NODE: |
| 5121 | case XML_ENTITY_NODE: |
| 5122 | case XML_DTD_NODE: |
| 5123 | case XML_DOCUMENT_NODE: |
| 5124 | case XML_ELEMENT_DECL: |
| 5125 | case XML_ATTRIBUTE_DECL: |
| 5126 | case XML_ENTITY_DECL: |
| 5127 | break; |
| 5128 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5129 | doc = cur->doc; |
| 5130 | if (doc != NULL) |
| 5131 | dict = doc->dict; |
| 5132 | else |
| 5133 | dict = NULL; |
| 5134 | if (dict != NULL) { |
| 5135 | if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name))) |
| 5136 | xmlFree((xmlChar *) cur->name); |
| 5137 | cur->name = xmlDictLookup(dict, name, -1); |
| 5138 | } else { |
| 5139 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 5140 | cur->name = xmlStrdup(name); |
| 5141 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5142 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 5143 | #endif |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5144 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 5145 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5146 | /** |
| 5147 | * xmlNodeSetBase: |
| 5148 | * @cur: the node being changed |
| 5149 | * @uri: the new base URI |
| 5150 | * |
| 5151 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 5152 | * xml:base attribute. |
| 5153 | */ |
| 5154 | void |
Daniel Veillard | f85ce8e | 2003-09-22 10:24:45 +0000 | [diff] [blame] | 5155 | xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5156 | xmlNsPtr ns; |
Martin Trappel | f370310 | 2010-01-22 12:08:00 +0100 | [diff] [blame] | 5157 | xmlChar* fixed; |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5158 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5159 | if (cur == NULL) return; |
| 5160 | switch(cur->type) { |
| 5161 | case XML_TEXT_NODE: |
| 5162 | case XML_CDATA_SECTION_NODE: |
| 5163 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5164 | case XML_DOCUMENT_TYPE_NODE: |
| 5165 | case XML_DOCUMENT_FRAG_NODE: |
| 5166 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5167 | case XML_DTD_NODE: |
| 5168 | case XML_ELEMENT_DECL: |
| 5169 | case XML_ATTRIBUTE_DECL: |
| 5170 | case XML_ENTITY_DECL: |
| 5171 | case XML_PI_NODE: |
| 5172 | case XML_ENTITY_REF_NODE: |
| 5173 | case XML_ENTITY_NODE: |
| 5174 | case XML_NAMESPACE_DECL: |
| 5175 | case XML_XINCLUDE_START: |
| 5176 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5177 | return; |
| 5178 | case XML_ELEMENT_NODE: |
| 5179 | case XML_ATTRIBUTE_NODE: |
| 5180 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 5181 | case XML_DOCUMENT_NODE: |
| 5182 | #ifdef LIBXML_DOCB_ENABLED |
| 5183 | case XML_DOCB_DOCUMENT_NODE: |
| 5184 | #endif |
| 5185 | case XML_HTML_DOCUMENT_NODE: { |
| 5186 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 5187 | |
| 5188 | if (doc->URL != NULL) |
| 5189 | xmlFree((xmlChar *) doc->URL); |
| 5190 | if (uri == NULL) |
| 5191 | doc->URL = NULL; |
| 5192 | else |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5193 | doc->URL = xmlPathToURI(uri); |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 5194 | return; |
| 5195 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5196 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5197 | |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5198 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 5199 | if (ns == NULL) |
| 5200 | return; |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5201 | fixed = xmlPathToURI(uri); |
| 5202 | if (fixed != NULL) { |
| 5203 | xmlSetNsProp(cur, ns, BAD_CAST "base", fixed); |
Martin Trappel | f370310 | 2010-01-22 12:08:00 +0100 | [diff] [blame] | 5204 | xmlFree(fixed); |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5205 | } else { |
| 5206 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
| 5207 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5208 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5209 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5210 | |
| 5211 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5212 | * xmlNodeGetBase: |
| 5213 | * @doc: the document the node pertains to |
| 5214 | * @cur: the node being checked |
| 5215 | * |
| 5216 | * Searches for the BASE URL. The code should work on both XML |
| 5217 | * and HTML document even if base mechanisms are completely different. |
| 5218 | * It returns the base as defined in RFC 2396 sections |
| 5219 | * 5.1.1. Base URI within Document Content |
| 5220 | * and |
| 5221 | * 5.1.2. Base URI from the Encapsulating Entity |
| 5222 | * However it does not return the document base (5.1.3), use |
Daniel Veillard | e4d1849 | 2010-03-09 11:12:30 +0100 | [diff] [blame] | 5223 | * doc->URL in this case |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5224 | * |
| 5225 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5226 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5227 | */ |
| 5228 | xmlChar * |
| 5229 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5230 | xmlChar *oldbase = NULL; |
| 5231 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5232 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5233 | if ((cur == NULL) && (doc == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5234 | return(NULL); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 5235 | if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) |
| 5236 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5237 | if (doc == NULL) doc = cur->doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5238 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 5239 | cur = doc->children; |
| 5240 | while ((cur != NULL) && (cur->name != NULL)) { |
| 5241 | if (cur->type != XML_ELEMENT_NODE) { |
| 5242 | cur = cur->next; |
| 5243 | continue; |
| 5244 | } |
| 5245 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 5246 | cur = cur->children; |
| 5247 | continue; |
| 5248 | } |
| 5249 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 5250 | cur = cur->children; |
| 5251 | continue; |
| 5252 | } |
| 5253 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 5254 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 5255 | } |
| 5256 | cur = cur->next; |
| 5257 | } |
| 5258 | return(NULL); |
| 5259 | } |
| 5260 | while (cur != NULL) { |
| 5261 | if (cur->type == XML_ENTITY_DECL) { |
| 5262 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 5263 | return(xmlStrdup(ent->URI)); |
| 5264 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5265 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5266 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5267 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5268 | if (oldbase != NULL) { |
| 5269 | newbase = xmlBuildURI(oldbase, base); |
| 5270 | if (newbase != NULL) { |
| 5271 | xmlFree(oldbase); |
| 5272 | xmlFree(base); |
| 5273 | oldbase = newbase; |
| 5274 | } else { |
| 5275 | xmlFree(oldbase); |
| 5276 | xmlFree(base); |
| 5277 | return(NULL); |
| 5278 | } |
| 5279 | } else { |
| 5280 | oldbase = base; |
| 5281 | } |
| 5282 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 5283 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 5284 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 5285 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5286 | } |
| 5287 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5288 | cur = cur->parent; |
| 5289 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5290 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 5291 | if (oldbase == NULL) |
| 5292 | return(xmlStrdup(doc->URL)); |
| 5293 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 5294 | xmlFree(oldbase); |
| 5295 | return(newbase); |
| 5296 | } |
| 5297 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5298 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5299 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5300 | /** |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5301 | * xmlNodeBufGetContent: |
| 5302 | * @buffer: a buffer |
| 5303 | * @cur: the node being read |
| 5304 | * |
| 5305 | * Read the value of a node @cur, this can be either the text carried |
| 5306 | * directly by this node if it's a TEXT node or the aggregate string |
| 5307 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 5308 | * Entity references are substituted. |
| 5309 | * Fills up the buffer @buffer with this value |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5310 | * |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5311 | * Returns 0 in case of success and -1 in case of error. |
| 5312 | */ |
| 5313 | int |
| 5314 | xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur) |
| 5315 | { |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5316 | xmlBufPtr buf; |
| 5317 | int ret; |
| 5318 | |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5319 | if ((cur == NULL) || (buffer == NULL)) return(-1); |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5320 | buf = xmlBufFromBuffer(buffer); |
| 5321 | ret = xmlBufGetNodeContent(buf, cur); |
| 5322 | buffer = xmlBufBackToBuffer(buf); |
| 5323 | if ((ret < 0) || (buffer == NULL)) |
| 5324 | return(-1); |
| 5325 | return(0); |
| 5326 | } |
| 5327 | |
| 5328 | /** |
| 5329 | * xmlBufGetNodeContent: |
Daniel Veillard | 28cc42d | 2012-08-10 10:00:18 +0800 | [diff] [blame] | 5330 | * @buf: a buffer xmlBufPtr |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5331 | * @cur: the node being read |
| 5332 | * |
| 5333 | * Read the value of a node @cur, this can be either the text carried |
| 5334 | * directly by this node if it's a TEXT node or the aggregate string |
| 5335 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 5336 | * Entity references are substituted. |
| 5337 | * Fills up the buffer @buffer with this value |
| 5338 | * |
| 5339 | * Returns 0 in case of success and -1 in case of error. |
| 5340 | */ |
| 5341 | int |
| 5342 | xmlBufGetNodeContent(xmlBufPtr buf, xmlNodePtr cur) |
| 5343 | { |
| 5344 | if ((cur == NULL) || (buf == NULL)) return(-1); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5345 | switch (cur->type) { |
| 5346 | case XML_CDATA_SECTION_NODE: |
| 5347 | case XML_TEXT_NODE: |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5348 | xmlBufCat(buf, cur->content); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5349 | break; |
| 5350 | case XML_DOCUMENT_FRAG_NODE: |
| 5351 | case XML_ELEMENT_NODE:{ |
| 5352 | xmlNodePtr tmp = cur; |
| 5353 | |
| 5354 | while (tmp != NULL) { |
| 5355 | switch (tmp->type) { |
| 5356 | case XML_CDATA_SECTION_NODE: |
| 5357 | case XML_TEXT_NODE: |
| 5358 | if (tmp->content != NULL) |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5359 | xmlBufCat(buf, tmp->content); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5360 | break; |
| 5361 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5362 | xmlBufGetNodeContent(buf, tmp); |
Rob Richards | 77b92ff | 2005-12-20 15:55:14 +0000 | [diff] [blame] | 5363 | break; |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5364 | default: |
| 5365 | break; |
| 5366 | } |
| 5367 | /* |
| 5368 | * Skip to next node |
| 5369 | */ |
| 5370 | if (tmp->children != NULL) { |
| 5371 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 5372 | tmp = tmp->children; |
| 5373 | continue; |
| 5374 | } |
| 5375 | } |
| 5376 | if (tmp == cur) |
| 5377 | break; |
| 5378 | |
| 5379 | if (tmp->next != NULL) { |
| 5380 | tmp = tmp->next; |
| 5381 | continue; |
| 5382 | } |
| 5383 | |
| 5384 | do { |
| 5385 | tmp = tmp->parent; |
| 5386 | if (tmp == NULL) |
| 5387 | break; |
| 5388 | if (tmp == cur) { |
| 5389 | tmp = NULL; |
| 5390 | break; |
| 5391 | } |
| 5392 | if (tmp->next != NULL) { |
| 5393 | tmp = tmp->next; |
| 5394 | break; |
| 5395 | } |
| 5396 | } while (tmp != NULL); |
| 5397 | } |
| 5398 | break; |
| 5399 | } |
| 5400 | case XML_ATTRIBUTE_NODE:{ |
| 5401 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 5402 | xmlNodePtr tmp = attr->children; |
| 5403 | |
| 5404 | while (tmp != NULL) { |
| 5405 | if (tmp->type == XML_TEXT_NODE) |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5406 | xmlBufCat(buf, tmp->content); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5407 | else |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5408 | xmlBufGetNodeContent(buf, tmp); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5409 | tmp = tmp->next; |
| 5410 | } |
| 5411 | break; |
| 5412 | } |
| 5413 | case XML_COMMENT_NODE: |
| 5414 | case XML_PI_NODE: |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5415 | xmlBufCat(buf, cur->content); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5416 | break; |
| 5417 | case XML_ENTITY_REF_NODE:{ |
| 5418 | xmlEntityPtr ent; |
| 5419 | xmlNodePtr tmp; |
| 5420 | |
| 5421 | /* lookup entity declaration */ |
| 5422 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5423 | if (ent == NULL) |
| 5424 | return(-1); |
| 5425 | |
| 5426 | /* an entity content can be any "well balanced chunk", |
| 5427 | * i.e. the result of the content [43] production: |
| 5428 | * http://www.w3.org/TR/REC-xml#NT-content |
| 5429 | * -> we iterate through child nodes and recursive call |
| 5430 | * xmlNodeGetContent() which handles all possible node types */ |
| 5431 | tmp = ent->children; |
| 5432 | while (tmp) { |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5433 | xmlBufGetNodeContent(buf, tmp); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5434 | tmp = tmp->next; |
| 5435 | } |
| 5436 | break; |
| 5437 | } |
| 5438 | case XML_ENTITY_NODE: |
| 5439 | case XML_DOCUMENT_TYPE_NODE: |
| 5440 | case XML_NOTATION_NODE: |
| 5441 | case XML_DTD_NODE: |
| 5442 | case XML_XINCLUDE_START: |
| 5443 | case XML_XINCLUDE_END: |
| 5444 | break; |
| 5445 | case XML_DOCUMENT_NODE: |
| 5446 | #ifdef LIBXML_DOCB_ENABLED |
| 5447 | case XML_DOCB_DOCUMENT_NODE: |
| 5448 | #endif |
| 5449 | case XML_HTML_DOCUMENT_NODE: |
| 5450 | cur = cur->children; |
| 5451 | while (cur!= NULL) { |
| 5452 | if ((cur->type == XML_ELEMENT_NODE) || |
| 5453 | (cur->type == XML_TEXT_NODE) || |
| 5454 | (cur->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5455 | xmlBufGetNodeContent(buf, cur); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5456 | } |
| 5457 | cur = cur->next; |
| 5458 | } |
| 5459 | break; |
| 5460 | case XML_NAMESPACE_DECL: |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5461 | xmlBufCat(buf, ((xmlNsPtr) cur)->href); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5462 | break; |
| 5463 | case XML_ELEMENT_DECL: |
| 5464 | case XML_ATTRIBUTE_DECL: |
| 5465 | case XML_ENTITY_DECL: |
| 5466 | break; |
| 5467 | } |
| 5468 | return(0); |
| 5469 | } |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5470 | |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5471 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5472 | * xmlNodeGetContent: |
| 5473 | * @cur: the node being read |
| 5474 | * |
| 5475 | * Read the value of a node, this can be either the text carried |
| 5476 | * directly by this node if it's a TEXT node or the aggregate string |
| 5477 | * 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] | 5478 | * Entity references are substituted. |
| 5479 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5480 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5481 | */ |
| 5482 | xmlChar * |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5483 | xmlNodeGetContent(xmlNodePtr cur) |
| 5484 | { |
| 5485 | if (cur == NULL) |
| 5486 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5487 | switch (cur->type) { |
| 5488 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5489 | case XML_ELEMENT_NODE:{ |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5490 | xmlBufPtr buf; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5491 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5492 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5493 | buf = xmlBufCreateSize(64); |
| 5494 | if (buf == NULL) |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5495 | return (NULL); |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5496 | xmlBufGetNodeContent(buf, cur); |
| 5497 | ret = xmlBufDetach(buf); |
| 5498 | xmlBufFree(buf); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5499 | return (ret); |
| 5500 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5501 | case XML_ATTRIBUTE_NODE: |
| 5502 | return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5503 | case XML_COMMENT_NODE: |
| 5504 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5505 | if (cur->content != NULL) |
| 5506 | return (xmlStrdup(cur->content)); |
| 5507 | return (NULL); |
| 5508 | case XML_ENTITY_REF_NODE:{ |
| 5509 | xmlEntityPtr ent; |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5510 | xmlBufPtr buf; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5511 | xmlChar *ret; |
| 5512 | |
| 5513 | /* lookup entity declaration */ |
| 5514 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5515 | if (ent == NULL) |
| 5516 | return (NULL); |
| 5517 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5518 | buf = xmlBufCreate(); |
| 5519 | if (buf == NULL) |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5520 | return (NULL); |
| 5521 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5522 | xmlBufGetNodeContent(buf, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5523 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5524 | ret = xmlBufDetach(buf); |
| 5525 | xmlBufFree(buf); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5526 | return (ret); |
| 5527 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5528 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5529 | case XML_DOCUMENT_TYPE_NODE: |
| 5530 | case XML_NOTATION_NODE: |
| 5531 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5532 | case XML_XINCLUDE_START: |
| 5533 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5534 | return (NULL); |
| 5535 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5536 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5537 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5538 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5539 | case XML_HTML_DOCUMENT_NODE: { |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5540 | xmlBufPtr buf; |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5541 | xmlChar *ret; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5542 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5543 | buf = xmlBufCreate(); |
| 5544 | if (buf == NULL) |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5545 | return (NULL); |
| 5546 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5547 | xmlBufGetNodeContent(buf, (xmlNodePtr) cur); |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5548 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5549 | ret = xmlBufDetach(buf); |
| 5550 | xmlBufFree(buf); |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5551 | return (ret); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5552 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 5553 | case XML_NAMESPACE_DECL: { |
| 5554 | xmlChar *tmp; |
| 5555 | |
| 5556 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 5557 | return (tmp); |
| 5558 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5559 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5560 | /* TODO !!! */ |
| 5561 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5562 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5563 | /* TODO !!! */ |
| 5564 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5565 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5566 | /* TODO !!! */ |
| 5567 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5568 | case XML_CDATA_SECTION_NODE: |
| 5569 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5570 | if (cur->content != NULL) |
| 5571 | return (xmlStrdup(cur->content)); |
| 5572 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5573 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5574 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5575 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5576 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5577 | /** |
| 5578 | * xmlNodeSetContent: |
| 5579 | * @cur: the node being modified |
| 5580 | * @content: the new value of the content |
| 5581 | * |
| 5582 | * Replace the content of a node. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5583 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 5584 | * references, but XML special chars need to be escaped first by using |
| 5585 | * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5586 | */ |
| 5587 | void |
| 5588 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 5589 | if (cur == NULL) { |
| 5590 | #ifdef DEBUG_TREE |
| 5591 | xmlGenericError(xmlGenericErrorContext, |
| 5592 | "xmlNodeSetContent : node == NULL\n"); |
| 5593 | #endif |
| 5594 | return; |
| 5595 | } |
| 5596 | switch (cur->type) { |
| 5597 | case XML_DOCUMENT_FRAG_NODE: |
| 5598 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5599 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5600 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5601 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 5602 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5603 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5604 | case XML_TEXT_NODE: |
| 5605 | case XML_CDATA_SECTION_NODE: |
| 5606 | case XML_ENTITY_REF_NODE: |
| 5607 | case XML_ENTITY_NODE: |
| 5608 | case XML_PI_NODE: |
| 5609 | case XML_COMMENT_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5610 | if ((cur->content != NULL) && |
| 5611 | (cur->content != (xmlChar *) &(cur->properties))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5612 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
Daniel Veillard | c587bce | 2005-05-10 15:28:08 +0000 | [diff] [blame] | 5613 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5614 | xmlFree(cur->content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5615 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5616 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5617 | cur->last = cur->children = NULL; |
| 5618 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5619 | cur->content = xmlStrdup(content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5620 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5621 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5622 | cur->properties = NULL; |
| 5623 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5624 | break; |
| 5625 | case XML_DOCUMENT_NODE: |
| 5626 | case XML_HTML_DOCUMENT_NODE: |
| 5627 | case XML_DOCUMENT_TYPE_NODE: |
| 5628 | case XML_XINCLUDE_START: |
| 5629 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5630 | #ifdef LIBXML_DOCB_ENABLED |
| 5631 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5632 | #endif |
| 5633 | break; |
| 5634 | case XML_NOTATION_NODE: |
| 5635 | break; |
| 5636 | case XML_DTD_NODE: |
| 5637 | break; |
| 5638 | case XML_NAMESPACE_DECL: |
| 5639 | break; |
| 5640 | case XML_ELEMENT_DECL: |
| 5641 | /* TODO !!! */ |
| 5642 | break; |
| 5643 | case XML_ATTRIBUTE_DECL: |
| 5644 | /* TODO !!! */ |
| 5645 | break; |
| 5646 | case XML_ENTITY_DECL: |
| 5647 | /* TODO !!! */ |
| 5648 | break; |
| 5649 | } |
| 5650 | } |
| 5651 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5652 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5653 | /** |
| 5654 | * xmlNodeSetContentLen: |
| 5655 | * @cur: the node being modified |
| 5656 | * @content: the new value of the content |
| 5657 | * @len: the size of @content |
| 5658 | * |
| 5659 | * Replace the content of a node. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5660 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 5661 | * references, but XML special chars need to be escaped first by using |
| 5662 | * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5663 | */ |
| 5664 | void |
| 5665 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5666 | if (cur == NULL) { |
| 5667 | #ifdef DEBUG_TREE |
| 5668 | xmlGenericError(xmlGenericErrorContext, |
| 5669 | "xmlNodeSetContentLen : node == NULL\n"); |
| 5670 | #endif |
| 5671 | return; |
| 5672 | } |
| 5673 | switch (cur->type) { |
| 5674 | case XML_DOCUMENT_FRAG_NODE: |
| 5675 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5676 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5677 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5678 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 5679 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5680 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5681 | case XML_TEXT_NODE: |
| 5682 | case XML_CDATA_SECTION_NODE: |
| 5683 | case XML_ENTITY_REF_NODE: |
| 5684 | case XML_ENTITY_NODE: |
| 5685 | case XML_PI_NODE: |
| 5686 | case XML_COMMENT_NODE: |
| 5687 | case XML_NOTATION_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5688 | if ((cur->content != NULL) && |
| 5689 | (cur->content != (xmlChar *) &(cur->properties))) { |
| 5690 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5691 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
| 5692 | xmlFree(cur->content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5693 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5694 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5695 | cur->children = cur->last = NULL; |
| 5696 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5697 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5698 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5699 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5700 | cur->properties = NULL; |
| 5701 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5702 | break; |
| 5703 | case XML_DOCUMENT_NODE: |
| 5704 | case XML_DTD_NODE: |
| 5705 | case XML_HTML_DOCUMENT_NODE: |
| 5706 | case XML_DOCUMENT_TYPE_NODE: |
| 5707 | case XML_NAMESPACE_DECL: |
| 5708 | case XML_XINCLUDE_START: |
| 5709 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5710 | #ifdef LIBXML_DOCB_ENABLED |
| 5711 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5712 | #endif |
| 5713 | break; |
| 5714 | case XML_ELEMENT_DECL: |
| 5715 | /* TODO !!! */ |
| 5716 | break; |
| 5717 | case XML_ATTRIBUTE_DECL: |
| 5718 | /* TODO !!! */ |
| 5719 | break; |
| 5720 | case XML_ENTITY_DECL: |
| 5721 | /* TODO !!! */ |
| 5722 | break; |
| 5723 | } |
| 5724 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5725 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5726 | |
| 5727 | /** |
| 5728 | * xmlNodeAddContentLen: |
| 5729 | * @cur: the node being modified |
| 5730 | * @content: extra content |
| 5731 | * @len: the size of @content |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5732 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5733 | * Append the extra substring to the node content. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5734 | * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be |
| 5735 | * raw text, so unescaped XML special chars are allowed, entity |
| 5736 | * references are not supported. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5737 | */ |
| 5738 | void |
| 5739 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5740 | if (cur == NULL) { |
| 5741 | #ifdef DEBUG_TREE |
| 5742 | xmlGenericError(xmlGenericErrorContext, |
| 5743 | "xmlNodeAddContentLen : node == NULL\n"); |
| 5744 | #endif |
| 5745 | return; |
| 5746 | } |
| 5747 | if (len <= 0) return; |
| 5748 | switch (cur->type) { |
| 5749 | case XML_DOCUMENT_FRAG_NODE: |
| 5750 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5751 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5752 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 5753 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5754 | newNode = xmlNewTextLen(content, len); |
| 5755 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5756 | tmp = xmlAddChild(cur, newNode); |
| 5757 | if (tmp != newNode) |
| 5758 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5759 | if ((last != NULL) && (last->next == newNode)) { |
| 5760 | xmlTextMerge(last, newNode); |
| 5761 | } |
| 5762 | } |
| 5763 | break; |
| 5764 | } |
| 5765 | case XML_ATTRIBUTE_NODE: |
| 5766 | break; |
| 5767 | case XML_TEXT_NODE: |
| 5768 | case XML_CDATA_SECTION_NODE: |
| 5769 | case XML_ENTITY_REF_NODE: |
| 5770 | case XML_ENTITY_NODE: |
| 5771 | case XML_PI_NODE: |
| 5772 | case XML_COMMENT_NODE: |
| 5773 | case XML_NOTATION_NODE: |
| 5774 | if (content != NULL) { |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5775 | if ((cur->content == (xmlChar *) &(cur->properties)) || |
| 5776 | ((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5777 | xmlDictOwns(cur->doc->dict, cur->content))) { |
| 5778 | cur->content = xmlStrncatNew(cur->content, content, len); |
| 5779 | cur->properties = NULL; |
| 5780 | cur->nsDef = NULL; |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5781 | break; |
| 5782 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5783 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5784 | } |
| 5785 | case XML_DOCUMENT_NODE: |
| 5786 | case XML_DTD_NODE: |
| 5787 | case XML_HTML_DOCUMENT_NODE: |
| 5788 | case XML_DOCUMENT_TYPE_NODE: |
| 5789 | case XML_NAMESPACE_DECL: |
| 5790 | case XML_XINCLUDE_START: |
| 5791 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5792 | #ifdef LIBXML_DOCB_ENABLED |
| 5793 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5794 | #endif |
| 5795 | break; |
| 5796 | case XML_ELEMENT_DECL: |
| 5797 | case XML_ATTRIBUTE_DECL: |
| 5798 | case XML_ENTITY_DECL: |
| 5799 | break; |
| 5800 | } |
| 5801 | } |
| 5802 | |
| 5803 | /** |
| 5804 | * xmlNodeAddContent: |
| 5805 | * @cur: the node being modified |
| 5806 | * @content: extra content |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5807 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5808 | * Append the extra substring to the node content. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5809 | * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be |
| 5810 | * raw text, so unescaped XML special chars are allowed, entity |
| 5811 | * references are not supported. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5812 | */ |
| 5813 | void |
| 5814 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5815 | int len; |
| 5816 | |
| 5817 | if (cur == NULL) { |
| 5818 | #ifdef DEBUG_TREE |
| 5819 | xmlGenericError(xmlGenericErrorContext, |
| 5820 | "xmlNodeAddContent : node == NULL\n"); |
| 5821 | #endif |
| 5822 | return; |
| 5823 | } |
| 5824 | if (content == NULL) return; |
| 5825 | len = xmlStrlen(content); |
| 5826 | xmlNodeAddContentLen(cur, content, len); |
| 5827 | } |
| 5828 | |
| 5829 | /** |
| 5830 | * xmlTextMerge: |
| 5831 | * @first: the first text node |
| 5832 | * @second: the second text node being merged |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5833 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5834 | * Merge two text nodes into one |
| 5835 | * Returns the first text node augmented |
| 5836 | */ |
| 5837 | xmlNodePtr |
| 5838 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5839 | if (first == NULL) return(second); |
| 5840 | if (second == NULL) return(first); |
| 5841 | if (first->type != XML_TEXT_NODE) return(first); |
| 5842 | if (second->type != XML_TEXT_NODE) return(first); |
| 5843 | if (second->name != first->name) |
| 5844 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5845 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5846 | xmlUnlinkNode(second); |
| 5847 | xmlFreeNode(second); |
| 5848 | return(first); |
| 5849 | } |
| 5850 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 5851 | #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] | 5852 | /** |
| 5853 | * xmlGetNsList: |
| 5854 | * @doc: the document |
| 5855 | * @node: the current node |
| 5856 | * |
| 5857 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5858 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5859 | * that need to be freed by the caller or NULL if no |
| 5860 | * namespace if defined |
| 5861 | */ |
| 5862 | xmlNsPtr * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5863 | xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) |
| 5864 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5865 | xmlNsPtr cur; |
| 5866 | xmlNsPtr *ret = NULL; |
| 5867 | int nbns = 0; |
| 5868 | int maxns = 10; |
| 5869 | int i; |
| 5870 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 5871 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 5872 | return(NULL); |
| 5873 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5874 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5875 | if (node->type == XML_ELEMENT_NODE) { |
| 5876 | cur = node->nsDef; |
| 5877 | while (cur != NULL) { |
| 5878 | if (ret == NULL) { |
| 5879 | ret = |
| 5880 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5881 | sizeof(xmlNsPtr)); |
| 5882 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5883 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5884 | return (NULL); |
| 5885 | } |
| 5886 | ret[nbns] = NULL; |
| 5887 | } |
| 5888 | for (i = 0; i < nbns; i++) { |
| 5889 | if ((cur->prefix == ret[i]->prefix) || |
| 5890 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5891 | break; |
| 5892 | } |
| 5893 | if (i >= nbns) { |
| 5894 | if (nbns >= maxns) { |
| 5895 | maxns *= 2; |
| 5896 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5897 | (maxns + |
| 5898 | 1) * |
| 5899 | sizeof(xmlNsPtr)); |
| 5900 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5901 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5902 | return (NULL); |
| 5903 | } |
| 5904 | } |
| 5905 | ret[nbns++] = cur; |
| 5906 | ret[nbns] = NULL; |
| 5907 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5908 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5909 | cur = cur->next; |
| 5910 | } |
| 5911 | } |
| 5912 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5913 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5914 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5915 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5916 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5917 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5918 | /* |
| 5919 | * xmlTreeEnsureXMLDecl: |
| 5920 | * @doc: the doc |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5921 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5922 | * Ensures that there is an XML namespace declaration on the doc. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5923 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5924 | * Returns the XML ns-struct or NULL on API and internal errors. |
| 5925 | */ |
| 5926 | static xmlNsPtr |
| 5927 | xmlTreeEnsureXMLDecl(xmlDocPtr doc) |
| 5928 | { |
| 5929 | if (doc == NULL) |
| 5930 | return (NULL); |
| 5931 | if (doc->oldNs != NULL) |
| 5932 | return (doc->oldNs); |
| 5933 | { |
| 5934 | xmlNsPtr ns; |
| 5935 | ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5936 | if (ns == NULL) { |
| 5937 | xmlTreeErrMemory( |
| 5938 | "allocating the XML namespace"); |
| 5939 | return (NULL); |
| 5940 | } |
| 5941 | memset(ns, 0, sizeof(xmlNs)); |
| 5942 | ns->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5943 | ns->href = xmlStrdup(XML_XML_NAMESPACE); |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5944 | ns->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5945 | doc->oldNs = ns; |
| 5946 | return (ns); |
| 5947 | } |
| 5948 | } |
| 5949 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5950 | /** |
| 5951 | * xmlSearchNs: |
| 5952 | * @doc: the document |
| 5953 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5954 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5955 | * |
| 5956 | * Search a Ns registered under a given name space for a document. |
| 5957 | * recurse on the parents until it finds the defined namespace |
| 5958 | * or return NULL otherwise. |
| 5959 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5960 | * We don't allow to cross entities boundaries. If you don't declare |
| 5961 | * the namespace within those you will be in troubles !!! A warning |
| 5962 | * is generated to cover this case. |
| 5963 | * |
| 5964 | * Returns the namespace pointer or NULL. |
| 5965 | */ |
| 5966 | xmlNsPtr |
| 5967 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5968 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5969 | xmlNsPtr cur; |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 5970 | xmlNodePtr orig = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5971 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 5972 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5973 | if ((nameSpace != NULL) && |
| 5974 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5975 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 5976 | /* |
| 5977 | * The XML-1.0 namespace is normally held on the root |
| 5978 | * element. In this case exceptionally create it on the |
| 5979 | * node element. |
| 5980 | */ |
| 5981 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5982 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5983 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5984 | return(NULL); |
| 5985 | } |
| 5986 | memset(cur, 0, sizeof(xmlNs)); |
| 5987 | cur->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5988 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 5989 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 5990 | cur->next = node->nsDef; |
| 5991 | node->nsDef = cur; |
| 5992 | return(cur); |
| 5993 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5994 | if (doc == NULL) { |
| 5995 | doc = node->doc; |
| 5996 | if (doc == NULL) |
| 5997 | return(NULL); |
| 5998 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5999 | /* |
| 6000 | * Return the XML namespace declaration held by the doc. |
| 6001 | */ |
| 6002 | if (doc->oldNs == NULL) |
| 6003 | return(xmlTreeEnsureXMLDecl(doc)); |
| 6004 | else |
| 6005 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6006 | } |
| 6007 | while (node != NULL) { |
| 6008 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 6009 | (node->type == XML_ENTITY_NODE) || |
| 6010 | (node->type == XML_ENTITY_DECL)) |
| 6011 | return(NULL); |
| 6012 | if (node->type == XML_ELEMENT_NODE) { |
| 6013 | cur = node->nsDef; |
| 6014 | while (cur != NULL) { |
| 6015 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 6016 | (cur->href != NULL)) |
| 6017 | return(cur); |
| 6018 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 6019 | (cur->href != NULL) && |
| 6020 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 6021 | return(cur); |
| 6022 | cur = cur->next; |
| 6023 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6024 | if (orig != node) { |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 6025 | cur = node->ns; |
| 6026 | if (cur != NULL) { |
| 6027 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 6028 | (cur->href != NULL)) |
| 6029 | return(cur); |
| 6030 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 6031 | (cur->href != NULL) && |
| 6032 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 6033 | return(cur); |
| 6034 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6035 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6036 | } |
| 6037 | node = node->parent; |
| 6038 | } |
| 6039 | return(NULL); |
| 6040 | } |
| 6041 | |
| 6042 | /** |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6043 | * xmlNsInScope: |
| 6044 | * @doc: the document |
| 6045 | * @node: the current node |
| 6046 | * @ancestor: the ancestor carrying the namespace |
| 6047 | * @prefix: the namespace prefix |
| 6048 | * |
| 6049 | * Verify that the given namespace held on @ancestor is still in scope |
| 6050 | * on node. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6051 | * |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6052 | * Returns 1 if true, 0 if false and -1 in case of error. |
| 6053 | */ |
| 6054 | static int |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6055 | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 6056 | xmlNodePtr ancestor, const xmlChar * prefix) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6057 | { |
| 6058 | xmlNsPtr tst; |
| 6059 | |
| 6060 | while ((node != NULL) && (node != ancestor)) { |
| 6061 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 6062 | (node->type == XML_ENTITY_NODE) || |
| 6063 | (node->type == XML_ENTITY_DECL)) |
| 6064 | return (-1); |
| 6065 | if (node->type == XML_ELEMENT_NODE) { |
| 6066 | tst = node->nsDef; |
| 6067 | while (tst != NULL) { |
| 6068 | if ((tst->prefix == NULL) |
| 6069 | && (prefix == NULL)) |
| 6070 | return (0); |
| 6071 | if ((tst->prefix != NULL) |
| 6072 | && (prefix != NULL) |
| 6073 | && (xmlStrEqual(tst->prefix, prefix))) |
| 6074 | return (0); |
| 6075 | tst = tst->next; |
| 6076 | } |
| 6077 | } |
| 6078 | node = node->parent; |
| 6079 | } |
| 6080 | if (node != ancestor) |
| 6081 | return (-1); |
| 6082 | return (1); |
| 6083 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6084 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6085 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6086 | * xmlSearchNsByHref: |
| 6087 | * @doc: the document |
| 6088 | * @node: the current node |
| 6089 | * @href: the namespace value |
| 6090 | * |
| 6091 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 6092 | * the defined namespace or return NULL otherwise. |
| 6093 | * Returns the namespace pointer or NULL. |
| 6094 | */ |
| 6095 | xmlNsPtr |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6096 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) |
| 6097 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6098 | xmlNsPtr cur; |
| 6099 | xmlNodePtr orig = node; |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6100 | int is_attr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6101 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 6102 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL)) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6103 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6104 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6105 | /* |
| 6106 | * Only the document can hold the XML spec namespace. |
| 6107 | */ |
| 6108 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 6109 | /* |
| 6110 | * The XML-1.0 namespace is normally held on the root |
| 6111 | * element. In this case exceptionally create it on the |
| 6112 | * node element. |
| 6113 | */ |
| 6114 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 6115 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6116 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6117 | return (NULL); |
| 6118 | } |
| 6119 | memset(cur, 0, sizeof(xmlNs)); |
| 6120 | cur->type = XML_LOCAL_NAMESPACE; |
| 6121 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 6122 | cur->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 6123 | cur->next = node->nsDef; |
| 6124 | node->nsDef = cur; |
| 6125 | return (cur); |
| 6126 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6127 | if (doc == NULL) { |
| 6128 | doc = node->doc; |
| 6129 | if (doc == NULL) |
| 6130 | return(NULL); |
| 6131 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 6132 | /* |
| 6133 | * Return the XML namespace declaration held by the doc. |
| 6134 | */ |
| 6135 | if (doc->oldNs == NULL) |
| 6136 | return(xmlTreeEnsureXMLDecl(doc)); |
| 6137 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6138 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6139 | } |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6140 | is_attr = (node->type == XML_ATTRIBUTE_NODE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6141 | while (node != NULL) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6142 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 6143 | (node->type == XML_ENTITY_NODE) || |
| 6144 | (node->type == XML_ENTITY_DECL)) |
| 6145 | return (NULL); |
| 6146 | if (node->type == XML_ELEMENT_NODE) { |
| 6147 | cur = node->nsDef; |
| 6148 | while (cur != NULL) { |
| 6149 | if ((cur->href != NULL) && (href != NULL) && |
| 6150 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6151 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 6152 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6153 | return (cur); |
| 6154 | } |
| 6155 | cur = cur->next; |
| 6156 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 6157 | if (orig != node) { |
| 6158 | cur = node->ns; |
| 6159 | if (cur != NULL) { |
| 6160 | if ((cur->href != NULL) && (href != NULL) && |
| 6161 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6162 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 6163 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 6164 | return (cur); |
| 6165 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6166 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6167 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6168 | } |
| 6169 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6170 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6171 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6175 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6176 | * @doc: the document |
| 6177 | * @tree: a node expected to hold the new namespace |
| 6178 | * @ns: the original namespace |
| 6179 | * |
| 6180 | * This function tries to locate a namespace definition in a tree |
| 6181 | * ancestors, or create a new namespace definition node similar to |
| 6182 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 6183 | * null (default namespace) or reused within the subtree defined by |
| 6184 | * @tree or on one of its ancestors then a new prefix is generated. |
| 6185 | * Returns the (new) namespace definition or NULL in case of error |
| 6186 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 6187 | static xmlNsPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6188 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 6189 | xmlNsPtr def; |
| 6190 | xmlChar prefix[50]; |
| 6191 | int counter = 1; |
| 6192 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 6193 | if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6194 | #ifdef DEBUG_TREE |
| 6195 | xmlGenericError(xmlGenericErrorContext, |
| 6196 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 6197 | #endif |
| 6198 | return(NULL); |
| 6199 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 6200 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6201 | #ifdef DEBUG_TREE |
| 6202 | xmlGenericError(xmlGenericErrorContext, |
| 6203 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 6204 | #endif |
| 6205 | return(NULL); |
| 6206 | } |
| 6207 | /* |
| 6208 | * Search an existing namespace definition inherited. |
| 6209 | */ |
| 6210 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 6211 | if (def != NULL) |
| 6212 | return(def); |
| 6213 | |
| 6214 | /* |
| 6215 | * Find a close prefix which is not already in use. |
| 6216 | * Let's strip namespace prefixes longer than 20 chars ! |
| 6217 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6218 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6219 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6220 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 6221 | snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6222 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6223 | def = xmlSearchNs(doc, tree, prefix); |
| 6224 | while (def != NULL) { |
| 6225 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6226 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6227 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6228 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 6229 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6230 | (char *)ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6231 | def = xmlSearchNs(doc, tree, prefix); |
| 6232 | } |
| 6233 | |
| 6234 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6235 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6236 | */ |
| 6237 | def = xmlNewNs(tree, ns->href, prefix); |
| 6238 | return(def); |
| 6239 | } |
| 6240 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6241 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6242 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6243 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6244 | * @doc: the document |
| 6245 | * @tree: a node defining the subtree to reconciliate |
| 6246 | * |
| 6247 | * This function checks that all the namespaces declared within the given |
| 6248 | * tree are properly declared. This is needed for example after Copy or Cut |
| 6249 | * and then paste operations. The subtree may still hold pointers to |
| 6250 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6251 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6252 | * the new environment. If not possible the new namespaces are redeclared |
| 6253 | * on @tree at the top of the given subtree. |
| 6254 | * Returns the number of namespace declarations created or -1 in case of error. |
| 6255 | */ |
| 6256 | int |
| 6257 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 6258 | xmlNsPtr *oldNs = NULL; |
| 6259 | xmlNsPtr *newNs = NULL; |
| 6260 | int sizeCache = 0; |
| 6261 | int nbCache = 0; |
| 6262 | |
| 6263 | xmlNsPtr n; |
| 6264 | xmlNodePtr node = tree; |
| 6265 | xmlAttrPtr attr; |
| 6266 | int ret = 0, i; |
| 6267 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 6268 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); |
| 6269 | if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1); |
| 6270 | if (node->doc != doc) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6271 | while (node != NULL) { |
| 6272 | /* |
| 6273 | * Reconciliate the node namespace |
| 6274 | */ |
| 6275 | if (node->ns != NULL) { |
| 6276 | /* |
| 6277 | * initialize the cache if needed |
| 6278 | */ |
| 6279 | if (sizeCache == 0) { |
| 6280 | sizeCache = 10; |
| 6281 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6282 | sizeof(xmlNsPtr)); |
| 6283 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6284 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6285 | return(-1); |
| 6286 | } |
| 6287 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6288 | sizeof(xmlNsPtr)); |
| 6289 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6290 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6291 | xmlFree(oldNs); |
| 6292 | return(-1); |
| 6293 | } |
| 6294 | } |
| 6295 | for (i = 0;i < nbCache;i++) { |
| 6296 | if (oldNs[i] == node->ns) { |
| 6297 | node->ns = newNs[i]; |
| 6298 | break; |
| 6299 | } |
| 6300 | } |
| 6301 | if (i == nbCache) { |
| 6302 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6303 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6304 | */ |
| 6305 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 6306 | if (n != NULL) { /* :-( what if else ??? */ |
| 6307 | /* |
| 6308 | * check if we need to grow the cache buffers. |
| 6309 | */ |
| 6310 | if (sizeCache <= nbCache) { |
| 6311 | sizeCache *= 2; |
| 6312 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 6313 | sizeof(xmlNsPtr)); |
| 6314 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6315 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6316 | xmlFree(newNs); |
| 6317 | return(-1); |
| 6318 | } |
| 6319 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 6320 | sizeof(xmlNsPtr)); |
| 6321 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6322 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6323 | xmlFree(oldNs); |
| 6324 | return(-1); |
| 6325 | } |
| 6326 | } |
| 6327 | newNs[nbCache] = n; |
| 6328 | oldNs[nbCache++] = node->ns; |
| 6329 | node->ns = n; |
| 6330 | } |
| 6331 | } |
| 6332 | } |
| 6333 | /* |
| 6334 | * now check for namespace hold by attributes on the node. |
| 6335 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6336 | if (node->type == XML_ELEMENT_NODE) { |
| 6337 | attr = node->properties; |
| 6338 | while (attr != NULL) { |
| 6339 | if (attr->ns != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6340 | /* |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6341 | * initialize the cache if needed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6342 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6343 | if (sizeCache == 0) { |
| 6344 | sizeCache = 10; |
| 6345 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6346 | sizeof(xmlNsPtr)); |
| 6347 | if (oldNs == NULL) { |
| 6348 | xmlTreeErrMemory("fixing namespaces"); |
| 6349 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6350 | } |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6351 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6352 | sizeof(xmlNsPtr)); |
| 6353 | if (newNs == NULL) { |
| 6354 | xmlTreeErrMemory("fixing namespaces"); |
| 6355 | xmlFree(oldNs); |
| 6356 | return(-1); |
| 6357 | } |
| 6358 | } |
| 6359 | for (i = 0;i < nbCache;i++) { |
| 6360 | if (oldNs[i] == attr->ns) { |
| 6361 | attr->ns = newNs[i]; |
| 6362 | break; |
| 6363 | } |
| 6364 | } |
| 6365 | if (i == nbCache) { |
| 6366 | /* |
| 6367 | * OK we need to recreate a new namespace definition |
| 6368 | */ |
| 6369 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 6370 | if (n != NULL) { /* :-( what if else ??? */ |
| 6371 | /* |
| 6372 | * check if we need to grow the cache buffers. |
| 6373 | */ |
| 6374 | if (sizeCache <= nbCache) { |
| 6375 | sizeCache *= 2; |
| 6376 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, |
| 6377 | sizeCache * sizeof(xmlNsPtr)); |
| 6378 | if (oldNs == NULL) { |
| 6379 | xmlTreeErrMemory("fixing namespaces"); |
| 6380 | xmlFree(newNs); |
| 6381 | return(-1); |
| 6382 | } |
| 6383 | newNs = (xmlNsPtr *) xmlRealloc(newNs, |
| 6384 | sizeCache * sizeof(xmlNsPtr)); |
| 6385 | if (newNs == NULL) { |
| 6386 | xmlTreeErrMemory("fixing namespaces"); |
| 6387 | xmlFree(oldNs); |
| 6388 | return(-1); |
| 6389 | } |
| 6390 | } |
| 6391 | newNs[nbCache] = n; |
| 6392 | oldNs[nbCache++] = attr->ns; |
| 6393 | attr->ns = n; |
| 6394 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6395 | } |
| 6396 | } |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6397 | attr = attr->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6398 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6399 | } |
| 6400 | |
| 6401 | /* |
| 6402 | * Browse the full subtree, deep first |
| 6403 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6404 | if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6405 | /* deep first */ |
| 6406 | node = node->children; |
| 6407 | } else if ((node != tree) && (node->next != NULL)) { |
| 6408 | /* then siblings */ |
| 6409 | node = node->next; |
| 6410 | } else if (node != tree) { |
| 6411 | /* go up to parents->next if needed */ |
| 6412 | while (node != tree) { |
| 6413 | if (node->parent != NULL) |
| 6414 | node = node->parent; |
| 6415 | if ((node != tree) && (node->next != NULL)) { |
| 6416 | node = node->next; |
| 6417 | break; |
| 6418 | } |
| 6419 | if (node->parent == NULL) { |
| 6420 | node = NULL; |
| 6421 | break; |
| 6422 | } |
| 6423 | } |
| 6424 | /* exit condition */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6425 | if (node == tree) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6426 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 6427 | } else |
| 6428 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6429 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6430 | if (oldNs != NULL) |
| 6431 | xmlFree(oldNs); |
| 6432 | if (newNs != NULL) |
| 6433 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6434 | return(ret); |
| 6435 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6436 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6437 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6438 | static xmlAttrPtr |
| 6439 | xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name, |
| 6440 | const xmlChar *nsName, int useDTD) |
| 6441 | { |
| 6442 | xmlAttrPtr prop; |
| 6443 | |
| 6444 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6445 | return(NULL); |
| 6446 | |
| 6447 | if (node->properties != NULL) { |
| 6448 | prop = node->properties; |
| 6449 | if (nsName == NULL) { |
| 6450 | /* |
| 6451 | * We want the attr to be in no namespace. |
| 6452 | */ |
| 6453 | do { |
| 6454 | if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) { |
| 6455 | return(prop); |
| 6456 | } |
| 6457 | prop = prop->next; |
| 6458 | } while (prop != NULL); |
| 6459 | } else { |
| 6460 | /* |
| 6461 | * We want the attr to be in the specified namespace. |
| 6462 | */ |
| 6463 | do { |
| 6464 | if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) && |
| 6465 | ((prop->ns->href == nsName) || |
| 6466 | xmlStrEqual(prop->ns->href, nsName))) |
| 6467 | { |
| 6468 | return(prop); |
| 6469 | } |
| 6470 | prop = prop->next; |
| 6471 | } while (prop != NULL); |
| 6472 | } |
| 6473 | } |
| 6474 | |
| 6475 | #ifdef LIBXML_TREE_ENABLED |
| 6476 | if (! useDTD) |
| 6477 | return(NULL); |
| 6478 | /* |
| 6479 | * Check if there is a default/fixed attribute declaration in |
| 6480 | * the internal or external subset. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6481 | */ |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6482 | if ((node->doc != NULL) && (node->doc->intSubset != NULL)) { |
| 6483 | xmlDocPtr doc = node->doc; |
| 6484 | xmlAttributePtr attrDecl = NULL; |
| 6485 | xmlChar *elemQName, *tmpstr = NULL; |
| 6486 | |
| 6487 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6488 | * We need the QName of the element for the DTD-lookup. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6489 | */ |
| 6490 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 6491 | tmpstr = xmlStrdup(node->ns->prefix); |
| 6492 | tmpstr = xmlStrcat(tmpstr, BAD_CAST ":"); |
| 6493 | tmpstr = xmlStrcat(tmpstr, node->name); |
| 6494 | if (tmpstr == NULL) |
| 6495 | return(NULL); |
| 6496 | elemQName = tmpstr; |
| 6497 | } else |
| 6498 | elemQName = (xmlChar *) node->name; |
| 6499 | if (nsName == NULL) { |
| 6500 | /* |
| 6501 | * The common and nice case: Attr in no namespace. |
| 6502 | */ |
| 6503 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, |
| 6504 | elemQName, name, NULL); |
| 6505 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 6506 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, |
| 6507 | elemQName, name, NULL); |
| 6508 | } |
| 6509 | } else { |
| 6510 | xmlNsPtr *nsList, *cur; |
| 6511 | |
| 6512 | /* |
| 6513 | * The ugly case: Search using the prefixes of in-scope |
| 6514 | * ns-decls corresponding to @nsName. |
| 6515 | */ |
| 6516 | nsList = xmlGetNsList(node->doc, node); |
| 6517 | if (nsList == NULL) { |
| 6518 | if (tmpstr != NULL) |
| 6519 | xmlFree(tmpstr); |
| 6520 | return(NULL); |
| 6521 | } |
| 6522 | cur = nsList; |
| 6523 | while (*cur != NULL) { |
| 6524 | if (xmlStrEqual((*cur)->href, nsName)) { |
| 6525 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName, |
| 6526 | name, (*cur)->prefix); |
| 6527 | if (attrDecl) |
| 6528 | break; |
| 6529 | if (doc->extSubset != NULL) { |
| 6530 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName, |
| 6531 | name, (*cur)->prefix); |
| 6532 | if (attrDecl) |
| 6533 | break; |
| 6534 | } |
| 6535 | } |
| 6536 | cur++; |
| 6537 | } |
| 6538 | xmlFree(nsList); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6539 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6540 | if (tmpstr != NULL) |
| 6541 | xmlFree(tmpstr); |
| 6542 | /* |
| 6543 | * Only default/fixed attrs are relevant. |
| 6544 | */ |
| 6545 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6546 | return((xmlAttrPtr) attrDecl); |
| 6547 | } |
| 6548 | #endif /* LIBXML_TREE_ENABLED */ |
| 6549 | return(NULL); |
| 6550 | } |
| 6551 | |
| 6552 | static xmlChar* |
| 6553 | xmlGetPropNodeValueInternal(xmlAttrPtr prop) |
| 6554 | { |
| 6555 | if (prop == NULL) |
| 6556 | return(NULL); |
| 6557 | if (prop->type == XML_ATTRIBUTE_NODE) { |
| 6558 | /* |
| 6559 | * Note that we return at least the empty string. |
| 6560 | * TODO: Do we really always want that? |
| 6561 | */ |
| 6562 | if (prop->children != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 6563 | if ((prop->children->next == NULL) && |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6564 | ((prop->children->type == XML_TEXT_NODE) || |
| 6565 | (prop->children->type == XML_CDATA_SECTION_NODE))) |
| 6566 | { |
| 6567 | /* |
| 6568 | * Optimization for the common case: only 1 text node. |
| 6569 | */ |
| 6570 | return(xmlStrdup(prop->children->content)); |
| 6571 | } else { |
| 6572 | xmlChar *ret; |
| 6573 | |
| 6574 | ret = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 6575 | if (ret != NULL) |
| 6576 | return(ret); |
| 6577 | } |
| 6578 | } |
| 6579 | return(xmlStrdup((xmlChar *)"")); |
| 6580 | } else if (prop->type == XML_ATTRIBUTE_DECL) { |
| 6581 | return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue)); |
| 6582 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6583 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6586 | /** |
| 6587 | * xmlHasProp: |
| 6588 | * @node: the node |
| 6589 | * @name: the attribute name |
| 6590 | * |
| 6591 | * Search an attribute associated to a node |
| 6592 | * This function also looks in DTD attribute declaration for #FIXED or |
| 6593 | * default declaration values unless DTD use has been turned off. |
| 6594 | * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6595 | * Returns the attribute or the attribute declaration or NULL if |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6596 | * neither was found. |
| 6597 | */ |
| 6598 | xmlAttrPtr |
| 6599 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 6600 | xmlAttrPtr prop; |
| 6601 | xmlDocPtr doc; |
| 6602 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6603 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6604 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6605 | /* |
| 6606 | * Check on the properties attached to the node |
| 6607 | */ |
| 6608 | prop = node->properties; |
| 6609 | while (prop != NULL) { |
| 6610 | if (xmlStrEqual(prop->name, name)) { |
| 6611 | return(prop); |
| 6612 | } |
| 6613 | prop = prop->next; |
| 6614 | } |
| 6615 | if (!xmlCheckDTD) return(NULL); |
| 6616 | |
| 6617 | /* |
| 6618 | * Check if there is a default declaration in the internal |
| 6619 | * or external subsets |
| 6620 | */ |
| 6621 | doc = node->doc; |
| 6622 | if (doc != NULL) { |
| 6623 | xmlAttributePtr attrDecl; |
| 6624 | if (doc->intSubset != NULL) { |
| 6625 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6626 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6627 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6628 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6629 | /* return attribute declaration only if a default value is given |
| 6630 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6631 | return((xmlAttrPtr) attrDecl); |
| 6632 | } |
| 6633 | } |
| 6634 | return(NULL); |
| 6635 | } |
| 6636 | |
| 6637 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6638 | * xmlHasNsProp: |
| 6639 | * @node: the node |
| 6640 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6641 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6642 | * |
| 6643 | * Search for an attribute associated to a node |
| 6644 | * This attribute has to be anchored in the namespace specified. |
| 6645 | * This does the entity substitution. |
| 6646 | * This function looks in DTD attribute declaration for #FIXED or |
| 6647 | * default declaration values unless DTD use has been turned off. |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6648 | * Note that a namespace of NULL indicates to use the default namespace. |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6649 | * |
| 6650 | * Returns the attribute or the attribute declaration or NULL |
| 6651 | * if neither was found. |
| 6652 | */ |
| 6653 | xmlAttrPtr |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6654 | xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6655 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6656 | return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD)); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6657 | } |
| 6658 | |
| 6659 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6660 | * xmlGetProp: |
| 6661 | * @node: the node |
| 6662 | * @name: the attribute name |
| 6663 | * |
| 6664 | * Search and get the value of an attribute associated to a node |
| 6665 | * This does the entity substitution. |
| 6666 | * This function looks in DTD attribute declaration for #FIXED or |
| 6667 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 6668 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6669 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 6670 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6671 | * |
| 6672 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6673 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6674 | */ |
| 6675 | xmlChar * |
| 6676 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6677 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6678 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6679 | prop = xmlHasProp(node, name); |
| 6680 | if (prop == NULL) |
| 6681 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6682 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6683 | } |
| 6684 | |
| 6685 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6686 | * xmlGetNoNsProp: |
| 6687 | * @node: the node |
| 6688 | * @name: the attribute name |
| 6689 | * |
| 6690 | * Search and get the value of an attribute associated to a node |
| 6691 | * This does the entity substitution. |
| 6692 | * This function looks in DTD attribute declaration for #FIXED or |
| 6693 | * default declaration values unless DTD use has been turned off. |
| 6694 | * This function is similar to xmlGetProp except it will accept only |
| 6695 | * an attribute in no namespace. |
| 6696 | * |
| 6697 | * Returns the attribute value or NULL if not found. |
| 6698 | * It's up to the caller to free the memory with xmlFree(). |
| 6699 | */ |
| 6700 | xmlChar * |
| 6701 | xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) { |
| 6702 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6703 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6704 | prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD); |
| 6705 | if (prop == NULL) |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6706 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6707 | return(xmlGetPropNodeValueInternal(prop)); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6708 | } |
| 6709 | |
| 6710 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6711 | * xmlGetNsProp: |
| 6712 | * @node: the node |
| 6713 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6714 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6715 | * |
| 6716 | * Search and get the value of an attribute associated to a node |
| 6717 | * This attribute has to be anchored in the namespace specified. |
| 6718 | * This does the entity substitution. |
| 6719 | * This function looks in DTD attribute declaration for #FIXED or |
| 6720 | * default declaration values unless DTD use has been turned off. |
| 6721 | * |
| 6722 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6723 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6724 | */ |
| 6725 | xmlChar * |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6726 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6727 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6728 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6729 | prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD); |
| 6730 | if (prop == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6731 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6732 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6733 | } |
| 6734 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6735 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
| 6736 | /** |
| 6737 | * xmlUnsetProp: |
| 6738 | * @node: the node |
| 6739 | * @name: the attribute name |
| 6740 | * |
| 6741 | * Remove an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6742 | * This handles only attributes in no namespace. |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6743 | * Returns 0 if successful, -1 if not found |
| 6744 | */ |
| 6745 | int |
| 6746 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6747 | xmlAttrPtr prop; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6748 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6749 | prop = xmlGetPropNodeInternal(node, name, NULL, 0); |
| 6750 | if (prop == NULL) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6751 | return(-1); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6752 | xmlUnlinkNode((xmlNodePtr) prop); |
| 6753 | xmlFreeProp(prop); |
| 6754 | return(0); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6755 | } |
| 6756 | |
| 6757 | /** |
| 6758 | * xmlUnsetNsProp: |
| 6759 | * @node: the node |
| 6760 | * @ns: the namespace definition |
| 6761 | * @name: the attribute name |
| 6762 | * |
| 6763 | * Remove an attribute carried by a node. |
| 6764 | * Returns 0 if successful, -1 if not found |
| 6765 | */ |
| 6766 | int |
| 6767 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6768 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6769 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6770 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6771 | if (prop == NULL) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6772 | return(-1); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6773 | xmlUnlinkNode((xmlNodePtr) prop); |
| 6774 | xmlFreeProp(prop); |
| 6775 | return(0); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6776 | } |
| 6777 | #endif |
| 6778 | |
| 6779 | #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] | 6780 | /** |
| 6781 | * xmlSetProp: |
| 6782 | * @node: the node |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6783 | * @name: the attribute name (a QName) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6784 | * @value: the attribute value |
| 6785 | * |
| 6786 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6787 | * If @name has a prefix, then the corresponding |
| 6788 | * namespace-binding will be used, if in scope; it is an |
| 6789 | * error it there's no such ns-binding for the prefix in |
| 6790 | * scope. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6791 | * Returns the attribute pointer. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6792 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6793 | */ |
| 6794 | xmlAttrPtr |
| 6795 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6796 | int len; |
| 6797 | const xmlChar *nqname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6798 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6799 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6800 | return(NULL); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6801 | |
| 6802 | /* |
| 6803 | * handle QNames |
| 6804 | */ |
| 6805 | nqname = xmlSplitQName3(name, &len); |
| 6806 | if (nqname != NULL) { |
| 6807 | xmlNsPtr ns; |
| 6808 | xmlChar *prefix = xmlStrndup(name, len); |
| 6809 | ns = xmlSearchNs(node->doc, node, prefix); |
| 6810 | if (prefix != NULL) |
| 6811 | xmlFree(prefix); |
| 6812 | if (ns != NULL) |
| 6813 | return(xmlSetNsProp(node, ns, nqname, value)); |
| 6814 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6815 | return(xmlSetNsProp(node, NULL, name, value)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6816 | } |
| 6817 | |
| 6818 | /** |
| 6819 | * xmlSetNsProp: |
| 6820 | * @node: the node |
| 6821 | * @ns: the namespace definition |
| 6822 | * @name: the attribute name |
| 6823 | * @value: the attribute value |
| 6824 | * |
| 6825 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6826 | * The ns structure must be in scope, this is not checked |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6827 | * |
| 6828 | * Returns the attribute pointer. |
| 6829 | */ |
| 6830 | xmlAttrPtr |
| 6831 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6832 | const xmlChar *value) |
| 6833 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6834 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6835 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6836 | if (ns && (ns->href == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6837 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6838 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6839 | if (prop != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6840 | /* |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6841 | * Modify the attribute's value. |
| 6842 | */ |
| 6843 | if (prop->atype == XML_ATTRIBUTE_ID) { |
| 6844 | xmlRemoveID(node->doc, prop); |
| 6845 | prop->atype = XML_ATTRIBUTE_ID; |
| 6846 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6847 | if (prop->children != NULL) |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6848 | xmlFreeNodeList(prop->children); |
| 6849 | prop->children = NULL; |
| 6850 | prop->last = NULL; |
| 6851 | prop->ns = ns; |
| 6852 | if (value != NULL) { |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6853 | xmlNodePtr tmp; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6854 | |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame] | 6855 | if(!xmlCheckUTF8(value)) { |
| 6856 | xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc, |
| 6857 | NULL); |
| 6858 | if (node->doc != NULL) |
| 6859 | node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 6860 | } |
| 6861 | prop->children = xmlNewDocText(node->doc, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6862 | prop->last = NULL; |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6863 | tmp = prop->children; |
| 6864 | while (tmp != NULL) { |
| 6865 | tmp->parent = (xmlNodePtr) prop; |
| 6866 | if (tmp->next == NULL) |
| 6867 | prop->last = tmp; |
| 6868 | tmp = tmp->next; |
| 6869 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6870 | } |
| 6871 | if (prop->atype == XML_ATTRIBUTE_ID) |
| 6872 | xmlAddID(NULL, node->doc, value, prop); |
| 6873 | return(prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6874 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6875 | /* |
| 6876 | * No equal attr found; create a new one. |
| 6877 | */ |
| 6878 | return(xmlNewPropInternal(node, ns, name, value, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6879 | } |
| 6880 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6881 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6882 | |
| 6883 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6884 | * xmlNodeIsText: |
| 6885 | * @node: the node |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6886 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6887 | * Is this node a Text node ? |
| 6888 | * Returns 1 yes, 0 no |
| 6889 | */ |
| 6890 | int |
| 6891 | xmlNodeIsText(xmlNodePtr node) { |
| 6892 | if (node == NULL) return(0); |
| 6893 | |
| 6894 | if (node->type == XML_TEXT_NODE) return(1); |
| 6895 | return(0); |
| 6896 | } |
| 6897 | |
| 6898 | /** |
| 6899 | * xmlIsBlankNode: |
| 6900 | * @node: the node |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6901 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6902 | * Checks whether this node is an empty or whitespace only |
| 6903 | * (and possibly ignorable) text-node. |
| 6904 | * |
| 6905 | * Returns 1 yes, 0 no |
| 6906 | */ |
| 6907 | int |
| 6908 | xmlIsBlankNode(xmlNodePtr node) { |
| 6909 | const xmlChar *cur; |
| 6910 | if (node == NULL) return(0); |
| 6911 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6912 | if ((node->type != XML_TEXT_NODE) && |
| 6913 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6914 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6915 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6916 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6917 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6918 | if (!IS_BLANK_CH(*cur)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6919 | cur++; |
| 6920 | } |
| 6921 | |
| 6922 | return(1); |
| 6923 | } |
| 6924 | |
| 6925 | /** |
| 6926 | * xmlTextConcat: |
| 6927 | * @node: the node |
| 6928 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6929 | * @len: @content length |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6930 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6931 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6932 | * |
| 6933 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6934 | */ |
| 6935 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6936 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6937 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6938 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6939 | |
| 6940 | if ((node->type != XML_TEXT_NODE) && |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 6941 | (node->type != XML_CDATA_SECTION_NODE) && |
| 6942 | (node->type != XML_COMMENT_NODE) && |
| 6943 | (node->type != XML_PI_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6944 | #ifdef DEBUG_TREE |
| 6945 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6946 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6947 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6948 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6949 | } |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6950 | /* need to check if content is currently in the dictionary */ |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6951 | if ((node->content == (xmlChar *) &(node->properties)) || |
| 6952 | ((node->doc != NULL) && (node->doc->dict != NULL) && |
| 6953 | xmlDictOwns(node->doc->dict, node->content))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6954 | node->content = xmlStrncatNew(node->content, content, len); |
| 6955 | } else { |
| 6956 | node->content = xmlStrncat(node->content, content, len); |
| 6957 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6958 | node->properties = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6959 | if (node->content == NULL) |
| 6960 | return(-1); |
| 6961 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6962 | } |
| 6963 | |
| 6964 | /************************************************************************ |
| 6965 | * * |
| 6966 | * Output : to a FILE or in memory * |
| 6967 | * * |
| 6968 | ************************************************************************/ |
| 6969 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6970 | /** |
| 6971 | * xmlBufferCreate: |
| 6972 | * |
| 6973 | * routine to create an XML buffer. |
| 6974 | * returns the new structure. |
| 6975 | */ |
| 6976 | xmlBufferPtr |
| 6977 | xmlBufferCreate(void) { |
| 6978 | xmlBufferPtr ret; |
| 6979 | |
| 6980 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 6981 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6982 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6983 | return(NULL); |
| 6984 | } |
| 6985 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 6986 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6987 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 6988 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6989 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6990 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6991 | xmlFree(ret); |
| 6992 | return(NULL); |
| 6993 | } |
| 6994 | ret->content[0] = 0; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 6995 | ret->contentIO = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6996 | return(ret); |
| 6997 | } |
| 6998 | |
| 6999 | /** |
| 7000 | * xmlBufferCreateSize: |
| 7001 | * @size: initial size of buffer |
| 7002 | * |
| 7003 | * routine to create an XML buffer. |
| 7004 | * returns the new structure. |
| 7005 | */ |
| 7006 | xmlBufferPtr |
| 7007 | xmlBufferCreateSize(size_t size) { |
| 7008 | xmlBufferPtr ret; |
| 7009 | |
| 7010 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 7011 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7012 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7013 | return(NULL); |
| 7014 | } |
| 7015 | ret->use = 0; |
| 7016 | ret->alloc = xmlBufferAllocScheme; |
| 7017 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 7018 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 7019 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7020 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7021 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7022 | xmlFree(ret); |
| 7023 | return(NULL); |
| 7024 | } |
| 7025 | ret->content[0] = 0; |
| 7026 | } else |
| 7027 | ret->content = NULL; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7028 | ret->contentIO = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7029 | return(ret); |
| 7030 | } |
| 7031 | |
| 7032 | /** |
Daniel Veillard | 79ee284 | 2012-05-15 10:25:31 +0800 | [diff] [blame] | 7033 | * xmlBufferDetach: |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 7034 | * @buf: the buffer |
| 7035 | * |
Daniel Veillard | 79ee284 | 2012-05-15 10:25:31 +0800 | [diff] [blame] | 7036 | * Remove the string contained in a buffer and gie it back to the |
Daniel Veillard | 94431ec | 2012-05-15 10:45:05 +0800 | [diff] [blame] | 7037 | * caller. The buffer is reset to an empty content. |
| 7038 | * This doesn't work with immutable buffers as they can't be reset. |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 7039 | * |
Daniel Veillard | 79ee284 | 2012-05-15 10:25:31 +0800 | [diff] [blame] | 7040 | * Returns the previous string contained by the buffer. |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 7041 | */ |
| 7042 | xmlChar * |
| 7043 | xmlBufferDetach(xmlBufferPtr buf) { |
| 7044 | xmlChar *ret; |
| 7045 | |
Daniel Veillard | 94431ec | 2012-05-15 10:45:05 +0800 | [diff] [blame] | 7046 | if (buf == NULL) |
| 7047 | return(NULL); |
| 7048 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) |
| 7049 | return(NULL); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 7050 | |
| 7051 | ret = buf->content; |
| 7052 | buf->content = NULL; |
| 7053 | buf->size = 0; |
| 7054 | buf->use = 0; |
| 7055 | |
| 7056 | return ret; |
| 7057 | } |
| 7058 | |
| 7059 | |
| 7060 | /** |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7061 | * xmlBufferCreateStatic: |
| 7062 | * @mem: the memory area |
| 7063 | * @size: the size in byte |
| 7064 | * |
MST 2003 John Fleck | a0e7e93 | 2003-12-19 03:13:47 +0000 | [diff] [blame] | 7065 | * routine to create an XML buffer from an immutable memory area. |
| 7066 | * 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] | 7067 | * present until the end of the buffer lifetime. |
| 7068 | * |
| 7069 | * returns the new structure. |
| 7070 | */ |
| 7071 | xmlBufferPtr |
| 7072 | xmlBufferCreateStatic(void *mem, size_t size) { |
| 7073 | xmlBufferPtr ret; |
| 7074 | |
| 7075 | if ((mem == NULL) || (size == 0)) |
| 7076 | return(NULL); |
| 7077 | |
| 7078 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 7079 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7080 | xmlTreeErrMemory("creating buffer"); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7081 | return(NULL); |
| 7082 | } |
| 7083 | ret->use = size; |
| 7084 | ret->size = size; |
| 7085 | ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; |
| 7086 | ret->content = (xmlChar *) mem; |
| 7087 | return(ret); |
| 7088 | } |
| 7089 | |
| 7090 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7091 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 7092 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7093 | * @scheme: allocation scheme to use |
| 7094 | * |
| 7095 | * Sets the allocation scheme for this buffer |
| 7096 | */ |
| 7097 | void |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7098 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7099 | xmlBufferAllocationScheme scheme) { |
| 7100 | if (buf == NULL) { |
| 7101 | #ifdef DEBUG_BUFFER |
| 7102 | xmlGenericError(xmlGenericErrorContext, |
| 7103 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 7104 | #endif |
| 7105 | return; |
| 7106 | } |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 7107 | if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || |
| 7108 | (buf->alloc == XML_BUFFER_ALLOC_IO)) return; |
| 7109 | if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) || |
| 7110 | (scheme == XML_BUFFER_ALLOC_EXACT) || |
Conrad Irwin | 7d0d2a5 | 2012-05-14 14:18:58 +0800 | [diff] [blame] | 7111 | (scheme == XML_BUFFER_ALLOC_HYBRID) || |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 7112 | (scheme == XML_BUFFER_ALLOC_IMMUTABLE)) |
| 7113 | buf->alloc = scheme; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7114 | } |
| 7115 | |
| 7116 | /** |
| 7117 | * xmlBufferFree: |
| 7118 | * @buf: the buffer to free |
| 7119 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 7120 | * Frees an XML buffer. It frees both the content and the structure which |
| 7121 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7122 | */ |
| 7123 | void |
| 7124 | xmlBufferFree(xmlBufferPtr buf) { |
| 7125 | if (buf == NULL) { |
| 7126 | #ifdef DEBUG_BUFFER |
| 7127 | xmlGenericError(xmlGenericErrorContext, |
| 7128 | "xmlBufferFree: buf == NULL\n"); |
| 7129 | #endif |
| 7130 | return; |
| 7131 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7132 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7133 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && |
| 7134 | (buf->contentIO != NULL)) { |
| 7135 | xmlFree(buf->contentIO); |
| 7136 | } else if ((buf->content != NULL) && |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7137 | (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7138 | xmlFree(buf->content); |
| 7139 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7140 | xmlFree(buf); |
| 7141 | } |
| 7142 | |
| 7143 | /** |
| 7144 | * xmlBufferEmpty: |
| 7145 | * @buf: the buffer |
| 7146 | * |
| 7147 | * empty a buffer. |
| 7148 | */ |
| 7149 | void |
| 7150 | xmlBufferEmpty(xmlBufferPtr buf) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7151 | if (buf == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7152 | if (buf->content == NULL) return; |
| 7153 | buf->use = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7154 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 7155 | buf->content = BAD_CAST ""; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7156 | } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) && |
| 7157 | (buf->contentIO != NULL)) { |
| 7158 | size_t start_buf = buf->content - buf->contentIO; |
| 7159 | |
| 7160 | buf->size += start_buf; |
| 7161 | buf->content = buf->contentIO; |
| 7162 | buf->content[0] = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7163 | } else { |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7164 | buf->content[0] = 0; |
| 7165 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7166 | } |
| 7167 | |
| 7168 | /** |
| 7169 | * xmlBufferShrink: |
| 7170 | * @buf: the buffer to dump |
| 7171 | * @len: the number of xmlChar to remove |
| 7172 | * |
| 7173 | * Remove the beginning of an XML buffer. |
| 7174 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7175 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7176 | */ |
| 7177 | int |
| 7178 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 7179 | if (buf == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7180 | if (len == 0) return(0); |
| 7181 | if (len > buf->use) return(-1); |
| 7182 | |
| 7183 | buf->use -= len; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7184 | if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || |
| 7185 | ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) { |
| 7186 | /* |
| 7187 | * we just move the content pointer, but also make sure |
| 7188 | * the perceived buffer size has shrinked accordingly |
| 7189 | */ |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7190 | buf->content += len; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7191 | buf->size -= len; |
| 7192 | |
| 7193 | /* |
| 7194 | * sometimes though it maybe be better to really shrink |
| 7195 | * on IO buffers |
| 7196 | */ |
| 7197 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7198 | size_t start_buf = buf->content - buf->contentIO; |
| 7199 | if (start_buf >= buf->size) { |
| 7200 | memmove(buf->contentIO, &buf->content[0], buf->use); |
| 7201 | buf->content = buf->contentIO; |
| 7202 | buf->content[buf->use] = 0; |
| 7203 | buf->size += start_buf; |
| 7204 | } |
| 7205 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7206 | } else { |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7207 | memmove(buf->content, &buf->content[len], buf->use); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7208 | buf->content[buf->use] = 0; |
| 7209 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7210 | return(len); |
| 7211 | } |
| 7212 | |
| 7213 | /** |
| 7214 | * xmlBufferGrow: |
| 7215 | * @buf: the buffer |
| 7216 | * @len: the minimum free size to allocate |
| 7217 | * |
| 7218 | * Grow the available space of an XML buffer. |
| 7219 | * |
| 7220 | * Returns the new available space or -1 in case of error |
| 7221 | */ |
| 7222 | int |
| 7223 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 7224 | int size; |
| 7225 | xmlChar *newbuf; |
| 7226 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 7227 | if (buf == NULL) return(-1); |
| 7228 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7229 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7230 | if (len + buf->use < buf->size) return(0); |
| 7231 | |
Daniel Veillard | ee20cd7 | 2009-08-22 15:18:31 +0200 | [diff] [blame] | 7232 | /* |
| 7233 | * Windows has a BIG problem on realloc timing, so we try to double |
| 7234 | * the buffer size (if that's enough) (bug 146697) |
| 7235 | * Apparently BSD too, and it's probably best for linux too |
| 7236 | * On an embedded system this may be something to change |
| 7237 | */ |
| 7238 | #if 1 |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 7239 | if (buf->size > len) |
| 7240 | size = buf->size * 2; |
| 7241 | else |
| 7242 | size = buf->use + len + 100; |
| 7243 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7244 | size = buf->use + len + 100; |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 7245 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7246 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7247 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7248 | size_t start_buf = buf->content - buf->contentIO; |
| 7249 | |
| 7250 | newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size); |
| 7251 | if (newbuf == NULL) { |
| 7252 | xmlTreeErrMemory("growing buffer"); |
| 7253 | return(-1); |
| 7254 | } |
| 7255 | buf->contentIO = newbuf; |
| 7256 | buf->content = newbuf + start_buf; |
| 7257 | } else { |
| 7258 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
| 7259 | if (newbuf == NULL) { |
| 7260 | xmlTreeErrMemory("growing buffer"); |
| 7261 | return(-1); |
| 7262 | } |
| 7263 | buf->content = newbuf; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7264 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7265 | buf->size = size; |
| 7266 | return(buf->size - buf->use); |
| 7267 | } |
| 7268 | |
| 7269 | /** |
| 7270 | * xmlBufferDump: |
| 7271 | * @file: the file output |
| 7272 | * @buf: the buffer to dump |
| 7273 | * |
| 7274 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7275 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7276 | */ |
| 7277 | int |
| 7278 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 7279 | int ret; |
| 7280 | |
| 7281 | if (buf == NULL) { |
| 7282 | #ifdef DEBUG_BUFFER |
| 7283 | xmlGenericError(xmlGenericErrorContext, |
| 7284 | "xmlBufferDump: buf == NULL\n"); |
| 7285 | #endif |
| 7286 | return(0); |
| 7287 | } |
| 7288 | if (buf->content == NULL) { |
| 7289 | #ifdef DEBUG_BUFFER |
| 7290 | xmlGenericError(xmlGenericErrorContext, |
| 7291 | "xmlBufferDump: buf->content == NULL\n"); |
| 7292 | #endif |
| 7293 | return(0); |
| 7294 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 7295 | if (file == NULL) |
| 7296 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7297 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 7298 | return(ret); |
| 7299 | } |
| 7300 | |
| 7301 | /** |
| 7302 | * xmlBufferContent: |
| 7303 | * @buf: the buffer |
| 7304 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7305 | * Function to extract the content of a buffer |
| 7306 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7307 | * Returns the internal content |
| 7308 | */ |
| 7309 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7310 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7311 | xmlBufferContent(const xmlBufferPtr buf) |
| 7312 | { |
| 7313 | if(!buf) |
| 7314 | return NULL; |
| 7315 | |
| 7316 | return buf->content; |
| 7317 | } |
| 7318 | |
| 7319 | /** |
| 7320 | * xmlBufferLength: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7321 | * @buf: the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7322 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7323 | * Function to get the length of a buffer |
| 7324 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7325 | * Returns the length of data in the internal content |
| 7326 | */ |
| 7327 | |
| 7328 | int |
| 7329 | xmlBufferLength(const xmlBufferPtr buf) |
| 7330 | { |
| 7331 | if(!buf) |
| 7332 | return 0; |
| 7333 | |
| 7334 | return buf->use; |
| 7335 | } |
| 7336 | |
| 7337 | /** |
| 7338 | * xmlBufferResize: |
| 7339 | * @buf: the buffer to resize |
| 7340 | * @size: the desired size |
| 7341 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7342 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7343 | * |
| 7344 | * Returns 0 in case of problems, 1 otherwise |
| 7345 | */ |
| 7346 | int |
| 7347 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 7348 | { |
| 7349 | unsigned int newSize; |
| 7350 | xmlChar* rebuf = NULL; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7351 | size_t start_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7352 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7353 | if (buf == NULL) |
| 7354 | return(0); |
| 7355 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7356 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
| 7357 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7358 | /* Don't resize if we don't have to */ |
| 7359 | if (size < buf->size) |
| 7360 | return 1; |
| 7361 | |
| 7362 | /* figure out new size */ |
| 7363 | switch (buf->alloc){ |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7364 | case XML_BUFFER_ALLOC_IO: |
| 7365 | case XML_BUFFER_ALLOC_DOUBLEIT: |
| 7366 | /*take care of empty case*/ |
| 7367 | newSize = (buf->size ? buf->size*2 : size + 10); |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 7368 | while (size > newSize) { |
| 7369 | if (newSize > UINT_MAX / 2) { |
| 7370 | xmlTreeErrMemory("growing buffer"); |
| 7371 | return 0; |
| 7372 | } |
| 7373 | newSize *= 2; |
| 7374 | } |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7375 | break; |
| 7376 | case XML_BUFFER_ALLOC_EXACT: |
| 7377 | newSize = size+10; |
| 7378 | break; |
Conrad Irwin | 7d0d2a5 | 2012-05-14 14:18:58 +0800 | [diff] [blame] | 7379 | case XML_BUFFER_ALLOC_HYBRID: |
| 7380 | if (buf->use < BASE_BUFFER_SIZE) |
| 7381 | newSize = size; |
| 7382 | else { |
| 7383 | newSize = buf->size * 2; |
| 7384 | while (size > newSize) { |
| 7385 | if (newSize > UINT_MAX / 2) { |
| 7386 | xmlTreeErrMemory("growing buffer"); |
| 7387 | return 0; |
| 7388 | } |
| 7389 | newSize *= 2; |
| 7390 | } |
| 7391 | } |
| 7392 | break; |
| 7393 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7394 | default: |
| 7395 | newSize = size+10; |
| 7396 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7397 | } |
| 7398 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7399 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7400 | start_buf = buf->content - buf->contentIO; |
| 7401 | |
| 7402 | if (start_buf > newSize) { |
| 7403 | /* move data back to start */ |
| 7404 | memmove(buf->contentIO, buf->content, buf->use); |
| 7405 | buf->content = buf->contentIO; |
| 7406 | buf->content[buf->use] = 0; |
| 7407 | buf->size += start_buf; |
| 7408 | } else { |
| 7409 | rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize); |
| 7410 | if (rebuf == NULL) { |
| 7411 | xmlTreeErrMemory("growing buffer"); |
| 7412 | return 0; |
| 7413 | } |
| 7414 | buf->contentIO = rebuf; |
| 7415 | buf->content = rebuf + start_buf; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7416 | } |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7417 | } else { |
| 7418 | if (buf->content == NULL) { |
| 7419 | rebuf = (xmlChar *) xmlMallocAtomic(newSize); |
| 7420 | } else if (buf->size - buf->use < 100) { |
| 7421 | rebuf = (xmlChar *) xmlRealloc(buf->content, newSize); |
| 7422 | } else { |
| 7423 | /* |
| 7424 | * if we are reallocating a buffer far from being full, it's |
| 7425 | * better to make a new allocation and copy only the used range |
| 7426 | * and free the old one. |
| 7427 | */ |
| 7428 | rebuf = (xmlChar *) xmlMallocAtomic(newSize); |
| 7429 | if (rebuf != NULL) { |
| 7430 | memcpy(rebuf, buf->content, buf->use); |
| 7431 | xmlFree(buf->content); |
| 7432 | rebuf[buf->use] = 0; |
| 7433 | } |
| 7434 | } |
| 7435 | if (rebuf == NULL) { |
| 7436 | xmlTreeErrMemory("growing buffer"); |
| 7437 | return 0; |
| 7438 | } |
| 7439 | buf->content = rebuf; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7440 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7441 | buf->size = newSize; |
| 7442 | |
| 7443 | return 1; |
| 7444 | } |
| 7445 | |
| 7446 | /** |
| 7447 | * xmlBufferAdd: |
| 7448 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7449 | * @str: the #xmlChar string |
| 7450 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7451 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7452 | * 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] | 7453 | * str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7454 | * |
| 7455 | * Returns 0 successful, a positive error code number otherwise |
| 7456 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7457 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7458 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7459 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 7460 | unsigned int needSize; |
| 7461 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7462 | if ((str == NULL) || (buf == NULL)) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7463 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7464 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7465 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7466 | if (len < -1) { |
| 7467 | #ifdef DEBUG_BUFFER |
| 7468 | xmlGenericError(xmlGenericErrorContext, |
| 7469 | "xmlBufferAdd: len < 0\n"); |
| 7470 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7471 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7472 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7473 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7474 | |
| 7475 | if (len < 0) |
| 7476 | len = xmlStrlen(str); |
| 7477 | |
Daniel Veillard | c992332 | 2007-04-24 18:12:06 +0000 | [diff] [blame] | 7478 | if (len < 0) return -1; |
| 7479 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7480 | |
| 7481 | needSize = buf->use + len + 2; |
| 7482 | if (needSize > buf->size){ |
| 7483 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7484 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7485 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7486 | } |
| 7487 | } |
| 7488 | |
| 7489 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 7490 | buf->use += len; |
| 7491 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7492 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7493 | } |
| 7494 | |
| 7495 | /** |
| 7496 | * xmlBufferAddHead: |
| 7497 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7498 | * @str: the #xmlChar string |
| 7499 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7500 | * |
| 7501 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7502 | * if len == -1, the length of @str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7503 | * |
| 7504 | * Returns 0 successful, a positive error code number otherwise |
| 7505 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7506 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7507 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7508 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 7509 | unsigned int needSize; |
| 7510 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7511 | if (buf == NULL) |
| 7512 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7513 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7514 | if (str == NULL) { |
| 7515 | #ifdef DEBUG_BUFFER |
| 7516 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7517 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7518 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7519 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7520 | } |
| 7521 | if (len < -1) { |
| 7522 | #ifdef DEBUG_BUFFER |
| 7523 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7524 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7525 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7526 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7527 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7528 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7529 | |
| 7530 | if (len < 0) |
| 7531 | len = xmlStrlen(str); |
| 7532 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7533 | if (len <= 0) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7534 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7535 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7536 | size_t start_buf = buf->content - buf->contentIO; |
| 7537 | |
| 7538 | if (start_buf > (unsigned int) len) { |
| 7539 | /* |
| 7540 | * We can add it in the space previously shrinked |
| 7541 | */ |
| 7542 | buf->content -= len; |
| 7543 | memmove(&buf->content[0], str, len); |
| 7544 | buf->use += len; |
| 7545 | buf->size += len; |
| 7546 | return(0); |
| 7547 | } |
| 7548 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7549 | needSize = buf->use + len + 2; |
| 7550 | if (needSize > buf->size){ |
| 7551 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7552 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7553 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7554 | } |
| 7555 | } |
| 7556 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7557 | memmove(&buf->content[len], &buf->content[0], buf->use); |
| 7558 | memmove(&buf->content[0], str, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7559 | buf->use += len; |
| 7560 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7561 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7562 | } |
| 7563 | |
| 7564 | /** |
| 7565 | * xmlBufferCat: |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7566 | * @buf: the buffer to add to |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7567 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7568 | * |
| 7569 | * Append a zero terminated string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7570 | * |
| 7571 | * Returns 0 successful, a positive error code number otherwise |
| 7572 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7573 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7574 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7575 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7576 | if (buf == NULL) |
| 7577 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7578 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
| 7579 | if (str == NULL) return -1; |
| 7580 | return xmlBufferAdd(buf, str, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7581 | } |
| 7582 | |
| 7583 | /** |
| 7584 | * xmlBufferCCat: |
| 7585 | * @buf: the buffer to dump |
| 7586 | * @str: the C char string |
| 7587 | * |
| 7588 | * Append a zero terminated C string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7589 | * |
| 7590 | * Returns 0 successful, a positive error code number otherwise |
| 7591 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7592 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7593 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7594 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 7595 | const char *cur; |
| 7596 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7597 | if (buf == NULL) |
| 7598 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7599 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7600 | if (str == NULL) { |
| 7601 | #ifdef DEBUG_BUFFER |
| 7602 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7603 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7604 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7605 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7606 | } |
| 7607 | for (cur = str;*cur != 0;cur++) { |
| 7608 | if (buf->use + 10 >= buf->size) { |
| 7609 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7610 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7611 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7612 | } |
| 7613 | } |
| 7614 | buf->content[buf->use++] = *cur; |
| 7615 | } |
| 7616 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7617 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7618 | } |
| 7619 | |
| 7620 | /** |
| 7621 | * xmlBufferWriteCHAR: |
| 7622 | * @buf: the XML buffer |
| 7623 | * @string: the string to add |
| 7624 | * |
| 7625 | * routine which manages and grows an output buffer. This one adds |
| 7626 | * xmlChars at the end of the buffer. |
| 7627 | */ |
| 7628 | void |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7629 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7630 | if (buf == NULL) |
| 7631 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7632 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7633 | xmlBufferCat(buf, string); |
| 7634 | } |
| 7635 | |
| 7636 | /** |
| 7637 | * xmlBufferWriteChar: |
| 7638 | * @buf: the XML buffer output |
| 7639 | * @string: the string to add |
| 7640 | * |
| 7641 | * routine which manage and grows an output buffer. This one add |
| 7642 | * C chars at the end of the array. |
| 7643 | */ |
| 7644 | void |
| 7645 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7646 | if (buf == NULL) |
| 7647 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7648 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7649 | xmlBufferCCat(buf, string); |
| 7650 | } |
| 7651 | |
| 7652 | |
| 7653 | /** |
| 7654 | * xmlBufferWriteQuotedString: |
| 7655 | * @buf: the XML buffer output |
| 7656 | * @string: the string to add |
| 7657 | * |
| 7658 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7659 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7660 | * quote or double-quotes internally |
| 7661 | */ |
| 7662 | void |
| 7663 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7664 | const xmlChar *cur, *base; |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7665 | if (buf == NULL) |
| 7666 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7667 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7668 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 7669 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7670 | #ifdef DEBUG_BUFFER |
| 7671 | xmlGenericError(xmlGenericErrorContext, |
| 7672 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 7673 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7674 | xmlBufferCCat(buf, "\""); |
| 7675 | base = cur = string; |
| 7676 | while(*cur != 0){ |
| 7677 | if(*cur == '"'){ |
| 7678 | if (base != cur) |
| 7679 | xmlBufferAdd(buf, base, cur - base); |
| 7680 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 7681 | cur++; |
| 7682 | base = cur; |
| 7683 | } |
| 7684 | else { |
| 7685 | cur++; |
| 7686 | } |
| 7687 | } |
| 7688 | if (base != cur) |
| 7689 | xmlBufferAdd(buf, base, cur - base); |
| 7690 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7691 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7692 | else{ |
| 7693 | xmlBufferCCat(buf, "\'"); |
| 7694 | xmlBufferCat(buf, string); |
| 7695 | xmlBufferCCat(buf, "\'"); |
| 7696 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7697 | } else { |
| 7698 | xmlBufferCCat(buf, "\""); |
| 7699 | xmlBufferCat(buf, string); |
| 7700 | xmlBufferCCat(buf, "\""); |
| 7701 | } |
| 7702 | } |
| 7703 | |
| 7704 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7705 | /** |
| 7706 | * xmlGetDocCompressMode: |
| 7707 | * @doc: the document |
| 7708 | * |
| 7709 | * get the compression ratio for a document, ZLIB based |
| 7710 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7711 | */ |
| 7712 | int |
| 7713 | xmlGetDocCompressMode (xmlDocPtr doc) { |
| 7714 | if (doc == NULL) return(-1); |
| 7715 | return(doc->compression); |
| 7716 | } |
| 7717 | |
| 7718 | /** |
| 7719 | * xmlSetDocCompressMode: |
| 7720 | * @doc: the document |
| 7721 | * @mode: the compression ratio |
| 7722 | * |
| 7723 | * set the compression ratio for a document, ZLIB based |
| 7724 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7725 | */ |
| 7726 | void |
| 7727 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 7728 | if (doc == NULL) return; |
| 7729 | if (mode < 0) doc->compression = 0; |
| 7730 | else if (mode > 9) doc->compression = 9; |
| 7731 | else doc->compression = mode; |
| 7732 | } |
| 7733 | |
| 7734 | /** |
| 7735 | * xmlGetCompressMode: |
| 7736 | * |
| 7737 | * get the default compression mode used, ZLIB based. |
| 7738 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7739 | */ |
| 7740 | int |
| 7741 | xmlGetCompressMode(void) |
| 7742 | { |
| 7743 | return (xmlCompressMode); |
| 7744 | } |
| 7745 | |
| 7746 | /** |
| 7747 | * xmlSetCompressMode: |
| 7748 | * @mode: the compression ratio |
| 7749 | * |
| 7750 | * set the default compression mode used, ZLIB based |
| 7751 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7752 | */ |
| 7753 | void |
| 7754 | xmlSetCompressMode(int mode) { |
| 7755 | if (mode < 0) xmlCompressMode = 0; |
| 7756 | else if (mode > 9) xmlCompressMode = 9; |
| 7757 | else xmlCompressMode = mode; |
| 7758 | } |
| 7759 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7760 | #define XML_TREE_NSMAP_PARENT -1 |
| 7761 | #define XML_TREE_NSMAP_XML -2 |
| 7762 | #define XML_TREE_NSMAP_DOC -3 |
| 7763 | #define XML_TREE_NSMAP_CUSTOM -4 |
| 7764 | |
| 7765 | typedef struct xmlNsMapItem *xmlNsMapItemPtr; |
| 7766 | struct xmlNsMapItem { |
| 7767 | xmlNsMapItemPtr next; |
| 7768 | xmlNsMapItemPtr prev; |
| 7769 | xmlNsPtr oldNs; /* old ns decl reference */ |
| 7770 | xmlNsPtr newNs; /* new ns decl reference */ |
| 7771 | int shadowDepth; /* Shadowed at this depth */ |
| 7772 | /* |
| 7773 | * depth: |
| 7774 | * >= 0 == @node's ns-decls |
| 7775 | * -1 == @parent's ns-decls |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7776 | * -2 == the doc->oldNs XML ns-decl |
| 7777 | * -3 == the doc->oldNs storage ns-decls |
| 7778 | * -4 == ns-decls provided via custom ns-handling |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7779 | */ |
| 7780 | int depth; |
| 7781 | }; |
| 7782 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7783 | typedef struct xmlNsMap *xmlNsMapPtr; |
| 7784 | struct xmlNsMap { |
| 7785 | xmlNsMapItemPtr first; |
| 7786 | xmlNsMapItemPtr last; |
| 7787 | xmlNsMapItemPtr pool; |
| 7788 | }; |
| 7789 | |
| 7790 | #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL)) |
| 7791 | #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next) |
| 7792 | #define XML_NSMAP_POP(m, i) \ |
| 7793 | i = (m)->last; \ |
| 7794 | (m)->last = (i)->prev; \ |
| 7795 | if ((m)->last == NULL) \ |
| 7796 | (m)->first = NULL; \ |
| 7797 | else \ |
| 7798 | (m)->last->next = NULL; \ |
| 7799 | (i)->next = (m)->pool; \ |
| 7800 | (m)->pool = i; |
| 7801 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7802 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7803 | * xmlDOMWrapNsMapFree: |
| 7804 | * @map: the ns-map |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7805 | * |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7806 | * Frees the ns-map |
| 7807 | */ |
| 7808 | static void |
| 7809 | xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap) |
| 7810 | { |
| 7811 | xmlNsMapItemPtr cur, tmp; |
| 7812 | |
| 7813 | if (nsmap == NULL) |
| 7814 | return; |
| 7815 | cur = nsmap->pool; |
| 7816 | while (cur != NULL) { |
| 7817 | tmp = cur; |
| 7818 | cur = cur->next; |
| 7819 | xmlFree(tmp); |
| 7820 | } |
| 7821 | cur = nsmap->first; |
| 7822 | while (cur != NULL) { |
| 7823 | tmp = cur; |
| 7824 | cur = cur->next; |
| 7825 | xmlFree(tmp); |
| 7826 | } |
| 7827 | xmlFree(nsmap); |
| 7828 | } |
| 7829 | |
| 7830 | /* |
| 7831 | * xmlDOMWrapNsMapAddItem: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7832 | * @map: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7833 | * @oldNs: the old ns-struct |
| 7834 | * @newNs: the new ns-struct |
| 7835 | * @depth: depth and ns-kind information |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7836 | * |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7837 | * Adds an ns-mapping item. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7838 | */ |
| 7839 | static xmlNsMapItemPtr |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7840 | xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7841 | xmlNsPtr oldNs, xmlNsPtr newNs, int depth) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7842 | { |
| 7843 | xmlNsMapItemPtr ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7844 | xmlNsMapPtr map; |
| 7845 | |
| 7846 | if (nsmap == NULL) |
| 7847 | return(NULL); |
| 7848 | if ((position != -1) && (position != 0)) |
| 7849 | return(NULL); |
| 7850 | map = *nsmap; |
| 7851 | |
| 7852 | if (map == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7853 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7854 | * Create the ns-map. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7855 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7856 | map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap)); |
| 7857 | if (map == NULL) { |
| 7858 | xmlTreeErrMemory("allocating namespace map"); |
| 7859 | return (NULL); |
| 7860 | } |
| 7861 | memset(map, 0, sizeof(struct xmlNsMap)); |
| 7862 | *nsmap = map; |
| 7863 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7864 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7865 | if (map->pool != NULL) { |
| 7866 | /* |
| 7867 | * Reuse an item from the pool. |
| 7868 | */ |
| 7869 | ret = map->pool; |
| 7870 | map->pool = ret->next; |
| 7871 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7872 | } else { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7873 | /* |
| 7874 | * Create a new item. |
| 7875 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7876 | ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem)); |
| 7877 | if (ret == NULL) { |
| 7878 | xmlTreeErrMemory("allocating namespace map item"); |
| 7879 | return (NULL); |
| 7880 | } |
| 7881 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7882 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7883 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7884 | if (map->first == NULL) { |
| 7885 | /* |
| 7886 | * First ever. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7887 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7888 | map->first = ret; |
| 7889 | map->last = ret; |
| 7890 | } else if (position == -1) { |
| 7891 | /* |
| 7892 | * Append. |
| 7893 | */ |
| 7894 | ret->prev = map->last; |
| 7895 | map->last->next = ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7896 | map->last = ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7897 | } else if (position == 0) { |
| 7898 | /* |
| 7899 | * Set on first position. |
| 7900 | */ |
| 7901 | map->first->prev = ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7902 | ret->next = map->first; |
| 7903 | map->first = ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7904 | } else |
| 7905 | return(NULL); |
| 7906 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7907 | ret->oldNs = oldNs; |
| 7908 | ret->newNs = newNs; |
| 7909 | ret->shadowDepth = -1; |
| 7910 | ret->depth = depth; |
| 7911 | return (ret); |
| 7912 | } |
| 7913 | |
| 7914 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7915 | * xmlDOMWrapStoreNs: |
| 7916 | * @doc: the doc |
| 7917 | * @nsName: the namespace name |
| 7918 | * @prefix: the prefix |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7919 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7920 | * Creates or reuses an xmlNs struct on doc->oldNs with |
| 7921 | * the given prefix and namespace name. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7922 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7923 | * Returns the aquired ns struct or NULL in case of an API |
| 7924 | * or internal error. |
| 7925 | */ |
| 7926 | static xmlNsPtr |
| 7927 | xmlDOMWrapStoreNs(xmlDocPtr doc, |
| 7928 | const xmlChar *nsName, |
| 7929 | const xmlChar *prefix) |
| 7930 | { |
| 7931 | xmlNsPtr ns; |
| 7932 | |
| 7933 | if (doc == NULL) |
| 7934 | return (NULL); |
| 7935 | ns = xmlTreeEnsureXMLDecl(doc); |
| 7936 | if (ns == NULL) |
| 7937 | return (NULL); |
| 7938 | if (ns->next != NULL) { |
| 7939 | /* Reuse. */ |
| 7940 | ns = ns->next; |
| 7941 | while (ns != NULL) { |
| 7942 | if (((ns->prefix == prefix) || |
| 7943 | xmlStrEqual(ns->prefix, prefix)) && |
| 7944 | xmlStrEqual(ns->href, nsName)) { |
| 7945 | return (ns); |
| 7946 | } |
| 7947 | if (ns->next == NULL) |
| 7948 | break; |
| 7949 | ns = ns->next; |
| 7950 | } |
| 7951 | } |
| 7952 | /* Create. */ |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 7953 | if (ns != NULL) { |
| 7954 | ns->next = xmlNewNs(NULL, nsName, prefix); |
| 7955 | return (ns->next); |
| 7956 | } |
| 7957 | return(NULL); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7958 | } |
| 7959 | |
| 7960 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7961 | * xmlDOMWrapNewCtxt: |
| 7962 | * |
| 7963 | * Allocates and initializes a new DOM-wrapper context. |
| 7964 | * |
Jan Pokorný | 7580165 | 2013-12-19 15:09:14 +0100 | [diff] [blame] | 7965 | * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7966 | */ |
| 7967 | xmlDOMWrapCtxtPtr |
| 7968 | xmlDOMWrapNewCtxt(void) |
| 7969 | { |
| 7970 | xmlDOMWrapCtxtPtr ret; |
| 7971 | |
| 7972 | ret = xmlMalloc(sizeof(xmlDOMWrapCtxt)); |
| 7973 | if (ret == NULL) { |
| 7974 | xmlTreeErrMemory("allocating DOM-wrapper context"); |
| 7975 | return (NULL); |
| 7976 | } |
| 7977 | memset(ret, 0, sizeof(xmlDOMWrapCtxt)); |
| 7978 | return (ret); |
| 7979 | } |
| 7980 | |
| 7981 | /* |
| 7982 | * xmlDOMWrapFreeCtxt: |
| 7983 | * @ctxt: the DOM-wrapper context |
| 7984 | * |
| 7985 | * Frees the DOM-wrapper context. |
| 7986 | */ |
| 7987 | void |
| 7988 | xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt) |
| 7989 | { |
| 7990 | if (ctxt == NULL) |
| 7991 | return; |
| 7992 | if (ctxt->namespaceMap != NULL) |
| 7993 | xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap); |
| 7994 | /* |
| 7995 | * TODO: Store the namespace map in the context. |
| 7996 | */ |
| 7997 | xmlFree(ctxt); |
| 7998 | } |
| 7999 | |
| 8000 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8001 | * xmlTreeLookupNsListByPrefix: |
| 8002 | * @nsList: a list of ns-structs |
| 8003 | * @prefix: the searched prefix |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8004 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8005 | * Searches for a ns-decl with the given prefix in @nsList. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8006 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8007 | * Returns the ns-decl if found, NULL if not found and on |
| 8008 | * API errors. |
| 8009 | */ |
| 8010 | static xmlNsPtr |
| 8011 | xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix) |
| 8012 | { |
| 8013 | if (nsList == NULL) |
| 8014 | return (NULL); |
| 8015 | { |
| 8016 | xmlNsPtr ns; |
| 8017 | ns = nsList; |
| 8018 | do { |
| 8019 | if ((prefix == ns->prefix) || |
| 8020 | xmlStrEqual(prefix, ns->prefix)) { |
| 8021 | return (ns); |
| 8022 | } |
| 8023 | ns = ns->next; |
| 8024 | } while (ns != NULL); |
| 8025 | } |
| 8026 | return (NULL); |
| 8027 | } |
| 8028 | |
| 8029 | /* |
| 8030 | * |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8031 | * xmlDOMWrapNSNormGatherInScopeNs: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8032 | * @map: the namespace map |
| 8033 | * @node: the node to start with |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8034 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8035 | * Puts in-scope namespaces into the ns-map. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8036 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8037 | * Returns 0 on success, -1 on API or internal errors. |
| 8038 | */ |
| 8039 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8040 | xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8041 | xmlNodePtr node) |
| 8042 | { |
| 8043 | xmlNodePtr cur; |
| 8044 | xmlNsPtr ns; |
| 8045 | xmlNsMapItemPtr mi; |
| 8046 | int shadowed; |
| 8047 | |
| 8048 | if ((map == NULL) || (*map != NULL)) |
| 8049 | return (-1); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8050 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 8051 | return (-1); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8052 | /* |
| 8053 | * Get in-scope ns-decls of @parent. |
| 8054 | */ |
| 8055 | cur = node; |
| 8056 | while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) { |
| 8057 | if (cur->type == XML_ELEMENT_NODE) { |
| 8058 | if (cur->nsDef != NULL) { |
| 8059 | ns = cur->nsDef; |
| 8060 | do { |
| 8061 | shadowed = 0; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8062 | if (XML_NSMAP_NOTEMPTY(*map)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8063 | /* |
| 8064 | * Skip shadowed prefixes. |
| 8065 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8066 | XML_NSMAP_FOREACH(*map, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8067 | if ((ns->prefix == mi->newNs->prefix) || |
| 8068 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) { |
| 8069 | shadowed = 1; |
| 8070 | break; |
| 8071 | } |
| 8072 | } |
| 8073 | } |
| 8074 | /* |
| 8075 | * Insert mapping. |
| 8076 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8077 | mi = xmlDOMWrapNsMapAddItem(map, 0, NULL, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8078 | ns, XML_TREE_NSMAP_PARENT); |
| 8079 | if (mi == NULL) |
| 8080 | return (-1); |
| 8081 | if (shadowed) |
| 8082 | mi->shadowDepth = 0; |
| 8083 | ns = ns->next; |
| 8084 | } while (ns != NULL); |
| 8085 | } |
| 8086 | } |
| 8087 | cur = cur->parent; |
| 8088 | } |
| 8089 | return (0); |
| 8090 | } |
| 8091 | |
| 8092 | /* |
| 8093 | * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict; |
| 8094 | * otherwise copy it, when it was in the source-dict. |
| 8095 | */ |
| 8096 | #define XML_TREE_ADOPT_STR(str) \ |
| 8097 | if (adoptStr && (str != NULL)) { \ |
| 8098 | if (destDoc->dict) { \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 8099 | const xmlChar *old = str; \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8100 | str = xmlDictLookup(destDoc->dict, str, -1); \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 8101 | if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \ |
| 8102 | (!xmlDictOwns(sourceDoc->dict, old))) \ |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 8103 | xmlFree((char *)old); \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8104 | } else if ((sourceDoc) && (sourceDoc->dict) && \ |
| 8105 | xmlDictOwns(sourceDoc->dict, str)) { \ |
| 8106 | str = BAD_CAST xmlStrdup(str); \ |
| 8107 | } \ |
| 8108 | } |
| 8109 | |
| 8110 | /* |
| 8111 | * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then |
| 8112 | * put it in dest-dict or copy it. |
| 8113 | */ |
| 8114 | #define XML_TREE_ADOPT_STR_2(str) \ |
| 8115 | if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \ |
| 8116 | (sourceDoc->dict != NULL) && \ |
| 8117 | xmlDictOwns(sourceDoc->dict, cur->content)) { \ |
| 8118 | if (destDoc->dict) \ |
| 8119 | cur->content = (xmlChar *) \ |
| 8120 | xmlDictLookup(destDoc->dict, cur->content, -1); \ |
| 8121 | else \ |
| 8122 | cur->content = xmlStrdup(BAD_CAST cur->content); \ |
| 8123 | } |
| 8124 | |
| 8125 | /* |
| 8126 | * xmlDOMWrapNSNormAddNsMapItem2: |
| 8127 | * |
| 8128 | * For internal use. Adds a ns-decl mapping. |
| 8129 | * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8130 | * Returns 0 on success, -1 on internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8131 | */ |
| 8132 | static int |
| 8133 | xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number, |
| 8134 | xmlNsPtr oldNs, xmlNsPtr newNs) |
| 8135 | { |
| 8136 | if (*list == NULL) { |
| 8137 | *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr)); |
| 8138 | if (*list == NULL) { |
| 8139 | xmlTreeErrMemory("alloc ns map item"); |
| 8140 | return(-1); |
| 8141 | } |
| 8142 | *size = 3; |
| 8143 | *number = 0; |
| 8144 | } else if ((*number) >= (*size)) { |
| 8145 | *size *= 2; |
| 8146 | *list = (xmlNsPtr *) xmlRealloc(*list, |
| 8147 | (*size) * 2 * sizeof(xmlNsPtr)); |
| 8148 | if (*list == NULL) { |
| 8149 | xmlTreeErrMemory("realloc ns map item"); |
| 8150 | return(-1); |
| 8151 | } |
| 8152 | } |
| 8153 | (*list)[2 * (*number)] = oldNs; |
| 8154 | (*list)[2 * (*number) +1] = newNs; |
| 8155 | (*number)++; |
| 8156 | return (0); |
| 8157 | } |
| 8158 | |
| 8159 | /* |
| 8160 | * xmlDOMWrapRemoveNode: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8161 | * @ctxt: a DOM wrapper context |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8162 | * @doc: the doc |
| 8163 | * @node: the node to be removed. |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8164 | * @options: set of options, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8165 | * |
| 8166 | * Unlinks the given node from its owner. |
| 8167 | * This will substitute ns-references to node->nsDef for |
| 8168 | * ns-references to doc->oldNs, thus ensuring the removed |
| 8169 | * branch to be autark wrt ns-references. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8170 | * |
| 8171 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8172 | * |
| 8173 | * Returns 0 on success, 1 if the node is not supported, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8174 | * -1 on API and internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8175 | */ |
| 8176 | int |
| 8177 | xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc, |
| 8178 | xmlNodePtr node, int options ATTRIBUTE_UNUSED) |
| 8179 | { |
| 8180 | xmlNsPtr *list = NULL; |
| 8181 | int sizeList, nbList, i, j; |
| 8182 | xmlNsPtr ns; |
| 8183 | |
| 8184 | if ((node == NULL) || (doc == NULL) || (node->doc != doc)) |
| 8185 | return (-1); |
| 8186 | |
| 8187 | /* TODO: 0 or -1 ? */ |
| 8188 | if (node->parent == NULL) |
| 8189 | return (0); |
| 8190 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8191 | switch (node->type) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8192 | case XML_TEXT_NODE: |
| 8193 | case XML_CDATA_SECTION_NODE: |
| 8194 | case XML_ENTITY_REF_NODE: |
| 8195 | case XML_PI_NODE: |
| 8196 | case XML_COMMENT_NODE: |
| 8197 | xmlUnlinkNode(node); |
| 8198 | return (0); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8199 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8200 | case XML_ATTRIBUTE_NODE: |
| 8201 | break; |
| 8202 | default: |
| 8203 | return (1); |
| 8204 | } |
| 8205 | xmlUnlinkNode(node); |
| 8206 | /* |
| 8207 | * Save out-of-scope ns-references in doc->oldNs. |
| 8208 | */ |
| 8209 | do { |
| 8210 | switch (node->type) { |
| 8211 | case XML_ELEMENT_NODE: |
| 8212 | if ((ctxt == NULL) && (node->nsDef != NULL)) { |
| 8213 | ns = node->nsDef; |
| 8214 | do { |
| 8215 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 8216 | &nbList, ns, ns) == -1) |
| 8217 | goto internal_error; |
| 8218 | ns = ns->next; |
| 8219 | } while (ns != NULL); |
| 8220 | } |
| 8221 | /* No break on purpose. */ |
| 8222 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8223 | if (node->ns != NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8224 | /* |
| 8225 | * Find a mapping. |
| 8226 | */ |
| 8227 | if (list != NULL) { |
| 8228 | for (i = 0, j = 0; i < nbList; i++, j += 2) { |
| 8229 | if (node->ns == list[j]) { |
| 8230 | node->ns = list[++j]; |
| 8231 | goto next_node; |
| 8232 | } |
| 8233 | } |
| 8234 | } |
| 8235 | ns = NULL; |
| 8236 | if (ctxt != NULL) { |
| 8237 | /* |
| 8238 | * User defined. |
| 8239 | */ |
| 8240 | } else { |
| 8241 | /* |
| 8242 | * Add to doc's oldNs. |
| 8243 | */ |
| 8244 | ns = xmlDOMWrapStoreNs(doc, node->ns->href, |
| 8245 | node->ns->prefix); |
| 8246 | if (ns == NULL) |
| 8247 | goto internal_error; |
| 8248 | } |
| 8249 | if (ns != NULL) { |
| 8250 | /* |
| 8251 | * Add mapping. |
| 8252 | */ |
| 8253 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 8254 | &nbList, node->ns, ns) == -1) |
| 8255 | goto internal_error; |
| 8256 | } |
| 8257 | node->ns = ns; |
| 8258 | } |
| 8259 | if ((node->type == XML_ELEMENT_NODE) && |
| 8260 | (node->properties != NULL)) { |
| 8261 | node = (xmlNodePtr) node->properties; |
| 8262 | continue; |
| 8263 | } |
| 8264 | break; |
| 8265 | default: |
| 8266 | goto next_sibling; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8267 | } |
| 8268 | next_node: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8269 | if ((node->type == XML_ELEMENT_NODE) && |
| 8270 | (node->children != NULL)) { |
| 8271 | node = node->children; |
| 8272 | continue; |
| 8273 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8274 | next_sibling: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8275 | if (node == NULL) |
| 8276 | break; |
| 8277 | if (node->next != NULL) |
| 8278 | node = node->next; |
| 8279 | else { |
| 8280 | node = node->parent; |
| 8281 | goto next_sibling; |
| 8282 | } |
| 8283 | } while (node != NULL); |
| 8284 | |
| 8285 | if (list != NULL) |
| 8286 | xmlFree(list); |
| 8287 | return (0); |
| 8288 | |
| 8289 | internal_error: |
| 8290 | if (list != NULL) |
| 8291 | xmlFree(list); |
| 8292 | return (-1); |
| 8293 | } |
| 8294 | |
| 8295 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8296 | * xmlSearchNsByNamespaceStrict: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8297 | * @doc: the document |
| 8298 | * @node: the start node |
| 8299 | * @nsName: the searched namespace name |
| 8300 | * @retNs: the resulting ns-decl |
| 8301 | * @prefixed: if the found ns-decl must have a prefix (for attributes) |
| 8302 | * |
| 8303 | * Dynamically searches for a ns-declaration which matches |
| 8304 | * the given @nsName in the ancestor-or-self axis of @node. |
| 8305 | * |
| 8306 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8307 | * and internal errors. |
| 8308 | */ |
| 8309 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8310 | xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node, |
| 8311 | const xmlChar* nsName, |
| 8312 | xmlNsPtr *retNs, int prefixed) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8313 | { |
| 8314 | xmlNodePtr cur, prev = NULL, out = NULL; |
| 8315 | xmlNsPtr ns, prevns; |
| 8316 | |
| 8317 | if ((doc == NULL) || (nsName == NULL) || (retNs == NULL)) |
| 8318 | return (-1); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8319 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 8320 | return(-1); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8321 | |
| 8322 | *retNs = NULL; |
| 8323 | if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
| 8324 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8325 | if (*retNs == NULL) |
| 8326 | return (-1); |
| 8327 | return (1); |
| 8328 | } |
| 8329 | cur = node; |
| 8330 | do { |
| 8331 | if (cur->type == XML_ELEMENT_NODE) { |
| 8332 | if (cur->nsDef != NULL) { |
| 8333 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8334 | if (prefixed && (ns->prefix == NULL)) |
| 8335 | continue; |
| 8336 | if (prev != NULL) { |
| 8337 | /* |
| 8338 | * Check the last level of ns-decls for a |
| 8339 | * shadowing prefix. |
| 8340 | */ |
| 8341 | prevns = prev->nsDef; |
| 8342 | do { |
| 8343 | if ((prevns->prefix == ns->prefix) || |
| 8344 | ((prevns->prefix != NULL) && |
| 8345 | (ns->prefix != NULL) && |
| 8346 | xmlStrEqual(prevns->prefix, ns->prefix))) { |
| 8347 | /* |
| 8348 | * Shadowed. |
| 8349 | */ |
| 8350 | break; |
| 8351 | } |
| 8352 | prevns = prevns->next; |
| 8353 | } while (prevns != NULL); |
| 8354 | if (prevns != NULL) |
| 8355 | continue; |
| 8356 | } |
| 8357 | /* |
| 8358 | * Ns-name comparison. |
| 8359 | */ |
| 8360 | if ((nsName == ns->href) || |
| 8361 | xmlStrEqual(nsName, ns->href)) { |
| 8362 | /* |
| 8363 | * At this point the prefix can only be shadowed, |
| 8364 | * if we are the the (at least) 3rd level of |
| 8365 | * ns-decls. |
| 8366 | */ |
| 8367 | if (out) { |
| 8368 | int ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8369 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8370 | ret = xmlNsInScope(doc, node, prev, ns->prefix); |
| 8371 | if (ret < 0) |
| 8372 | return (-1); |
| 8373 | /* |
| 8374 | * TODO: Should we try to find a matching ns-name |
| 8375 | * only once? This here keeps on searching. |
| 8376 | * I think we should try further since, there might |
| 8377 | * be an other matching ns-decl with an unshadowed |
| 8378 | * prefix. |
| 8379 | */ |
| 8380 | if (! ret) |
| 8381 | continue; |
| 8382 | } |
| 8383 | *retNs = ns; |
| 8384 | return (1); |
| 8385 | } |
| 8386 | } |
| 8387 | out = prev; |
| 8388 | prev = cur; |
| 8389 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8390 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 8391 | (cur->type == XML_ENTITY_DECL)) |
| 8392 | return (0); |
| 8393 | cur = cur->parent; |
| 8394 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 8395 | return (0); |
| 8396 | } |
| 8397 | |
| 8398 | /* |
| 8399 | * xmlSearchNsByPrefixStrict: |
| 8400 | * @doc: the document |
| 8401 | * @node: the start node |
| 8402 | * @prefix: the searched namespace prefix |
| 8403 | * @retNs: the resulting ns-decl |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8404 | * |
| 8405 | * Dynamically searches for a ns-declaration which matches |
| 8406 | * the given @nsName in the ancestor-or-self axis of @node. |
| 8407 | * |
| 8408 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8409 | * and internal errors. |
| 8410 | */ |
| 8411 | static int |
| 8412 | xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node, |
| 8413 | const xmlChar* prefix, |
| 8414 | xmlNsPtr *retNs) |
| 8415 | { |
| 8416 | xmlNodePtr cur; |
| 8417 | xmlNsPtr ns; |
| 8418 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8419 | if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 8420 | return(-1); |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8421 | |
| 8422 | if (retNs) |
| 8423 | *retNs = NULL; |
| 8424 | if (IS_STR_XML(prefix)) { |
| 8425 | if (retNs) { |
| 8426 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8427 | if (*retNs == NULL) |
| 8428 | return (-1); |
| 8429 | } |
| 8430 | return (1); |
| 8431 | } |
| 8432 | cur = node; |
| 8433 | do { |
| 8434 | if (cur->type == XML_ELEMENT_NODE) { |
| 8435 | if (cur->nsDef != NULL) { |
| 8436 | ns = cur->nsDef; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8437 | do { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8438 | if ((prefix == ns->prefix) || |
| 8439 | xmlStrEqual(prefix, ns->prefix)) |
| 8440 | { |
| 8441 | /* |
| 8442 | * Disabled namespaces, e.g. xmlns:abc="". |
| 8443 | */ |
| 8444 | if (ns->href == NULL) |
| 8445 | return(0); |
| 8446 | if (retNs) |
| 8447 | *retNs = ns; |
| 8448 | return (1); |
| 8449 | } |
| 8450 | ns = ns->next; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8451 | } while (ns != NULL); |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8452 | } |
| 8453 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 8454 | (cur->type == XML_ENTITY_DECL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8455 | return (0); |
| 8456 | cur = cur->parent; |
| 8457 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 8458 | return (0); |
| 8459 | } |
| 8460 | |
| 8461 | /* |
| 8462 | * xmlDOMWrapNSNormDeclareNsForced: |
| 8463 | * @doc: the doc |
| 8464 | * @elem: the element-node to declare on |
| 8465 | * @nsName: the namespace-name of the ns-decl |
| 8466 | * @prefix: the preferred prefix of the ns-decl |
| 8467 | * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls |
| 8468 | * |
| 8469 | * Declares a new namespace on @elem. It tries to use the |
| 8470 | * given @prefix; if a ns-decl with the given prefix is already existent |
| 8471 | * on @elem, it will generate an other prefix. |
| 8472 | * |
| 8473 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8474 | * and internal errors. |
| 8475 | */ |
| 8476 | static xmlNsPtr |
| 8477 | xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc, |
| 8478 | xmlNodePtr elem, |
| 8479 | const xmlChar *nsName, |
| 8480 | const xmlChar *prefix, |
| 8481 | int checkShadow) |
| 8482 | { |
| 8483 | |
| 8484 | xmlNsPtr ret; |
| 8485 | char buf[50]; |
| 8486 | const xmlChar *pref; |
| 8487 | int counter = 0; |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8488 | |
| 8489 | if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE)) |
| 8490 | return(NULL); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8491 | /* |
| 8492 | * Create a ns-decl on @anchor. |
| 8493 | */ |
| 8494 | pref = prefix; |
| 8495 | while (1) { |
| 8496 | /* |
| 8497 | * Lookup whether the prefix is unused in elem's ns-decls. |
| 8498 | */ |
| 8499 | if ((elem->nsDef != NULL) && |
| 8500 | (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL)) |
| 8501 | goto ns_next_prefix; |
| 8502 | if (checkShadow && elem->parent && |
| 8503 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8504 | /* |
| 8505 | * Does it shadow ancestor ns-decls? |
| 8506 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8507 | if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8508 | goto ns_next_prefix; |
| 8509 | } |
| 8510 | ret = xmlNewNs(NULL, nsName, pref); |
| 8511 | if (ret == NULL) |
| 8512 | return (NULL); |
| 8513 | if (elem->nsDef == NULL) |
| 8514 | elem->nsDef = ret; |
| 8515 | else { |
| 8516 | xmlNsPtr ns2 = elem->nsDef; |
| 8517 | while (ns2->next != NULL) |
| 8518 | ns2 = ns2->next; |
| 8519 | ns2->next = ret; |
| 8520 | } |
| 8521 | return (ret); |
| 8522 | ns_next_prefix: |
| 8523 | counter++; |
| 8524 | if (counter > 1000) |
| 8525 | return (NULL); |
| 8526 | if (prefix == NULL) { |
| 8527 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8528 | "ns_%d", counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8529 | } else |
| 8530 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8531 | "%.30s_%d", (char *)prefix, counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8532 | pref = BAD_CAST buf; |
| 8533 | } |
| 8534 | } |
| 8535 | |
| 8536 | /* |
| 8537 | * xmlDOMWrapNSNormAquireNormalizedNs: |
| 8538 | * @doc: the doc |
| 8539 | * @elem: the element-node to declare namespaces on |
| 8540 | * @ns: the ns-struct to use for the search |
| 8541 | * @retNs: the found/created ns-struct |
| 8542 | * @nsMap: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8543 | * @depth: the current tree depth |
| 8544 | * @ancestorsOnly: search in ancestor ns-decls only |
| 8545 | * @prefixed: if the searched ns-decl must have a prefix (for attributes) |
| 8546 | * |
| 8547 | * Searches for a matching ns-name in the ns-decls of @nsMap, if not |
| 8548 | * found it will either declare it on @elem, or store it in doc->oldNs. |
| 8549 | * If a new ns-decl needs to be declared on @elem, it tries to use the |
| 8550 | * @ns->prefix for it, if this prefix is already in use on @elem, it will |
| 8551 | * change the prefix or the new ns-decl. |
| 8552 | * |
| 8553 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8554 | */ |
| 8555 | static int |
| 8556 | xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc, |
| 8557 | xmlNodePtr elem, |
| 8558 | xmlNsPtr ns, |
| 8559 | xmlNsPtr *retNs, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8560 | xmlNsMapPtr *nsMap, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8561 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8562 | int depth, |
| 8563 | int ancestorsOnly, |
| 8564 | int prefixed) |
| 8565 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8566 | xmlNsMapItemPtr mi; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8567 | |
| 8568 | if ((doc == NULL) || (ns == NULL) || (retNs == NULL) || |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8569 | (nsMap == NULL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8570 | return (-1); |
| 8571 | |
| 8572 | *retNs = NULL; |
| 8573 | /* |
| 8574 | * Handle XML namespace. |
| 8575 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8576 | if (IS_STR_XML(ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8577 | /* |
| 8578 | * Insert XML namespace mapping. |
| 8579 | */ |
| 8580 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8581 | if (*retNs == NULL) |
| 8582 | return (-1); |
| 8583 | return (0); |
| 8584 | } |
| 8585 | /* |
| 8586 | * If the search should be done in ancestors only and no |
| 8587 | * @elem (the first ancestor) was specified, then skip the search. |
| 8588 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8589 | if ((XML_NSMAP_NOTEMPTY(*nsMap)) && |
| 8590 | (! (ancestorsOnly && (elem == NULL)))) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8591 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8592 | /* |
| 8593 | * Try to find an equal ns-name in in-scope ns-decls. |
| 8594 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8595 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8596 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8597 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8598 | * ancestorsOnly: This should be turned on to gain speed, |
| 8599 | * if one knows that the branch itself was already |
| 8600 | * ns-wellformed and no stale references existed. |
| 8601 | * I.e. it searches in the ancestor axis only. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8602 | */ |
| 8603 | ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) && |
| 8604 | /* Skip shadowed prefixes. */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8605 | (mi->shadowDepth == -1) && |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8606 | /* Skip xmlns="" or xmlns:foo="". */ |
| 8607 | ((mi->newNs->href != NULL) && |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8608 | (mi->newNs->href[0] != 0)) && |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8609 | /* Ensure a prefix if wanted. */ |
| 8610 | ((! prefixed) || (mi->newNs->prefix != NULL)) && |
| 8611 | /* Equal ns name */ |
| 8612 | ((mi->newNs->href == ns->href) || |
| 8613 | xmlStrEqual(mi->newNs->href, ns->href))) { |
| 8614 | /* Set the mapping. */ |
| 8615 | mi->oldNs = ns; |
| 8616 | *retNs = mi->newNs; |
| 8617 | return (0); |
| 8618 | } |
| 8619 | } |
| 8620 | } |
| 8621 | /* |
| 8622 | * No luck, the namespace is out of scope or shadowed. |
| 8623 | */ |
| 8624 | if (elem == NULL) { |
| 8625 | xmlNsPtr tmpns; |
| 8626 | |
| 8627 | /* |
| 8628 | * Store ns-decls in "oldNs" of the document-node. |
| 8629 | */ |
| 8630 | tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix); |
| 8631 | if (tmpns == NULL) |
| 8632 | return (-1); |
| 8633 | /* |
| 8634 | * Insert mapping. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8635 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8636 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8637 | tmpns, XML_TREE_NSMAP_DOC) == NULL) { |
| 8638 | xmlFreeNs(tmpns); |
| 8639 | return (-1); |
| 8640 | } |
| 8641 | *retNs = tmpns; |
| 8642 | } else { |
| 8643 | xmlNsPtr tmpns; |
| 8644 | |
| 8645 | tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href, |
| 8646 | ns->prefix, 0); |
| 8647 | if (tmpns == NULL) |
| 8648 | return (-1); |
| 8649 | |
| 8650 | if (*nsMap != NULL) { |
| 8651 | /* |
| 8652 | * Does it shadow ancestor ns-decls? |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8653 | */ |
| 8654 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8655 | if ((mi->depth < depth) && |
| 8656 | (mi->shadowDepth == -1) && |
| 8657 | ((ns->prefix == mi->newNs->prefix) || |
| 8658 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8659 | /* |
| 8660 | * Shadows. |
| 8661 | */ |
| 8662 | mi->shadowDepth = depth; |
| 8663 | break; |
| 8664 | } |
| 8665 | } |
| 8666 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8667 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8668 | xmlFreeNs(tmpns); |
| 8669 | return (-1); |
| 8670 | } |
| 8671 | *retNs = tmpns; |
| 8672 | } |
| 8673 | return (0); |
| 8674 | } |
| 8675 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8676 | typedef enum { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8677 | XML_DOM_RECONNS_REMOVEREDUND = 1<<0 |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8678 | } xmlDOMReconcileNSOptions; |
| 8679 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8680 | /* |
| 8681 | * xmlDOMWrapReconcileNamespaces: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8682 | * @ctxt: DOM wrapper context, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8683 | * @elem: the element-node |
| 8684 | * @options: option flags |
| 8685 | * |
| 8686 | * Ensures that ns-references point to ns-decls hold on element-nodes. |
| 8687 | * Ensures that the tree is namespace wellformed by creating additional |
| 8688 | * ns-decls where needed. Note that, since prefixes of already existent |
| 8689 | * ns-decls can be shadowed by this process, it could break QNames in |
| 8690 | * attribute values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8691 | * |
| 8692 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8693 | * |
| 8694 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8695 | */ |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8696 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8697 | int |
| 8698 | xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8699 | xmlNodePtr elem, |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8700 | int options) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8701 | { |
| 8702 | int depth = -1, adoptns = 0, parnsdone = 0; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8703 | xmlNsPtr ns, prevns; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8704 | xmlDocPtr doc; |
| 8705 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8706 | xmlNsMapPtr nsMap = NULL; |
| 8707 | xmlNsMapItemPtr /* topmi = NULL, */ mi; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8708 | /* @ancestorsOnly should be set by an option flag. */ |
| 8709 | int ancestorsOnly = 0; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8710 | int optRemoveRedundantNS = |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8711 | ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0; |
| 8712 | xmlNsPtr *listRedund = NULL; |
| 8713 | int sizeRedund = 0, nbRedund = 0, ret, i, j; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8714 | |
| 8715 | if ((elem == NULL) || (elem->doc == NULL) || |
| 8716 | (elem->type != XML_ELEMENT_NODE)) |
| 8717 | return (-1); |
| 8718 | |
| 8719 | doc = elem->doc; |
| 8720 | cur = elem; |
| 8721 | do { |
| 8722 | switch (cur->type) { |
| 8723 | case XML_ELEMENT_NODE: |
| 8724 | adoptns = 1; |
| 8725 | curElem = cur; |
| 8726 | depth++; |
| 8727 | /* |
| 8728 | * Namespace declarations. |
| 8729 | */ |
| 8730 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8731 | prevns = NULL; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8732 | ns = cur->nsDef; |
| 8733 | while (ns != NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8734 | if (! parnsdone) { |
| 8735 | if ((elem->parent) && |
| 8736 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8737 | /* |
| 8738 | * Gather ancestor in-scope ns-decls. |
| 8739 | */ |
| 8740 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8741 | elem->parent) == -1) |
| 8742 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8743 | } |
| 8744 | parnsdone = 1; |
| 8745 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8746 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8747 | /* |
| 8748 | * Lookup the ns ancestor-axis for equal ns-decls in scope. |
| 8749 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8750 | if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8751 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8752 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8753 | (mi->shadowDepth == -1) && |
| 8754 | ((ns->prefix == mi->newNs->prefix) || |
| 8755 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) && |
| 8756 | ((ns->href == mi->newNs->href) || |
| 8757 | xmlStrEqual(ns->href, mi->newNs->href))) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8758 | { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8759 | /* |
| 8760 | * A redundant ns-decl was found. |
| 8761 | * Add it to the list of redundant ns-decls. |
| 8762 | */ |
| 8763 | if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund, |
| 8764 | &sizeRedund, &nbRedund, ns, mi->newNs) == -1) |
| 8765 | goto internal_error; |
| 8766 | /* |
| 8767 | * Remove the ns-decl from the element-node. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8768 | */ |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8769 | if (prevns) |
| 8770 | prevns->next = ns->next; |
| 8771 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8772 | cur->nsDef = ns->next; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8773 | goto next_ns_decl; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8774 | } |
| 8775 | } |
| 8776 | } |
| 8777 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8778 | /* |
| 8779 | * Skip ns-references handling if the referenced |
| 8780 | * ns-decl is declared on the same element. |
| 8781 | */ |
| 8782 | if ((cur->ns != NULL) && adoptns && (cur->ns == ns)) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8783 | adoptns = 0; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8784 | /* |
| 8785 | * Does it shadow any ns-decl? |
| 8786 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8787 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
| 8788 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8789 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8790 | (mi->shadowDepth == -1) && |
| 8791 | ((ns->prefix == mi->newNs->prefix) || |
| 8792 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8793 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8794 | mi->shadowDepth = depth; |
| 8795 | } |
| 8796 | } |
| 8797 | } |
| 8798 | /* |
| 8799 | * Push mapping. |
| 8800 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8801 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8802 | depth) == NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8803 | goto internal_error; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8804 | |
| 8805 | prevns = ns; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8806 | next_ns_decl: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8807 | ns = ns->next; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8808 | } |
| 8809 | } |
| 8810 | if (! adoptns) |
| 8811 | goto ns_end; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8812 | /* No break on purpose. */ |
| 8813 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8814 | /* No ns, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8815 | if (cur->ns == NULL) |
| 8816 | goto ns_end; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8817 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8818 | if (! parnsdone) { |
| 8819 | if ((elem->parent) && |
| 8820 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8821 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8822 | elem->parent) == -1) |
| 8823 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8824 | } |
| 8825 | parnsdone = 1; |
| 8826 | } |
| 8827 | /* |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8828 | * Adjust the reference if this was a redundant ns-decl. |
| 8829 | */ |
| 8830 | if (listRedund) { |
| 8831 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8832 | if (cur->ns == listRedund[j]) { |
| 8833 | cur->ns = listRedund[++j]; |
| 8834 | break; |
| 8835 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8836 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8837 | } |
| 8838 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8839 | * Adopt ns-references. |
| 8840 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8841 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8842 | /* |
| 8843 | * Search for a mapping. |
| 8844 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8845 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8846 | if ((mi->shadowDepth == -1) && |
| 8847 | (cur->ns == mi->oldNs)) { |
| 8848 | |
| 8849 | cur->ns = mi->newNs; |
| 8850 | goto ns_end; |
| 8851 | } |
| 8852 | } |
| 8853 | } |
| 8854 | /* |
| 8855 | * Aquire a normalized ns-decl and add it to the map. |
| 8856 | */ |
| 8857 | if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem, |
| 8858 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8859 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8860 | ancestorsOnly, |
| 8861 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8862 | goto internal_error; |
| 8863 | cur->ns = ns; |
| 8864 | |
| 8865 | ns_end: |
| 8866 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8867 | (cur->properties != NULL)) { |
| 8868 | /* |
| 8869 | * Process attributes. |
| 8870 | */ |
| 8871 | cur = (xmlNodePtr) cur->properties; |
| 8872 | continue; |
| 8873 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8874 | break; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8875 | default: |
| 8876 | goto next_sibling; |
| 8877 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8878 | into_content: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8879 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8880 | (cur->children != NULL)) { |
| 8881 | /* |
| 8882 | * Process content of element-nodes only. |
| 8883 | */ |
| 8884 | cur = cur->children; |
| 8885 | continue; |
| 8886 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8887 | next_sibling: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8888 | if (cur == elem) |
| 8889 | break; |
| 8890 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8891 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8892 | /* |
| 8893 | * Pop mappings. |
| 8894 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8895 | while ((nsMap->last != NULL) && |
| 8896 | (nsMap->last->depth >= depth)) |
| 8897 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8898 | XML_NSMAP_POP(nsMap, mi) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8899 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8900 | /* |
| 8901 | * Unshadow. |
| 8902 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8903 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8904 | if (mi->shadowDepth >= depth) |
| 8905 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8906 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8907 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8908 | depth--; |
| 8909 | } |
| 8910 | if (cur->next != NULL) |
| 8911 | cur = cur->next; |
| 8912 | else { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8913 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 8914 | cur = cur->parent; |
| 8915 | goto into_content; |
| 8916 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8917 | cur = cur->parent; |
| 8918 | goto next_sibling; |
| 8919 | } |
| 8920 | } while (cur != NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8921 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8922 | ret = 0; |
| 8923 | goto exit; |
| 8924 | internal_error: |
| 8925 | ret = -1; |
| 8926 | exit: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8927 | if (listRedund) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8928 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8929 | xmlFreeNs(listRedund[j]); |
| 8930 | } |
| 8931 | xmlFree(listRedund); |
| 8932 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8933 | if (nsMap != NULL) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8934 | xmlDOMWrapNsMapFree(nsMap); |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8935 | return (ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8936 | } |
| 8937 | |
| 8938 | /* |
| 8939 | * xmlDOMWrapAdoptBranch: |
| 8940 | * @ctxt: the optional context for custom processing |
| 8941 | * @sourceDoc: the optional sourceDoc |
| 8942 | * @node: the element-node to start with |
| 8943 | * @destDoc: the destination doc for adoption |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8944 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8945 | * @options: option flags |
| 8946 | * |
| 8947 | * Ensures that ns-references point to @destDoc: either to |
| 8948 | * elements->nsDef entries if @destParent is given, or to |
| 8949 | * @destDoc->oldNs otherwise. |
| 8950 | * If @destParent is given, it ensures that the tree is namespace |
| 8951 | * wellformed by creating additional ns-decls where needed. |
| 8952 | * Note that, since prefixes of already existent ns-decls can be |
| 8953 | * shadowed by this process, it could break QNames in attribute |
| 8954 | * values or element content. |
| 8955 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8956 | * NOTE: This function was not intensively tested. |
| 8957 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8958 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8959 | */ |
| 8960 | static int |
| 8961 | xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt, |
| 8962 | xmlDocPtr sourceDoc, |
| 8963 | xmlNodePtr node, |
| 8964 | xmlDocPtr destDoc, |
| 8965 | xmlNodePtr destParent, |
| 8966 | int options ATTRIBUTE_UNUSED) |
| 8967 | { |
| 8968 | int ret = 0; |
| 8969 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8970 | xmlNsMapPtr nsMap = NULL; |
| 8971 | xmlNsMapItemPtr mi; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 8972 | xmlNsPtr ns = NULL; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8973 | int depth = -1, adoptStr = 1; |
| 8974 | /* gather @parent's ns-decls. */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8975 | int parnsdone; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8976 | /* @ancestorsOnly should be set per option. */ |
| 8977 | int ancestorsOnly = 0; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8978 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8979 | /* |
| 8980 | * Optimize string adoption for equal or none dicts. |
| 8981 | */ |
| 8982 | if ((sourceDoc != NULL) && |
| 8983 | (sourceDoc->dict == destDoc->dict)) |
| 8984 | adoptStr = 0; |
| 8985 | else |
| 8986 | adoptStr = 1; |
| 8987 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8988 | /* |
| 8989 | * Get the ns-map from the context if available. |
| 8990 | */ |
| 8991 | if (ctxt) |
| 8992 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 8993 | /* |
| 8994 | * Disable search for ns-decls in the parent-axis of the |
| 8995 | * desination element, if: |
| 8996 | * 1) there's no destination parent |
| 8997 | * 2) custom ns-reference handling is used |
| 8998 | */ |
| 8999 | if ((destParent == NULL) || |
| 9000 | (ctxt && ctxt->getNsForNodeFunc)) |
| 9001 | { |
| 9002 | parnsdone = 1; |
| 9003 | } else |
| 9004 | parnsdone = 0; |
| 9005 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9006 | cur = node; |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 9007 | if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) |
| 9008 | goto internal_error; |
| 9009 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9010 | while (cur != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9011 | /* |
| 9012 | * Paranoid source-doc sanity check. |
| 9013 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9014 | if (cur->doc != sourceDoc) { |
| 9015 | /* |
| 9016 | * We'll assume XIncluded nodes if the doc differs. |
| 9017 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 9018 | * This here skips XIncluded nodes and tries to handle |
| 9019 | * broken sequences. |
| 9020 | */ |
| 9021 | if (cur->next == NULL) |
| 9022 | goto leave_node; |
| 9023 | do { |
| 9024 | cur = cur->next; |
| 9025 | if ((cur->type == XML_XINCLUDE_END) || |
| 9026 | (cur->doc == node->doc)) |
| 9027 | break; |
| 9028 | } while (cur->next != NULL); |
| 9029 | |
| 9030 | if (cur->doc != node->doc) |
| 9031 | goto leave_node; |
| 9032 | } |
| 9033 | cur->doc = destDoc; |
| 9034 | switch (cur->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9035 | case XML_XINCLUDE_START: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9036 | case XML_XINCLUDE_END: |
| 9037 | /* |
| 9038 | * TODO |
| 9039 | */ |
| 9040 | return (-1); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9041 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9042 | curElem = cur; |
| 9043 | depth++; |
| 9044 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9045 | * Namespace declarations. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9046 | * - ns->href and ns->prefix are never in the dict, so |
| 9047 | * we need not move the values over to the destination dict. |
| 9048 | * - Note that for custom handling of ns-references, |
| 9049 | * the ns-decls need not be stored in the ns-map, |
| 9050 | * since they won't be referenced by node->ns. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9051 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9052 | if ((cur->nsDef) && |
| 9053 | ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL))) |
| 9054 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9055 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9056 | /* |
| 9057 | * Gather @parent's in-scope ns-decls. |
| 9058 | */ |
| 9059 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 9060 | destParent) == -1) |
| 9061 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9062 | parnsdone = 1; |
| 9063 | } |
| 9064 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 9065 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9066 | * NOTE: ns->prefix and ns->href are never in the dict. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9067 | * XML_TREE_ADOPT_STR(ns->prefix) |
| 9068 | * XML_TREE_ADOPT_STR(ns->href) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9069 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9070 | /* |
| 9071 | * Does it shadow any ns-decl? |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9072 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9073 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9074 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9075 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 9076 | (mi->shadowDepth == -1) && |
| 9077 | ((ns->prefix == mi->newNs->prefix) || |
| 9078 | xmlStrEqual(ns->prefix, |
| 9079 | mi->newNs->prefix))) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9080 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9081 | mi->shadowDepth = depth; |
| 9082 | } |
| 9083 | } |
| 9084 | } |
| 9085 | /* |
| 9086 | * Push mapping. |
| 9087 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9088 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9089 | ns, ns, depth) == NULL) |
| 9090 | goto internal_error; |
| 9091 | } |
| 9092 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9093 | /* No break on purpose. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9094 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9095 | /* No namespace, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9096 | if (cur->ns == NULL) |
| 9097 | goto ns_end; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9098 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9099 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9100 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 9101 | destParent) == -1) |
| 9102 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9103 | parnsdone = 1; |
| 9104 | } |
| 9105 | /* |
| 9106 | * Adopt ns-references. |
| 9107 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9108 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9109 | /* |
| 9110 | * Search for a mapping. |
| 9111 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9112 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9113 | if ((mi->shadowDepth == -1) && |
| 9114 | (cur->ns == mi->oldNs)) { |
| 9115 | |
| 9116 | cur->ns = mi->newNs; |
| 9117 | goto ns_end; |
| 9118 | } |
| 9119 | } |
| 9120 | } |
| 9121 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9122 | * No matching namespace in scope. We need a new one. |
| 9123 | */ |
| 9124 | if ((ctxt) && (ctxt->getNsForNodeFunc)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9125 | /* |
| 9126 | * User-defined behaviour. |
| 9127 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9128 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 9129 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9130 | /* |
| 9131 | * Insert mapping if ns is available; it's the users fault |
| 9132 | * if not. |
| 9133 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9134 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9135 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9136 | goto internal_error; |
| 9137 | cur->ns = ns; |
| 9138 | } else { |
| 9139 | /* |
| 9140 | * Aquire a normalized ns-decl and add it to the map. |
| 9141 | */ |
| 9142 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9143 | /* ns-decls on curElem or on destDoc->oldNs */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9144 | destParent ? curElem : NULL, |
| 9145 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9146 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9147 | ancestorsOnly, |
| 9148 | /* ns-decls must be prefixed for attributes. */ |
| 9149 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 9150 | goto internal_error; |
| 9151 | cur->ns = ns; |
| 9152 | } |
| 9153 | ns_end: |
| 9154 | /* |
| 9155 | * Further node properties. |
| 9156 | * TODO: Is this all? |
| 9157 | */ |
| 9158 | XML_TREE_ADOPT_STR(cur->name) |
| 9159 | if (cur->type == XML_ELEMENT_NODE) { |
| 9160 | cur->psvi = NULL; |
| 9161 | cur->line = 0; |
| 9162 | cur->extra = 0; |
| 9163 | /* |
| 9164 | * Walk attributes. |
| 9165 | */ |
| 9166 | if (cur->properties != NULL) { |
| 9167 | /* |
| 9168 | * Process first attribute node. |
| 9169 | */ |
| 9170 | cur = (xmlNodePtr) cur->properties; |
| 9171 | continue; |
| 9172 | } |
| 9173 | } else { |
| 9174 | /* |
| 9175 | * Attributes. |
| 9176 | */ |
| 9177 | if ((sourceDoc != NULL) && |
| 9178 | (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID)) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9179 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9180 | xmlRemoveID(sourceDoc, (xmlAttrPtr) cur); |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9181 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9182 | ((xmlAttrPtr) cur)->atype = 0; |
| 9183 | ((xmlAttrPtr) cur)->psvi = NULL; |
| 9184 | } |
| 9185 | break; |
| 9186 | case XML_TEXT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9187 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9188 | /* |
| 9189 | * This puts the content in the dest dict, only if |
| 9190 | * it was previously in the source dict. |
| 9191 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9192 | XML_TREE_ADOPT_STR_2(cur->content) |
| 9193 | goto leave_node; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9194 | case XML_ENTITY_REF_NODE: |
| 9195 | /* |
| 9196 | * Remove reference to the entitity-node. |
| 9197 | */ |
| 9198 | cur->content = NULL; |
| 9199 | cur->children = NULL; |
| 9200 | cur->last = NULL; |
| 9201 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9202 | xmlEntityPtr ent; |
| 9203 | /* |
| 9204 | * Assign new entity-node if available. |
| 9205 | */ |
| 9206 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9207 | if (ent != NULL) { |
| 9208 | cur->content = ent->content; |
| 9209 | cur->children = (xmlNodePtr) ent; |
| 9210 | cur->last = (xmlNodePtr) ent; |
| 9211 | } |
| 9212 | } |
| 9213 | goto leave_node; |
| 9214 | case XML_PI_NODE: |
| 9215 | XML_TREE_ADOPT_STR(cur->name) |
| 9216 | XML_TREE_ADOPT_STR_2(cur->content) |
| 9217 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9218 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9219 | break; |
| 9220 | default: |
| 9221 | goto internal_error; |
| 9222 | } |
| 9223 | /* |
| 9224 | * Walk the tree. |
| 9225 | */ |
| 9226 | if (cur->children != NULL) { |
| 9227 | cur = cur->children; |
| 9228 | continue; |
| 9229 | } |
| 9230 | |
| 9231 | leave_node: |
| 9232 | if (cur == node) |
| 9233 | break; |
| 9234 | if ((cur->type == XML_ELEMENT_NODE) || |
| 9235 | (cur->type == XML_XINCLUDE_START) || |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9236 | (cur->type == XML_XINCLUDE_END)) |
| 9237 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9238 | /* |
| 9239 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 9240 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9241 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9242 | /* |
| 9243 | * Pop mappings. |
| 9244 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9245 | while ((nsMap->last != NULL) && |
| 9246 | (nsMap->last->depth >= depth)) |
| 9247 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9248 | XML_NSMAP_POP(nsMap, mi) |
| 9249 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9250 | /* |
| 9251 | * Unshadow. |
| 9252 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9253 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9254 | if (mi->shadowDepth >= depth) |
| 9255 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9256 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9257 | } |
| 9258 | depth--; |
| 9259 | } |
| 9260 | if (cur->next != NULL) |
| 9261 | cur = cur->next; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9262 | else if ((cur->type == XML_ATTRIBUTE_NODE) && |
| 9263 | (cur->parent->children != NULL)) |
| 9264 | { |
| 9265 | cur = cur->parent->children; |
| 9266 | } else { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9267 | cur = cur->parent; |
| 9268 | goto leave_node; |
| 9269 | } |
| 9270 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9271 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9272 | goto exit; |
| 9273 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9274 | internal_error: |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9275 | ret = -1; |
| 9276 | |
| 9277 | exit: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9278 | /* |
| 9279 | * Cleanup. |
| 9280 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9281 | if (nsMap != NULL) { |
| 9282 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 9283 | /* |
| 9284 | * Just cleanup the map but don't free. |
| 9285 | */ |
| 9286 | if (nsMap->first) { |
| 9287 | if (nsMap->pool) |
| 9288 | nsMap->last->next = nsMap->pool; |
| 9289 | nsMap->pool = nsMap->first; |
| 9290 | nsMap->first = NULL; |
| 9291 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9292 | } else |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9293 | xmlDOMWrapNsMapFree(nsMap); |
| 9294 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9295 | return(ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9296 | } |
| 9297 | |
| 9298 | /* |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9299 | * xmlDOMWrapCloneNode: |
| 9300 | * @ctxt: the optional context for custom processing |
| 9301 | * @sourceDoc: the optional sourceDoc |
| 9302 | * @node: the node to start with |
| 9303 | * @resNode: the clone of the given @node |
| 9304 | * @destDoc: the destination doc |
| 9305 | * @destParent: the optional new parent of @node in @destDoc |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 9306 | * @deep: descend into child if set |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9307 | * @options: option flags |
| 9308 | * |
| 9309 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 9310 | * 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] | 9311 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used. |
| 9312 | * This is the case when you don't know already where the cloned branch |
| 9313 | * will be added to. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9314 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9315 | * If @destParent is given, it ensures that the tree is namespace |
| 9316 | * wellformed by creating additional ns-decls where needed. |
| 9317 | * Note that, since prefixes of already existent ns-decls can be |
| 9318 | * shadowed by this process, it could break QNames in attribute |
| 9319 | * values or element content. |
| 9320 | * TODO: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9321 | * 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] | 9322 | * |
| 9323 | * Returns 0 if the operation succeeded, |
| 9324 | * 1 if a node of unsupported (or not yet supported) type was given, |
| 9325 | * -1 on API/internal errors. |
| 9326 | */ |
| 9327 | |
| 9328 | int |
| 9329 | xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt, |
| 9330 | xmlDocPtr sourceDoc, |
| 9331 | xmlNodePtr node, |
| 9332 | xmlNodePtr *resNode, |
| 9333 | xmlDocPtr destDoc, |
| 9334 | xmlNodePtr destParent, |
| 9335 | int deep, |
| 9336 | int options ATTRIBUTE_UNUSED) |
| 9337 | { |
| 9338 | int ret = 0; |
| 9339 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9340 | xmlNsMapPtr nsMap = NULL; |
| 9341 | xmlNsMapItemPtr mi; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9342 | xmlNsPtr ns; |
| 9343 | int depth = -1; |
| 9344 | /* int adoptStr = 1; */ |
| 9345 | /* gather @parent's ns-decls. */ |
| 9346 | int parnsdone = 0; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9347 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9348 | * @ancestorsOnly: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9349 | * TODO: @ancestorsOnly should be set per option. |
| 9350 | * |
| 9351 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9352 | int ancestorsOnly = 0; |
| 9353 | xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9354 | xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL; |
| 9355 | xmlDictPtr dict; /* The destination dict */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9356 | |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9357 | if ((node == NULL) || (resNode == NULL) || (destDoc == NULL)) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9358 | return(-1); |
| 9359 | /* |
| 9360 | * TODO: Initially we support only element-nodes. |
| 9361 | */ |
| 9362 | if (node->type != XML_ELEMENT_NODE) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9363 | return(1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9364 | /* |
| 9365 | * Check node->doc sanity. |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9366 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9367 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 9368 | (node->doc != sourceDoc)) { |
| 9369 | /* |
| 9370 | * Might be an XIncluded node. |
| 9371 | */ |
| 9372 | return (-1); |
| 9373 | } |
| 9374 | if (sourceDoc == NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9375 | sourceDoc = node->doc; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9376 | if (sourceDoc == NULL) |
| 9377 | return (-1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9378 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9379 | dict = destDoc->dict; |
| 9380 | /* |
| 9381 | * Reuse the namespace map of the context. |
| 9382 | */ |
| 9383 | if (ctxt) |
| 9384 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 9385 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9386 | *resNode = NULL; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9387 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9388 | cur = node; |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 9389 | if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) |
| 9390 | return(-1); |
| 9391 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9392 | while (cur != NULL) { |
| 9393 | if (cur->doc != sourceDoc) { |
| 9394 | /* |
| 9395 | * We'll assume XIncluded nodes if the doc differs. |
| 9396 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 9397 | * TODO: This here returns -1 in this case. |
| 9398 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9399 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9400 | } |
| 9401 | /* |
| 9402 | * Create a new node. |
| 9403 | */ |
| 9404 | switch (cur->type) { |
| 9405 | case XML_XINCLUDE_START: |
| 9406 | case XML_XINCLUDE_END: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9407 | /* |
| 9408 | * TODO: What to do with XInclude? |
| 9409 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9410 | goto internal_error; |
| 9411 | break; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9412 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9413 | case XML_TEXT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9414 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9415 | case XML_COMMENT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9416 | case XML_PI_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9417 | case XML_DOCUMENT_FRAG_NODE: |
| 9418 | case XML_ENTITY_REF_NODE: |
| 9419 | case XML_ENTITY_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9420 | /* |
| 9421 | * Nodes of xmlNode structure. |
| 9422 | */ |
| 9423 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 9424 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9425 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9426 | goto internal_error; |
| 9427 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9428 | memset(clone, 0, sizeof(xmlNode)); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9429 | /* |
| 9430 | * Set hierachical links. |
| 9431 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9432 | if (resultClone != NULL) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9433 | clone->parent = parentClone; |
| 9434 | if (prevClone) { |
| 9435 | prevClone->next = clone; |
| 9436 | clone->prev = prevClone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9437 | } else |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9438 | parentClone->children = clone; |
| 9439 | } else |
| 9440 | resultClone = clone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9441 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9442 | break; |
| 9443 | case XML_ATTRIBUTE_NODE: |
| 9444 | /* |
| 9445 | * Attributes (xmlAttr). |
| 9446 | */ |
| 9447 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr)); |
| 9448 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9449 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9450 | goto internal_error; |
| 9451 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9452 | memset(clone, 0, sizeof(xmlAttr)); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9453 | /* |
| 9454 | * Set hierachical links. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9455 | * TODO: Change this to add to the end of attributes. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9456 | */ |
| 9457 | if (resultClone != NULL) { |
| 9458 | clone->parent = parentClone; |
| 9459 | if (prevClone) { |
| 9460 | prevClone->next = clone; |
| 9461 | clone->prev = prevClone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9462 | } else |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9463 | parentClone->properties = (xmlAttrPtr) clone; |
| 9464 | } else |
| 9465 | resultClone = clone; |
| 9466 | break; |
| 9467 | default: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9468 | /* |
| 9469 | * TODO QUESTION: Any other nodes expected? |
| 9470 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9471 | goto internal_error; |
| 9472 | } |
| 9473 | |
| 9474 | clone->type = cur->type; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9475 | clone->doc = destDoc; |
| 9476 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9477 | /* |
| 9478 | * Clone the name of the node if any. |
| 9479 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9480 | if (cur->name == xmlStringText) |
| 9481 | clone->name = xmlStringText; |
| 9482 | else if (cur->name == xmlStringTextNoenc) |
| 9483 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9484 | * NOTE: Although xmlStringTextNoenc is never assigned to a node |
| 9485 | * in tree.c, it might be set in Libxslt via |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9486 | * "xsl:disable-output-escaping". |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9487 | */ |
| 9488 | clone->name = xmlStringTextNoenc; |
| 9489 | else if (cur->name == xmlStringComment) |
| 9490 | clone->name = xmlStringComment; |
| 9491 | else if (cur->name != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9492 | DICT_CONST_COPY(cur->name, clone->name); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9493 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9494 | |
| 9495 | switch (cur->type) { |
| 9496 | case XML_XINCLUDE_START: |
| 9497 | case XML_XINCLUDE_END: |
| 9498 | /* |
| 9499 | * TODO |
| 9500 | */ |
| 9501 | return (-1); |
| 9502 | case XML_ELEMENT_NODE: |
| 9503 | curElem = cur; |
| 9504 | depth++; |
| 9505 | /* |
| 9506 | * Namespace declarations. |
| 9507 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9508 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9509 | if (! parnsdone) { |
| 9510 | if (destParent && (ctxt == NULL)) { |
| 9511 | /* |
| 9512 | * Gather @parent's in-scope ns-decls. |
| 9513 | */ |
| 9514 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 9515 | destParent) == -1) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9516 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9517 | } |
| 9518 | parnsdone = 1; |
| 9519 | } |
| 9520 | /* |
| 9521 | * Clone namespace declarations. |
| 9522 | */ |
| 9523 | cloneNsDefSlot = &(clone->nsDef); |
| 9524 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 9525 | /* |
| 9526 | * Create a new xmlNs. |
| 9527 | */ |
| 9528 | cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 9529 | if (cloneNs == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9530 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): " |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9531 | "allocating namespace"); |
| 9532 | return(-1); |
| 9533 | } |
| 9534 | memset(cloneNs, 0, sizeof(xmlNs)); |
| 9535 | cloneNs->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9536 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9537 | if (ns->href != NULL) |
| 9538 | cloneNs->href = xmlStrdup(ns->href); |
| 9539 | if (ns->prefix != NULL) |
| 9540 | cloneNs->prefix = xmlStrdup(ns->prefix); |
| 9541 | |
| 9542 | *cloneNsDefSlot = cloneNs; |
| 9543 | cloneNsDefSlot = &(cloneNs->next); |
| 9544 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9545 | /* |
| 9546 | * Note that for custom handling of ns-references, |
| 9547 | * the ns-decls need not be stored in the ns-map, |
| 9548 | * since they won't be referenced by node->ns. |
| 9549 | */ |
| 9550 | if ((ctxt == NULL) || |
| 9551 | (ctxt->getNsForNodeFunc == NULL)) |
| 9552 | { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9553 | /* |
| 9554 | * Does it shadow any ns-decl? |
| 9555 | */ |
| 9556 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9557 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9558 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 9559 | (mi->shadowDepth == -1) && |
| 9560 | ((ns->prefix == mi->newNs->prefix) || |
| 9561 | xmlStrEqual(ns->prefix, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9562 | mi->newNs->prefix))) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9563 | /* |
| 9564 | * Mark as shadowed at the current |
| 9565 | * depth. |
| 9566 | */ |
| 9567 | mi->shadowDepth = depth; |
| 9568 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9569 | } |
| 9570 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9571 | /* |
| 9572 | * Push mapping. |
| 9573 | */ |
| 9574 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
| 9575 | ns, cloneNs, depth) == NULL) |
| 9576 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9577 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9578 | } |
| 9579 | } |
| 9580 | /* cur->ns will be processed further down. */ |
| 9581 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9582 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9583 | /* IDs will be processed further down. */ |
| 9584 | /* cur->ns will be processed further down. */ |
| 9585 | break; |
| 9586 | case XML_TEXT_NODE: |
| 9587 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9588 | /* |
| 9589 | * Note that this will also cover the values of attributes. |
| 9590 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9591 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9592 | goto leave_node; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9593 | case XML_ENTITY_NODE: |
| 9594 | /* TODO: What to do here? */ |
| 9595 | goto leave_node; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9596 | case XML_ENTITY_REF_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9597 | if (sourceDoc != destDoc) { |
| 9598 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9599 | xmlEntityPtr ent; |
| 9600 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9601 | * Different doc: Assign new entity-node if available. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9602 | */ |
| 9603 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9604 | if (ent != NULL) { |
| 9605 | clone->content = ent->content; |
| 9606 | clone->children = (xmlNodePtr) ent; |
| 9607 | clone->last = (xmlNodePtr) ent; |
| 9608 | } |
| 9609 | } |
| 9610 | } else { |
| 9611 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9612 | * Same doc: Use the current node's entity declaration |
| 9613 | * and value. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9614 | */ |
| 9615 | clone->content = cur->content; |
| 9616 | clone->children = cur->children; |
| 9617 | clone->last = cur->last; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9618 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9619 | goto leave_node; |
| 9620 | case XML_PI_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9621 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9622 | goto leave_node; |
| 9623 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9624 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9625 | goto leave_node; |
| 9626 | default: |
| 9627 | goto internal_error; |
| 9628 | } |
| 9629 | |
| 9630 | if (cur->ns == NULL) |
| 9631 | goto end_ns_reference; |
| 9632 | |
| 9633 | /* handle_ns_reference: */ |
| 9634 | /* |
| 9635 | ** The following will take care of references to ns-decls ******** |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9636 | ** and is intended only for element- and attribute-nodes. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9637 | ** |
| 9638 | */ |
| 9639 | if (! parnsdone) { |
| 9640 | if (destParent && (ctxt == NULL)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9641 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9642 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9643 | } |
| 9644 | parnsdone = 1; |
| 9645 | } |
| 9646 | /* |
| 9647 | * Adopt ns-references. |
| 9648 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9649 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9650 | /* |
| 9651 | * Search for a mapping. |
| 9652 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9653 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9654 | if ((mi->shadowDepth == -1) && |
| 9655 | (cur->ns == mi->oldNs)) { |
| 9656 | /* |
| 9657 | * This is the nice case: a mapping was found. |
| 9658 | */ |
| 9659 | clone->ns = mi->newNs; |
| 9660 | goto end_ns_reference; |
| 9661 | } |
| 9662 | } |
| 9663 | } |
| 9664 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9665 | * No matching namespace in scope. We need a new one. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9666 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9667 | if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9668 | /* |
| 9669 | * User-defined behaviour. |
| 9670 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9671 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 9672 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9673 | /* |
| 9674 | * Add user's mapping. |
| 9675 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9676 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9677 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
| 9678 | goto internal_error; |
| 9679 | clone->ns = ns; |
| 9680 | } else { |
| 9681 | /* |
| 9682 | * Aquire a normalized ns-decl and add it to the map. |
| 9683 | */ |
| 9684 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9685 | /* ns-decls on curElem or on destDoc->oldNs */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9686 | destParent ? curElem : NULL, |
| 9687 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9688 | &nsMap, depth, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9689 | /* if we need to search only in the ancestor-axis */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9690 | ancestorsOnly, |
| 9691 | /* ns-decls must be prefixed for attributes. */ |
| 9692 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 9693 | goto internal_error; |
| 9694 | clone->ns = ns; |
| 9695 | } |
| 9696 | |
| 9697 | end_ns_reference: |
| 9698 | |
| 9699 | /* |
| 9700 | * Some post-processing. |
| 9701 | * |
| 9702 | * Handle ID attributes. |
| 9703 | */ |
| 9704 | if ((clone->type == XML_ATTRIBUTE_NODE) && |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9705 | (clone->parent != NULL)) |
| 9706 | { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9707 | if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9708 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9709 | xmlChar *idVal; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9710 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9711 | idVal = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 9712 | if (idVal != NULL) { |
| 9713 | if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) { |
| 9714 | /* TODO: error message. */ |
| 9715 | xmlFree(idVal); |
| 9716 | goto internal_error; |
| 9717 | } |
| 9718 | xmlFree(idVal); |
| 9719 | } |
| 9720 | } |
| 9721 | } |
| 9722 | /* |
| 9723 | ** |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9724 | ** The following will traverse the tree ************************** |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9725 | ** |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9726 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9727 | * Walk the element's attributes before descending into child-nodes. |
| 9728 | */ |
| 9729 | if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) { |
| 9730 | prevClone = NULL; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9731 | parentClone = clone; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9732 | cur = (xmlNodePtr) cur->properties; |
| 9733 | continue; |
| 9734 | } |
| 9735 | into_content: |
| 9736 | /* |
| 9737 | * Descend into child-nodes. |
| 9738 | */ |
| 9739 | if (cur->children != NULL) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9740 | if (deep || (cur->type == XML_ATTRIBUTE_NODE)) { |
| 9741 | prevClone = NULL; |
| 9742 | parentClone = clone; |
| 9743 | cur = cur->children; |
| 9744 | continue; |
| 9745 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9746 | } |
| 9747 | |
| 9748 | leave_node: |
| 9749 | /* |
| 9750 | * At this point we are done with the node, its content |
| 9751 | * and an element-nodes's attribute-nodes. |
| 9752 | */ |
| 9753 | if (cur == node) |
| 9754 | break; |
| 9755 | if ((cur->type == XML_ELEMENT_NODE) || |
| 9756 | (cur->type == XML_XINCLUDE_START) || |
| 9757 | (cur->type == XML_XINCLUDE_END)) { |
| 9758 | /* |
| 9759 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 9760 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9761 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9762 | /* |
| 9763 | * Pop mappings. |
| 9764 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9765 | while ((nsMap->last != NULL) && |
| 9766 | (nsMap->last->depth >= depth)) |
| 9767 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9768 | XML_NSMAP_POP(nsMap, mi) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9769 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9770 | /* |
| 9771 | * Unshadow. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9772 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9773 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9774 | if (mi->shadowDepth >= depth) |
| 9775 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9776 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9777 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9778 | depth--; |
| 9779 | } |
| 9780 | if (cur->next != NULL) { |
| 9781 | prevClone = clone; |
| 9782 | cur = cur->next; |
| 9783 | } else if (cur->type != XML_ATTRIBUTE_NODE) { |
| 9784 | /* |
| 9785 | * Set clone->last. |
| 9786 | */ |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9787 | if (clone->parent != NULL) |
| 9788 | clone->parent->last = clone; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9789 | clone = clone->parent; |
Daniel Veillard | 75d1309 | 2013-09-11 15:11:27 +0800 | [diff] [blame] | 9790 | if (clone != NULL) |
| 9791 | parentClone = clone->parent; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9792 | /* |
| 9793 | * Process parent --> next; |
| 9794 | */ |
| 9795 | cur = cur->parent; |
| 9796 | goto leave_node; |
| 9797 | } else { |
| 9798 | /* This is for attributes only. */ |
| 9799 | clone = clone->parent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9800 | parentClone = clone->parent; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9801 | /* |
| 9802 | * Process parent-element --> children. |
| 9803 | */ |
| 9804 | cur = cur->parent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9805 | goto into_content; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9806 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9807 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9808 | goto exit; |
| 9809 | |
| 9810 | internal_error: |
| 9811 | ret = -1; |
| 9812 | |
| 9813 | exit: |
| 9814 | /* |
| 9815 | * Cleanup. |
| 9816 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9817 | if (nsMap != NULL) { |
| 9818 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 9819 | /* |
| 9820 | * Just cleanup the map but don't free. |
| 9821 | */ |
| 9822 | if (nsMap->first) { |
| 9823 | if (nsMap->pool) |
| 9824 | nsMap->last->next = nsMap->pool; |
| 9825 | nsMap->pool = nsMap->first; |
| 9826 | nsMap->first = NULL; |
| 9827 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9828 | } else |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9829 | xmlDOMWrapNsMapFree(nsMap); |
| 9830 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9831 | /* |
| 9832 | * TODO: Should we try a cleanup of the cloned node in case of a |
| 9833 | * fatal error? |
| 9834 | */ |
| 9835 | *resNode = resultClone; |
| 9836 | return (ret); |
| 9837 | } |
| 9838 | |
| 9839 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9840 | * xmlDOMWrapAdoptAttr: |
| 9841 | * @ctxt: the optional context for custom processing |
| 9842 | * @sourceDoc: the optional source document of attr |
| 9843 | * @attr: the attribute-node to be adopted |
| 9844 | * @destDoc: the destination doc for adoption |
| 9845 | * @destParent: the optional new parent of @attr in @destDoc |
| 9846 | * @options: option flags |
| 9847 | * |
| 9848 | * @attr is adopted by @destDoc. |
| 9849 | * Ensures that ns-references point to @destDoc: either to |
| 9850 | * elements->nsDef entries if @destParent is given, or to |
| 9851 | * @destDoc->oldNs otherwise. |
| 9852 | * |
| 9853 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 9854 | */ |
| 9855 | static int |
| 9856 | xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt, |
| 9857 | xmlDocPtr sourceDoc, |
| 9858 | xmlAttrPtr attr, |
| 9859 | xmlDocPtr destDoc, |
| 9860 | xmlNodePtr destParent, |
| 9861 | int options ATTRIBUTE_UNUSED) |
| 9862 | { |
| 9863 | xmlNodePtr cur; |
| 9864 | int adoptStr = 1; |
| 9865 | |
| 9866 | if ((attr == NULL) || (destDoc == NULL)) |
| 9867 | return (-1); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9868 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9869 | attr->doc = destDoc; |
| 9870 | if (attr->ns != NULL) { |
| 9871 | xmlNsPtr ns = NULL; |
| 9872 | |
| 9873 | if (ctxt != NULL) { |
| 9874 | /* TODO: User defined. */ |
| 9875 | } |
| 9876 | /* XML Namespace. */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9877 | if (IS_STR_XML(attr->ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9878 | ns = xmlTreeEnsureXMLDecl(destDoc); |
| 9879 | } else if (destParent == NULL) { |
| 9880 | /* |
| 9881 | * Store in @destDoc->oldNs. |
| 9882 | */ |
| 9883 | ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix); |
| 9884 | } else { |
| 9885 | /* |
| 9886 | * Declare on @destParent. |
| 9887 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9888 | if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9889 | &ns, 1) == -1) |
| 9890 | goto internal_error; |
| 9891 | if (ns == NULL) { |
| 9892 | ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent, |
| 9893 | attr->ns->href, attr->ns->prefix, 1); |
| 9894 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9895 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9896 | if (ns == NULL) |
| 9897 | goto internal_error; |
| 9898 | attr->ns = ns; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9899 | } |
| 9900 | |
| 9901 | XML_TREE_ADOPT_STR(attr->name); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9902 | attr->atype = 0; |
| 9903 | attr->psvi = NULL; |
| 9904 | /* |
| 9905 | * Walk content. |
| 9906 | */ |
| 9907 | if (attr->children == NULL) |
| 9908 | return (0); |
| 9909 | cur = attr->children; |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 9910 | if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) |
| 9911 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9912 | while (cur != NULL) { |
| 9913 | cur->doc = destDoc; |
| 9914 | switch (cur->type) { |
| 9915 | case XML_TEXT_NODE: |
| 9916 | case XML_CDATA_SECTION_NODE: |
| 9917 | XML_TREE_ADOPT_STR_2(cur->content) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9918 | break; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9919 | case XML_ENTITY_REF_NODE: |
| 9920 | /* |
| 9921 | * Remove reference to the entitity-node. |
| 9922 | */ |
| 9923 | cur->content = NULL; |
| 9924 | cur->children = NULL; |
| 9925 | cur->last = NULL; |
| 9926 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9927 | xmlEntityPtr ent; |
| 9928 | /* |
| 9929 | * Assign new entity-node if available. |
| 9930 | */ |
| 9931 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9932 | if (ent != NULL) { |
| 9933 | cur->content = ent->content; |
| 9934 | cur->children = (xmlNodePtr) ent; |
| 9935 | cur->last = (xmlNodePtr) ent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9936 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9937 | } |
| 9938 | break; |
| 9939 | default: |
| 9940 | break; |
| 9941 | } |
| 9942 | if (cur->children != NULL) { |
| 9943 | cur = cur->children; |
| 9944 | continue; |
| 9945 | } |
| 9946 | next_sibling: |
| 9947 | if (cur == (xmlNodePtr) attr) |
| 9948 | break; |
| 9949 | if (cur->next != NULL) |
| 9950 | cur = cur->next; |
| 9951 | else { |
| 9952 | cur = cur->parent; |
| 9953 | goto next_sibling; |
| 9954 | } |
| 9955 | } |
| 9956 | return (0); |
| 9957 | internal_error: |
| 9958 | return (-1); |
| 9959 | } |
| 9960 | |
| 9961 | /* |
| 9962 | * xmlDOMWrapAdoptNode: |
| 9963 | * @ctxt: the optional context for custom processing |
| 9964 | * @sourceDoc: the optional sourceDoc |
| 9965 | * @node: the node to start with |
| 9966 | * @destDoc: the destination doc |
Kasimier T. Buchcik | 4d9c948 | 2005-06-27 15:04:46 +0000 | [diff] [blame] | 9967 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9968 | * @options: option flags |
| 9969 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9970 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 9971 | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
| 9972 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used |
Daniel Veillard | 7e35abe | 2014-03-28 22:55:31 +0800 | [diff] [blame] | 9973 | * This is the case when you have an unlinked node and just want to move it |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9974 | * to the context of |
| 9975 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9976 | * If @destParent is given, it ensures that the tree is namespace |
| 9977 | * wellformed by creating additional ns-decls where needed. |
| 9978 | * Note that, since prefixes of already existent ns-decls can be |
| 9979 | * shadowed by this process, it could break QNames in attribute |
| 9980 | * values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9981 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9982 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9983 | * Returns 0 if the operation succeeded, |
| 9984 | * 1 if a node of unsupported type was given, |
| 9985 | * 2 if a node of not yet supported type was given and |
| 9986 | * -1 on API/internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9987 | */ |
| 9988 | int |
| 9989 | xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt, |
| 9990 | xmlDocPtr sourceDoc, |
| 9991 | xmlNodePtr node, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9992 | xmlDocPtr destDoc, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9993 | xmlNodePtr destParent, |
| 9994 | int options) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9995 | { |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 9996 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || |
| 9997 | (destDoc == NULL) || |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9998 | ((destParent != NULL) && (destParent->doc != destDoc))) |
| 9999 | return(-1); |
| 10000 | /* |
| 10001 | * Check node->doc sanity. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 10002 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10003 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 10004 | (node->doc != sourceDoc)) { |
| 10005 | /* |
| 10006 | * Might be an XIncluded node. |
| 10007 | */ |
| 10008 | return (-1); |
| 10009 | } |
| 10010 | if (sourceDoc == NULL) |
| 10011 | sourceDoc = node->doc; |
| 10012 | if (sourceDoc == destDoc) |
| 10013 | return (-1); |
| 10014 | switch (node->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 10015 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10016 | case XML_ATTRIBUTE_NODE: |
| 10017 | case XML_TEXT_NODE: |
| 10018 | case XML_CDATA_SECTION_NODE: |
| 10019 | case XML_ENTITY_REF_NODE: |
| 10020 | case XML_PI_NODE: |
| 10021 | case XML_COMMENT_NODE: |
| 10022 | break; |
| 10023 | case XML_DOCUMENT_FRAG_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 10024 | /* TODO: Support document-fragment-nodes. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10025 | return (2); |
| 10026 | default: |
| 10027 | return (1); |
| 10028 | } |
| 10029 | /* |
| 10030 | * Unlink only if @node was not already added to @destParent. |
| 10031 | */ |
| 10032 | if ((node->parent != NULL) && (destParent != node->parent)) |
| 10033 | xmlUnlinkNode(node); |
| 10034 | |
| 10035 | if (node->type == XML_ELEMENT_NODE) { |
| 10036 | return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node, |
| 10037 | destDoc, destParent, options)); |
| 10038 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
| 10039 | return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc, |
| 10040 | (xmlAttrPtr) node, destDoc, destParent, options)); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 10041 | } else { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10042 | xmlNodePtr cur = node; |
| 10043 | int adoptStr = 1; |
| 10044 | |
| 10045 | cur->doc = destDoc; |
| 10046 | /* |
| 10047 | * Optimize string adoption. |
| 10048 | */ |
| 10049 | if ((sourceDoc != NULL) && |
| 10050 | (sourceDoc->dict == destDoc->dict)) |
| 10051 | adoptStr = 0; |
| 10052 | switch (node->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 10053 | case XML_TEXT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10054 | case XML_CDATA_SECTION_NODE: |
| 10055 | XML_TREE_ADOPT_STR_2(node->content) |
| 10056 | break; |
| 10057 | case XML_ENTITY_REF_NODE: |
| 10058 | /* |
| 10059 | * Remove reference to the entitity-node. |
| 10060 | */ |
| 10061 | node->content = NULL; |
| 10062 | node->children = NULL; |
| 10063 | node->last = NULL; |
| 10064 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 10065 | xmlEntityPtr ent; |
| 10066 | /* |
| 10067 | * Assign new entity-node if available. |
| 10068 | */ |
| 10069 | ent = xmlGetDocEntity(destDoc, node->name); |
| 10070 | if (ent != NULL) { |
| 10071 | node->content = ent->content; |
| 10072 | node->children = (xmlNodePtr) ent; |
| 10073 | node->last = (xmlNodePtr) ent; |
| 10074 | } |
| 10075 | } |
| 10076 | XML_TREE_ADOPT_STR(node->name) |
| 10077 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 10078 | case XML_PI_NODE: { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10079 | XML_TREE_ADOPT_STR(node->name) |
| 10080 | XML_TREE_ADOPT_STR_2(node->content) |
| 10081 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 10082 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10083 | default: |
| 10084 | break; |
| 10085 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 10086 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10087 | return (0); |
| 10088 | } |
| 10089 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 10090 | #define bottom_tree |
| 10091 | #include "elfgcchack.h" |