blob: 6a158cec335bc8436b5393291e3eb6227633c002 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
Daniel Veillardd1640922001-12-17 15:30:10 +00002 * tree.c : implementation of access function for an XML tree.
Owen Taylor3473f882001-02-23 17:55:21 +00003 *
Daniel Veillardd5c2f922002-11-21 14:10:52 +00004 * References:
5 * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
6 *
Owen Taylor3473f882001-02-23 17:55:21 +00007 * See Copyright for the status of this software.
8 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00009 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000010 *
Owen Taylor3473f882001-02-23 17:55:21 +000011 */
12
Daniel Veillard34ce8be2002-03-18 19:37:11 +000013#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000014#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000015
Owen Taylor3473f882001-02-23 17:55:21 +000016#include <string.h> /* for memset() only ! */
Daniel Veillard1dc9feb2008-11-17 15:59:21 +000017#include <limits.h>
Owen Taylor3473f882001-02-23 17:55:21 +000018#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 Veillardb8c9be92001-07-09 16:01:19 +000031#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000032#include <libxml/entities.h>
33#include <libxml/valid.h>
34#include <libxml/xmlerror.h>
Daniel Veillardbdb9ba72001-04-11 11:28:06 +000035#include <libxml/parserInternals.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000036#include <libxml/globals.h>
Daniel Veillardd5c2f922002-11-21 14:10:52 +000037#ifdef LIBXML_HTML_ENABLED
38#include <libxml/HTMLtree.h>
39#endif
William M. Brack1d8c9b22004-12-25 10:14:57 +000040#ifdef LIBXML_DEBUG_ENABLED
41#include <libxml/debugXML.h>
42#endif
Owen Taylor3473f882001-02-23 17:55:21 +000043
Daniel Veillarddddeede2012-07-16 14:44:26 +080044#include "buf.h"
Daniel Veillard7d4c5292012-09-05 11:45:32 +080045#include "save.h"
Daniel Veillarddddeede2012-07-16 14:44:26 +080046
Daniel Veillarda880b122003-04-21 21:36:41 +000047int __xmlRegisterCallbacks = 0;
48
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000049/************************************************************************
50 * *
Daniel Veillardaa6de472008-08-25 14:53:31 +000051 * Forward declarations *
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000052 * *
53 ************************************************************************/
54
Daniel Veillard8ed10722009-08-20 19:17:36 +020055static xmlNsPtr
56xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
Daniel Veillard56a4cb82001-03-24 17:00:36 +000057
Kurt Roeckx95ebe532014-10-13 16:06:21 +080058static xmlChar* xmlGetPropNodeValueInternal(const xmlAttr *prop);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000059
Daniel Veillard56a4cb82001-03-24 17:00:36 +000060/************************************************************************
61 * *
Daniel Veillardaa6de472008-08-25 14:53:31 +000062 * Tree memory error handler *
Daniel Veillard18ec16e2003-10-07 23:16:40 +000063 * *
64 ************************************************************************/
65/**
66 * xmlTreeErrMemory:
67 * @extra: extra informations
68 *
69 * Handle an out of memory condition
70 */
71static void
72xmlTreeErrMemory(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 */
84static void
85xmlTreeErr(int code, xmlNodePtr node, const char *extra)
86{
87 const char *msg = NULL;
88
89 switch(code) {
90 case XML_TREE_INVALID_HEX:
Daniel Veillardac996a12004-07-30 12:02:58 +000091 msg = "invalid hexadecimal character value\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000092 break;
93 case XML_TREE_INVALID_DEC:
Daniel Veillardac996a12004-07-30 12:02:58 +000094 msg = "invalid decimal character value\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000095 break;
96 case XML_TREE_UNTERMINATED_ENTITY:
Daniel Veillardac996a12004-07-30 12:02:58 +000097 msg = "unterminated entity reference %15s\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000098 break;
Daniel Veillard6f8611f2008-02-15 08:33:21 +000099 case XML_TREE_NOT_UTF8:
100 msg = "string is not in UTF-8\n";
101 break;
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000102 default:
Daniel Veillardac996a12004-07-30 12:02:58 +0000103 msg = "unexpected error number\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000104 }
105 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
106}
107
108/************************************************************************
109 * *
Daniel Veillardaa6de472008-08-25 14:53:31 +0000110 * A few static variables and macros *
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000111 * *
112 ************************************************************************/
Daniel Veillardd0463562001-10-13 09:15:48 +0000113/* #undef xmlStringText */
Daniel Veillard22090732001-07-16 00:06:07 +0000114const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000115/* #undef xmlStringTextNoenc */
Daniel Veillard22090732001-07-16 00:06:07 +0000116const xmlChar xmlStringTextNoenc[] =
Owen Taylor3473f882001-02-23 17:55:21 +0000117 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000118/* #undef xmlStringComment */
Daniel Veillard22090732001-07-16 00:06:07 +0000119const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
120
Owen Taylor3473f882001-02-23 17:55:21 +0000121static int xmlCompressMode = 0;
122static int xmlCheckDTD = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000123
Owen Taylor3473f882001-02-23 17:55:21 +0000124#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 Veillardaa6de472008-08-25 14:53:31 +0000130 ulccur->parent = (n); \
Owen Taylor3473f882001-02-23 17:55:21 +0000131 ulccur = ulccur->next; \
132 } \
133 ulccur->parent = (n); \
134 (n)->last = ulccur; \
135}}
136
Kasimier T. Buchcik44353412006-03-06 13:26:16 +0000137#define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
138 (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
139
Owen Taylor3473f882001-02-23 17:55:21 +0000140/* #define DEBUG_BUFFER */
141/* #define DEBUG_TREE */
142
143/************************************************************************
144 * *
Daniel Veillardaa6de472008-08-25 14:53:31 +0000145 * Functions to move to entities.c once the *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000146 * API freeze is smoothen and they can be made public. *
147 * *
148 ************************************************************************/
149#include <libxml/hash.h>
Daniel Veillardaa6de472008-08-25 14:53:31 +0000150
Daniel Veillard652327a2003-09-29 18:02:38 +0000151#ifdef LIBXML_TREE_ENABLED
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000152/**
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 Veillardaa6de472008-08-25 14:53:31 +0000159 *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000160 * Returns A pointer to the entity structure or NULL if not found.
161 */
162static xmlEntityPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +0800163xmlGetEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000164 xmlEntitiesTablePtr table;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000165
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000166 if((dtd != NULL) && (dtd->entities != NULL)) {
167 table = (xmlEntitiesTablePtr) dtd->entities;
168 return((xmlEntityPtr) xmlHashLookup(table, name));
Daniel Veillardaa6de472008-08-25 14:53:31 +0000169 /* return(xmlGetEntityFromTable(table, name)); */
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000170 }
171 return(NULL);
172}
173/**
174 * xmlGetParameterEntityFromDtd:
175 * @dtd: A pointer to the DTD to search
176 * @name: The entity name
Daniel Veillardaa6de472008-08-25 14:53:31 +0000177 *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000178 * 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 */
183static xmlEntityPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +0800184xmlGetParameterEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000185 xmlEntitiesTablePtr table;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000186
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000187 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 Veillard652327a2003-09-29 18:02:38 +0000194#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000195
196/************************************************************************
197 * *
Daniel Veillardc00cda82003-04-07 10:22:39 +0000198 * 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 */
216xmlChar *
217xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
218 xmlChar *memory, int len) {
219 int lenn, lenp;
220 xmlChar *ret;
221
Daniel Veillard3b7840c2003-09-11 23:42:01 +0000222 if (ncname == NULL) return(NULL);
223 if (prefix == NULL) return((xmlChar *) ncname);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000224
225 lenn = strlen((char *) ncname);
226 lenp = strlen((char *) prefix);
227
228 if ((memory == NULL) || (len < lenn + lenp + 2)) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000229 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000230 if (ret == NULL) {
231 xmlTreeErrMemory("building QName");
232 return(NULL);
233 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000234 } 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 Veillardaa6de472008-08-25 14:53:31 +0000247 * @prefix: a xmlChar **
Daniel Veillardc00cda82003-04-07 10:22:39 +0000248 *
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
261xmlChar *
262xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
263 int len = 0;
264 xmlChar *ret = NULL;
265
Daniel Veillardd5cc0f72004-11-06 19:24:28 +0000266 if (prefix == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000267 *prefix = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000268 if (name == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000269
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 Veillardaa6de472008-08-25 14:53:31 +0000285 while ((name[len] != 0) && (name[len] != ':'))
Daniel Veillardc00cda82003-04-07 10:22:39 +0000286 len++;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000287
Daniel Veillardc00cda82003-04-07 10:22:39 +0000288 if (name[len] == 0)
289 return(NULL);
290
291 *prefix = xmlStrndup(name, len);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000292 if (*prefix == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000293 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000294 return(NULL);
295 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000296 ret = xmlStrdup(&name[len + 1]);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000297 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000298 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000299 if (*prefix != NULL) {
300 xmlFree(*prefix);
301 *prefix = NULL;
302 }
303 return(NULL);
304 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000305
306 return(ret);
307}
308
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000309/**
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 Woodfb27e2c2012-09-28 08:59:33 +0200317 * with the length in byte of the prefix and return a pointer
Daniel Veillard54f9a4f2005-09-03 13:28:24 +0000318 * to the start of the name without the prefix
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000319 */
320
321const xmlChar *
322xmlSplitQName3(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 Veillardaa6de472008-08-25 14:53:31 +0000336 while ((name[l] != 0) && (name[l] != ':'))
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000337 l++;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000338
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000339 if (name[l] == 0)
340 return(NULL);
341
342 *len = l;
343
344 return(&name[l+1]);
345}
346
Daniel Veillardc00cda82003-04-07 10:22:39 +0000347/************************************************************************
348 * *
Daniel Veillardd2298792003-02-14 16:54:11 +0000349 * Check Name, NCName and QName strings *
350 * *
351 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +0000352
Daniel Veillardd2298792003-02-14 16:54:11 +0000353#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
354
Nicolas Le Cam77b5b462014-02-10 10:32:45 +0800355#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 Veillardd2298792003-02-14 16:54:11 +0000356/**
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 */
366int
367xmlValidateNCName(const xmlChar *value, int space) {
368 const xmlChar *cur = value;
369 int c,l;
370
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000371 if (value == NULL)
372 return(-1);
373
Daniel Veillardd2298792003-02-14 16:54:11 +0000374 /*
375 * First quick algorithm for ASCII range
376 */
377 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000378 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000379 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. Brack76e95df2003-10-18 16:20:14 +0000390 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000391 if (*cur == 0)
392 return(0);
393
394try_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. Brack871611b2003-10-18 04:53:14 +0000406 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000407 return(1);
408 cur += l;
409 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000410 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
411 (c == '-') || (c == '_') || IS_COMBINING(c) ||
412 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000413 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 Veillard2156d432004-03-04 15:59:36 +0000427#endif
Daniel Veillardd2298792003-02-14 16:54:11 +0000428
Daniel Veillard2156d432004-03-04 15:59:36 +0000429#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Daniel Veillardd2298792003-02-14 16:54:11 +0000430/**
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 */
440int
441xmlValidateQName(const xmlChar *value, int space) {
442 const xmlChar *cur = value;
443 int c,l;
444
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000445 if (value == NULL)
446 return(-1);
Daniel Veillardd2298792003-02-14 16:54:11 +0000447 /*
448 * First quick algorithm for ASCII range
449 */
450 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000451 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000452 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. Brack76e95df2003-10-18 16:20:14 +0000477 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000478 if (*cur == 0)
479 return(0);
480
481try_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. Brack871611b2003-10-18 04:53:14 +0000493 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000494 return(1);
495 cur += l;
496 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000497 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
498 (c == '-') || (c == '_') || IS_COMBINING(c) ||
499 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000500 cur += l;
501 c = CUR_SCHAR(cur, l);
502 }
503 if (c == ':') {
504 cur += l;
505 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000506 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000507 return(1);
508 cur += l;
509 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000510 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
511 (c == '-') || (c == '_') || IS_COMBINING(c) ||
512 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000513 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 */
538int
539xmlValidateName(const xmlChar *value, int space) {
540 const xmlChar *cur = value;
541 int c,l;
542
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000543 if (value == NULL)
544 return(-1);
Daniel Veillardd2298792003-02-14 16:54:11 +0000545 /*
546 * First quick algorithm for ASCII range
547 */
548 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000549 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000550 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. Brack76e95df2003-10-18 16:20:14 +0000561 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000562 if (*cur == 0)
563 return(0);
564
565try_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. Brack871611b2003-10-18 04:53:14 +0000577 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000578 return(1);
579 cur += l;
580 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000581 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
582 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000583 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 Veillardd4310742003-02-18 21:12:46 +0000597/**
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 */
607int
608xmlValidateNMToken(const xmlChar *value, int space) {
609 const xmlChar *cur = value;
610 int c,l;
611
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000612 if (value == NULL)
613 return(-1);
Daniel Veillardd4310742003-02-18 21:12:46 +0000614 /*
615 * First quick algorithm for ASCII range
616 */
617 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000618 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000619 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. Brack76e95df2003-10-18 16:20:14 +0000632 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000633 if (*cur == 0)
634 return(0);
635
636try_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. Brack871611b2003-10-18 04:53:14 +0000648 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
649 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
Daniel Veillardd4310742003-02-18 21:12:46 +0000650 return(1);
651 cur += l;
652 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000653 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
654 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd4310742003-02-18 21:12:46 +0000655 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 Veillard652327a2003-09-29 18:02:38 +0000668#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardd4310742003-02-18 21:12:46 +0000669
Daniel Veillardd2298792003-02-14 16:54:11 +0000670/************************************************************************
671 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000672 * Allocation and deallocation of basic structures *
673 * *
674 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +0000675
Owen Taylor3473f882001-02-23 17:55:21 +0000676/**
677 * xmlSetBufferAllocationScheme:
678 * @scheme: allocation method to use
Daniel Veillardaa6de472008-08-25 14:53:31 +0000679 *
Owen Taylor3473f882001-02-23 17:55:21 +0000680 * Set the buffer allocation method. Types are
681 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
Daniel Veillardaa6de472008-08-25 14:53:31 +0000682 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
Owen Taylor3473f882001-02-23 17:55:21 +0000683 * improves performance
684 */
685void
686xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
Daniel Veillardda3fee42008-09-01 13:08:57 +0000687 if ((scheme == XML_BUFFER_ALLOC_EXACT) ||
Conrad Irwin7d0d2a52012-05-14 14:18:58 +0800688 (scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
689 (scheme == XML_BUFFER_ALLOC_HYBRID))
Daniel Veillardda3fee42008-09-01 13:08:57 +0000690 xmlBufferAllocScheme = scheme;
Owen Taylor3473f882001-02-23 17:55:21 +0000691}
692
693/**
694 * xmlGetBufferAllocationScheme:
695 *
696 * Types are
697 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
Daniel Veillardaa6de472008-08-25 14:53:31 +0000698 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
Owen Taylor3473f882001-02-23 17:55:21 +0000699 * improves performance
Conrad Irwin7d0d2a52012-05-14 14:18:58 +0800700 * 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 Veillardaa6de472008-08-25 14:53:31 +0000703 *
Owen Taylor3473f882001-02-23 17:55:21 +0000704 * Returns the current allocation scheme
705 */
706xmlBufferAllocationScheme
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000707xmlGetBufferAllocationScheme(void) {
Daniel Veillarde043ee12001-04-16 14:08:07 +0000708 return(xmlBufferAllocScheme);
Owen Taylor3473f882001-02-23 17:55:21 +0000709}
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 Veillard81b96172013-07-22 13:01:11 +0800720 * Note that for a default namespace, @prefix should be NULL.
721 *
Owen Taylor3473f882001-02-23 17:55:21 +0000722 * We use href==NULL in the case of an element creation where the namespace
723 * was not defined.
Daniel Veillard81b96172013-07-22 13:01:11 +0800724 *
Daniel Veillardd1640922001-12-17 15:30:10 +0000725 * Returns a new namespace pointer or NULL
Owen Taylor3473f882001-02-23 17:55:21 +0000726 */
727xmlNsPtr
728xmlNewNs(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 Veillardaa54d372010-09-09 18:17:47 +0200734 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 Veillard20ee8c02001-10-05 09:18:14 +0000747
Owen Taylor3473f882001-02-23 17:55:21 +0000748 /*
749 * Allocate a new Namespace and fill the fields.
750 */
751 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
752 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000753 xmlTreeErrMemory("building namespace");
Owen Taylor3473f882001-02-23 17:55:21 +0000754 return(NULL);
755 }
756 memset(cur, 0, sizeof(xmlNs));
757 cur->type = XML_LOCAL_NAMESPACE;
758
759 if (href != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000760 cur->href = xmlStrdup(href);
Owen Taylor3473f882001-02-23 17:55:21 +0000761 if (prefix != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000762 cur->prefix = xmlStrdup(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000763
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 Veillardaa6de472008-08-25 14:53:31 +0000778 }
Owen Taylor3473f882001-02-23 17:55:21 +0000779 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 Veillardaa6de472008-08-25 14:53:31 +0000785 }
Owen Taylor3473f882001-02-23 17:55:21 +0000786 }
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 */
800void
801xmlSetNs(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 Veillard81b96172013-07-22 13:01:11 +0800809 if ((node->type == XML_ELEMENT_NODE) ||
810 (node->type == XML_ATTRIBUTE_NODE))
811 node->ns = ns;
Owen Taylor3473f882001-02-23 17:55:21 +0000812}
813
814/**
815 * xmlFreeNs:
816 * @cur: the namespace pointer
817 *
818 * Free up the structures associated to a namespace
819 */
820void
821xmlFreeNs(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 Taylor3473f882001-02-23 17:55:21 +0000831 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 */
840void
841xmlFreeNsList(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 */
869xmlDtdPtr
870xmlNewDtd(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 Veillard18ec16e2003-10-07 23:16:40 +0000889 xmlTreeErrMemory("building DTD");
Owen Taylor3473f882001-02-23 17:55:21 +0000890 return(NULL);
891 }
892 memset(cur, 0 , sizeof(xmlDtd));
893 cur->type = XML_DTD_NODE;
894
895 if (name != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000896 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +0000897 if (ExternalID != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000898 cur->ExternalID = xmlStrdup(ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +0000899 if (SystemID != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000900 cur->SystemID = xmlStrdup(SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +0000901 if (doc != NULL)
902 doc->extSubset = cur;
903 cur->doc = doc;
904
Daniel Veillarda880b122003-04-21 21:36:41 +0000905 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +0000906 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000907 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
918xmlDtdPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +0800919xmlGetIntSubset(const xmlDoc *doc) {
Owen Taylor3473f882001-02-23 17:55:21 +0000920 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 Veillarde356c282001-03-10 12:32:04 +0000937 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000938 * @SystemID: the system ID
939 *
940 * Create the internal subset of a document
941 * Returns a pointer to the new DTD structure
942 */
943xmlDtdPtr
944xmlCreateIntSubset(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 Veillard18ec16e2003-10-07 23:16:40 +0000963 xmlTreeErrMemory("building internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000964 return(NULL);
965 }
966 memset(cur, 0, sizeof(xmlDtd));
967 cur->type = XML_DTD_NODE;
968
William M. Bracka3215c72004-07-31 16:24:01 +0000969 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 Veillardaa6de472008-08-25 14:53:31 +0000978 cur->ExternalID = xmlStrdup(ExternalID);
William M. Bracka3215c72004-07-31 16:24:01 +0000979 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 Veillardaa6de472008-08-25 14:53:31 +0000988 cur->SystemID = xmlStrdup(SystemID);
William M. Bracka3215c72004-07-31 16:24:01 +0000989 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 Taylor3473f882001-02-23 17:55:21 +0000999 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 Taylor3473f882001-02-23 17:55:21 +00001007 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +00001008 xmlNodePtr prev;
1009
Owen Taylor3473f882001-02-23 17:55:21 +00001010 prev = doc->children;
1011 prev->prev = (xmlNodePtr) cur;
1012 cur->next = prev;
1013 doc->children = (xmlNodePtr) cur;
1014 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +00001015 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 Taylor3473f882001-02-23 17:55:21 +00001034 }
1035 }
1036 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001037
Daniel Veillarda880b122003-04-21 21:36:41 +00001038 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001039 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001040 return(cur);
1041}
1042
1043/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001044 * DICT_FREE:
1045 * @str: a string
1046 *
1047 * Free a string if it is not owned by the "dict" dictionnary in the
1048 * current scope
1049 */
1050#define DICT_FREE(str) \
Daniel Veillardaa6de472008-08-25 14:53:31 +00001051 if ((str) && ((!dict) || \
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001052 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1053 xmlFree((char *)(str));
1054
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00001055
1056/**
1057 * DICT_COPY:
1058 * @str: a string
1059 *
1060 * Copy a string using a "dict" dictionnary in the current scope,
1061 * if availabe.
1062 */
1063#define DICT_COPY(str, cpy) \
1064 if (str) { \
1065 if (dict) { \
1066 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1067 cpy = (xmlChar *) (str); \
1068 else \
1069 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1070 } else \
1071 cpy = xmlStrdup((const xmlChar *)(str)); }
1072
1073/**
1074 * DICT_CONST_COPY:
1075 * @str: a string
1076 *
1077 * Copy a string using a "dict" dictionnary in the current scope,
1078 * if availabe.
1079 */
1080#define DICT_CONST_COPY(str, cpy) \
1081 if (str) { \
1082 if (dict) { \
1083 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1084 cpy = (const xmlChar *) (str); \
1085 else \
1086 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1087 } else \
1088 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1089
1090
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001091/**
Owen Taylor3473f882001-02-23 17:55:21 +00001092 * xmlFreeDtd:
1093 * @cur: the DTD structure to free up
1094 *
1095 * Free a DTD structure.
1096 */
1097void
1098xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001099 xmlDictPtr dict = NULL;
1100
Owen Taylor3473f882001-02-23 17:55:21 +00001101 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001102 return;
1103 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001104 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001105
Daniel Veillarda880b122003-04-21 21:36:41 +00001106 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001107 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1108
Owen Taylor3473f882001-02-23 17:55:21 +00001109 if (cur->children != NULL) {
1110 xmlNodePtr next, c = cur->children;
1111
1112 /*
William M. Brack18a04f22004-08-03 16:42:37 +00001113 * Cleanup all nodes which are not part of the specific lists
1114 * of notations, elements, attributes and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00001115 */
1116 while (c != NULL) {
1117 next = c->next;
William M. Brack18a04f22004-08-03 16:42:37 +00001118 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 Taylor3473f882001-02-23 17:55:21 +00001122 xmlUnlinkNode(c);
1123 xmlFreeNode(c);
1124 }
1125 c = next;
1126 }
1127 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001128 DICT_FREE(cur->name)
1129 DICT_FREE(cur->SystemID)
1130 DICT_FREE(cur->ExternalID)
Owen Taylor3473f882001-02-23 17:55:21 +00001131 /* TODO !!! */
1132 if (cur->notations != NULL)
1133 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
Daniel Veillardaa6de472008-08-25 14:53:31 +00001134
Owen Taylor3473f882001-02-23 17:55:21 +00001135 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 Taylor3473f882001-02-23 17:55:21 +00001144 xmlFree(cur);
1145}
1146
1147/**
1148 * xmlNewDoc:
1149 * @version: xmlChar string giving the version of XML "1.0"
1150 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001151 * Creates a new XML document
1152 *
Owen Taylor3473f882001-02-23 17:55:21 +00001153 * Returns a new document
1154 */
1155xmlDocPtr
1156xmlNewDoc(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 Veillard18ec16e2003-10-07 23:16:40 +00001167 xmlTreeErrMemory("building doc");
Owen Taylor3473f882001-02-23 17:55:21 +00001168 return(NULL);
1169 }
1170 memset(cur, 0, sizeof(xmlDoc));
1171 cur->type = XML_DOCUMENT_NODE;
1172
Daniel Veillardaa6de472008-08-25 14:53:31 +00001173 cur->version = xmlStrdup(version);
William M. Bracka3215c72004-07-31 16:24:01 +00001174 if (cur->version == NULL) {
1175 xmlTreeErrMemory("building doc");
1176 xmlFree(cur);
Daniel Veillardae0765b2008-07-31 19:54:59 +00001177 return(NULL);
William M. Bracka3215c72004-07-31 16:24:01 +00001178 }
Owen Taylor3473f882001-02-23 17:55:21 +00001179 cur->standalone = -1;
1180 cur->compression = -1; /* not initialized */
1181 cur->doc = cur;
Daniel Veillardae0765b2008-07-31 19:54:59 +00001182 cur->parseFlags = 0;
1183 cur->properties = XML_DOC_USERBUILT;
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001184 /*
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 Veillardd2f3ec72001-04-11 07:50:02 +00001189 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001190
Daniel Veillarda880b122003-04-21 21:36:41 +00001191 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001192 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001193 return(cur);
1194}
1195
1196/**
1197 * xmlFreeDoc:
1198 * @cur: pointer to the document
Owen Taylor3473f882001-02-23 17:55:21 +00001199 *
1200 * Free up all the structures used by a document, tree included.
1201 */
1202void
1203xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillarda9142e72001-06-19 11:07:54 +00001204 xmlDtdPtr extSubset, intSubset;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001205 xmlDictPtr dict = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001206
Owen Taylor3473f882001-02-23 17:55:21 +00001207 if (cur == NULL) {
1208#ifdef DEBUG_TREE
1209 xmlGenericError(xmlGenericErrorContext,
1210 "xmlFreeDoc : document == NULL\n");
1211#endif
1212 return;
1213 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001214#ifdef LIBXML_DEBUG_RUNTIME
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001215#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001216 xmlDebugCheckDocument(stderr, cur);
1217#endif
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001218#endif
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001219
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001220 if (cur != NULL) dict = cur->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001221
Daniel Veillarda880b122003-04-21 21:36:41 +00001222 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001223 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1224
Daniel Veillard76d66f42001-05-16 21:05:17 +00001225 /*
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 Veillarda9142e72001-06-19 11:07:54 +00001232 extSubset = cur->extSubset;
1233 intSubset = cur->intSubset;
Daniel Veillard5997aca2002-03-18 18:36:20 +00001234 if (intSubset == extSubset)
1235 extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001236 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001237 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001238 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001239 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001240 }
Daniel Veillarda9142e72001-06-19 11:07:54 +00001241 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001242 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001243 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001244 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001245 }
1246
1247 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001248 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001249
1250 DICT_FREE(cur->version)
1251 DICT_FREE(cur->name)
1252 DICT_FREE(cur->encoding)
1253 DICT_FREE(cur->URL)
Owen Taylor3473f882001-02-23 17:55:21 +00001254 xmlFree(cur);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001255 if (dict) xmlDictFree(dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001256}
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 */
1268xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001269xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) {
Owen Taylor3473f882001-02-23 17:55:21 +00001270 xmlNodePtr ret = NULL, last = NULL;
1271 xmlNodePtr node;
1272 xmlChar *val;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001273 const xmlChar *cur = value, *end = cur + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001274 const xmlChar *q;
1275 xmlEntityPtr ent;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001276 xmlBufPtr buf;
Owen Taylor3473f882001-02-23 17:55:21 +00001277
1278 if (value == NULL) return(NULL);
1279
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001280 buf = xmlBufCreateSize(0);
1281 if (buf == NULL) return(NULL);
1282 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001283
Owen Taylor3473f882001-02-23 17:55:21 +00001284 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001285 while ((cur < end) && (*cur != 0)) {
1286 if (cur[0] == '&') {
1287 int charval = 0;
1288 xmlChar tmp;
1289
Owen Taylor3473f882001-02-23 17:55:21 +00001290 /*
1291 * Save the current text.
1292 */
1293 if (cur != q) {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001294 if (xmlBufAdd(buf, q, cur - q))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001295 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00001296 }
Owen Taylor3473f882001-02-23 17:55:21 +00001297 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001298 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 Veillardaa6de472008-08-25 14:53:31 +00001305 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillard07cb8222003-09-10 10:51:05 +00001306 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 Taylor3473f882001-02-23 17:55:21 +00001311 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001312 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1313 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001314 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 Veillardaa6de472008-08-25 14:53:31 +00001333 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillard07cb8222003-09-10 10:51:05 +00001334 charval = charval * 10 + (tmp - '0');
1335 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001336 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1337 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001338 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 Veillard18ec16e2003-10-07 23:16:40 +00001358 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1359 (const char *) q);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001360 goto out;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001361 }
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 Irwin7d553f82012-05-10 20:17:25 -07001370
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001371 if (xmlBufCat(buf, ent->content))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001372 goto out;
Daniel Veillardaa6de472008-08-25 14:53:31 +00001373
Daniel Veillard07cb8222003-09-10 10:51:05 +00001374 } else {
1375 /*
Conrad Irwin7d553f82012-05-10 20:17:25 -07001376 * Flush buffer so far
1377 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001378 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001379 node = xmlNewDocText(doc, NULL);
1380 if (node == NULL) {
1381 if (val != NULL) xmlFree(val);
1382 goto out;
1383 }
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001384 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001385
1386 if (last == NULL) {
1387 last = ret = node;
1388 } else {
1389 last = xmlAddNextSibling(last, node);
1390 }
1391 }
1392
1393 /*
Daniel Veillard07cb8222003-09-10 10:51:05 +00001394 * Create a new REFERENCE_REF node
1395 */
1396 node = xmlNewReference(doc, val);
1397 if (node == NULL) {
1398 if (val != NULL) xmlFree(val);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001399 goto out;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001400 }
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 Veillard8de5c0b2004-10-07 13:14:19 +00001410 ent->last = temp;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001411 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 Veillardc15df7d2012-08-07 15:15:04 +08001426 xmlChar buffer[10];
Daniel Veillard07cb8222003-09-10 10:51:05 +00001427 int l;
1428
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001429 l = xmlCopyCharMultiByte(buffer, charval);
1430 buffer[l] = 0;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001431
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001432 if (xmlBufCat(buf, buffer))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001433 goto out;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001434 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001435 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001436 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001437 cur++;
1438 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001439
1440 if (cur != q) {
Owen Taylor3473f882001-02-23 17:55:21 +00001441 /*
1442 * Handle the last piece of text.
1443 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001444 if (xmlBufAdd(buf, q, cur - q))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001445 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00001446 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001447
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001448 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001449 node = xmlNewDocText(doc, NULL);
1450 if (node == NULL) goto out;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001451 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001452
1453 if (last == NULL) {
Philip Withnall57941042014-10-26 18:08:04 +00001454 ret = node;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001455 } else {
Philip Withnall57941042014-10-26 18:08:04 +00001456 xmlAddNextSibling(last, node);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001457 }
1458 } else if (ret == NULL) {
1459 ret = xmlNewDocText(doc, BAD_CAST "");
1460 }
1461
1462out:
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001463 xmlBufFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001464 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 */
1476xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001477xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001478 xmlNodePtr ret = NULL, last = NULL;
1479 xmlNodePtr node;
1480 xmlChar *val;
1481 const xmlChar *cur = value;
1482 const xmlChar *q;
1483 xmlEntityPtr ent;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001484 xmlBufPtr buf;
Owen Taylor3473f882001-02-23 17:55:21 +00001485
1486 if (value == NULL) return(NULL);
1487
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001488 buf = xmlBufCreateSize(0);
1489 if (buf == NULL) return(NULL);
1490 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001491
Owen Taylor3473f882001-02-23 17:55:21 +00001492 q = cur;
1493 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001494 if (cur[0] == '&') {
1495 int charval = 0;
1496 xmlChar tmp;
1497
Owen Taylor3473f882001-02-23 17:55:21 +00001498 /*
1499 * Save the current text.
1500 */
1501 if (cur != q) {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001502 if (xmlBufAdd(buf, q, cur - q))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001503 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00001504 }
Owen Taylor3473f882001-02-23 17:55:21 +00001505 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001506 if ((cur[1] == '#') && (cur[2] == 'x')) {
1507 cur += 3;
1508 tmp = *cur;
1509 while (tmp != ';') { /* Non input consuming loop */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001510 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001511 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 Taylor3473f882001-02-23 17:55:21 +00001516 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001517 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1518 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001519 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 Veillardaa6de472008-08-25 14:53:31 +00001532 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001533 charval = charval * 10 + (tmp - '0');
1534 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001535 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1536 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001537 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 Veillard18ec16e2003-10-07 23:16:40 +00001554 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1555 (xmlNodePtr) doc, (const char *) q);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001556 goto out;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001557 }
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 Irwin7d553f82012-05-10 20:17:25 -07001566
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001567 if (xmlBufCat(buf, ent->content))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001568 goto out;
Daniel Veillardaa6de472008-08-25 14:53:31 +00001569
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001570 } else {
1571 /*
Conrad Irwin7d553f82012-05-10 20:17:25 -07001572 * Flush buffer so far
1573 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001574 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001575 node = xmlNewDocText(doc, NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001576 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001577
1578 if (last == NULL) {
1579 last = ret = node;
1580 } else {
1581 last = xmlAddNextSibling(last, node);
1582 }
1583 }
1584
1585 /*
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001586 * Create a new REFERENCE_REF node
1587 */
1588 node = xmlNewReference(doc, val);
1589 if (node == NULL) {
1590 if (val != NULL) xmlFree(val);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001591 goto out;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001592 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001593 else if ((ent != NULL) && (ent->children == NULL)) {
1594 xmlNodePtr temp;
1595
1596 ent->children = xmlStringGetNodeList(doc,
1597 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001598 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001599 temp = ent->children;
1600 while (temp) {
1601 temp->parent = (xmlNodePtr)ent;
1602 temp = temp->next;
1603 }
1604 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001605 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 Veillardc15df7d2012-08-07 15:15:04 +08001617 xmlChar buffer[10];
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001618 int len;
1619
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001620 len = xmlCopyCharMultiByte(buffer, charval);
1621 buffer[len] = 0;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001622
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001623 if (xmlBufCat(buf, buffer))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001624 goto out;
1625 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001626 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001627 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001628 cur++;
1629 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001630 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001631 /*
1632 * Handle the last piece of text.
1633 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001634 xmlBufAdd(buf, q, cur - q);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001635 }
1636
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001637 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001638 node = xmlNewDocText(doc, NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001639 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001640
1641 if (last == NULL) {
Philip Withnall57941042014-10-26 18:08:04 +00001642 ret = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001643 } else {
Philip Withnall57941042014-10-26 18:08:04 +00001644 xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001645 }
1646 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001647
1648out:
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001649 xmlBufFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001650 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 Veillarda9b66d02002-12-11 14:23:49 +00001659 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001660 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001661 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001662 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001663 */
1664xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001665xmlNodeListGetString(xmlDocPtr doc, const xmlNode *list, int inLine)
Daniel Veillard7646b182002-04-20 06:41:40 +00001666{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001667 const xmlNode *node = list;
Owen Taylor3473f882001-02-23 17:55:21 +00001668 xmlChar *ret = NULL;
1669 xmlEntityPtr ent;
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001670 int attr;
Owen Taylor3473f882001-02-23 17:55:21 +00001671
Daniel Veillard7646b182002-04-20 06:41:40 +00001672 if (list == NULL)
1673 return (NULL);
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001674 if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE))
1675 attr = 1;
1676 else
1677 attr = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001678
1679 while (node != NULL) {
1680 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001681 (node->type == XML_CDATA_SECTION_NODE)) {
1682 if (inLine) {
1683 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001684 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001685 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001686
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001687 if (attr)
1688 buffer = xmlEncodeAttributeEntities(doc, node->content);
1689 else
1690 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
Daniel Veillard7646b182002-04-20 06:41:40 +00001691 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 Veillard652327a2003-09-29 18:02:38 +00001740
1741#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001742/**
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 Veillarda9b66d02002-12-11 14:23:49 +00001748 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001749 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1750 * this function doesn't do any character encoding handling.
1751 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001752 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001753 */
1754xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001755xmlNodeListGetRawString(const xmlDoc *doc, const xmlNode *list, int inLine)
Daniel Veillard7646b182002-04-20 06:41:40 +00001756{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001757 const xmlNode *node = list;
Owen Taylor3473f882001-02-23 17:55:21 +00001758 xmlChar *ret = NULL;
1759 xmlEntityPtr ent;
1760
Daniel Veillard7646b182002-04-20 06:41:40 +00001761 if (list == NULL)
1762 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001763
1764 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001765 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001766 (node->type == XML_CDATA_SECTION_NODE)) {
1767 if (inLine) {
1768 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001769 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001770 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 Taylor3473f882001-02-23 17:55:21 +00001812#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001813 else {
1814 xmlGenericError(xmlGenericErrorContext,
1815 "xmlGetNodeListString : invalid node type %d\n",
1816 node->type);
1817 }
Owen Taylor3473f882001-02-23 17:55:21 +00001818#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001819 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001820 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001821 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001822}
Daniel Veillard652327a2003-09-29 18:02:38 +00001823#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001824
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001825static xmlAttrPtr
1826xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1827 const xmlChar * name, const xmlChar * value,
1828 int eatname)
1829{
Owen Taylor3473f882001-02-23 17:55:21 +00001830 xmlAttrPtr cur;
1831 xmlDocPtr doc = NULL;
1832
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001833 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001834 if ((eatname == 1) &&
1835 ((node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001836 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001837 xmlFree((xmlChar *) name);
1838 return (NULL);
1839 }
Owen Taylor3473f882001-02-23 17:55:21 +00001840
1841 /*
1842 * Allocate a new property and fill the fields.
1843 */
1844 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1845 if (cur == NULL) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001846 if ((eatname == 1) &&
Daniel Veillard76d36452009-09-07 11:19:33 +02001847 ((node == NULL) || (node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001848 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001849 xmlFree((xmlChar *) name);
1850 xmlTreeErrMemory("building attribute");
1851 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001852 }
1853 memset(cur, 0, sizeof(xmlAttr));
1854 cur->type = XML_ATTRIBUTE_NODE;
1855
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001856 cur->parent = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001857 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001858 doc = node->doc;
1859 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001860 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001861 cur->ns = ns;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001862
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001863 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 Veillard5cd3e8c2005-03-27 11:25:28 +00001870
Owen Taylor3473f882001-02-23 17:55:21 +00001871 if (value != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001872 xmlNodePtr tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001873
Daniel Veillard6f8611f2008-02-15 08:33:21 +00001874 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 Veillard54f9a4f2005-09-03 13:28:24 +00001881 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 Veillard54f9a4f2005-09-03 13:28:24 +00001889 }
Owen Taylor3473f882001-02-23 17:55:21 +00001890
1891 /*
1892 * Add it at the end to preserve parsing order ...
1893 */
1894 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001895 if (node->properties == NULL) {
1896 node->properties = cur;
1897 } else {
1898 xmlAttrPtr prev = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00001899
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001900 while (prev->next != NULL)
1901 prev = prev->next;
1902 prev->next = cur;
1903 cur->prev = prev;
1904 }
Owen Taylor3473f882001-02-23 17:55:21 +00001905 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001906
Daniel Veillard76d36452009-09-07 11:19:33 +02001907 if ((value != NULL) && (node != NULL) &&
1908 (xmlIsID(node->doc, node, cur) == 1))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001909 xmlAddID(NULL, node->doc, value, cur);
1910
Daniel Veillarda880b122003-04-21 21:36:41 +00001911 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001912 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1913 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001914}
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001915
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 */
1927xmlAttrPtr
1928xmlNewProp(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 Veillard652327a2003-09-29 18:02:38 +00001940#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001941
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 */
1952xmlAttrPtr
1953xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1954 const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001955
1956 if (name == NULL) {
1957#ifdef DEBUG_TREE
1958 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001959 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001960#endif
1961 return(NULL);
1962 }
1963
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001964 return xmlNewPropInternal(node, ns, name, value, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001965}
1966
1967/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001968 * 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 */
1977xmlAttrPtr
1978xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1979 const xmlChar *value) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00001980
1981 if (name == NULL) {
1982#ifdef DEBUG_TREE
1983 xmlGenericError(xmlGenericErrorContext,
1984 "xmlNewNsPropEatName : name == NULL\n");
1985#endif
1986 return(NULL);
1987 }
1988
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001989 return xmlNewPropInternal(node, ns, name, value, 1);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001990}
1991
1992/**
Owen Taylor3473f882001-02-23 17:55:21 +00001993 * 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 */
2001xmlAttrPtr
2002xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
2003 xmlAttrPtr cur;
2004
2005 if (name == NULL) {
2006#ifdef DEBUG_TREE
2007 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00002008 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002009#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 Veillard18ec16e2003-10-07 23:16:40 +00002018 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00002019 return(NULL);
2020 }
2021 memset(cur, 0, sizeof(xmlAttr));
2022 cur->type = XML_ATTRIBUTE_NODE;
2023
Daniel Veillard03a53c32004-10-26 16:06:51 +00002024 if ((doc != NULL) && (doc->dict != NULL))
2025 cur->name = xmlDictLookup(doc->dict, name, -1);
2026 else
2027 cur->name = xmlStrdup(name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00002028 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002029 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 Veillard5335dc52003-01-01 20:59:38 +00002043
Daniel Veillarda880b122003-04-21 21:36:41 +00002044 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002045 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002046 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 */
2055void
2056xmlFreePropList(xmlAttrPtr cur) {
2057 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002058 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002059 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 */
2072void
2073xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002074 xmlDictPtr dict = NULL;
2075 if (cur == NULL) return;
2076
2077 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002078
Daniel Veillarda880b122003-04-21 21:36:41 +00002079 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002080 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2081
Owen Taylor3473f882001-02-23 17:55:21 +00002082 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillardda6f4af2005-06-20 17:17:54 +00002083 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2084 xmlRemoveID(cur->doc, cur);
Daniel Veillard76d66f42001-05-16 21:05:17 +00002085 }
Owen Taylor3473f882001-02-23 17:55:21 +00002086 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002087 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002088 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 */
2100int
2101xmlRemoveProp(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 Richards19dc9612005-10-28 16:15:16 +00002120 if (cur->next != NULL)
2121 cur->next->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002122 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 Veillard03a53c32004-10-26 16:06:51 +00002143 * xmlNewDocPI:
2144 * @doc: the target document
Owen Taylor3473f882001-02-23 17:55:21 +00002145 * @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 */
2151xmlNodePtr
Daniel Veillard03a53c32004-10-26 16:06:51 +00002152xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002153 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 Veillard18ec16e2003-10-07 23:16:40 +00002168 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002169 return(NULL);
2170 }
2171 memset(cur, 0, sizeof(xmlNode));
2172 cur->type = XML_PI_NODE;
2173
Daniel Veillard03a53c32004-10-26 16:06:51 +00002174 if ((doc != NULL) && (doc->dict != NULL))
2175 cur->name = xmlDictLookup(doc->dict, name, -1);
2176 else
2177 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00002178 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002179 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002180 }
Daniel Veillarda03e3652004-11-02 18:45:30 +00002181 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002182
Daniel Veillarda880b122003-04-21 21:36:41 +00002183 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002184 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002185 return(cur);
2186}
2187
2188/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002189 * 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 */
2198xmlNodePtr
2199xmlNewPI(const xmlChar *name, const xmlChar *content) {
2200 return(xmlNewDocPI(NULL, name, content));
2201}
2202
2203/**
Owen Taylor3473f882001-02-23 17:55:21 +00002204 * xmlNewNode:
2205 * @ns: namespace if any
2206 * @name: the node name
2207 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002208 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002209 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002210 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2211 * copy of @name.
Owen Taylor3473f882001-02-23 17:55:21 +00002212 */
2213xmlNodePtr
2214xmlNewNode(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 Veillard18ec16e2003-10-07 23:16:40 +00002230 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002231 return(NULL);
2232 }
2233 memset(cur, 0, sizeof(xmlNode));
2234 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002235
Owen Taylor3473f882001-02-23 17:55:21 +00002236 cur->name = xmlStrdup(name);
2237 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002238
Daniel Veillarda880b122003-04-21 21:36:41 +00002239 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002240 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002241 return(cur);
2242}
2243
2244/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002245 * 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. Brackd7cf7f82003-11-14 07:13:16 +00002251 * 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 Veillard46de64e2002-05-29 08:21:33 +00002254 */
2255xmlNodePtr
2256xmlNewNodeEatName(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 Veillard18ec16e2003-10-07 23:16:40 +00002272 xmlTreeErrMemory("building node");
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00002273 /* we can't check here that name comes from the doc dictionnary */
Daniel Veillard46de64e2002-05-29 08:21:33 +00002274 return(NULL);
2275 }
2276 memset(cur, 0, sizeof(xmlNode));
2277 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002278
Daniel Veillard46de64e2002-05-29 08:21:33 +00002279 cur->name = name;
2280 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002281
Daniel Veillarda880b122003-04-21 21:36:41 +00002282 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002283 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002284 return(cur);
2285}
2286
2287/**
Owen Taylor3473f882001-02-23 17:55:21 +00002288 * 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 Veillardd1640922001-12-17 15:30:10 +00002295 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002296 * 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 */
2303xmlNodePtr
2304xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2305 const xmlChar *name, const xmlChar *content) {
2306 xmlNodePtr cur;
2307
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002308 if ((doc != NULL) && (doc->dict != NULL))
Daniel Veillard03a53c32004-10-26 16:06:51 +00002309 cur = xmlNewNodeEatName(ns, (xmlChar *)
2310 xmlDictLookup(doc->dict, name, -1));
2311 else
2312 cur = xmlNewNode(ns, name);
Owen Taylor3473f882001-02-23 17:55:21 +00002313 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 Veillard5335dc52003-01-01 20:59:38 +00002320
Owen Taylor3473f882001-02-23 17:55:21 +00002321 return(cur);
2322}
2323
Daniel Veillard46de64e2002-05-29 08:21:33 +00002324/**
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 */
2340xmlNodePtr
2341xmlNewDocNodeEatName(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 Veillard8f6c2b12008-04-03 11:17:21 +00002352 } else {
2353 /* if name don't come from the doc dictionnary free it here */
2354 if ((name != NULL) && (doc != NULL) &&
2355 (!(xmlDictOwns(doc->dict, name))))
2356 xmlFree(name);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002357 }
2358 return(cur);
2359}
2360
Daniel Veillard652327a2003-09-29 18:02:38 +00002361#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002362/**
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 Veillardd1640922001-12-17 15:30:10 +00002370 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002371 *
2372 * Returns a pointer to the new node object.
2373 */
2374xmlNodePtr
2375xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2376 const xmlChar *name, const xmlChar *content) {
2377 xmlNodePtr cur;
2378
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002379 cur = xmlNewDocNode(doc, ns, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002380 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 */
2397xmlNodePtr
2398xmlNewDocFragment(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 Veillard18ec16e2003-10-07 23:16:40 +00002406 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002407 return(NULL);
2408 }
2409 memset(cur, 0, sizeof(xmlNode));
2410 cur->type = XML_DOCUMENT_FRAG_NODE;
2411
2412 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002413
Daniel Veillarda880b122003-04-21 21:36:41 +00002414 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002415 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002416 return(cur);
2417}
Daniel Veillard652327a2003-09-29 18:02:38 +00002418#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002419
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 */
2427xmlNodePtr
2428xmlNewText(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 Veillard18ec16e2003-10-07 23:16:40 +00002436 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002437 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 Taylor3473f882001-02-23 17:55:21 +00002444 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002445 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002446
Daniel Veillarda880b122003-04-21 21:36:41 +00002447 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002448 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002449 return(cur);
2450}
2451
Daniel Veillard652327a2003-09-29 18:02:38 +00002452#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002453/**
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 Flecke1f70492003-12-20 23:43:28 +00002461 * @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. Brackd7cf7f82003-11-14 07:13:16 +00002463 * a child TEXT node will be created containing the string @content.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002464 * 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 Veillardaa6de472008-08-25 14:53:31 +00002466 * 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 Taylor3473f882001-02-23 17:55:21 +00002469 *
2470 * Returns a pointer to the new node object.
2471 */
2472xmlNodePtr
2473xmlNewTextChild(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 Veillard254b1262003-11-01 17:04:58 +00002496 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 Taylor3473f882001-02-23 17:55:21 +00002512 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 Veillard652327a2003-09-29 18:02:38 +00002532#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002533
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 */
2542xmlNodePtr
2543xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2544 xmlNodePtr cur;
2545
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002546 if (name == NULL)
2547 return(NULL);
2548
Owen Taylor3473f882001-02-23 17:55:21 +00002549 /*
2550 * Allocate a new node and fill the fields.
2551 */
2552 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2553 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002554 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002555 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 Veillard5335dc52003-01-01 20:59:38 +00002571
Daniel Veillarda880b122003-04-21 21:36:41 +00002572 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002573 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002574 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 */
2585xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002586xmlNewReference(const xmlDoc *doc, const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00002587 xmlNodePtr cur;
2588 xmlEntityPtr ent;
2589
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002590 if (name == NULL)
2591 return(NULL);
2592
Owen Taylor3473f882001-02-23 17:55:21 +00002593 /*
2594 * Allocate a new node and fill the fields.
2595 */
2596 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2597 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002598 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002599 return(NULL);
2600 }
2601 memset(cur, 0, sizeof(xmlNode));
2602 cur->type = XML_ENTITY_REF_NODE;
2603
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002604 cur->doc = (xmlDoc *)doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002605 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 Taylor3473f882001-02-23 17:55:21 +00002618 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002619 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002620 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002621 * updated. Not sure if this is 100% correct.
2622 * -George
2623 */
2624 cur->children = (xmlNodePtr) ent;
2625 cur->last = (xmlNodePtr) ent;
2626 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002627
Daniel Veillarda880b122003-04-21 21:36:41 +00002628 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002629 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002630 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 */
2641xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002642xmlNewDocText(const xmlDoc *doc, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002643 xmlNodePtr cur;
2644
2645 cur = xmlNewText(content);
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002646 if (cur != NULL) cur->doc = (xmlDoc *)doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002647 return(cur);
2648}
2649
2650/**
2651 * xmlNewTextLen:
2652 * @content: the text content
2653 * @len: the text len.
2654 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002655 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002656 * Returns a pointer to the new node object.
2657 */
2658xmlNodePtr
2659xmlNewTextLen(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 Veillard18ec16e2003-10-07 23:16:40 +00002667 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002668 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 Taylor3473f882001-02-23 17:55:21 +00002675 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002676 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002677
Daniel Veillarda880b122003-04-21 21:36:41 +00002678 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002679 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002680 return(cur);
2681}
2682
2683/**
2684 * xmlNewDocTextLen:
2685 * @doc: the document
2686 * @content: the text content
2687 * @len: the text len.
2688 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002689 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002690 * text node pertain to a given document.
2691 * Returns a pointer to the new node object.
2692 */
2693xmlNodePtr
2694xmlNewDocTextLen(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 */
2709xmlNodePtr
2710xmlNewComment(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 Veillard18ec16e2003-10-07 23:16:40 +00002718 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002719 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 Taylor3473f882001-02-23 17:55:21 +00002726 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002727 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002728
Daniel Veillarda880b122003-04-21 21:36:41 +00002729 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002730 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002731 return(cur);
2732}
2733
2734/**
2735 * xmlNewCDataBlock:
2736 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002737 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002738 * @len: the length of the block
2739 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002740 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002741 * Returns a pointer to the new node object.
2742 */
2743xmlNodePtr
2744xmlNewCDataBlock(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 Veillard18ec16e2003-10-07 23:16:40 +00002752 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002753 return(NULL);
2754 }
2755 memset(cur, 0, sizeof(xmlNode));
2756 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002757 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002758
2759 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002760 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002761 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002762
Daniel Veillarda880b122003-04-21 21:36:41 +00002763 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002764 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002765 return(cur);
2766}
2767
2768/**
2769 * xmlNewDocComment:
2770 * @doc: the document
2771 * @content: the comment content
2772 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002773 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002774 * Returns a pointer to the new node object.
2775 */
2776xmlNodePtr
2777xmlNewDocComment(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 */
2792void
2793xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002794 xmlAttrPtr prop;
2795
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002796 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002797 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002798 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002799 if(tree->type == XML_ELEMENT_NODE) {
2800 prop = tree->properties;
2801 while (prop != NULL) {
Nick Wellnhoferf54d6a92014-12-19 00:08:35 +01002802 if (prop->atype == XML_ATTRIBUTE_ID) {
2803 xmlRemoveID(tree->doc, prop);
2804 }
2805
Daniel Veillard36065812002-01-24 15:02:46 +00002806 prop->doc = doc;
2807 xmlSetListDoc(prop->children, doc);
Nick Wellnhoferf54d6a92014-12-19 00:08:35 +01002808
Nick Wellnhofer220a7ba2014-12-23 21:28:37 +01002809 /*
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 Wellnhoferf54d6a92014-12-19 00:08:35 +01002816 if (xmlIsID(doc, tree, prop)) {
2817 xmlChar *idVal = xmlNodeListGetString(doc, prop->children,
2818 1);
2819 xmlAddID(NULL, doc, idVal, prop);
2820 }
Nick Wellnhofer220a7ba2014-12-23 21:28:37 +01002821#endif
Nick Wellnhoferf54d6a92014-12-19 00:08:35 +01002822
Daniel Veillard36065812002-01-24 15:02:46 +00002823 prop = prop->next;
2824 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002825 }
Owen Taylor3473f882001-02-23 17:55:21 +00002826 if (tree->children != NULL)
2827 xmlSetListDoc(tree->children, doc);
2828 tree->doc = doc;
2829 }
2830}
2831
2832/**
2833 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002834 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002835 * @doc: the document
2836 *
2837 * update all nodes in the list to point to the right document
2838 */
2839void
2840xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2841 xmlNodePtr cur;
2842
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002843 if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002844 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 Veillard2156d432004-03-04 15:59:36 +00002853#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002854/**
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 Flecke1f70492003-12-20 23:43:28 +00002862 * @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 Taylor3473f882001-02-23 17:55:21 +00002864 * a child list containing the TEXTs and ENTITY_REFs node will be created.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002865 * 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 Taylor3473f882001-02-23 17:55:21 +00002868 *
2869 * Returns a pointer to the new node object.
2870 */
2871xmlNodePtr
2872xmlNewChild(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 Veillard36eea2d2002-02-04 00:17:01 +00002895 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 Veillard7e3f1402002-10-28 18:52:57 +00002906 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2907 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002908 } else {
2909 return(NULL);
2910 }
Owen Taylor3473f882001-02-23 17:55:21 +00002911 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 Veillard652327a2003-09-29 18:02:38 +00002931#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002932
2933/**
Rob Richards65815122006-02-25 17:13:33 +00002934 * xmlAddPropSibling:
Daniel Veillardaa6de472008-08-25 14:53:31 +00002935 * @prev: the attribute to which @prop is added after
Rob Richards65815122006-02-25 17:13:33 +00002936 * @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 Veillardaa6de472008-08-25 14:53:31 +00002942 * If an existing attribute is found it is detroyed prior to adding @prop.
Rob Richards65815122006-02-25 17:13:33 +00002943 *
2944 * Returns the attribute being inserted or NULL in case of error.
2945 */
2946static xmlNodePtr
2947xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2948 xmlAttrPtr attr;
2949
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002950 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 Richards65815122006-02-25 17:13:33 +00002953 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 Taylor3473f882001-02-23 17:55:21 +00002985 * xmlAddNextSibling:
2986 * @cur: the child node
2987 * @elem: the new node
2988 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002989 * 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 Taylor3473f882001-02-23 17:55:21 +00002991 * first unlinked from its existing context.
2992 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002993 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00002994 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002995 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002996 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002997 */
2998xmlNodePtr
2999xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003000 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003001#ifdef DEBUG_TREE
3002 xmlGenericError(xmlGenericErrorContext,
3003 "xmlAddNextSibling : cur == NULL\n");
3004#endif
3005 return(NULL);
3006 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003007 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003008#ifdef DEBUG_TREE
3009 xmlGenericError(xmlGenericErrorContext,
3010 "xmlAddNextSibling : elem == NULL\n");
3011#endif
3012 return(NULL);
3013 }
3014
Rob Richards19dc9612005-10-28 16:15:16 +00003015 if (cur == elem) {
3016#ifdef DEBUG_TREE
3017 xmlGenericError(xmlGenericErrorContext,
3018 "xmlAddNextSibling : cur == elem\n");
3019#endif
3020 return(NULL);
3021 }
3022
Owen Taylor3473f882001-02-23 17:55:21 +00003023 xmlUnlinkNode(elem);
3024
3025 if (elem->type == XML_TEXT_NODE) {
3026 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003027 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003028 xmlFreeNode(elem);
3029 return(cur);
3030 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003031 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
3032 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003033 xmlChar *tmp;
3034
3035 tmp = xmlStrdup(elem->content);
3036 tmp = xmlStrcat(tmp, cur->next->content);
3037 xmlNodeSetContent(cur->next, tmp);
3038 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003039 xmlFreeNode(elem);
3040 return(cur->next);
3041 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003042 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003043 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003044 }
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 Richards65815122006-02-25 17:13:33 +00003055 if ((elem->parent != NULL) && (elem->parent->last == cur))
Owen Taylor3473f882001-02-23 17:55:21 +00003056 elem->parent->last = elem;
3057 return(elem);
3058}
3059
William M. Brack21e4ef22005-01-02 09:53:13 +00003060#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
Nicolas Le Cam41586ca2013-06-17 13:01:33 +02003061 defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003062/**
3063 * xmlAddPrevSibling:
3064 * @cur: the child node
3065 * @elem: the new node
3066 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003067 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00003068 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003069 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003070 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003071 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003072 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00003073 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003074 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003075 */
3076xmlNodePtr
3077xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003078 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003079#ifdef DEBUG_TREE
3080 xmlGenericError(xmlGenericErrorContext,
3081 "xmlAddPrevSibling : cur == NULL\n");
3082#endif
3083 return(NULL);
3084 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003085 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003086#ifdef DEBUG_TREE
3087 xmlGenericError(xmlGenericErrorContext,
3088 "xmlAddPrevSibling : elem == NULL\n");
3089#endif
3090 return(NULL);
3091 }
3092
Rob Richards19dc9612005-10-28 16:15:16 +00003093 if (cur == elem) {
3094#ifdef DEBUG_TREE
3095 xmlGenericError(xmlGenericErrorContext,
3096 "xmlAddPrevSibling : cur == elem\n");
3097#endif
3098 return(NULL);
3099 }
3100
Owen Taylor3473f882001-02-23 17:55:21 +00003101 xmlUnlinkNode(elem);
3102
3103 if (elem->type == XML_TEXT_NODE) {
3104 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003105 xmlChar *tmp;
3106
3107 tmp = xmlStrdup(elem->content);
3108 tmp = xmlStrcat(tmp, cur->content);
3109 xmlNodeSetContent(cur, tmp);
3110 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003111 xmlFreeNode(elem);
3112 return(cur);
3113 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003114 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3115 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003116 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003117 xmlFreeNode(elem);
3118 return(cur->prev);
3119 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003120 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003121 return xmlAddPropSibling(cur->prev, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003122 }
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 Richards65815122006-02-25 17:13:33 +00003133 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003134 elem->parent->children = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003135 }
Owen Taylor3473f882001-02-23 17:55:21 +00003136 return(elem);
3137}
Daniel Veillard652327a2003-09-29 18:02:38 +00003138#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003139
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 */
3152xmlNodePtr
3153xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3154 xmlNodePtr parent;
3155
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003156 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003157#ifdef DEBUG_TREE
3158 xmlGenericError(xmlGenericErrorContext,
3159 "xmlAddSibling : cur == NULL\n");
3160#endif
3161 return(NULL);
3162 }
3163
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003164 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003165#ifdef DEBUG_TREE
3166 xmlGenericError(xmlGenericErrorContext,
3167 "xmlAddSibling : elem == NULL\n");
3168#endif
3169 return(NULL);
3170 }
3171
Daniel Veillard43bc89c2009-03-23 19:32:04 +00003172 if (cur == elem) {
3173#ifdef DEBUG_TREE
3174 xmlGenericError(xmlGenericErrorContext,
3175 "xmlAddSibling : cur == elem\n");
3176#endif
3177 return(NULL);
3178 }
3179
Owen Taylor3473f882001-02-23 17:55:21 +00003180 /*
3181 * Constant time is we can rely on the ->parent->last to find
3182 * the last sibling.
3183 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00003184 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003185 (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 Veillarde22dd5c2003-10-29 12:53:27 +00003195 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3196 (cur->name == elem->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003197 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003198 xmlFreeNode(elem);
3199 return(cur);
Rob Richards65815122006-02-25 17:13:33 +00003200 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3201 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003202 }
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 */
3228xmlNodePtr
3229xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3230 xmlNodePtr prev;
3231
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003232 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003233#ifdef DEBUG_TREE
3234 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003235 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003236#endif
3237 return(NULL);
3238 }
3239
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003240 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003241#ifdef DEBUG_TREE
3242 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003243 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003244#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 Veillard5335dc52003-01-01 20:59:38 +00003259
Owen Taylor3473f882001-02-23 17:55:21 +00003260 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 Veillardaa6de472008-08-25 14:53:31 +00003266 if ((cur->type == XML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003267 (parent->last->type == XML_TEXT_NODE) &&
3268 (cur->name == parent->last->name)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00003269 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003270 /*
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 Richards810a78b2008-12-31 22:13:57 +00003293 /* the parent may not be linked to a doc ! */
3294 if (cur->doc != parent->doc) {
3295 xmlSetTreeDoc(cur, parent->doc);
3296 }
Owen Taylor3473f882001-02-23 17:55:21 +00003297 parent->last = cur;
3298
3299 return(cur);
3300}
3301
3302/**
3303 * xmlAddChild:
3304 * @parent: the parent node
3305 * @cur: the child node
3306 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003307 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003308 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003309 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003310 * If there is an attribute with equal name, it is first destroyed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003311 *
Owen Taylor3473f882001-02-23 17:55:21 +00003312 * Returns the child or NULL in case of error.
3313 */
3314xmlNodePtr
3315xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3316 xmlNodePtr prev;
3317
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003318 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003319#ifdef DEBUG_TREE
3320 xmlGenericError(xmlGenericErrorContext,
3321 "xmlAddChild : parent == NULL\n");
3322#endif
3323 return(NULL);
3324 }
3325
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003326 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003327#ifdef DEBUG_TREE
3328 xmlGenericError(xmlGenericErrorContext,
3329 "xmlAddChild : child == NULL\n");
3330#endif
3331 return(NULL);
3332 }
3333
Rob Richards19dc9612005-10-28 16:15:16 +00003334 if (parent == cur) {
3335#ifdef DEBUG_TREE
3336 xmlGenericError(xmlGenericErrorContext,
3337 "xmlAddChild : parent == cur\n");
3338#endif
3339 return(NULL);
3340 }
Owen Taylor3473f882001-02-23 17:55:21 +00003341 /*
3342 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003343 * cur is then freed.
3344 */
3345 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003346 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003347 (parent->content != NULL) &&
Rob Richards19dc9612005-10-28 16:15:16 +00003348 (parent->name == cur->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003349 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003350 xmlFreeNode(cur);
3351 return(parent);
3352 }
3353 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003354 (parent->last->name == cur->name) &&
3355 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003356 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003357 xmlFreeNode(cur);
3358 return(parent->last);
3359 }
3360 }
3361
3362 /*
3363 * add the new element at the end of the children list.
3364 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003365 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003366 cur->parent = parent;
3367 if (cur->doc != parent->doc) {
3368 xmlSetTreeDoc(cur, parent->doc);
3369 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003370 /* 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 Taylor3473f882001-02-23 17:55:21 +00003375
3376 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003377 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003378 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003379 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003380 (parent->content != NULL) &&
3381 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003382 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003383 xmlFreeNode(cur);
3384 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003385 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003386 if (cur->type == XML_ATTRIBUTE_NODE) {
Rob Richards19dc9612005-10-28 16:15:16 +00003387 if (parent->type != XML_ELEMENT_NODE)
3388 return(NULL);
Rob Richards810a78b2008-12-31 22:13:57 +00003389 if (parent->properties != NULL) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003390 /* check if an attribute with the same name exists */
3391 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003392
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003393 if (cur->ns == NULL)
Rob Richardsc342ec62005-10-25 00:10:12 +00003394 lastattr = xmlHasNsProp(parent, cur->name, NULL);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003395 else
3396 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
Rob Richardsc342ec62005-10-25 00:10:12 +00003397 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003398 /* different instance, destroy it (attributes must be unique) */
Rob Richards19dc9612005-10-28 16:15:16 +00003399 xmlUnlinkNode((xmlNodePtr) lastattr);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003400 xmlFreeProp(lastattr);
3401 }
Rob Richards19dc9612005-10-28 16:15:16 +00003402 if (lastattr == (xmlAttrPtr) cur)
3403 return(cur);
Rob Richards810a78b2008-12-31 22:13:57 +00003404
3405 }
3406 if (parent->properties == NULL) {
3407 parent->properties = (xmlAttrPtr) cur;
3408 } else {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003409 /* find the end */
Rob Richards810a78b2008-12-31 22:13:57 +00003410 xmlAttrPtr lastattr = parent->properties;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003411 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 Taylor3473f882001-02-23 17:55:21 +00003428 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 */
3438xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08003439xmlGetLastChild(const xmlNode *parent) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003440 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003441#ifdef DEBUG_TREE
3442 xmlGenericError(xmlGenericErrorContext,
3443 "xmlGetLastChild : parent == NULL\n");
3444#endif
3445 return(NULL);
3446 }
3447 return(parent->last);
3448}
3449
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003450#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 */
3467unsigned long
3468xmlChildElementCount(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 VanderBeek1db99692014-07-29 00:32:15 -07003478 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003479 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 */
3504xmlNodePtr
3505xmlFirstElementChild(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 VanderBeek1db99692014-07-29 00:32:15 -07003514 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003515 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 */
3540xmlNodePtr
3541xmlLastElementChild(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 VanderBeek1db99692014-07-29 00:32:15 -07003550 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003551 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 */
3577xmlNodePtr
3578xmlPreviousElementSibling(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 Delyon2f700902010-02-03 17:32:37 +01003599 node = node->prev;
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003600 }
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 */
3616xmlNodePtr
3617xmlNextElementSibling(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 Taylor3473f882001-02-23 17:55:21 +00003646/**
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 */
3653void
3654xmlFreeNodeList(xmlNodePtr cur) {
3655 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003656 xmlDictPtr dict = NULL;
3657
3658 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003659 if (cur->type == XML_NAMESPACE_DECL) {
3660 xmlFreeNsList((xmlNsPtr) cur);
3661 return;
3662 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003663 if ((cur->type == XML_DOCUMENT_NODE) ||
3664#ifdef LIBXML_DOCB_ENABLED
3665 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003666#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003667 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003668 xmlFreeDoc((xmlDocPtr) cur);
3669 return;
3670 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003671 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003672 while (cur != NULL) {
3673 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003674 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003675
Daniel Veillarda880b122003-04-21 21:36:41 +00003676 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003677 xmlDeregisterNodeDefaultValue(cur);
3678
Daniel Veillard02141ea2001-04-30 11:46:40 +00003679 if ((cur->children != NULL) &&
3680 (cur->type != XML_ENTITY_REF_NODE))
3681 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003682 if (((cur->type == XML_ELEMENT_NODE) ||
3683 (cur->type == XML_XINCLUDE_START) ||
3684 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003685 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003686 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003687 if ((cur->type != XML_ELEMENT_NODE) &&
3688 (cur->type != XML_XINCLUDE_START) &&
3689 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003690 (cur->type != XML_ENTITY_REF_NODE) &&
3691 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003692 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003693 }
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 Veillard9cc6dc62001-06-11 08:09:20 +00003700 /*
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 Veillarde96a2a42003-09-24 21:23:56 +00003703 * Otherwise the node name might come from the document's
3704 * dictionnary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003705 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003706 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003707 (cur->type != XML_TEXT_NODE) &&
3708 (cur->type != XML_COMMENT_NODE))
3709 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003710 xmlFree(cur);
3711 }
Owen Taylor3473f882001-02-23 17:55:21 +00003712 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 */
3723void
3724xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003725 xmlDictPtr dict = NULL;
3726
3727 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003728
Daniel Veillard02141ea2001-04-30 11:46:40 +00003729 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003730 if (cur->type == XML_DTD_NODE) {
3731 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003732 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003733 }
3734 if (cur->type == XML_NAMESPACE_DECL) {
3735 xmlFreeNs((xmlNsPtr) cur);
3736 return;
3737 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003738 if (cur->type == XML_ATTRIBUTE_NODE) {
3739 xmlFreeProp((xmlAttrPtr) cur);
3740 return;
3741 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003742
Daniel Veillarda880b122003-04-21 21:36:41 +00003743 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003744 xmlDeregisterNodeDefaultValue(cur);
3745
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003746 if (cur->doc != NULL) dict = cur->doc->dict;
3747
Daniel Veillard2cba4152008-08-27 11:45:41 +00003748 if (cur->type == XML_ENTITY_DECL) {
3749 xmlEntityPtr ent = (xmlEntityPtr) cur;
3750 DICT_FREE(ent->SystemID);
3751 DICT_FREE(ent->ExternalID);
3752 }
Owen Taylor3473f882001-02-23 17:55:21 +00003753 if ((cur->children != NULL) &&
3754 (cur->type != XML_ENTITY_REF_NODE))
3755 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003756 if (((cur->type == XML_ELEMENT_NODE) ||
3757 (cur->type == XML_XINCLUDE_START) ||
3758 (cur->type == XML_XINCLUDE_END)) &&
3759 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003760 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003761 if ((cur->type != XML_ELEMENT_NODE) &&
3762 (cur->content != NULL) &&
3763 (cur->type != XML_ENTITY_REF_NODE) &&
3764 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003765 (cur->type != XML_XINCLUDE_START) &&
3766 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003767 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003768 }
3769
Daniel Veillardacd370f2001-06-09 17:17:51 +00003770 /*
3771 * When a node is a text node or a comment, it uses a global static
3772 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003773 * Otherwise the node name might come from the document's dictionnary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003774 */
Owen Taylor3473f882001-02-23 17:55:21 +00003775 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003776 (cur->type != XML_TEXT_NODE) &&
3777 (cur->type != XML_COMMENT_NODE))
3778 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003779
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003780 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 Taylor3473f882001-02-23 17:55:21 +00003785 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 Veillard39d027c2012-05-11 12:38:23 +08003793 * If one need to free the node, use xmlFreeNode() routine after the
3794 * unlink to discard it.
Daniel Veillard6ca24a32012-08-08 15:31:55 +08003795 * Note that namespace nodes can't be unlinked as they do not have
3796 * pointer to their parent.
Owen Taylor3473f882001-02-23 17:55:21 +00003797 */
3798void
3799xmlUnlinkNode(xmlNodePtr cur) {
3800 if (cur == NULL) {
3801#ifdef DEBUG_TREE
3802 xmlGenericError(xmlGenericErrorContext,
3803 "xmlUnlinkNode : node == NULL\n");
3804#endif
3805 return;
3806 }
Daniel Veillard6ca24a32012-08-08 15:31:55 +08003807 if (cur->type == XML_NAMESPACE_DECL)
3808 return;
Daniel Veillard29e43992001-12-13 22:21:58 +00003809 if (cur->type == XML_DTD_NODE) {
3810 xmlDocPtr doc;
3811 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003812 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 Veillard29e43992001-12-13 22:21:58 +00003818 }
Daniel Veillard2cba4152008-08-27 11:45:41 +00003819 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 Veillardc169f8b2002-01-22 21:40:13 +00003841 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 Taylor3473f882001-02-23 17:55:21 +00003855 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 Taylor3473f882001-02-23 17:55:21 +00003860}
3861
Daniel Veillard2156d432004-03-04 15:59:36 +00003862#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003863/**
3864 * xmlReplaceNode:
3865 * @old: the old node
3866 * @cur: the node
3867 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00003868 * Unlink the old node from its current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003869 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003870 * first unlinked from its existing context.
3871 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003872 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003873 */
3874xmlNodePtr
3875xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003876 if (old == cur) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003877 if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
3878 (old->parent == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003879#ifdef DEBUG_TREE
3880 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda03e3652004-11-02 18:45:30 +00003881 "xmlReplaceNode : old == NULL or without parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003882#endif
3883 return(NULL);
3884 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003885 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003886 xmlUnlinkNode(old);
3887 return(old);
3888 }
3889 if (cur == old) {
3890 return(old);
3891 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003892 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 Taylor3473f882001-02-23 17:55:21 +00003906 xmlUnlinkNode(cur);
Daniel Veillard64d7d122005-05-11 18:03:42 +00003907 xmlSetTreeDoc(cur, old->doc);
Owen Taylor3473f882001-02-23 17:55:21 +00003908 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 Veillardc169f8b2002-01-22 21:40:13 +00003916 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 Taylor3473f882001-02-23 17:55:21 +00003925 }
3926 old->next = old->prev = NULL;
3927 old->parent = NULL;
3928 return(old);
3929}
Daniel Veillard652327a2003-09-29 18:02:38 +00003930#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003931
3932/************************************************************************
3933 * *
3934 * Copy operations *
3935 * *
3936 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00003937
Owen Taylor3473f882001-02-23 17:55:21 +00003938/**
3939 * xmlCopyNamespace:
3940 * @cur: the namespace
3941 *
3942 * Do a copy of the namespace.
3943 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003944 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003945 */
3946xmlNsPtr
3947xmlCopyNamespace(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 Veillardd1640922001-12-17 15:30:10 +00003971 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003972 */
3973xmlNsPtr
3974xmlCopyNamespaceList(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
3991static xmlNodePtr
3992xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
Rob Richards19dc9612005-10-28 16:15:16 +00003993
3994static xmlAttrPtr
3995xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003996 xmlAttrPtr ret;
3997
3998 if (cur == NULL) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003999 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4000 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004001 if (target != NULL)
4002 ret = xmlNewDocProp(target->doc, cur->name, NULL);
Rob Richards19dc9612005-10-28 16:15:16 +00004003 else if (doc != NULL)
4004 ret = xmlNewDocProp(doc, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004005 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 Veillardd4f41aa2002-03-03 14:13:46 +00004013
Owen Taylor3473f882001-02-23 17:55:21 +00004014 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00004015 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00004016
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00004017 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 Veillard044fc6b2002-03-04 17:09:44 +00004045 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00004046 /* 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 Veillardaa6de472008-08-25 14:53:31 +00004056
Owen Taylor3473f882001-02-23 17:55:21 +00004057 } 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 Veillardc5f05ad2002-02-10 11:57:22 +00004073 /*
4074 * Try to handle IDs
4075 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00004076 if ((target!= NULL) && (cur!= NULL) &&
4077 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00004078 (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 Taylor3473f882001-02-23 17:55:21 +00004089 return(ret);
4090}
4091
4092/**
Rob Richards19dc9612005-10-28 16:15:16 +00004093 * 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 */
4101xmlAttrPtr
4102xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4103 return xmlCopyPropInternal(NULL, target, cur);
4104}
4105
4106/**
Owen Taylor3473f882001-02-23 17:55:21 +00004107 * 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 Veillardd1640922001-12-17 15:30:10 +00004113 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004114 */
4115xmlAttrPtr
4116xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4117 xmlAttrPtr ret = NULL;
4118 xmlAttrPtr p = NULL,q;
4119
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004120 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4121 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004122 while (cur != NULL) {
4123 q = xmlCopyProp(target, cur);
William M. Brack13dfa872004-09-18 04:52:08 +00004124 if (q == NULL)
4125 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004126 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 Veillardd1640922001-12-17 15:30:10 +00004139 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00004140 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004141 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00004142 * 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. Brack57e9e912004-03-09 16:19:02 +00004151 * 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 Taylor3473f882001-02-23 17:55:21 +00004155 */
4156
4157static xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004158xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
William M. Brack57e9e912004-03-09 16:19:02 +00004159 int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004160 xmlNodePtr ret;
4161
4162 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00004163 switch (node->type) {
4164 case XML_TEXT_NODE:
4165 case XML_CDATA_SECTION_NODE:
4166 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00004167 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004168 case XML_ENTITY_REF_NODE:
4169 case XML_ENTITY_NODE:
4170 case XML_PI_NODE:
4171 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004172 case XML_XINCLUDE_START:
4173 case XML_XINCLUDE_END:
4174 break;
4175 case XML_ATTRIBUTE_NODE:
Rob Richards19dc9612005-10-28 16:15:16 +00004176 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004177 case XML_NAMESPACE_DECL:
4178 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
Daniel Veillardaa6de472008-08-25 14:53:31 +00004179
Daniel Veillard39196eb2001-06-19 18:09:42 +00004180 case XML_DOCUMENT_NODE:
4181 case XML_HTML_DOCUMENT_NODE:
4182#ifdef LIBXML_DOCB_ENABLED
4183 case XML_DOCB_DOCUMENT_NODE:
4184#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00004185#ifdef LIBXML_TREE_ENABLED
William M. Brack57e9e912004-03-09 16:19:02 +00004186 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
Daniel Veillard652327a2003-09-29 18:02:38 +00004187#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00004188 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004189 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 Veillardb33c2012001-04-25 12:59:04 +00004196
Owen Taylor3473f882001-02-23 17:55:21 +00004197 /*
4198 * Allocate a new node and fill the fields.
4199 */
4200 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4201 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004202 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00004203 return(NULL);
4204 }
4205 memset(ret, 0, sizeof(xmlNode));
4206 ret->type = node->type;
4207
4208 ret->doc = doc;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004209 ret->parent = parent;
Owen Taylor3473f882001-02-23 17:55:21 +00004210 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 Veillard03a53c32004-10-26 16:06:51 +00004216 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 Veillard7db37732001-07-12 01:20:08 +00004222 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 Taylor3473f882001-02-23 17:55:21 +00004227 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00004228 }else{
4229 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004230 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00004231 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004232 if (parent != NULL) {
4233 xmlNodePtr tmp;
4234
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004235 /*
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 Veillarda880b122003-04-21 21:36:41 +00004240 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004241 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4242
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004243 tmp = xmlAddChild(parent, ret);
4244 /* node could have coalesced */
4245 if (tmp != ret)
4246 return(tmp);
4247 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004248
William M. Brack57e9e912004-03-09 16:19:02 +00004249 if (!extended)
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004250 goto out;
Petr Pajas2afca4a2009-07-30 17:47:32 +02004251 if (((node->type == XML_ELEMENT_NODE) ||
4252 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004253 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 Veillarde82a9922001-04-22 12:12:58 +00004270 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Rob Richardsddb01cb2010-01-29 13:32:12 -05004271 } else {
4272 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
Owen Taylor3473f882001-02-23 17:55:21 +00004273 }
4274 } else {
4275 /*
4276 * reference the existing namespace definition in our own tree.
4277 */
4278 ret->ns = ns;
4279 }
4280 }
Petr Pajas2afca4a2009-07-30 17:47:32 +02004281 if (((node->type == XML_ELEMENT_NODE) ||
4282 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004283 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004284 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 Veillardd1640922001-12-17 15:30:10 +00004288 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00004289 * 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 Veillard0ec98632001-11-14 15:04:32 +00004296 ret->last = ret->children;
William M. Brack57e9e912004-03-09 16:19:02 +00004297 } else if ((node->children != NULL) && (extended != 2)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004298 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00004299 UPDATE_LAST_CHILD_AND_PARENT(ret)
4300 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004301
4302out:
4303 /* if parent != NULL we already registered the node above */
Daniel Veillardac996a12004-07-30 12:02:58 +00004304 if ((parent == NULL) &&
4305 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004306 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004307 return(ret);
4308}
4309
4310static xmlNodePtr
4311xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4312 xmlNodePtr ret = NULL;
4313 xmlNodePtr p = NULL,q;
4314
4315 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00004316#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004317 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00004318 if (doc == NULL) {
4319 node = node->next;
4320 continue;
4321 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00004322 if (doc->intSubset == NULL) {
4323 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
Gaurav98a4e712013-11-29 23:28:21 +08004324 if (q == NULL) return(NULL);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004325 q->doc = doc;
4326 q->parent = parent;
4327 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004328 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004329 } else {
4330 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004331 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004332 }
4333 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00004334#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00004335 q = xmlStaticCopyNode(node, doc, parent, 1);
Gaurav98a4e712013-11-29 23:28:21 +08004336 if (q == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004337 if (ret == NULL) {
4338 q->prev = NULL;
4339 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004340 } else if (p != q) {
4341 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00004342 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. Brack57e9e912004-03-09 16:19:02 +00004354 * @extended: if 1 do a recursive copy (properties, namespaces and children
4355 * when applicable)
4356 * if 2 copy properties and namespaces (when applicable)
Owen Taylor3473f882001-02-23 17:55:21 +00004357 *
4358 * Do a copy of the node.
4359 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004360 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004361 */
4362xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004363xmlCopyNode(xmlNodePtr node, int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004364 xmlNodePtr ret;
4365
William M. Brack57e9e912004-03-09 16:19:02 +00004366 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
Owen Taylor3473f882001-02-23 17:55:21 +00004367 return(ret);
4368}
4369
4370/**
Daniel Veillard82daa812001-04-12 08:55:36 +00004371 * xmlDocCopyNode:
4372 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00004373 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004374 * @extended: if 1 do a recursive copy (properties, namespaces and children
4375 * when applicable)
4376 * if 2 copy properties and namespaces (when applicable)
Daniel Veillard82daa812001-04-12 08:55:36 +00004377 *
4378 * Do a copy of the node to a given document.
4379 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004380 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00004381 */
4382xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004383xmlDocCopyNode(xmlNodePtr node, xmlDocPtr doc, int extended) {
Daniel Veillard82daa812001-04-12 08:55:36 +00004384 xmlNodePtr ret;
4385
William M. Brack57e9e912004-03-09 16:19:02 +00004386 ret = xmlStaticCopyNode(node, doc, NULL, extended);
Daniel Veillard82daa812001-04-12 08:55:36 +00004387 return(ret);
4388}
4389
4390/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00004391 * 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 Roeckx95ebe532014-10-13 16:06:21 +08004399xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, xmlNodePtr node) {
Daniel Veillard03a53c32004-10-26 16:06:51 +00004400 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4401 return(ret);
4402}
4403
4404/**
Owen Taylor3473f882001-02-23 17:55:21 +00004405 * xmlCopyNodeList:
4406 * @node: the first node in the list.
4407 *
4408 * Do a recursive copy of the node list.
Daniel Veillard03a53c32004-10-26 16:06:51 +00004409 * Use xmlDocCopyNodeList() if possible to ensure string interning.
Owen Taylor3473f882001-02-23 17:55:21 +00004410 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004411 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004412 */
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004413xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00004414 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4415 return(ret);
4416}
4417
Daniel Veillard2156d432004-03-04 15:59:36 +00004418#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004419/**
Owen Taylor3473f882001-02-23 17:55:21 +00004420 * xmlCopyDtd:
4421 * @dtd: the dtd
4422 *
4423 * Do a copy of the dtd.
4424 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004425 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004426 */
4427xmlDtdPtr
4428xmlCopyDtd(xmlDtdPtr dtd) {
4429 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004430 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00004431
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 Veillard8ee9c8f2002-01-26 21:42:58 +00004447 if (dtd->pentities != NULL)
4448 ret->pentities = (void *) xmlCopyEntitiesTable(
4449 (xmlEntitiesTablePtr) dtd->pentities);
Daniel Veillardaa6de472008-08-25 14:53:31 +00004450
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004451 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 Veillardaa6de472008-08-25 14:53:31 +00004465 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004466 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 Veillardaa6de472008-08-25 14:53:31 +00004477 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004478 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4479 } else if (cur->type == XML_COMMENT_NODE) {
4480 q = xmlCopyNode(cur, 0);
4481 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004482
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004483 if (q == NULL) {
4484 cur = cur->next;
4485 continue;
4486 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004487
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004488 if (p == NULL)
4489 ret->children = q;
4490 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00004491 p->next = q;
4492
4493 q->prev = p;
4494 q->parent = (xmlNodePtr) ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004495 q->next = NULL;
4496 ret->last = q;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004497 p = q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004498 cur = cur->next;
4499 }
4500
Owen Taylor3473f882001-02-23 17:55:21 +00004501 return(ret);
4502}
Daniel Veillard2156d432004-03-04 15:59:36 +00004503#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004504
Daniel Veillard2156d432004-03-04 15:59:36 +00004505#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004506/**
4507 * xmlCopyDoc:
4508 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004509 * @recursive: if not zero do a recursive copy.
Owen Taylor3473f882001-02-23 17:55:21 +00004510 *
4511 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004512 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004513 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004514 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004515 */
4516xmlDocPtr
4517xmlCopyDoc(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 Veillardf59507d2005-01-27 17:26:49 +00004527 if (doc->URL != NULL)
4528 ret->URL = xmlStrdup(doc->URL);
Owen Taylor3473f882001-02-23 17:55:21 +00004529 ret->charset = doc->charset;
4530 ret->compression = doc->compression;
4531 ret->standalone = doc->standalone;
4532 if (!recursive) return(ret);
4533
Daniel Veillardb33c2012001-04-25 12:59:04 +00004534 ret->last = NULL;
4535 ret->children = NULL;
Daniel Veillard2156d432004-03-04 15:59:36 +00004536#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb33c2012001-04-25 12:59:04 +00004537 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004538 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard42870f42014-07-26 21:04:54 +08004539 if (ret->intSubset == NULL) {
4540 xmlFreeDoc(ret);
4541 return(NULL);
4542 }
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004543 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004544 ret->intSubset->parent = ret;
4545 }
Daniel Veillard2156d432004-03-04 15:59:36 +00004546#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004547 if (doc->oldNs != NULL)
4548 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4549 if (doc->children != NULL) {
4550 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004551
Daniel Veillardb33c2012001-04-25 12:59:04 +00004552 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4553 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004554 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 Veillard652327a2003-09-29 18:02:38 +00004564#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004565
4566/************************************************************************
4567 * *
4568 * Content access functions *
4569 * *
4570 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00004571
Owen Taylor3473f882001-02-23 17:55:21 +00004572/**
Daniel Veillard968a03a2012-08-13 12:41:33 +08004573 * xmlGetLineNoInternal:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004574 * @node: valid node
Daniel Veillard968a03a2012-08-13 12:41:33 +08004575 * @depth: used to limit any risk of recursion
Daniel Veillard8faa7832001-11-26 15:58:08 +00004576 *
Daniel Veillard968a03a2012-08-13 12:41:33 +08004577 * Get line number of @node.
4578 * Try to override the limitation of lines being store in 16 bits ints
Daniel Veillard8faa7832001-11-26 15:58:08 +00004579 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004580 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004581 */
Daniel Veillard968a03a2012-08-13 12:41:33 +08004582static long
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004583xmlGetLineNoInternal(const xmlNode *node, int depth)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004584{
4585 long result = -1;
4586
Daniel Veillard968a03a2012-08-13 12:41:33 +08004587 if (depth >= 5)
4588 return(-1);
4589
Daniel Veillard8faa7832001-11-26 15:58:08 +00004590 if (!node)
4591 return result;
Daniel Veillard73da77e2005-08-24 14:05:37 +00004592 if ((node->type == XML_ELEMENT_NODE) ||
4593 (node->type == XML_TEXT_NODE) ||
4594 (node->type == XML_COMMENT_NODE) ||
Daniel Veillard968a03a2012-08-13 12:41:33 +08004595 (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 Veillard8faa7832001-11-26 15:58:08 +00004610 ((node->prev->type == XML_ELEMENT_NODE) ||
Daniel Veillard73da77e2005-08-24 14:05:37 +00004611 (node->prev->type == XML_TEXT_NODE) ||
4612 (node->prev->type == XML_COMMENT_NODE) ||
4613 (node->prev->type == XML_PI_NODE)))
Daniel Veillard968a03a2012-08-13 12:41:33 +08004614 result = xmlGetLineNoInternal(node->prev, depth + 1);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004615 else if ((node->parent != NULL) &&
Daniel Veillard73da77e2005-08-24 14:05:37 +00004616 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard968a03a2012-08-13 12:41:33 +08004617 result = xmlGetLineNoInternal(node->parent, depth + 1);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004618
4619 return result;
4620}
4621
Daniel Veillard968a03a2012-08-13 12:41:33 +08004622/**
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 */
4632long
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004633xmlGetLineNo(const xmlNode *node)
Daniel Veillard968a03a2012-08-13 12:41:33 +08004634{
4635 return(xmlGetLineNoInternal(node, 0));
4636}
4637
Daniel Veillard2156d432004-03-04 15:59:36 +00004638#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004639/**
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 */
4648xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004649xmlGetNodePath(const xmlNode *node)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004650{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004651 const xmlNode *cur, *tmp, *next;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004652 xmlChar *buffer = NULL, *temp;
4653 size_t buf_len;
4654 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004655 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004656 const char *name;
4657 char nametemp[100];
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004658 int occur = 0, generic;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004659
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004660 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004661 return (NULL);
4662
4663 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004664 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004665 if (buffer == NULL) {
4666 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004667 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004668 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004669 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004670 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004671 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004672 xmlFree(buffer);
4673 return (NULL);
4674 }
4675
4676 buffer[0] = 0;
4677 cur = node;
4678 do {
4679 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004680 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004681 occur = 0;
4682 if ((cur->type == XML_DOCUMENT_NODE) ||
4683 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4684 if (buffer[0] == '/')
4685 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004686 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004687 next = NULL;
4688 } else if (cur->type == XML_ELEMENT_NODE) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004689 generic = 0;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004690 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004691 name = (const char *) cur->name;
4692 if (cur->ns) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004693 if (cur->ns->prefix != NULL) {
William M. Brack13dfa872004-09-18 04:52:08 +00004694 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004695 (char *)cur->ns->prefix, (char *)cur->name);
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004696 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 Veillardaa6de472008-08-25 14:53:31 +00004705 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004706 }
4707 next = cur->parent;
4708
4709 /*
4710 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004711 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004712 */
4713 tmp = cur->prev;
4714 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004715 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004716 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004717 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004718 ((tmp->ns == cur->ns) ||
4719 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004720 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004721 occur++;
4722 tmp = tmp->prev;
4723 }
4724 if (occur == 0) {
4725 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004726 while (tmp != NULL && occur == 0) {
4727 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004728 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004729 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004730 ((tmp->ns == cur->ns) ||
4731 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004732 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004733 occur++;
4734 tmp = tmp->next;
4735 }
4736 if (occur != 0)
4737 occur = 1;
4738 } else
4739 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004740 } 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. Buchcikeb468702006-02-15 10:57:50 +00004776 if ((tmp->type == XML_TEXT_NODE) ||
4777 (tmp->type == XML_CDATA_SECTION_NODE))
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004778 occur++;
4779 tmp = tmp->prev;
4780 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004781 /*
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 Veillard8606bbb2002-11-12 12:36:52 +00004785 if (occur == 0) {
4786 tmp = cur->next;
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004787 while (tmp != NULL) {
4788 if ((tmp->type == XML_TEXT_NODE) ||
4789 (tmp->type == XML_CDATA_SECTION_NODE))
4790 {
4791 occur = 1;
4792 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004793 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004794 tmp = tmp->next;
4795 }
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004796 } else
4797 occur++;
4798 } else if (cur->type == XML_PI_NODE) {
4799 sep = "/";
4800 snprintf(nametemp, sizeof(nametemp) - 1,
William M. Brack13dfa872004-09-18 04:52:08 +00004801 "processing-instruction('%s')", (char *)cur->name);
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004802 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 Veillard8faa7832001-11-26 15:58:08 +00004830 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004831 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004832 name = (const char *) (((xmlAttrPtr) cur)->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004833 if (cur->ns) {
4834 if (cur->ns->prefix != NULL)
4835 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004836 (char *)cur->ns->prefix, (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004837 else
4838 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004839 (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004840 nametemp[sizeof(nametemp) - 1] = 0;
4841 name = nametemp;
4842 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004843 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 Veillard18ec16e2003-10-07 23:16:40 +00004856 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004857 xmlFree(buf);
4858 xmlFree(buffer);
4859 return (NULL);
4860 }
4861 buffer = temp;
4862 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4863 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004864 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004865 xmlFree(buf);
4866 xmlFree(buffer);
4867 return (NULL);
4868 }
4869 buf = temp;
4870 }
4871 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004872 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004873 sep, name, (char *) buffer);
4874 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004875 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004876 sep, name, occur, (char *) buffer);
William M. Brack13dfa872004-09-18 04:52:08 +00004877 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004878 cur = next;
4879 } while (cur != NULL);
4880 xmlFree(buf);
4881 return (buffer);
4882}
Daniel Veillard652327a2003-09-29 18:02:38 +00004883#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004884
4885/**
Owen Taylor3473f882001-02-23 17:55:21 +00004886 * 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 Veillardd1640922001-12-17 15:30:10 +00004892 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004893 */
4894xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004895xmlDocGetRootElement(const xmlDoc *doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00004896 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 Veillardaa6de472008-08-25 14:53:31 +00004907
Daniel Veillard2156d432004-03-04 15:59:36 +00004908#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004909/**
4910 * xmlDocSetRootElement:
4911 * @doc: the document
Daniel Veillard26a45c82006-10-20 12:55:34 +00004912 * @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 Taylor3473f882001-02-23 17:55:21 +00004914 *
4915 * Set the root element of the document (doc->children is a list
4916 * containing possibly comments, PIs, etc ...).
4917 *
Daniel Veillard26a45c82006-10-20 12:55:34 +00004918 * Returns the old root element if any was found, NULL if root was NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004919 */
4920xmlNodePtr
4921xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4922 xmlNodePtr old = NULL;
4923
4924 if (doc == NULL) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004925 if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
Daniel Veillardc575b992002-02-08 13:28:40 +00004926 return(NULL);
4927 xmlUnlinkNode(root);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00004928 xmlSetTreeDoc(root, doc);
Daniel Veillardc575b992002-02-08 13:28:40 +00004929 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004930 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 Veillard2156d432004-03-04 15:59:36 +00004948#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00004949
Daniel Veillard2156d432004-03-04 15:59:36 +00004950#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004951/**
4952 * xmlNodeSetLang:
4953 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004954 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004955 *
4956 * Set the language of a node, i.e. the values of the xml:lang
4957 * attribute.
4958 */
4959void
4960xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004961 xmlNsPtr ns;
4962
Owen Taylor3473f882001-02-23 17:55:21 +00004963 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 Veillardeae522a2001-04-23 13:41:34 +00004981#ifdef LIBXML_DOCB_ENABLED
4982 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004983#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 Veillardcfa0d812002-01-17 08:46:58 +00004991 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4992 if (ns == NULL)
4993 return;
4994 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004995}
Daniel Veillard652327a2003-09-29 18:02:38 +00004996#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardaa6de472008-08-25 14:53:31 +00004997
Owen Taylor3473f882001-02-23 17:55:21 +00004998/**
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 Veillardbd9afb52002-09-25 22:25:35 +00005006 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005007 */
5008xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005009xmlNodeGetLang(const xmlNode *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00005010 xmlChar *lang;
5011
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005012 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
5013 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005014 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00005015 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00005016 if (lang != NULL)
5017 return(lang);
5018 cur = cur->parent;
5019 }
5020 return(NULL);
5021}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005022
Owen Taylor3473f882001-02-23 17:55:21 +00005023
Daniel Veillard652327a2003-09-29 18:02:38 +00005024#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005025/**
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 */
5033void
5034xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005035 xmlNsPtr ns;
5036
Owen Taylor3473f882001-02-23 17:55:21 +00005037 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 Veillardeae522a2001-04-23 13:41:34 +00005057#ifdef LIBXML_DOCB_ENABLED
5058 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005059#endif
5060 return;
5061 case XML_ELEMENT_NODE:
5062 case XML_ATTRIBUTE_NODE:
5063 break;
5064 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005065 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5066 if (ns == NULL)
5067 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005068 switch (val) {
5069 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005070 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00005071 break;
5072 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005073 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00005074 break;
5075 }
5076}
Daniel Veillard652327a2003-09-29 18:02:38 +00005077#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005078
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 Veillardd1640922001-12-17 15:30:10 +00005087 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00005088 */
5089int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005090xmlNodeGetSpacePreserve(const xmlNode *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00005091 xmlChar *space;
5092
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005093 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
5094 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00005095 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005096 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00005097 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 Veillardaa6de472008-08-25 14:53:31 +00005112
Daniel Veillard652327a2003-09-29 18:02:38 +00005113#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005114/**
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 */
5121void
5122xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00005123 xmlDocPtr doc;
5124 xmlDictPtr dict;
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005125 const xmlChar *freeme = NULL;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005126
Owen Taylor3473f882001-02-23 17:55:21 +00005127 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 Veillardeae522a2001-04-23 13:41:34 +00005140#ifdef LIBXML_DOCB_ENABLED
5141 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005142#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 Veillardce244ad2004-11-05 10:03:46 +00005156 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 Berkomf0dd6e12014-04-22 21:15:05 -04005163 freeme = cur->name;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005164 cur->name = xmlDictLookup(dict, name, -1);
5165 } else {
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005166 if (cur->name != NULL)
5167 freeme = cur->name;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005168 cur->name = xmlStrdup(name);
5169 }
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005170
5171 if (freeme)
5172 xmlFree((xmlChar *) freeme);
Owen Taylor3473f882001-02-23 17:55:21 +00005173}
Daniel Veillard2156d432004-03-04 15:59:36 +00005174#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00005175
Daniel Veillard2156d432004-03-04 15:59:36 +00005176#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005177/**
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 */
5185void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00005186xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005187 xmlNsPtr ns;
Martin Trappelf3703102010-01-22 12:08:00 +01005188 xmlChar* fixed;
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005189
Owen Taylor3473f882001-02-23 17:55:21 +00005190 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 Taylor3473f882001-02-23 17:55:21 +00005195 case XML_DOCUMENT_TYPE_NODE:
5196 case XML_DOCUMENT_FRAG_NODE:
5197 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005198 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 Taylor3473f882001-02-23 17:55:21 +00005208 return;
5209 case XML_ELEMENT_NODE:
5210 case XML_ATTRIBUTE_NODE:
5211 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005212 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 Veillardb8efdda2006-10-10 12:37:14 +00005224 doc->URL = xmlPathToURI(uri);
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005225 return;
5226 }
Owen Taylor3473f882001-02-23 17:55:21 +00005227 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00005228
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005229 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5230 if (ns == NULL)
5231 return;
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005232 fixed = xmlPathToURI(uri);
5233 if (fixed != NULL) {
5234 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
Martin Trappelf3703102010-01-22 12:08:00 +01005235 xmlFree(fixed);
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005236 } else {
5237 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5238 }
Owen Taylor3473f882001-02-23 17:55:21 +00005239}
Daniel Veillard652327a2003-09-29 18:02:38 +00005240#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005241
5242/**
Owen Taylor3473f882001-02-23 17:55:21 +00005243 * 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 Veillarde4d18492010-03-09 11:12:30 +01005254 * doc->URL in this case
Owen Taylor3473f882001-02-23 17:55:21 +00005255 *
5256 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005257 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005258 */
5259xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005260xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005261 xmlChar *oldbase = NULL;
5262 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00005263
Daniel Veillardaa6de472008-08-25 14:53:31 +00005264 if ((cur == NULL) && (doc == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005265 return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005266 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
5267 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005268 if (doc == NULL) doc = cur->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00005269 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 Veillard42596ad2001-05-22 16:57:14 +00005296 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005297 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00005298 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005299 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 Veillard42596ad2001-05-22 16:57:14 +00005317 }
5318 }
Owen Taylor3473f882001-02-23 17:55:21 +00005319 cur = cur->parent;
5320 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005321 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 Taylor3473f882001-02-23 17:55:21 +00005329}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005330
Owen Taylor3473f882001-02-23 17:55:21 +00005331/**
Daniel Veillard78697292003-10-19 20:44:43 +00005332 * 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 Veillardaa6de472008-08-25 14:53:31 +00005341 *
Daniel Veillard78697292003-10-19 20:44:43 +00005342 * Returns 0 in case of success and -1 in case of error.
5343 */
5344int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005345xmlNodeBufGetContent(xmlBufferPtr buffer, const xmlNode *cur)
Daniel Veillard78697292003-10-19 20:44:43 +00005346{
Daniel Veillarddddeede2012-07-16 14:44:26 +08005347 xmlBufPtr buf;
5348 int ret;
5349
Daniel Veillard78697292003-10-19 20:44:43 +00005350 if ((cur == NULL) || (buffer == NULL)) return(-1);
Daniel Veillarddddeede2012-07-16 14:44:26 +08005351 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 Veillard28cc42d2012-08-10 10:00:18 +08005361 * @buf: a buffer xmlBufPtr
Daniel Veillarddddeede2012-07-16 14:44:26 +08005362 * @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 Roeckx95ebe532014-10-13 16:06:21 +08005368 * Fills up the buffer @buf with this value
Daniel Veillarddddeede2012-07-16 14:44:26 +08005369 *
5370 * Returns 0 in case of success and -1 in case of error.
5371 */
5372int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005373xmlBufGetNodeContent(xmlBufPtr buf, const xmlNode *cur)
Daniel Veillarddddeede2012-07-16 14:44:26 +08005374{
5375 if ((cur == NULL) || (buf == NULL)) return(-1);
Daniel Veillard78697292003-10-19 20:44:43 +00005376 switch (cur->type) {
5377 case XML_CDATA_SECTION_NODE:
5378 case XML_TEXT_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005379 xmlBufCat(buf, cur->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005380 break;
5381 case XML_DOCUMENT_FRAG_NODE:
5382 case XML_ELEMENT_NODE:{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005383 const xmlNode *tmp = cur;
Daniel Veillard78697292003-10-19 20:44:43 +00005384
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 Veillarddddeede2012-07-16 14:44:26 +08005390 xmlBufCat(buf, tmp->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005391 break;
5392 case XML_ENTITY_REF_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005393 xmlBufGetNodeContent(buf, tmp);
Rob Richards77b92ff2005-12-20 15:55:14 +00005394 break;
Daniel Veillard78697292003-10-19 20:44:43 +00005395 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 Veillarddddeede2012-07-16 14:44:26 +08005437 xmlBufCat(buf, tmp->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005438 else
Daniel Veillarddddeede2012-07-16 14:44:26 +08005439 xmlBufGetNodeContent(buf, tmp);
Daniel Veillard78697292003-10-19 20:44:43 +00005440 tmp = tmp->next;
5441 }
5442 break;
5443 }
5444 case XML_COMMENT_NODE:
5445 case XML_PI_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005446 xmlBufCat(buf, cur->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005447 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 Veillarddddeede2012-07-16 14:44:26 +08005464 xmlBufGetNodeContent(buf, tmp);
Daniel Veillard78697292003-10-19 20:44:43 +00005465 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 Veillarddddeede2012-07-16 14:44:26 +08005486 xmlBufGetNodeContent(buf, cur);
Daniel Veillard78697292003-10-19 20:44:43 +00005487 }
5488 cur = cur->next;
5489 }
5490 break;
5491 case XML_NAMESPACE_DECL:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005492 xmlBufCat(buf, ((xmlNsPtr) cur)->href);
Daniel Veillard78697292003-10-19 20:44:43 +00005493 break;
5494 case XML_ELEMENT_DECL:
5495 case XML_ATTRIBUTE_DECL:
5496 case XML_ENTITY_DECL:
5497 break;
5498 }
5499 return(0);
5500}
Daniel Veillarddddeede2012-07-16 14:44:26 +08005501
Daniel Veillard78697292003-10-19 20:44:43 +00005502/**
Owen Taylor3473f882001-02-23 17:55:21 +00005503 * 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 Veillardd1640922001-12-17 15:30:10 +00005509 * Entity references are substituted.
5510 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005511 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005512 */
5513xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005514xmlNodeGetContent(const xmlNode *cur)
Daniel Veillard7646b182002-04-20 06:41:40 +00005515{
5516 if (cur == NULL)
5517 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005518 switch (cur->type) {
5519 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005520 case XML_ELEMENT_NODE:{
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005521 xmlBufPtr buf;
Daniel Veillard7646b182002-04-20 06:41:40 +00005522 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00005523
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005524 buf = xmlBufCreateSize(64);
5525 if (buf == NULL)
Daniel Veillard7646b182002-04-20 06:41:40 +00005526 return (NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005527 xmlBufGetNodeContent(buf, cur);
5528 ret = xmlBufDetach(buf);
5529 xmlBufFree(buf);
Daniel Veillard7646b182002-04-20 06:41:40 +00005530 return (ret);
5531 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005532 case XML_ATTRIBUTE_NODE:
5533 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
Owen Taylor3473f882001-02-23 17:55:21 +00005534 case XML_COMMENT_NODE:
5535 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005536 if (cur->content != NULL)
5537 return (xmlStrdup(cur->content));
5538 return (NULL);
5539 case XML_ENTITY_REF_NODE:{
5540 xmlEntityPtr ent;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005541 xmlBufPtr buf;
Daniel Veillard7646b182002-04-20 06:41:40 +00005542 xmlChar *ret;
5543
5544 /* lookup entity declaration */
5545 ent = xmlGetDocEntity(cur->doc, cur->name);
5546 if (ent == NULL)
5547 return (NULL);
5548
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005549 buf = xmlBufCreate();
5550 if (buf == NULL)
Daniel Veillard7646b182002-04-20 06:41:40 +00005551 return (NULL);
5552
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005553 xmlBufGetNodeContent(buf, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005554
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005555 ret = xmlBufDetach(buf);
5556 xmlBufFree(buf);
Daniel Veillard7646b182002-04-20 06:41:40 +00005557 return (ret);
5558 }
Owen Taylor3473f882001-02-23 17:55:21 +00005559 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005560 case XML_DOCUMENT_TYPE_NODE:
5561 case XML_NOTATION_NODE:
5562 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005563 case XML_XINCLUDE_START:
5564 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00005565 return (NULL);
5566 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005567#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00005568 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005569#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00005570 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005571 xmlBufPtr buf;
Daniel Veillardc4696922003-10-19 21:47:14 +00005572 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00005573
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005574 buf = xmlBufCreate();
5575 if (buf == NULL)
Daniel Veillardc4696922003-10-19 21:47:14 +00005576 return (NULL);
5577
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005578 xmlBufGetNodeContent(buf, (xmlNodePtr) cur);
Daniel Veillardc4696922003-10-19 21:47:14 +00005579
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005580 ret = xmlBufDetach(buf);
5581 xmlBufFree(buf);
Daniel Veillardc4696922003-10-19 21:47:14 +00005582 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00005583 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00005584 case XML_NAMESPACE_DECL: {
5585 xmlChar *tmp;
5586
5587 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5588 return (tmp);
5589 }
Owen Taylor3473f882001-02-23 17:55:21 +00005590 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005591 /* TODO !!! */
5592 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005593 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005594 /* TODO !!! */
5595 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005596 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005597 /* TODO !!! */
5598 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005599 case XML_CDATA_SECTION_NODE:
5600 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005601 if (cur->content != NULL)
5602 return (xmlStrdup(cur->content));
5603 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005604 }
Daniel Veillard7646b182002-04-20 06:41:40 +00005605 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005606}
Daniel Veillard652327a2003-09-29 18:02:38 +00005607
Owen Taylor3473f882001-02-23 17:55:21 +00005608/**
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 Veillard0e05f4c2006-11-01 15:33:04 +00005614 * 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 Taylor3473f882001-02-23 17:55:21 +00005617 */
5618void
5619xmlNodeSetContent(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 Veillard2c748c62002-01-16 15:37:50 +00005630 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005631 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5632 cur->children = xmlStringGetNodeList(cur->doc, content);
5633 UPDATE_LAST_CHILD_AND_PARENT(cur)
5634 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005635 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 Veillard8874b942005-08-25 13:19:21 +00005641 if ((cur->content != NULL) &&
5642 (cur->content != (xmlChar *) &(cur->properties))) {
William M. Brack7762bb12004-01-04 14:49:01 +00005643 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
Daniel Veillardc587bce2005-05-10 15:28:08 +00005644 (xmlDictOwns(cur->doc->dict, cur->content))))
William M. Brack7762bb12004-01-04 14:49:01 +00005645 xmlFree(cur->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005646 }
Owen Taylor3473f882001-02-23 17:55:21 +00005647 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5648 cur->last = cur->children = NULL;
5649 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005650 cur->content = xmlStrdup(content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005651 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005652 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005653 cur->properties = NULL;
5654 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005655 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 Veillardeae522a2001-04-23 13:41:34 +00005661#ifdef LIBXML_DOCB_ENABLED
5662 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005663#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 Veillard652327a2003-09-29 18:02:38 +00005683#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005684/**
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 Veillard0e05f4c2006-11-01 15:33:04 +00005691 * 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 Taylor3473f882001-02-23 17:55:21 +00005694 */
5695void
5696xmlNodeSetContentLen(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 Veillard2c748c62002-01-16 15:37:50 +00005707 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005708 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 Taylor3473f882001-02-23 17:55:21 +00005712 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 Veillard8874b942005-08-25 13:19:21 +00005719 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 Veillardaa6de472008-08-25 14:53:31 +00005724 }
Owen Taylor3473f882001-02-23 17:55:21 +00005725 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5726 cur->children = cur->last = NULL;
5727 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005728 cur->content = xmlStrndup(content, len);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005729 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005730 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005731 cur->properties = NULL;
5732 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005733 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 Veillardeae522a2001-04-23 13:41:34 +00005741#ifdef LIBXML_DOCB_ENABLED
5742 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005743#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 Veillard652327a2003-09-29 18:02:38 +00005756#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005757
5758/**
5759 * xmlNodeAddContentLen:
5760 * @cur: the node being modified
5761 * @content: extra content
5762 * @len: the size of @content
Daniel Veillardaa6de472008-08-25 14:53:31 +00005763 *
Owen Taylor3473f882001-02-23 17:55:21 +00005764 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005765 * 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 Taylor3473f882001-02-23 17:55:21 +00005768 */
5769void
5770xmlNodeAddContentLen(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 Veillardacb2bda2002-01-13 16:15:43 +00005782 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005783
Daniel Veillard7db37732001-07-12 01:20:08 +00005784 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005785 newNode = xmlNewTextLen(content, len);
5786 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005787 tmp = xmlAddChild(cur, newNode);
5788 if (tmp != newNode)
5789 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005790 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 Veillard8874b942005-08-25 13:19:21 +00005806 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. Brack7762bb12004-01-04 14:49:01 +00005812 break;
5813 }
Owen Taylor3473f882001-02-23 17:55:21 +00005814 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005815 }
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 Veillardeae522a2001-04-23 13:41:34 +00005823#ifdef LIBXML_DOCB_ENABLED
5824 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005825#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 Veillardaa6de472008-08-25 14:53:31 +00005838 *
Owen Taylor3473f882001-02-23 17:55:21 +00005839 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005840 * 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 Taylor3473f882001-02-23 17:55:21 +00005843 */
5844void
5845xmlNodeAddContent(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 Veillardaa6de472008-08-25 14:53:31 +00005864 *
Owen Taylor3473f882001-02-23 17:55:21 +00005865 * Merge two text nodes into one
5866 * Returns the first text node augmented
5867 */
5868xmlNodePtr
5869xmlTextMerge(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 Taylor3473f882001-02-23 17:55:21 +00005876 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005877 xmlUnlinkNode(second);
5878 xmlFreeNode(second);
5879 return(first);
5880}
5881
Daniel Veillardf1a27c62006-10-13 22:33:03 +00005882#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005883/**
5884 * xmlGetNsList:
5885 * @doc: the document
5886 * @node: the current node
5887 *
5888 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005889 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005890 * that need to be freed by the caller or NULL if no
5891 * namespace if defined
5892 */
5893xmlNsPtr *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005894xmlGetNsList(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node)
Daniel Veillard77044732001-06-29 21:31:07 +00005895{
Owen Taylor3473f882001-02-23 17:55:21 +00005896 xmlNsPtr cur;
5897 xmlNsPtr *ret = NULL;
5898 int nbns = 0;
5899 int maxns = 10;
5900 int i;
5901
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005902 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5903 return(NULL);
5904
Owen Taylor3473f882001-02-23 17:55:21 +00005905 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005906 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 Veillard18ec16e2003-10-07 23:16:40 +00005914 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005915 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 Veillard18ec16e2003-10-07 23:16:40 +00005932 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005933 return (NULL);
5934 }
5935 }
5936 ret[nbns++] = cur;
5937 ret[nbns] = NULL;
5938 }
Owen Taylor3473f882001-02-23 17:55:21 +00005939
Daniel Veillard77044732001-06-29 21:31:07 +00005940 cur = cur->next;
5941 }
5942 }
5943 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005944 }
Daniel Veillard77044732001-06-29 21:31:07 +00005945 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005946}
Daniel Veillard652327a2003-09-29 18:02:38 +00005947#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005948
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005949/*
5950* xmlTreeEnsureXMLDecl:
5951* @doc: the doc
Daniel Veillardaa6de472008-08-25 14:53:31 +00005952*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005953* Ensures that there is an XML namespace declaration on the doc.
Daniel Veillardaa6de472008-08-25 14:53:31 +00005954*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005955* Returns the XML ns-struct or NULL on API and internal errors.
5956*/
5957static xmlNsPtr
5958xmlTreeEnsureXMLDecl(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 Veillardaa6de472008-08-25 14:53:31 +00005974 ns->href = xmlStrdup(XML_XML_NAMESPACE);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005975 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5976 doc->oldNs = ns;
5977 return (ns);
5978 }
5979}
5980
Owen Taylor3473f882001-02-23 17:55:21 +00005981/**
5982 * xmlSearchNs:
5983 * @doc: the document
5984 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005985 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005986 *
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 */
5997xmlNsPtr
5998xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00005999
Owen Taylor3473f882001-02-23 17:55:21 +00006000 xmlNsPtr cur;
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006001 const xmlNode *orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00006002
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006003 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006004 if ((nameSpace != NULL) &&
6005 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00006006 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 Veillard18ec16e2003-10-07 23:16:40 +00006014 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00006015 return(NULL);
6016 }
6017 memset(cur, 0, sizeof(xmlNs));
6018 cur->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006019 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6020 cur->prefix = xmlStrdup((const xmlChar *)"xml");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00006021 cur->next = node->nsDef;
6022 node->nsDef = cur;
6023 return(cur);
6024 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00006025 if (doc == NULL) {
6026 doc = node->doc;
6027 if (doc == NULL)
6028 return(NULL);
6029 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006030 /*
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 Taylor3473f882001-02-23 17:55:21 +00006037 }
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 Veillardaa6de472008-08-25 14:53:31 +00006055 if (orig != node) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00006056 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 Veillardaa6de472008-08-25 14:53:31 +00006066 }
Owen Taylor3473f882001-02-23 17:55:21 +00006067 }
6068 node = node->parent;
6069 }
6070 return(NULL);
6071}
6072
6073/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006074 * 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 Veillardaa6de472008-08-25 14:53:31 +00006082 *
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006083 * Returns 1 if true, 0 if false and -1 in case of error.
6084 */
6085static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006086xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
6087 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006088{
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 Veillardaa6de472008-08-25 14:53:31 +00006115
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006116/**
Owen Taylor3473f882001-02-23 17:55:21 +00006117 * 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 */
6126xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006127xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
6128{
Owen Taylor3473f882001-02-23 17:55:21 +00006129 xmlNsPtr cur;
6130 xmlNodePtr orig = node;
Daniel Veillard62040be2004-05-17 03:17:26 +00006131 int is_attr;
Owen Taylor3473f882001-02-23 17:55:21 +00006132
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006133 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006134 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006135 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006136 /*
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 Veillard18ec16e2003-10-07 23:16:40 +00006147 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006148 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 Veillard11ce4002006-03-10 00:36:23 +00006158 if (doc == NULL) {
6159 doc = node->doc;
6160 if (doc == NULL)
6161 return(NULL);
6162 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006163 /*
6164 * Return the XML namespace declaration held by the doc.
6165 */
6166 if (doc->oldNs == NULL)
6167 return(xmlTreeEnsureXMLDecl(doc));
6168 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00006169 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006170 }
Daniel Veillard62040be2004-05-17 03:17:26 +00006171 is_attr = (node->type == XML_ATTRIBUTE_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00006172 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006173 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 Veillard62040be2004-05-17 03:17:26 +00006182 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006183 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006184 return (cur);
6185 }
6186 cur = cur->next;
6187 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00006188 if (orig != node) {
6189 cur = node->ns;
6190 if (cur != NULL) {
6191 if ((cur->href != NULL) && (href != NULL) &&
6192 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00006193 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006194 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillardf4e56292003-10-28 14:27:41 +00006195 return (cur);
6196 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006197 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006198 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006199 }
6200 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00006201 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006202 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006203}
6204
6205/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006206 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006207 * @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 Veillard8ed10722009-08-20 19:17:36 +02006218static xmlNsPtr
Owen Taylor3473f882001-02-23 17:55:21 +00006219xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6220 xmlNsPtr def;
6221 xmlChar prefix[50];
6222 int counter = 1;
6223
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006224 if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006225#ifdef DEBUG_TREE
6226 xmlGenericError(xmlGenericErrorContext,
6227 "xmlNewReconciliedNs : tree == NULL\n");
6228#endif
6229 return(NULL);
6230 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00006231 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006232#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 Veillardf742d342002-03-07 00:05:35 +00006249 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006250 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00006251 else
William M. Brack13dfa872004-09-18 04:52:08 +00006252 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00006253
Owen Taylor3473f882001-02-23 17:55:21 +00006254 def = xmlSearchNs(doc, tree, prefix);
6255 while (def != NULL) {
6256 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00006257 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006258 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00006259 else
William M. Brack13dfa872004-09-18 04:52:08 +00006260 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
Daniel Veillardaa6de472008-08-25 14:53:31 +00006261 (char *)ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00006262 def = xmlSearchNs(doc, tree, prefix);
6263 }
6264
6265 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00006266 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00006267 */
6268 def = xmlNewNs(tree, ns->href, prefix);
6269 return(def);
6270}
6271
Daniel Veillard652327a2003-09-29 18:02:38 +00006272#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006273/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006274 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006275 * @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 Veillardd1640922001-12-17 15:30:10 +00006282 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00006283 * 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 */
6287int
6288xmlReconciliateNs(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 Veillardce244ad2004-11-05 10:03:46 +00006299 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 Taylor3473f882001-02-23 17:55:21 +00006302 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 Veillard18ec16e2003-10-07 23:16:40 +00006315 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006316 return(-1);
6317 }
6318 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6319 sizeof(xmlNsPtr));
6320 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006321 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006322 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 Veillardd1640922001-12-17 15:30:10 +00006334 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00006335 */
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 Veillard18ec16e2003-10-07 23:16:40 +00006346 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006347 xmlFree(newNs);
6348 return(-1);
6349 }
6350 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6351 sizeof(xmlNsPtr));
6352 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006353 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006354 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 Veillardb5f11972006-10-14 08:46:40 +00006367 if (node->type == XML_ELEMENT_NODE) {
6368 attr = node->properties;
6369 while (attr != NULL) {
6370 if (attr->ns != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006371 /*
Daniel Veillardb5f11972006-10-14 08:46:40 +00006372 * initialize the cache if needed
Owen Taylor3473f882001-02-23 17:55:21 +00006373 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006374 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 Taylor3473f882001-02-23 17:55:21 +00006381 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006382 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 Taylor3473f882001-02-23 17:55:21 +00006426 }
6427 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006428 attr = attr->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006429 }
Owen Taylor3473f882001-02-23 17:55:21 +00006430 }
6431
6432 /*
6433 * Browse the full subtree, deep first
6434 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006435 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006436 /* 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 Veillardaa6de472008-08-25 14:53:31 +00006456 if (node == tree)
Owen Taylor3473f882001-02-23 17:55:21 +00006457 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00006458 } else
6459 break;
Owen Taylor3473f882001-02-23 17:55:21 +00006460 }
Daniel Veillardf742d342002-03-07 00:05:35 +00006461 if (oldNs != NULL)
6462 xmlFree(oldNs);
6463 if (newNs != NULL)
6464 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006465 return(ret);
6466}
Daniel Veillard652327a2003-09-29 18:02:38 +00006467#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00006468
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006469static xmlAttrPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006470xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006471 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 Veillardaa6de472008-08-25 14:53:31 +00006512 */
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006513 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 Veillardaa6de472008-08-25 14:53:31 +00006519 * We need the QName of the element for the DTD-lookup.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006520 */
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 Veillardaa6de472008-08-25 14:53:31 +00006570 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006571 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
6583static xmlChar*
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006584xmlGetPropNodeValueInternal(const xmlAttr *prop)
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006585{
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. Buchcik978039b2006-06-16 19:46:26 +00006594 if ((prop->children->next == NULL) &&
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006595 ((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 Veillardaa6de472008-08-25 14:53:31 +00006614 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006615}
6616
Owen Taylor3473f882001-02-23 17:55:21 +00006617/**
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 Veillardaa6de472008-08-25 14:53:31 +00006626 * Returns the attribute or the attribute declaration or NULL if
Owen Taylor3473f882001-02-23 17:55:21 +00006627 * neither was found.
6628 */
6629xmlAttrPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006630xmlHasProp(const xmlNode *node, const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00006631 xmlAttrPtr prop;
6632 xmlDocPtr doc;
6633
Daniel Veillard8874b942005-08-25 13:19:21 +00006634 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6635 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006636 /*
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 Veillard75eb1ad2003-07-07 14:42:44 +00006659 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6660 /* return attribute declaration only if a default value is given
6661 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00006662 return((xmlAttrPtr) attrDecl);
6663 }
6664 }
6665 return(NULL);
6666}
6667
6668/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00006669 * xmlHasNsProp:
6670 * @node: the node
6671 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006672 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00006673 *
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. Brack2c228442004-10-03 04:10:00 +00006679 * Note that a namespace of NULL indicates to use the default namespace.
Daniel Veillarde95e2392001-06-06 10:46:28 +00006680 *
6681 * Returns the attribute or the attribute declaration or NULL
6682 * if neither was found.
6683 */
6684xmlAttrPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006685xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00006686
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006687 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
Daniel Veillarde95e2392001-06-06 10:46:28 +00006688}
6689
6690/**
Owen Taylor3473f882001-02-23 17:55:21 +00006691 * 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 Veillard784b9352003-02-16 15:50:27 +00006699 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00006700 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6701 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00006702 *
6703 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006704 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006705 */
6706xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006707xmlGetProp(const xmlNode *node, const xmlChar *name) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00006708 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006709
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006710 prop = xmlHasProp(node, name);
6711 if (prop == NULL)
6712 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00006713 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006714}
6715
6716/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006717 * 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 */
6731xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006732xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) {
Daniel Veillard71531f32003-02-05 13:19:53 +00006733 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006734
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006735 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6736 if (prop == NULL)
Daniel Veillard8874b942005-08-25 13:19:21 +00006737 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006738 return(xmlGetPropNodeValueInternal(prop));
Daniel Veillard71531f32003-02-05 13:19:53 +00006739}
6740
6741/**
Owen Taylor3473f882001-02-23 17:55:21 +00006742 * xmlGetNsProp:
6743 * @node: the node
6744 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006745 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006746 *
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 Veillardbd9afb52002-09-25 22:25:35 +00006754 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006755 */
6756xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006757xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006758 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006759
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006760 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6761 if (prop == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006762 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006763 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006764}
6765
Daniel Veillard2156d432004-03-04 15:59:36 +00006766#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. Buchcik30f874d2006-03-02 18:04:29 +00006773 * This handles only attributes in no namespace.
Daniel Veillard2156d432004-03-04 15:59:36 +00006774 * Returns 0 if successful, -1 if not found
6775 */
6776int
6777xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006778 xmlAttrPtr prop;
Daniel Veillard2156d432004-03-04 15:59:36 +00006779
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006780 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6781 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006782 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006783 xmlUnlinkNode((xmlNodePtr) prop);
6784 xmlFreeProp(prop);
6785 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006786}
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 */
6797int
6798xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006799 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006800
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006801 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6802 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006803 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006804 xmlUnlinkNode((xmlNodePtr) prop);
6805 xmlFreeProp(prop);
6806 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006807}
6808#endif
6809
6810#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00006811/**
6812 * xmlSetProp:
6813 * @node: the node
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006814 * @name: the attribute name (a QName)
Owen Taylor3473f882001-02-23 17:55:21 +00006815 * @value: the attribute value
6816 *
6817 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006818 * 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 Taylor3473f882001-02-23 17:55:21 +00006822 * Returns the attribute pointer.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006823 *
Owen Taylor3473f882001-02-23 17:55:21 +00006824 */
6825xmlAttrPtr
6826xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006827 int len;
6828 const xmlChar *nqname;
Owen Taylor3473f882001-02-23 17:55:21 +00006829
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006830 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006831 return(NULL);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006832
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. Buchcik30f874d2006-03-02 18:04:29 +00006846 return(xmlSetNsProp(node, NULL, name, value));
Owen Taylor3473f882001-02-23 17:55:21 +00006847}
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. Buchcik30f874d2006-03-02 18:04:29 +00006857 * The ns structure must be in scope, this is not checked
Owen Taylor3473f882001-02-23 17:55:21 +00006858 *
6859 * Returns the attribute pointer.
6860 */
6861xmlAttrPtr
6862xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006863 const xmlChar *value)
6864{
Owen Taylor3473f882001-02-23 17:55:21 +00006865 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006866
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006867 if (ns && (ns->href == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006868 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006869 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6870 if (prop != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006871 /*
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006872 * 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 Veillardaa6de472008-08-25 14:53:31 +00006878 if (prop->children != NULL)
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006879 xmlFreeNodeList(prop->children);
6880 prop->children = NULL;
6881 prop->last = NULL;
6882 prop->ns = ns;
6883 if (value != NULL) {
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006884 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006885
Daniel Veillard6f8611f2008-02-15 08:33:21 +00006886 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 Taylor3473f882001-02-23 17:55:21 +00006893 prop->last = NULL;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006894 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. Buchcik30f874d2006-03-02 18:04:29 +00006901 }
6902 if (prop->atype == XML_ATTRIBUTE_ID)
6903 xmlAddID(NULL, node->doc, value, prop);
6904 return(prop);
Owen Taylor3473f882001-02-23 17:55:21 +00006905 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006906 /*
6907 * No equal attr found; create a new one.
6908 */
6909 return(xmlNewPropInternal(node, ns, name, value, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00006910}
6911
Daniel Veillard652327a2003-09-29 18:02:38 +00006912#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006913
6914/**
Owen Taylor3473f882001-02-23 17:55:21 +00006915 * xmlNodeIsText:
6916 * @node: the node
Daniel Veillardaa6de472008-08-25 14:53:31 +00006917 *
Owen Taylor3473f882001-02-23 17:55:21 +00006918 * Is this node a Text node ?
6919 * Returns 1 yes, 0 no
6920 */
6921int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006922xmlNodeIsText(const xmlNode *node) {
Owen Taylor3473f882001-02-23 17:55:21 +00006923 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 Veillardaa6de472008-08-25 14:53:31 +00006932 *
Owen Taylor3473f882001-02-23 17:55:21 +00006933 * Checks whether this node is an empty or whitespace only
6934 * (and possibly ignorable) text-node.
6935 *
6936 * Returns 1 yes, 0 no
6937 */
6938int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006939xmlIsBlankNode(const xmlNode *node) {
Owen Taylor3473f882001-02-23 17:55:21 +00006940 const xmlChar *cur;
6941 if (node == NULL) return(0);
6942
Daniel Veillard7db37732001-07-12 01:20:08 +00006943 if ((node->type != XML_TEXT_NODE) &&
6944 (node->type != XML_CDATA_SECTION_NODE))
6945 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006946 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006947 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006948 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006949 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006950 cur++;
6951 }
6952
6953 return(1);
6954}
6955
6956/**
6957 * xmlTextConcat:
6958 * @node: the node
6959 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006960 * @len: @content length
Daniel Veillardaa6de472008-08-25 14:53:31 +00006961 *
Owen Taylor3473f882001-02-23 17:55:21 +00006962 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006963 *
6964 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006965 */
6966
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006967int
Owen Taylor3473f882001-02-23 17:55:21 +00006968xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006969 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006970
6971 if ((node->type != XML_TEXT_NODE) &&
Rob Richardsa02f1992006-09-16 14:04:26 +00006972 (node->type != XML_CDATA_SECTION_NODE) &&
6973 (node->type != XML_COMMENT_NODE) &&
6974 (node->type != XML_PI_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006975#ifdef DEBUG_TREE
6976 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006977 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006978#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006979 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006980 }
William M. Brack7762bb12004-01-04 14:49:01 +00006981 /* need to check if content is currently in the dictionary */
Daniel Veillard8874b942005-08-25 13:19:21 +00006982 if ((node->content == (xmlChar *) &(node->properties)) ||
6983 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6984 xmlDictOwns(node->doc->dict, node->content))) {
William M. Brack7762bb12004-01-04 14:49:01 +00006985 node->content = xmlStrncatNew(node->content, content, len);
6986 } else {
6987 node->content = xmlStrncat(node->content, content, len);
6988 }
Daniel Veillard8874b942005-08-25 13:19:21 +00006989 node->properties = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006990 if (node->content == NULL)
6991 return(-1);
6992 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006993}
6994
6995/************************************************************************
6996 * *
6997 * Output : to a FILE or in memory *
6998 * *
6999 ************************************************************************/
7000
Owen Taylor3473f882001-02-23 17:55:21 +00007001/**
7002 * xmlBufferCreate:
7003 *
7004 * routine to create an XML buffer.
7005 * returns the new structure.
7006 */
7007xmlBufferPtr
7008xmlBufferCreate(void) {
7009 xmlBufferPtr ret;
7010
7011 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7012 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007013 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007014 return(NULL);
7015 }
7016 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00007017 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00007018 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007019 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007020 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007021 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007022 xmlFree(ret);
7023 return(NULL);
7024 }
7025 ret->content[0] = 0;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007026 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00007027 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 */
7037xmlBufferPtr
7038xmlBufferCreateSize(size_t size) {
7039 xmlBufferPtr ret;
7040
7041 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7042 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007043 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007044 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 Veillard3c908dc2003-04-19 00:07:51 +00007050 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007051 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007052 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007053 xmlFree(ret);
7054 return(NULL);
7055 }
7056 ret->content[0] = 0;
7057 } else
7058 ret->content = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007059 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00007060 return(ret);
7061}
7062
7063/**
Daniel Veillard79ee2842012-05-15 10:25:31 +08007064 * xmlBufferDetach:
Conrad Irwin7d553f82012-05-10 20:17:25 -07007065 * @buf: the buffer
7066 *
Daniel Veillard79ee2842012-05-15 10:25:31 +08007067 * Remove the string contained in a buffer and gie it back to the
Daniel Veillard94431ec2012-05-15 10:45:05 +08007068 * caller. The buffer is reset to an empty content.
7069 * This doesn't work with immutable buffers as they can't be reset.
Conrad Irwin7d553f82012-05-10 20:17:25 -07007070 *
Daniel Veillard79ee2842012-05-15 10:25:31 +08007071 * Returns the previous string contained by the buffer.
Conrad Irwin7d553f82012-05-10 20:17:25 -07007072 */
7073xmlChar *
7074xmlBufferDetach(xmlBufferPtr buf) {
7075 xmlChar *ret;
7076
Daniel Veillard94431ec2012-05-15 10:45:05 +08007077 if (buf == NULL)
7078 return(NULL);
7079 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
7080 return(NULL);
Conrad Irwin7d553f82012-05-10 20:17:25 -07007081
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 Veillard53350552003-09-18 13:35:51 +00007092 * xmlBufferCreateStatic:
7093 * @mem: the memory area
7094 * @size: the size in byte
7095 *
MST 2003 John Flecka0e7e932003-12-19 03:13:47 +00007096 * 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 Veillard53350552003-09-18 13:35:51 +00007098 * present until the end of the buffer lifetime.
7099 *
7100 * returns the new structure.
7101 */
7102xmlBufferPtr
7103xmlBufferCreateStatic(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 Veillard18ec16e2003-10-07 23:16:40 +00007111 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00007112 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 Taylor3473f882001-02-23 17:55:21 +00007122 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00007123 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00007124 * @scheme: allocation scheme to use
7125 *
7126 * Sets the allocation scheme for this buffer
7127 */
7128void
Daniel Veillardaa6de472008-08-25 14:53:31 +00007129xmlBufferSetAllocationScheme(xmlBufferPtr buf,
Owen Taylor3473f882001-02-23 17:55:21 +00007130 xmlBufferAllocationScheme scheme) {
7131 if (buf == NULL) {
7132#ifdef DEBUG_BUFFER
7133 xmlGenericError(xmlGenericErrorContext,
7134 "xmlBufferSetAllocationScheme: buf == NULL\n");
7135#endif
7136 return;
7137 }
Daniel Veillardda3fee42008-09-01 13:08:57 +00007138 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 Irwin7d0d2a52012-05-14 14:18:58 +08007142 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
Daniel Veillardda3fee42008-09-01 13:08:57 +00007143 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
7144 buf->alloc = scheme;
Owen Taylor3473f882001-02-23 17:55:21 +00007145}
7146
7147/**
7148 * xmlBufferFree:
7149 * @buf: the buffer to free
7150 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00007151 * Frees an XML buffer. It frees both the content and the structure which
7152 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00007153 */
7154void
7155xmlBufferFree(xmlBufferPtr buf) {
7156 if (buf == NULL) {
7157#ifdef DEBUG_BUFFER
7158 xmlGenericError(xmlGenericErrorContext,
7159 "xmlBufferFree: buf == NULL\n");
7160#endif
7161 return;
7162 }
Daniel Veillard53350552003-09-18 13:35:51 +00007163
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007164 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7165 (buf->contentIO != NULL)) {
7166 xmlFree(buf->contentIO);
7167 } else if ((buf->content != NULL) &&
Daniel Veillard53350552003-09-18 13:35:51 +00007168 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007169 xmlFree(buf->content);
7170 }
Owen Taylor3473f882001-02-23 17:55:21 +00007171 xmlFree(buf);
7172}
7173
7174/**
7175 * xmlBufferEmpty:
7176 * @buf: the buffer
7177 *
7178 * empty a buffer.
7179 */
7180void
7181xmlBufferEmpty(xmlBufferPtr buf) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007182 if (buf == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007183 if (buf->content == NULL) return;
7184 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00007185 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00007186 buf->content = BAD_CAST "";
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007187 } 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 Veillard53350552003-09-18 13:35:51 +00007194 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007195 buf->content[0] = 0;
7196 }
Owen Taylor3473f882001-02-23 17:55:21 +00007197}
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 Veillardd1640922001-12-17 15:30:10 +00007206 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00007207 */
7208int
7209xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00007210 if (buf == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007211 if (len == 0) return(0);
7212 if (len > buf->use) return(-1);
7213
7214 buf->use -= len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007215 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 Veillard53350552003-09-18 13:35:51 +00007221 buf->content += len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007222 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 Veillard53350552003-09-18 13:35:51 +00007237 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007238 memmove(buf->content, &buf->content[len], buf->use);
Daniel Veillard53350552003-09-18 13:35:51 +00007239 buf->content[buf->use] = 0;
7240 }
Owen Taylor3473f882001-02-23 17:55:21 +00007241 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 */
7253int
7254xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7255 int size;
7256 xmlChar *newbuf;
7257
Daniel Veillard3d97e662004-11-04 10:49:00 +00007258 if (buf == NULL) return(-1);
7259
Daniel Veillard53350552003-09-18 13:35:51 +00007260 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00007261 if (len + buf->use < buf->size) return(0);
7262
Daniel Veillardee20cd72009-08-22 15:18:31 +02007263 /*
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. Brack30fe43f2004-07-26 18:00:58 +00007270 if (buf->size > len)
7271 size = buf->size * 2;
7272 else
7273 size = buf->use + len + 100;
7274#else
Owen Taylor3473f882001-02-23 17:55:21 +00007275 size = buf->use + len + 100;
William M. Brack30fe43f2004-07-26 18:00:58 +00007276#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007277
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007278 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 Veillard18ec16e2003-10-07 23:16:40 +00007295 }
Owen Taylor3473f882001-02-23 17:55:21 +00007296 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 Veillardd1640922001-12-17 15:30:10 +00007306 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00007307 */
7308int
7309xmlBufferDump(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 Veillardcd337f02001-11-22 18:20:37 +00007326 if (file == NULL)
7327 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00007328 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7329 return(ret);
7330}
7331
7332/**
7333 * xmlBufferContent:
7334 * @buf: the buffer
7335 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007336 * Function to extract the content of a buffer
7337 *
Owen Taylor3473f882001-02-23 17:55:21 +00007338 * Returns the internal content
7339 */
7340
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007341const xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08007342xmlBufferContent(const xmlBuffer *buf)
Owen Taylor3473f882001-02-23 17:55:21 +00007343{
7344 if(!buf)
7345 return NULL;
7346
7347 return buf->content;
7348}
7349
7350/**
7351 * xmlBufferLength:
Daniel Veillardaa6de472008-08-25 14:53:31 +00007352 * @buf: the buffer
Owen Taylor3473f882001-02-23 17:55:21 +00007353 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007354 * Function to get the length of a buffer
7355 *
Owen Taylor3473f882001-02-23 17:55:21 +00007356 * Returns the length of data in the internal content
7357 */
7358
7359int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08007360xmlBufferLength(const xmlBuffer *buf)
Owen Taylor3473f882001-02-23 17:55:21 +00007361{
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 Veillardd1640922001-12-17 15:30:10 +00007373 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00007374 *
7375 * Returns 0 in case of problems, 1 otherwise
7376 */
7377int
7378xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7379{
7380 unsigned int newSize;
7381 xmlChar* rebuf = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007382 size_t start_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00007383
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007384 if (buf == NULL)
7385 return(0);
7386
Daniel Veillard53350552003-09-18 13:35:51 +00007387 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7388
Owen Taylor3473f882001-02-23 17:55:21 +00007389 /* 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 Veillarde83e93e2008-08-30 12:52:26 +00007395 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 Veillard1dc9feb2008-11-17 15:59:21 +00007399 while (size > newSize) {
7400 if (newSize > UINT_MAX / 2) {
7401 xmlTreeErrMemory("growing buffer");
7402 return 0;
7403 }
7404 newSize *= 2;
7405 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007406 break;
7407 case XML_BUFFER_ALLOC_EXACT:
7408 newSize = size+10;
7409 break;
Conrad Irwin7d0d2a52012-05-14 14:18:58 +08007410 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 Veillarde83e93e2008-08-30 12:52:26 +00007425 default:
7426 newSize = size+10;
7427 break;
Owen Taylor3473f882001-02-23 17:55:21 +00007428 }
7429
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007430 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 Veillard6155d8a2003-08-19 15:01:28 +00007447 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007448 } 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 Veillard6155d8a2003-08-19 15:01:28 +00007471 }
Owen Taylor3473f882001-02-23 17:55:21 +00007472 buf->size = newSize;
7473
7474 return 1;
7475}
7476
7477/**
7478 * xmlBufferAdd:
7479 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00007480 * @str: the #xmlChar string
7481 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007482 *
Daniel Veillard60087f32001-10-10 09:45:09 +00007483 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00007484 * str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007485 *
7486 * Returns 0 successful, a positive error code number otherwise
7487 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007488 */
William M. Bracka3215c72004-07-31 16:24:01 +00007489int
Owen Taylor3473f882001-02-23 17:55:21 +00007490xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7491 unsigned int needSize;
7492
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007493 if ((str == NULL) || (buf == NULL)) {
William M. Bracka3215c72004-07-31 16:24:01 +00007494 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007495 }
William M. Bracka3215c72004-07-31 16:24:01 +00007496 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007497 if (len < -1) {
7498#ifdef DEBUG_BUFFER
7499 xmlGenericError(xmlGenericErrorContext,
7500 "xmlBufferAdd: len < 0\n");
7501#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007502 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007503 }
William M. Bracka3215c72004-07-31 16:24:01 +00007504 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007505
7506 if (len < 0)
7507 len = xmlStrlen(str);
7508
Daniel Veillardc9923322007-04-24 18:12:06 +00007509 if (len < 0) return -1;
7510 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007511
7512 needSize = buf->use + len + 2;
7513 if (needSize > buf->size){
7514 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007515 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007516 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007517 }
7518 }
7519
7520 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7521 buf->use += len;
7522 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007523 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007524}
7525
7526/**
7527 * xmlBufferAddHead:
7528 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00007529 * @str: the #xmlChar string
7530 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007531 *
7532 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00007533 * if len == -1, the length of @str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007534 *
7535 * Returns 0 successful, a positive error code number otherwise
7536 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007537 */
William M. Bracka3215c72004-07-31 16:24:01 +00007538int
Owen Taylor3473f882001-02-23 17:55:21 +00007539xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7540 unsigned int needSize;
7541
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007542 if (buf == NULL)
7543 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007544 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007545 if (str == NULL) {
7546#ifdef DEBUG_BUFFER
7547 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007548 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007549#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007550 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007551 }
7552 if (len < -1) {
7553#ifdef DEBUG_BUFFER
7554 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007555 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007556#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007557 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007558 }
William M. Bracka3215c72004-07-31 16:24:01 +00007559 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007560
7561 if (len < 0)
7562 len = xmlStrlen(str);
7563
William M. Bracka3215c72004-07-31 16:24:01 +00007564 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007565
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007566 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 Taylor3473f882001-02-23 17:55:21 +00007580 needSize = buf->use + len + 2;
7581 if (needSize > buf->size){
7582 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007583 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007584 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007585 }
7586 }
7587
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007588 memmove(&buf->content[len], &buf->content[0], buf->use);
7589 memmove(&buf->content[0], str, len);
Owen Taylor3473f882001-02-23 17:55:21 +00007590 buf->use += len;
7591 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007592 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007593}
7594
7595/**
7596 * xmlBufferCat:
William M. Bracka3215c72004-07-31 16:24:01 +00007597 * @buf: the buffer to add to
Daniel Veillardd1640922001-12-17 15:30:10 +00007598 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00007599 *
7600 * Append a zero terminated string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007601 *
7602 * Returns 0 successful, a positive error code number otherwise
7603 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007604 */
William M. Bracka3215c72004-07-31 16:24:01 +00007605int
Owen Taylor3473f882001-02-23 17:55:21 +00007606xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007607 if (buf == NULL)
7608 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007609 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7610 if (str == NULL) return -1;
7611 return xmlBufferAdd(buf, str, -1);
Owen Taylor3473f882001-02-23 17:55:21 +00007612}
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. Bracka3215c72004-07-31 16:24:01 +00007620 *
7621 * Returns 0 successful, a positive error code number otherwise
7622 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007623 */
William M. Bracka3215c72004-07-31 16:24:01 +00007624int
Owen Taylor3473f882001-02-23 17:55:21 +00007625xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7626 const char *cur;
7627
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007628 if (buf == NULL)
7629 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007630 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007631 if (str == NULL) {
7632#ifdef DEBUG_BUFFER
7633 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007634 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007635#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007636 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007637 }
7638 for (cur = str;*cur != 0;cur++) {
7639 if (buf->use + 10 >= buf->size) {
7640 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007641 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007642 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007643 }
7644 }
7645 buf->content[buf->use++] = *cur;
7646 }
7647 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007648 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007649}
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 */
7659void
Daniel Veillard53350552003-09-18 13:35:51 +00007660xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007661 if (buf == NULL)
7662 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007663 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007664 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 */
7675void
7676xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007677 if (buf == NULL)
7678 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007679 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007680 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 Veillardd1640922001-12-17 15:30:10 +00007690 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00007691 * quote or double-quotes internally
7692 */
7693void
7694xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00007695 const xmlChar *cur, *base;
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007696 if (buf == NULL)
7697 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007698 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00007699 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00007700 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007701#ifdef DEBUG_BUFFER
7702 xmlGenericError(xmlGenericErrorContext,
7703 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7704#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00007705 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 "&quot;", 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 Taylor3473f882001-02-23 17:55:21 +00007722 }
Daniel Veillard39057f42003-08-04 01:33:43 +00007723 else{
7724 xmlBufferCCat(buf, "\'");
7725 xmlBufferCat(buf, string);
7726 xmlBufferCCat(buf, "\'");
7727 }
Owen Taylor3473f882001-02-23 17:55:21 +00007728 } else {
7729 xmlBufferCCat(buf, "\"");
7730 xmlBufferCat(buf, string);
7731 xmlBufferCCat(buf, "\"");
7732 }
7733}
7734
7735
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007736/**
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 */
7743int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08007744xmlGetDocCompressMode (const xmlDoc *doc) {
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007745 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 */
7757void
7758xmlSetDocCompressMode (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 */
7771int
7772xmlGetCompressMode(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 */
7784void
7785xmlSetCompressMode(int mode) {
7786 if (mode < 0) xmlCompressMode = 0;
7787 else if (mode > 9) xmlCompressMode = 9;
7788 else xmlCompressMode = mode;
7789}
7790
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007791#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
7796typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7797struct 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. Buchcike01b2fd2006-02-01 16:36:13 +00007807 * -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. Buchcikbc0e3c62005-06-27 10:28:23 +00007810 */
7811 int depth;
7812};
7813
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007814typedef struct xmlNsMap *xmlNsMapPtr;
7815struct 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. Buchcikbc0e3c62005-06-27 10:28:23 +00007833/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007834* xmlDOMWrapNsMapFree:
7835* @map: the ns-map
Daniel Veillardaa6de472008-08-25 14:53:31 +00007836*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007837* Frees the ns-map
7838*/
7839static void
7840xmlDOMWrapNsMapFree(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. Buchcikbc0e3c62005-06-27 10:28:23 +00007863* @map: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007864* @oldNs: the old ns-struct
7865* @newNs: the new ns-struct
7866* @depth: depth and ns-kind information
Daniel Veillardaa6de472008-08-25 14:53:31 +00007867*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007868* Adds an ns-mapping item.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007869*/
7870static xmlNsMapItemPtr
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007871xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007872 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007873{
7874 xmlNsMapItemPtr ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007875 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. Buchcikbc0e3c62005-06-27 10:28:23 +00007884 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007885 * Create the ns-map.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007886 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007887 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 Veillardaa6de472008-08-25 14:53:31 +00007895
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007896 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. Buchcikbc0e3c62005-06-27 10:28:23 +00007903 } else {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007904 /*
7905 * Create a new item.
7906 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007907 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. Buchcikbc0e3c62005-06-27 10:28:23 +00007913 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00007914
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007915 if (map->first == NULL) {
7916 /*
7917 * First ever.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007918 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007919 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 Veillardaa6de472008-08-25 14:53:31 +00007927 map->last = ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007928 } else if (position == 0) {
7929 /*
7930 * Set on first position.
7931 */
7932 map->first->prev = ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00007933 ret->next = map->first;
7934 map->first = ret;
Gaurav Gupta6d93e9e2014-10-06 20:20:00 +08007935 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007936
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007937 ret->oldNs = oldNs;
7938 ret->newNs = newNs;
7939 ret->shadowDepth = -1;
7940 ret->depth = depth;
7941 return (ret);
7942}
7943
7944/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007945* xmlDOMWrapStoreNs:
7946* @doc: the doc
7947* @nsName: the namespace name
7948* @prefix: the prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00007949*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007950* Creates or reuses an xmlNs struct on doc->oldNs with
7951* the given prefix and namespace name.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007952*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007953* Returns the aquired ns struct or NULL in case of an API
7954* or internal error.
7955*/
7956static xmlNsPtr
7957xmlDOMWrapStoreNs(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 Veillard76d36452009-09-07 11:19:33 +02007983 if (ns != NULL) {
7984 ns->next = xmlNewNs(NULL, nsName, prefix);
7985 return (ns->next);
7986 }
7987 return(NULL);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007988}
7989
7990/*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007991* xmlDOMWrapNewCtxt:
7992*
7993* Allocates and initializes a new DOM-wrapper context.
7994*
Jan Pokorný75801652013-12-19 15:09:14 +01007995* Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007996*/
7997xmlDOMWrapCtxtPtr
7998xmlDOMWrapNewCtxt(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*/
8017void
8018xmlDOMWrapFreeCtxt(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. Buchcikbc0e3c62005-06-27 10:28:23 +00008031* xmlTreeLookupNsListByPrefix:
8032* @nsList: a list of ns-structs
8033* @prefix: the searched prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00008034*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008035* Searches for a ns-decl with the given prefix in @nsList.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008036*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008037* Returns the ns-decl if found, NULL if not found and on
8038* API errors.
8039*/
8040static xmlNsPtr
8041xmlTreeNSListLookupByPrefix(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. Buchcike01b2fd2006-02-01 16:36:13 +00008061* xmlDOMWrapNSNormGatherInScopeNs:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008062* @map: the namespace map
8063* @node: the node to start with
Daniel Veillardaa6de472008-08-25 14:53:31 +00008064*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008065* Puts in-scope namespaces into the ns-map.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008066*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008067* Returns 0 on success, -1 on API or internal errors.
8068*/
8069static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008070xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008071 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 Veillard3e62adb2012-08-09 14:24:02 +08008080 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8081 return (-1);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008082 /*
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. Buchcik44353412006-03-06 13:26:16 +00008092 if (XML_NSMAP_NOTEMPTY(*map)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008093 /*
8094 * Skip shadowed prefixes.
8095 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008096 XML_NSMAP_FOREACH(*map, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008097 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. Buchcik44353412006-03-06 13:26:16 +00008107 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008108 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 Veillard7e21fd12005-07-03 21:44:07 +00008129 const xmlChar *old = str; \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008130 str = xmlDictLookup(destDoc->dict, str, -1); \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008131 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
8132 (!xmlDictOwns(sourceDoc->dict, old))) \
Daniel Veillard39e5c892005-07-03 22:48:50 +00008133 xmlFree((char *)old); \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008134 } 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 Veillardaa6de472008-08-25 14:53:31 +00008160* Returns 0 on success, -1 on internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008161*/
8162static int
8163xmlDOMWrapNSNormAddNsMapItem2(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 Veillard304e78c2005-07-03 16:19:41 +00008191* @ctxt: a DOM wrapper context
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008192* @doc: the doc
8193* @node: the node to be removed.
Daniel Veillard304e78c2005-07-03 16:19:41 +00008194* @options: set of options, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008195*
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. Buchcik978039b2006-06-16 19:46:26 +00008200*
8201* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008202*
8203* Returns 0 on success, 1 if the node is not supported,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008204* -1 on API and internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008205*/
8206int
8207xmlDOMWrapRemoveNode(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 Veillardaa6de472008-08-25 14:53:31 +00008221 switch (node->type) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008222 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 Veillardaa6de472008-08-25 14:53:31 +00008229 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008230 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 Veillardaa6de472008-08-25 14:53:31 +00008253 if (node->ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008254 /*
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 Veillardaa6de472008-08-25 14:53:31 +00008297 }
8298next_node:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008299 if ((node->type == XML_ELEMENT_NODE) &&
8300 (node->children != NULL)) {
8301 node = node->children;
8302 continue;
8303 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008304next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008305 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
8319internal_error:
8320 if (list != NULL)
8321 xmlFree(list);
8322 return (-1);
8323}
8324
8325/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008326* xmlSearchNsByNamespaceStrict:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008327* @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*/
8339static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008340xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8341 const xmlChar* nsName,
8342 xmlNsPtr *retNs, int prefixed)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008343{
8344 xmlNodePtr cur, prev = NULL, out = NULL;
8345 xmlNsPtr ns, prevns;
8346
8347 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8348 return (-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008349 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8350 return(-1);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008351
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 Veillardaa6de472008-08-25 14:53:31 +00008399
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008400 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. Buchcik44353412006-03-06 13:26:16 +00008420 } 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. Buchcik44353412006-03-06 13:26:16 +00008434*
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*/
8441static int
8442xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8443 const xmlChar* prefix,
8444 xmlNsPtr *retNs)
8445{
8446 xmlNodePtr cur;
8447 xmlNsPtr ns;
8448
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008449 if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
8450 return(-1);
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008451
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 Veillardaa6de472008-08-25 14:53:31 +00008467 do {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008468 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 Veillardaa6de472008-08-25 14:53:31 +00008481 } while (ns != NULL);
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008482 }
8483 } else if ((cur->type == XML_ENTITY_NODE) ||
8484 (cur->type == XML_ENTITY_DECL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008485 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*/
8506static xmlNsPtr
8507xmlDOMWrapNSNormDeclareNsForced(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 Veillard3e62adb2012-08-09 14:24:02 +08008518
8519 if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
8520 return(NULL);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008521 /*
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. Buchcik44353412006-03-06 13:26:16 +00008537 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008538 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);
8552ns_next_prefix:
8553 counter++;
8554 if (counter > 1000)
8555 return (NULL);
8556 if (prefix == NULL) {
8557 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008558 "ns_%d", counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008559 } else
8560 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008561 "%.30s_%d", (char *)prefix, counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008562 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. Buchcikbc0e3c62005-06-27 10:28:23 +00008573* @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*/
8585static int
8586xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8587 xmlNodePtr elem,
8588 xmlNsPtr ns,
8589 xmlNsPtr *retNs,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008590 xmlNsMapPtr *nsMap,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008591
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008592 int depth,
8593 int ancestorsOnly,
8594 int prefixed)
8595{
Daniel Veillardaa6de472008-08-25 14:53:31 +00008596 xmlNsMapItemPtr mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008597
8598 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008599 (nsMap == NULL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008600 return (-1);
8601
8602 *retNs = NULL;
8603 /*
8604 * Handle XML namespace.
8605 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008606 if (IS_STR_XML(ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008607 /*
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. Buchcik978039b2006-06-16 19:46:26 +00008619 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8620 (! (ancestorsOnly && (elem == NULL))))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008621 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008622 /*
8623 * Try to find an equal ns-name in in-scope ns-decls.
8624 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008625 XML_NSMAP_FOREACH(*nsMap, mi) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008626 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8627 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008628 * 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. Buchcikbc0e3c62005-06-27 10:28:23 +00008632 */
8633 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8634 /* Skip shadowed prefixes. */
Daniel Veillardaa6de472008-08-25 14:53:31 +00008635 (mi->shadowDepth == -1) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008636 /* Skip xmlns="" or xmlns:foo="". */
8637 ((mi->newNs->href != NULL) &&
Daniel Veillardaa6de472008-08-25 14:53:31 +00008638 (mi->newNs->href[0] != 0)) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008639 /* 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 Veillardaa6de472008-08-25 14:53:31 +00008665 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008666 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008667 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. Buchcik44353412006-03-06 13:26:16 +00008683 */
8684 XML_NSMAP_FOREACH(*nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008685 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. Buchcik44353412006-03-06 13:26:16 +00008697 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008698 xmlFreeNs(tmpns);
8699 return (-1);
8700 }
8701 *retNs = tmpns;
8702 }
8703 return (0);
8704}
8705
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008706typedef enum {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008707 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008708} xmlDOMReconcileNSOptions;
8709
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008710/*
8711* xmlDOMWrapReconcileNamespaces:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008712* @ctxt: DOM wrapper context, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008713* @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. Buchcik978039b2006-06-16 19:46:26 +00008721*
8722* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008723*
8724* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008725*/
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008726
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008727int
8728xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8729 xmlNodePtr elem,
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008730 int options)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008731{
8732 int depth = -1, adoptns = 0, parnsdone = 0;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008733 xmlNsPtr ns, prevns;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008734 xmlDocPtr doc;
8735 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008736 xmlNsMapPtr nsMap = NULL;
8737 xmlNsMapItemPtr /* topmi = NULL, */ mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008738 /* @ancestorsOnly should be set by an option flag. */
8739 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008740 int optRemoveRedundantNS =
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008741 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8742 xmlNsPtr *listRedund = NULL;
8743 int sizeRedund = 0, nbRedund = 0, ret, i, j;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008744
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. Buchcike01b2fd2006-02-01 16:36:13 +00008761 prevns = NULL;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008762 ns = cur->nsDef;
8763 while (ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008764 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. Buchcikbc0e3c62005-06-27 10:28:23 +00008773 }
8774 parnsdone = 1;
8775 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008776
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008777 /*
8778 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8779 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008780 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008781 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008782 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 Veillardaa6de472008-08-25 14:53:31 +00008788 {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008789 /*
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 Veillardaa6de472008-08-25 14:53:31 +00008798 */
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008799 if (prevns)
8800 prevns->next = ns->next;
8801 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00008802 cur->nsDef = ns->next;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008803 goto next_ns_decl;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008804 }
8805 }
8806 }
8807
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008808 /*
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 Veillardaa6de472008-08-25 14:53:31 +00008813 adoptns = 0;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008814 /*
8815 * Does it shadow any ns-decl?
8816 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008817 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8818 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008819 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 Veillardaa6de472008-08-25 14:53:31 +00008823
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008824 mi->shadowDepth = depth;
8825 }
8826 }
8827 }
8828 /*
8829 * Push mapping.
8830 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008831 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008832 depth) == NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +00008833 goto internal_error;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008834
8835 prevns = ns;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008836next_ns_decl:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008837 ns = ns->next;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008838 }
8839 }
8840 if (! adoptns)
8841 goto ns_end;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008842 /* No break on purpose. */
8843 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008844 /* No ns, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008845 if (cur->ns == NULL)
8846 goto ns_end;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008847
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008848 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. Buchcikbc0e3c62005-06-27 10:28:23 +00008854 }
8855 parnsdone = 1;
8856 }
8857 /*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008858 * 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 Veillardaa6de472008-08-25 14:53:31 +00008866 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008867 }
8868 /*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008869 * Adopt ns-references.
8870 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008871 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008872 /*
8873 * Search for a mapping.
8874 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008875 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008876 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. Buchcik44353412006-03-06 13:26:16 +00008889 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008890 ancestorsOnly,
8891 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8892 goto internal_error;
8893 cur->ns = ns;
8894
8895ns_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 Veillardaa6de472008-08-25 14:53:31 +00008904 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008905 default:
8906 goto next_sibling;
8907 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008908into_content:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008909 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 Veillardaa6de472008-08-25 14:53:31 +00008917next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008918 if (cur == elem)
8919 break;
8920 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008921 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008922 /*
8923 * Pop mappings.
8924 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008925 while ((nsMap->last != NULL) &&
8926 (nsMap->last->depth >= depth))
8927 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008928 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008929 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008930 /*
8931 * Unshadow.
8932 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008933 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008934 if (mi->shadowDepth >= depth)
8935 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008936 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008937 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008938 depth--;
8939 }
8940 if (cur->next != NULL)
8941 cur = cur->next;
8942 else {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008943 if (cur->type == XML_ATTRIBUTE_NODE) {
8944 cur = cur->parent;
8945 goto into_content;
8946 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008947 cur = cur->parent;
8948 goto next_sibling;
8949 }
8950 } while (cur != NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008951
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008952 ret = 0;
8953 goto exit;
8954internal_error:
8955 ret = -1;
8956exit:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008957 if (listRedund) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008958 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8959 xmlFreeNs(listRedund[j]);
8960 }
8961 xmlFree(listRedund);
8962 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008963 if (nsMap != NULL)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008964 xmlDOMWrapNsMapFree(nsMap);
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008965 return (ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008966}
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. Buchcike01b2fd2006-02-01 16:36:13 +00008974* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008975* @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. Buchcik978039b2006-06-16 19:46:26 +00008986* NOTE: This function was not intensively tested.
8987*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008988* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8989*/
8990static int
8991xmlDOMWrapAdoptBranch(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. Buchcik44353412006-03-06 13:26:16 +00009000 xmlNsMapPtr nsMap = NULL;
9001 xmlNsMapItemPtr mi;
Daniel Veillard11ce4002006-03-10 00:36:23 +00009002 xmlNsPtr ns = NULL;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009003 int depth = -1, adoptStr = 1;
9004 /* gather @parent's ns-decls. */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009005 int parnsdone;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009006 /* @ancestorsOnly should be set per option. */
9007 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009008
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009009 /*
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. Buchcik978039b2006-06-16 19:46:26 +00009018 /*
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. Buchcikbc0e3c62005-06-27 10:28:23 +00009036 cur = node;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08009037 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9038 goto internal_error;
9039
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009040 while (cur != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009041 /*
9042 * Paranoid source-doc sanity check.
9043 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009044 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 Veillardaa6de472008-08-25 14:53:31 +00009065 case XML_XINCLUDE_START:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009066 case XML_XINCLUDE_END:
9067 /*
9068 * TODO
9069 */
9070 return (-1);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009071 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009072 curElem = cur;
9073 depth++;
9074 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00009075 * Namespace declarations.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009076 * - 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. Buchcikbc0e3c62005-06-27 10:28:23 +00009081 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009082 if ((cur->nsDef) &&
9083 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
9084 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009085 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009086 /*
9087 * Gather @parent's in-scope ns-decls.
9088 */
9089 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9090 destParent) == -1)
9091 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009092 parnsdone = 1;
9093 }
9094 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9095 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009096 * NOTE: ns->prefix and ns->href are never in the dict.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009097 * XML_TREE_ADOPT_STR(ns->prefix)
9098 * XML_TREE_ADOPT_STR(ns->href)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009099 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009100 /*
9101 * Does it shadow any ns-decl?
Daniel Veillardaa6de472008-08-25 14:53:31 +00009102 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009103 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009104 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009105 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 Veillardaa6de472008-08-25 14:53:31 +00009110
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009111 mi->shadowDepth = depth;
9112 }
9113 }
9114 }
9115 /*
9116 * Push mapping.
9117 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009118 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009119 ns, ns, depth) == NULL)
9120 goto internal_error;
9121 }
9122 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009123 /* No break on purpose. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009124 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009125 /* No namespace, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009126 if (cur->ns == NULL)
9127 goto ns_end;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009128
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009129 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009130 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9131 destParent) == -1)
9132 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009133 parnsdone = 1;
9134 }
9135 /*
9136 * Adopt ns-references.
9137 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009138 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009139 /*
9140 * Search for a mapping.
9141 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009142 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009143 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. Buchcik978039b2006-06-16 19:46:26 +00009152 * No matching namespace in scope. We need a new one.
9153 */
9154 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009155 /*
9156 * User-defined behaviour.
9157 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009158 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9159 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009160 /*
9161 * Insert mapping if ns is available; it's the users fault
9162 * if not.
9163 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009164 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009165 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009166 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 Veillardaa6de472008-08-25 14:53:31 +00009173 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009174 destParent ? curElem : NULL,
9175 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009176 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009177 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 }
9183ns_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 Veillardaa6de472008-08-25 14:53:31 +00009209 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009210 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009211 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009212 ((xmlAttrPtr) cur)->atype = 0;
9213 ((xmlAttrPtr) cur)->psvi = NULL;
9214 }
9215 break;
9216 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009217 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009218 /*
9219 * This puts the content in the dest dict, only if
9220 * it was previously in the source dict.
9221 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009222 XML_TREE_ADOPT_STR_2(cur->content)
9223 goto leave_node;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009224 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 Veillardaa6de472008-08-25 14:53:31 +00009248 case XML_COMMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009249 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
9261leave_node:
9262 if (cur == node)
9263 break;
9264 if ((cur->type == XML_ELEMENT_NODE) ||
9265 (cur->type == XML_XINCLUDE_START) ||
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009266 (cur->type == XML_XINCLUDE_END))
9267 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009268 /*
9269 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9270 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009271 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009272 /*
9273 * Pop mappings.
9274 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009275 while ((nsMap->last != NULL) &&
9276 (nsMap->last->depth >= depth))
9277 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009278 XML_NSMAP_POP(nsMap, mi)
9279 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009280 /*
9281 * Unshadow.
9282 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009283 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009284 if (mi->shadowDepth >= depth)
9285 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009286 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009287 }
9288 depth--;
9289 }
9290 if (cur->next != NULL)
9291 cur = cur->next;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009292 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9293 (cur->parent->children != NULL))
9294 {
9295 cur = cur->parent->children;
9296 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009297 cur = cur->parent;
9298 goto leave_node;
9299 }
9300 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009301
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009302 goto exit;
9303
Daniel Veillardaa6de472008-08-25 14:53:31 +00009304internal_error:
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009305 ret = -1;
9306
9307exit:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009308 /*
9309 * Cleanup.
9310 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009311 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 Veillardaa6de472008-08-25 14:53:31 +00009322 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009323 xmlDOMWrapNsMapFree(nsMap);
9324 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009325 return(ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009326}
9327
9328/*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009329* 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 Veillardb2f8f1d2006-04-28 16:30:48 +00009336* @deep: descend into child if set
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009337* @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. Buchcik978039b2006-06-16 19:46:26 +00009341* 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 Veillardaa6de472008-08-25 14:53:31 +00009344*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009345* 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. Buchcik978039b2006-06-16 19:46:26 +00009351* 1) What to do with XInclude? Currently this returns an error for XInclude.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009352*
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
9358int
9359xmlDOMWrapCloneNode(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. Buchcik44353412006-03-06 13:26:16 +00009370 xmlNsMapPtr nsMap = NULL;
9371 xmlNsMapItemPtr mi;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009372 xmlNsPtr ns;
9373 int depth = -1;
9374 /* int adoptStr = 1; */
9375 /* gather @parent's ns-decls. */
9376 int parnsdone = 0;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009377 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00009378 * @ancestorsOnly:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009379 * TODO: @ancestorsOnly should be set per option.
9380 *
9381 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009382 int ancestorsOnly = 0;
9383 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009384 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9385 xmlDictPtr dict; /* The destination dict */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009386
Daniel Veillard11ce4002006-03-10 00:36:23 +00009387 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009388 return(-1);
9389 /*
9390 * TODO: Initially we support only element-nodes.
9391 */
9392 if (node->type != XML_ELEMENT_NODE)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009393 return(1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009394 /*
9395 * Check node->doc sanity.
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009396 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009397 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 Veillardaa6de472008-08-25 14:53:31 +00009405 sourceDoc = node->doc;
Daniel Veillard11ce4002006-03-10 00:36:23 +00009406 if (sourceDoc == NULL)
9407 return (-1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009408
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009409 dict = destDoc->dict;
9410 /*
9411 * Reuse the namespace map of the context.
9412 */
9413 if (ctxt)
9414 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9415
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009416 *resNode = NULL;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009417
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009418 cur = node;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08009419 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9420 return(-1);
9421
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009422 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 Veillardaa6de472008-08-25 14:53:31 +00009429 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009430 }
9431 /*
9432 * Create a new node.
9433 */
9434 switch (cur->type) {
9435 case XML_XINCLUDE_START:
9436 case XML_XINCLUDE_END:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009437 /*
9438 * TODO: What to do with XInclude?
9439 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009440 goto internal_error;
9441 break;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009442 case XML_ELEMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009443 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009444 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009445 case XML_COMMENT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009446 case XML_PI_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009447 case XML_DOCUMENT_FRAG_NODE:
9448 case XML_ENTITY_REF_NODE:
9449 case XML_ENTITY_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009450 /*
9451 * Nodes of xmlNode structure.
9452 */
9453 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9454 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009455 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009456 goto internal_error;
9457 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009458 memset(clone, 0, sizeof(xmlNode));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009459 /*
9460 * Set hierachical links.
9461 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009462 if (resultClone != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009463 clone->parent = parentClone;
9464 if (prevClone) {
9465 prevClone->next = clone;
9466 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009467 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009468 parentClone->children = clone;
9469 } else
9470 resultClone = clone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009471
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009472 break;
9473 case XML_ATTRIBUTE_NODE:
9474 /*
9475 * Attributes (xmlAttr).
9476 */
9477 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9478 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009479 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009480 goto internal_error;
9481 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009482 memset(clone, 0, sizeof(xmlAttr));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009483 /*
9484 * Set hierachical links.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009485 * TODO: Change this to add to the end of attributes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009486 */
9487 if (resultClone != NULL) {
9488 clone->parent = parentClone;
9489 if (prevClone) {
9490 prevClone->next = clone;
9491 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009492 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009493 parentClone->properties = (xmlAttrPtr) clone;
9494 } else
9495 resultClone = clone;
9496 break;
9497 default:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009498 /*
9499 * TODO QUESTION: Any other nodes expected?
9500 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009501 goto internal_error;
9502 }
9503
9504 clone->type = cur->type;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009505 clone->doc = destDoc;
9506
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009507 /*
9508 * Clone the name of the node if any.
9509 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009510 if (cur->name == xmlStringText)
9511 clone->name = xmlStringText;
9512 else if (cur->name == xmlStringTextNoenc)
9513 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009514 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9515 * in tree.c, it might be set in Libxslt via
Daniel Veillardaa6de472008-08-25 14:53:31 +00009516 * "xsl:disable-output-escaping".
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009517 */
9518 clone->name = xmlStringTextNoenc;
9519 else if (cur->name == xmlStringComment)
9520 clone->name = xmlStringComment;
9521 else if (cur->name != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009522 DICT_CONST_COPY(cur->name, clone->name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009523 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009524
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. Buchcik44353412006-03-06 13:26:16 +00009538 if (cur->nsDef != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009539 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 Veillardaa6de472008-08-25 14:53:31 +00009546 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009547 }
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. Buchcik978039b2006-06-16 19:46:26 +00009560 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009561 "allocating namespace");
9562 return(-1);
9563 }
9564 memset(cloneNs, 0, sizeof(xmlNs));
9565 cloneNs->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009566
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009567 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. Buchcik978039b2006-06-16 19:46:26 +00009575 /*
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. Buchcik44353412006-03-06 13:26:16 +00009583 /*
9584 * Does it shadow any ns-decl?
9585 */
9586 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009587 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009588 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9589 (mi->shadowDepth == -1) &&
9590 ((ns->prefix == mi->newNs->prefix) ||
9591 xmlStrEqual(ns->prefix,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009592 mi->newNs->prefix))) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009593 /*
9594 * Mark as shadowed at the current
9595 * depth.
9596 */
9597 mi->shadowDepth = depth;
9598 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009599 }
9600 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009601 /*
9602 * Push mapping.
9603 */
9604 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9605 ns, cloneNs, depth) == NULL)
9606 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009607 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009608 }
9609 }
9610 /* cur->ns will be processed further down. */
9611 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009612 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009613 /* 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. Buchcik978039b2006-06-16 19:46:26 +00009618 /*
9619 * Note that this will also cover the values of attributes.
9620 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009621 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009622 goto leave_node;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009623 case XML_ENTITY_NODE:
9624 /* TODO: What to do here? */
9625 goto leave_node;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009626 case XML_ENTITY_REF_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009627 if (sourceDoc != destDoc) {
9628 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9629 xmlEntityPtr ent;
9630 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009631 * Different doc: Assign new entity-node if available.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009632 */
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. Buchcik44353412006-03-06 13:26:16 +00009642 * Same doc: Use the current node's entity declaration
9643 * and value.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009644 */
9645 clone->content = cur->content;
9646 clone->children = cur->children;
9647 clone->last = cur->last;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009648 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009649 goto leave_node;
9650 case XML_PI_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009651 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009652 goto leave_node;
9653 case XML_COMMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009654 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009655 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 Veillardaa6de472008-08-25 14:53:31 +00009666 ** and is intended only for element- and attribute-nodes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009667 **
9668 */
9669 if (! parnsdone) {
9670 if (destParent && (ctxt == NULL)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009671 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009672 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009673 }
9674 parnsdone = 1;
9675 }
9676 /*
9677 * Adopt ns-references.
9678 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009679 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009680 /*
9681 * Search for a mapping.
9682 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009683 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009684 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. Buchcik978039b2006-06-16 19:46:26 +00009695 * No matching namespace in scope. We need a new one.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009696 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009697 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009698 /*
9699 * User-defined behaviour.
9700 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009701 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9702 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009703 /*
9704 * Add user's mapping.
9705 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009706 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009707 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 Veillardaa6de472008-08-25 14:53:31 +00009715 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009716 destParent ? curElem : NULL,
9717 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009718 &nsMap, depth,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009719 /* if we need to search only in the ancestor-axis */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009720 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
9727end_ns_reference:
9728
9729 /*
9730 * Some post-processing.
9731 *
9732 * Handle ID attributes.
9733 */
9734 if ((clone->type == XML_ATTRIBUTE_NODE) &&
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009735 (clone->parent != NULL))
9736 {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009737 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009738
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009739 xmlChar *idVal;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009740
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009741 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. Buchcik978039b2006-06-16 19:46:26 +00009754 ** The following will traverse the tree **************************
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009755 **
Daniel Veillardaa6de472008-08-25 14:53:31 +00009756 *
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009757 * 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 Veillardaa6de472008-08-25 14:53:31 +00009761 parentClone = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009762 cur = (xmlNodePtr) cur->properties;
9763 continue;
9764 }
9765into_content:
9766 /*
9767 * Descend into child-nodes.
9768 */
9769 if (cur->children != NULL) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009770 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9771 prevClone = NULL;
9772 parentClone = clone;
9773 cur = cur->children;
9774 continue;
9775 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009776 }
9777
9778leave_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 Veillardaa6de472008-08-25 14:53:31 +00009791 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009792 /*
9793 * Pop mappings.
9794 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009795 while ((nsMap->last != NULL) &&
9796 (nsMap->last->depth >= depth))
9797 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009798 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009799 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009800 /*
9801 * Unshadow.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009802 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009803 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009804 if (mi->shadowDepth >= depth)
9805 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009806 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009807 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009808 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 Veillard11ce4002006-03-10 00:36:23 +00009817 if (clone->parent != NULL)
9818 clone->parent->last = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009819 clone = clone->parent;
Daniel Veillard75d13092013-09-11 15:11:27 +08009820 if (clone != NULL)
9821 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009822 /*
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 Veillardaa6de472008-08-25 14:53:31 +00009830 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009831 /*
9832 * Process parent-element --> children.
9833 */
9834 cur = cur->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009835 goto into_content;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009836 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009837 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009838 goto exit;
9839
9840internal_error:
9841 ret = -1;
9842
9843exit:
9844 /*
9845 * Cleanup.
9846 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009847 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 Veillardaa6de472008-08-25 14:53:31 +00009858 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009859 xmlDOMWrapNsMapFree(nsMap);
9860 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009861 /*
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. Buchcikbc0e3c62005-06-27 10:28:23 +00009870* 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*/
9885static int
9886xmlDOMWrapAdoptAttr(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 Veillardaa6de472008-08-25 14:53:31 +00009898
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009899 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. Buchcik44353412006-03-06 13:26:16 +00009907 if (IS_STR_XML(attr->ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009908 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. Buchcik44353412006-03-06 13:26:16 +00009918 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009919 &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 Veillardaa6de472008-08-25 14:53:31 +00009925 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009926 if (ns == NULL)
9927 goto internal_error;
9928 attr->ns = ns;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009929 }
9930
9931 XML_TREE_ADOPT_STR(attr->name);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009932 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 Veillard3e62adb2012-08-09 14:24:02 +08009940 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9941 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009942 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 Veillardaa6de472008-08-25 14:53:31 +00009948 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009949 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 Veillardaa6de472008-08-25 14:53:31 +00009966 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009967 }
9968 break;
9969 default:
9970 break;
9971 }
9972 if (cur->children != NULL) {
9973 cur = cur->children;
9974 continue;
9975 }
9976next_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);
9987internal_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. Buchcik4d9c9482005-06-27 15:04:46 +00009997* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009998* @options: option flags
9999*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010000* 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 Veillard7e35abe2014-03-28 22:55:31 +080010003* This is the case when you have an unlinked node and just want to move it
Daniel Veillardaa6de472008-08-25 14:53:31 +000010004* to the context of
10005*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010006* 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. Buchcik978039b2006-06-16 19:46:26 +000010011* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010012*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010013* 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. Buchcikbc0e3c62005-06-27 10:28:23 +000010017*/
10018int
10019xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
10020 xmlDocPtr sourceDoc,
10021 xmlNodePtr node,
Daniel Veillardaa6de472008-08-25 14:53:31 +000010022 xmlDocPtr destDoc,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010023 xmlNodePtr destParent,
10024 int options)
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010025{
Daniel Veillard3e62adb2012-08-09 14:24:02 +080010026 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
10027 (destDoc == NULL) ||
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010028 ((destParent != NULL) && (destParent->doc != destDoc)))
10029 return(-1);
10030 /*
10031 * Check node->doc sanity.
Daniel Veillardaa6de472008-08-25 14:53:31 +000010032 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010033 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 Veillardaa6de472008-08-25 14:53:31 +000010045 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010046 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. Buchcikcab801b2006-02-03 16:35:27 +000010054 /* TODO: Support document-fragment-nodes. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010055 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 Veillardaa6de472008-08-25 14:53:31 +000010071 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010072 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 Veillardaa6de472008-08-25 14:53:31 +000010083 case XML_TEXT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010084 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 Veillard7e21fd12005-07-03 21:44:07 +000010108 case XML_PI_NODE: {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010109 XML_TREE_ADOPT_STR(node->name)
10110 XML_TREE_ADOPT_STR_2(node->content)
10111 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +000010112 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010113 default:
10114 break;
10115 }
Daniel Veillardaa6de472008-08-25 14:53:31 +000010116 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010117 return (0);
10118}
10119
Daniel Veillard5d4644e2005-04-01 13:11:58 +000010120#define bottom_tree
10121#include "elfgcchack.h"