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 | |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 58 | static xmlChar* xmlGetPropNodeValueInternal(const xmlAttr *prop); |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 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 |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 163 | xmlGetEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) { |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 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 |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 184 | xmlGetParameterEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) { |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 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 |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 919 | xmlGetIntSubset(const xmlDoc *doc) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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 | * |
Jan Pokorný | bb654fe | 2016-04-13 16:56:07 +0200 | [diff] [blame] | 1047 | * Free a string if it is not owned by the "dict" dictionary in the |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 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 | * |
Jan Pokorný | bb654fe | 2016-04-13 16:56:07 +0200 | [diff] [blame] | 1060 | * Copy a string using a "dict" dictionary in the current scope, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 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 | * |
Jan Pokorný | bb654fe | 2016-04-13 16:56:07 +0200 | [diff] [blame] | 1077 | * Copy a string using a "dict" dictionary in the current scope, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 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 |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 1269 | xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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) { |
Philip Withnall | 5794104 | 2014-10-26 18:08:04 +0000 | [diff] [blame] | 1454 | ret = node; |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 1455 | } else { |
Philip Withnall | 5794104 | 2014-10-26 18:08:04 +0000 | [diff] [blame] | 1456 | xmlAddNextSibling(last, node); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 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 |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 1477 | xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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) { |
Philip Withnall | 5794104 | 2014-10-26 18:08:04 +0000 | [diff] [blame] | 1642 | ret = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1643 | } else { |
Philip Withnall | 5794104 | 2014-10-26 18:08:04 +0000 | [diff] [blame] | 1644 | 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 * |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 1665 | xmlNodeListGetString(xmlDocPtr doc, const xmlNode *list, int inLine) |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1666 | { |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 1667 | const xmlNode *node = list; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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 * |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 1755 | xmlNodeListGetRawString(const xmlDoc *doc, const xmlNode *list, int inLine) |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1756 | { |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 1757 | const xmlNode *node = list; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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"); |
Jan Pokorný | bb654fe | 2016-04-13 16:56:07 +0200 | [diff] [blame] | 2273 | /* we can't check here that name comes from the doc dictionary */ |
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 { |
Jan Pokorný | bb654fe | 2016-04-13 16:56:07 +0200 | [diff] [blame] | 2353 | /* if name don't come from the doc dictionary free it here */ |
Daniel Veillard | 8f6c2b1 | 2008-04-03 11:17:21 +0000 | [diff] [blame] | 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 |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 2586 | xmlNewReference(const xmlDoc *doc, const xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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 | |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 2604 | cur->doc = (xmlDoc *)doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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 |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 2642 | xmlNewDocText(const xmlDoc *doc, const xmlChar *content) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2643 | xmlNodePtr cur; |
| 2644 | |
| 2645 | cur = xmlNewText(content); |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 2646 | if (cur != NULL) cur->doc = (xmlDoc *)doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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) { |
Nick Wellnhofer | f54d6a9 | 2014-12-19 00:08:35 +0100 | [diff] [blame] | 2802 | if (prop->atype == XML_ATTRIBUTE_ID) { |
| 2803 | xmlRemoveID(tree->doc, prop); |
| 2804 | } |
| 2805 | |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2806 | prop->doc = doc; |
| 2807 | xmlSetListDoc(prop->children, doc); |
Nick Wellnhofer | f54d6a9 | 2014-12-19 00:08:35 +0100 | [diff] [blame] | 2808 | |
Nick Wellnhofer | 220a7ba | 2014-12-23 21:28:37 +0100 | [diff] [blame] | 2809 | /* |
| 2810 | * TODO: ID attributes should be also added to the new |
| 2811 | * document, but this breaks things like xmlReplaceNode. |
| 2812 | * The underlying problem is that xmlRemoveID is only called |
| 2813 | * if a node is destroyed, not if it's unlinked. |
| 2814 | */ |
| 2815 | #if 0 |
Nick Wellnhofer | f54d6a9 | 2014-12-19 00:08:35 +0100 | [diff] [blame] | 2816 | if (xmlIsID(doc, tree, prop)) { |
| 2817 | xmlChar *idVal = xmlNodeListGetString(doc, prop->children, |
| 2818 | 1); |
| 2819 | xmlAddID(NULL, doc, idVal, prop); |
| 2820 | } |
Nick Wellnhofer | 220a7ba | 2014-12-23 21:28:37 +0100 | [diff] [blame] | 2821 | #endif |
Nick Wellnhofer | f54d6a9 | 2014-12-19 00:08:35 +0100 | [diff] [blame] | 2822 | |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2823 | prop = prop->next; |
| 2824 | } |
Daniel Veillard | 19e96c3 | 2001-07-09 10:32:59 +0000 | [diff] [blame] | 2825 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2826 | if (tree->children != NULL) |
| 2827 | xmlSetListDoc(tree->children, doc); |
| 2828 | tree->doc = doc; |
| 2829 | } |
| 2830 | } |
| 2831 | |
| 2832 | /** |
| 2833 | * xmlSetListDoc: |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 2834 | * @list: the first element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2835 | * @doc: the document |
| 2836 | * |
| 2837 | * update all nodes in the list to point to the right document |
| 2838 | */ |
| 2839 | void |
| 2840 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 2841 | xmlNodePtr cur; |
| 2842 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 2843 | if ((list == NULL) || (list->type == XML_NAMESPACE_DECL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2844 | return; |
| 2845 | cur = list; |
| 2846 | while (cur != NULL) { |
| 2847 | if (cur->doc != doc) |
| 2848 | xmlSetTreeDoc(cur, doc); |
| 2849 | cur = cur->next; |
| 2850 | } |
| 2851 | } |
| 2852 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 2853 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2854 | /** |
| 2855 | * xmlNewChild: |
| 2856 | * @parent: the parent node |
| 2857 | * @ns: a namespace if any |
| 2858 | * @name: the name of the child |
| 2859 | * @content: the XML content of the child if any. |
| 2860 | * |
| 2861 | * 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] | 2862 | * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly |
| 2863 | * created element inherits the namespace of @parent. If @content is non NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2864 | * 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] | 2865 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 2866 | * references. XML special chars must be escaped first by using |
| 2867 | * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2868 | * |
| 2869 | * Returns a pointer to the new node object. |
| 2870 | */ |
| 2871 | xmlNodePtr |
| 2872 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
| 2873 | const xmlChar *name, const xmlChar *content) { |
| 2874 | xmlNodePtr cur, prev; |
| 2875 | |
| 2876 | if (parent == NULL) { |
| 2877 | #ifdef DEBUG_TREE |
| 2878 | xmlGenericError(xmlGenericErrorContext, |
| 2879 | "xmlNewChild : parent == NULL\n"); |
| 2880 | #endif |
| 2881 | return(NULL); |
| 2882 | } |
| 2883 | |
| 2884 | if (name == NULL) { |
| 2885 | #ifdef DEBUG_TREE |
| 2886 | xmlGenericError(xmlGenericErrorContext, |
| 2887 | "xmlNewChild : name == NULL\n"); |
| 2888 | #endif |
| 2889 | return(NULL); |
| 2890 | } |
| 2891 | |
| 2892 | /* |
| 2893 | * Allocate a new node |
| 2894 | */ |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2895 | if (parent->type == XML_ELEMENT_NODE) { |
| 2896 | if (ns == NULL) |
| 2897 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
| 2898 | else |
| 2899 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
| 2900 | } else if ((parent->type == XML_DOCUMENT_NODE) || |
| 2901 | (parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 2902 | if (ns == NULL) |
| 2903 | cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content); |
| 2904 | else |
| 2905 | cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content); |
Daniel Veillard | 7e3f140 | 2002-10-28 18:52:57 +0000 | [diff] [blame] | 2906 | } else if (parent->type == XML_DOCUMENT_FRAG_NODE) { |
| 2907 | cur = xmlNewDocNode( parent->doc, ns, name, content); |
Daniel Veillard | 36eea2d | 2002-02-04 00:17:01 +0000 | [diff] [blame] | 2908 | } else { |
| 2909 | return(NULL); |
| 2910 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2911 | if (cur == NULL) return(NULL); |
| 2912 | |
| 2913 | /* |
| 2914 | * add the new element at the end of the children list. |
| 2915 | */ |
| 2916 | cur->type = XML_ELEMENT_NODE; |
| 2917 | cur->parent = parent; |
| 2918 | cur->doc = parent->doc; |
| 2919 | if (parent->children == NULL) { |
| 2920 | parent->children = cur; |
| 2921 | parent->last = cur; |
| 2922 | } else { |
| 2923 | prev = parent->last; |
| 2924 | prev->next = cur; |
| 2925 | cur->prev = prev; |
| 2926 | parent->last = cur; |
| 2927 | } |
| 2928 | |
| 2929 | return(cur); |
| 2930 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2931 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2932 | |
| 2933 | /** |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2934 | * xmlAddPropSibling: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2935 | * @prev: the attribute to which @prop is added after |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2936 | * @cur: the base attribute passed to calling function |
| 2937 | * @prop: the new attribute |
| 2938 | * |
| 2939 | * Add a new attribute after @prev using @cur as base attribute. |
| 2940 | * When inserting before @cur, @prev is passed as @cur->prev. |
| 2941 | * When inserting after @cur, @prev is passed as @cur. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 2942 | * 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] | 2943 | * |
| 2944 | * Returns the attribute being inserted or NULL in case of error. |
| 2945 | */ |
| 2946 | static xmlNodePtr |
| 2947 | xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) { |
| 2948 | xmlAttrPtr attr; |
| 2949 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 2950 | if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) || |
| 2951 | (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) || |
| 2952 | ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE))) |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 2953 | return(NULL); |
| 2954 | |
| 2955 | /* check if an attribute with the same name exists */ |
| 2956 | if (prop->ns == NULL) |
| 2957 | attr = xmlHasNsProp(cur->parent, prop->name, NULL); |
| 2958 | else |
| 2959 | attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href); |
| 2960 | |
| 2961 | if (prop->doc != cur->doc) { |
| 2962 | xmlSetTreeDoc(prop, cur->doc); |
| 2963 | } |
| 2964 | prop->parent = cur->parent; |
| 2965 | prop->prev = prev; |
| 2966 | if (prev != NULL) { |
| 2967 | prop->next = prev->next; |
| 2968 | prev->next = prop; |
| 2969 | if (prop->next) |
| 2970 | prop->next->prev = prop; |
| 2971 | } else { |
| 2972 | prop->next = cur; |
| 2973 | cur->prev = prop; |
| 2974 | } |
| 2975 | if (prop->prev == NULL && prop->parent != NULL) |
| 2976 | prop->parent->properties = (xmlAttrPtr) prop; |
| 2977 | if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) { |
| 2978 | /* different instance, destroy it (attributes must be unique) */ |
| 2979 | xmlRemoveProp((xmlAttrPtr) attr); |
| 2980 | } |
| 2981 | return prop; |
| 2982 | } |
| 2983 | |
| 2984 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2985 | * xmlAddNextSibling: |
| 2986 | * @cur: the child node |
| 2987 | * @elem: the new node |
| 2988 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2989 | * Add a new node @elem as the next sibling of @cur |
| 2990 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2991 | * first unlinked from its existing context. |
| 2992 | * As a result of text merging @elem may be freed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2993 | * 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] | 2994 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2995 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 2996 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2997 | */ |
| 2998 | xmlNodePtr |
| 2999 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3000 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3001 | #ifdef DEBUG_TREE |
| 3002 | xmlGenericError(xmlGenericErrorContext, |
| 3003 | "xmlAddNextSibling : cur == NULL\n"); |
| 3004 | #endif |
| 3005 | return(NULL); |
| 3006 | } |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3007 | if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3008 | #ifdef DEBUG_TREE |
| 3009 | xmlGenericError(xmlGenericErrorContext, |
| 3010 | "xmlAddNextSibling : elem == NULL\n"); |
| 3011 | #endif |
| 3012 | return(NULL); |
| 3013 | } |
| 3014 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3015 | if (cur == elem) { |
| 3016 | #ifdef DEBUG_TREE |
| 3017 | xmlGenericError(xmlGenericErrorContext, |
| 3018 | "xmlAddNextSibling : cur == elem\n"); |
| 3019 | #endif |
| 3020 | return(NULL); |
| 3021 | } |
| 3022 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3023 | xmlUnlinkNode(elem); |
| 3024 | |
| 3025 | if (elem->type == XML_TEXT_NODE) { |
| 3026 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3027 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3028 | xmlFreeNode(elem); |
| 3029 | return(cur); |
| 3030 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 3031 | if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) && |
| 3032 | (cur->name == cur->next->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3033 | xmlChar *tmp; |
| 3034 | |
| 3035 | tmp = xmlStrdup(elem->content); |
| 3036 | tmp = xmlStrcat(tmp, cur->next->content); |
| 3037 | xmlNodeSetContent(cur->next, tmp); |
| 3038 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3039 | xmlFreeNode(elem); |
| 3040 | return(cur->next); |
| 3041 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3042 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3043 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | if (elem->doc != cur->doc) { |
| 3047 | xmlSetTreeDoc(elem, cur->doc); |
| 3048 | } |
| 3049 | elem->parent = cur->parent; |
| 3050 | elem->prev = cur; |
| 3051 | elem->next = cur->next; |
| 3052 | cur->next = elem; |
| 3053 | if (elem->next != NULL) |
| 3054 | elem->next->prev = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3055 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3056 | elem->parent->last = elem; |
| 3057 | return(elem); |
| 3058 | } |
| 3059 | |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 3060 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \ |
Nicolas Le Cam | 41586ca | 2013-06-17 13:01:33 +0200 | [diff] [blame] | 3061 | defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3062 | /** |
| 3063 | * xmlAddPrevSibling: |
| 3064 | * @cur: the child node |
| 3065 | * @elem: the new node |
| 3066 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3067 | * Add a new node @elem as the previous sibling of @cur |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3068 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3069 | * If the new node was already inserted in a document it is |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3070 | * first unlinked from its existing context. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3071 | * 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] | 3072 | * If there is an attribute with equal name, it is first destroyed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3073 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3074 | * Returns the new node or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3075 | */ |
| 3076 | xmlNodePtr |
| 3077 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3078 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3079 | #ifdef DEBUG_TREE |
| 3080 | xmlGenericError(xmlGenericErrorContext, |
| 3081 | "xmlAddPrevSibling : cur == NULL\n"); |
| 3082 | #endif |
| 3083 | return(NULL); |
| 3084 | } |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3085 | if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3086 | #ifdef DEBUG_TREE |
| 3087 | xmlGenericError(xmlGenericErrorContext, |
| 3088 | "xmlAddPrevSibling : elem == NULL\n"); |
| 3089 | #endif |
| 3090 | return(NULL); |
| 3091 | } |
| 3092 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3093 | if (cur == elem) { |
| 3094 | #ifdef DEBUG_TREE |
| 3095 | xmlGenericError(xmlGenericErrorContext, |
| 3096 | "xmlAddPrevSibling : cur == elem\n"); |
| 3097 | #endif |
| 3098 | return(NULL); |
| 3099 | } |
| 3100 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3101 | xmlUnlinkNode(elem); |
| 3102 | |
| 3103 | if (elem->type == XML_TEXT_NODE) { |
| 3104 | if (cur->type == XML_TEXT_NODE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3105 | xmlChar *tmp; |
| 3106 | |
| 3107 | tmp = xmlStrdup(elem->content); |
| 3108 | tmp = xmlStrcat(tmp, cur->content); |
| 3109 | xmlNodeSetContent(cur, tmp); |
| 3110 | xmlFree(tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3111 | xmlFreeNode(elem); |
| 3112 | return(cur); |
| 3113 | } |
Daniel Veillard | 9e1c72d | 2001-08-31 20:03:19 +0000 | [diff] [blame] | 3114 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) && |
| 3115 | (cur->name == cur->prev->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3116 | xmlNodeAddContent(cur->prev, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3117 | xmlFreeNode(elem); |
| 3118 | return(cur->prev); |
| 3119 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3120 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3121 | return xmlAddPropSibling(cur->prev, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | if (elem->doc != cur->doc) { |
| 3125 | xmlSetTreeDoc(elem, cur->doc); |
| 3126 | } |
| 3127 | elem->parent = cur->parent; |
| 3128 | elem->next = cur; |
| 3129 | elem->prev = cur->prev; |
| 3130 | cur->prev = elem; |
| 3131 | if (elem->prev != NULL) |
| 3132 | elem->prev->next = elem; |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3133 | if ((elem->parent != NULL) && (elem->parent->children == cur)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3134 | elem->parent->children = elem; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3135 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3136 | return(elem); |
| 3137 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3138 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3139 | |
| 3140 | /** |
| 3141 | * xmlAddSibling: |
| 3142 | * @cur: the child node |
| 3143 | * @elem: the new node |
| 3144 | * |
| 3145 | * Add a new element @elem to the list of siblings of @cur |
| 3146 | * merging adjacent TEXT nodes (@elem may be freed) |
| 3147 | * If the new element was already inserted in a document it is |
| 3148 | * first unlinked from its existing context. |
| 3149 | * |
| 3150 | * Returns the new element or NULL in case of error. |
| 3151 | */ |
| 3152 | xmlNodePtr |
| 3153 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 3154 | xmlNodePtr parent; |
| 3155 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3156 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3157 | #ifdef DEBUG_TREE |
| 3158 | xmlGenericError(xmlGenericErrorContext, |
| 3159 | "xmlAddSibling : cur == NULL\n"); |
| 3160 | #endif |
| 3161 | return(NULL); |
| 3162 | } |
| 3163 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3164 | if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3165 | #ifdef DEBUG_TREE |
| 3166 | xmlGenericError(xmlGenericErrorContext, |
| 3167 | "xmlAddSibling : elem == NULL\n"); |
| 3168 | #endif |
| 3169 | return(NULL); |
| 3170 | } |
| 3171 | |
Daniel Veillard | 43bc89c | 2009-03-23 19:32:04 +0000 | [diff] [blame] | 3172 | if (cur == elem) { |
| 3173 | #ifdef DEBUG_TREE |
| 3174 | xmlGenericError(xmlGenericErrorContext, |
| 3175 | "xmlAddSibling : cur == elem\n"); |
| 3176 | #endif |
| 3177 | return(NULL); |
| 3178 | } |
| 3179 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3180 | /* |
| 3181 | * Constant time is we can rely on the ->parent->last to find |
| 3182 | * the last sibling. |
| 3183 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3184 | if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3185 | (cur->parent->children != NULL) && |
| 3186 | (cur->parent->last != NULL) && |
| 3187 | (cur->parent->last->next == NULL)) { |
| 3188 | cur = cur->parent->last; |
| 3189 | } else { |
| 3190 | while (cur->next != NULL) cur = cur->next; |
| 3191 | } |
| 3192 | |
| 3193 | xmlUnlinkNode(elem); |
| 3194 | |
Daniel Veillard | e22dd5c | 2003-10-29 12:53:27 +0000 | [diff] [blame] | 3195 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) && |
| 3196 | (cur->name == elem->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3197 | xmlNodeAddContent(cur, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3198 | xmlFreeNode(elem); |
| 3199 | return(cur); |
Rob Richards | 6581512 | 2006-02-25 17:13:33 +0000 | [diff] [blame] | 3200 | } else if (elem->type == XML_ATTRIBUTE_NODE) { |
| 3201 | return xmlAddPropSibling(cur, cur, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
| 3204 | if (elem->doc != cur->doc) { |
| 3205 | xmlSetTreeDoc(elem, cur->doc); |
| 3206 | } |
| 3207 | parent = cur->parent; |
| 3208 | elem->prev = cur; |
| 3209 | elem->next = NULL; |
| 3210 | elem->parent = parent; |
| 3211 | cur->next = elem; |
| 3212 | if (parent != NULL) |
| 3213 | parent->last = elem; |
| 3214 | |
| 3215 | return(elem); |
| 3216 | } |
| 3217 | |
| 3218 | /** |
| 3219 | * xmlAddChildList: |
| 3220 | * @parent: the parent node |
| 3221 | * @cur: the first node in the list |
| 3222 | * |
| 3223 | * Add a list of node at the end of the child list of the parent |
| 3224 | * merging adjacent TEXT nodes (@cur may be freed) |
| 3225 | * |
| 3226 | * Returns the last child or NULL in case of error. |
| 3227 | */ |
| 3228 | xmlNodePtr |
| 3229 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 3230 | xmlNodePtr prev; |
| 3231 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3232 | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3233 | #ifdef DEBUG_TREE |
| 3234 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3235 | "xmlAddChildList : parent == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3236 | #endif |
| 3237 | return(NULL); |
| 3238 | } |
| 3239 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3240 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3241 | #ifdef DEBUG_TREE |
| 3242 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3243 | "xmlAddChildList : child == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3244 | #endif |
| 3245 | return(NULL); |
| 3246 | } |
| 3247 | |
| 3248 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 3249 | (cur->doc != parent->doc)) { |
| 3250 | #ifdef DEBUG_TREE |
| 3251 | xmlGenericError(xmlGenericErrorContext, |
| 3252 | "Elements moved to a different document\n"); |
| 3253 | #endif |
| 3254 | } |
| 3255 | |
| 3256 | /* |
| 3257 | * add the first element at the end of the children list. |
| 3258 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3259 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3260 | if (parent->children == NULL) { |
| 3261 | parent->children = cur; |
| 3262 | } else { |
| 3263 | /* |
| 3264 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 3265 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3266 | if ((cur->type == XML_TEXT_NODE) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3267 | (parent->last->type == XML_TEXT_NODE) && |
| 3268 | (cur->name == parent->last->name)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3269 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3270 | /* |
| 3271 | * if it's the only child, nothing more to be done. |
| 3272 | */ |
| 3273 | if (cur->next == NULL) { |
| 3274 | xmlFreeNode(cur); |
| 3275 | return(parent->last); |
| 3276 | } |
| 3277 | prev = cur; |
| 3278 | cur = cur->next; |
| 3279 | xmlFreeNode(prev); |
| 3280 | } |
| 3281 | prev = parent->last; |
| 3282 | prev->next = cur; |
| 3283 | cur->prev = prev; |
| 3284 | } |
| 3285 | while (cur->next != NULL) { |
| 3286 | cur->parent = parent; |
| 3287 | if (cur->doc != parent->doc) { |
| 3288 | xmlSetTreeDoc(cur, parent->doc); |
| 3289 | } |
| 3290 | cur = cur->next; |
| 3291 | } |
| 3292 | cur->parent = parent; |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3293 | /* the parent may not be linked to a doc ! */ |
| 3294 | if (cur->doc != parent->doc) { |
| 3295 | xmlSetTreeDoc(cur, parent->doc); |
| 3296 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3297 | parent->last = cur; |
| 3298 | |
| 3299 | return(cur); |
| 3300 | } |
| 3301 | |
| 3302 | /** |
| 3303 | * xmlAddChild: |
| 3304 | * @parent: the parent node |
| 3305 | * @cur: the child node |
| 3306 | * |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3307 | * 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] | 3308 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3309 | * 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] | 3310 | * If there is an attribute with equal name, it is first destroyed. |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3311 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3312 | * Returns the child or NULL in case of error. |
| 3313 | */ |
| 3314 | xmlNodePtr |
| 3315 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
| 3316 | xmlNodePtr prev; |
| 3317 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3318 | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3319 | #ifdef DEBUG_TREE |
| 3320 | xmlGenericError(xmlGenericErrorContext, |
| 3321 | "xmlAddChild : parent == NULL\n"); |
| 3322 | #endif |
| 3323 | return(NULL); |
| 3324 | } |
| 3325 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3326 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3327 | #ifdef DEBUG_TREE |
| 3328 | xmlGenericError(xmlGenericErrorContext, |
| 3329 | "xmlAddChild : child == NULL\n"); |
| 3330 | #endif |
| 3331 | return(NULL); |
| 3332 | } |
| 3333 | |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3334 | if (parent == cur) { |
| 3335 | #ifdef DEBUG_TREE |
| 3336 | xmlGenericError(xmlGenericErrorContext, |
| 3337 | "xmlAddChild : parent == cur\n"); |
| 3338 | #endif |
| 3339 | return(NULL); |
| 3340 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3341 | /* |
| 3342 | * 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] | 3343 | * cur is then freed. |
| 3344 | */ |
| 3345 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3346 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3347 | (parent->content != NULL) && |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3348 | (parent->name == cur->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3349 | xmlNodeAddContent(parent, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3350 | xmlFreeNode(cur); |
| 3351 | return(parent); |
| 3352 | } |
| 3353 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3354 | (parent->last->name == cur->name) && |
| 3355 | (parent->last != cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3356 | xmlNodeAddContent(parent->last, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3357 | xmlFreeNode(cur); |
| 3358 | return(parent->last); |
| 3359 | } |
| 3360 | } |
| 3361 | |
| 3362 | /* |
| 3363 | * add the new element at the end of the children list. |
| 3364 | */ |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3365 | prev = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3366 | cur->parent = parent; |
| 3367 | if (cur->doc != parent->doc) { |
| 3368 | xmlSetTreeDoc(cur, parent->doc); |
| 3369 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3370 | /* this check prevents a loop on tree-traversions if a developer |
| 3371 | * tries to add a node to its parent multiple times |
| 3372 | */ |
| 3373 | if (prev == parent) |
| 3374 | return(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3375 | |
| 3376 | /* |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3377 | * Coalescing |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3378 | */ |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3379 | if ((parent->type == XML_TEXT_NODE) && |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3380 | (parent->content != NULL) && |
| 3381 | (parent != cur)) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3382 | xmlNodeAddContent(parent, cur->content); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3383 | xmlFreeNode(cur); |
| 3384 | return(parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3385 | } |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3386 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3387 | if (parent->type != XML_ELEMENT_NODE) |
| 3388 | return(NULL); |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3389 | if (parent->properties != NULL) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3390 | /* check if an attribute with the same name exists */ |
| 3391 | xmlAttrPtr lastattr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3392 | |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3393 | if (cur->ns == NULL) |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3394 | lastattr = xmlHasNsProp(parent, cur->name, NULL); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3395 | else |
| 3396 | lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href); |
Rob Richards | c342ec6 | 2005-10-25 00:10:12 +0000 | [diff] [blame] | 3397 | if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3398 | /* different instance, destroy it (attributes must be unique) */ |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3399 | xmlUnlinkNode((xmlNodePtr) lastattr); |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3400 | xmlFreeProp(lastattr); |
| 3401 | } |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3402 | if (lastattr == (xmlAttrPtr) cur) |
| 3403 | return(cur); |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3404 | |
| 3405 | } |
| 3406 | if (parent->properties == NULL) { |
| 3407 | parent->properties = (xmlAttrPtr) cur; |
| 3408 | } else { |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3409 | /* find the end */ |
Rob Richards | 810a78b | 2008-12-31 22:13:57 +0000 | [diff] [blame] | 3410 | xmlAttrPtr lastattr = parent->properties; |
Daniel Veillard | bd227ae | 2002-01-24 16:05:41 +0000 | [diff] [blame] | 3411 | while (lastattr->next != NULL) { |
| 3412 | lastattr = lastattr->next; |
| 3413 | } |
| 3414 | lastattr->next = (xmlAttrPtr) cur; |
| 3415 | ((xmlAttrPtr) cur)->prev = lastattr; |
| 3416 | } |
| 3417 | } else { |
| 3418 | if (parent->children == NULL) { |
| 3419 | parent->children = cur; |
| 3420 | parent->last = cur; |
| 3421 | } else { |
| 3422 | prev = parent->last; |
| 3423 | prev->next = cur; |
| 3424 | cur->prev = prev; |
| 3425 | parent->last = cur; |
| 3426 | } |
| 3427 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3428 | return(cur); |
| 3429 | } |
| 3430 | |
| 3431 | /** |
| 3432 | * xmlGetLastChild: |
| 3433 | * @parent: the parent node |
| 3434 | * |
| 3435 | * Search the last child of a node. |
| 3436 | * Returns the last child or NULL if none. |
| 3437 | */ |
| 3438 | xmlNodePtr |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 3439 | xmlGetLastChild(const xmlNode *parent) { |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3440 | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3441 | #ifdef DEBUG_TREE |
| 3442 | xmlGenericError(xmlGenericErrorContext, |
| 3443 | "xmlGetLastChild : parent == NULL\n"); |
| 3444 | #endif |
| 3445 | return(NULL); |
| 3446 | } |
| 3447 | return(parent->last); |
| 3448 | } |
| 3449 | |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3450 | #ifdef LIBXML_TREE_ENABLED |
| 3451 | /* |
| 3452 | * 5 interfaces from DOM ElementTraversal |
| 3453 | */ |
| 3454 | |
| 3455 | /** |
| 3456 | * xmlChildElementCount: |
| 3457 | * @parent: the parent node |
| 3458 | * |
| 3459 | * Finds the current number of child nodes of that element which are |
| 3460 | * element nodes. |
| 3461 | * Note the handling of entities references is different than in |
| 3462 | * the W3C DOM element traversal spec since we don't have back reference |
| 3463 | * from entities content to entities references. |
| 3464 | * |
| 3465 | * Returns the count of element child or 0 if not available |
| 3466 | */ |
| 3467 | unsigned long |
| 3468 | xmlChildElementCount(xmlNodePtr parent) { |
| 3469 | unsigned long ret = 0; |
| 3470 | xmlNodePtr cur = NULL; |
| 3471 | |
| 3472 | if (parent == NULL) |
| 3473 | return(0); |
| 3474 | switch (parent->type) { |
| 3475 | case XML_ELEMENT_NODE: |
| 3476 | case XML_ENTITY_NODE: |
| 3477 | case XML_DOCUMENT_NODE: |
Kyle VanderBeek | 1db9969 | 2014-07-29 00:32:15 -0700 | [diff] [blame] | 3478 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3479 | case XML_HTML_DOCUMENT_NODE: |
| 3480 | cur = parent->children; |
| 3481 | break; |
| 3482 | default: |
| 3483 | return(0); |
| 3484 | } |
| 3485 | while (cur != NULL) { |
| 3486 | if (cur->type == XML_ELEMENT_NODE) |
| 3487 | ret++; |
| 3488 | cur = cur->next; |
| 3489 | } |
| 3490 | return(ret); |
| 3491 | } |
| 3492 | |
| 3493 | /** |
| 3494 | * xmlFirstElementChild: |
| 3495 | * @parent: the parent node |
| 3496 | * |
| 3497 | * Finds the first child node of that element which is a Element node |
| 3498 | * Note the handling of entities references is different than in |
| 3499 | * the W3C DOM element traversal spec since we don't have back reference |
| 3500 | * from entities content to entities references. |
| 3501 | * |
| 3502 | * Returns the first element child or NULL if not available |
| 3503 | */ |
| 3504 | xmlNodePtr |
| 3505 | xmlFirstElementChild(xmlNodePtr parent) { |
| 3506 | xmlNodePtr cur = NULL; |
| 3507 | |
| 3508 | if (parent == NULL) |
| 3509 | return(NULL); |
| 3510 | switch (parent->type) { |
| 3511 | case XML_ELEMENT_NODE: |
| 3512 | case XML_ENTITY_NODE: |
| 3513 | case XML_DOCUMENT_NODE: |
Kyle VanderBeek | 1db9969 | 2014-07-29 00:32:15 -0700 | [diff] [blame] | 3514 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3515 | case XML_HTML_DOCUMENT_NODE: |
| 3516 | cur = parent->children; |
| 3517 | break; |
| 3518 | default: |
| 3519 | return(NULL); |
| 3520 | } |
| 3521 | while (cur != NULL) { |
| 3522 | if (cur->type == XML_ELEMENT_NODE) |
| 3523 | return(cur); |
| 3524 | cur = cur->next; |
| 3525 | } |
| 3526 | return(NULL); |
| 3527 | } |
| 3528 | |
| 3529 | /** |
| 3530 | * xmlLastElementChild: |
| 3531 | * @parent: the parent node |
| 3532 | * |
| 3533 | * Finds the last child node of that element which is a Element node |
| 3534 | * Note the handling of entities references is different than in |
| 3535 | * the W3C DOM element traversal spec since we don't have back reference |
| 3536 | * from entities content to entities references. |
| 3537 | * |
| 3538 | * Returns the last element child or NULL if not available |
| 3539 | */ |
| 3540 | xmlNodePtr |
| 3541 | xmlLastElementChild(xmlNodePtr parent) { |
| 3542 | xmlNodePtr cur = NULL; |
| 3543 | |
| 3544 | if (parent == NULL) |
| 3545 | return(NULL); |
| 3546 | switch (parent->type) { |
| 3547 | case XML_ELEMENT_NODE: |
| 3548 | case XML_ENTITY_NODE: |
| 3549 | case XML_DOCUMENT_NODE: |
Kyle VanderBeek | 1db9969 | 2014-07-29 00:32:15 -0700 | [diff] [blame] | 3550 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3551 | case XML_HTML_DOCUMENT_NODE: |
| 3552 | cur = parent->last; |
| 3553 | break; |
| 3554 | default: |
| 3555 | return(NULL); |
| 3556 | } |
| 3557 | while (cur != NULL) { |
| 3558 | if (cur->type == XML_ELEMENT_NODE) |
| 3559 | return(cur); |
| 3560 | cur = cur->prev; |
| 3561 | } |
| 3562 | return(NULL); |
| 3563 | } |
| 3564 | |
| 3565 | /** |
| 3566 | * xmlPreviousElementSibling: |
| 3567 | * @node: the current node |
| 3568 | * |
| 3569 | * Finds the first closest previous sibling of the node which is an |
| 3570 | * element node. |
| 3571 | * Note the handling of entities references is different than in |
| 3572 | * the W3C DOM element traversal spec since we don't have back reference |
| 3573 | * from entities content to entities references. |
| 3574 | * |
| 3575 | * Returns the previous element sibling or NULL if not available |
| 3576 | */ |
| 3577 | xmlNodePtr |
| 3578 | xmlPreviousElementSibling(xmlNodePtr node) { |
| 3579 | if (node == NULL) |
| 3580 | return(NULL); |
| 3581 | switch (node->type) { |
| 3582 | case XML_ELEMENT_NODE: |
| 3583 | case XML_TEXT_NODE: |
| 3584 | case XML_CDATA_SECTION_NODE: |
| 3585 | case XML_ENTITY_REF_NODE: |
| 3586 | case XML_ENTITY_NODE: |
| 3587 | case XML_PI_NODE: |
| 3588 | case XML_COMMENT_NODE: |
| 3589 | case XML_XINCLUDE_START: |
| 3590 | case XML_XINCLUDE_END: |
| 3591 | node = node->prev; |
| 3592 | break; |
| 3593 | default: |
| 3594 | return(NULL); |
| 3595 | } |
| 3596 | while (node != NULL) { |
| 3597 | if (node->type == XML_ELEMENT_NODE) |
| 3598 | return(node); |
François Delyon | 2f70090 | 2010-02-03 17:32:37 +0100 | [diff] [blame] | 3599 | node = node->prev; |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3600 | } |
| 3601 | return(NULL); |
| 3602 | } |
| 3603 | |
| 3604 | /** |
| 3605 | * xmlNextElementSibling: |
| 3606 | * @node: the current node |
| 3607 | * |
| 3608 | * Finds the first closest next sibling of the node which is an |
| 3609 | * element node. |
| 3610 | * Note the handling of entities references is different than in |
| 3611 | * the W3C DOM element traversal spec since we don't have back reference |
| 3612 | * from entities content to entities references. |
| 3613 | * |
| 3614 | * Returns the next element sibling or NULL if not available |
| 3615 | */ |
| 3616 | xmlNodePtr |
| 3617 | xmlNextElementSibling(xmlNodePtr node) { |
| 3618 | if (node == NULL) |
| 3619 | return(NULL); |
| 3620 | switch (node->type) { |
| 3621 | case XML_ELEMENT_NODE: |
| 3622 | case XML_TEXT_NODE: |
| 3623 | case XML_CDATA_SECTION_NODE: |
| 3624 | case XML_ENTITY_REF_NODE: |
| 3625 | case XML_ENTITY_NODE: |
| 3626 | case XML_PI_NODE: |
| 3627 | case XML_COMMENT_NODE: |
| 3628 | case XML_DTD_NODE: |
| 3629 | case XML_XINCLUDE_START: |
| 3630 | case XML_XINCLUDE_END: |
| 3631 | node = node->next; |
| 3632 | break; |
| 3633 | default: |
| 3634 | return(NULL); |
| 3635 | } |
| 3636 | while (node != NULL) { |
| 3637 | if (node->type == XML_ELEMENT_NODE) |
| 3638 | return(node); |
| 3639 | node = node->next; |
| 3640 | } |
| 3641 | return(NULL); |
| 3642 | } |
| 3643 | |
| 3644 | #endif /* LIBXML_TREE_ENABLED */ |
| 3645 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3646 | /** |
| 3647 | * xmlFreeNodeList: |
| 3648 | * @cur: the first node in the list |
| 3649 | * |
| 3650 | * Free a node and all its siblings, this is a recursive behaviour, all |
| 3651 | * the children are freed too. |
| 3652 | */ |
| 3653 | void |
| 3654 | xmlFreeNodeList(xmlNodePtr cur) { |
| 3655 | xmlNodePtr next; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3656 | xmlDictPtr dict = NULL; |
| 3657 | |
| 3658 | if (cur == NULL) return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3659 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3660 | xmlFreeNsList((xmlNsPtr) cur); |
| 3661 | return; |
| 3662 | } |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3663 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 3664 | #ifdef LIBXML_DOCB_ENABLED |
| 3665 | (cur->type == XML_DOCB_DOCUMENT_NODE) || |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3666 | #endif |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3667 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3668 | xmlFreeDoc((xmlDocPtr) cur); |
| 3669 | return; |
| 3670 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3671 | if (cur->doc != NULL) dict = cur->doc->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3672 | while (cur != NULL) { |
| 3673 | next = cur->next; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3674 | if (cur->type != XML_DTD_NODE) { |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3675 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3676 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3677 | xmlDeregisterNodeDefaultValue(cur); |
| 3678 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3679 | if ((cur->children != NULL) && |
| 3680 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3681 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3682 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3683 | (cur->type == XML_XINCLUDE_START) || |
| 3684 | (cur->type == XML_XINCLUDE_END)) && |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3685 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3686 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3687 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3688 | (cur->type != XML_XINCLUDE_START) && |
| 3689 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3690 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3691 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3692 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3693 | } |
| 3694 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3695 | (cur->type == XML_XINCLUDE_START) || |
| 3696 | (cur->type == XML_XINCLUDE_END)) && |
| 3697 | (cur->nsDef != NULL)) |
| 3698 | xmlFreeNsList(cur->nsDef); |
| 3699 | |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3700 | /* |
| 3701 | * When a node is a text node or a comment, it uses a global static |
| 3702 | * variable for the name of the node. |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3703 | * Otherwise the node name might come from the document's |
Jan Pokorný | bb654fe | 2016-04-13 16:56:07 +0200 | [diff] [blame] | 3704 | * dictionary |
Daniel Veillard | 9cc6dc6 | 2001-06-11 08:09:20 +0000 | [diff] [blame] | 3705 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3706 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3707 | (cur->type != XML_TEXT_NODE) && |
| 3708 | (cur->type != XML_COMMENT_NODE)) |
| 3709 | DICT_FREE(cur->name) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3710 | xmlFree(cur); |
| 3711 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3712 | cur = next; |
| 3713 | } |
| 3714 | } |
| 3715 | |
| 3716 | /** |
| 3717 | * xmlFreeNode: |
| 3718 | * @cur: the node |
| 3719 | * |
| 3720 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 3721 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
| 3722 | */ |
| 3723 | void |
| 3724 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3725 | xmlDictPtr dict = NULL; |
| 3726 | |
| 3727 | if (cur == NULL) return; |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3728 | |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3729 | /* use xmlFreeDtd for DTD nodes */ |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3730 | if (cur->type == XML_DTD_NODE) { |
| 3731 | xmlFreeDtd((xmlDtdPtr) cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3732 | return; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 3733 | } |
| 3734 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3735 | xmlFreeNs((xmlNsPtr) cur); |
| 3736 | return; |
| 3737 | } |
Daniel Veillard | a70d62f | 2002-11-07 14:18:03 +0000 | [diff] [blame] | 3738 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3739 | xmlFreeProp((xmlAttrPtr) cur); |
| 3740 | return; |
| 3741 | } |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3742 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 3743 | if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
Daniel Veillard | 5335dc5 | 2003-01-01 20:59:38 +0000 | [diff] [blame] | 3744 | xmlDeregisterNodeDefaultValue(cur); |
| 3745 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3746 | if (cur->doc != NULL) dict = cur->doc->dict; |
| 3747 | |
Daniel Veillard | 2cba415 | 2008-08-27 11:45:41 +0000 | [diff] [blame] | 3748 | if (cur->type == XML_ENTITY_DECL) { |
| 3749 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 3750 | DICT_FREE(ent->SystemID); |
| 3751 | DICT_FREE(ent->ExternalID); |
| 3752 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3753 | if ((cur->children != NULL) && |
| 3754 | (cur->type != XML_ENTITY_REF_NODE)) |
| 3755 | xmlFreeNodeList(cur->children); |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3756 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3757 | (cur->type == XML_XINCLUDE_START) || |
| 3758 | (cur->type == XML_XINCLUDE_END)) && |
| 3759 | (cur->properties != NULL)) |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 3760 | xmlFreePropList(cur->properties); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3761 | if ((cur->type != XML_ELEMENT_NODE) && |
| 3762 | (cur->content != NULL) && |
| 3763 | (cur->type != XML_ENTITY_REF_NODE) && |
| 3764 | (cur->type != XML_XINCLUDE_END) && |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 3765 | (cur->type != XML_XINCLUDE_START) && |
| 3766 | (cur->content != (xmlChar *) &(cur->properties))) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3767 | DICT_FREE(cur->content) |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3770 | /* |
| 3771 | * When a node is a text node or a comment, it uses a global static |
| 3772 | * variable for the name of the node. |
Jan Pokorný | bb654fe | 2016-04-13 16:56:07 +0200 | [diff] [blame] | 3773 | * Otherwise the node name might come from the document's dictionary |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3774 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3775 | if ((cur->name != NULL) && |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 3776 | (cur->type != XML_TEXT_NODE) && |
| 3777 | (cur->type != XML_COMMENT_NODE)) |
| 3778 | DICT_FREE(cur->name) |
Daniel Veillard | acd370f | 2001-06-09 17:17:51 +0000 | [diff] [blame] | 3779 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3780 | if (((cur->type == XML_ELEMENT_NODE) || |
| 3781 | (cur->type == XML_XINCLUDE_START) || |
| 3782 | (cur->type == XML_XINCLUDE_END)) && |
| 3783 | (cur->nsDef != NULL)) |
| 3784 | xmlFreeNsList(cur->nsDef); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3785 | xmlFree(cur); |
| 3786 | } |
| 3787 | |
| 3788 | /** |
| 3789 | * xmlUnlinkNode: |
| 3790 | * @cur: the node |
| 3791 | * |
| 3792 | * 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] | 3793 | * If one need to free the node, use xmlFreeNode() routine after the |
| 3794 | * unlink to discard it. |
Daniel Veillard | 6ca24a3 | 2012-08-08 15:31:55 +0800 | [diff] [blame] | 3795 | * Note that namespace nodes can't be unlinked as they do not have |
| 3796 | * pointer to their parent. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3797 | */ |
| 3798 | void |
| 3799 | xmlUnlinkNode(xmlNodePtr cur) { |
| 3800 | if (cur == NULL) { |
| 3801 | #ifdef DEBUG_TREE |
| 3802 | xmlGenericError(xmlGenericErrorContext, |
| 3803 | "xmlUnlinkNode : node == NULL\n"); |
| 3804 | #endif |
| 3805 | return; |
| 3806 | } |
Daniel Veillard | 6ca24a3 | 2012-08-08 15:31:55 +0800 | [diff] [blame] | 3807 | if (cur->type == XML_NAMESPACE_DECL) |
| 3808 | return; |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3809 | if (cur->type == XML_DTD_NODE) { |
| 3810 | xmlDocPtr doc; |
| 3811 | doc = cur->doc; |
Daniel Veillard | a067e65 | 2003-05-01 08:03:46 +0000 | [diff] [blame] | 3812 | if (doc != NULL) { |
| 3813 | if (doc->intSubset == (xmlDtdPtr) cur) |
| 3814 | doc->intSubset = NULL; |
| 3815 | if (doc->extSubset == (xmlDtdPtr) cur) |
| 3816 | doc->extSubset = NULL; |
| 3817 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 3818 | } |
Daniel Veillard | 2cba415 | 2008-08-27 11:45:41 +0000 | [diff] [blame] | 3819 | if (cur->type == XML_ENTITY_DECL) { |
| 3820 | xmlDocPtr doc; |
| 3821 | doc = cur->doc; |
| 3822 | if (doc != NULL) { |
| 3823 | if (doc->intSubset != NULL) { |
| 3824 | if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur) |
| 3825 | xmlHashRemoveEntry(doc->intSubset->entities, cur->name, |
| 3826 | NULL); |
| 3827 | if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur) |
| 3828 | xmlHashRemoveEntry(doc->intSubset->pentities, cur->name, |
| 3829 | NULL); |
| 3830 | } |
| 3831 | if (doc->extSubset != NULL) { |
| 3832 | if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur) |
| 3833 | xmlHashRemoveEntry(doc->extSubset->entities, cur->name, |
| 3834 | NULL); |
| 3835 | if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur) |
| 3836 | xmlHashRemoveEntry(doc->extSubset->pentities, cur->name, |
| 3837 | NULL); |
| 3838 | } |
| 3839 | } |
| 3840 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3841 | if (cur->parent != NULL) { |
| 3842 | xmlNodePtr parent; |
| 3843 | parent = cur->parent; |
| 3844 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3845 | if (parent->properties == (xmlAttrPtr) cur) |
| 3846 | parent->properties = ((xmlAttrPtr) cur)->next; |
| 3847 | } else { |
| 3848 | if (parent->children == cur) |
| 3849 | parent->children = cur->next; |
| 3850 | if (parent->last == cur) |
| 3851 | parent->last = cur->prev; |
| 3852 | } |
| 3853 | cur->parent = NULL; |
| 3854 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3855 | if (cur->next != NULL) |
| 3856 | cur->next->prev = cur->prev; |
| 3857 | if (cur->prev != NULL) |
| 3858 | cur->prev->next = cur->next; |
| 3859 | cur->next = cur->prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 3862 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3863 | /** |
| 3864 | * xmlReplaceNode: |
| 3865 | * @old: the old node |
| 3866 | * @cur: the node |
| 3867 | * |
William M. Brack | d7cf7f8 | 2003-11-14 07:13:16 +0000 | [diff] [blame] | 3868 | * Unlink the old node from its current context, prune the new one |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3869 | * 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] | 3870 | * first unlinked from its existing context. |
| 3871 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3872 | * Returns the @old node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3873 | */ |
| 3874 | xmlNodePtr |
| 3875 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 3876 | if (old == cur) return(NULL); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3877 | if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) || |
| 3878 | (old->parent == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3879 | #ifdef DEBUG_TREE |
| 3880 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3881 | "xmlReplaceNode : old == NULL or without parent\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3882 | #endif |
| 3883 | return(NULL); |
| 3884 | } |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3885 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3886 | xmlUnlinkNode(old); |
| 3887 | return(old); |
| 3888 | } |
| 3889 | if (cur == old) { |
| 3890 | return(old); |
| 3891 | } |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3892 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
| 3893 | #ifdef DEBUG_TREE |
| 3894 | xmlGenericError(xmlGenericErrorContext, |
| 3895 | "xmlReplaceNode : Trying to replace attribute node with other node type\n"); |
| 3896 | #endif |
| 3897 | return(old); |
| 3898 | } |
| 3899 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
| 3900 | #ifdef DEBUG_TREE |
| 3901 | xmlGenericError(xmlGenericErrorContext, |
| 3902 | "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n"); |
| 3903 | #endif |
| 3904 | return(old); |
| 3905 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3906 | xmlUnlinkNode(cur); |
Daniel Veillard | 64d7d12 | 2005-05-11 18:03:42 +0000 | [diff] [blame] | 3907 | xmlSetTreeDoc(cur, old->doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3908 | cur->parent = old->parent; |
| 3909 | cur->next = old->next; |
| 3910 | if (cur->next != NULL) |
| 3911 | cur->next->prev = cur; |
| 3912 | cur->prev = old->prev; |
| 3913 | if (cur->prev != NULL) |
| 3914 | cur->prev->next = cur; |
| 3915 | if (cur->parent != NULL) { |
Daniel Veillard | c169f8b | 2002-01-22 21:40:13 +0000 | [diff] [blame] | 3916 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 3917 | if (cur->parent->properties == (xmlAttrPtr)old) |
| 3918 | cur->parent->properties = ((xmlAttrPtr) cur); |
| 3919 | } else { |
| 3920 | if (cur->parent->children == old) |
| 3921 | cur->parent->children = cur; |
| 3922 | if (cur->parent->last == old) |
| 3923 | cur->parent->last = cur; |
| 3924 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3925 | } |
| 3926 | old->next = old->prev = NULL; |
| 3927 | old->parent = NULL; |
| 3928 | return(old); |
| 3929 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3930 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3931 | |
| 3932 | /************************************************************************ |
| 3933 | * * |
| 3934 | * Copy operations * |
| 3935 | * * |
| 3936 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 3937 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3938 | /** |
| 3939 | * xmlCopyNamespace: |
| 3940 | * @cur: the namespace |
| 3941 | * |
| 3942 | * Do a copy of the namespace. |
| 3943 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3944 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3945 | */ |
| 3946 | xmlNsPtr |
| 3947 | xmlCopyNamespace(xmlNsPtr cur) { |
| 3948 | xmlNsPtr ret; |
| 3949 | |
| 3950 | if (cur == NULL) return(NULL); |
| 3951 | switch (cur->type) { |
| 3952 | case XML_LOCAL_NAMESPACE: |
| 3953 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 3954 | break; |
| 3955 | default: |
| 3956 | #ifdef DEBUG_TREE |
| 3957 | xmlGenericError(xmlGenericErrorContext, |
| 3958 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
| 3959 | #endif |
| 3960 | return(NULL); |
| 3961 | } |
| 3962 | return(ret); |
| 3963 | } |
| 3964 | |
| 3965 | /** |
| 3966 | * xmlCopyNamespaceList: |
| 3967 | * @cur: the first namespace |
| 3968 | * |
| 3969 | * Do a copy of an namespace list. |
| 3970 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 3971 | * Returns: a new #xmlNsPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3972 | */ |
| 3973 | xmlNsPtr |
| 3974 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 3975 | xmlNsPtr ret = NULL; |
| 3976 | xmlNsPtr p = NULL,q; |
| 3977 | |
| 3978 | while (cur != NULL) { |
| 3979 | q = xmlCopyNamespace(cur); |
| 3980 | if (p == NULL) { |
| 3981 | ret = p = q; |
| 3982 | } else { |
| 3983 | p->next = q; |
| 3984 | p = q; |
| 3985 | } |
| 3986 | cur = cur->next; |
| 3987 | } |
| 3988 | return(ret); |
| 3989 | } |
| 3990 | |
| 3991 | static xmlNodePtr |
| 3992 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 3993 | |
| 3994 | static xmlAttrPtr |
| 3995 | xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3996 | xmlAttrPtr ret; |
| 3997 | |
| 3998 | if (cur == NULL) return(NULL); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 3999 | if ((target != NULL) && (target->type != XML_ELEMENT_NODE)) |
| 4000 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4001 | if (target != NULL) |
| 4002 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 4003 | else if (doc != NULL) |
| 4004 | ret = xmlNewDocProp(doc, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4005 | else if (cur->parent != NULL) |
| 4006 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 4007 | else if (cur->children != NULL) |
| 4008 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 4009 | else |
| 4010 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 4011 | if (ret == NULL) return(NULL); |
| 4012 | ret->parent = target; |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 4013 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4014 | if ((cur->ns != NULL) && (target != NULL)) { |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 4015 | xmlNsPtr ns; |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4016 | |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 4017 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 4018 | if (ns == NULL) { |
| 4019 | /* |
| 4020 | * Humm, we are copying an element whose namespace is defined |
| 4021 | * out of the new tree scope. Search it in the original tree |
| 4022 | * and add it at the top of the new tree |
| 4023 | */ |
| 4024 | ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); |
| 4025 | if (ns != NULL) { |
| 4026 | xmlNodePtr root = target; |
| 4027 | xmlNodePtr pred = NULL; |
| 4028 | |
| 4029 | while (root->parent != NULL) { |
| 4030 | pred = root; |
| 4031 | root = root->parent; |
| 4032 | } |
| 4033 | if (root == (xmlNodePtr) target->doc) { |
| 4034 | /* correct possibly cycling above the document elt */ |
| 4035 | root = pred; |
| 4036 | } |
| 4037 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 4038 | } |
| 4039 | } else { |
| 4040 | /* |
| 4041 | * we have to find something appropriate here since |
| 4042 | * we cant be sure, that the namespce we found is identified |
| 4043 | * by the prefix |
| 4044 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 4045 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
Daniel Veillard | d4f41aa | 2002-03-03 14:13:46 +0000 | [diff] [blame] | 4046 | /* this is the nice case */ |
| 4047 | ret->ns = ns; |
| 4048 | } else { |
| 4049 | /* |
| 4050 | * we are in trouble: we need a new reconcilied namespace. |
| 4051 | * This is expensive |
| 4052 | */ |
| 4053 | ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns); |
| 4054 | } |
| 4055 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4056 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4057 | } else |
| 4058 | ret->ns = NULL; |
| 4059 | |
| 4060 | if (cur->children != NULL) { |
| 4061 | xmlNodePtr tmp; |
| 4062 | |
| 4063 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
| 4064 | ret->last = NULL; |
| 4065 | tmp = ret->children; |
| 4066 | while (tmp != NULL) { |
| 4067 | /* tmp->parent = (xmlNodePtr)ret; */ |
| 4068 | if (tmp->next == NULL) |
| 4069 | ret->last = tmp; |
| 4070 | tmp = tmp->next; |
| 4071 | } |
| 4072 | } |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 4073 | /* |
| 4074 | * Try to handle IDs |
| 4075 | */ |
Daniel Veillard | a3db2e3 | 2002-03-08 15:46:57 +0000 | [diff] [blame] | 4076 | if ((target!= NULL) && (cur!= NULL) && |
| 4077 | (target->doc != NULL) && (cur->doc != NULL) && |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 4078 | (cur->doc->ids != NULL) && (cur->parent != NULL)) { |
| 4079 | if (xmlIsID(cur->doc, cur->parent, cur)) { |
| 4080 | xmlChar *id; |
| 4081 | |
| 4082 | id = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 4083 | if (id != NULL) { |
| 4084 | xmlAddID(NULL, target->doc, id, ret); |
| 4085 | xmlFree(id); |
| 4086 | } |
| 4087 | } |
| 4088 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4089 | return(ret); |
| 4090 | } |
| 4091 | |
| 4092 | /** |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 4093 | * xmlCopyProp: |
| 4094 | * @target: the element where the attribute will be grafted |
| 4095 | * @cur: the attribute |
| 4096 | * |
| 4097 | * Do a copy of the attribute. |
| 4098 | * |
| 4099 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
| 4100 | */ |
| 4101 | xmlAttrPtr |
| 4102 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
| 4103 | return xmlCopyPropInternal(NULL, target, cur); |
| 4104 | } |
| 4105 | |
| 4106 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4107 | * xmlCopyPropList: |
| 4108 | * @target: the element where the attributes will be grafted |
| 4109 | * @cur: the first attribute |
| 4110 | * |
| 4111 | * Do a copy of an attribute list. |
| 4112 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4113 | * Returns: a new #xmlAttrPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4114 | */ |
| 4115 | xmlAttrPtr |
| 4116 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
| 4117 | xmlAttrPtr ret = NULL; |
| 4118 | xmlAttrPtr p = NULL,q; |
| 4119 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 4120 | if ((target != NULL) && (target->type != XML_ELEMENT_NODE)) |
| 4121 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4122 | while (cur != NULL) { |
| 4123 | q = xmlCopyProp(target, cur); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4124 | if (q == NULL) |
| 4125 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4126 | if (p == NULL) { |
| 4127 | ret = p = q; |
| 4128 | } else { |
| 4129 | p->next = q; |
| 4130 | q->prev = p; |
| 4131 | p = q; |
| 4132 | } |
| 4133 | cur = cur->next; |
| 4134 | } |
| 4135 | return(ret); |
| 4136 | } |
| 4137 | |
| 4138 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4139 | * NOTE about the CopyNode operations ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4140 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4141 | * They are split into external and internal parts for one |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4142 | * tricky reason: namespaces. Doing a direct copy of a node |
| 4143 | * say RPM:Copyright without changing the namespace pointer to |
| 4144 | * something else can produce stale links. One way to do it is |
| 4145 | * to keep a reference counter but this doesn't work as soon |
| 4146 | * as one move the element or the subtree out of the scope of |
| 4147 | * the existing namespace. The actual solution seems to add |
| 4148 | * a copy of the namespace at the top of the copied tree if |
| 4149 | * not available in the subtree. |
| 4150 | * Hence two functions, the public front-end call the inner ones |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4151 | * The argument "recursive" normally indicates a recursive copy |
| 4152 | * of the node with values 0 (no) and 1 (yes). For XInclude, |
| 4153 | * however, we allow a value of 2 to indicate copy properties and |
| 4154 | * namespace info, but don't recurse on children. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4155 | */ |
| 4156 | |
| 4157 | static xmlNodePtr |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 4158 | xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4159 | int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4160 | xmlNodePtr ret; |
| 4161 | |
| 4162 | if (node == NULL) return(NULL); |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4163 | switch (node->type) { |
| 4164 | case XML_TEXT_NODE: |
| 4165 | case XML_CDATA_SECTION_NODE: |
| 4166 | case XML_ELEMENT_NODE: |
Daniel Veillard | ec6725e | 2002-09-05 11:12:45 +0000 | [diff] [blame] | 4167 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4168 | case XML_ENTITY_REF_NODE: |
| 4169 | case XML_ENTITY_NODE: |
| 4170 | case XML_PI_NODE: |
| 4171 | case XML_COMMENT_NODE: |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4172 | case XML_XINCLUDE_START: |
| 4173 | case XML_XINCLUDE_END: |
| 4174 | break; |
| 4175 | case XML_ATTRIBUTE_NODE: |
Rob Richards | 19dc961 | 2005-10-28 16:15:16 +0000 | [diff] [blame] | 4176 | return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node)); |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4177 | case XML_NAMESPACE_DECL: |
| 4178 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4179 | |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4180 | case XML_DOCUMENT_NODE: |
| 4181 | case XML_HTML_DOCUMENT_NODE: |
| 4182 | #ifdef LIBXML_DOCB_ENABLED |
| 4183 | case XML_DOCB_DOCUMENT_NODE: |
| 4184 | #endif |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4185 | #ifdef LIBXML_TREE_ENABLED |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4186 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended)); |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4187 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4188 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 39196eb | 2001-06-19 18:09:42 +0000 | [diff] [blame] | 4189 | case XML_NOTATION_NODE: |
| 4190 | case XML_DTD_NODE: |
| 4191 | case XML_ELEMENT_DECL: |
| 4192 | case XML_ATTRIBUTE_DECL: |
| 4193 | case XML_ENTITY_DECL: |
| 4194 | return(NULL); |
| 4195 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4196 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4197 | /* |
| 4198 | * Allocate a new node and fill the fields. |
| 4199 | */ |
| 4200 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 4201 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4202 | xmlTreeErrMemory("copying node"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4203 | return(NULL); |
| 4204 | } |
| 4205 | memset(ret, 0, sizeof(xmlNode)); |
| 4206 | ret->type = node->type; |
| 4207 | |
| 4208 | ret->doc = doc; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4209 | ret->parent = parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4210 | if (node->name == xmlStringText) |
| 4211 | ret->name = xmlStringText; |
| 4212 | else if (node->name == xmlStringTextNoenc) |
| 4213 | ret->name = xmlStringTextNoenc; |
| 4214 | else if (node->name == xmlStringComment) |
| 4215 | ret->name = xmlStringComment; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4216 | else if (node->name != NULL) { |
| 4217 | if ((doc != NULL) && (doc->dict != NULL)) |
| 4218 | ret->name = xmlDictLookup(doc->dict, node->name, -1); |
| 4219 | else |
| 4220 | ret->name = xmlStrdup(node->name); |
| 4221 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 4222 | if ((node->type != XML_ELEMENT_NODE) && |
| 4223 | (node->content != NULL) && |
| 4224 | (node->type != XML_ENTITY_REF_NODE) && |
| 4225 | (node->type != XML_XINCLUDE_END) && |
| 4226 | (node->type != XML_XINCLUDE_START)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4227 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 4228 | }else{ |
| 4229 | if (node->type == XML_ELEMENT_NODE) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 4230 | ret->line = node->line; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4231 | } |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4232 | if (parent != NULL) { |
| 4233 | xmlNodePtr tmp; |
| 4234 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4235 | /* |
| 4236 | * this is a tricky part for the node register thing: |
| 4237 | * in case ret does get coalesced in xmlAddChild |
| 4238 | * the deregister-node callback is called; so we register ret now already |
| 4239 | */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 4240 | if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4241 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
| 4242 | |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4243 | tmp = xmlAddChild(parent, ret); |
| 4244 | /* node could have coalesced */ |
| 4245 | if (tmp != ret) |
| 4246 | return(tmp); |
| 4247 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4248 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4249 | if (!extended) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4250 | goto out; |
Petr Pajas | 2afca4a | 2009-07-30 17:47:32 +0200 | [diff] [blame] | 4251 | if (((node->type == XML_ELEMENT_NODE) || |
| 4252 | (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4253 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 4254 | |
| 4255 | if (node->ns != NULL) { |
| 4256 | xmlNsPtr ns; |
| 4257 | |
| 4258 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 4259 | if (ns == NULL) { |
| 4260 | /* |
| 4261 | * Humm, we are copying an element whose namespace is defined |
| 4262 | * out of the new tree scope. Search it in the original tree |
| 4263 | * and add it at the top of the new tree |
| 4264 | */ |
| 4265 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 4266 | if (ns != NULL) { |
| 4267 | xmlNodePtr root = ret; |
| 4268 | |
| 4269 | while (root->parent != NULL) root = root->parent; |
Daniel Veillard | e82a992 | 2001-04-22 12:12:58 +0000 | [diff] [blame] | 4270 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
Rob Richards | ddb01cb | 2010-01-29 13:32:12 -0500 | [diff] [blame] | 4271 | } else { |
| 4272 | ret->ns = xmlNewReconciliedNs(doc, ret, node->ns); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4273 | } |
| 4274 | } else { |
| 4275 | /* |
| 4276 | * reference the existing namespace definition in our own tree. |
| 4277 | */ |
| 4278 | ret->ns = ns; |
| 4279 | } |
| 4280 | } |
Petr Pajas | 2afca4a | 2009-07-30 17:47:32 +0200 | [diff] [blame] | 4281 | if (((node->type == XML_ELEMENT_NODE) || |
| 4282 | (node->type == XML_XINCLUDE_START)) && (node->properties != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4283 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4284 | if (node->type == XML_ENTITY_REF_NODE) { |
| 4285 | if ((doc == NULL) || (node->doc != doc)) { |
| 4286 | /* |
| 4287 | * The copied node will go into a separate document, so |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4288 | * to avoid dangling references to the ENTITY_DECL node |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4289 | * we cannot keep the reference. Try to find it in the |
| 4290 | * target document. |
| 4291 | */ |
| 4292 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
| 4293 | } else { |
| 4294 | ret->children = node->children; |
| 4295 | } |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 4296 | ret->last = ret->children; |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4297 | } else if ((node->children != NULL) && (extended != 2)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4298 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 0ec9863 | 2001-11-14 15:04:32 +0000 | [diff] [blame] | 4299 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
| 4300 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4301 | |
| 4302 | out: |
| 4303 | /* if parent != NULL we already registered the node above */ |
Daniel Veillard | ac996a1 | 2004-07-30 12:02:58 +0000 | [diff] [blame] | 4304 | if ((parent == NULL) && |
| 4305 | ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))) |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 4306 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4307 | return(ret); |
| 4308 | } |
| 4309 | |
| 4310 | static xmlNodePtr |
| 4311 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 4312 | xmlNodePtr ret = NULL; |
| 4313 | xmlNodePtr p = NULL,q; |
| 4314 | |
| 4315 | while (node != NULL) { |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4316 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | 1d0bfab | 2001-07-26 11:49:41 +0000 | [diff] [blame] | 4317 | if (node->type == XML_DTD_NODE ) { |
Daniel Veillard | 4497e69 | 2001-06-09 14:19:02 +0000 | [diff] [blame] | 4318 | if (doc == NULL) { |
| 4319 | node = node->next; |
| 4320 | continue; |
| 4321 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4322 | if (doc->intSubset == NULL) { |
| 4323 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
Gaurav | 98a4e71 | 2013-11-29 23:28:21 +0800 | [diff] [blame] | 4324 | if (q == NULL) return(NULL); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4325 | q->doc = doc; |
| 4326 | q->parent = parent; |
| 4327 | doc->intSubset = (xmlDtdPtr) q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4328 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4329 | } else { |
| 4330 | q = (xmlNodePtr) doc->intSubset; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4331 | xmlAddChild(parent, q); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4332 | } |
| 4333 | } else |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4334 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4335 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Gaurav | 98a4e71 | 2013-11-29 23:28:21 +0800 | [diff] [blame] | 4336 | if (q == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4337 | if (ret == NULL) { |
| 4338 | q->prev = NULL; |
| 4339 | ret = p = q; |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 4340 | } else if (p != q) { |
| 4341 | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4342 | p->next = q; |
| 4343 | q->prev = p; |
| 4344 | p = q; |
| 4345 | } |
| 4346 | node = node->next; |
| 4347 | } |
| 4348 | return(ret); |
| 4349 | } |
| 4350 | |
| 4351 | /** |
| 4352 | * xmlCopyNode: |
| 4353 | * @node: the node |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4354 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4355 | * when applicable) |
| 4356 | * if 2 copy properties and namespaces (when applicable) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4357 | * |
| 4358 | * Do a copy of the node. |
| 4359 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4360 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4361 | */ |
| 4362 | xmlNodePtr |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 4363 | xmlCopyNode(xmlNodePtr node, int extended) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4364 | xmlNodePtr ret; |
| 4365 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4366 | ret = xmlStaticCopyNode(node, NULL, NULL, extended); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4367 | return(ret); |
| 4368 | } |
| 4369 | |
| 4370 | /** |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4371 | * xmlDocCopyNode: |
| 4372 | * @node: the node |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4373 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4374 | * @extended: if 1 do a recursive copy (properties, namespaces and children |
| 4375 | * when applicable) |
| 4376 | * if 2 copy properties and namespaces (when applicable) |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4377 | * |
| 4378 | * Do a copy of the node to a given document. |
| 4379 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4380 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4381 | */ |
| 4382 | xmlNodePtr |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 4383 | xmlDocCopyNode(xmlNodePtr node, xmlDocPtr doc, int extended) { |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4384 | xmlNodePtr ret; |
| 4385 | |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4386 | ret = xmlStaticCopyNode(node, doc, NULL, extended); |
Daniel Veillard | 82daa81 | 2001-04-12 08:55:36 +0000 | [diff] [blame] | 4387 | return(ret); |
| 4388 | } |
| 4389 | |
| 4390 | /** |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4391 | * xmlDocCopyNodeList: |
| 4392 | * @doc: the target document |
| 4393 | * @node: the first node in the list. |
| 4394 | * |
| 4395 | * Do a recursive copy of the node list. |
| 4396 | * |
| 4397 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
| 4398 | */ |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 4399 | xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, xmlNodePtr node) { |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4400 | xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL); |
| 4401 | return(ret); |
| 4402 | } |
| 4403 | |
| 4404 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4405 | * xmlCopyNodeList: |
| 4406 | * @node: the first node in the list. |
| 4407 | * |
| 4408 | * Do a recursive copy of the node list. |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 4409 | * Use xmlDocCopyNodeList() if possible to ensure string interning. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4410 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4411 | * Returns: a new #xmlNodePtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4412 | */ |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 4413 | xmlNodePtr xmlCopyNodeList(xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4414 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 4415 | return(ret); |
| 4416 | } |
| 4417 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4418 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4419 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4420 | * xmlCopyDtd: |
| 4421 | * @dtd: the dtd |
| 4422 | * |
| 4423 | * Do a copy of the dtd. |
| 4424 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4425 | * Returns: a new #xmlDtdPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4426 | */ |
| 4427 | xmlDtdPtr |
| 4428 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 4429 | xmlDtdPtr ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4430 | xmlNodePtr cur, p = NULL, q; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4431 | |
| 4432 | if (dtd == NULL) return(NULL); |
| 4433 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 4434 | if (ret == NULL) return(NULL); |
| 4435 | if (dtd->entities != NULL) |
| 4436 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 4437 | (xmlEntitiesTablePtr) dtd->entities); |
| 4438 | if (dtd->notations != NULL) |
| 4439 | ret->notations = (void *) xmlCopyNotationTable( |
| 4440 | (xmlNotationTablePtr) dtd->notations); |
| 4441 | if (dtd->elements != NULL) |
| 4442 | ret->elements = (void *) xmlCopyElementTable( |
| 4443 | (xmlElementTablePtr) dtd->elements); |
| 4444 | if (dtd->attributes != NULL) |
| 4445 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 4446 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4447 | if (dtd->pentities != NULL) |
| 4448 | ret->pentities = (void *) xmlCopyEntitiesTable( |
| 4449 | (xmlEntitiesTablePtr) dtd->pentities); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4450 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4451 | cur = dtd->children; |
| 4452 | while (cur != NULL) { |
| 4453 | q = NULL; |
| 4454 | |
| 4455 | if (cur->type == XML_ENTITY_DECL) { |
| 4456 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
| 4457 | switch (tmp->etype) { |
| 4458 | case XML_INTERNAL_GENERAL_ENTITY: |
| 4459 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 4460 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 4461 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
| 4462 | break; |
| 4463 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 4464 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4465 | q = (xmlNodePtr) |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4466 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
| 4467 | break; |
| 4468 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 4469 | break; |
| 4470 | } |
| 4471 | } else if (cur->type == XML_ELEMENT_DECL) { |
| 4472 | xmlElementPtr tmp = (xmlElementPtr) cur; |
| 4473 | q = (xmlNodePtr) |
| 4474 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
| 4475 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4476 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4477 | q = (xmlNodePtr) |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4478 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
| 4479 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4480 | q = xmlCopyNode(cur, 0); |
| 4481 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4482 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4483 | if (q == NULL) { |
| 4484 | cur = cur->next; |
| 4485 | continue; |
| 4486 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4487 | |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4488 | if (p == NULL) |
| 4489 | ret->children = q; |
| 4490 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4491 | p->next = q; |
| 4492 | |
| 4493 | q->prev = p; |
| 4494 | q->parent = (xmlNodePtr) ret; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4495 | q->next = NULL; |
| 4496 | ret->last = q; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4497 | p = q; |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4498 | cur = cur->next; |
| 4499 | } |
| 4500 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4501 | return(ret); |
| 4502 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4503 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4504 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4505 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4506 | /** |
| 4507 | * xmlCopyDoc: |
| 4508 | * @doc: the document |
William M. Brack | 57e9e91 | 2004-03-09 16:19:02 +0000 | [diff] [blame] | 4509 | * @recursive: if not zero do a recursive copy. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4510 | * |
| 4511 | * 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] | 4512 | * be copied too as well as DTD, namespaces and entities. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4513 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4514 | * Returns: a new #xmlDocPtr, or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4515 | */ |
| 4516 | xmlDocPtr |
| 4517 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 4518 | xmlDocPtr ret; |
| 4519 | |
| 4520 | if (doc == NULL) return(NULL); |
| 4521 | ret = xmlNewDoc(doc->version); |
| 4522 | if (ret == NULL) return(NULL); |
| 4523 | if (doc->name != NULL) |
| 4524 | ret->name = xmlMemStrdup(doc->name); |
| 4525 | if (doc->encoding != NULL) |
| 4526 | ret->encoding = xmlStrdup(doc->encoding); |
Daniel Veillard | f59507d | 2005-01-27 17:26:49 +0000 | [diff] [blame] | 4527 | if (doc->URL != NULL) |
| 4528 | ret->URL = xmlStrdup(doc->URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4529 | ret->charset = doc->charset; |
| 4530 | ret->compression = doc->compression; |
| 4531 | ret->standalone = doc->standalone; |
| 4532 | if (!recursive) return(ret); |
| 4533 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4534 | ret->last = NULL; |
| 4535 | ret->children = NULL; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4536 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4537 | if (doc->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4538 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | 42870f4 | 2014-07-26 21:04:54 +0800 | [diff] [blame] | 4539 | if (ret->intSubset == NULL) { |
| 4540 | xmlFreeDoc(ret); |
| 4541 | return(NULL); |
| 4542 | } |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 4543 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4544 | ret->intSubset->parent = ret; |
| 4545 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4546 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4547 | if (doc->oldNs != NULL) |
| 4548 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
| 4549 | if (doc->children != NULL) { |
| 4550 | xmlNodePtr tmp; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4551 | |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 4552 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 4553 | (xmlNodePtr)ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4554 | ret->last = NULL; |
| 4555 | tmp = ret->children; |
| 4556 | while (tmp != NULL) { |
| 4557 | if (tmp->next == NULL) |
| 4558 | ret->last = tmp; |
| 4559 | tmp = tmp->next; |
| 4560 | } |
| 4561 | } |
| 4562 | return(ret); |
| 4563 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4564 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4565 | |
| 4566 | /************************************************************************ |
| 4567 | * * |
| 4568 | * Content access functions * |
| 4569 | * * |
| 4570 | ************************************************************************/ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4571 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4572 | /** |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4573 | * xmlGetLineNoInternal: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4574 | * @node: valid node |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4575 | * @depth: used to limit any risk of recursion |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4576 | * |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4577 | * Get line number of @node. |
| 4578 | * 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] | 4579 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4580 | * Returns the line number if successful, -1 otherwise |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4581 | */ |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4582 | static long |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 4583 | xmlGetLineNoInternal(const xmlNode *node, int depth) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4584 | { |
| 4585 | long result = -1; |
| 4586 | |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4587 | if (depth >= 5) |
| 4588 | return(-1); |
| 4589 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4590 | if (!node) |
| 4591 | return result; |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4592 | if ((node->type == XML_ELEMENT_NODE) || |
| 4593 | (node->type == XML_TEXT_NODE) || |
| 4594 | (node->type == XML_COMMENT_NODE) || |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4595 | (node->type == XML_PI_NODE)) { |
| 4596 | if (node->line == 65535) { |
| 4597 | if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL)) |
| 4598 | result = (long) node->psvi; |
| 4599 | else if ((node->type == XML_ELEMENT_NODE) && |
| 4600 | (node->children != NULL)) |
| 4601 | result = xmlGetLineNoInternal(node->children, depth + 1); |
| 4602 | else if (node->next != NULL) |
| 4603 | result = xmlGetLineNoInternal(node->next, depth + 1); |
| 4604 | else if (node->prev != NULL) |
| 4605 | result = xmlGetLineNoInternal(node->prev, depth + 1); |
| 4606 | } |
| 4607 | if ((result == -1) || (result == 65535)) |
| 4608 | result = (long) node->line; |
| 4609 | } else if ((node->prev != NULL) && |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4610 | ((node->prev->type == XML_ELEMENT_NODE) || |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4611 | (node->prev->type == XML_TEXT_NODE) || |
| 4612 | (node->prev->type == XML_COMMENT_NODE) || |
| 4613 | (node->prev->type == XML_PI_NODE))) |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4614 | result = xmlGetLineNoInternal(node->prev, depth + 1); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4615 | else if ((node->parent != NULL) && |
Daniel Veillard | 73da77e | 2005-08-24 14:05:37 +0000 | [diff] [blame] | 4616 | (node->parent->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4617 | result = xmlGetLineNoInternal(node->parent, depth + 1); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4618 | |
| 4619 | return result; |
| 4620 | } |
| 4621 | |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4622 | /** |
| 4623 | * xmlGetLineNo: |
| 4624 | * @node: valid node |
| 4625 | * |
| 4626 | * Get line number of @node. |
| 4627 | * Try to override the limitation of lines being store in 16 bits ints |
| 4628 | * if XML_PARSE_BIG_LINES parser option was used |
| 4629 | * |
| 4630 | * Returns the line number if successful, -1 otherwise |
| 4631 | */ |
| 4632 | long |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 4633 | xmlGetLineNo(const xmlNode *node) |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 4634 | { |
| 4635 | return(xmlGetLineNoInternal(node, 0)); |
| 4636 | } |
| 4637 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4638 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4639 | /** |
| 4640 | * xmlGetNodePath: |
| 4641 | * @node: a node |
| 4642 | * |
| 4643 | * Build a structure based Path for the given node |
| 4644 | * |
| 4645 | * Returns the new path or NULL in case of error. The caller must free |
| 4646 | * the returned string |
| 4647 | */ |
| 4648 | xmlChar * |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 4649 | xmlGetNodePath(const xmlNode *node) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4650 | { |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 4651 | const xmlNode *cur, *tmp, *next; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4652 | xmlChar *buffer = NULL, *temp; |
| 4653 | size_t buf_len; |
| 4654 | xmlChar *buf; |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 4655 | const char *sep; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4656 | const char *name; |
| 4657 | char nametemp[100]; |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4658 | int occur = 0, generic; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4659 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 4660 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4661 | return (NULL); |
| 4662 | |
| 4663 | buf_len = 500; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4664 | buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4665 | if (buffer == NULL) { |
| 4666 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4667 | return (NULL); |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4668 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4669 | buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar)); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4670 | if (buf == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4671 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4672 | xmlFree(buffer); |
| 4673 | return (NULL); |
| 4674 | } |
| 4675 | |
| 4676 | buffer[0] = 0; |
| 4677 | cur = node; |
| 4678 | do { |
| 4679 | name = ""; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4680 | sep = "?"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4681 | occur = 0; |
| 4682 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 4683 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 4684 | if (buffer[0] == '/') |
| 4685 | break; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4686 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4687 | next = NULL; |
| 4688 | } else if (cur->type == XML_ELEMENT_NODE) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4689 | generic = 0; |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4690 | sep = "/"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4691 | name = (const char *) cur->name; |
| 4692 | if (cur->ns) { |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4693 | if (cur->ns->prefix != NULL) { |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4694 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4695 | (char *)cur->ns->prefix, (char *)cur->name); |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4696 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4697 | name = nametemp; |
| 4698 | } else { |
| 4699 | /* |
| 4700 | * We cannot express named elements in the default |
| 4701 | * namespace, so use "*". |
| 4702 | */ |
| 4703 | generic = 1; |
| 4704 | name = "*"; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4705 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4706 | } |
| 4707 | next = cur->parent; |
| 4708 | |
| 4709 | /* |
| 4710 | * Thumbler index computation |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4711 | * TODO: the ocurence test seems bogus for namespaced names |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4712 | */ |
| 4713 | tmp = cur->prev; |
| 4714 | while (tmp != NULL) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 4715 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4716 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4717 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4718 | ((tmp->ns == cur->ns) || |
| 4719 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4720 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4721 | occur++; |
| 4722 | tmp = tmp->prev; |
| 4723 | } |
| 4724 | if (occur == 0) { |
| 4725 | tmp = cur->next; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4726 | while (tmp != NULL && occur == 0) { |
| 4727 | if ((tmp->type == XML_ELEMENT_NODE) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4728 | (generic || |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4729 | (xmlStrEqual(cur->name, tmp->name) && |
Kasimier T. Buchcik | d38c63f | 2006-06-12 10:58:24 +0000 | [diff] [blame] | 4730 | ((tmp->ns == cur->ns) || |
| 4731 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
Kasimier T. Buchcik | 43ceb1e | 2006-06-12 11:08:18 +0000 | [diff] [blame] | 4732 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4733 | occur++; |
| 4734 | tmp = tmp->next; |
| 4735 | } |
| 4736 | if (occur != 0) |
| 4737 | occur = 1; |
| 4738 | } else |
| 4739 | occur++; |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4740 | } else if (cur->type == XML_COMMENT_NODE) { |
| 4741 | sep = "/"; |
| 4742 | name = "comment()"; |
| 4743 | next = cur->parent; |
| 4744 | |
| 4745 | /* |
| 4746 | * Thumbler index computation |
| 4747 | */ |
| 4748 | tmp = cur->prev; |
| 4749 | while (tmp != NULL) { |
| 4750 | if (tmp->type == XML_COMMENT_NODE) |
| 4751 | occur++; |
| 4752 | tmp = tmp->prev; |
| 4753 | } |
| 4754 | if (occur == 0) { |
| 4755 | tmp = cur->next; |
| 4756 | while (tmp != NULL && occur == 0) { |
| 4757 | if (tmp->type == XML_COMMENT_NODE) |
| 4758 | occur++; |
| 4759 | tmp = tmp->next; |
| 4760 | } |
| 4761 | if (occur != 0) |
| 4762 | occur = 1; |
| 4763 | } else |
| 4764 | occur++; |
| 4765 | } else if ((cur->type == XML_TEXT_NODE) || |
| 4766 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 4767 | sep = "/"; |
| 4768 | name = "text()"; |
| 4769 | next = cur->parent; |
| 4770 | |
| 4771 | /* |
| 4772 | * Thumbler index computation |
| 4773 | */ |
| 4774 | tmp = cur->prev; |
| 4775 | while (tmp != NULL) { |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4776 | if ((tmp->type == XML_TEXT_NODE) || |
| 4777 | (tmp->type == XML_CDATA_SECTION_NODE)) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4778 | occur++; |
| 4779 | tmp = tmp->prev; |
| 4780 | } |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4781 | /* |
| 4782 | * Evaluate if this is the only text- or CDATA-section-node; |
| 4783 | * if yes, then we'll get "text()", otherwise "text()[1]". |
| 4784 | */ |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4785 | if (occur == 0) { |
| 4786 | tmp = cur->next; |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4787 | while (tmp != NULL) { |
| 4788 | if ((tmp->type == XML_TEXT_NODE) || |
| 4789 | (tmp->type == XML_CDATA_SECTION_NODE)) |
| 4790 | { |
| 4791 | occur = 1; |
| 4792 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4793 | } |
Kasimier T. Buchcik | eb46870 | 2006-02-15 10:57:50 +0000 | [diff] [blame] | 4794 | tmp = tmp->next; |
| 4795 | } |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4796 | } else |
| 4797 | occur++; |
| 4798 | } else if (cur->type == XML_PI_NODE) { |
| 4799 | sep = "/"; |
| 4800 | snprintf(nametemp, sizeof(nametemp) - 1, |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4801 | "processing-instruction('%s')", (char *)cur->name); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 4802 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4803 | name = nametemp; |
| 4804 | |
| 4805 | next = cur->parent; |
| 4806 | |
| 4807 | /* |
| 4808 | * Thumbler index computation |
| 4809 | */ |
| 4810 | tmp = cur->prev; |
| 4811 | while (tmp != NULL) { |
| 4812 | if ((tmp->type == XML_PI_NODE) && |
| 4813 | (xmlStrEqual(cur->name, tmp->name))) |
| 4814 | occur++; |
| 4815 | tmp = tmp->prev; |
| 4816 | } |
| 4817 | if (occur == 0) { |
| 4818 | tmp = cur->next; |
| 4819 | while (tmp != NULL && occur == 0) { |
| 4820 | if ((tmp->type == XML_PI_NODE) && |
| 4821 | (xmlStrEqual(cur->name, tmp->name))) |
| 4822 | occur++; |
| 4823 | tmp = tmp->next; |
| 4824 | } |
| 4825 | if (occur != 0) |
| 4826 | occur = 1; |
| 4827 | } else |
| 4828 | occur++; |
| 4829 | |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4830 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4831 | sep = "/@"; |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4832 | name = (const char *) (((xmlAttrPtr) cur)->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4833 | if (cur->ns) { |
| 4834 | if (cur->ns->prefix != NULL) |
| 4835 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4836 | (char *)cur->ns->prefix, (char *)cur->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4837 | else |
| 4838 | snprintf(nametemp, sizeof(nametemp) - 1, "%s", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4839 | (char *)cur->name); |
Daniel Veillard | 365c806 | 2005-07-19 11:31:55 +0000 | [diff] [blame] | 4840 | nametemp[sizeof(nametemp) - 1] = 0; |
| 4841 | name = nametemp; |
| 4842 | } |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4843 | next = ((xmlAttrPtr) cur)->parent; |
| 4844 | } else { |
| 4845 | next = cur->parent; |
| 4846 | } |
| 4847 | |
| 4848 | /* |
| 4849 | * Make sure there is enough room |
| 4850 | */ |
| 4851 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
| 4852 | buf_len = |
| 4853 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
| 4854 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
| 4855 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4856 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4857 | xmlFree(buf); |
| 4858 | xmlFree(buffer); |
| 4859 | return (NULL); |
| 4860 | } |
| 4861 | buffer = temp; |
| 4862 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
| 4863 | if (temp == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 4864 | xmlTreeErrMemory("getting node path"); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4865 | xmlFree(buf); |
| 4866 | xmlFree(buffer); |
| 4867 | return (NULL); |
| 4868 | } |
| 4869 | buf = temp; |
| 4870 | } |
| 4871 | if (occur == 0) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4872 | snprintf((char *) buf, buf_len, "%s%s%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4873 | sep, name, (char *) buffer); |
| 4874 | else |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 4875 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4876 | sep, name, occur, (char *) buffer); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 4877 | snprintf((char *) buffer, buf_len, "%s", (char *)buf); |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4878 | cur = next; |
| 4879 | } while (cur != NULL); |
| 4880 | xmlFree(buf); |
| 4881 | return (buffer); |
| 4882 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4883 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 8faa783 | 2001-11-26 15:58:08 +0000 | [diff] [blame] | 4884 | |
| 4885 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4886 | * xmlDocGetRootElement: |
| 4887 | * @doc: the document |
| 4888 | * |
| 4889 | * Get the root element of the document (doc->children is a list |
| 4890 | * containing possibly comments, PIs, etc ...). |
| 4891 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4892 | * Returns the #xmlNodePtr for the root or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4893 | */ |
| 4894 | xmlNodePtr |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 4895 | xmlDocGetRootElement(const xmlDoc *doc) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4896 | xmlNodePtr ret; |
| 4897 | |
| 4898 | if (doc == NULL) return(NULL); |
| 4899 | ret = doc->children; |
| 4900 | while (ret != NULL) { |
| 4901 | if (ret->type == XML_ELEMENT_NODE) |
| 4902 | return(ret); |
| 4903 | ret = ret->next; |
| 4904 | } |
| 4905 | return(ret); |
| 4906 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4907 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4908 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4909 | /** |
| 4910 | * xmlDocSetRootElement: |
| 4911 | * @doc: the document |
Daniel Veillard | 26a45c8 | 2006-10-20 12:55:34 +0000 | [diff] [blame] | 4912 | * @root: the new document root element, if root is NULL no action is taken, |
| 4913 | * to remove a node from a document use xmlUnlinkNode(root) instead. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4914 | * |
| 4915 | * Set the root element of the document (doc->children is a list |
| 4916 | * containing possibly comments, PIs, etc ...). |
| 4917 | * |
Daniel Veillard | 26a45c8 | 2006-10-20 12:55:34 +0000 | [diff] [blame] | 4918 | * 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] | 4919 | */ |
| 4920 | xmlNodePtr |
| 4921 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 4922 | xmlNodePtr old = NULL; |
| 4923 | |
| 4924 | if (doc == NULL) return(NULL); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 4925 | if ((root == NULL) || (root->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4926 | return(NULL); |
| 4927 | xmlUnlinkNode(root); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 4928 | xmlSetTreeDoc(root, doc); |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 4929 | root->parent = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4930 | old = doc->children; |
| 4931 | while (old != NULL) { |
| 4932 | if (old->type == XML_ELEMENT_NODE) |
| 4933 | break; |
| 4934 | old = old->next; |
| 4935 | } |
| 4936 | if (old == NULL) { |
| 4937 | if (doc->children == NULL) { |
| 4938 | doc->children = root; |
| 4939 | doc->last = root; |
| 4940 | } else { |
| 4941 | xmlAddSibling(doc->children, root); |
| 4942 | } |
| 4943 | } else { |
| 4944 | xmlReplaceNode(old, root); |
| 4945 | } |
| 4946 | return(old); |
| 4947 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4948 | #endif |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4949 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 4950 | #if defined(LIBXML_TREE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4951 | /** |
| 4952 | * xmlNodeSetLang: |
| 4953 | * @cur: the node being changed |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 4954 | * @lang: the language description |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4955 | * |
| 4956 | * Set the language of a node, i.e. the values of the xml:lang |
| 4957 | * attribute. |
| 4958 | */ |
| 4959 | void |
| 4960 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4961 | xmlNsPtr ns; |
| 4962 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4963 | if (cur == NULL) return; |
| 4964 | switch(cur->type) { |
| 4965 | case XML_TEXT_NODE: |
| 4966 | case XML_CDATA_SECTION_NODE: |
| 4967 | case XML_COMMENT_NODE: |
| 4968 | case XML_DOCUMENT_NODE: |
| 4969 | case XML_DOCUMENT_TYPE_NODE: |
| 4970 | case XML_DOCUMENT_FRAG_NODE: |
| 4971 | case XML_NOTATION_NODE: |
| 4972 | case XML_HTML_DOCUMENT_NODE: |
| 4973 | case XML_DTD_NODE: |
| 4974 | case XML_ELEMENT_DECL: |
| 4975 | case XML_ATTRIBUTE_DECL: |
| 4976 | case XML_ENTITY_DECL: |
| 4977 | case XML_PI_NODE: |
| 4978 | case XML_ENTITY_REF_NODE: |
| 4979 | case XML_ENTITY_NODE: |
| 4980 | case XML_NAMESPACE_DECL: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4981 | #ifdef LIBXML_DOCB_ENABLED |
| 4982 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4983 | #endif |
| 4984 | case XML_XINCLUDE_START: |
| 4985 | case XML_XINCLUDE_END: |
| 4986 | return; |
| 4987 | case XML_ELEMENT_NODE: |
| 4988 | case XML_ATTRIBUTE_NODE: |
| 4989 | break; |
| 4990 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 4991 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 4992 | if (ns == NULL) |
| 4993 | return; |
| 4994 | xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4995 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4996 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 4997 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4998 | /** |
| 4999 | * xmlNodeGetLang: |
| 5000 | * @cur: the node being checked |
| 5001 | * |
| 5002 | * Searches the language of a node, i.e. the values of the xml:lang |
| 5003 | * attribute or the one carried by the nearest ancestor. |
| 5004 | * |
| 5005 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5006 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5007 | */ |
| 5008 | xmlChar * |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 5009 | xmlNodeGetLang(const xmlNode *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5010 | xmlChar *lang; |
| 5011 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 5012 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) |
| 5013 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5014 | while (cur != NULL) { |
Daniel Veillard | c17337c | 2001-05-09 10:51:31 +0000 | [diff] [blame] | 5015 | lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5016 | if (lang != NULL) |
| 5017 | return(lang); |
| 5018 | cur = cur->parent; |
| 5019 | } |
| 5020 | return(NULL); |
| 5021 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5022 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5023 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5024 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5025 | /** |
| 5026 | * xmlNodeSetSpacePreserve: |
| 5027 | * @cur: the node being changed |
| 5028 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 5029 | * |
| 5030 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 5031 | * value of the xml:space attribute. |
| 5032 | */ |
| 5033 | void |
| 5034 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5035 | xmlNsPtr ns; |
| 5036 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5037 | if (cur == NULL) return; |
| 5038 | switch(cur->type) { |
| 5039 | case XML_TEXT_NODE: |
| 5040 | case XML_CDATA_SECTION_NODE: |
| 5041 | case XML_COMMENT_NODE: |
| 5042 | case XML_DOCUMENT_NODE: |
| 5043 | case XML_DOCUMENT_TYPE_NODE: |
| 5044 | case XML_DOCUMENT_FRAG_NODE: |
| 5045 | case XML_NOTATION_NODE: |
| 5046 | case XML_HTML_DOCUMENT_NODE: |
| 5047 | case XML_DTD_NODE: |
| 5048 | case XML_ELEMENT_DECL: |
| 5049 | case XML_ATTRIBUTE_DECL: |
| 5050 | case XML_ENTITY_DECL: |
| 5051 | case XML_PI_NODE: |
| 5052 | case XML_ENTITY_REF_NODE: |
| 5053 | case XML_ENTITY_NODE: |
| 5054 | case XML_NAMESPACE_DECL: |
| 5055 | case XML_XINCLUDE_START: |
| 5056 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5057 | #ifdef LIBXML_DOCB_ENABLED |
| 5058 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5059 | #endif |
| 5060 | return; |
| 5061 | case XML_ELEMENT_NODE: |
| 5062 | case XML_ATTRIBUTE_NODE: |
| 5063 | break; |
| 5064 | } |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5065 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 5066 | if (ns == NULL) |
| 5067 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5068 | switch (val) { |
| 5069 | case 0: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5070 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5071 | break; |
| 5072 | case 1: |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5073 | xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5074 | break; |
| 5075 | } |
| 5076 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5077 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5078 | |
| 5079 | /** |
| 5080 | * xmlNodeGetSpacePreserve: |
| 5081 | * @cur: the node being checked |
| 5082 | * |
| 5083 | * Searches the space preserving behaviour of a node, i.e. the values |
| 5084 | * of the xml:space attribute or the one carried by the nearest |
| 5085 | * ancestor. |
| 5086 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5087 | * 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] | 5088 | */ |
| 5089 | int |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 5090 | xmlNodeGetSpacePreserve(const xmlNode *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5091 | xmlChar *space; |
| 5092 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 5093 | if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) |
| 5094 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5095 | while (cur != NULL) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5096 | space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5097 | if (space != NULL) { |
| 5098 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
| 5099 | xmlFree(space); |
| 5100 | return(1); |
| 5101 | } |
| 5102 | if (xmlStrEqual(space, BAD_CAST "default")) { |
| 5103 | xmlFree(space); |
| 5104 | return(0); |
| 5105 | } |
| 5106 | xmlFree(space); |
| 5107 | } |
| 5108 | cur = cur->parent; |
| 5109 | } |
| 5110 | return(-1); |
| 5111 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5112 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5113 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5114 | /** |
| 5115 | * xmlNodeSetName: |
| 5116 | * @cur: the node being changed |
| 5117 | * @name: the new tag name |
| 5118 | * |
| 5119 | * Set (or reset) the name of a node. |
| 5120 | */ |
| 5121 | void |
| 5122 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5123 | xmlDocPtr doc; |
| 5124 | xmlDictPtr dict; |
Tristan Van Berkom | f0dd6e1 | 2014-04-22 21:15:05 -0400 | [diff] [blame] | 5125 | const xmlChar *freeme = NULL; |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5126 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5127 | if (cur == NULL) return; |
| 5128 | if (name == NULL) return; |
| 5129 | switch(cur->type) { |
| 5130 | case XML_TEXT_NODE: |
| 5131 | case XML_CDATA_SECTION_NODE: |
| 5132 | case XML_COMMENT_NODE: |
| 5133 | case XML_DOCUMENT_TYPE_NODE: |
| 5134 | case XML_DOCUMENT_FRAG_NODE: |
| 5135 | case XML_NOTATION_NODE: |
| 5136 | case XML_HTML_DOCUMENT_NODE: |
| 5137 | case XML_NAMESPACE_DECL: |
| 5138 | case XML_XINCLUDE_START: |
| 5139 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5140 | #ifdef LIBXML_DOCB_ENABLED |
| 5141 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5142 | #endif |
| 5143 | return; |
| 5144 | case XML_ELEMENT_NODE: |
| 5145 | case XML_ATTRIBUTE_NODE: |
| 5146 | case XML_PI_NODE: |
| 5147 | case XML_ENTITY_REF_NODE: |
| 5148 | case XML_ENTITY_NODE: |
| 5149 | case XML_DTD_NODE: |
| 5150 | case XML_DOCUMENT_NODE: |
| 5151 | case XML_ELEMENT_DECL: |
| 5152 | case XML_ATTRIBUTE_DECL: |
| 5153 | case XML_ENTITY_DECL: |
| 5154 | break; |
| 5155 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5156 | doc = cur->doc; |
| 5157 | if (doc != NULL) |
| 5158 | dict = doc->dict; |
| 5159 | else |
| 5160 | dict = NULL; |
| 5161 | if (dict != NULL) { |
| 5162 | if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name))) |
Tristan Van Berkom | f0dd6e1 | 2014-04-22 21:15:05 -0400 | [diff] [blame] | 5163 | freeme = cur->name; |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5164 | cur->name = xmlDictLookup(dict, name, -1); |
| 5165 | } else { |
Tristan Van Berkom | f0dd6e1 | 2014-04-22 21:15:05 -0400 | [diff] [blame] | 5166 | if (cur->name != NULL) |
| 5167 | freeme = cur->name; |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 5168 | cur->name = xmlStrdup(name); |
| 5169 | } |
Tristan Van Berkom | f0dd6e1 | 2014-04-22 21:15:05 -0400 | [diff] [blame] | 5170 | |
| 5171 | if (freeme) |
| 5172 | xmlFree((xmlChar *) freeme); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5173 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 5174 | #endif |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5175 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 5176 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5177 | /** |
| 5178 | * xmlNodeSetBase: |
| 5179 | * @cur: the node being changed |
| 5180 | * @uri: the new base URI |
| 5181 | * |
| 5182 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 5183 | * xml:base attribute. |
| 5184 | */ |
| 5185 | void |
Daniel Veillard | f85ce8e | 2003-09-22 10:24:45 +0000 | [diff] [blame] | 5186 | xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5187 | xmlNsPtr ns; |
Martin Trappel | f370310 | 2010-01-22 12:08:00 +0100 | [diff] [blame] | 5188 | xmlChar* fixed; |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5189 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5190 | if (cur == NULL) return; |
| 5191 | switch(cur->type) { |
| 5192 | case XML_TEXT_NODE: |
| 5193 | case XML_CDATA_SECTION_NODE: |
| 5194 | case XML_COMMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5195 | case XML_DOCUMENT_TYPE_NODE: |
| 5196 | case XML_DOCUMENT_FRAG_NODE: |
| 5197 | case XML_NOTATION_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5198 | case XML_DTD_NODE: |
| 5199 | case XML_ELEMENT_DECL: |
| 5200 | case XML_ATTRIBUTE_DECL: |
| 5201 | case XML_ENTITY_DECL: |
| 5202 | case XML_PI_NODE: |
| 5203 | case XML_ENTITY_REF_NODE: |
| 5204 | case XML_ENTITY_NODE: |
| 5205 | case XML_NAMESPACE_DECL: |
| 5206 | case XML_XINCLUDE_START: |
| 5207 | case XML_XINCLUDE_END: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5208 | return; |
| 5209 | case XML_ELEMENT_NODE: |
| 5210 | case XML_ATTRIBUTE_NODE: |
| 5211 | break; |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 5212 | case XML_DOCUMENT_NODE: |
| 5213 | #ifdef LIBXML_DOCB_ENABLED |
| 5214 | case XML_DOCB_DOCUMENT_NODE: |
| 5215 | #endif |
| 5216 | case XML_HTML_DOCUMENT_NODE: { |
| 5217 | xmlDocPtr doc = (xmlDocPtr) cur; |
| 5218 | |
| 5219 | if (doc->URL != NULL) |
| 5220 | xmlFree((xmlChar *) doc->URL); |
| 5221 | if (uri == NULL) |
| 5222 | doc->URL = NULL; |
| 5223 | else |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5224 | doc->URL = xmlPathToURI(uri); |
Daniel Veillard | 4cbe470 | 2002-05-05 06:57:27 +0000 | [diff] [blame] | 5225 | return; |
| 5226 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5227 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5228 | |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 5229 | ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); |
| 5230 | if (ns == NULL) |
| 5231 | return; |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5232 | fixed = xmlPathToURI(uri); |
| 5233 | if (fixed != NULL) { |
| 5234 | xmlSetNsProp(cur, ns, BAD_CAST "base", fixed); |
Martin Trappel | f370310 | 2010-01-22 12:08:00 +0100 | [diff] [blame] | 5235 | xmlFree(fixed); |
Daniel Veillard | b8efdda | 2006-10-10 12:37:14 +0000 | [diff] [blame] | 5236 | } else { |
| 5237 | xmlSetNsProp(cur, ns, BAD_CAST "base", uri); |
| 5238 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5239 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5240 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5241 | |
| 5242 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5243 | * xmlNodeGetBase: |
| 5244 | * @doc: the document the node pertains to |
| 5245 | * @cur: the node being checked |
| 5246 | * |
| 5247 | * Searches for the BASE URL. The code should work on both XML |
| 5248 | * and HTML document even if base mechanisms are completely different. |
| 5249 | * It returns the base as defined in RFC 2396 sections |
| 5250 | * 5.1.1. Base URI within Document Content |
| 5251 | * and |
| 5252 | * 5.1.2. Base URI from the Encapsulating Entity |
| 5253 | * However it does not return the document base (5.1.3), use |
Daniel Veillard | e4d1849 | 2010-03-09 11:12:30 +0100 | [diff] [blame] | 5254 | * doc->URL in this case |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5255 | * |
| 5256 | * Returns a pointer to the base URL, or NULL if not found |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5257 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5258 | */ |
| 5259 | xmlChar * |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 5260 | xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5261 | xmlChar *oldbase = NULL; |
| 5262 | xmlChar *base, *newbase; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5263 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5264 | if ((cur == NULL) && (doc == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5265 | return(NULL); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 5266 | if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) |
| 5267 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5268 | if (doc == NULL) doc = cur->doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5269 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
| 5270 | cur = doc->children; |
| 5271 | while ((cur != NULL) && (cur->name != NULL)) { |
| 5272 | if (cur->type != XML_ELEMENT_NODE) { |
| 5273 | cur = cur->next; |
| 5274 | continue; |
| 5275 | } |
| 5276 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
| 5277 | cur = cur->children; |
| 5278 | continue; |
| 5279 | } |
| 5280 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
| 5281 | cur = cur->children; |
| 5282 | continue; |
| 5283 | } |
| 5284 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 5285 | return(xmlGetProp(cur, BAD_CAST "href")); |
| 5286 | } |
| 5287 | cur = cur->next; |
| 5288 | } |
| 5289 | return(NULL); |
| 5290 | } |
| 5291 | while (cur != NULL) { |
| 5292 | if (cur->type == XML_ENTITY_DECL) { |
| 5293 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 5294 | return(xmlStrdup(ent->URI)); |
| 5295 | } |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5296 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5297 | base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5298 | if (base != NULL) { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5299 | if (oldbase != NULL) { |
| 5300 | newbase = xmlBuildURI(oldbase, base); |
| 5301 | if (newbase != NULL) { |
| 5302 | xmlFree(oldbase); |
| 5303 | xmlFree(base); |
| 5304 | oldbase = newbase; |
| 5305 | } else { |
| 5306 | xmlFree(oldbase); |
| 5307 | xmlFree(base); |
| 5308 | return(NULL); |
| 5309 | } |
| 5310 | } else { |
| 5311 | oldbase = base; |
| 5312 | } |
| 5313 | if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) || |
| 5314 | (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) || |
| 5315 | (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4))) |
| 5316 | return(oldbase); |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 5317 | } |
| 5318 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5319 | cur = cur->parent; |
| 5320 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 5321 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 5322 | if (oldbase == NULL) |
| 5323 | return(xmlStrdup(doc->URL)); |
| 5324 | newbase = xmlBuildURI(oldbase, doc->URL); |
| 5325 | xmlFree(oldbase); |
| 5326 | return(newbase); |
| 5327 | } |
| 5328 | return(oldbase); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5329 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5330 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5331 | /** |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5332 | * xmlNodeBufGetContent: |
| 5333 | * @buffer: a buffer |
| 5334 | * @cur: the node being read |
| 5335 | * |
| 5336 | * Read the value of a node @cur, this can be either the text carried |
| 5337 | * directly by this node if it's a TEXT node or the aggregate string |
| 5338 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 5339 | * Entity references are substituted. |
| 5340 | * Fills up the buffer @buffer with this value |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5341 | * |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5342 | * Returns 0 in case of success and -1 in case of error. |
| 5343 | */ |
| 5344 | int |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 5345 | xmlNodeBufGetContent(xmlBufferPtr buffer, const xmlNode *cur) |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5346 | { |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5347 | xmlBufPtr buf; |
| 5348 | int ret; |
| 5349 | |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5350 | if ((cur == NULL) || (buffer == NULL)) return(-1); |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5351 | buf = xmlBufFromBuffer(buffer); |
| 5352 | ret = xmlBufGetNodeContent(buf, cur); |
| 5353 | buffer = xmlBufBackToBuffer(buf); |
| 5354 | if ((ret < 0) || (buffer == NULL)) |
| 5355 | return(-1); |
| 5356 | return(0); |
| 5357 | } |
| 5358 | |
| 5359 | /** |
| 5360 | * xmlBufGetNodeContent: |
Daniel Veillard | 28cc42d | 2012-08-10 10:00:18 +0800 | [diff] [blame] | 5361 | * @buf: a buffer xmlBufPtr |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5362 | * @cur: the node being read |
| 5363 | * |
| 5364 | * Read the value of a node @cur, this can be either the text carried |
| 5365 | * directly by this node if it's a TEXT node or the aggregate string |
| 5366 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 5367 | * Entity references are substituted. |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 5368 | * Fills up the buffer @buf with this value |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5369 | * |
| 5370 | * Returns 0 in case of success and -1 in case of error. |
| 5371 | */ |
| 5372 | int |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 5373 | xmlBufGetNodeContent(xmlBufPtr buf, const xmlNode *cur) |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5374 | { |
| 5375 | if ((cur == NULL) || (buf == NULL)) return(-1); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5376 | switch (cur->type) { |
| 5377 | case XML_CDATA_SECTION_NODE: |
| 5378 | case XML_TEXT_NODE: |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5379 | xmlBufCat(buf, cur->content); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5380 | break; |
| 5381 | case XML_DOCUMENT_FRAG_NODE: |
| 5382 | case XML_ELEMENT_NODE:{ |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 5383 | const xmlNode *tmp = cur; |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5384 | |
| 5385 | while (tmp != NULL) { |
| 5386 | switch (tmp->type) { |
| 5387 | case XML_CDATA_SECTION_NODE: |
| 5388 | case XML_TEXT_NODE: |
| 5389 | if (tmp->content != NULL) |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5390 | xmlBufCat(buf, tmp->content); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5391 | break; |
| 5392 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5393 | xmlBufGetNodeContent(buf, tmp); |
Rob Richards | 77b92ff | 2005-12-20 15:55:14 +0000 | [diff] [blame] | 5394 | break; |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5395 | default: |
| 5396 | break; |
| 5397 | } |
| 5398 | /* |
| 5399 | * Skip to next node |
| 5400 | */ |
| 5401 | if (tmp->children != NULL) { |
| 5402 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 5403 | tmp = tmp->children; |
| 5404 | continue; |
| 5405 | } |
| 5406 | } |
| 5407 | if (tmp == cur) |
| 5408 | break; |
| 5409 | |
| 5410 | if (tmp->next != NULL) { |
| 5411 | tmp = tmp->next; |
| 5412 | continue; |
| 5413 | } |
| 5414 | |
| 5415 | do { |
| 5416 | tmp = tmp->parent; |
| 5417 | if (tmp == NULL) |
| 5418 | break; |
| 5419 | if (tmp == cur) { |
| 5420 | tmp = NULL; |
| 5421 | break; |
| 5422 | } |
| 5423 | if (tmp->next != NULL) { |
| 5424 | tmp = tmp->next; |
| 5425 | break; |
| 5426 | } |
| 5427 | } while (tmp != NULL); |
| 5428 | } |
| 5429 | break; |
| 5430 | } |
| 5431 | case XML_ATTRIBUTE_NODE:{ |
| 5432 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 5433 | xmlNodePtr tmp = attr->children; |
| 5434 | |
| 5435 | while (tmp != NULL) { |
| 5436 | if (tmp->type == XML_TEXT_NODE) |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5437 | xmlBufCat(buf, tmp->content); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5438 | else |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5439 | xmlBufGetNodeContent(buf, tmp); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5440 | tmp = tmp->next; |
| 5441 | } |
| 5442 | break; |
| 5443 | } |
| 5444 | case XML_COMMENT_NODE: |
| 5445 | case XML_PI_NODE: |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5446 | xmlBufCat(buf, cur->content); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5447 | break; |
| 5448 | case XML_ENTITY_REF_NODE:{ |
| 5449 | xmlEntityPtr ent; |
| 5450 | xmlNodePtr tmp; |
| 5451 | |
| 5452 | /* lookup entity declaration */ |
| 5453 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5454 | if (ent == NULL) |
| 5455 | return(-1); |
| 5456 | |
| 5457 | /* an entity content can be any "well balanced chunk", |
| 5458 | * i.e. the result of the content [43] production: |
| 5459 | * http://www.w3.org/TR/REC-xml#NT-content |
| 5460 | * -> we iterate through child nodes and recursive call |
| 5461 | * xmlNodeGetContent() which handles all possible node types */ |
| 5462 | tmp = ent->children; |
| 5463 | while (tmp) { |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5464 | xmlBufGetNodeContent(buf, tmp); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5465 | tmp = tmp->next; |
| 5466 | } |
| 5467 | break; |
| 5468 | } |
| 5469 | case XML_ENTITY_NODE: |
| 5470 | case XML_DOCUMENT_TYPE_NODE: |
| 5471 | case XML_NOTATION_NODE: |
| 5472 | case XML_DTD_NODE: |
| 5473 | case XML_XINCLUDE_START: |
| 5474 | case XML_XINCLUDE_END: |
| 5475 | break; |
| 5476 | case XML_DOCUMENT_NODE: |
| 5477 | #ifdef LIBXML_DOCB_ENABLED |
| 5478 | case XML_DOCB_DOCUMENT_NODE: |
| 5479 | #endif |
| 5480 | case XML_HTML_DOCUMENT_NODE: |
| 5481 | cur = cur->children; |
| 5482 | while (cur!= NULL) { |
| 5483 | if ((cur->type == XML_ELEMENT_NODE) || |
| 5484 | (cur->type == XML_TEXT_NODE) || |
| 5485 | (cur->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5486 | xmlBufGetNodeContent(buf, cur); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5487 | } |
| 5488 | cur = cur->next; |
| 5489 | } |
| 5490 | break; |
| 5491 | case XML_NAMESPACE_DECL: |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5492 | xmlBufCat(buf, ((xmlNsPtr) cur)->href); |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5493 | break; |
| 5494 | case XML_ELEMENT_DECL: |
| 5495 | case XML_ATTRIBUTE_DECL: |
| 5496 | case XML_ENTITY_DECL: |
| 5497 | break; |
| 5498 | } |
| 5499 | return(0); |
| 5500 | } |
Daniel Veillard | dddeede | 2012-07-16 14:44:26 +0800 | [diff] [blame] | 5501 | |
Daniel Veillard | 7869729 | 2003-10-19 20:44:43 +0000 | [diff] [blame] | 5502 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5503 | * xmlNodeGetContent: |
| 5504 | * @cur: the node being read |
| 5505 | * |
| 5506 | * Read the value of a node, this can be either the text carried |
| 5507 | * directly by this node if it's a TEXT node or the aggregate string |
| 5508 | * 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] | 5509 | * Entity references are substituted. |
| 5510 | * Returns a new #xmlChar * or NULL if no content is available. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5511 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5512 | */ |
| 5513 | xmlChar * |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 5514 | xmlNodeGetContent(const xmlNode *cur) |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5515 | { |
| 5516 | if (cur == NULL) |
| 5517 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5518 | switch (cur->type) { |
| 5519 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5520 | case XML_ELEMENT_NODE:{ |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5521 | xmlBufPtr buf; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5522 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5523 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5524 | buf = xmlBufCreateSize(64); |
| 5525 | if (buf == NULL) |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5526 | return (NULL); |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5527 | xmlBufGetNodeContent(buf, cur); |
| 5528 | ret = xmlBufDetach(buf); |
| 5529 | xmlBufFree(buf); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5530 | return (ret); |
| 5531 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5532 | case XML_ATTRIBUTE_NODE: |
| 5533 | return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5534 | case XML_COMMENT_NODE: |
| 5535 | case XML_PI_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5536 | if (cur->content != NULL) |
| 5537 | return (xmlStrdup(cur->content)); |
| 5538 | return (NULL); |
| 5539 | case XML_ENTITY_REF_NODE:{ |
| 5540 | xmlEntityPtr ent; |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5541 | xmlBufPtr buf; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5542 | xmlChar *ret; |
| 5543 | |
| 5544 | /* lookup entity declaration */ |
| 5545 | ent = xmlGetDocEntity(cur->doc, cur->name); |
| 5546 | if (ent == NULL) |
| 5547 | return (NULL); |
| 5548 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5549 | buf = xmlBufCreate(); |
| 5550 | if (buf == NULL) |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5551 | return (NULL); |
| 5552 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5553 | xmlBufGetNodeContent(buf, cur); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5554 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5555 | ret = xmlBufDetach(buf); |
| 5556 | xmlBufFree(buf); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5557 | return (ret); |
| 5558 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5559 | case XML_ENTITY_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5560 | case XML_DOCUMENT_TYPE_NODE: |
| 5561 | case XML_NOTATION_NODE: |
| 5562 | case XML_DTD_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5563 | case XML_XINCLUDE_START: |
| 5564 | case XML_XINCLUDE_END: |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5565 | return (NULL); |
| 5566 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5567 | #ifdef LIBXML_DOCB_ENABLED |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5568 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5569 | #endif |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5570 | case XML_HTML_DOCUMENT_NODE: { |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5571 | xmlBufPtr buf; |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5572 | xmlChar *ret; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5573 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5574 | buf = xmlBufCreate(); |
| 5575 | if (buf == NULL) |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5576 | return (NULL); |
| 5577 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5578 | xmlBufGetNodeContent(buf, (xmlNodePtr) cur); |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5579 | |
Daniel Veillard | c15df7d | 2012-08-07 15:15:04 +0800 | [diff] [blame] | 5580 | ret = xmlBufDetach(buf); |
| 5581 | xmlBufFree(buf); |
Daniel Veillard | c469692 | 2003-10-19 21:47:14 +0000 | [diff] [blame] | 5582 | return (ret); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 5583 | } |
Daniel Veillard | 96c3a3b | 2002-10-14 15:39:04 +0000 | [diff] [blame] | 5584 | case XML_NAMESPACE_DECL: { |
| 5585 | xmlChar *tmp; |
| 5586 | |
| 5587 | tmp = xmlStrdup(((xmlNsPtr) cur)->href); |
| 5588 | return (tmp); |
| 5589 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5590 | case XML_ELEMENT_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5591 | /* TODO !!! */ |
| 5592 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5593 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5594 | /* TODO !!! */ |
| 5595 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5596 | case XML_ENTITY_DECL: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5597 | /* TODO !!! */ |
| 5598 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5599 | case XML_CDATA_SECTION_NODE: |
| 5600 | case XML_TEXT_NODE: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5601 | if (cur->content != NULL) |
| 5602 | return (xmlStrdup(cur->content)); |
| 5603 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5604 | } |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5605 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5606 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5607 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5608 | /** |
| 5609 | * xmlNodeSetContent: |
| 5610 | * @cur: the node being modified |
| 5611 | * @content: the new value of the content |
| 5612 | * |
| 5613 | * Replace the content of a node. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5614 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 5615 | * references, but XML special chars need to be escaped first by using |
| 5616 | * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5617 | */ |
| 5618 | void |
| 5619 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
| 5620 | if (cur == NULL) { |
| 5621 | #ifdef DEBUG_TREE |
| 5622 | xmlGenericError(xmlGenericErrorContext, |
| 5623 | "xmlNodeSetContent : node == NULL\n"); |
| 5624 | #endif |
| 5625 | return; |
| 5626 | } |
| 5627 | switch (cur->type) { |
| 5628 | case XML_DOCUMENT_FRAG_NODE: |
| 5629 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5630 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5631 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5632 | cur->children = xmlStringGetNodeList(cur->doc, content); |
| 5633 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5634 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5635 | case XML_TEXT_NODE: |
| 5636 | case XML_CDATA_SECTION_NODE: |
| 5637 | case XML_ENTITY_REF_NODE: |
| 5638 | case XML_ENTITY_NODE: |
| 5639 | case XML_PI_NODE: |
| 5640 | case XML_COMMENT_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5641 | if ((cur->content != NULL) && |
| 5642 | (cur->content != (xmlChar *) &(cur->properties))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5643 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
Daniel Veillard | c587bce | 2005-05-10 15:28:08 +0000 | [diff] [blame] | 5644 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5645 | xmlFree(cur->content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5646 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5647 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5648 | cur->last = cur->children = NULL; |
| 5649 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5650 | cur->content = xmlStrdup(content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5651 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5652 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5653 | cur->properties = NULL; |
| 5654 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5655 | break; |
| 5656 | case XML_DOCUMENT_NODE: |
| 5657 | case XML_HTML_DOCUMENT_NODE: |
| 5658 | case XML_DOCUMENT_TYPE_NODE: |
| 5659 | case XML_XINCLUDE_START: |
| 5660 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5661 | #ifdef LIBXML_DOCB_ENABLED |
| 5662 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5663 | #endif |
| 5664 | break; |
| 5665 | case XML_NOTATION_NODE: |
| 5666 | break; |
| 5667 | case XML_DTD_NODE: |
| 5668 | break; |
| 5669 | case XML_NAMESPACE_DECL: |
| 5670 | break; |
| 5671 | case XML_ELEMENT_DECL: |
| 5672 | /* TODO !!! */ |
| 5673 | break; |
| 5674 | case XML_ATTRIBUTE_DECL: |
| 5675 | /* TODO !!! */ |
| 5676 | break; |
| 5677 | case XML_ENTITY_DECL: |
| 5678 | /* TODO !!! */ |
| 5679 | break; |
| 5680 | } |
| 5681 | } |
| 5682 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5683 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5684 | /** |
| 5685 | * xmlNodeSetContentLen: |
| 5686 | * @cur: the node being modified |
| 5687 | * @content: the new value of the content |
| 5688 | * @len: the size of @content |
| 5689 | * |
| 5690 | * Replace the content of a node. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5691 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity |
| 5692 | * references, but XML special chars need to be escaped first by using |
| 5693 | * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5694 | */ |
| 5695 | void |
| 5696 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5697 | if (cur == NULL) { |
| 5698 | #ifdef DEBUG_TREE |
| 5699 | xmlGenericError(xmlGenericErrorContext, |
| 5700 | "xmlNodeSetContentLen : node == NULL\n"); |
| 5701 | #endif |
| 5702 | return; |
| 5703 | } |
| 5704 | switch (cur->type) { |
| 5705 | case XML_DOCUMENT_FRAG_NODE: |
| 5706 | case XML_ELEMENT_NODE: |
Daniel Veillard | 2c748c6 | 2002-01-16 15:37:50 +0000 | [diff] [blame] | 5707 | case XML_ATTRIBUTE_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5708 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5709 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
| 5710 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
| 5711 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5712 | case XML_TEXT_NODE: |
| 5713 | case XML_CDATA_SECTION_NODE: |
| 5714 | case XML_ENTITY_REF_NODE: |
| 5715 | case XML_ENTITY_NODE: |
| 5716 | case XML_PI_NODE: |
| 5717 | case XML_COMMENT_NODE: |
| 5718 | case XML_NOTATION_NODE: |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5719 | if ((cur->content != NULL) && |
| 5720 | (cur->content != (xmlChar *) &(cur->properties))) { |
| 5721 | if (!((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5722 | (xmlDictOwns(cur->doc->dict, cur->content)))) |
| 5723 | xmlFree(cur->content); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5724 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5725 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 5726 | cur->children = cur->last = NULL; |
| 5727 | if (content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5728 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5729 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5730 | cur->content = NULL; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5731 | cur->properties = NULL; |
| 5732 | cur->nsDef = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5733 | break; |
| 5734 | case XML_DOCUMENT_NODE: |
| 5735 | case XML_DTD_NODE: |
| 5736 | case XML_HTML_DOCUMENT_NODE: |
| 5737 | case XML_DOCUMENT_TYPE_NODE: |
| 5738 | case XML_NAMESPACE_DECL: |
| 5739 | case XML_XINCLUDE_START: |
| 5740 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5741 | #ifdef LIBXML_DOCB_ENABLED |
| 5742 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5743 | #endif |
| 5744 | break; |
| 5745 | case XML_ELEMENT_DECL: |
| 5746 | /* TODO !!! */ |
| 5747 | break; |
| 5748 | case XML_ATTRIBUTE_DECL: |
| 5749 | /* TODO !!! */ |
| 5750 | break; |
| 5751 | case XML_ENTITY_DECL: |
| 5752 | /* TODO !!! */ |
| 5753 | break; |
| 5754 | } |
| 5755 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5756 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5757 | |
| 5758 | /** |
| 5759 | * xmlNodeAddContentLen: |
| 5760 | * @cur: the node being modified |
| 5761 | * @content: extra content |
| 5762 | * @len: the size of @content |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5763 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5764 | * Append the extra substring to the node content. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5765 | * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be |
| 5766 | * raw text, so unescaped XML special chars are allowed, entity |
| 5767 | * references are not supported. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5768 | */ |
| 5769 | void |
| 5770 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
| 5771 | if (cur == NULL) { |
| 5772 | #ifdef DEBUG_TREE |
| 5773 | xmlGenericError(xmlGenericErrorContext, |
| 5774 | "xmlNodeAddContentLen : node == NULL\n"); |
| 5775 | #endif |
| 5776 | return; |
| 5777 | } |
| 5778 | if (len <= 0) return; |
| 5779 | switch (cur->type) { |
| 5780 | case XML_DOCUMENT_FRAG_NODE: |
| 5781 | case XML_ELEMENT_NODE: { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5782 | xmlNodePtr last, newNode, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5783 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 5784 | last = cur->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5785 | newNode = xmlNewTextLen(content, len); |
| 5786 | if (newNode != NULL) { |
Daniel Veillard | acb2bda | 2002-01-13 16:15:43 +0000 | [diff] [blame] | 5787 | tmp = xmlAddChild(cur, newNode); |
| 5788 | if (tmp != newNode) |
| 5789 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5790 | if ((last != NULL) && (last->next == newNode)) { |
| 5791 | xmlTextMerge(last, newNode); |
| 5792 | } |
| 5793 | } |
| 5794 | break; |
| 5795 | } |
| 5796 | case XML_ATTRIBUTE_NODE: |
| 5797 | break; |
| 5798 | case XML_TEXT_NODE: |
| 5799 | case XML_CDATA_SECTION_NODE: |
| 5800 | case XML_ENTITY_REF_NODE: |
| 5801 | case XML_ENTITY_NODE: |
| 5802 | case XML_PI_NODE: |
| 5803 | case XML_COMMENT_NODE: |
| 5804 | case XML_NOTATION_NODE: |
| 5805 | if (content != NULL) { |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 5806 | if ((cur->content == (xmlChar *) &(cur->properties)) || |
| 5807 | ((cur->doc != NULL) && (cur->doc->dict != NULL) && |
| 5808 | xmlDictOwns(cur->doc->dict, cur->content))) { |
| 5809 | cur->content = xmlStrncatNew(cur->content, content, len); |
| 5810 | cur->properties = NULL; |
| 5811 | cur->nsDef = NULL; |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 5812 | break; |
| 5813 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5814 | cur->content = xmlStrncat(cur->content, content, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5815 | } |
| 5816 | case XML_DOCUMENT_NODE: |
| 5817 | case XML_DTD_NODE: |
| 5818 | case XML_HTML_DOCUMENT_NODE: |
| 5819 | case XML_DOCUMENT_TYPE_NODE: |
| 5820 | case XML_NAMESPACE_DECL: |
| 5821 | case XML_XINCLUDE_START: |
| 5822 | case XML_XINCLUDE_END: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5823 | #ifdef LIBXML_DOCB_ENABLED |
| 5824 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5825 | #endif |
| 5826 | break; |
| 5827 | case XML_ELEMENT_DECL: |
| 5828 | case XML_ATTRIBUTE_DECL: |
| 5829 | case XML_ENTITY_DECL: |
| 5830 | break; |
| 5831 | } |
| 5832 | } |
| 5833 | |
| 5834 | /** |
| 5835 | * xmlNodeAddContent: |
| 5836 | * @cur: the node being modified |
| 5837 | * @content: extra content |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5838 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5839 | * Append the extra substring to the node content. |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 5840 | * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be |
| 5841 | * raw text, so unescaped XML special chars are allowed, entity |
| 5842 | * references are not supported. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5843 | */ |
| 5844 | void |
| 5845 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
| 5846 | int len; |
| 5847 | |
| 5848 | if (cur == NULL) { |
| 5849 | #ifdef DEBUG_TREE |
| 5850 | xmlGenericError(xmlGenericErrorContext, |
| 5851 | "xmlNodeAddContent : node == NULL\n"); |
| 5852 | #endif |
| 5853 | return; |
| 5854 | } |
| 5855 | if (content == NULL) return; |
| 5856 | len = xmlStrlen(content); |
| 5857 | xmlNodeAddContentLen(cur, content, len); |
| 5858 | } |
| 5859 | |
| 5860 | /** |
| 5861 | * xmlTextMerge: |
| 5862 | * @first: the first text node |
| 5863 | * @second: the second text node being merged |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5864 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5865 | * Merge two text nodes into one |
| 5866 | * Returns the first text node augmented |
| 5867 | */ |
| 5868 | xmlNodePtr |
| 5869 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 5870 | if (first == NULL) return(second); |
| 5871 | if (second == NULL) return(first); |
| 5872 | if (first->type != XML_TEXT_NODE) return(first); |
| 5873 | if (second->type != XML_TEXT_NODE) return(first); |
| 5874 | if (second->name != first->name) |
| 5875 | return(first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5876 | xmlNodeAddContent(first, second->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5877 | xmlUnlinkNode(second); |
| 5878 | xmlFreeNode(second); |
| 5879 | return(first); |
| 5880 | } |
| 5881 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 5882 | #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] | 5883 | /** |
| 5884 | * xmlGetNsList: |
| 5885 | * @doc: the document |
| 5886 | * @node: the current node |
| 5887 | * |
| 5888 | * Search all the namespace applying to a given element. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 5889 | * Returns an NULL terminated array of all the #xmlNsPtr found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5890 | * that need to be freed by the caller or NULL if no |
| 5891 | * namespace if defined |
| 5892 | */ |
| 5893 | xmlNsPtr * |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 5894 | xmlGetNsList(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node) |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5895 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5896 | xmlNsPtr cur; |
| 5897 | xmlNsPtr *ret = NULL; |
| 5898 | int nbns = 0; |
| 5899 | int maxns = 10; |
| 5900 | int i; |
| 5901 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 5902 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 5903 | return(NULL); |
| 5904 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5905 | while (node != NULL) { |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5906 | if (node->type == XML_ELEMENT_NODE) { |
| 5907 | cur = node->nsDef; |
| 5908 | while (cur != NULL) { |
| 5909 | if (ret == NULL) { |
| 5910 | ret = |
| 5911 | (xmlNsPtr *) xmlMalloc((maxns + 1) * |
| 5912 | sizeof(xmlNsPtr)); |
| 5913 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5914 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5915 | return (NULL); |
| 5916 | } |
| 5917 | ret[nbns] = NULL; |
| 5918 | } |
| 5919 | for (i = 0; i < nbns; i++) { |
| 5920 | if ((cur->prefix == ret[i]->prefix) || |
| 5921 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) |
| 5922 | break; |
| 5923 | } |
| 5924 | if (i >= nbns) { |
| 5925 | if (nbns >= maxns) { |
| 5926 | maxns *= 2; |
| 5927 | ret = (xmlNsPtr *) xmlRealloc(ret, |
| 5928 | (maxns + |
| 5929 | 1) * |
| 5930 | sizeof(xmlNsPtr)); |
| 5931 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 5932 | xmlTreeErrMemory("getting namespace list"); |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5933 | return (NULL); |
| 5934 | } |
| 5935 | } |
| 5936 | ret[nbns++] = cur; |
| 5937 | ret[nbns] = NULL; |
| 5938 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5939 | |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5940 | cur = cur->next; |
| 5941 | } |
| 5942 | } |
| 5943 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5944 | } |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 5945 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5946 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 5947 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5948 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5949 | /* |
| 5950 | * xmlTreeEnsureXMLDecl: |
| 5951 | * @doc: the doc |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5952 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5953 | * Ensures that there is an XML namespace declaration on the doc. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5954 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5955 | * Returns the XML ns-struct or NULL on API and internal errors. |
| 5956 | */ |
| 5957 | static xmlNsPtr |
| 5958 | xmlTreeEnsureXMLDecl(xmlDocPtr doc) |
| 5959 | { |
| 5960 | if (doc == NULL) |
| 5961 | return (NULL); |
| 5962 | if (doc->oldNs != NULL) |
| 5963 | return (doc->oldNs); |
| 5964 | { |
| 5965 | xmlNsPtr ns; |
| 5966 | ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 5967 | if (ns == NULL) { |
| 5968 | xmlTreeErrMemory( |
| 5969 | "allocating the XML namespace"); |
| 5970 | return (NULL); |
| 5971 | } |
| 5972 | memset(ns, 0, sizeof(xmlNs)); |
| 5973 | ns->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5974 | ns->href = xmlStrdup(XML_XML_NAMESPACE); |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 5975 | ns->prefix = xmlStrdup((const xmlChar *)"xml"); |
| 5976 | doc->oldNs = ns; |
| 5977 | return (ns); |
| 5978 | } |
| 5979 | } |
| 5980 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5981 | /** |
| 5982 | * xmlSearchNs: |
| 5983 | * @doc: the document |
| 5984 | * @node: the current node |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 5985 | * @nameSpace: the namespace prefix |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5986 | * |
| 5987 | * Search a Ns registered under a given name space for a document. |
| 5988 | * recurse on the parents until it finds the defined namespace |
| 5989 | * or return NULL otherwise. |
| 5990 | * @nameSpace can be NULL, this is a search for the default namespace. |
| 5991 | * We don't allow to cross entities boundaries. If you don't declare |
| 5992 | * the namespace within those you will be in troubles !!! A warning |
| 5993 | * is generated to cover this case. |
| 5994 | * |
| 5995 | * Returns the namespace pointer or NULL. |
| 5996 | */ |
| 5997 | xmlNsPtr |
| 5998 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 5999 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6000 | xmlNsPtr cur; |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 6001 | const xmlNode *orig = node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6002 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 6003 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6004 | if ((nameSpace != NULL) && |
| 6005 | (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 6006 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 6007 | /* |
| 6008 | * The XML-1.0 namespace is normally held on the root |
| 6009 | * element. In this case exceptionally create it on the |
| 6010 | * node element. |
| 6011 | */ |
| 6012 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 6013 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6014 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 6015 | return(NULL); |
| 6016 | } |
| 6017 | memset(cur, 0, sizeof(xmlNs)); |
| 6018 | cur->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6019 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 6020 | cur->prefix = xmlStrdup((const xmlChar *)"xml"); |
Daniel Veillard | 6f46f6c | 2002-08-01 12:22:24 +0000 | [diff] [blame] | 6021 | cur->next = node->nsDef; |
| 6022 | node->nsDef = cur; |
| 6023 | return(cur); |
| 6024 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6025 | if (doc == NULL) { |
| 6026 | doc = node->doc; |
| 6027 | if (doc == NULL) |
| 6028 | return(NULL); |
| 6029 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 6030 | /* |
| 6031 | * Return the XML namespace declaration held by the doc. |
| 6032 | */ |
| 6033 | if (doc->oldNs == NULL) |
| 6034 | return(xmlTreeEnsureXMLDecl(doc)); |
| 6035 | else |
| 6036 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6037 | } |
| 6038 | while (node != NULL) { |
| 6039 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 6040 | (node->type == XML_ENTITY_NODE) || |
| 6041 | (node->type == XML_ENTITY_DECL)) |
| 6042 | return(NULL); |
| 6043 | if (node->type == XML_ELEMENT_NODE) { |
| 6044 | cur = node->nsDef; |
| 6045 | while (cur != NULL) { |
| 6046 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 6047 | (cur->href != NULL)) |
| 6048 | return(cur); |
| 6049 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 6050 | (cur->href != NULL) && |
| 6051 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 6052 | return(cur); |
| 6053 | cur = cur->next; |
| 6054 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6055 | if (orig != node) { |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 6056 | cur = node->ns; |
| 6057 | if (cur != NULL) { |
| 6058 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 6059 | (cur->href != NULL)) |
| 6060 | return(cur); |
| 6061 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
| 6062 | (cur->href != NULL) && |
| 6063 | (xmlStrEqual(cur->prefix, nameSpace))) |
| 6064 | return(cur); |
| 6065 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6066 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6067 | } |
| 6068 | node = node->parent; |
| 6069 | } |
| 6070 | return(NULL); |
| 6071 | } |
| 6072 | |
| 6073 | /** |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6074 | * xmlNsInScope: |
| 6075 | * @doc: the document |
| 6076 | * @node: the current node |
| 6077 | * @ancestor: the ancestor carrying the namespace |
| 6078 | * @prefix: the namespace prefix |
| 6079 | * |
| 6080 | * Verify that the given namespace held on @ancestor is still in scope |
| 6081 | * on node. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6082 | * |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6083 | * Returns 1 if true, 0 if false and -1 in case of error. |
| 6084 | */ |
| 6085 | static int |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6086 | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 6087 | xmlNodePtr ancestor, const xmlChar * prefix) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6088 | { |
| 6089 | xmlNsPtr tst; |
| 6090 | |
| 6091 | while ((node != NULL) && (node != ancestor)) { |
| 6092 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 6093 | (node->type == XML_ENTITY_NODE) || |
| 6094 | (node->type == XML_ENTITY_DECL)) |
| 6095 | return (-1); |
| 6096 | if (node->type == XML_ELEMENT_NODE) { |
| 6097 | tst = node->nsDef; |
| 6098 | while (tst != NULL) { |
| 6099 | if ((tst->prefix == NULL) |
| 6100 | && (prefix == NULL)) |
| 6101 | return (0); |
| 6102 | if ((tst->prefix != NULL) |
| 6103 | && (prefix != NULL) |
| 6104 | && (xmlStrEqual(tst->prefix, prefix))) |
| 6105 | return (0); |
| 6106 | tst = tst->next; |
| 6107 | } |
| 6108 | } |
| 6109 | node = node->parent; |
| 6110 | } |
| 6111 | if (node != ancestor) |
| 6112 | return (-1); |
| 6113 | return (1); |
| 6114 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6115 | |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6116 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6117 | * xmlSearchNsByHref: |
| 6118 | * @doc: the document |
| 6119 | * @node: the current node |
| 6120 | * @href: the namespace value |
| 6121 | * |
| 6122 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 6123 | * the defined namespace or return NULL otherwise. |
| 6124 | * Returns the namespace pointer or NULL. |
| 6125 | */ |
| 6126 | xmlNsPtr |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6127 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) |
| 6128 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6129 | xmlNsPtr cur; |
| 6130 | xmlNodePtr orig = node; |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6131 | int is_attr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6132 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 6133 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL)) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6134 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6135 | if (xmlStrEqual(href, XML_XML_NAMESPACE)) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6136 | /* |
| 6137 | * Only the document can hold the XML spec namespace. |
| 6138 | */ |
| 6139 | if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { |
| 6140 | /* |
| 6141 | * The XML-1.0 namespace is normally held on the root |
| 6142 | * element. In this case exceptionally create it on the |
| 6143 | * node element. |
| 6144 | */ |
| 6145 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 6146 | if (cur == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6147 | xmlTreeErrMemory("searching namespace"); |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6148 | return (NULL); |
| 6149 | } |
| 6150 | memset(cur, 0, sizeof(xmlNs)); |
| 6151 | cur->type = XML_LOCAL_NAMESPACE; |
| 6152 | cur->href = xmlStrdup(XML_XML_NAMESPACE); |
| 6153 | cur->prefix = xmlStrdup((const xmlChar *) "xml"); |
| 6154 | cur->next = node->nsDef; |
| 6155 | node->nsDef = cur; |
| 6156 | return (cur); |
| 6157 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6158 | if (doc == NULL) { |
| 6159 | doc = node->doc; |
| 6160 | if (doc == NULL) |
| 6161 | return(NULL); |
| 6162 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 6163 | /* |
| 6164 | * Return the XML namespace declaration held by the doc. |
| 6165 | */ |
| 6166 | if (doc->oldNs == NULL) |
| 6167 | return(xmlTreeEnsureXMLDecl(doc)); |
| 6168 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6169 | return(doc->oldNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6170 | } |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6171 | is_attr = (node->type == XML_ATTRIBUTE_NODE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6172 | while (node != NULL) { |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6173 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 6174 | (node->type == XML_ENTITY_NODE) || |
| 6175 | (node->type == XML_ENTITY_DECL)) |
| 6176 | return (NULL); |
| 6177 | if (node->type == XML_ELEMENT_NODE) { |
| 6178 | cur = node->nsDef; |
| 6179 | while (cur != NULL) { |
| 6180 | if ((cur->href != NULL) && (href != NULL) && |
| 6181 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6182 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 6183 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6184 | return (cur); |
| 6185 | } |
| 6186 | cur = cur->next; |
| 6187 | } |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 6188 | if (orig != node) { |
| 6189 | cur = node->ns; |
| 6190 | if (cur != NULL) { |
| 6191 | if ((cur->href != NULL) && (href != NULL) && |
| 6192 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | 62040be | 2004-05-17 03:17:26 +0000 | [diff] [blame] | 6193 | if (((!is_attr) || (cur->prefix != NULL)) && |
Kasimier T. Buchcik | ba70cc0 | 2005-02-28 10:28:21 +0000 | [diff] [blame] | 6194 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) |
Daniel Veillard | f4e5629 | 2003-10-28 14:27:41 +0000 | [diff] [blame] | 6195 | return (cur); |
| 6196 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6197 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6198 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6199 | } |
| 6200 | node = node->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6201 | } |
Daniel Veillard | 2a3fea3 | 2003-09-12 09:44:56 +0000 | [diff] [blame] | 6202 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
| 6205 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6206 | * xmlNewReconciliedNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6207 | * @doc: the document |
| 6208 | * @tree: a node expected to hold the new namespace |
| 6209 | * @ns: the original namespace |
| 6210 | * |
| 6211 | * This function tries to locate a namespace definition in a tree |
| 6212 | * ancestors, or create a new namespace definition node similar to |
| 6213 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 6214 | * null (default namespace) or reused within the subtree defined by |
| 6215 | * @tree or on one of its ancestors then a new prefix is generated. |
| 6216 | * Returns the (new) namespace definition or NULL in case of error |
| 6217 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 6218 | static xmlNsPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6219 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 6220 | xmlNsPtr def; |
| 6221 | xmlChar prefix[50]; |
| 6222 | int counter = 1; |
| 6223 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 6224 | if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6225 | #ifdef DEBUG_TREE |
| 6226 | xmlGenericError(xmlGenericErrorContext, |
| 6227 | "xmlNewReconciliedNs : tree == NULL\n"); |
| 6228 | #endif |
| 6229 | return(NULL); |
| 6230 | } |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 6231 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6232 | #ifdef DEBUG_TREE |
| 6233 | xmlGenericError(xmlGenericErrorContext, |
| 6234 | "xmlNewReconciliedNs : ns == NULL\n"); |
| 6235 | #endif |
| 6236 | return(NULL); |
| 6237 | } |
| 6238 | /* |
| 6239 | * Search an existing namespace definition inherited. |
| 6240 | */ |
| 6241 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 6242 | if (def != NULL) |
| 6243 | return(def); |
| 6244 | |
| 6245 | /* |
| 6246 | * Find a close prefix which is not already in use. |
| 6247 | * Let's strip namespace prefixes longer than 20 chars ! |
| 6248 | */ |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6249 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6250 | snprintf((char *) prefix, sizeof(prefix), "default"); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6251 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 6252 | snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6253 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6254 | def = xmlSearchNs(doc, tree, prefix); |
| 6255 | while (def != NULL) { |
| 6256 | if (counter > 1000) return(NULL); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6257 | if (ns->prefix == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6258 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6259 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 6260 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6261 | (char *)ns->prefix, counter++); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6262 | def = xmlSearchNs(doc, tree, prefix); |
| 6263 | } |
| 6264 | |
| 6265 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6266 | * OK, now we are ready to create a new one. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6267 | */ |
| 6268 | def = xmlNewNs(tree, ns->href, prefix); |
| 6269 | return(def); |
| 6270 | } |
| 6271 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6272 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6273 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 6274 | * xmlReconciliateNs: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6275 | * @doc: the document |
| 6276 | * @tree: a node defining the subtree to reconciliate |
| 6277 | * |
| 6278 | * This function checks that all the namespaces declared within the given |
| 6279 | * tree are properly declared. This is needed for example after Copy or Cut |
| 6280 | * and then paste operations. The subtree may still hold pointers to |
| 6281 | * namespace declarations outside the subtree or invalid/masked. As much |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6282 | * as possible the function try to reuse the existing namespaces found in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6283 | * the new environment. If not possible the new namespaces are redeclared |
| 6284 | * on @tree at the top of the given subtree. |
| 6285 | * Returns the number of namespace declarations created or -1 in case of error. |
| 6286 | */ |
| 6287 | int |
| 6288 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 6289 | xmlNsPtr *oldNs = NULL; |
| 6290 | xmlNsPtr *newNs = NULL; |
| 6291 | int sizeCache = 0; |
| 6292 | int nbCache = 0; |
| 6293 | |
| 6294 | xmlNsPtr n; |
| 6295 | xmlNodePtr node = tree; |
| 6296 | xmlAttrPtr attr; |
| 6297 | int ret = 0, i; |
| 6298 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 6299 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); |
| 6300 | if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1); |
| 6301 | if (node->doc != doc) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6302 | while (node != NULL) { |
| 6303 | /* |
| 6304 | * Reconciliate the node namespace |
| 6305 | */ |
| 6306 | if (node->ns != NULL) { |
| 6307 | /* |
| 6308 | * initialize the cache if needed |
| 6309 | */ |
| 6310 | if (sizeCache == 0) { |
| 6311 | sizeCache = 10; |
| 6312 | oldNs = (xmlNsPtr *) xmlMalloc(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 | return(-1); |
| 6317 | } |
| 6318 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6319 | sizeof(xmlNsPtr)); |
| 6320 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6321 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6322 | xmlFree(oldNs); |
| 6323 | return(-1); |
| 6324 | } |
| 6325 | } |
| 6326 | for (i = 0;i < nbCache;i++) { |
| 6327 | if (oldNs[i] == node->ns) { |
| 6328 | node->ns = newNs[i]; |
| 6329 | break; |
| 6330 | } |
| 6331 | } |
| 6332 | if (i == nbCache) { |
| 6333 | /* |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6334 | * OK we need to recreate a new namespace definition |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6335 | */ |
| 6336 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 6337 | if (n != NULL) { /* :-( what if else ??? */ |
| 6338 | /* |
| 6339 | * check if we need to grow the cache buffers. |
| 6340 | */ |
| 6341 | if (sizeCache <= nbCache) { |
| 6342 | sizeCache *= 2; |
| 6343 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 6344 | sizeof(xmlNsPtr)); |
| 6345 | if (oldNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6346 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6347 | xmlFree(newNs); |
| 6348 | return(-1); |
| 6349 | } |
| 6350 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 6351 | sizeof(xmlNsPtr)); |
| 6352 | if (newNs == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 6353 | xmlTreeErrMemory("fixing namespaces"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6354 | xmlFree(oldNs); |
| 6355 | return(-1); |
| 6356 | } |
| 6357 | } |
| 6358 | newNs[nbCache] = n; |
| 6359 | oldNs[nbCache++] = node->ns; |
| 6360 | node->ns = n; |
| 6361 | } |
| 6362 | } |
| 6363 | } |
| 6364 | /* |
| 6365 | * now check for namespace hold by attributes on the node. |
| 6366 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6367 | if (node->type == XML_ELEMENT_NODE) { |
| 6368 | attr = node->properties; |
| 6369 | while (attr != NULL) { |
| 6370 | if (attr->ns != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6371 | /* |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6372 | * initialize the cache if needed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6373 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6374 | if (sizeCache == 0) { |
| 6375 | sizeCache = 10; |
| 6376 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6377 | sizeof(xmlNsPtr)); |
| 6378 | if (oldNs == NULL) { |
| 6379 | xmlTreeErrMemory("fixing namespaces"); |
| 6380 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6381 | } |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6382 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 6383 | sizeof(xmlNsPtr)); |
| 6384 | if (newNs == NULL) { |
| 6385 | xmlTreeErrMemory("fixing namespaces"); |
| 6386 | xmlFree(oldNs); |
| 6387 | return(-1); |
| 6388 | } |
| 6389 | } |
| 6390 | for (i = 0;i < nbCache;i++) { |
| 6391 | if (oldNs[i] == attr->ns) { |
| 6392 | attr->ns = newNs[i]; |
| 6393 | break; |
| 6394 | } |
| 6395 | } |
| 6396 | if (i == nbCache) { |
| 6397 | /* |
| 6398 | * OK we need to recreate a new namespace definition |
| 6399 | */ |
| 6400 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 6401 | if (n != NULL) { /* :-( what if else ??? */ |
| 6402 | /* |
| 6403 | * check if we need to grow the cache buffers. |
| 6404 | */ |
| 6405 | if (sizeCache <= nbCache) { |
| 6406 | sizeCache *= 2; |
| 6407 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, |
| 6408 | sizeCache * sizeof(xmlNsPtr)); |
| 6409 | if (oldNs == NULL) { |
| 6410 | xmlTreeErrMemory("fixing namespaces"); |
| 6411 | xmlFree(newNs); |
| 6412 | return(-1); |
| 6413 | } |
| 6414 | newNs = (xmlNsPtr *) xmlRealloc(newNs, |
| 6415 | sizeCache * sizeof(xmlNsPtr)); |
| 6416 | if (newNs == NULL) { |
| 6417 | xmlTreeErrMemory("fixing namespaces"); |
| 6418 | xmlFree(oldNs); |
| 6419 | return(-1); |
| 6420 | } |
| 6421 | } |
| 6422 | newNs[nbCache] = n; |
| 6423 | oldNs[nbCache++] = attr->ns; |
| 6424 | attr->ns = n; |
| 6425 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6426 | } |
| 6427 | } |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6428 | attr = attr->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6429 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6430 | } |
| 6431 | |
| 6432 | /* |
| 6433 | * Browse the full subtree, deep first |
| 6434 | */ |
Daniel Veillard | b5f1197 | 2006-10-14 08:46:40 +0000 | [diff] [blame] | 6435 | if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6436 | /* deep first */ |
| 6437 | node = node->children; |
| 6438 | } else if ((node != tree) && (node->next != NULL)) { |
| 6439 | /* then siblings */ |
| 6440 | node = node->next; |
| 6441 | } else if (node != tree) { |
| 6442 | /* go up to parents->next if needed */ |
| 6443 | while (node != tree) { |
| 6444 | if (node->parent != NULL) |
| 6445 | node = node->parent; |
| 6446 | if ((node != tree) && (node->next != NULL)) { |
| 6447 | node = node->next; |
| 6448 | break; |
| 6449 | } |
| 6450 | if (node->parent == NULL) { |
| 6451 | node = NULL; |
| 6452 | break; |
| 6453 | } |
| 6454 | } |
| 6455 | /* exit condition */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6456 | if (node == tree) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6457 | node = NULL; |
Daniel Veillard | 1e77438 | 2002-03-06 17:35:40 +0000 | [diff] [blame] | 6458 | } else |
| 6459 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6460 | } |
Daniel Veillard | f742d34 | 2002-03-07 00:05:35 +0000 | [diff] [blame] | 6461 | if (oldNs != NULL) |
| 6462 | xmlFree(oldNs); |
| 6463 | if (newNs != NULL) |
| 6464 | xmlFree(newNs); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6465 | return(ret); |
| 6466 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6467 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6468 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6469 | static xmlAttrPtr |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 6470 | xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name, |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6471 | const xmlChar *nsName, int useDTD) |
| 6472 | { |
| 6473 | xmlAttrPtr prop; |
| 6474 | |
| 6475 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6476 | return(NULL); |
| 6477 | |
| 6478 | if (node->properties != NULL) { |
| 6479 | prop = node->properties; |
| 6480 | if (nsName == NULL) { |
| 6481 | /* |
| 6482 | * We want the attr to be in no namespace. |
| 6483 | */ |
| 6484 | do { |
| 6485 | if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) { |
| 6486 | return(prop); |
| 6487 | } |
| 6488 | prop = prop->next; |
| 6489 | } while (prop != NULL); |
| 6490 | } else { |
| 6491 | /* |
| 6492 | * We want the attr to be in the specified namespace. |
| 6493 | */ |
| 6494 | do { |
| 6495 | if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) && |
| 6496 | ((prop->ns->href == nsName) || |
| 6497 | xmlStrEqual(prop->ns->href, nsName))) |
| 6498 | { |
| 6499 | return(prop); |
| 6500 | } |
| 6501 | prop = prop->next; |
| 6502 | } while (prop != NULL); |
| 6503 | } |
| 6504 | } |
| 6505 | |
| 6506 | #ifdef LIBXML_TREE_ENABLED |
| 6507 | if (! useDTD) |
| 6508 | return(NULL); |
| 6509 | /* |
| 6510 | * Check if there is a default/fixed attribute declaration in |
| 6511 | * the internal or external subset. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6512 | */ |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6513 | if ((node->doc != NULL) && (node->doc->intSubset != NULL)) { |
| 6514 | xmlDocPtr doc = node->doc; |
| 6515 | xmlAttributePtr attrDecl = NULL; |
| 6516 | xmlChar *elemQName, *tmpstr = NULL; |
| 6517 | |
| 6518 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6519 | * We need the QName of the element for the DTD-lookup. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6520 | */ |
| 6521 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 6522 | tmpstr = xmlStrdup(node->ns->prefix); |
| 6523 | tmpstr = xmlStrcat(tmpstr, BAD_CAST ":"); |
| 6524 | tmpstr = xmlStrcat(tmpstr, node->name); |
| 6525 | if (tmpstr == NULL) |
| 6526 | return(NULL); |
| 6527 | elemQName = tmpstr; |
| 6528 | } else |
| 6529 | elemQName = (xmlChar *) node->name; |
| 6530 | if (nsName == NULL) { |
| 6531 | /* |
| 6532 | * The common and nice case: Attr in no namespace. |
| 6533 | */ |
| 6534 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, |
| 6535 | elemQName, name, NULL); |
| 6536 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 6537 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, |
| 6538 | elemQName, name, NULL); |
| 6539 | } |
| 6540 | } else { |
| 6541 | xmlNsPtr *nsList, *cur; |
| 6542 | |
| 6543 | /* |
| 6544 | * The ugly case: Search using the prefixes of in-scope |
| 6545 | * ns-decls corresponding to @nsName. |
| 6546 | */ |
| 6547 | nsList = xmlGetNsList(node->doc, node); |
| 6548 | if (nsList == NULL) { |
| 6549 | if (tmpstr != NULL) |
| 6550 | xmlFree(tmpstr); |
| 6551 | return(NULL); |
| 6552 | } |
| 6553 | cur = nsList; |
| 6554 | while (*cur != NULL) { |
| 6555 | if (xmlStrEqual((*cur)->href, nsName)) { |
| 6556 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName, |
| 6557 | name, (*cur)->prefix); |
| 6558 | if (attrDecl) |
| 6559 | break; |
| 6560 | if (doc->extSubset != NULL) { |
| 6561 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName, |
| 6562 | name, (*cur)->prefix); |
| 6563 | if (attrDecl) |
| 6564 | break; |
| 6565 | } |
| 6566 | } |
| 6567 | cur++; |
| 6568 | } |
| 6569 | xmlFree(nsList); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6570 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6571 | if (tmpstr != NULL) |
| 6572 | xmlFree(tmpstr); |
| 6573 | /* |
| 6574 | * Only default/fixed attrs are relevant. |
| 6575 | */ |
| 6576 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6577 | return((xmlAttrPtr) attrDecl); |
| 6578 | } |
| 6579 | #endif /* LIBXML_TREE_ENABLED */ |
| 6580 | return(NULL); |
| 6581 | } |
| 6582 | |
| 6583 | static xmlChar* |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 6584 | xmlGetPropNodeValueInternal(const xmlAttr *prop) |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6585 | { |
| 6586 | if (prop == NULL) |
| 6587 | return(NULL); |
| 6588 | if (prop->type == XML_ATTRIBUTE_NODE) { |
| 6589 | /* |
| 6590 | * Note that we return at least the empty string. |
| 6591 | * TODO: Do we really always want that? |
| 6592 | */ |
| 6593 | if (prop->children != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 6594 | if ((prop->children->next == NULL) && |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6595 | ((prop->children->type == XML_TEXT_NODE) || |
| 6596 | (prop->children->type == XML_CDATA_SECTION_NODE))) |
| 6597 | { |
| 6598 | /* |
| 6599 | * Optimization for the common case: only 1 text node. |
| 6600 | */ |
| 6601 | return(xmlStrdup(prop->children->content)); |
| 6602 | } else { |
| 6603 | xmlChar *ret; |
| 6604 | |
| 6605 | ret = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 6606 | if (ret != NULL) |
| 6607 | return(ret); |
| 6608 | } |
| 6609 | } |
| 6610 | return(xmlStrdup((xmlChar *)"")); |
| 6611 | } else if (prop->type == XML_ATTRIBUTE_DECL) { |
| 6612 | return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue)); |
| 6613 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6614 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6615 | } |
| 6616 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6617 | /** |
| 6618 | * xmlHasProp: |
| 6619 | * @node: the node |
| 6620 | * @name: the attribute name |
| 6621 | * |
| 6622 | * Search an attribute associated to a node |
| 6623 | * This function also looks in DTD attribute declaration for #FIXED or |
| 6624 | * default declaration values unless DTD use has been turned off. |
| 6625 | * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6626 | * Returns the attribute or the attribute declaration or NULL if |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6627 | * neither was found. |
| 6628 | */ |
| 6629 | xmlAttrPtr |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 6630 | xmlHasProp(const xmlNode *node, const xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6631 | xmlAttrPtr prop; |
| 6632 | xmlDocPtr doc; |
| 6633 | |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6634 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
| 6635 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6636 | /* |
| 6637 | * Check on the properties attached to the node |
| 6638 | */ |
| 6639 | prop = node->properties; |
| 6640 | while (prop != NULL) { |
| 6641 | if (xmlStrEqual(prop->name, name)) { |
| 6642 | return(prop); |
| 6643 | } |
| 6644 | prop = prop->next; |
| 6645 | } |
| 6646 | if (!xmlCheckDTD) return(NULL); |
| 6647 | |
| 6648 | /* |
| 6649 | * Check if there is a default declaration in the internal |
| 6650 | * or external subsets |
| 6651 | */ |
| 6652 | doc = node->doc; |
| 6653 | if (doc != NULL) { |
| 6654 | xmlAttributePtr attrDecl; |
| 6655 | if (doc->intSubset != NULL) { |
| 6656 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 6657 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 6658 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 6659 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
| 6660 | /* return attribute declaration only if a default value is given |
| 6661 | (that includes #FIXED declarations) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6662 | return((xmlAttrPtr) attrDecl); |
| 6663 | } |
| 6664 | } |
| 6665 | return(NULL); |
| 6666 | } |
| 6667 | |
| 6668 | /** |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6669 | * xmlHasNsProp: |
| 6670 | * @node: the node |
| 6671 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6672 | * @nameSpace: the URI of the namespace |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6673 | * |
| 6674 | * Search for an attribute associated to a node |
| 6675 | * This attribute has to be anchored in the namespace specified. |
| 6676 | * This does the entity substitution. |
| 6677 | * This function looks in DTD attribute declaration for #FIXED or |
| 6678 | * default declaration values unless DTD use has been turned off. |
William M. Brack | 2c22844 | 2004-10-03 04:10:00 +0000 | [diff] [blame] | 6679 | * Note that a namespace of NULL indicates to use the default namespace. |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6680 | * |
| 6681 | * Returns the attribute or the attribute declaration or NULL |
| 6682 | * if neither was found. |
| 6683 | */ |
| 6684 | xmlAttrPtr |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 6685 | xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) { |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6686 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6687 | return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD)); |
Daniel Veillard | e95e239 | 2001-06-06 10:46:28 +0000 | [diff] [blame] | 6688 | } |
| 6689 | |
| 6690 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6691 | * xmlGetProp: |
| 6692 | * @node: the node |
| 6693 | * @name: the attribute name |
| 6694 | * |
| 6695 | * Search and get the value of an attribute associated to a node |
| 6696 | * This does the entity substitution. |
| 6697 | * This function looks in DTD attribute declaration for #FIXED or |
| 6698 | * default declaration values unless DTD use has been turned off. |
Daniel Veillard | 784b935 | 2003-02-16 15:50:27 +0000 | [diff] [blame] | 6699 | * NOTE: this function acts independently of namespaces associated |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6700 | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
| 6701 | * for namespace aware processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6702 | * |
| 6703 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6704 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6705 | */ |
| 6706 | xmlChar * |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 6707 | xmlGetProp(const xmlNode *node, const xmlChar *name) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6708 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6709 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6710 | prop = xmlHasProp(node, name); |
| 6711 | if (prop == NULL) |
| 6712 | return(NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6713 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6714 | } |
| 6715 | |
| 6716 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6717 | * xmlGetNoNsProp: |
| 6718 | * @node: the node |
| 6719 | * @name: the attribute name |
| 6720 | * |
| 6721 | * Search and get the value of an attribute associated to a node |
| 6722 | * This does the entity substitution. |
| 6723 | * This function looks in DTD attribute declaration for #FIXED or |
| 6724 | * default declaration values unless DTD use has been turned off. |
| 6725 | * This function is similar to xmlGetProp except it will accept only |
| 6726 | * an attribute in no namespace. |
| 6727 | * |
| 6728 | * Returns the attribute value or NULL if not found. |
| 6729 | * It's up to the caller to free the memory with xmlFree(). |
| 6730 | */ |
| 6731 | xmlChar * |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 6732 | xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) { |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6733 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6734 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6735 | prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD); |
| 6736 | if (prop == NULL) |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6737 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6738 | return(xmlGetPropNodeValueInternal(prop)); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6739 | } |
| 6740 | |
| 6741 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6742 | * xmlGetNsProp: |
| 6743 | * @node: the node |
| 6744 | * @name: the attribute name |
Daniel Veillard | ca2366a | 2001-06-11 12:09:01 +0000 | [diff] [blame] | 6745 | * @nameSpace: the URI of the namespace |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6746 | * |
| 6747 | * Search and get the value of an attribute associated to a node |
| 6748 | * This attribute has to be anchored in the namespace specified. |
| 6749 | * This does the entity substitution. |
| 6750 | * This function looks in DTD attribute declaration for #FIXED or |
| 6751 | * default declaration values unless DTD use has been turned off. |
| 6752 | * |
| 6753 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 6754 | * It's up to the caller to free the memory with xmlFree(). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6755 | */ |
| 6756 | xmlChar * |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 6757 | xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6758 | xmlAttrPtr prop; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6759 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6760 | prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD); |
| 6761 | if (prop == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6762 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6763 | return(xmlGetPropNodeValueInternal(prop)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6764 | } |
| 6765 | |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6766 | #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
| 6767 | /** |
| 6768 | * xmlUnsetProp: |
| 6769 | * @node: the node |
| 6770 | * @name: the attribute name |
| 6771 | * |
| 6772 | * Remove an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6773 | * This handles only attributes in no namespace. |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6774 | * Returns 0 if successful, -1 if not found |
| 6775 | */ |
| 6776 | int |
| 6777 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6778 | xmlAttrPtr prop; |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6779 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6780 | prop = xmlGetPropNodeInternal(node, name, NULL, 0); |
| 6781 | if (prop == NULL) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6782 | return(-1); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6783 | xmlUnlinkNode((xmlNodePtr) prop); |
| 6784 | xmlFreeProp(prop); |
| 6785 | return(0); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6786 | } |
| 6787 | |
| 6788 | /** |
| 6789 | * xmlUnsetNsProp: |
| 6790 | * @node: the node |
| 6791 | * @ns: the namespace definition |
| 6792 | * @name: the attribute name |
| 6793 | * |
| 6794 | * Remove an attribute carried by a node. |
| 6795 | * Returns 0 if successful, -1 if not found |
| 6796 | */ |
| 6797 | int |
| 6798 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
Kasimier T. Buchcik | 65c2f1d | 2005-10-17 12:39:58 +0000 | [diff] [blame] | 6799 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6800 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6801 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6802 | if (prop == NULL) |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6803 | return(-1); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6804 | xmlUnlinkNode((xmlNodePtr) prop); |
| 6805 | xmlFreeProp(prop); |
| 6806 | return(0); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 6807 | } |
| 6808 | #endif |
| 6809 | |
| 6810 | #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] | 6811 | /** |
| 6812 | * xmlSetProp: |
| 6813 | * @node: the node |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6814 | * @name: the attribute name (a QName) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6815 | * @value: the attribute value |
| 6816 | * |
| 6817 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6818 | * If @name has a prefix, then the corresponding |
| 6819 | * namespace-binding will be used, if in scope; it is an |
| 6820 | * error it there's no such ns-binding for the prefix in |
| 6821 | * scope. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6822 | * Returns the attribute pointer. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6823 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6824 | */ |
| 6825 | xmlAttrPtr |
| 6826 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6827 | int len; |
| 6828 | const xmlChar *nqname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6829 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6830 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6831 | return(NULL); |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 6832 | |
| 6833 | /* |
| 6834 | * handle QNames |
| 6835 | */ |
| 6836 | nqname = xmlSplitQName3(name, &len); |
| 6837 | if (nqname != NULL) { |
| 6838 | xmlNsPtr ns; |
| 6839 | xmlChar *prefix = xmlStrndup(name, len); |
| 6840 | ns = xmlSearchNs(node->doc, node, prefix); |
| 6841 | if (prefix != NULL) |
| 6842 | xmlFree(prefix); |
| 6843 | if (ns != NULL) |
| 6844 | return(xmlSetNsProp(node, ns, nqname, value)); |
| 6845 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6846 | return(xmlSetNsProp(node, NULL, name, value)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6847 | } |
| 6848 | |
| 6849 | /** |
| 6850 | * xmlSetNsProp: |
| 6851 | * @node: the node |
| 6852 | * @ns: the namespace definition |
| 6853 | * @name: the attribute name |
| 6854 | * @value: the attribute value |
| 6855 | * |
| 6856 | * Set (or reset) an attribute carried by a node. |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6857 | * The ns structure must be in scope, this is not checked |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6858 | * |
| 6859 | * Returns the attribute pointer. |
| 6860 | */ |
| 6861 | xmlAttrPtr |
| 6862 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6863 | const xmlChar *value) |
| 6864 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6865 | xmlAttrPtr prop; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6866 | |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6867 | if (ns && (ns->href == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6868 | return(NULL); |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6869 | prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0); |
| 6870 | if (prop != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6871 | /* |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6872 | * Modify the attribute's value. |
| 6873 | */ |
| 6874 | if (prop->atype == XML_ATTRIBUTE_ID) { |
| 6875 | xmlRemoveID(node->doc, prop); |
| 6876 | prop->atype = XML_ATTRIBUTE_ID; |
| 6877 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6878 | if (prop->children != NULL) |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6879 | xmlFreeNodeList(prop->children); |
| 6880 | prop->children = NULL; |
| 6881 | prop->last = NULL; |
| 6882 | prop->ns = ns; |
| 6883 | if (value != NULL) { |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6884 | xmlNodePtr tmp; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6885 | |
Daniel Veillard | 6f8611f | 2008-02-15 08:33:21 +0000 | [diff] [blame] | 6886 | if(!xmlCheckUTF8(value)) { |
| 6887 | xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc, |
| 6888 | NULL); |
| 6889 | if (node->doc != NULL) |
| 6890 | node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
| 6891 | } |
| 6892 | prop->children = xmlNewDocText(node->doc, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6893 | prop->last = NULL; |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6894 | tmp = prop->children; |
| 6895 | while (tmp != NULL) { |
| 6896 | tmp->parent = (xmlNodePtr) prop; |
| 6897 | if (tmp->next == NULL) |
| 6898 | prop->last = tmp; |
| 6899 | tmp = tmp->next; |
| 6900 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6901 | } |
| 6902 | if (prop->atype == XML_ATTRIBUTE_ID) |
| 6903 | xmlAddID(NULL, node->doc, value, prop); |
| 6904 | return(prop); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6905 | } |
Kasimier T. Buchcik | 30f874d | 2006-03-02 18:04:29 +0000 | [diff] [blame] | 6906 | /* |
| 6907 | * No equal attr found; create a new one. |
| 6908 | */ |
| 6909 | return(xmlNewPropInternal(node, ns, name, value, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6910 | } |
| 6911 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 6912 | #endif /* LIBXML_TREE_ENABLED */ |
Daniel Veillard | 75bea54 | 2001-05-11 17:41:21 +0000 | [diff] [blame] | 6913 | |
| 6914 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6915 | * xmlNodeIsText: |
| 6916 | * @node: the node |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6917 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6918 | * Is this node a Text node ? |
| 6919 | * Returns 1 yes, 0 no |
| 6920 | */ |
| 6921 | int |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 6922 | xmlNodeIsText(const xmlNode *node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6923 | if (node == NULL) return(0); |
| 6924 | |
| 6925 | if (node->type == XML_TEXT_NODE) return(1); |
| 6926 | return(0); |
| 6927 | } |
| 6928 | |
| 6929 | /** |
| 6930 | * xmlIsBlankNode: |
| 6931 | * @node: the node |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6932 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6933 | * Checks whether this node is an empty or whitespace only |
| 6934 | * (and possibly ignorable) text-node. |
| 6935 | * |
| 6936 | * Returns 1 yes, 0 no |
| 6937 | */ |
| 6938 | int |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 6939 | xmlIsBlankNode(const xmlNode *node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6940 | const xmlChar *cur; |
| 6941 | if (node == NULL) return(0); |
| 6942 | |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 6943 | if ((node->type != XML_TEXT_NODE) && |
| 6944 | (node->type != XML_CDATA_SECTION_NODE)) |
| 6945 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6946 | if (node->content == NULL) return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6947 | cur = node->content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6948 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6949 | if (!IS_BLANK_CH(*cur)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6950 | cur++; |
| 6951 | } |
| 6952 | |
| 6953 | return(1); |
| 6954 | } |
| 6955 | |
| 6956 | /** |
| 6957 | * xmlTextConcat: |
| 6958 | * @node: the node |
| 6959 | * @content: the content |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 6960 | * @len: @content length |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 6961 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6962 | * Concat the given string at the end of the existing node content |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6963 | * |
| 6964 | * Returns -1 in case of error, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6965 | */ |
| 6966 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6967 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6968 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6969 | if (node == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6970 | |
| 6971 | if ((node->type != XML_TEXT_NODE) && |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 6972 | (node->type != XML_CDATA_SECTION_NODE) && |
| 6973 | (node->type != XML_COMMENT_NODE) && |
| 6974 | (node->type != XML_PI_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6975 | #ifdef DEBUG_TREE |
| 6976 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 6977 | "xmlTextConcat: node is not text nor CDATA\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6978 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6979 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6980 | } |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6981 | /* need to check if content is currently in the dictionary */ |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6982 | if ((node->content == (xmlChar *) &(node->properties)) || |
| 6983 | ((node->doc != NULL) && (node->doc->dict != NULL) && |
| 6984 | xmlDictOwns(node->doc->dict, node->content))) { |
William M. Brack | 7762bb1 | 2004-01-04 14:49:01 +0000 | [diff] [blame] | 6985 | node->content = xmlStrncatNew(node->content, content, len); |
| 6986 | } else { |
| 6987 | node->content = xmlStrncat(node->content, content, len); |
| 6988 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6989 | node->properties = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6990 | if (node->content == NULL) |
| 6991 | return(-1); |
| 6992 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6993 | } |
| 6994 | |
| 6995 | /************************************************************************ |
| 6996 | * * |
| 6997 | * Output : to a FILE or in memory * |
| 6998 | * * |
| 6999 | ************************************************************************/ |
| 7000 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7001 | /** |
| 7002 | * xmlBufferCreate: |
| 7003 | * |
| 7004 | * routine to create an XML buffer. |
| 7005 | * returns the new structure. |
| 7006 | */ |
| 7007 | xmlBufferPtr |
| 7008 | xmlBufferCreate(void) { |
| 7009 | xmlBufferPtr ret; |
| 7010 | |
| 7011 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 7012 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7013 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7014 | return(NULL); |
| 7015 | } |
| 7016 | ret->use = 0; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 7017 | ret->size = xmlDefaultBufferSize; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7018 | ret->alloc = xmlBufferAllocScheme; |
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; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7026 | ret->contentIO = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7027 | return(ret); |
| 7028 | } |
| 7029 | |
| 7030 | /** |
| 7031 | * xmlBufferCreateSize: |
| 7032 | * @size: initial size of buffer |
| 7033 | * |
| 7034 | * routine to create an XML buffer. |
| 7035 | * returns the new structure. |
| 7036 | */ |
| 7037 | xmlBufferPtr |
| 7038 | xmlBufferCreateSize(size_t size) { |
| 7039 | xmlBufferPtr ret; |
| 7040 | |
| 7041 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 7042 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7043 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7044 | return(NULL); |
| 7045 | } |
| 7046 | ret->use = 0; |
| 7047 | ret->alloc = xmlBufferAllocScheme; |
| 7048 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
| 7049 | if (ret->size){ |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 7050 | ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7051 | if (ret->content == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7052 | xmlTreeErrMemory("creating buffer"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7053 | xmlFree(ret); |
| 7054 | return(NULL); |
| 7055 | } |
| 7056 | ret->content[0] = 0; |
| 7057 | } else |
| 7058 | ret->content = NULL; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7059 | ret->contentIO = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7060 | return(ret); |
| 7061 | } |
| 7062 | |
| 7063 | /** |
Daniel Veillard | 79ee284 | 2012-05-15 10:25:31 +0800 | [diff] [blame] | 7064 | * xmlBufferDetach: |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 7065 | * @buf: the buffer |
| 7066 | * |
Daniel Veillard | 79ee284 | 2012-05-15 10:25:31 +0800 | [diff] [blame] | 7067 | * 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] | 7068 | * caller. The buffer is reset to an empty content. |
| 7069 | * 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] | 7070 | * |
Daniel Veillard | 79ee284 | 2012-05-15 10:25:31 +0800 | [diff] [blame] | 7071 | * Returns the previous string contained by the buffer. |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 7072 | */ |
| 7073 | xmlChar * |
| 7074 | xmlBufferDetach(xmlBufferPtr buf) { |
| 7075 | xmlChar *ret; |
| 7076 | |
Daniel Veillard | 94431ec | 2012-05-15 10:45:05 +0800 | [diff] [blame] | 7077 | if (buf == NULL) |
| 7078 | return(NULL); |
| 7079 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) |
| 7080 | return(NULL); |
Conrad Irwin | 7d553f8 | 2012-05-10 20:17:25 -0700 | [diff] [blame] | 7081 | |
| 7082 | ret = buf->content; |
| 7083 | buf->content = NULL; |
| 7084 | buf->size = 0; |
| 7085 | buf->use = 0; |
| 7086 | |
| 7087 | return ret; |
| 7088 | } |
| 7089 | |
| 7090 | |
| 7091 | /** |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7092 | * xmlBufferCreateStatic: |
| 7093 | * @mem: the memory area |
| 7094 | * @size: the size in byte |
| 7095 | * |
MST 2003 John Fleck | a0e7e93 | 2003-12-19 03:13:47 +0000 | [diff] [blame] | 7096 | * routine to create an XML buffer from an immutable memory area. |
| 7097 | * 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] | 7098 | * present until the end of the buffer lifetime. |
| 7099 | * |
| 7100 | * returns the new structure. |
| 7101 | */ |
| 7102 | xmlBufferPtr |
| 7103 | xmlBufferCreateStatic(void *mem, size_t size) { |
| 7104 | xmlBufferPtr ret; |
| 7105 | |
| 7106 | if ((mem == NULL) || (size == 0)) |
| 7107 | return(NULL); |
| 7108 | |
| 7109 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 7110 | if (ret == NULL) { |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7111 | xmlTreeErrMemory("creating buffer"); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7112 | return(NULL); |
| 7113 | } |
| 7114 | ret->use = size; |
| 7115 | ret->size = size; |
| 7116 | ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; |
| 7117 | ret->content = (xmlChar *) mem; |
| 7118 | return(ret); |
| 7119 | } |
| 7120 | |
| 7121 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7122 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 7123 | * @buf: the buffer to tune |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7124 | * @scheme: allocation scheme to use |
| 7125 | * |
| 7126 | * Sets the allocation scheme for this buffer |
| 7127 | */ |
| 7128 | void |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7129 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7130 | xmlBufferAllocationScheme scheme) { |
| 7131 | if (buf == NULL) { |
| 7132 | #ifdef DEBUG_BUFFER |
| 7133 | xmlGenericError(xmlGenericErrorContext, |
| 7134 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
| 7135 | #endif |
| 7136 | return; |
| 7137 | } |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 7138 | if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || |
| 7139 | (buf->alloc == XML_BUFFER_ALLOC_IO)) return; |
| 7140 | if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) || |
| 7141 | (scheme == XML_BUFFER_ALLOC_EXACT) || |
Conrad Irwin | 7d0d2a5 | 2012-05-14 14:18:58 +0800 | [diff] [blame] | 7142 | (scheme == XML_BUFFER_ALLOC_HYBRID) || |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 7143 | (scheme == XML_BUFFER_ALLOC_IMMUTABLE)) |
| 7144 | buf->alloc = scheme; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7145 | } |
| 7146 | |
| 7147 | /** |
| 7148 | * xmlBufferFree: |
| 7149 | * @buf: the buffer to free |
| 7150 | * |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 7151 | * Frees an XML buffer. It frees both the content and the structure which |
| 7152 | * encapsulate it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7153 | */ |
| 7154 | void |
| 7155 | xmlBufferFree(xmlBufferPtr buf) { |
| 7156 | if (buf == NULL) { |
| 7157 | #ifdef DEBUG_BUFFER |
| 7158 | xmlGenericError(xmlGenericErrorContext, |
| 7159 | "xmlBufferFree: buf == NULL\n"); |
| 7160 | #endif |
| 7161 | return; |
| 7162 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7163 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7164 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && |
| 7165 | (buf->contentIO != NULL)) { |
| 7166 | xmlFree(buf->contentIO); |
| 7167 | } else if ((buf->content != NULL) && |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7168 | (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7169 | xmlFree(buf->content); |
| 7170 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7171 | xmlFree(buf); |
| 7172 | } |
| 7173 | |
| 7174 | /** |
| 7175 | * xmlBufferEmpty: |
| 7176 | * @buf: the buffer |
| 7177 | * |
| 7178 | * empty a buffer. |
| 7179 | */ |
| 7180 | void |
| 7181 | xmlBufferEmpty(xmlBufferPtr buf) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7182 | if (buf == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7183 | if (buf->content == NULL) return; |
| 7184 | buf->use = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7185 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 7186 | buf->content = BAD_CAST ""; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7187 | } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) && |
| 7188 | (buf->contentIO != NULL)) { |
| 7189 | size_t start_buf = buf->content - buf->contentIO; |
| 7190 | |
| 7191 | buf->size += start_buf; |
| 7192 | buf->content = buf->contentIO; |
| 7193 | buf->content[0] = 0; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7194 | } else { |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7195 | buf->content[0] = 0; |
| 7196 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7197 | } |
| 7198 | |
| 7199 | /** |
| 7200 | * xmlBufferShrink: |
| 7201 | * @buf: the buffer to dump |
| 7202 | * @len: the number of xmlChar to remove |
| 7203 | * |
| 7204 | * Remove the beginning of an XML buffer. |
| 7205 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7206 | * Returns the number of #xmlChar removed, or -1 in case of failure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7207 | */ |
| 7208 | int |
| 7209 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 7210 | if (buf == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7211 | if (len == 0) return(0); |
| 7212 | if (len > buf->use) return(-1); |
| 7213 | |
| 7214 | buf->use -= len; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7215 | if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || |
| 7216 | ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) { |
| 7217 | /* |
| 7218 | * we just move the content pointer, but also make sure |
| 7219 | * the perceived buffer size has shrinked accordingly |
| 7220 | */ |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7221 | buf->content += len; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7222 | buf->size -= len; |
| 7223 | |
| 7224 | /* |
| 7225 | * sometimes though it maybe be better to really shrink |
| 7226 | * on IO buffers |
| 7227 | */ |
| 7228 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7229 | size_t start_buf = buf->content - buf->contentIO; |
| 7230 | if (start_buf >= buf->size) { |
| 7231 | memmove(buf->contentIO, &buf->content[0], buf->use); |
| 7232 | buf->content = buf->contentIO; |
| 7233 | buf->content[buf->use] = 0; |
| 7234 | buf->size += start_buf; |
| 7235 | } |
| 7236 | } |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7237 | } else { |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7238 | memmove(buf->content, &buf->content[len], buf->use); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7239 | buf->content[buf->use] = 0; |
| 7240 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7241 | return(len); |
| 7242 | } |
| 7243 | |
| 7244 | /** |
| 7245 | * xmlBufferGrow: |
| 7246 | * @buf: the buffer |
| 7247 | * @len: the minimum free size to allocate |
| 7248 | * |
| 7249 | * Grow the available space of an XML buffer. |
| 7250 | * |
| 7251 | * Returns the new available space or -1 in case of error |
| 7252 | */ |
| 7253 | int |
| 7254 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
| 7255 | int size; |
| 7256 | xmlChar *newbuf; |
| 7257 | |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 7258 | if (buf == NULL) return(-1); |
| 7259 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7260 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7261 | if (len + buf->use < buf->size) return(0); |
| 7262 | |
Daniel Veillard | ee20cd7 | 2009-08-22 15:18:31 +0200 | [diff] [blame] | 7263 | /* |
| 7264 | * Windows has a BIG problem on realloc timing, so we try to double |
| 7265 | * the buffer size (if that's enough) (bug 146697) |
| 7266 | * Apparently BSD too, and it's probably best for linux too |
| 7267 | * On an embedded system this may be something to change |
| 7268 | */ |
| 7269 | #if 1 |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 7270 | if (buf->size > len) |
| 7271 | size = buf->size * 2; |
| 7272 | else |
| 7273 | size = buf->use + len + 100; |
| 7274 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7275 | size = buf->use + len + 100; |
William M. Brack | 30fe43f | 2004-07-26 18:00:58 +0000 | [diff] [blame] | 7276 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7277 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7278 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7279 | size_t start_buf = buf->content - buf->contentIO; |
| 7280 | |
| 7281 | newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size); |
| 7282 | if (newbuf == NULL) { |
| 7283 | xmlTreeErrMemory("growing buffer"); |
| 7284 | return(-1); |
| 7285 | } |
| 7286 | buf->contentIO = newbuf; |
| 7287 | buf->content = newbuf + start_buf; |
| 7288 | } else { |
| 7289 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
| 7290 | if (newbuf == NULL) { |
| 7291 | xmlTreeErrMemory("growing buffer"); |
| 7292 | return(-1); |
| 7293 | } |
| 7294 | buf->content = newbuf; |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7295 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7296 | buf->size = size; |
| 7297 | return(buf->size - buf->use); |
| 7298 | } |
| 7299 | |
| 7300 | /** |
| 7301 | * xmlBufferDump: |
| 7302 | * @file: the file output |
| 7303 | * @buf: the buffer to dump |
| 7304 | * |
| 7305 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7306 | * Returns the number of #xmlChar written |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7307 | */ |
| 7308 | int |
| 7309 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 7310 | int ret; |
| 7311 | |
| 7312 | if (buf == NULL) { |
| 7313 | #ifdef DEBUG_BUFFER |
| 7314 | xmlGenericError(xmlGenericErrorContext, |
| 7315 | "xmlBufferDump: buf == NULL\n"); |
| 7316 | #endif |
| 7317 | return(0); |
| 7318 | } |
| 7319 | if (buf->content == NULL) { |
| 7320 | #ifdef DEBUG_BUFFER |
| 7321 | xmlGenericError(xmlGenericErrorContext, |
| 7322 | "xmlBufferDump: buf->content == NULL\n"); |
| 7323 | #endif |
| 7324 | return(0); |
| 7325 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 7326 | if (file == NULL) |
| 7327 | file = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7328 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
| 7329 | return(ret); |
| 7330 | } |
| 7331 | |
| 7332 | /** |
| 7333 | * xmlBufferContent: |
| 7334 | * @buf: the buffer |
| 7335 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7336 | * Function to extract the content of a buffer |
| 7337 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7338 | * Returns the internal content |
| 7339 | */ |
| 7340 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7341 | const xmlChar * |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 7342 | xmlBufferContent(const xmlBuffer *buf) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7343 | { |
| 7344 | if(!buf) |
| 7345 | return NULL; |
| 7346 | |
| 7347 | return buf->content; |
| 7348 | } |
| 7349 | |
| 7350 | /** |
| 7351 | * xmlBufferLength: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7352 | * @buf: the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7353 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 7354 | * Function to get the length of a buffer |
| 7355 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7356 | * Returns the length of data in the internal content |
| 7357 | */ |
| 7358 | |
| 7359 | int |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 7360 | xmlBufferLength(const xmlBuffer *buf) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7361 | { |
| 7362 | if(!buf) |
| 7363 | return 0; |
| 7364 | |
| 7365 | return buf->use; |
| 7366 | } |
| 7367 | |
| 7368 | /** |
| 7369 | * xmlBufferResize: |
| 7370 | * @buf: the buffer to resize |
| 7371 | * @size: the desired size |
| 7372 | * |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7373 | * Resize a buffer to accommodate minimum size of @size. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7374 | * |
| 7375 | * Returns 0 in case of problems, 1 otherwise |
| 7376 | */ |
| 7377 | int |
| 7378 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
| 7379 | { |
| 7380 | unsigned int newSize; |
| 7381 | xmlChar* rebuf = NULL; |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7382 | size_t start_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7383 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7384 | if (buf == NULL) |
| 7385 | return(0); |
| 7386 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7387 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); |
| 7388 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7389 | /* Don't resize if we don't have to */ |
| 7390 | if (size < buf->size) |
| 7391 | return 1; |
| 7392 | |
| 7393 | /* figure out new size */ |
| 7394 | switch (buf->alloc){ |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7395 | case XML_BUFFER_ALLOC_IO: |
| 7396 | case XML_BUFFER_ALLOC_DOUBLEIT: |
| 7397 | /*take care of empty case*/ |
| 7398 | newSize = (buf->size ? buf->size*2 : size + 10); |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 7399 | while (size > newSize) { |
| 7400 | if (newSize > UINT_MAX / 2) { |
| 7401 | xmlTreeErrMemory("growing buffer"); |
| 7402 | return 0; |
| 7403 | } |
| 7404 | newSize *= 2; |
| 7405 | } |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7406 | break; |
| 7407 | case XML_BUFFER_ALLOC_EXACT: |
| 7408 | newSize = size+10; |
| 7409 | break; |
Conrad Irwin | 7d0d2a5 | 2012-05-14 14:18:58 +0800 | [diff] [blame] | 7410 | case XML_BUFFER_ALLOC_HYBRID: |
| 7411 | if (buf->use < BASE_BUFFER_SIZE) |
| 7412 | newSize = size; |
| 7413 | else { |
| 7414 | newSize = buf->size * 2; |
| 7415 | while (size > newSize) { |
| 7416 | if (newSize > UINT_MAX / 2) { |
| 7417 | xmlTreeErrMemory("growing buffer"); |
| 7418 | return 0; |
| 7419 | } |
| 7420 | newSize *= 2; |
| 7421 | } |
| 7422 | } |
| 7423 | break; |
| 7424 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7425 | default: |
| 7426 | newSize = size+10; |
| 7427 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7428 | } |
| 7429 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7430 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7431 | start_buf = buf->content - buf->contentIO; |
| 7432 | |
| 7433 | if (start_buf > newSize) { |
| 7434 | /* move data back to start */ |
| 7435 | memmove(buf->contentIO, buf->content, buf->use); |
| 7436 | buf->content = buf->contentIO; |
| 7437 | buf->content[buf->use] = 0; |
| 7438 | buf->size += start_buf; |
| 7439 | } else { |
| 7440 | rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize); |
| 7441 | if (rebuf == NULL) { |
| 7442 | xmlTreeErrMemory("growing buffer"); |
| 7443 | return 0; |
| 7444 | } |
| 7445 | buf->contentIO = rebuf; |
| 7446 | buf->content = rebuf + start_buf; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7447 | } |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7448 | } else { |
| 7449 | if (buf->content == NULL) { |
| 7450 | rebuf = (xmlChar *) xmlMallocAtomic(newSize); |
| 7451 | } else if (buf->size - buf->use < 100) { |
| 7452 | rebuf = (xmlChar *) xmlRealloc(buf->content, newSize); |
| 7453 | } else { |
| 7454 | /* |
| 7455 | * if we are reallocating a buffer far from being full, it's |
| 7456 | * better to make a new allocation and copy only the used range |
| 7457 | * and free the old one. |
| 7458 | */ |
| 7459 | rebuf = (xmlChar *) xmlMallocAtomic(newSize); |
| 7460 | if (rebuf != NULL) { |
| 7461 | memcpy(rebuf, buf->content, buf->use); |
| 7462 | xmlFree(buf->content); |
| 7463 | rebuf[buf->use] = 0; |
| 7464 | } |
| 7465 | } |
| 7466 | if (rebuf == NULL) { |
| 7467 | xmlTreeErrMemory("growing buffer"); |
| 7468 | return 0; |
| 7469 | } |
| 7470 | buf->content = rebuf; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7471 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7472 | buf->size = newSize; |
| 7473 | |
| 7474 | return 1; |
| 7475 | } |
| 7476 | |
| 7477 | /** |
| 7478 | * xmlBufferAdd: |
| 7479 | * @buf: the buffer to dump |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7480 | * @str: the #xmlChar string |
| 7481 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7482 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7483 | * 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] | 7484 | * str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7485 | * |
| 7486 | * Returns 0 successful, a positive error code number otherwise |
| 7487 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7488 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7489 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7490 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 7491 | unsigned int needSize; |
| 7492 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7493 | if ((str == NULL) || (buf == NULL)) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7494 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7495 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7496 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7497 | if (len < -1) { |
| 7498 | #ifdef DEBUG_BUFFER |
| 7499 | xmlGenericError(xmlGenericErrorContext, |
| 7500 | "xmlBufferAdd: len < 0\n"); |
| 7501 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7502 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7503 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7504 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7505 | |
| 7506 | if (len < 0) |
| 7507 | len = xmlStrlen(str); |
| 7508 | |
Daniel Veillard | c992332 | 2007-04-24 18:12:06 +0000 | [diff] [blame] | 7509 | if (len < 0) return -1; |
| 7510 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7511 | |
| 7512 | needSize = buf->use + len + 2; |
| 7513 | if (needSize > buf->size){ |
| 7514 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7515 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7516 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7517 | } |
| 7518 | } |
| 7519 | |
| 7520 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
| 7521 | buf->use += len; |
| 7522 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7523 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7524 | } |
| 7525 | |
| 7526 | /** |
| 7527 | * xmlBufferAddHead: |
| 7528 | * @buf: the buffer |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7529 | * @str: the #xmlChar string |
| 7530 | * @len: the number of #xmlChar to add |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7531 | * |
| 7532 | * Add a string range to the beginning of an XML buffer. |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7533 | * if len == -1, the length of @str is recomputed. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7534 | * |
| 7535 | * Returns 0 successful, a positive error code number otherwise |
| 7536 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7537 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7538 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7539 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
| 7540 | unsigned int needSize; |
| 7541 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7542 | if (buf == NULL) |
| 7543 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7544 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7545 | if (str == NULL) { |
| 7546 | #ifdef DEBUG_BUFFER |
| 7547 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7548 | "xmlBufferAddHead: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7549 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7550 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7551 | } |
| 7552 | if (len < -1) { |
| 7553 | #ifdef DEBUG_BUFFER |
| 7554 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7555 | "xmlBufferAddHead: len < 0\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7556 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7557 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7558 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7559 | if (len == 0) return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7560 | |
| 7561 | if (len < 0) |
| 7562 | len = xmlStrlen(str); |
| 7563 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7564 | if (len <= 0) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7565 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7566 | if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { |
| 7567 | size_t start_buf = buf->content - buf->contentIO; |
| 7568 | |
| 7569 | if (start_buf > (unsigned int) len) { |
| 7570 | /* |
| 7571 | * We can add it in the space previously shrinked |
| 7572 | */ |
| 7573 | buf->content -= len; |
| 7574 | memmove(&buf->content[0], str, len); |
| 7575 | buf->use += len; |
| 7576 | buf->size += len; |
| 7577 | return(0); |
| 7578 | } |
| 7579 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7580 | needSize = buf->use + len + 2; |
| 7581 | if (needSize > buf->size){ |
| 7582 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7583 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7584 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7585 | } |
| 7586 | } |
| 7587 | |
Daniel Veillard | e83e93e | 2008-08-30 12:52:26 +0000 | [diff] [blame] | 7588 | memmove(&buf->content[len], &buf->content[0], buf->use); |
| 7589 | memmove(&buf->content[0], str, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7590 | buf->use += len; |
| 7591 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7592 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7593 | } |
| 7594 | |
| 7595 | /** |
| 7596 | * xmlBufferCat: |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7597 | * @buf: the buffer to add to |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7598 | * @str: the #xmlChar string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7599 | * |
| 7600 | * Append a zero terminated string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7601 | * |
| 7602 | * Returns 0 successful, a positive error code number otherwise |
| 7603 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7604 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7605 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7606 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7607 | if (buf == NULL) |
| 7608 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7609 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
| 7610 | if (str == NULL) return -1; |
| 7611 | return xmlBufferAdd(buf, str, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7612 | } |
| 7613 | |
| 7614 | /** |
| 7615 | * xmlBufferCCat: |
| 7616 | * @buf: the buffer to dump |
| 7617 | * @str: the C char string |
| 7618 | * |
| 7619 | * Append a zero terminated C string to an XML buffer. |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7620 | * |
| 7621 | * Returns 0 successful, a positive error code number otherwise |
| 7622 | * and -1 in case of internal or API error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7623 | */ |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7624 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7625 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 7626 | const char *cur; |
| 7627 | |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7628 | if (buf == NULL) |
| 7629 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7630 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7631 | if (str == NULL) { |
| 7632 | #ifdef DEBUG_BUFFER |
| 7633 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7634 | "xmlBufferCCat: str == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7635 | #endif |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7636 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7637 | } |
| 7638 | for (cur = str;*cur != 0;cur++) { |
| 7639 | if (buf->use + 10 >= buf->size) { |
| 7640 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 7641 | xmlTreeErrMemory("growing buffer"); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7642 | return XML_ERR_NO_MEMORY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7643 | } |
| 7644 | } |
| 7645 | buf->content[buf->use++] = *cur; |
| 7646 | } |
| 7647 | buf->content[buf->use] = 0; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 7648 | return 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7649 | } |
| 7650 | |
| 7651 | /** |
| 7652 | * xmlBufferWriteCHAR: |
| 7653 | * @buf: the XML buffer |
| 7654 | * @string: the string to add |
| 7655 | * |
| 7656 | * routine which manages and grows an output buffer. This one adds |
| 7657 | * xmlChars at the end of the buffer. |
| 7658 | */ |
| 7659 | void |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7660 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7661 | if (buf == NULL) |
| 7662 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7663 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7664 | xmlBufferCat(buf, string); |
| 7665 | } |
| 7666 | |
| 7667 | /** |
| 7668 | * xmlBufferWriteChar: |
| 7669 | * @buf: the XML buffer output |
| 7670 | * @string: the string to add |
| 7671 | * |
| 7672 | * routine which manage and grows an output buffer. This one add |
| 7673 | * C chars at the end of the array. |
| 7674 | */ |
| 7675 | void |
| 7676 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7677 | if (buf == NULL) |
| 7678 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7679 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7680 | xmlBufferCCat(buf, string); |
| 7681 | } |
| 7682 | |
| 7683 | |
| 7684 | /** |
| 7685 | * xmlBufferWriteQuotedString: |
| 7686 | * @buf: the XML buffer output |
| 7687 | * @string: the string to add |
| 7688 | * |
| 7689 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | d164092 | 2001-12-17 15:30:10 +0000 | [diff] [blame] | 7690 | * a quoted or double quoted #xmlChar string, checking first if it holds |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7691 | * quote or double-quotes internally |
| 7692 | */ |
| 7693 | void |
| 7694 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7695 | const xmlChar *cur, *base; |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 7696 | if (buf == NULL) |
| 7697 | return; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 7698 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7699 | if (xmlStrchr(string, '\"')) { |
Daniel Veillard | 20aa0fb | 2003-08-04 19:43:15 +0000 | [diff] [blame] | 7700 | if (xmlStrchr(string, '\'')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7701 | #ifdef DEBUG_BUFFER |
| 7702 | xmlGenericError(xmlGenericErrorContext, |
| 7703 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
| 7704 | #endif |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7705 | xmlBufferCCat(buf, "\""); |
| 7706 | base = cur = string; |
| 7707 | while(*cur != 0){ |
| 7708 | if(*cur == '"'){ |
| 7709 | if (base != cur) |
| 7710 | xmlBufferAdd(buf, base, cur - base); |
| 7711 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 7712 | cur++; |
| 7713 | base = cur; |
| 7714 | } |
| 7715 | else { |
| 7716 | cur++; |
| 7717 | } |
| 7718 | } |
| 7719 | if (base != cur) |
| 7720 | xmlBufferAdd(buf, base, cur - base); |
| 7721 | xmlBufferCCat(buf, "\""); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7722 | } |
Daniel Veillard | 39057f4 | 2003-08-04 01:33:43 +0000 | [diff] [blame] | 7723 | else{ |
| 7724 | xmlBufferCCat(buf, "\'"); |
| 7725 | xmlBufferCat(buf, string); |
| 7726 | xmlBufferCCat(buf, "\'"); |
| 7727 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7728 | } else { |
| 7729 | xmlBufferCCat(buf, "\""); |
| 7730 | xmlBufferCat(buf, string); |
| 7731 | xmlBufferCCat(buf, "\""); |
| 7732 | } |
| 7733 | } |
| 7734 | |
| 7735 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7736 | /** |
| 7737 | * xmlGetDocCompressMode: |
| 7738 | * @doc: the document |
| 7739 | * |
| 7740 | * get the compression ratio for a document, ZLIB based |
| 7741 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7742 | */ |
| 7743 | int |
Kurt Roeckx | 95ebe53 | 2014-10-13 16:06:21 +0800 | [diff] [blame] | 7744 | xmlGetDocCompressMode (const xmlDoc *doc) { |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7745 | if (doc == NULL) return(-1); |
| 7746 | return(doc->compression); |
| 7747 | } |
| 7748 | |
| 7749 | /** |
| 7750 | * xmlSetDocCompressMode: |
| 7751 | * @doc: the document |
| 7752 | * @mode: the compression ratio |
| 7753 | * |
| 7754 | * set the compression ratio for a document, ZLIB based |
| 7755 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7756 | */ |
| 7757 | void |
| 7758 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
| 7759 | if (doc == NULL) return; |
| 7760 | if (mode < 0) doc->compression = 0; |
| 7761 | else if (mode > 9) doc->compression = 9; |
| 7762 | else doc->compression = mode; |
| 7763 | } |
| 7764 | |
| 7765 | /** |
| 7766 | * xmlGetCompressMode: |
| 7767 | * |
| 7768 | * get the default compression mode used, ZLIB based. |
| 7769 | * Returns 0 (uncompressed) to 9 (max compression) |
| 7770 | */ |
| 7771 | int |
| 7772 | xmlGetCompressMode(void) |
| 7773 | { |
| 7774 | return (xmlCompressMode); |
| 7775 | } |
| 7776 | |
| 7777 | /** |
| 7778 | * xmlSetCompressMode: |
| 7779 | * @mode: the compression ratio |
| 7780 | * |
| 7781 | * set the default compression mode used, ZLIB based |
| 7782 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 7783 | */ |
| 7784 | void |
| 7785 | xmlSetCompressMode(int mode) { |
| 7786 | if (mode < 0) xmlCompressMode = 0; |
| 7787 | else if (mode > 9) xmlCompressMode = 9; |
| 7788 | else xmlCompressMode = mode; |
| 7789 | } |
| 7790 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7791 | #define XML_TREE_NSMAP_PARENT -1 |
| 7792 | #define XML_TREE_NSMAP_XML -2 |
| 7793 | #define XML_TREE_NSMAP_DOC -3 |
| 7794 | #define XML_TREE_NSMAP_CUSTOM -4 |
| 7795 | |
| 7796 | typedef struct xmlNsMapItem *xmlNsMapItemPtr; |
| 7797 | struct xmlNsMapItem { |
| 7798 | xmlNsMapItemPtr next; |
| 7799 | xmlNsMapItemPtr prev; |
| 7800 | xmlNsPtr oldNs; /* old ns decl reference */ |
| 7801 | xmlNsPtr newNs; /* new ns decl reference */ |
| 7802 | int shadowDepth; /* Shadowed at this depth */ |
| 7803 | /* |
| 7804 | * depth: |
| 7805 | * >= 0 == @node's ns-decls |
| 7806 | * -1 == @parent's ns-decls |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 7807 | * -2 == the doc->oldNs XML ns-decl |
| 7808 | * -3 == the doc->oldNs storage ns-decls |
| 7809 | * -4 == ns-decls provided via custom ns-handling |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7810 | */ |
| 7811 | int depth; |
| 7812 | }; |
| 7813 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7814 | typedef struct xmlNsMap *xmlNsMapPtr; |
| 7815 | struct xmlNsMap { |
| 7816 | xmlNsMapItemPtr first; |
| 7817 | xmlNsMapItemPtr last; |
| 7818 | xmlNsMapItemPtr pool; |
| 7819 | }; |
| 7820 | |
| 7821 | #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL)) |
| 7822 | #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next) |
| 7823 | #define XML_NSMAP_POP(m, i) \ |
| 7824 | i = (m)->last; \ |
| 7825 | (m)->last = (i)->prev; \ |
| 7826 | if ((m)->last == NULL) \ |
| 7827 | (m)->first = NULL; \ |
| 7828 | else \ |
| 7829 | (m)->last->next = NULL; \ |
| 7830 | (i)->next = (m)->pool; \ |
| 7831 | (m)->pool = i; |
| 7832 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7833 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7834 | * xmlDOMWrapNsMapFree: |
| 7835 | * @map: the ns-map |
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 | * Frees the ns-map |
| 7838 | */ |
| 7839 | static void |
| 7840 | xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap) |
| 7841 | { |
| 7842 | xmlNsMapItemPtr cur, tmp; |
| 7843 | |
| 7844 | if (nsmap == NULL) |
| 7845 | return; |
| 7846 | cur = nsmap->pool; |
| 7847 | while (cur != NULL) { |
| 7848 | tmp = cur; |
| 7849 | cur = cur->next; |
| 7850 | xmlFree(tmp); |
| 7851 | } |
| 7852 | cur = nsmap->first; |
| 7853 | while (cur != NULL) { |
| 7854 | tmp = cur; |
| 7855 | cur = cur->next; |
| 7856 | xmlFree(tmp); |
| 7857 | } |
| 7858 | xmlFree(nsmap); |
| 7859 | } |
| 7860 | |
| 7861 | /* |
| 7862 | * xmlDOMWrapNsMapAddItem: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7863 | * @map: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7864 | * @oldNs: the old ns-struct |
| 7865 | * @newNs: the new ns-struct |
| 7866 | * @depth: depth and ns-kind information |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7867 | * |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7868 | * Adds an ns-mapping item. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7869 | */ |
| 7870 | static xmlNsMapItemPtr |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7871 | xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7872 | xmlNsPtr oldNs, xmlNsPtr newNs, int depth) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7873 | { |
| 7874 | xmlNsMapItemPtr ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7875 | xmlNsMapPtr map; |
| 7876 | |
| 7877 | if (nsmap == NULL) |
| 7878 | return(NULL); |
| 7879 | if ((position != -1) && (position != 0)) |
| 7880 | return(NULL); |
| 7881 | map = *nsmap; |
| 7882 | |
| 7883 | if (map == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7884 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7885 | * Create the ns-map. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7886 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7887 | map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap)); |
| 7888 | if (map == NULL) { |
| 7889 | xmlTreeErrMemory("allocating namespace map"); |
| 7890 | return (NULL); |
| 7891 | } |
| 7892 | memset(map, 0, sizeof(struct xmlNsMap)); |
| 7893 | *nsmap = map; |
| 7894 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7895 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7896 | if (map->pool != NULL) { |
| 7897 | /* |
| 7898 | * Reuse an item from the pool. |
| 7899 | */ |
| 7900 | ret = map->pool; |
| 7901 | map->pool = ret->next; |
| 7902 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7903 | } else { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7904 | /* |
| 7905 | * Create a new item. |
| 7906 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7907 | ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem)); |
| 7908 | if (ret == NULL) { |
| 7909 | xmlTreeErrMemory("allocating namespace map item"); |
| 7910 | return (NULL); |
| 7911 | } |
| 7912 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7913 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7914 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7915 | if (map->first == NULL) { |
| 7916 | /* |
| 7917 | * First ever. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7918 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7919 | map->first = ret; |
| 7920 | map->last = ret; |
| 7921 | } else if (position == -1) { |
| 7922 | /* |
| 7923 | * Append. |
| 7924 | */ |
| 7925 | ret->prev = map->last; |
| 7926 | map->last->next = ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7927 | map->last = ret; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7928 | } else if (position == 0) { |
| 7929 | /* |
| 7930 | * Set on first position. |
| 7931 | */ |
| 7932 | map->first->prev = ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7933 | ret->next = map->first; |
| 7934 | map->first = ret; |
Gaurav Gupta | 6d93e9e | 2014-10-06 20:20:00 +0800 | [diff] [blame] | 7935 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 7936 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7937 | ret->oldNs = oldNs; |
| 7938 | ret->newNs = newNs; |
| 7939 | ret->shadowDepth = -1; |
| 7940 | ret->depth = depth; |
| 7941 | return (ret); |
| 7942 | } |
| 7943 | |
| 7944 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7945 | * xmlDOMWrapStoreNs: |
| 7946 | * @doc: the doc |
| 7947 | * @nsName: the namespace name |
| 7948 | * @prefix: the prefix |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7949 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7950 | * Creates or reuses an xmlNs struct on doc->oldNs with |
| 7951 | * the given prefix and namespace name. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 7952 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7953 | * Returns the aquired ns struct or NULL in case of an API |
| 7954 | * or internal error. |
| 7955 | */ |
| 7956 | static xmlNsPtr |
| 7957 | xmlDOMWrapStoreNs(xmlDocPtr doc, |
| 7958 | const xmlChar *nsName, |
| 7959 | const xmlChar *prefix) |
| 7960 | { |
| 7961 | xmlNsPtr ns; |
| 7962 | |
| 7963 | if (doc == NULL) |
| 7964 | return (NULL); |
| 7965 | ns = xmlTreeEnsureXMLDecl(doc); |
| 7966 | if (ns == NULL) |
| 7967 | return (NULL); |
| 7968 | if (ns->next != NULL) { |
| 7969 | /* Reuse. */ |
| 7970 | ns = ns->next; |
| 7971 | while (ns != NULL) { |
| 7972 | if (((ns->prefix == prefix) || |
| 7973 | xmlStrEqual(ns->prefix, prefix)) && |
| 7974 | xmlStrEqual(ns->href, nsName)) { |
| 7975 | return (ns); |
| 7976 | } |
| 7977 | if (ns->next == NULL) |
| 7978 | break; |
| 7979 | ns = ns->next; |
| 7980 | } |
| 7981 | } |
| 7982 | /* Create. */ |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 7983 | if (ns != NULL) { |
| 7984 | ns->next = xmlNewNs(NULL, nsName, prefix); |
| 7985 | return (ns->next); |
| 7986 | } |
| 7987 | return(NULL); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 7988 | } |
| 7989 | |
| 7990 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7991 | * xmlDOMWrapNewCtxt: |
| 7992 | * |
| 7993 | * Allocates and initializes a new DOM-wrapper context. |
| 7994 | * |
Jan Pokorný | 7580165 | 2013-12-19 15:09:14 +0100 | [diff] [blame] | 7995 | * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 7996 | */ |
| 7997 | xmlDOMWrapCtxtPtr |
| 7998 | xmlDOMWrapNewCtxt(void) |
| 7999 | { |
| 8000 | xmlDOMWrapCtxtPtr ret; |
| 8001 | |
| 8002 | ret = xmlMalloc(sizeof(xmlDOMWrapCtxt)); |
| 8003 | if (ret == NULL) { |
| 8004 | xmlTreeErrMemory("allocating DOM-wrapper context"); |
| 8005 | return (NULL); |
| 8006 | } |
| 8007 | memset(ret, 0, sizeof(xmlDOMWrapCtxt)); |
| 8008 | return (ret); |
| 8009 | } |
| 8010 | |
| 8011 | /* |
| 8012 | * xmlDOMWrapFreeCtxt: |
| 8013 | * @ctxt: the DOM-wrapper context |
| 8014 | * |
| 8015 | * Frees the DOM-wrapper context. |
| 8016 | */ |
| 8017 | void |
| 8018 | xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt) |
| 8019 | { |
| 8020 | if (ctxt == NULL) |
| 8021 | return; |
| 8022 | if (ctxt->namespaceMap != NULL) |
| 8023 | xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap); |
| 8024 | /* |
| 8025 | * TODO: Store the namespace map in the context. |
| 8026 | */ |
| 8027 | xmlFree(ctxt); |
| 8028 | } |
| 8029 | |
| 8030 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8031 | * xmlTreeLookupNsListByPrefix: |
| 8032 | * @nsList: a list of ns-structs |
| 8033 | * @prefix: the searched prefix |
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 | * Searches for a ns-decl with the given prefix in @nsList. |
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 the ns-decl if found, NULL if not found and on |
| 8038 | * API errors. |
| 8039 | */ |
| 8040 | static xmlNsPtr |
| 8041 | xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix) |
| 8042 | { |
| 8043 | if (nsList == NULL) |
| 8044 | return (NULL); |
| 8045 | { |
| 8046 | xmlNsPtr ns; |
| 8047 | ns = nsList; |
| 8048 | do { |
| 8049 | if ((prefix == ns->prefix) || |
| 8050 | xmlStrEqual(prefix, ns->prefix)) { |
| 8051 | return (ns); |
| 8052 | } |
| 8053 | ns = ns->next; |
| 8054 | } while (ns != NULL); |
| 8055 | } |
| 8056 | return (NULL); |
| 8057 | } |
| 8058 | |
| 8059 | /* |
| 8060 | * |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8061 | * xmlDOMWrapNSNormGatherInScopeNs: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8062 | * @map: the namespace map |
| 8063 | * @node: the node to start with |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8064 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8065 | * Puts in-scope namespaces into the ns-map. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8066 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8067 | * Returns 0 on success, -1 on API or internal errors. |
| 8068 | */ |
| 8069 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8070 | xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8071 | xmlNodePtr node) |
| 8072 | { |
| 8073 | xmlNodePtr cur; |
| 8074 | xmlNsPtr ns; |
| 8075 | xmlNsMapItemPtr mi; |
| 8076 | int shadowed; |
| 8077 | |
| 8078 | if ((map == NULL) || (*map != NULL)) |
| 8079 | return (-1); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8080 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 8081 | return (-1); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8082 | /* |
| 8083 | * Get in-scope ns-decls of @parent. |
| 8084 | */ |
| 8085 | cur = node; |
| 8086 | while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) { |
| 8087 | if (cur->type == XML_ELEMENT_NODE) { |
| 8088 | if (cur->nsDef != NULL) { |
| 8089 | ns = cur->nsDef; |
| 8090 | do { |
| 8091 | shadowed = 0; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8092 | if (XML_NSMAP_NOTEMPTY(*map)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8093 | /* |
| 8094 | * Skip shadowed prefixes. |
| 8095 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8096 | XML_NSMAP_FOREACH(*map, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8097 | if ((ns->prefix == mi->newNs->prefix) || |
| 8098 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) { |
| 8099 | shadowed = 1; |
| 8100 | break; |
| 8101 | } |
| 8102 | } |
| 8103 | } |
| 8104 | /* |
| 8105 | * Insert mapping. |
| 8106 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8107 | mi = xmlDOMWrapNsMapAddItem(map, 0, NULL, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8108 | ns, XML_TREE_NSMAP_PARENT); |
| 8109 | if (mi == NULL) |
| 8110 | return (-1); |
| 8111 | if (shadowed) |
| 8112 | mi->shadowDepth = 0; |
| 8113 | ns = ns->next; |
| 8114 | } while (ns != NULL); |
| 8115 | } |
| 8116 | } |
| 8117 | cur = cur->parent; |
| 8118 | } |
| 8119 | return (0); |
| 8120 | } |
| 8121 | |
| 8122 | /* |
| 8123 | * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict; |
| 8124 | * otherwise copy it, when it was in the source-dict. |
| 8125 | */ |
| 8126 | #define XML_TREE_ADOPT_STR(str) \ |
| 8127 | if (adoptStr && (str != NULL)) { \ |
| 8128 | if (destDoc->dict) { \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 8129 | const xmlChar *old = str; \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8130 | str = xmlDictLookup(destDoc->dict, str, -1); \ |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 8131 | if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \ |
| 8132 | (!xmlDictOwns(sourceDoc->dict, old))) \ |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 8133 | xmlFree((char *)old); \ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8134 | } else if ((sourceDoc) && (sourceDoc->dict) && \ |
| 8135 | xmlDictOwns(sourceDoc->dict, str)) { \ |
| 8136 | str = BAD_CAST xmlStrdup(str); \ |
| 8137 | } \ |
| 8138 | } |
| 8139 | |
| 8140 | /* |
| 8141 | * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then |
| 8142 | * put it in dest-dict or copy it. |
| 8143 | */ |
| 8144 | #define XML_TREE_ADOPT_STR_2(str) \ |
| 8145 | if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \ |
| 8146 | (sourceDoc->dict != NULL) && \ |
| 8147 | xmlDictOwns(sourceDoc->dict, cur->content)) { \ |
| 8148 | if (destDoc->dict) \ |
| 8149 | cur->content = (xmlChar *) \ |
| 8150 | xmlDictLookup(destDoc->dict, cur->content, -1); \ |
| 8151 | else \ |
| 8152 | cur->content = xmlStrdup(BAD_CAST cur->content); \ |
| 8153 | } |
| 8154 | |
| 8155 | /* |
| 8156 | * xmlDOMWrapNSNormAddNsMapItem2: |
| 8157 | * |
| 8158 | * For internal use. Adds a ns-decl mapping. |
| 8159 | * |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8160 | * Returns 0 on success, -1 on internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8161 | */ |
| 8162 | static int |
| 8163 | xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number, |
| 8164 | xmlNsPtr oldNs, xmlNsPtr newNs) |
| 8165 | { |
| 8166 | if (*list == NULL) { |
| 8167 | *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr)); |
| 8168 | if (*list == NULL) { |
| 8169 | xmlTreeErrMemory("alloc ns map item"); |
| 8170 | return(-1); |
| 8171 | } |
| 8172 | *size = 3; |
| 8173 | *number = 0; |
| 8174 | } else if ((*number) >= (*size)) { |
| 8175 | *size *= 2; |
| 8176 | *list = (xmlNsPtr *) xmlRealloc(*list, |
| 8177 | (*size) * 2 * sizeof(xmlNsPtr)); |
| 8178 | if (*list == NULL) { |
| 8179 | xmlTreeErrMemory("realloc ns map item"); |
| 8180 | return(-1); |
| 8181 | } |
| 8182 | } |
| 8183 | (*list)[2 * (*number)] = oldNs; |
| 8184 | (*list)[2 * (*number) +1] = newNs; |
| 8185 | (*number)++; |
| 8186 | return (0); |
| 8187 | } |
| 8188 | |
| 8189 | /* |
| 8190 | * xmlDOMWrapRemoveNode: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8191 | * @ctxt: a DOM wrapper context |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8192 | * @doc: the doc |
| 8193 | * @node: the node to be removed. |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8194 | * @options: set of options, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8195 | * |
| 8196 | * Unlinks the given node from its owner. |
| 8197 | * This will substitute ns-references to node->nsDef for |
| 8198 | * ns-references to doc->oldNs, thus ensuring the removed |
| 8199 | * branch to be autark wrt ns-references. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8200 | * |
| 8201 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8202 | * |
| 8203 | * Returns 0 on success, 1 if the node is not supported, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8204 | * -1 on API and internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8205 | */ |
| 8206 | int |
| 8207 | xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc, |
| 8208 | xmlNodePtr node, int options ATTRIBUTE_UNUSED) |
| 8209 | { |
| 8210 | xmlNsPtr *list = NULL; |
| 8211 | int sizeList, nbList, i, j; |
| 8212 | xmlNsPtr ns; |
| 8213 | |
| 8214 | if ((node == NULL) || (doc == NULL) || (node->doc != doc)) |
| 8215 | return (-1); |
| 8216 | |
| 8217 | /* TODO: 0 or -1 ? */ |
| 8218 | if (node->parent == NULL) |
| 8219 | return (0); |
| 8220 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8221 | switch (node->type) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8222 | case XML_TEXT_NODE: |
| 8223 | case XML_CDATA_SECTION_NODE: |
| 8224 | case XML_ENTITY_REF_NODE: |
| 8225 | case XML_PI_NODE: |
| 8226 | case XML_COMMENT_NODE: |
| 8227 | xmlUnlinkNode(node); |
| 8228 | return (0); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8229 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8230 | case XML_ATTRIBUTE_NODE: |
| 8231 | break; |
| 8232 | default: |
| 8233 | return (1); |
| 8234 | } |
| 8235 | xmlUnlinkNode(node); |
| 8236 | /* |
| 8237 | * Save out-of-scope ns-references in doc->oldNs. |
| 8238 | */ |
| 8239 | do { |
| 8240 | switch (node->type) { |
| 8241 | case XML_ELEMENT_NODE: |
| 8242 | if ((ctxt == NULL) && (node->nsDef != NULL)) { |
| 8243 | ns = node->nsDef; |
| 8244 | do { |
| 8245 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 8246 | &nbList, ns, ns) == -1) |
| 8247 | goto internal_error; |
| 8248 | ns = ns->next; |
| 8249 | } while (ns != NULL); |
| 8250 | } |
| 8251 | /* No break on purpose. */ |
| 8252 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8253 | if (node->ns != NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8254 | /* |
| 8255 | * Find a mapping. |
| 8256 | */ |
| 8257 | if (list != NULL) { |
| 8258 | for (i = 0, j = 0; i < nbList; i++, j += 2) { |
| 8259 | if (node->ns == list[j]) { |
| 8260 | node->ns = list[++j]; |
| 8261 | goto next_node; |
| 8262 | } |
| 8263 | } |
| 8264 | } |
| 8265 | ns = NULL; |
| 8266 | if (ctxt != NULL) { |
| 8267 | /* |
| 8268 | * User defined. |
| 8269 | */ |
| 8270 | } else { |
| 8271 | /* |
| 8272 | * Add to doc's oldNs. |
| 8273 | */ |
| 8274 | ns = xmlDOMWrapStoreNs(doc, node->ns->href, |
| 8275 | node->ns->prefix); |
| 8276 | if (ns == NULL) |
| 8277 | goto internal_error; |
| 8278 | } |
| 8279 | if (ns != NULL) { |
| 8280 | /* |
| 8281 | * Add mapping. |
| 8282 | */ |
| 8283 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
| 8284 | &nbList, node->ns, ns) == -1) |
| 8285 | goto internal_error; |
| 8286 | } |
| 8287 | node->ns = ns; |
| 8288 | } |
| 8289 | if ((node->type == XML_ELEMENT_NODE) && |
| 8290 | (node->properties != NULL)) { |
| 8291 | node = (xmlNodePtr) node->properties; |
| 8292 | continue; |
| 8293 | } |
| 8294 | break; |
| 8295 | default: |
| 8296 | goto next_sibling; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8297 | } |
| 8298 | next_node: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8299 | if ((node->type == XML_ELEMENT_NODE) && |
| 8300 | (node->children != NULL)) { |
| 8301 | node = node->children; |
| 8302 | continue; |
| 8303 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8304 | next_sibling: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8305 | if (node == NULL) |
| 8306 | break; |
| 8307 | if (node->next != NULL) |
| 8308 | node = node->next; |
| 8309 | else { |
| 8310 | node = node->parent; |
| 8311 | goto next_sibling; |
| 8312 | } |
| 8313 | } while (node != NULL); |
| 8314 | |
| 8315 | if (list != NULL) |
| 8316 | xmlFree(list); |
| 8317 | return (0); |
| 8318 | |
| 8319 | internal_error: |
| 8320 | if (list != NULL) |
| 8321 | xmlFree(list); |
| 8322 | return (-1); |
| 8323 | } |
| 8324 | |
| 8325 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8326 | * xmlSearchNsByNamespaceStrict: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8327 | * @doc: the document |
| 8328 | * @node: the start node |
| 8329 | * @nsName: the searched namespace name |
| 8330 | * @retNs: the resulting ns-decl |
| 8331 | * @prefixed: if the found ns-decl must have a prefix (for attributes) |
| 8332 | * |
| 8333 | * Dynamically searches for a ns-declaration which matches |
| 8334 | * the given @nsName in the ancestor-or-self axis of @node. |
| 8335 | * |
| 8336 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8337 | * and internal errors. |
| 8338 | */ |
| 8339 | static int |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8340 | xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node, |
| 8341 | const xmlChar* nsName, |
| 8342 | xmlNsPtr *retNs, int prefixed) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8343 | { |
| 8344 | xmlNodePtr cur, prev = NULL, out = NULL; |
| 8345 | xmlNsPtr ns, prevns; |
| 8346 | |
| 8347 | if ((doc == NULL) || (nsName == NULL) || (retNs == NULL)) |
| 8348 | return (-1); |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8349 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 8350 | return(-1); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8351 | |
| 8352 | *retNs = NULL; |
| 8353 | if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
| 8354 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8355 | if (*retNs == NULL) |
| 8356 | return (-1); |
| 8357 | return (1); |
| 8358 | } |
| 8359 | cur = node; |
| 8360 | do { |
| 8361 | if (cur->type == XML_ELEMENT_NODE) { |
| 8362 | if (cur->nsDef != NULL) { |
| 8363 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 8364 | if (prefixed && (ns->prefix == NULL)) |
| 8365 | continue; |
| 8366 | if (prev != NULL) { |
| 8367 | /* |
| 8368 | * Check the last level of ns-decls for a |
| 8369 | * shadowing prefix. |
| 8370 | */ |
| 8371 | prevns = prev->nsDef; |
| 8372 | do { |
| 8373 | if ((prevns->prefix == ns->prefix) || |
| 8374 | ((prevns->prefix != NULL) && |
| 8375 | (ns->prefix != NULL) && |
| 8376 | xmlStrEqual(prevns->prefix, ns->prefix))) { |
| 8377 | /* |
| 8378 | * Shadowed. |
| 8379 | */ |
| 8380 | break; |
| 8381 | } |
| 8382 | prevns = prevns->next; |
| 8383 | } while (prevns != NULL); |
| 8384 | if (prevns != NULL) |
| 8385 | continue; |
| 8386 | } |
| 8387 | /* |
| 8388 | * Ns-name comparison. |
| 8389 | */ |
| 8390 | if ((nsName == ns->href) || |
| 8391 | xmlStrEqual(nsName, ns->href)) { |
| 8392 | /* |
| 8393 | * At this point the prefix can only be shadowed, |
| 8394 | * if we are the the (at least) 3rd level of |
| 8395 | * ns-decls. |
| 8396 | */ |
| 8397 | if (out) { |
| 8398 | int ret; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8399 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8400 | ret = xmlNsInScope(doc, node, prev, ns->prefix); |
| 8401 | if (ret < 0) |
| 8402 | return (-1); |
| 8403 | /* |
| 8404 | * TODO: Should we try to find a matching ns-name |
| 8405 | * only once? This here keeps on searching. |
| 8406 | * I think we should try further since, there might |
| 8407 | * be an other matching ns-decl with an unshadowed |
| 8408 | * prefix. |
| 8409 | */ |
| 8410 | if (! ret) |
| 8411 | continue; |
| 8412 | } |
| 8413 | *retNs = ns; |
| 8414 | return (1); |
| 8415 | } |
| 8416 | } |
| 8417 | out = prev; |
| 8418 | prev = cur; |
| 8419 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8420 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 8421 | (cur->type == XML_ENTITY_DECL)) |
| 8422 | return (0); |
| 8423 | cur = cur->parent; |
| 8424 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 8425 | return (0); |
| 8426 | } |
| 8427 | |
| 8428 | /* |
| 8429 | * xmlSearchNsByPrefixStrict: |
| 8430 | * @doc: the document |
| 8431 | * @node: the start node |
| 8432 | * @prefix: the searched namespace prefix |
| 8433 | * @retNs: the resulting ns-decl |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8434 | * |
| 8435 | * Dynamically searches for a ns-declaration which matches |
| 8436 | * the given @nsName in the ancestor-or-self axis of @node. |
| 8437 | * |
| 8438 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8439 | * and internal errors. |
| 8440 | */ |
| 8441 | static int |
| 8442 | xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node, |
| 8443 | const xmlChar* prefix, |
| 8444 | xmlNsPtr *retNs) |
| 8445 | { |
| 8446 | xmlNodePtr cur; |
| 8447 | xmlNsPtr ns; |
| 8448 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8449 | if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 8450 | return(-1); |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8451 | |
| 8452 | if (retNs) |
| 8453 | *retNs = NULL; |
| 8454 | if (IS_STR_XML(prefix)) { |
| 8455 | if (retNs) { |
| 8456 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8457 | if (*retNs == NULL) |
| 8458 | return (-1); |
| 8459 | } |
| 8460 | return (1); |
| 8461 | } |
| 8462 | cur = node; |
| 8463 | do { |
| 8464 | if (cur->type == XML_ELEMENT_NODE) { |
| 8465 | if (cur->nsDef != NULL) { |
| 8466 | ns = cur->nsDef; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8467 | do { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8468 | if ((prefix == ns->prefix) || |
| 8469 | xmlStrEqual(prefix, ns->prefix)) |
| 8470 | { |
| 8471 | /* |
| 8472 | * Disabled namespaces, e.g. xmlns:abc="". |
| 8473 | */ |
| 8474 | if (ns->href == NULL) |
| 8475 | return(0); |
| 8476 | if (retNs) |
| 8477 | *retNs = ns; |
| 8478 | return (1); |
| 8479 | } |
| 8480 | ns = ns->next; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8481 | } while (ns != NULL); |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8482 | } |
| 8483 | } else if ((cur->type == XML_ENTITY_NODE) || |
| 8484 | (cur->type == XML_ENTITY_DECL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8485 | return (0); |
| 8486 | cur = cur->parent; |
| 8487 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
| 8488 | return (0); |
| 8489 | } |
| 8490 | |
| 8491 | /* |
| 8492 | * xmlDOMWrapNSNormDeclareNsForced: |
| 8493 | * @doc: the doc |
| 8494 | * @elem: the element-node to declare on |
| 8495 | * @nsName: the namespace-name of the ns-decl |
| 8496 | * @prefix: the preferred prefix of the ns-decl |
| 8497 | * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls |
| 8498 | * |
| 8499 | * Declares a new namespace on @elem. It tries to use the |
| 8500 | * given @prefix; if a ns-decl with the given prefix is already existent |
| 8501 | * on @elem, it will generate an other prefix. |
| 8502 | * |
| 8503 | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
| 8504 | * and internal errors. |
| 8505 | */ |
| 8506 | static xmlNsPtr |
| 8507 | xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc, |
| 8508 | xmlNodePtr elem, |
| 8509 | const xmlChar *nsName, |
| 8510 | const xmlChar *prefix, |
| 8511 | int checkShadow) |
| 8512 | { |
| 8513 | |
| 8514 | xmlNsPtr ret; |
| 8515 | char buf[50]; |
| 8516 | const xmlChar *pref; |
| 8517 | int counter = 0; |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 8518 | |
| 8519 | if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE)) |
| 8520 | return(NULL); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8521 | /* |
| 8522 | * Create a ns-decl on @anchor. |
| 8523 | */ |
| 8524 | pref = prefix; |
| 8525 | while (1) { |
| 8526 | /* |
| 8527 | * Lookup whether the prefix is unused in elem's ns-decls. |
| 8528 | */ |
| 8529 | if ((elem->nsDef != NULL) && |
| 8530 | (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL)) |
| 8531 | goto ns_next_prefix; |
| 8532 | if (checkShadow && elem->parent && |
| 8533 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8534 | /* |
| 8535 | * Does it shadow ancestor ns-decls? |
| 8536 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8537 | if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8538 | goto ns_next_prefix; |
| 8539 | } |
| 8540 | ret = xmlNewNs(NULL, nsName, pref); |
| 8541 | if (ret == NULL) |
| 8542 | return (NULL); |
| 8543 | if (elem->nsDef == NULL) |
| 8544 | elem->nsDef = ret; |
| 8545 | else { |
| 8546 | xmlNsPtr ns2 = elem->nsDef; |
| 8547 | while (ns2->next != NULL) |
| 8548 | ns2 = ns2->next; |
| 8549 | ns2->next = ret; |
| 8550 | } |
| 8551 | return (ret); |
| 8552 | ns_next_prefix: |
| 8553 | counter++; |
| 8554 | if (counter > 1000) |
| 8555 | return (NULL); |
| 8556 | if (prefix == NULL) { |
| 8557 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8558 | "ns_%d", counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8559 | } else |
| 8560 | snprintf((char *) buf, sizeof(buf), |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8561 | "%.30s_%d", (char *)prefix, counter); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8562 | pref = BAD_CAST buf; |
| 8563 | } |
| 8564 | } |
| 8565 | |
| 8566 | /* |
| 8567 | * xmlDOMWrapNSNormAquireNormalizedNs: |
| 8568 | * @doc: the doc |
| 8569 | * @elem: the element-node to declare namespaces on |
| 8570 | * @ns: the ns-struct to use for the search |
| 8571 | * @retNs: the found/created ns-struct |
| 8572 | * @nsMap: the ns-map |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8573 | * @depth: the current tree depth |
| 8574 | * @ancestorsOnly: search in ancestor ns-decls only |
| 8575 | * @prefixed: if the searched ns-decl must have a prefix (for attributes) |
| 8576 | * |
| 8577 | * Searches for a matching ns-name in the ns-decls of @nsMap, if not |
| 8578 | * found it will either declare it on @elem, or store it in doc->oldNs. |
| 8579 | * If a new ns-decl needs to be declared on @elem, it tries to use the |
| 8580 | * @ns->prefix for it, if this prefix is already in use on @elem, it will |
| 8581 | * change the prefix or the new ns-decl. |
| 8582 | * |
| 8583 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8584 | */ |
| 8585 | static int |
| 8586 | xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc, |
| 8587 | xmlNodePtr elem, |
| 8588 | xmlNsPtr ns, |
| 8589 | xmlNsPtr *retNs, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8590 | xmlNsMapPtr *nsMap, |
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 | int depth, |
| 8593 | int ancestorsOnly, |
| 8594 | int prefixed) |
| 8595 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8596 | xmlNsMapItemPtr mi; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8597 | |
| 8598 | if ((doc == NULL) || (ns == NULL) || (retNs == NULL) || |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8599 | (nsMap == NULL)) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8600 | return (-1); |
| 8601 | |
| 8602 | *retNs = NULL; |
| 8603 | /* |
| 8604 | * Handle XML namespace. |
| 8605 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8606 | if (IS_STR_XML(ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8607 | /* |
| 8608 | * Insert XML namespace mapping. |
| 8609 | */ |
| 8610 | *retNs = xmlTreeEnsureXMLDecl(doc); |
| 8611 | if (*retNs == NULL) |
| 8612 | return (-1); |
| 8613 | return (0); |
| 8614 | } |
| 8615 | /* |
| 8616 | * If the search should be done in ancestors only and no |
| 8617 | * @elem (the first ancestor) was specified, then skip the search. |
| 8618 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8619 | if ((XML_NSMAP_NOTEMPTY(*nsMap)) && |
| 8620 | (! (ancestorsOnly && (elem == NULL)))) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8621 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8622 | /* |
| 8623 | * Try to find an equal ns-name in in-scope ns-decls. |
| 8624 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8625 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8626 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8627 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8628 | * ancestorsOnly: This should be turned on to gain speed, |
| 8629 | * if one knows that the branch itself was already |
| 8630 | * ns-wellformed and no stale references existed. |
| 8631 | * I.e. it searches in the ancestor axis only. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8632 | */ |
| 8633 | ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) && |
| 8634 | /* Skip shadowed prefixes. */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8635 | (mi->shadowDepth == -1) && |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8636 | /* Skip xmlns="" or xmlns:foo="". */ |
| 8637 | ((mi->newNs->href != NULL) && |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8638 | (mi->newNs->href[0] != 0)) && |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8639 | /* Ensure a prefix if wanted. */ |
| 8640 | ((! prefixed) || (mi->newNs->prefix != NULL)) && |
| 8641 | /* Equal ns name */ |
| 8642 | ((mi->newNs->href == ns->href) || |
| 8643 | xmlStrEqual(mi->newNs->href, ns->href))) { |
| 8644 | /* Set the mapping. */ |
| 8645 | mi->oldNs = ns; |
| 8646 | *retNs = mi->newNs; |
| 8647 | return (0); |
| 8648 | } |
| 8649 | } |
| 8650 | } |
| 8651 | /* |
| 8652 | * No luck, the namespace is out of scope or shadowed. |
| 8653 | */ |
| 8654 | if (elem == NULL) { |
| 8655 | xmlNsPtr tmpns; |
| 8656 | |
| 8657 | /* |
| 8658 | * Store ns-decls in "oldNs" of the document-node. |
| 8659 | */ |
| 8660 | tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix); |
| 8661 | if (tmpns == NULL) |
| 8662 | return (-1); |
| 8663 | /* |
| 8664 | * Insert mapping. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8665 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8666 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8667 | tmpns, XML_TREE_NSMAP_DOC) == NULL) { |
| 8668 | xmlFreeNs(tmpns); |
| 8669 | return (-1); |
| 8670 | } |
| 8671 | *retNs = tmpns; |
| 8672 | } else { |
| 8673 | xmlNsPtr tmpns; |
| 8674 | |
| 8675 | tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href, |
| 8676 | ns->prefix, 0); |
| 8677 | if (tmpns == NULL) |
| 8678 | return (-1); |
| 8679 | |
| 8680 | if (*nsMap != NULL) { |
| 8681 | /* |
| 8682 | * Does it shadow ancestor ns-decls? |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8683 | */ |
| 8684 | XML_NSMAP_FOREACH(*nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8685 | if ((mi->depth < depth) && |
| 8686 | (mi->shadowDepth == -1) && |
| 8687 | ((ns->prefix == mi->newNs->prefix) || |
| 8688 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
| 8689 | /* |
| 8690 | * Shadows. |
| 8691 | */ |
| 8692 | mi->shadowDepth = depth; |
| 8693 | break; |
| 8694 | } |
| 8695 | } |
| 8696 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8697 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8698 | xmlFreeNs(tmpns); |
| 8699 | return (-1); |
| 8700 | } |
| 8701 | *retNs = tmpns; |
| 8702 | } |
| 8703 | return (0); |
| 8704 | } |
| 8705 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8706 | typedef enum { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8707 | XML_DOM_RECONNS_REMOVEREDUND = 1<<0 |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8708 | } xmlDOMReconcileNSOptions; |
| 8709 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8710 | /* |
| 8711 | * xmlDOMWrapReconcileNamespaces: |
Daniel Veillard | 304e78c | 2005-07-03 16:19:41 +0000 | [diff] [blame] | 8712 | * @ctxt: DOM wrapper context, unused at the moment |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8713 | * @elem: the element-node |
| 8714 | * @options: option flags |
| 8715 | * |
| 8716 | * Ensures that ns-references point to ns-decls hold on element-nodes. |
| 8717 | * Ensures that the tree is namespace wellformed by creating additional |
| 8718 | * ns-decls where needed. Note that, since prefixes of already existent |
| 8719 | * ns-decls can be shadowed by this process, it could break QNames in |
| 8720 | * attribute values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8721 | * |
| 8722 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8723 | * |
| 8724 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8725 | */ |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8726 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8727 | int |
| 8728 | xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8729 | xmlNodePtr elem, |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8730 | int options) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8731 | { |
| 8732 | int depth = -1, adoptns = 0, parnsdone = 0; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8733 | xmlNsPtr ns, prevns; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8734 | xmlDocPtr doc; |
| 8735 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8736 | xmlNsMapPtr nsMap = NULL; |
| 8737 | xmlNsMapItemPtr /* topmi = NULL, */ mi; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8738 | /* @ancestorsOnly should be set by an option flag. */ |
| 8739 | int ancestorsOnly = 0; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8740 | int optRemoveRedundantNS = |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8741 | ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0; |
| 8742 | xmlNsPtr *listRedund = NULL; |
| 8743 | int sizeRedund = 0, nbRedund = 0, ret, i, j; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8744 | |
| 8745 | if ((elem == NULL) || (elem->doc == NULL) || |
| 8746 | (elem->type != XML_ELEMENT_NODE)) |
| 8747 | return (-1); |
| 8748 | |
| 8749 | doc = elem->doc; |
| 8750 | cur = elem; |
| 8751 | do { |
| 8752 | switch (cur->type) { |
| 8753 | case XML_ELEMENT_NODE: |
| 8754 | adoptns = 1; |
| 8755 | curElem = cur; |
| 8756 | depth++; |
| 8757 | /* |
| 8758 | * Namespace declarations. |
| 8759 | */ |
| 8760 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8761 | prevns = NULL; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8762 | ns = cur->nsDef; |
| 8763 | while (ns != NULL) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8764 | if (! parnsdone) { |
| 8765 | if ((elem->parent) && |
| 8766 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8767 | /* |
| 8768 | * Gather ancestor in-scope ns-decls. |
| 8769 | */ |
| 8770 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8771 | elem->parent) == -1) |
| 8772 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8773 | } |
| 8774 | parnsdone = 1; |
| 8775 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8776 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8777 | /* |
| 8778 | * Lookup the ns ancestor-axis for equal ns-decls in scope. |
| 8779 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8780 | if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8781 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8782 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8783 | (mi->shadowDepth == -1) && |
| 8784 | ((ns->prefix == mi->newNs->prefix) || |
| 8785 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) && |
| 8786 | ((ns->href == mi->newNs->href) || |
| 8787 | xmlStrEqual(ns->href, mi->newNs->href))) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8788 | { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8789 | /* |
| 8790 | * A redundant ns-decl was found. |
| 8791 | * Add it to the list of redundant ns-decls. |
| 8792 | */ |
| 8793 | if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund, |
| 8794 | &sizeRedund, &nbRedund, ns, mi->newNs) == -1) |
| 8795 | goto internal_error; |
| 8796 | /* |
| 8797 | * Remove the ns-decl from the element-node. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8798 | */ |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8799 | if (prevns) |
| 8800 | prevns->next = ns->next; |
| 8801 | else |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8802 | cur->nsDef = ns->next; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8803 | goto next_ns_decl; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8804 | } |
| 8805 | } |
| 8806 | } |
| 8807 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8808 | /* |
| 8809 | * Skip ns-references handling if the referenced |
| 8810 | * ns-decl is declared on the same element. |
| 8811 | */ |
| 8812 | if ((cur->ns != NULL) && adoptns && (cur->ns == ns)) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8813 | adoptns = 0; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8814 | /* |
| 8815 | * Does it shadow any ns-decl? |
| 8816 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8817 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
| 8818 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8819 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 8820 | (mi->shadowDepth == -1) && |
| 8821 | ((ns->prefix == mi->newNs->prefix) || |
| 8822 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8823 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8824 | mi->shadowDepth = depth; |
| 8825 | } |
| 8826 | } |
| 8827 | } |
| 8828 | /* |
| 8829 | * Push mapping. |
| 8830 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8831 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8832 | depth) == NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8833 | goto internal_error; |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8834 | |
| 8835 | prevns = ns; |
Kasimier T. Buchcik | e8f8d75 | 2006-02-02 12:13:07 +0000 | [diff] [blame] | 8836 | next_ns_decl: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8837 | ns = ns->next; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8838 | } |
| 8839 | } |
| 8840 | if (! adoptns) |
| 8841 | goto ns_end; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8842 | /* No break on purpose. */ |
| 8843 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8844 | /* No ns, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8845 | if (cur->ns == NULL) |
| 8846 | goto ns_end; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8847 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8848 | if (! parnsdone) { |
| 8849 | if ((elem->parent) && |
| 8850 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
| 8851 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 8852 | elem->parent) == -1) |
| 8853 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8854 | } |
| 8855 | parnsdone = 1; |
| 8856 | } |
| 8857 | /* |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8858 | * Adjust the reference if this was a redundant ns-decl. |
| 8859 | */ |
| 8860 | if (listRedund) { |
| 8861 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8862 | if (cur->ns == listRedund[j]) { |
| 8863 | cur->ns = listRedund[++j]; |
| 8864 | break; |
| 8865 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8866 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8867 | } |
| 8868 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8869 | * Adopt ns-references. |
| 8870 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8871 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8872 | /* |
| 8873 | * Search for a mapping. |
| 8874 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8875 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8876 | if ((mi->shadowDepth == -1) && |
| 8877 | (cur->ns == mi->oldNs)) { |
| 8878 | |
| 8879 | cur->ns = mi->newNs; |
| 8880 | goto ns_end; |
| 8881 | } |
| 8882 | } |
| 8883 | } |
| 8884 | /* |
| 8885 | * Aquire a normalized ns-decl and add it to the map. |
| 8886 | */ |
| 8887 | if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem, |
| 8888 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8889 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8890 | ancestorsOnly, |
| 8891 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 8892 | goto internal_error; |
| 8893 | cur->ns = ns; |
| 8894 | |
| 8895 | ns_end: |
| 8896 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8897 | (cur->properties != NULL)) { |
| 8898 | /* |
| 8899 | * Process attributes. |
| 8900 | */ |
| 8901 | cur = (xmlNodePtr) cur->properties; |
| 8902 | continue; |
| 8903 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8904 | break; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8905 | default: |
| 8906 | goto next_sibling; |
| 8907 | } |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8908 | into_content: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8909 | if ((cur->type == XML_ELEMENT_NODE) && |
| 8910 | (cur->children != NULL)) { |
| 8911 | /* |
| 8912 | * Process content of element-nodes only. |
| 8913 | */ |
| 8914 | cur = cur->children; |
| 8915 | continue; |
| 8916 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8917 | next_sibling: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8918 | if (cur == elem) |
| 8919 | break; |
| 8920 | if (cur->type == XML_ELEMENT_NODE) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8921 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8922 | /* |
| 8923 | * Pop mappings. |
| 8924 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8925 | while ((nsMap->last != NULL) && |
| 8926 | (nsMap->last->depth >= depth)) |
| 8927 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8928 | XML_NSMAP_POP(nsMap, mi) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8929 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8930 | /* |
| 8931 | * Unshadow. |
| 8932 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8933 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8934 | if (mi->shadowDepth >= depth) |
| 8935 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8936 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8937 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8938 | depth--; |
| 8939 | } |
| 8940 | if (cur->next != NULL) |
| 8941 | cur = cur->next; |
| 8942 | else { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8943 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 8944 | cur = cur->parent; |
| 8945 | goto into_content; |
| 8946 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8947 | cur = cur->parent; |
| 8948 | goto next_sibling; |
| 8949 | } |
| 8950 | } while (cur != NULL); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8951 | |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8952 | ret = 0; |
| 8953 | goto exit; |
| 8954 | internal_error: |
| 8955 | ret = -1; |
| 8956 | exit: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 8957 | if (listRedund) { |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8958 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
| 8959 | xmlFreeNs(listRedund[j]); |
| 8960 | } |
| 8961 | xmlFree(listRedund); |
| 8962 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8963 | if (nsMap != NULL) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 8964 | xmlDOMWrapNsMapFree(nsMap); |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8965 | return (ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8966 | } |
| 8967 | |
| 8968 | /* |
| 8969 | * xmlDOMWrapAdoptBranch: |
| 8970 | * @ctxt: the optional context for custom processing |
| 8971 | * @sourceDoc: the optional sourceDoc |
| 8972 | * @node: the element-node to start with |
| 8973 | * @destDoc: the destination doc for adoption |
Kasimier T. Buchcik | e01b2fd | 2006-02-01 16:36:13 +0000 | [diff] [blame] | 8974 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8975 | * @options: option flags |
| 8976 | * |
| 8977 | * Ensures that ns-references point to @destDoc: either to |
| 8978 | * elements->nsDef entries if @destParent is given, or to |
| 8979 | * @destDoc->oldNs otherwise. |
| 8980 | * If @destParent is given, it ensures that the tree is namespace |
| 8981 | * wellformed by creating additional ns-decls where needed. |
| 8982 | * Note that, since prefixes of already existent ns-decls can be |
| 8983 | * shadowed by this process, it could break QNames in attribute |
| 8984 | * values or element content. |
| 8985 | * |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 8986 | * NOTE: This function was not intensively tested. |
| 8987 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 8988 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 8989 | */ |
| 8990 | static int |
| 8991 | xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt, |
| 8992 | xmlDocPtr sourceDoc, |
| 8993 | xmlNodePtr node, |
| 8994 | xmlDocPtr destDoc, |
| 8995 | xmlNodePtr destParent, |
| 8996 | int options ATTRIBUTE_UNUSED) |
| 8997 | { |
| 8998 | int ret = 0; |
| 8999 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9000 | xmlNsMapPtr nsMap = NULL; |
| 9001 | xmlNsMapItemPtr mi; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9002 | xmlNsPtr ns = NULL; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9003 | int depth = -1, adoptStr = 1; |
| 9004 | /* gather @parent's ns-decls. */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9005 | int parnsdone; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9006 | /* @ancestorsOnly should be set per option. */ |
| 9007 | int ancestorsOnly = 0; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9008 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9009 | /* |
| 9010 | * Optimize string adoption for equal or none dicts. |
| 9011 | */ |
| 9012 | if ((sourceDoc != NULL) && |
| 9013 | (sourceDoc->dict == destDoc->dict)) |
| 9014 | adoptStr = 0; |
| 9015 | else |
| 9016 | adoptStr = 1; |
| 9017 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9018 | /* |
| 9019 | * Get the ns-map from the context if available. |
| 9020 | */ |
| 9021 | if (ctxt) |
| 9022 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 9023 | /* |
| 9024 | * Disable search for ns-decls in the parent-axis of the |
| 9025 | * desination element, if: |
| 9026 | * 1) there's no destination parent |
| 9027 | * 2) custom ns-reference handling is used |
| 9028 | */ |
| 9029 | if ((destParent == NULL) || |
| 9030 | (ctxt && ctxt->getNsForNodeFunc)) |
| 9031 | { |
| 9032 | parnsdone = 1; |
| 9033 | } else |
| 9034 | parnsdone = 0; |
| 9035 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9036 | cur = node; |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 9037 | if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) |
| 9038 | goto internal_error; |
| 9039 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9040 | while (cur != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9041 | /* |
| 9042 | * Paranoid source-doc sanity check. |
| 9043 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9044 | if (cur->doc != sourceDoc) { |
| 9045 | /* |
| 9046 | * We'll assume XIncluded nodes if the doc differs. |
| 9047 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 9048 | * This here skips XIncluded nodes and tries to handle |
| 9049 | * broken sequences. |
| 9050 | */ |
| 9051 | if (cur->next == NULL) |
| 9052 | goto leave_node; |
| 9053 | do { |
| 9054 | cur = cur->next; |
| 9055 | if ((cur->type == XML_XINCLUDE_END) || |
| 9056 | (cur->doc == node->doc)) |
| 9057 | break; |
| 9058 | } while (cur->next != NULL); |
| 9059 | |
| 9060 | if (cur->doc != node->doc) |
| 9061 | goto leave_node; |
| 9062 | } |
| 9063 | cur->doc = destDoc; |
| 9064 | switch (cur->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9065 | case XML_XINCLUDE_START: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9066 | case XML_XINCLUDE_END: |
| 9067 | /* |
| 9068 | * TODO |
| 9069 | */ |
| 9070 | return (-1); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9071 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9072 | curElem = cur; |
| 9073 | depth++; |
| 9074 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9075 | * Namespace declarations. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9076 | * - ns->href and ns->prefix are never in the dict, so |
| 9077 | * we need not move the values over to the destination dict. |
| 9078 | * - Note that for custom handling of ns-references, |
| 9079 | * the ns-decls need not be stored in the ns-map, |
| 9080 | * since they won't be referenced by node->ns. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9081 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9082 | if ((cur->nsDef) && |
| 9083 | ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL))) |
| 9084 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9085 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9086 | /* |
| 9087 | * Gather @parent's in-scope ns-decls. |
| 9088 | */ |
| 9089 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 9090 | destParent) == -1) |
| 9091 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9092 | parnsdone = 1; |
| 9093 | } |
| 9094 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 9095 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9096 | * NOTE: ns->prefix and ns->href are never in the dict. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9097 | * XML_TREE_ADOPT_STR(ns->prefix) |
| 9098 | * XML_TREE_ADOPT_STR(ns->href) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9099 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9100 | /* |
| 9101 | * Does it shadow any ns-decl? |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9102 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9103 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9104 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9105 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 9106 | (mi->shadowDepth == -1) && |
| 9107 | ((ns->prefix == mi->newNs->prefix) || |
| 9108 | xmlStrEqual(ns->prefix, |
| 9109 | mi->newNs->prefix))) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9110 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9111 | mi->shadowDepth = depth; |
| 9112 | } |
| 9113 | } |
| 9114 | } |
| 9115 | /* |
| 9116 | * Push mapping. |
| 9117 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9118 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9119 | ns, ns, depth) == NULL) |
| 9120 | goto internal_error; |
| 9121 | } |
| 9122 | } |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9123 | /* No break on purpose. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9124 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9125 | /* No namespace, no fun. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9126 | if (cur->ns == NULL) |
| 9127 | goto ns_end; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9128 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9129 | if (! parnsdone) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9130 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 9131 | destParent) == -1) |
| 9132 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9133 | parnsdone = 1; |
| 9134 | } |
| 9135 | /* |
| 9136 | * Adopt ns-references. |
| 9137 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9138 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9139 | /* |
| 9140 | * Search for a mapping. |
| 9141 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9142 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9143 | if ((mi->shadowDepth == -1) && |
| 9144 | (cur->ns == mi->oldNs)) { |
| 9145 | |
| 9146 | cur->ns = mi->newNs; |
| 9147 | goto ns_end; |
| 9148 | } |
| 9149 | } |
| 9150 | } |
| 9151 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9152 | * No matching namespace in scope. We need a new one. |
| 9153 | */ |
| 9154 | if ((ctxt) && (ctxt->getNsForNodeFunc)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9155 | /* |
| 9156 | * User-defined behaviour. |
| 9157 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9158 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 9159 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9160 | /* |
| 9161 | * Insert mapping if ns is available; it's the users fault |
| 9162 | * if not. |
| 9163 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9164 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9165 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9166 | goto internal_error; |
| 9167 | cur->ns = ns; |
| 9168 | } else { |
| 9169 | /* |
| 9170 | * Aquire a normalized ns-decl and add it to the map. |
| 9171 | */ |
| 9172 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9173 | /* ns-decls on curElem or on destDoc->oldNs */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9174 | destParent ? curElem : NULL, |
| 9175 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9176 | &nsMap, depth, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9177 | ancestorsOnly, |
| 9178 | /* ns-decls must be prefixed for attributes. */ |
| 9179 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 9180 | goto internal_error; |
| 9181 | cur->ns = ns; |
| 9182 | } |
| 9183 | ns_end: |
| 9184 | /* |
| 9185 | * Further node properties. |
| 9186 | * TODO: Is this all? |
| 9187 | */ |
| 9188 | XML_TREE_ADOPT_STR(cur->name) |
| 9189 | if (cur->type == XML_ELEMENT_NODE) { |
| 9190 | cur->psvi = NULL; |
| 9191 | cur->line = 0; |
| 9192 | cur->extra = 0; |
| 9193 | /* |
| 9194 | * Walk attributes. |
| 9195 | */ |
| 9196 | if (cur->properties != NULL) { |
| 9197 | /* |
| 9198 | * Process first attribute node. |
| 9199 | */ |
| 9200 | cur = (xmlNodePtr) cur->properties; |
| 9201 | continue; |
| 9202 | } |
| 9203 | } else { |
| 9204 | /* |
| 9205 | * Attributes. |
| 9206 | */ |
| 9207 | if ((sourceDoc != NULL) && |
| 9208 | (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID)) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9209 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9210 | xmlRemoveID(sourceDoc, (xmlAttrPtr) cur); |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9211 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9212 | ((xmlAttrPtr) cur)->atype = 0; |
| 9213 | ((xmlAttrPtr) cur)->psvi = NULL; |
| 9214 | } |
| 9215 | break; |
| 9216 | case XML_TEXT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9217 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9218 | /* |
| 9219 | * This puts the content in the dest dict, only if |
| 9220 | * it was previously in the source dict. |
| 9221 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9222 | XML_TREE_ADOPT_STR_2(cur->content) |
| 9223 | goto leave_node; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9224 | case XML_ENTITY_REF_NODE: |
| 9225 | /* |
| 9226 | * Remove reference to the entitity-node. |
| 9227 | */ |
| 9228 | cur->content = NULL; |
| 9229 | cur->children = NULL; |
| 9230 | cur->last = NULL; |
| 9231 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9232 | xmlEntityPtr ent; |
| 9233 | /* |
| 9234 | * Assign new entity-node if available. |
| 9235 | */ |
| 9236 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9237 | if (ent != NULL) { |
| 9238 | cur->content = ent->content; |
| 9239 | cur->children = (xmlNodePtr) ent; |
| 9240 | cur->last = (xmlNodePtr) ent; |
| 9241 | } |
| 9242 | } |
| 9243 | goto leave_node; |
| 9244 | case XML_PI_NODE: |
| 9245 | XML_TREE_ADOPT_STR(cur->name) |
| 9246 | XML_TREE_ADOPT_STR_2(cur->content) |
| 9247 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9248 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9249 | break; |
| 9250 | default: |
| 9251 | goto internal_error; |
| 9252 | } |
| 9253 | /* |
| 9254 | * Walk the tree. |
| 9255 | */ |
| 9256 | if (cur->children != NULL) { |
| 9257 | cur = cur->children; |
| 9258 | continue; |
| 9259 | } |
| 9260 | |
| 9261 | leave_node: |
| 9262 | if (cur == node) |
| 9263 | break; |
| 9264 | if ((cur->type == XML_ELEMENT_NODE) || |
| 9265 | (cur->type == XML_XINCLUDE_START) || |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9266 | (cur->type == XML_XINCLUDE_END)) |
| 9267 | { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9268 | /* |
| 9269 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 9270 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9271 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9272 | /* |
| 9273 | * Pop mappings. |
| 9274 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9275 | while ((nsMap->last != NULL) && |
| 9276 | (nsMap->last->depth >= depth)) |
| 9277 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9278 | XML_NSMAP_POP(nsMap, mi) |
| 9279 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9280 | /* |
| 9281 | * Unshadow. |
| 9282 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9283 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9284 | if (mi->shadowDepth >= depth) |
| 9285 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9286 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9287 | } |
| 9288 | depth--; |
| 9289 | } |
| 9290 | if (cur->next != NULL) |
| 9291 | cur = cur->next; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9292 | else if ((cur->type == XML_ATTRIBUTE_NODE) && |
| 9293 | (cur->parent->children != NULL)) |
| 9294 | { |
| 9295 | cur = cur->parent->children; |
| 9296 | } else { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9297 | cur = cur->parent; |
| 9298 | goto leave_node; |
| 9299 | } |
| 9300 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9301 | |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9302 | goto exit; |
| 9303 | |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9304 | internal_error: |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9305 | ret = -1; |
| 9306 | |
| 9307 | exit: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9308 | /* |
| 9309 | * Cleanup. |
| 9310 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9311 | if (nsMap != NULL) { |
| 9312 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 9313 | /* |
| 9314 | * Just cleanup the map but don't free. |
| 9315 | */ |
| 9316 | if (nsMap->first) { |
| 9317 | if (nsMap->pool) |
| 9318 | nsMap->last->next = nsMap->pool; |
| 9319 | nsMap->pool = nsMap->first; |
| 9320 | nsMap->first = NULL; |
| 9321 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9322 | } else |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9323 | xmlDOMWrapNsMapFree(nsMap); |
| 9324 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9325 | return(ret); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9326 | } |
| 9327 | |
| 9328 | /* |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9329 | * xmlDOMWrapCloneNode: |
| 9330 | * @ctxt: the optional context for custom processing |
| 9331 | * @sourceDoc: the optional sourceDoc |
| 9332 | * @node: the node to start with |
| 9333 | * @resNode: the clone of the given @node |
| 9334 | * @destDoc: the destination doc |
| 9335 | * @destParent: the optional new parent of @node in @destDoc |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 9336 | * @deep: descend into child if set |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9337 | * @options: option flags |
| 9338 | * |
| 9339 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 9340 | * 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] | 9341 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used. |
| 9342 | * This is the case when you don't know already where the cloned branch |
| 9343 | * will be added to. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9344 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9345 | * If @destParent is given, it ensures that the tree is namespace |
| 9346 | * wellformed by creating additional ns-decls where needed. |
| 9347 | * Note that, since prefixes of already existent ns-decls can be |
| 9348 | * shadowed by this process, it could break QNames in attribute |
| 9349 | * values or element content. |
| 9350 | * TODO: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9351 | * 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] | 9352 | * |
| 9353 | * Returns 0 if the operation succeeded, |
| 9354 | * 1 if a node of unsupported (or not yet supported) type was given, |
| 9355 | * -1 on API/internal errors. |
| 9356 | */ |
| 9357 | |
| 9358 | int |
| 9359 | xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt, |
| 9360 | xmlDocPtr sourceDoc, |
| 9361 | xmlNodePtr node, |
| 9362 | xmlNodePtr *resNode, |
| 9363 | xmlDocPtr destDoc, |
| 9364 | xmlNodePtr destParent, |
| 9365 | int deep, |
| 9366 | int options ATTRIBUTE_UNUSED) |
| 9367 | { |
| 9368 | int ret = 0; |
| 9369 | xmlNodePtr cur, curElem = NULL; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9370 | xmlNsMapPtr nsMap = NULL; |
| 9371 | xmlNsMapItemPtr mi; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9372 | xmlNsPtr ns; |
| 9373 | int depth = -1; |
| 9374 | /* int adoptStr = 1; */ |
| 9375 | /* gather @parent's ns-decls. */ |
| 9376 | int parnsdone = 0; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9377 | /* |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9378 | * @ancestorsOnly: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9379 | * TODO: @ancestorsOnly should be set per option. |
| 9380 | * |
| 9381 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9382 | int ancestorsOnly = 0; |
| 9383 | xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9384 | xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL; |
| 9385 | xmlDictPtr dict; /* The destination dict */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9386 | |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9387 | if ((node == NULL) || (resNode == NULL) || (destDoc == NULL)) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9388 | return(-1); |
| 9389 | /* |
| 9390 | * TODO: Initially we support only element-nodes. |
| 9391 | */ |
| 9392 | if (node->type != XML_ELEMENT_NODE) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9393 | return(1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9394 | /* |
| 9395 | * Check node->doc sanity. |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9396 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9397 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 9398 | (node->doc != sourceDoc)) { |
| 9399 | /* |
| 9400 | * Might be an XIncluded node. |
| 9401 | */ |
| 9402 | return (-1); |
| 9403 | } |
| 9404 | if (sourceDoc == NULL) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9405 | sourceDoc = node->doc; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9406 | if (sourceDoc == NULL) |
| 9407 | return (-1); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9408 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9409 | dict = destDoc->dict; |
| 9410 | /* |
| 9411 | * Reuse the namespace map of the context. |
| 9412 | */ |
| 9413 | if (ctxt) |
| 9414 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
| 9415 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9416 | *resNode = NULL; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9417 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9418 | cur = node; |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 9419 | if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) |
| 9420 | return(-1); |
| 9421 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9422 | while (cur != NULL) { |
| 9423 | if (cur->doc != sourceDoc) { |
| 9424 | /* |
| 9425 | * We'll assume XIncluded nodes if the doc differs. |
| 9426 | * TODO: Do we need to reconciliate XIncluded nodes? |
| 9427 | * TODO: This here returns -1 in this case. |
| 9428 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9429 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9430 | } |
| 9431 | /* |
| 9432 | * Create a new node. |
| 9433 | */ |
| 9434 | switch (cur->type) { |
| 9435 | case XML_XINCLUDE_START: |
| 9436 | case XML_XINCLUDE_END: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9437 | /* |
| 9438 | * TODO: What to do with XInclude? |
| 9439 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9440 | goto internal_error; |
| 9441 | break; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9442 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9443 | case XML_TEXT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9444 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9445 | case XML_COMMENT_NODE: |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9446 | case XML_PI_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9447 | case XML_DOCUMENT_FRAG_NODE: |
| 9448 | case XML_ENTITY_REF_NODE: |
| 9449 | case XML_ENTITY_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9450 | /* |
| 9451 | * Nodes of xmlNode structure. |
| 9452 | */ |
| 9453 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 9454 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9455 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9456 | goto internal_error; |
| 9457 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9458 | memset(clone, 0, sizeof(xmlNode)); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9459 | /* |
| 9460 | * Set hierachical links. |
| 9461 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9462 | if (resultClone != NULL) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9463 | clone->parent = parentClone; |
| 9464 | if (prevClone) { |
| 9465 | prevClone->next = clone; |
| 9466 | clone->prev = prevClone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9467 | } else |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9468 | parentClone->children = clone; |
| 9469 | } else |
| 9470 | resultClone = clone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9471 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9472 | break; |
| 9473 | case XML_ATTRIBUTE_NODE: |
| 9474 | /* |
| 9475 | * Attributes (xmlAttr). |
| 9476 | */ |
| 9477 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr)); |
| 9478 | if (clone == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9479 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node"); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9480 | goto internal_error; |
| 9481 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9482 | memset(clone, 0, sizeof(xmlAttr)); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9483 | /* |
| 9484 | * Set hierachical links. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9485 | * TODO: Change this to add to the end of attributes. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9486 | */ |
| 9487 | if (resultClone != NULL) { |
| 9488 | clone->parent = parentClone; |
| 9489 | if (prevClone) { |
| 9490 | prevClone->next = clone; |
| 9491 | clone->prev = prevClone; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9492 | } else |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9493 | parentClone->properties = (xmlAttrPtr) clone; |
| 9494 | } else |
| 9495 | resultClone = clone; |
| 9496 | break; |
| 9497 | default: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9498 | /* |
| 9499 | * TODO QUESTION: Any other nodes expected? |
| 9500 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9501 | goto internal_error; |
| 9502 | } |
| 9503 | |
| 9504 | clone->type = cur->type; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9505 | clone->doc = destDoc; |
| 9506 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9507 | /* |
| 9508 | * Clone the name of the node if any. |
| 9509 | */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9510 | if (cur->name == xmlStringText) |
| 9511 | clone->name = xmlStringText; |
| 9512 | else if (cur->name == xmlStringTextNoenc) |
| 9513 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9514 | * NOTE: Although xmlStringTextNoenc is never assigned to a node |
| 9515 | * in tree.c, it might be set in Libxslt via |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9516 | * "xsl:disable-output-escaping". |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9517 | */ |
| 9518 | clone->name = xmlStringTextNoenc; |
| 9519 | else if (cur->name == xmlStringComment) |
| 9520 | clone->name = xmlStringComment; |
| 9521 | else if (cur->name != NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9522 | DICT_CONST_COPY(cur->name, clone->name); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9523 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9524 | |
| 9525 | switch (cur->type) { |
| 9526 | case XML_XINCLUDE_START: |
| 9527 | case XML_XINCLUDE_END: |
| 9528 | /* |
| 9529 | * TODO |
| 9530 | */ |
| 9531 | return (-1); |
| 9532 | case XML_ELEMENT_NODE: |
| 9533 | curElem = cur; |
| 9534 | depth++; |
| 9535 | /* |
| 9536 | * Namespace declarations. |
| 9537 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9538 | if (cur->nsDef != NULL) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9539 | if (! parnsdone) { |
| 9540 | if (destParent && (ctxt == NULL)) { |
| 9541 | /* |
| 9542 | * Gather @parent's in-scope ns-decls. |
| 9543 | */ |
| 9544 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
| 9545 | destParent) == -1) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9546 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9547 | } |
| 9548 | parnsdone = 1; |
| 9549 | } |
| 9550 | /* |
| 9551 | * Clone namespace declarations. |
| 9552 | */ |
| 9553 | cloneNsDefSlot = &(clone->nsDef); |
| 9554 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 9555 | /* |
| 9556 | * Create a new xmlNs. |
| 9557 | */ |
| 9558 | cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 9559 | if (cloneNs == NULL) { |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9560 | xmlTreeErrMemory("xmlDOMWrapCloneNode(): " |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9561 | "allocating namespace"); |
| 9562 | return(-1); |
| 9563 | } |
| 9564 | memset(cloneNs, 0, sizeof(xmlNs)); |
| 9565 | cloneNs->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9566 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9567 | if (ns->href != NULL) |
| 9568 | cloneNs->href = xmlStrdup(ns->href); |
| 9569 | if (ns->prefix != NULL) |
| 9570 | cloneNs->prefix = xmlStrdup(ns->prefix); |
| 9571 | |
| 9572 | *cloneNsDefSlot = cloneNs; |
| 9573 | cloneNsDefSlot = &(cloneNs->next); |
| 9574 | |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9575 | /* |
| 9576 | * Note that for custom handling of ns-references, |
| 9577 | * the ns-decls need not be stored in the ns-map, |
| 9578 | * since they won't be referenced by node->ns. |
| 9579 | */ |
| 9580 | if ((ctxt == NULL) || |
| 9581 | (ctxt->getNsForNodeFunc == NULL)) |
| 9582 | { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9583 | /* |
| 9584 | * Does it shadow any ns-decl? |
| 9585 | */ |
| 9586 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9587 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9588 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
| 9589 | (mi->shadowDepth == -1) && |
| 9590 | ((ns->prefix == mi->newNs->prefix) || |
| 9591 | xmlStrEqual(ns->prefix, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9592 | mi->newNs->prefix))) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9593 | /* |
| 9594 | * Mark as shadowed at the current |
| 9595 | * depth. |
| 9596 | */ |
| 9597 | mi->shadowDepth = depth; |
| 9598 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9599 | } |
| 9600 | } |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9601 | /* |
| 9602 | * Push mapping. |
| 9603 | */ |
| 9604 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
| 9605 | ns, cloneNs, depth) == NULL) |
| 9606 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9607 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9608 | } |
| 9609 | } |
| 9610 | /* cur->ns will be processed further down. */ |
| 9611 | break; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9612 | case XML_ATTRIBUTE_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9613 | /* IDs will be processed further down. */ |
| 9614 | /* cur->ns will be processed further down. */ |
| 9615 | break; |
| 9616 | case XML_TEXT_NODE: |
| 9617 | case XML_CDATA_SECTION_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9618 | /* |
| 9619 | * Note that this will also cover the values of attributes. |
| 9620 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +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; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9623 | case XML_ENTITY_NODE: |
| 9624 | /* TODO: What to do here? */ |
| 9625 | goto leave_node; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9626 | case XML_ENTITY_REF_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9627 | if (sourceDoc != destDoc) { |
| 9628 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9629 | xmlEntityPtr ent; |
| 9630 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9631 | * Different doc: Assign new entity-node if available. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9632 | */ |
| 9633 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9634 | if (ent != NULL) { |
| 9635 | clone->content = ent->content; |
| 9636 | clone->children = (xmlNodePtr) ent; |
| 9637 | clone->last = (xmlNodePtr) ent; |
| 9638 | } |
| 9639 | } |
| 9640 | } else { |
| 9641 | /* |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9642 | * Same doc: Use the current node's entity declaration |
| 9643 | * and value. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9644 | */ |
| 9645 | clone->content = cur->content; |
| 9646 | clone->children = cur->children; |
| 9647 | clone->last = cur->last; |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9648 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9649 | goto leave_node; |
| 9650 | case XML_PI_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9651 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9652 | goto leave_node; |
| 9653 | case XML_COMMENT_NODE: |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9654 | DICT_COPY(cur->content, clone->content); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9655 | goto leave_node; |
| 9656 | default: |
| 9657 | goto internal_error; |
| 9658 | } |
| 9659 | |
| 9660 | if (cur->ns == NULL) |
| 9661 | goto end_ns_reference; |
| 9662 | |
| 9663 | /* handle_ns_reference: */ |
| 9664 | /* |
| 9665 | ** The following will take care of references to ns-decls ******** |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9666 | ** and is intended only for element- and attribute-nodes. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9667 | ** |
| 9668 | */ |
| 9669 | if (! parnsdone) { |
| 9670 | if (destParent && (ctxt == NULL)) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9671 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9672 | goto internal_error; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9673 | } |
| 9674 | parnsdone = 1; |
| 9675 | } |
| 9676 | /* |
| 9677 | * Adopt ns-references. |
| 9678 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9679 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9680 | /* |
| 9681 | * Search for a mapping. |
| 9682 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9683 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9684 | if ((mi->shadowDepth == -1) && |
| 9685 | (cur->ns == mi->oldNs)) { |
| 9686 | /* |
| 9687 | * This is the nice case: a mapping was found. |
| 9688 | */ |
| 9689 | clone->ns = mi->newNs; |
| 9690 | goto end_ns_reference; |
| 9691 | } |
| 9692 | } |
| 9693 | } |
| 9694 | /* |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9695 | * No matching namespace in scope. We need a new one. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9696 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9697 | if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9698 | /* |
| 9699 | * User-defined behaviour. |
| 9700 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9701 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
| 9702 | cur->ns->href, cur->ns->prefix); |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9703 | /* |
| 9704 | * Add user's mapping. |
| 9705 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9706 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9707 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
| 9708 | goto internal_error; |
| 9709 | clone->ns = ns; |
| 9710 | } else { |
| 9711 | /* |
| 9712 | * Aquire a normalized ns-decl and add it to the map. |
| 9713 | */ |
| 9714 | if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9715 | /* ns-decls on curElem or on destDoc->oldNs */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9716 | destParent ? curElem : NULL, |
| 9717 | cur->ns, &ns, |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9718 | &nsMap, depth, |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9719 | /* if we need to search only in the ancestor-axis */ |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9720 | ancestorsOnly, |
| 9721 | /* ns-decls must be prefixed for attributes. */ |
| 9722 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
| 9723 | goto internal_error; |
| 9724 | clone->ns = ns; |
| 9725 | } |
| 9726 | |
| 9727 | end_ns_reference: |
| 9728 | |
| 9729 | /* |
| 9730 | * Some post-processing. |
| 9731 | * |
| 9732 | * Handle ID attributes. |
| 9733 | */ |
| 9734 | if ((clone->type == XML_ATTRIBUTE_NODE) && |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9735 | (clone->parent != NULL)) |
| 9736 | { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9737 | if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9738 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9739 | xmlChar *idVal; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9740 | |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9741 | idVal = xmlNodeListGetString(cur->doc, cur->children, 1); |
| 9742 | if (idVal != NULL) { |
| 9743 | if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) { |
| 9744 | /* TODO: error message. */ |
| 9745 | xmlFree(idVal); |
| 9746 | goto internal_error; |
| 9747 | } |
| 9748 | xmlFree(idVal); |
| 9749 | } |
| 9750 | } |
| 9751 | } |
| 9752 | /* |
| 9753 | ** |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9754 | ** The following will traverse the tree ************************** |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9755 | ** |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9756 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9757 | * Walk the element's attributes before descending into child-nodes. |
| 9758 | */ |
| 9759 | if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) { |
| 9760 | prevClone = NULL; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9761 | parentClone = clone; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9762 | cur = (xmlNodePtr) cur->properties; |
| 9763 | continue; |
| 9764 | } |
| 9765 | into_content: |
| 9766 | /* |
| 9767 | * Descend into child-nodes. |
| 9768 | */ |
| 9769 | if (cur->children != NULL) { |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9770 | if (deep || (cur->type == XML_ATTRIBUTE_NODE)) { |
| 9771 | prevClone = NULL; |
| 9772 | parentClone = clone; |
| 9773 | cur = cur->children; |
| 9774 | continue; |
| 9775 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9776 | } |
| 9777 | |
| 9778 | leave_node: |
| 9779 | /* |
| 9780 | * At this point we are done with the node, its content |
| 9781 | * and an element-nodes's attribute-nodes. |
| 9782 | */ |
| 9783 | if (cur == node) |
| 9784 | break; |
| 9785 | if ((cur->type == XML_ELEMENT_NODE) || |
| 9786 | (cur->type == XML_XINCLUDE_START) || |
| 9787 | (cur->type == XML_XINCLUDE_END)) { |
| 9788 | /* |
| 9789 | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
| 9790 | */ |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9791 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9792 | /* |
| 9793 | * Pop mappings. |
| 9794 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9795 | while ((nsMap->last != NULL) && |
| 9796 | (nsMap->last->depth >= depth)) |
| 9797 | { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9798 | XML_NSMAP_POP(nsMap, mi) |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9799 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9800 | /* |
| 9801 | * Unshadow. |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9802 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9803 | XML_NSMAP_FOREACH(nsMap, mi) { |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9804 | if (mi->shadowDepth >= depth) |
| 9805 | mi->shadowDepth = -1; |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +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 | depth--; |
| 9809 | } |
| 9810 | if (cur->next != NULL) { |
| 9811 | prevClone = clone; |
| 9812 | cur = cur->next; |
| 9813 | } else if (cur->type != XML_ATTRIBUTE_NODE) { |
| 9814 | /* |
| 9815 | * Set clone->last. |
| 9816 | */ |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 9817 | if (clone->parent != NULL) |
| 9818 | clone->parent->last = clone; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9819 | clone = clone->parent; |
Daniel Veillard | 75d1309 | 2013-09-11 15:11:27 +0800 | [diff] [blame] | 9820 | if (clone != NULL) |
| 9821 | parentClone = clone->parent; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9822 | /* |
| 9823 | * Process parent --> next; |
| 9824 | */ |
| 9825 | cur = cur->parent; |
| 9826 | goto leave_node; |
| 9827 | } else { |
| 9828 | /* This is for attributes only. */ |
| 9829 | clone = clone->parent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9830 | parentClone = clone->parent; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9831 | /* |
| 9832 | * Process parent-element --> children. |
| 9833 | */ |
| 9834 | cur = cur->parent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9835 | goto into_content; |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9836 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9837 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9838 | goto exit; |
| 9839 | |
| 9840 | internal_error: |
| 9841 | ret = -1; |
| 9842 | |
| 9843 | exit: |
| 9844 | /* |
| 9845 | * Cleanup. |
| 9846 | */ |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9847 | if (nsMap != NULL) { |
| 9848 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
| 9849 | /* |
| 9850 | * Just cleanup the map but don't free. |
| 9851 | */ |
| 9852 | if (nsMap->first) { |
| 9853 | if (nsMap->pool) |
| 9854 | nsMap->last->next = nsMap->pool; |
| 9855 | nsMap->pool = nsMap->first; |
| 9856 | nsMap->first = NULL; |
| 9857 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9858 | } else |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 9859 | xmlDOMWrapNsMapFree(nsMap); |
| 9860 | } |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 9861 | /* |
| 9862 | * TODO: Should we try a cleanup of the cloned node in case of a |
| 9863 | * fatal error? |
| 9864 | */ |
| 9865 | *resNode = resultClone; |
| 9866 | return (ret); |
| 9867 | } |
| 9868 | |
| 9869 | /* |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9870 | * xmlDOMWrapAdoptAttr: |
| 9871 | * @ctxt: the optional context for custom processing |
| 9872 | * @sourceDoc: the optional source document of attr |
| 9873 | * @attr: the attribute-node to be adopted |
| 9874 | * @destDoc: the destination doc for adoption |
| 9875 | * @destParent: the optional new parent of @attr in @destDoc |
| 9876 | * @options: option flags |
| 9877 | * |
| 9878 | * @attr is adopted by @destDoc. |
| 9879 | * Ensures that ns-references point to @destDoc: either to |
| 9880 | * elements->nsDef entries if @destParent is given, or to |
| 9881 | * @destDoc->oldNs otherwise. |
| 9882 | * |
| 9883 | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
| 9884 | */ |
| 9885 | static int |
| 9886 | xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt, |
| 9887 | xmlDocPtr sourceDoc, |
| 9888 | xmlAttrPtr attr, |
| 9889 | xmlDocPtr destDoc, |
| 9890 | xmlNodePtr destParent, |
| 9891 | int options ATTRIBUTE_UNUSED) |
| 9892 | { |
| 9893 | xmlNodePtr cur; |
| 9894 | int adoptStr = 1; |
| 9895 | |
| 9896 | if ((attr == NULL) || (destDoc == NULL)) |
| 9897 | return (-1); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9898 | |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9899 | attr->doc = destDoc; |
| 9900 | if (attr->ns != NULL) { |
| 9901 | xmlNsPtr ns = NULL; |
| 9902 | |
| 9903 | if (ctxt != NULL) { |
| 9904 | /* TODO: User defined. */ |
| 9905 | } |
| 9906 | /* XML Namespace. */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9907 | if (IS_STR_XML(attr->ns->prefix)) { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9908 | ns = xmlTreeEnsureXMLDecl(destDoc); |
| 9909 | } else if (destParent == NULL) { |
| 9910 | /* |
| 9911 | * Store in @destDoc->oldNs. |
| 9912 | */ |
| 9913 | ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix); |
| 9914 | } else { |
| 9915 | /* |
| 9916 | * Declare on @destParent. |
| 9917 | */ |
Kasimier T. Buchcik | 4435341 | 2006-03-06 13:26:16 +0000 | [diff] [blame] | 9918 | if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9919 | &ns, 1) == -1) |
| 9920 | goto internal_error; |
| 9921 | if (ns == NULL) { |
| 9922 | ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent, |
| 9923 | attr->ns->href, attr->ns->prefix, 1); |
| 9924 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9925 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9926 | if (ns == NULL) |
| 9927 | goto internal_error; |
| 9928 | attr->ns = ns; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9929 | } |
| 9930 | |
| 9931 | XML_TREE_ADOPT_STR(attr->name); |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9932 | attr->atype = 0; |
| 9933 | attr->psvi = NULL; |
| 9934 | /* |
| 9935 | * Walk content. |
| 9936 | */ |
| 9937 | if (attr->children == NULL) |
| 9938 | return (0); |
| 9939 | cur = attr->children; |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 9940 | if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) |
| 9941 | goto internal_error; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9942 | while (cur != NULL) { |
| 9943 | cur->doc = destDoc; |
| 9944 | switch (cur->type) { |
| 9945 | case XML_TEXT_NODE: |
| 9946 | case XML_CDATA_SECTION_NODE: |
| 9947 | XML_TREE_ADOPT_STR_2(cur->content) |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9948 | break; |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9949 | case XML_ENTITY_REF_NODE: |
| 9950 | /* |
| 9951 | * Remove reference to the entitity-node. |
| 9952 | */ |
| 9953 | cur->content = NULL; |
| 9954 | cur->children = NULL; |
| 9955 | cur->last = NULL; |
| 9956 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 9957 | xmlEntityPtr ent; |
| 9958 | /* |
| 9959 | * Assign new entity-node if available. |
| 9960 | */ |
| 9961 | ent = xmlGetDocEntity(destDoc, cur->name); |
| 9962 | if (ent != NULL) { |
| 9963 | cur->content = ent->content; |
| 9964 | cur->children = (xmlNodePtr) ent; |
| 9965 | cur->last = (xmlNodePtr) ent; |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 9966 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9967 | } |
| 9968 | break; |
| 9969 | default: |
| 9970 | break; |
| 9971 | } |
| 9972 | if (cur->children != NULL) { |
| 9973 | cur = cur->children; |
| 9974 | continue; |
| 9975 | } |
| 9976 | next_sibling: |
| 9977 | if (cur == (xmlNodePtr) attr) |
| 9978 | break; |
| 9979 | if (cur->next != NULL) |
| 9980 | cur = cur->next; |
| 9981 | else { |
| 9982 | cur = cur->parent; |
| 9983 | goto next_sibling; |
| 9984 | } |
| 9985 | } |
| 9986 | return (0); |
| 9987 | internal_error: |
| 9988 | return (-1); |
| 9989 | } |
| 9990 | |
| 9991 | /* |
| 9992 | * xmlDOMWrapAdoptNode: |
| 9993 | * @ctxt: the optional context for custom processing |
| 9994 | * @sourceDoc: the optional sourceDoc |
| 9995 | * @node: the node to start with |
| 9996 | * @destDoc: the destination doc |
Kasimier T. Buchcik | 4d9c948 | 2005-06-27 15:04:46 +0000 | [diff] [blame] | 9997 | * @destParent: the optional new parent of @node in @destDoc |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 9998 | * @options: option flags |
| 9999 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 10000 | * References of out-of scope ns-decls are remapped to point to @destDoc: |
| 10001 | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
| 10002 | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used |
Daniel Veillard | 7e35abe | 2014-03-28 22:55:31 +0800 | [diff] [blame] | 10003 | * 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] | 10004 | * to the context of |
| 10005 | * |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10006 | * If @destParent is given, it ensures that the tree is namespace |
| 10007 | * wellformed by creating additional ns-decls where needed. |
| 10008 | * Note that, since prefixes of already existent ns-decls can be |
| 10009 | * shadowed by this process, it could break QNames in attribute |
| 10010 | * values or element content. |
Kasimier T. Buchcik | 978039b | 2006-06-16 19:46:26 +0000 | [diff] [blame] | 10011 | * NOTE: This function was not intensively tested. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10012 | * |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 10013 | * Returns 0 if the operation succeeded, |
| 10014 | * 1 if a node of unsupported type was given, |
| 10015 | * 2 if a node of not yet supported type was given and |
| 10016 | * -1 on API/internal errors. |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10017 | */ |
| 10018 | int |
| 10019 | xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt, |
| 10020 | xmlDocPtr sourceDoc, |
| 10021 | xmlNodePtr node, |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 10022 | xmlDocPtr destDoc, |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10023 | xmlNodePtr destParent, |
| 10024 | int options) |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 10025 | { |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 10026 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || |
| 10027 | (destDoc == NULL) || |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10028 | ((destParent != NULL) && (destParent->doc != destDoc))) |
| 10029 | return(-1); |
| 10030 | /* |
| 10031 | * Check node->doc sanity. |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 10032 | */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10033 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
| 10034 | (node->doc != sourceDoc)) { |
| 10035 | /* |
| 10036 | * Might be an XIncluded node. |
| 10037 | */ |
| 10038 | return (-1); |
| 10039 | } |
| 10040 | if (sourceDoc == NULL) |
| 10041 | sourceDoc = node->doc; |
| 10042 | if (sourceDoc == destDoc) |
| 10043 | return (-1); |
| 10044 | switch (node->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 10045 | case XML_ELEMENT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10046 | case XML_ATTRIBUTE_NODE: |
| 10047 | case XML_TEXT_NODE: |
| 10048 | case XML_CDATA_SECTION_NODE: |
| 10049 | case XML_ENTITY_REF_NODE: |
| 10050 | case XML_PI_NODE: |
| 10051 | case XML_COMMENT_NODE: |
| 10052 | break; |
| 10053 | case XML_DOCUMENT_FRAG_NODE: |
Kasimier T. Buchcik | cab801b | 2006-02-03 16:35:27 +0000 | [diff] [blame] | 10054 | /* TODO: Support document-fragment-nodes. */ |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10055 | return (2); |
| 10056 | default: |
| 10057 | return (1); |
| 10058 | } |
| 10059 | /* |
| 10060 | * Unlink only if @node was not already added to @destParent. |
| 10061 | */ |
| 10062 | if ((node->parent != NULL) && (destParent != node->parent)) |
| 10063 | xmlUnlinkNode(node); |
| 10064 | |
| 10065 | if (node->type == XML_ELEMENT_NODE) { |
| 10066 | return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node, |
| 10067 | destDoc, destParent, options)); |
| 10068 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
| 10069 | return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc, |
| 10070 | (xmlAttrPtr) node, destDoc, destParent, options)); |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 10071 | } else { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10072 | xmlNodePtr cur = node; |
| 10073 | int adoptStr = 1; |
| 10074 | |
| 10075 | cur->doc = destDoc; |
| 10076 | /* |
| 10077 | * Optimize string adoption. |
| 10078 | */ |
| 10079 | if ((sourceDoc != NULL) && |
| 10080 | (sourceDoc->dict == destDoc->dict)) |
| 10081 | adoptStr = 0; |
| 10082 | switch (node->type) { |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 10083 | case XML_TEXT_NODE: |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10084 | case XML_CDATA_SECTION_NODE: |
| 10085 | XML_TREE_ADOPT_STR_2(node->content) |
| 10086 | break; |
| 10087 | case XML_ENTITY_REF_NODE: |
| 10088 | /* |
| 10089 | * Remove reference to the entitity-node. |
| 10090 | */ |
| 10091 | node->content = NULL; |
| 10092 | node->children = NULL; |
| 10093 | node->last = NULL; |
| 10094 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
| 10095 | xmlEntityPtr ent; |
| 10096 | /* |
| 10097 | * Assign new entity-node if available. |
| 10098 | */ |
| 10099 | ent = xmlGetDocEntity(destDoc, node->name); |
| 10100 | if (ent != NULL) { |
| 10101 | node->content = ent->content; |
| 10102 | node->children = (xmlNodePtr) ent; |
| 10103 | node->last = (xmlNodePtr) ent; |
| 10104 | } |
| 10105 | } |
| 10106 | XML_TREE_ADOPT_STR(node->name) |
| 10107 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 10108 | case XML_PI_NODE: { |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10109 | XML_TREE_ADOPT_STR(node->name) |
| 10110 | XML_TREE_ADOPT_STR_2(node->content) |
| 10111 | break; |
Daniel Veillard | 7e21fd1 | 2005-07-03 21:44:07 +0000 | [diff] [blame] | 10112 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10113 | default: |
| 10114 | break; |
| 10115 | } |
Daniel Veillard | aa6de47 | 2008-08-25 14:53:31 +0000 | [diff] [blame] | 10116 | } |
Kasimier T. Buchcik | bc0e3c6 | 2005-06-27 10:28:23 +0000 | [diff] [blame] | 10117 | return (0); |
| 10118 | } |
| 10119 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 10120 | #define bottom_tree |
| 10121 | #include "elfgcchack.h" |