blob: e154ec02370ffe4044e3e9833130936419d13c63 [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 *
Jan Pokornýbb654fe2016-04-13 16:56:07 +02001047 * Free a string if it is not owned by the "dict" dictionary in the
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001048 * 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 *
Jan Pokornýbb654fe2016-04-13 16:56:07 +02001060 * Copy a string using a "dict" dictionary in the current scope,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00001061 * if availabe.
1062 */
1063#define DICT_COPY(str, cpy) \
1064 if (str) { \
1065 if (dict) { \
1066 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1067 cpy = (xmlChar *) (str); \
1068 else \
1069 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1070 } else \
1071 cpy = xmlStrdup((const xmlChar *)(str)); }
1072
1073/**
1074 * DICT_CONST_COPY:
1075 * @str: a string
1076 *
Jan Pokornýbb654fe2016-04-13 16:56:07 +02001077 * Copy a string using a "dict" dictionary in the current scope,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00001078 * 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
Nick Wellnhofer8c82f5d2017-06-07 18:32:49 +02001404 /* Set to non-NULL value to avoid recursion. */
1405 ent->children = (xmlNodePtr) -1;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001406 ent->children = xmlStringGetNodeList(doc,
1407 (const xmlChar*)node->content);
1408 ent->owner = 1;
1409 temp = ent->children;
1410 while (temp) {
1411 temp->parent = (xmlNodePtr)ent;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001412 ent->last = temp;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001413 temp = temp->next;
1414 }
1415 }
1416 if (last == NULL) {
1417 last = ret = node;
1418 } else {
1419 last = xmlAddNextSibling(last, node);
1420 }
1421 }
1422 xmlFree(val);
1423 }
1424 cur++;
1425 q = cur;
1426 }
1427 if (charval != 0) {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001428 xmlChar buffer[10];
Daniel Veillard07cb8222003-09-10 10:51:05 +00001429 int l;
1430
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001431 l = xmlCopyCharMultiByte(buffer, charval);
1432 buffer[l] = 0;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001433
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001434 if (xmlBufCat(buf, buffer))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001435 goto out;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001436 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001437 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001438 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001439 cur++;
1440 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001441
1442 if (cur != q) {
Owen Taylor3473f882001-02-23 17:55:21 +00001443 /*
1444 * Handle the last piece of text.
1445 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001446 if (xmlBufAdd(buf, q, cur - q))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001447 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00001448 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001449
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001450 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001451 node = xmlNewDocText(doc, NULL);
1452 if (node == NULL) goto out;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001453 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001454
1455 if (last == NULL) {
Philip Withnall57941042014-10-26 18:08:04 +00001456 ret = node;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001457 } else {
Philip Withnall57941042014-10-26 18:08:04 +00001458 xmlAddNextSibling(last, node);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001459 }
1460 } else if (ret == NULL) {
1461 ret = xmlNewDocText(doc, BAD_CAST "");
1462 }
1463
1464out:
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001465 xmlBufFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001466 return(ret);
1467}
1468
1469/**
1470 * xmlStringGetNodeList:
1471 * @doc: the document
1472 * @value: the value of the attribute
1473 *
1474 * Parse the value string and build the node list associated. Should
1475 * produce a flat tree with only TEXTs and ENTITY_REFs.
1476 * Returns a pointer to the first child
1477 */
1478xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001479xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001480 xmlNodePtr ret = NULL, last = NULL;
1481 xmlNodePtr node;
1482 xmlChar *val;
1483 const xmlChar *cur = value;
1484 const xmlChar *q;
1485 xmlEntityPtr ent;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001486 xmlBufPtr buf;
Owen Taylor3473f882001-02-23 17:55:21 +00001487
1488 if (value == NULL) return(NULL);
1489
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001490 buf = xmlBufCreateSize(0);
1491 if (buf == NULL) return(NULL);
1492 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001493
Owen Taylor3473f882001-02-23 17:55:21 +00001494 q = cur;
1495 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001496 if (cur[0] == '&') {
1497 int charval = 0;
1498 xmlChar tmp;
1499
Owen Taylor3473f882001-02-23 17:55:21 +00001500 /*
1501 * Save the current text.
1502 */
1503 if (cur != q) {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001504 if (xmlBufAdd(buf, q, cur - q))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001505 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00001506 }
Owen Taylor3473f882001-02-23 17:55:21 +00001507 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001508 if ((cur[1] == '#') && (cur[2] == 'x')) {
1509 cur += 3;
1510 tmp = *cur;
1511 while (tmp != ';') { /* Non input consuming loop */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001512 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001513 charval = charval * 16 + (tmp - '0');
1514 else if ((tmp >= 'a') && (tmp <= 'f'))
1515 charval = charval * 16 + (tmp - 'a') + 10;
1516 else if ((tmp >= 'A') && (tmp <= 'F'))
1517 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001518 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001519 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1520 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001521 charval = 0;
1522 break;
1523 }
1524 cur++;
1525 tmp = *cur;
1526 }
1527 if (tmp == ';')
1528 cur++;
1529 q = cur;
1530 } else if (cur[1] == '#') {
1531 cur += 2;
1532 tmp = *cur;
1533 while (tmp != ';') { /* Non input consuming loops */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001534 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001535 charval = charval * 10 + (tmp - '0');
1536 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001537 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1538 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001539 charval = 0;
1540 break;
1541 }
1542 cur++;
1543 tmp = *cur;
1544 }
1545 if (tmp == ';')
1546 cur++;
1547 q = cur;
1548 } else {
1549 /*
1550 * Read the entity string
1551 */
1552 cur++;
1553 q = cur;
1554 while ((*cur != 0) && (*cur != ';')) cur++;
1555 if (*cur == 0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001556 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1557 (xmlNodePtr) doc, (const char *) q);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001558 goto out;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001559 }
1560 if (cur != q) {
1561 /*
1562 * Predefined entities don't generate nodes
1563 */
1564 val = xmlStrndup(q, cur - q);
1565 ent = xmlGetDocEntity(doc, val);
1566 if ((ent != NULL) &&
1567 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001568
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001569 if (xmlBufCat(buf, ent->content))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001570 goto out;
Daniel Veillardaa6de472008-08-25 14:53:31 +00001571
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001572 } else {
1573 /*
Conrad Irwin7d553f82012-05-10 20:17:25 -07001574 * Flush buffer so far
1575 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001576 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001577 node = xmlNewDocText(doc, NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001578 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001579
1580 if (last == NULL) {
1581 last = ret = node;
1582 } else {
1583 last = xmlAddNextSibling(last, node);
1584 }
1585 }
1586
1587 /*
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001588 * Create a new REFERENCE_REF node
1589 */
1590 node = xmlNewReference(doc, val);
1591 if (node == NULL) {
1592 if (val != NULL) xmlFree(val);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001593 goto out;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001594 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001595 else if ((ent != NULL) && (ent->children == NULL)) {
1596 xmlNodePtr temp;
1597
Nick Wellnhofer8c82f5d2017-06-07 18:32:49 +02001598 /* Set to non-NULL value to avoid recursion. */
Daniel Veillardbdd66182016-05-23 12:27:58 +08001599 ent->children = (xmlNodePtr) -1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001600 ent->children = xmlStringGetNodeList(doc,
1601 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001602 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001603 temp = ent->children;
1604 while (temp) {
1605 temp->parent = (xmlNodePtr)ent;
Nick Wellnhofer8c82f5d2017-06-07 18:32:49 +02001606 ent->last = temp;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001607 temp = temp->next;
1608 }
1609 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001610 if (last == NULL) {
1611 last = ret = node;
1612 } else {
1613 last = xmlAddNextSibling(last, node);
1614 }
1615 }
1616 xmlFree(val);
1617 }
1618 cur++;
1619 q = cur;
1620 }
1621 if (charval != 0) {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001622 xmlChar buffer[10];
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001623 int len;
1624
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001625 len = xmlCopyCharMultiByte(buffer, charval);
1626 buffer[len] = 0;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001627
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001628 if (xmlBufCat(buf, buffer))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001629 goto out;
1630 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001631 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001632 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001633 cur++;
1634 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001635 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001636 /*
1637 * Handle the last piece of text.
1638 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001639 xmlBufAdd(buf, q, cur - q);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001640 }
1641
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001642 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001643 node = xmlNewDocText(doc, NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001644 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001645
1646 if (last == NULL) {
Philip Withnall57941042014-10-26 18:08:04 +00001647 ret = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001648 } else {
Philip Withnall57941042014-10-26 18:08:04 +00001649 xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001650 }
1651 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001652
1653out:
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001654 xmlBufFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001655 return(ret);
1656}
1657
1658/**
1659 * xmlNodeListGetString:
1660 * @doc: the document
1661 * @list: a Node list
1662 * @inLine: should we replace entity contents or show their external form
1663 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001664 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001665 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001666 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001667 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001668 */
1669xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001670xmlNodeListGetString(xmlDocPtr doc, const xmlNode *list, int inLine)
Daniel Veillard7646b182002-04-20 06:41:40 +00001671{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001672 const xmlNode *node = list;
Owen Taylor3473f882001-02-23 17:55:21 +00001673 xmlChar *ret = NULL;
1674 xmlEntityPtr ent;
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001675 int attr;
Owen Taylor3473f882001-02-23 17:55:21 +00001676
Daniel Veillard7646b182002-04-20 06:41:40 +00001677 if (list == NULL)
1678 return (NULL);
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001679 if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE))
1680 attr = 1;
1681 else
1682 attr = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001683
1684 while (node != NULL) {
1685 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001686 (node->type == XML_CDATA_SECTION_NODE)) {
1687 if (inLine) {
1688 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001689 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001690 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001691
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001692 if (attr)
1693 buffer = xmlEncodeAttributeEntities(doc, node->content);
1694 else
1695 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
Daniel Veillard7646b182002-04-20 06:41:40 +00001696 if (buffer != NULL) {
1697 ret = xmlStrcat(ret, buffer);
1698 xmlFree(buffer);
1699 }
1700 }
1701 } else if (node->type == XML_ENTITY_REF_NODE) {
1702 if (inLine) {
1703 ent = xmlGetDocEntity(doc, node->name);
1704 if (ent != NULL) {
1705 xmlChar *buffer;
1706
1707 /* an entity content can be any "well balanced chunk",
1708 * i.e. the result of the content [43] production:
1709 * http://www.w3.org/TR/REC-xml#NT-content.
1710 * So it can contain text, CDATA section or nested
1711 * entity reference nodes (among others).
1712 * -> we recursive call xmlNodeListGetString()
1713 * which handles these types */
1714 buffer = xmlNodeListGetString(doc, ent->children, 1);
1715 if (buffer != NULL) {
1716 ret = xmlStrcat(ret, buffer);
1717 xmlFree(buffer);
1718 }
1719 } else {
1720 ret = xmlStrcat(ret, node->content);
1721 }
1722 } else {
1723 xmlChar buf[2];
1724
1725 buf[0] = '&';
1726 buf[1] = 0;
1727 ret = xmlStrncat(ret, buf, 1);
1728 ret = xmlStrcat(ret, node->name);
1729 buf[0] = ';';
1730 buf[1] = 0;
1731 ret = xmlStrncat(ret, buf, 1);
1732 }
1733 }
1734#if 0
1735 else {
1736 xmlGenericError(xmlGenericErrorContext,
1737 "xmlGetNodeListString : invalid node type %d\n",
1738 node->type);
1739 }
1740#endif
1741 node = node->next;
1742 }
1743 return (ret);
1744}
Daniel Veillard652327a2003-09-29 18:02:38 +00001745
1746#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001747/**
1748 * xmlNodeListGetRawString:
1749 * @doc: the document
1750 * @list: a Node list
1751 * @inLine: should we replace entity contents or show their external form
1752 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001753 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001754 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1755 * this function doesn't do any character encoding handling.
1756 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001757 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001758 */
1759xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001760xmlNodeListGetRawString(const xmlDoc *doc, const xmlNode *list, int inLine)
Daniel Veillard7646b182002-04-20 06:41:40 +00001761{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001762 const xmlNode *node = list;
Owen Taylor3473f882001-02-23 17:55:21 +00001763 xmlChar *ret = NULL;
1764 xmlEntityPtr ent;
1765
Daniel Veillard7646b182002-04-20 06:41:40 +00001766 if (list == NULL)
1767 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001768
1769 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001770 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001771 (node->type == XML_CDATA_SECTION_NODE)) {
1772 if (inLine) {
1773 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001774 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001775 xmlChar *buffer;
1776
1777 buffer = xmlEncodeSpecialChars(doc, node->content);
1778 if (buffer != NULL) {
1779 ret = xmlStrcat(ret, buffer);
1780 xmlFree(buffer);
1781 }
1782 }
1783 } else if (node->type == XML_ENTITY_REF_NODE) {
1784 if (inLine) {
1785 ent = xmlGetDocEntity(doc, node->name);
1786 if (ent != NULL) {
1787 xmlChar *buffer;
1788
1789 /* an entity content can be any "well balanced chunk",
1790 * i.e. the result of the content [43] production:
1791 * http://www.w3.org/TR/REC-xml#NT-content.
1792 * So it can contain text, CDATA section or nested
1793 * entity reference nodes (among others).
1794 * -> we recursive call xmlNodeListGetRawString()
1795 * which handles these types */
1796 buffer =
1797 xmlNodeListGetRawString(doc, ent->children, 1);
1798 if (buffer != NULL) {
1799 ret = xmlStrcat(ret, buffer);
1800 xmlFree(buffer);
1801 }
1802 } else {
1803 ret = xmlStrcat(ret, node->content);
1804 }
1805 } else {
1806 xmlChar buf[2];
1807
1808 buf[0] = '&';
1809 buf[1] = 0;
1810 ret = xmlStrncat(ret, buf, 1);
1811 ret = xmlStrcat(ret, node->name);
1812 buf[0] = ';';
1813 buf[1] = 0;
1814 ret = xmlStrncat(ret, buf, 1);
1815 }
1816 }
Owen Taylor3473f882001-02-23 17:55:21 +00001817#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001818 else {
1819 xmlGenericError(xmlGenericErrorContext,
1820 "xmlGetNodeListString : invalid node type %d\n",
1821 node->type);
1822 }
Owen Taylor3473f882001-02-23 17:55:21 +00001823#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001824 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001825 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001826 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001827}
Daniel Veillard652327a2003-09-29 18:02:38 +00001828#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001829
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001830static xmlAttrPtr
1831xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1832 const xmlChar * name, const xmlChar * value,
1833 int eatname)
1834{
Owen Taylor3473f882001-02-23 17:55:21 +00001835 xmlAttrPtr cur;
1836 xmlDocPtr doc = NULL;
1837
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001838 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001839 if ((eatname == 1) &&
1840 ((node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001841 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001842 xmlFree((xmlChar *) name);
1843 return (NULL);
1844 }
Owen Taylor3473f882001-02-23 17:55:21 +00001845
1846 /*
1847 * Allocate a new property and fill the fields.
1848 */
1849 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1850 if (cur == NULL) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001851 if ((eatname == 1) &&
Daniel Veillard76d36452009-09-07 11:19:33 +02001852 ((node == NULL) || (node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001853 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001854 xmlFree((xmlChar *) name);
1855 xmlTreeErrMemory("building attribute");
1856 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001857 }
1858 memset(cur, 0, sizeof(xmlAttr));
1859 cur->type = XML_ATTRIBUTE_NODE;
1860
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001861 cur->parent = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001862 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001863 doc = node->doc;
1864 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001865 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001866 cur->ns = ns;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001867
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001868 if (eatname == 0) {
1869 if ((doc != NULL) && (doc->dict != NULL))
1870 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1871 else
1872 cur->name = xmlStrdup(name);
1873 } else
1874 cur->name = name;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001875
Owen Taylor3473f882001-02-23 17:55:21 +00001876 if (value != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001877 xmlNodePtr tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001878
Daniel Veillard6f8611f2008-02-15 08:33:21 +00001879 if(!xmlCheckUTF8(value)) {
1880 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1881 NULL);
1882 if (doc != NULL)
1883 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1884 }
1885 cur->children = xmlNewDocText(doc, value);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001886 cur->last = NULL;
1887 tmp = cur->children;
1888 while (tmp != NULL) {
1889 tmp->parent = (xmlNodePtr) cur;
1890 if (tmp->next == NULL)
1891 cur->last = tmp;
1892 tmp = tmp->next;
1893 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001894 }
Owen Taylor3473f882001-02-23 17:55:21 +00001895
1896 /*
1897 * Add it at the end to preserve parsing order ...
1898 */
1899 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001900 if (node->properties == NULL) {
1901 node->properties = cur;
1902 } else {
1903 xmlAttrPtr prev = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00001904
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001905 while (prev->next != NULL)
1906 prev = prev->next;
1907 prev->next = cur;
1908 cur->prev = prev;
1909 }
Owen Taylor3473f882001-02-23 17:55:21 +00001910 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001911
Daniel Veillard76d36452009-09-07 11:19:33 +02001912 if ((value != NULL) && (node != NULL) &&
1913 (xmlIsID(node->doc, node, cur) == 1))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001914 xmlAddID(NULL, node->doc, value, cur);
1915
Daniel Veillarda880b122003-04-21 21:36:41 +00001916 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001917 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1918 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001919}
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001920
1921#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1922 defined(LIBXML_SCHEMAS_ENABLED)
1923/**
1924 * xmlNewProp:
1925 * @node: the holding node
1926 * @name: the name of the attribute
1927 * @value: the value of the attribute
1928 *
1929 * Create a new property carried by a node.
1930 * Returns a pointer to the attribute
1931 */
1932xmlAttrPtr
1933xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1934
1935 if (name == NULL) {
1936#ifdef DEBUG_TREE
1937 xmlGenericError(xmlGenericErrorContext,
1938 "xmlNewProp : name == NULL\n");
1939#endif
1940 return(NULL);
1941 }
1942
1943 return xmlNewPropInternal(node, NULL, name, value, 0);
1944}
Daniel Veillard652327a2003-09-29 18:02:38 +00001945#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001946
1947/**
1948 * xmlNewNsProp:
1949 * @node: the holding node
1950 * @ns: the namespace
1951 * @name: the name of the attribute
1952 * @value: the value of the attribute
1953 *
1954 * Create a new property tagged with a namespace and carried by a node.
1955 * Returns a pointer to the attribute
1956 */
1957xmlAttrPtr
1958xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1959 const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001960
1961 if (name == NULL) {
1962#ifdef DEBUG_TREE
1963 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001964 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001965#endif
1966 return(NULL);
1967 }
1968
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001969 return xmlNewPropInternal(node, ns, name, value, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001970}
1971
1972/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001973 * xmlNewNsPropEatName:
1974 * @node: the holding node
1975 * @ns: the namespace
1976 * @name: the name of the attribute
1977 * @value: the value of the attribute
1978 *
1979 * Create a new property tagged with a namespace and carried by a node.
1980 * Returns a pointer to the attribute
1981 */
1982xmlAttrPtr
1983xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1984 const xmlChar *value) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00001985
1986 if (name == NULL) {
1987#ifdef DEBUG_TREE
1988 xmlGenericError(xmlGenericErrorContext,
1989 "xmlNewNsPropEatName : name == NULL\n");
1990#endif
1991 return(NULL);
1992 }
1993
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001994 return xmlNewPropInternal(node, ns, name, value, 1);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001995}
1996
1997/**
Owen Taylor3473f882001-02-23 17:55:21 +00001998 * xmlNewDocProp:
1999 * @doc: the document
2000 * @name: the name of the attribute
2001 * @value: the value of the attribute
2002 *
2003 * Create a new property carried by a document.
2004 * Returns a pointer to the attribute
2005 */
2006xmlAttrPtr
2007xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
2008 xmlAttrPtr cur;
2009
2010 if (name == NULL) {
2011#ifdef DEBUG_TREE
2012 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00002013 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002014#endif
2015 return(NULL);
2016 }
2017
2018 /*
2019 * Allocate a new property and fill the fields.
2020 */
2021 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
2022 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002023 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00002024 return(NULL);
2025 }
2026 memset(cur, 0, sizeof(xmlAttr));
2027 cur->type = XML_ATTRIBUTE_NODE;
2028
Daniel Veillard03a53c32004-10-26 16:06:51 +00002029 if ((doc != NULL) && (doc->dict != NULL))
2030 cur->name = xmlDictLookup(doc->dict, name, -1);
2031 else
2032 cur->name = xmlStrdup(name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00002033 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002034 if (value != NULL) {
2035 xmlNodePtr tmp;
2036
2037 cur->children = xmlStringGetNodeList(doc, value);
2038 cur->last = NULL;
2039
2040 tmp = cur->children;
2041 while (tmp != NULL) {
2042 tmp->parent = (xmlNodePtr) cur;
2043 if (tmp->next == NULL)
2044 cur->last = tmp;
2045 tmp = tmp->next;
2046 }
2047 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002048
Daniel Veillarda880b122003-04-21 21:36:41 +00002049 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002050 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002051 return(cur);
2052}
2053
2054/**
2055 * xmlFreePropList:
2056 * @cur: the first property in the list
2057 *
2058 * Free a property and all its siblings, all the children are freed too.
2059 */
2060void
2061xmlFreePropList(xmlAttrPtr cur) {
2062 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002063 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002064 while (cur != NULL) {
2065 next = cur->next;
2066 xmlFreeProp(cur);
2067 cur = next;
2068 }
2069}
2070
2071/**
2072 * xmlFreeProp:
2073 * @cur: an attribute
2074 *
2075 * Free one attribute, all the content is freed too
2076 */
2077void
2078xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002079 xmlDictPtr dict = NULL;
2080 if (cur == NULL) return;
2081
2082 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002083
Daniel Veillarda880b122003-04-21 21:36:41 +00002084 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002085 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2086
Owen Taylor3473f882001-02-23 17:55:21 +00002087 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillardda6f4af2005-06-20 17:17:54 +00002088 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2089 xmlRemoveID(cur->doc, cur);
Daniel Veillard76d66f42001-05-16 21:05:17 +00002090 }
Owen Taylor3473f882001-02-23 17:55:21 +00002091 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002092 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002093 xmlFree(cur);
2094}
2095
2096/**
2097 * xmlRemoveProp:
2098 * @cur: an attribute
2099 *
2100 * Unlink and free one attribute, all the content is freed too
2101 * Note this doesn't work for namespace definition attributes
2102 *
2103 * Returns 0 if success and -1 in case of error.
2104 */
2105int
2106xmlRemoveProp(xmlAttrPtr cur) {
2107 xmlAttrPtr tmp;
2108 if (cur == NULL) {
2109#ifdef DEBUG_TREE
2110 xmlGenericError(xmlGenericErrorContext,
2111 "xmlRemoveProp : cur == NULL\n");
2112#endif
2113 return(-1);
2114 }
2115 if (cur->parent == NULL) {
2116#ifdef DEBUG_TREE
2117 xmlGenericError(xmlGenericErrorContext,
2118 "xmlRemoveProp : cur->parent == NULL\n");
2119#endif
2120 return(-1);
2121 }
2122 tmp = cur->parent->properties;
2123 if (tmp == cur) {
2124 cur->parent->properties = cur->next;
Rob Richards19dc9612005-10-28 16:15:16 +00002125 if (cur->next != NULL)
2126 cur->next->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002127 xmlFreeProp(cur);
2128 return(0);
2129 }
2130 while (tmp != NULL) {
2131 if (tmp->next == cur) {
2132 tmp->next = cur->next;
2133 if (tmp->next != NULL)
2134 tmp->next->prev = tmp;
2135 xmlFreeProp(cur);
2136 return(0);
2137 }
2138 tmp = tmp->next;
2139 }
2140#ifdef DEBUG_TREE
2141 xmlGenericError(xmlGenericErrorContext,
2142 "xmlRemoveProp : attribute not owned by its node\n");
2143#endif
2144 return(-1);
2145}
2146
2147/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002148 * xmlNewDocPI:
2149 * @doc: the target document
Owen Taylor3473f882001-02-23 17:55:21 +00002150 * @name: the processing instruction name
2151 * @content: the PI content
2152 *
2153 * Creation of a processing instruction element.
2154 * Returns a pointer to the new node object.
2155 */
2156xmlNodePtr
Daniel Veillard03a53c32004-10-26 16:06:51 +00002157xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002158 xmlNodePtr cur;
2159
2160 if (name == NULL) {
2161#ifdef DEBUG_TREE
2162 xmlGenericError(xmlGenericErrorContext,
2163 "xmlNewPI : name == NULL\n");
2164#endif
2165 return(NULL);
2166 }
2167
2168 /*
2169 * Allocate a new node and fill the fields.
2170 */
2171 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2172 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002173 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002174 return(NULL);
2175 }
2176 memset(cur, 0, sizeof(xmlNode));
2177 cur->type = XML_PI_NODE;
2178
Daniel Veillard03a53c32004-10-26 16:06:51 +00002179 if ((doc != NULL) && (doc->dict != NULL))
2180 cur->name = xmlDictLookup(doc->dict, name, -1);
2181 else
2182 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00002183 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002184 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002185 }
Daniel Veillarda03e3652004-11-02 18:45:30 +00002186 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002187
Daniel Veillarda880b122003-04-21 21:36:41 +00002188 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002189 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002190 return(cur);
2191}
2192
2193/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002194 * xmlNewPI:
2195 * @name: the processing instruction name
2196 * @content: the PI content
2197 *
2198 * Creation of a processing instruction element.
2199 * Use xmlDocNewPI preferably to get string interning
2200 *
2201 * Returns a pointer to the new node object.
2202 */
2203xmlNodePtr
2204xmlNewPI(const xmlChar *name, const xmlChar *content) {
2205 return(xmlNewDocPI(NULL, name, content));
2206}
2207
2208/**
Owen Taylor3473f882001-02-23 17:55:21 +00002209 * xmlNewNode:
2210 * @ns: namespace if any
2211 * @name: the node name
2212 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002213 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002214 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002215 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2216 * copy of @name.
Owen Taylor3473f882001-02-23 17:55:21 +00002217 */
2218xmlNodePtr
2219xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2220 xmlNodePtr cur;
2221
2222 if (name == NULL) {
2223#ifdef DEBUG_TREE
2224 xmlGenericError(xmlGenericErrorContext,
2225 "xmlNewNode : name == NULL\n");
2226#endif
2227 return(NULL);
2228 }
2229
2230 /*
2231 * Allocate a new node and fill the fields.
2232 */
2233 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2234 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002235 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002236 return(NULL);
2237 }
2238 memset(cur, 0, sizeof(xmlNode));
2239 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002240
Owen Taylor3473f882001-02-23 17:55:21 +00002241 cur->name = xmlStrdup(name);
2242 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002243
Daniel Veillarda880b122003-04-21 21:36:41 +00002244 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002245 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002246 return(cur);
2247}
2248
2249/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002250 * xmlNewNodeEatName:
2251 * @ns: namespace if any
2252 * @name: the node name
2253 *
2254 * Creation of a new node element. @ns is optional (NULL).
2255 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002256 * Returns a pointer to the new node object, with pointer @name as
2257 * new node's name. Use xmlNewNode() if a copy of @name string is
2258 * is needed as new node's name.
Daniel Veillard46de64e2002-05-29 08:21:33 +00002259 */
2260xmlNodePtr
2261xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2262 xmlNodePtr cur;
2263
2264 if (name == NULL) {
2265#ifdef DEBUG_TREE
2266 xmlGenericError(xmlGenericErrorContext,
2267 "xmlNewNode : name == NULL\n");
2268#endif
2269 return(NULL);
2270 }
2271
2272 /*
2273 * Allocate a new node and fill the fields.
2274 */
2275 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2276 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002277 xmlTreeErrMemory("building node");
Jan Pokornýbb654fe2016-04-13 16:56:07 +02002278 /* we can't check here that name comes from the doc dictionary */
Daniel Veillard46de64e2002-05-29 08:21:33 +00002279 return(NULL);
2280 }
2281 memset(cur, 0, sizeof(xmlNode));
2282 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002283
Daniel Veillard46de64e2002-05-29 08:21:33 +00002284 cur->name = name;
2285 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002286
Daniel Veillarda880b122003-04-21 21:36:41 +00002287 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002288 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002289 return(cur);
2290}
2291
2292/**
Owen Taylor3473f882001-02-23 17:55:21 +00002293 * xmlNewDocNode:
2294 * @doc: the document
2295 * @ns: namespace if any
2296 * @name: the node name
2297 * @content: the XML text content if any
2298 *
2299 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002300 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002301 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2302 * references, but XML special chars need to be escaped first by using
2303 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2304 * need entities support.
2305 *
2306 * Returns a pointer to the new node object.
2307 */
2308xmlNodePtr
2309xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2310 const xmlChar *name, const xmlChar *content) {
2311 xmlNodePtr cur;
2312
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002313 if ((doc != NULL) && (doc->dict != NULL))
Daniel Veillard03a53c32004-10-26 16:06:51 +00002314 cur = xmlNewNodeEatName(ns, (xmlChar *)
2315 xmlDictLookup(doc->dict, name, -1));
2316 else
2317 cur = xmlNewNode(ns, name);
Owen Taylor3473f882001-02-23 17:55:21 +00002318 if (cur != NULL) {
2319 cur->doc = doc;
2320 if (content != NULL) {
2321 cur->children = xmlStringGetNodeList(doc, content);
2322 UPDATE_LAST_CHILD_AND_PARENT(cur)
2323 }
2324 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002325
Owen Taylor3473f882001-02-23 17:55:21 +00002326 return(cur);
2327}
2328
Daniel Veillard46de64e2002-05-29 08:21:33 +00002329/**
2330 * xmlNewDocNodeEatName:
2331 * @doc: the document
2332 * @ns: namespace if any
2333 * @name: the node name
2334 * @content: the XML text content if any
2335 *
2336 * Creation of a new node element within a document. @ns and @content
2337 * are optional (NULL).
2338 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2339 * references, but XML special chars need to be escaped first by using
2340 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2341 * need entities support.
2342 *
2343 * Returns a pointer to the new node object.
2344 */
2345xmlNodePtr
2346xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2347 xmlChar *name, const xmlChar *content) {
2348 xmlNodePtr cur;
2349
2350 cur = xmlNewNodeEatName(ns, name);
2351 if (cur != NULL) {
2352 cur->doc = doc;
2353 if (content != NULL) {
2354 cur->children = xmlStringGetNodeList(doc, content);
2355 UPDATE_LAST_CHILD_AND_PARENT(cur)
2356 }
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00002357 } else {
Jan Pokornýbb654fe2016-04-13 16:56:07 +02002358 /* if name don't come from the doc dictionary free it here */
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00002359 if ((name != NULL) && (doc != NULL) &&
2360 (!(xmlDictOwns(doc->dict, name))))
2361 xmlFree(name);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002362 }
2363 return(cur);
2364}
2365
Daniel Veillard652327a2003-09-29 18:02:38 +00002366#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002367/**
2368 * xmlNewDocRawNode:
2369 * @doc: the document
2370 * @ns: namespace if any
2371 * @name: the node name
2372 * @content: the text content if any
2373 *
2374 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002375 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002376 *
2377 * Returns a pointer to the new node object.
2378 */
2379xmlNodePtr
2380xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2381 const xmlChar *name, const xmlChar *content) {
2382 xmlNodePtr cur;
2383
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002384 cur = xmlNewDocNode(doc, ns, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002385 if (cur != NULL) {
2386 cur->doc = doc;
2387 if (content != NULL) {
2388 cur->children = xmlNewDocText(doc, content);
2389 UPDATE_LAST_CHILD_AND_PARENT(cur)
2390 }
2391 }
2392 return(cur);
2393}
2394
2395/**
2396 * xmlNewDocFragment:
2397 * @doc: the document owning the fragment
2398 *
2399 * Creation of a new Fragment node.
2400 * Returns a pointer to the new node object.
2401 */
2402xmlNodePtr
2403xmlNewDocFragment(xmlDocPtr doc) {
2404 xmlNodePtr cur;
2405
2406 /*
2407 * Allocate a new DocumentFragment node and fill the fields.
2408 */
2409 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2410 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002411 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002412 return(NULL);
2413 }
2414 memset(cur, 0, sizeof(xmlNode));
2415 cur->type = XML_DOCUMENT_FRAG_NODE;
2416
2417 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002418
Daniel Veillarda880b122003-04-21 21:36:41 +00002419 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002420 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002421 return(cur);
2422}
Daniel Veillard652327a2003-09-29 18:02:38 +00002423#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002424
2425/**
2426 * xmlNewText:
2427 * @content: the text content
2428 *
2429 * Creation of a new text node.
2430 * Returns a pointer to the new node object.
2431 */
2432xmlNodePtr
2433xmlNewText(const xmlChar *content) {
2434 xmlNodePtr cur;
2435
2436 /*
2437 * Allocate a new node and fill the fields.
2438 */
2439 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2440 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002441 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002442 return(NULL);
2443 }
2444 memset(cur, 0, sizeof(xmlNode));
2445 cur->type = XML_TEXT_NODE;
2446
2447 cur->name = xmlStringText;
2448 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002449 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002450 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002451
Daniel Veillarda880b122003-04-21 21:36:41 +00002452 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002453 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002454 return(cur);
2455}
2456
Daniel Veillard652327a2003-09-29 18:02:38 +00002457#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002458/**
2459 * xmlNewTextChild:
2460 * @parent: the parent node
2461 * @ns: a namespace if any
2462 * @name: the name of the child
2463 * @content: the text content of the child if any.
2464 *
2465 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002466 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2467 * created element inherits the namespace of @parent. If @content is non NULL,
William M. Brackd7cf7f82003-11-14 07:13:16 +00002468 * a child TEXT node will be created containing the string @content.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002469 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2470 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
Daniel Veillardaa6de472008-08-25 14:53:31 +00002471 * reserved XML chars that might appear in @content, such as the ampersand,
2472 * greater-than or less-than signs, are automatically replaced by their XML
2473 * escaped entity representations.
Owen Taylor3473f882001-02-23 17:55:21 +00002474 *
2475 * Returns a pointer to the new node object.
2476 */
2477xmlNodePtr
2478xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2479 const xmlChar *name, const xmlChar *content) {
2480 xmlNodePtr cur, prev;
2481
2482 if (parent == NULL) {
2483#ifdef DEBUG_TREE
2484 xmlGenericError(xmlGenericErrorContext,
2485 "xmlNewTextChild : parent == NULL\n");
2486#endif
2487 return(NULL);
2488 }
2489
2490 if (name == NULL) {
2491#ifdef DEBUG_TREE
2492 xmlGenericError(xmlGenericErrorContext,
2493 "xmlNewTextChild : name == NULL\n");
2494#endif
2495 return(NULL);
2496 }
2497
2498 /*
2499 * Allocate a new node
2500 */
Daniel Veillard254b1262003-11-01 17:04:58 +00002501 if (parent->type == XML_ELEMENT_NODE) {
2502 if (ns == NULL)
2503 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2504 else
2505 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2506 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2507 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2508 if (ns == NULL)
2509 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2510 else
2511 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2512 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2513 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2514 } else {
2515 return(NULL);
2516 }
Owen Taylor3473f882001-02-23 17:55:21 +00002517 if (cur == NULL) return(NULL);
2518
2519 /*
2520 * add the new element at the end of the children list.
2521 */
2522 cur->type = XML_ELEMENT_NODE;
2523 cur->parent = parent;
2524 cur->doc = parent->doc;
2525 if (parent->children == NULL) {
2526 parent->children = cur;
2527 parent->last = cur;
2528 } else {
2529 prev = parent->last;
2530 prev->next = cur;
2531 cur->prev = prev;
2532 parent->last = cur;
2533 }
2534
2535 return(cur);
2536}
Daniel Veillard652327a2003-09-29 18:02:38 +00002537#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002538
2539/**
2540 * xmlNewCharRef:
2541 * @doc: the document
2542 * @name: the char ref string, starting with # or "&# ... ;"
2543 *
2544 * Creation of a new character reference node.
2545 * Returns a pointer to the new node object.
2546 */
2547xmlNodePtr
2548xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2549 xmlNodePtr cur;
2550
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002551 if (name == NULL)
2552 return(NULL);
2553
Owen Taylor3473f882001-02-23 17:55:21 +00002554 /*
2555 * Allocate a new node and fill the fields.
2556 */
2557 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2558 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002559 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002560 return(NULL);
2561 }
2562 memset(cur, 0, sizeof(xmlNode));
2563 cur->type = XML_ENTITY_REF_NODE;
2564
2565 cur->doc = doc;
2566 if (name[0] == '&') {
2567 int len;
2568 name++;
2569 len = xmlStrlen(name);
2570 if (name[len - 1] == ';')
2571 cur->name = xmlStrndup(name, len - 1);
2572 else
2573 cur->name = xmlStrndup(name, len);
2574 } else
2575 cur->name = xmlStrdup(name);
Daniel Veillard5335dc52003-01-01 20:59:38 +00002576
Daniel Veillarda880b122003-04-21 21:36:41 +00002577 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002578 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002579 return(cur);
2580}
2581
2582/**
2583 * xmlNewReference:
2584 * @doc: the document
2585 * @name: the reference name, or the reference string with & and ;
2586 *
2587 * Creation of a new reference node.
2588 * Returns a pointer to the new node object.
2589 */
2590xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002591xmlNewReference(const xmlDoc *doc, const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00002592 xmlNodePtr cur;
2593 xmlEntityPtr ent;
2594
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002595 if (name == NULL)
2596 return(NULL);
2597
Owen Taylor3473f882001-02-23 17:55:21 +00002598 /*
2599 * Allocate a new node and fill the fields.
2600 */
2601 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2602 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002603 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002604 return(NULL);
2605 }
2606 memset(cur, 0, sizeof(xmlNode));
2607 cur->type = XML_ENTITY_REF_NODE;
2608
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002609 cur->doc = (xmlDoc *)doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002610 if (name[0] == '&') {
2611 int len;
2612 name++;
2613 len = xmlStrlen(name);
2614 if (name[len - 1] == ';')
2615 cur->name = xmlStrndup(name, len - 1);
2616 else
2617 cur->name = xmlStrndup(name, len);
2618 } else
2619 cur->name = xmlStrdup(name);
2620
2621 ent = xmlGetDocEntity(doc, cur->name);
2622 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002623 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002624 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002625 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002626 * updated. Not sure if this is 100% correct.
2627 * -George
2628 */
2629 cur->children = (xmlNodePtr) ent;
2630 cur->last = (xmlNodePtr) ent;
2631 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002632
Daniel Veillarda880b122003-04-21 21:36:41 +00002633 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002634 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002635 return(cur);
2636}
2637
2638/**
2639 * xmlNewDocText:
2640 * @doc: the document
2641 * @content: the text content
2642 *
2643 * Creation of a new text node within a document.
2644 * Returns a pointer to the new node object.
2645 */
2646xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002647xmlNewDocText(const xmlDoc *doc, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002648 xmlNodePtr cur;
2649
2650 cur = xmlNewText(content);
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002651 if (cur != NULL) cur->doc = (xmlDoc *)doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002652 return(cur);
2653}
2654
2655/**
2656 * xmlNewTextLen:
2657 * @content: the text content
2658 * @len: the text len.
2659 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002660 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002661 * Returns a pointer to the new node object.
2662 */
2663xmlNodePtr
2664xmlNewTextLen(const xmlChar *content, int len) {
2665 xmlNodePtr cur;
2666
2667 /*
2668 * Allocate a new node and fill the fields.
2669 */
2670 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2671 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002672 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002673 return(NULL);
2674 }
2675 memset(cur, 0, sizeof(xmlNode));
2676 cur->type = XML_TEXT_NODE;
2677
2678 cur->name = xmlStringText;
2679 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002680 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002681 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002682
Daniel Veillarda880b122003-04-21 21:36:41 +00002683 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002684 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002685 return(cur);
2686}
2687
2688/**
2689 * xmlNewDocTextLen:
2690 * @doc: the document
2691 * @content: the text content
2692 * @len: the text len.
2693 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002694 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002695 * text node pertain to a given document.
2696 * Returns a pointer to the new node object.
2697 */
2698xmlNodePtr
2699xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2700 xmlNodePtr cur;
2701
2702 cur = xmlNewTextLen(content, len);
2703 if (cur != NULL) cur->doc = doc;
2704 return(cur);
2705}
2706
2707/**
2708 * xmlNewComment:
2709 * @content: the comment content
2710 *
2711 * Creation of a new node containing a comment.
2712 * Returns a pointer to the new node object.
2713 */
2714xmlNodePtr
2715xmlNewComment(const xmlChar *content) {
2716 xmlNodePtr cur;
2717
2718 /*
2719 * Allocate a new node and fill the fields.
2720 */
2721 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2722 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002723 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002724 return(NULL);
2725 }
2726 memset(cur, 0, sizeof(xmlNode));
2727 cur->type = XML_COMMENT_NODE;
2728
2729 cur->name = xmlStringComment;
2730 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002731 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002732 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002733
Daniel Veillarda880b122003-04-21 21:36:41 +00002734 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002735 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002736 return(cur);
2737}
2738
2739/**
2740 * xmlNewCDataBlock:
2741 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002742 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002743 * @len: the length of the block
2744 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002745 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002746 * Returns a pointer to the new node object.
2747 */
2748xmlNodePtr
2749xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2750 xmlNodePtr cur;
2751
2752 /*
2753 * Allocate a new node and fill the fields.
2754 */
2755 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2756 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002757 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002758 return(NULL);
2759 }
2760 memset(cur, 0, sizeof(xmlNode));
2761 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002762 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002763
2764 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002765 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002766 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002767
Daniel Veillarda880b122003-04-21 21:36:41 +00002768 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002769 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002770 return(cur);
2771}
2772
2773/**
2774 * xmlNewDocComment:
2775 * @doc: the document
2776 * @content: the comment content
2777 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002778 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002779 * Returns a pointer to the new node object.
2780 */
2781xmlNodePtr
2782xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2783 xmlNodePtr cur;
2784
2785 cur = xmlNewComment(content);
2786 if (cur != NULL) cur->doc = doc;
2787 return(cur);
2788}
2789
2790/**
2791 * xmlSetTreeDoc:
2792 * @tree: the top element
2793 * @doc: the document
2794 *
2795 * update all nodes under the tree to point to the right document
2796 */
2797void
2798xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002799 xmlAttrPtr prop;
2800
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002801 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002802 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002803 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002804 if(tree->type == XML_ELEMENT_NODE) {
2805 prop = tree->properties;
2806 while (prop != NULL) {
Nick Wellnhoferf54d6a92014-12-19 00:08:35 +01002807 if (prop->atype == XML_ATTRIBUTE_ID) {
2808 xmlRemoveID(tree->doc, prop);
2809 }
2810
Daniel Veillard36065812002-01-24 15:02:46 +00002811 prop->doc = doc;
2812 xmlSetListDoc(prop->children, doc);
Nick Wellnhoferf54d6a92014-12-19 00:08:35 +01002813
Nick Wellnhofer220a7ba2014-12-23 21:28:37 +01002814 /*
2815 * TODO: ID attributes should be also added to the new
2816 * document, but this breaks things like xmlReplaceNode.
2817 * The underlying problem is that xmlRemoveID is only called
2818 * if a node is destroyed, not if it's unlinked.
2819 */
2820#if 0
Nick Wellnhoferf54d6a92014-12-19 00:08:35 +01002821 if (xmlIsID(doc, tree, prop)) {
2822 xmlChar *idVal = xmlNodeListGetString(doc, prop->children,
2823 1);
2824 xmlAddID(NULL, doc, idVal, prop);
2825 }
Nick Wellnhofer220a7ba2014-12-23 21:28:37 +01002826#endif
Nick Wellnhoferf54d6a92014-12-19 00:08:35 +01002827
Daniel Veillard36065812002-01-24 15:02:46 +00002828 prop = prop->next;
2829 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002830 }
Owen Taylor3473f882001-02-23 17:55:21 +00002831 if (tree->children != NULL)
2832 xmlSetListDoc(tree->children, doc);
2833 tree->doc = doc;
2834 }
2835}
2836
2837/**
2838 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002839 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002840 * @doc: the document
2841 *
2842 * update all nodes in the list to point to the right document
2843 */
2844void
2845xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2846 xmlNodePtr cur;
2847
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002848 if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002849 return;
2850 cur = list;
2851 while (cur != NULL) {
2852 if (cur->doc != doc)
2853 xmlSetTreeDoc(cur, doc);
2854 cur = cur->next;
2855 }
2856}
2857
Daniel Veillard2156d432004-03-04 15:59:36 +00002858#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002859/**
2860 * xmlNewChild:
2861 * @parent: the parent node
2862 * @ns: a namespace if any
2863 * @name: the name of the child
2864 * @content: the XML content of the child if any.
2865 *
2866 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002867 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2868 * created element inherits the namespace of @parent. If @content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002869 * a child list containing the TEXTs and ENTITY_REFs node will be created.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002870 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2871 * references. XML special chars must be escaped first by using
2872 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
Owen Taylor3473f882001-02-23 17:55:21 +00002873 *
2874 * Returns a pointer to the new node object.
2875 */
2876xmlNodePtr
2877xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2878 const xmlChar *name, const xmlChar *content) {
2879 xmlNodePtr cur, prev;
2880
2881 if (parent == NULL) {
2882#ifdef DEBUG_TREE
2883 xmlGenericError(xmlGenericErrorContext,
2884 "xmlNewChild : parent == NULL\n");
2885#endif
2886 return(NULL);
2887 }
2888
2889 if (name == NULL) {
2890#ifdef DEBUG_TREE
2891 xmlGenericError(xmlGenericErrorContext,
2892 "xmlNewChild : name == NULL\n");
2893#endif
2894 return(NULL);
2895 }
2896
2897 /*
2898 * Allocate a new node
2899 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002900 if (parent->type == XML_ELEMENT_NODE) {
2901 if (ns == NULL)
2902 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2903 else
2904 cur = xmlNewDocNode(parent->doc, ns, name, content);
2905 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2906 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2907 if (ns == NULL)
2908 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2909 else
2910 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002911 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2912 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002913 } else {
2914 return(NULL);
2915 }
Owen Taylor3473f882001-02-23 17:55:21 +00002916 if (cur == NULL) return(NULL);
2917
2918 /*
2919 * add the new element at the end of the children list.
2920 */
2921 cur->type = XML_ELEMENT_NODE;
2922 cur->parent = parent;
2923 cur->doc = parent->doc;
2924 if (parent->children == NULL) {
2925 parent->children = cur;
2926 parent->last = cur;
2927 } else {
2928 prev = parent->last;
2929 prev->next = cur;
2930 cur->prev = prev;
2931 parent->last = cur;
2932 }
2933
2934 return(cur);
2935}
Daniel Veillard652327a2003-09-29 18:02:38 +00002936#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002937
2938/**
Rob Richards65815122006-02-25 17:13:33 +00002939 * xmlAddPropSibling:
Daniel Veillardaa6de472008-08-25 14:53:31 +00002940 * @prev: the attribute to which @prop is added after
Rob Richards65815122006-02-25 17:13:33 +00002941 * @cur: the base attribute passed to calling function
2942 * @prop: the new attribute
2943 *
2944 * Add a new attribute after @prev using @cur as base attribute.
2945 * When inserting before @cur, @prev is passed as @cur->prev.
2946 * When inserting after @cur, @prev is passed as @cur.
Daniel Veillardaa6de472008-08-25 14:53:31 +00002947 * If an existing attribute is found it is detroyed prior to adding @prop.
Rob Richards65815122006-02-25 17:13:33 +00002948 *
2949 * Returns the attribute being inserted or NULL in case of error.
2950 */
2951static xmlNodePtr
2952xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2953 xmlAttrPtr attr;
2954
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002955 if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) ||
2956 (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) ||
2957 ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)))
Rob Richards65815122006-02-25 17:13:33 +00002958 return(NULL);
2959
2960 /* check if an attribute with the same name exists */
2961 if (prop->ns == NULL)
2962 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2963 else
2964 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2965
2966 if (prop->doc != cur->doc) {
2967 xmlSetTreeDoc(prop, cur->doc);
2968 }
2969 prop->parent = cur->parent;
2970 prop->prev = prev;
2971 if (prev != NULL) {
2972 prop->next = prev->next;
2973 prev->next = prop;
2974 if (prop->next)
2975 prop->next->prev = prop;
2976 } else {
2977 prop->next = cur;
2978 cur->prev = prop;
2979 }
2980 if (prop->prev == NULL && prop->parent != NULL)
2981 prop->parent->properties = (xmlAttrPtr) prop;
2982 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2983 /* different instance, destroy it (attributes must be unique) */
2984 xmlRemoveProp((xmlAttrPtr) attr);
2985 }
2986 return prop;
2987}
2988
2989/**
Owen Taylor3473f882001-02-23 17:55:21 +00002990 * xmlAddNextSibling:
2991 * @cur: the child node
2992 * @elem: the new node
2993 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002994 * Add a new node @elem as the next sibling of @cur
2995 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002996 * first unlinked from its existing context.
2997 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002998 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00002999 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00003000 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003001 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003002 */
3003xmlNodePtr
3004xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003005 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003006#ifdef DEBUG_TREE
3007 xmlGenericError(xmlGenericErrorContext,
3008 "xmlAddNextSibling : cur == NULL\n");
3009#endif
3010 return(NULL);
3011 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003012 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003013#ifdef DEBUG_TREE
3014 xmlGenericError(xmlGenericErrorContext,
3015 "xmlAddNextSibling : elem == NULL\n");
3016#endif
3017 return(NULL);
3018 }
3019
Rob Richards19dc9612005-10-28 16:15:16 +00003020 if (cur == elem) {
3021#ifdef DEBUG_TREE
3022 xmlGenericError(xmlGenericErrorContext,
3023 "xmlAddNextSibling : cur == elem\n");
3024#endif
3025 return(NULL);
3026 }
3027
Owen Taylor3473f882001-02-23 17:55:21 +00003028 xmlUnlinkNode(elem);
3029
3030 if (elem->type == XML_TEXT_NODE) {
3031 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003032 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003033 xmlFreeNode(elem);
3034 return(cur);
3035 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003036 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
3037 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003038 xmlChar *tmp;
3039
3040 tmp = xmlStrdup(elem->content);
3041 tmp = xmlStrcat(tmp, cur->next->content);
3042 xmlNodeSetContent(cur->next, tmp);
3043 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003044 xmlFreeNode(elem);
3045 return(cur->next);
3046 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003047 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003048 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003049 }
3050
3051 if (elem->doc != cur->doc) {
3052 xmlSetTreeDoc(elem, cur->doc);
3053 }
3054 elem->parent = cur->parent;
3055 elem->prev = cur;
3056 elem->next = cur->next;
3057 cur->next = elem;
3058 if (elem->next != NULL)
3059 elem->next->prev = elem;
Rob Richards65815122006-02-25 17:13:33 +00003060 if ((elem->parent != NULL) && (elem->parent->last == cur))
Owen Taylor3473f882001-02-23 17:55:21 +00003061 elem->parent->last = elem;
3062 return(elem);
3063}
3064
William M. Brack21e4ef22005-01-02 09:53:13 +00003065#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
Nicolas Le Cam41586ca2013-06-17 13:01:33 +02003066 defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003067/**
3068 * xmlAddPrevSibling:
3069 * @cur: the child node
3070 * @elem: the new node
3071 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003072 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00003073 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003074 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003075 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003076 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003077 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00003078 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003079 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003080 */
3081xmlNodePtr
3082xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003083 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003084#ifdef DEBUG_TREE
3085 xmlGenericError(xmlGenericErrorContext,
3086 "xmlAddPrevSibling : cur == NULL\n");
3087#endif
3088 return(NULL);
3089 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003090 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003091#ifdef DEBUG_TREE
3092 xmlGenericError(xmlGenericErrorContext,
3093 "xmlAddPrevSibling : elem == NULL\n");
3094#endif
3095 return(NULL);
3096 }
3097
Rob Richards19dc9612005-10-28 16:15:16 +00003098 if (cur == elem) {
3099#ifdef DEBUG_TREE
3100 xmlGenericError(xmlGenericErrorContext,
3101 "xmlAddPrevSibling : cur == elem\n");
3102#endif
3103 return(NULL);
3104 }
3105
Owen Taylor3473f882001-02-23 17:55:21 +00003106 xmlUnlinkNode(elem);
3107
3108 if (elem->type == XML_TEXT_NODE) {
3109 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003110 xmlChar *tmp;
3111
3112 tmp = xmlStrdup(elem->content);
3113 tmp = xmlStrcat(tmp, cur->content);
3114 xmlNodeSetContent(cur, tmp);
3115 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003116 xmlFreeNode(elem);
3117 return(cur);
3118 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003119 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3120 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003121 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003122 xmlFreeNode(elem);
3123 return(cur->prev);
3124 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003125 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003126 return xmlAddPropSibling(cur->prev, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003127 }
3128
3129 if (elem->doc != cur->doc) {
3130 xmlSetTreeDoc(elem, cur->doc);
3131 }
3132 elem->parent = cur->parent;
3133 elem->next = cur;
3134 elem->prev = cur->prev;
3135 cur->prev = elem;
3136 if (elem->prev != NULL)
3137 elem->prev->next = elem;
Rob Richards65815122006-02-25 17:13:33 +00003138 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003139 elem->parent->children = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003140 }
Owen Taylor3473f882001-02-23 17:55:21 +00003141 return(elem);
3142}
Daniel Veillard652327a2003-09-29 18:02:38 +00003143#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003144
3145/**
3146 * xmlAddSibling:
3147 * @cur: the child node
3148 * @elem: the new node
3149 *
3150 * Add a new element @elem to the list of siblings of @cur
3151 * merging adjacent TEXT nodes (@elem may be freed)
3152 * If the new element was already inserted in a document it is
3153 * first unlinked from its existing context.
3154 *
3155 * Returns the new element or NULL in case of error.
3156 */
3157xmlNodePtr
3158xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3159 xmlNodePtr parent;
3160
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003161 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003162#ifdef DEBUG_TREE
3163 xmlGenericError(xmlGenericErrorContext,
3164 "xmlAddSibling : cur == NULL\n");
3165#endif
3166 return(NULL);
3167 }
3168
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003169 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003170#ifdef DEBUG_TREE
3171 xmlGenericError(xmlGenericErrorContext,
3172 "xmlAddSibling : elem == NULL\n");
3173#endif
3174 return(NULL);
3175 }
3176
Daniel Veillard43bc89c2009-03-23 19:32:04 +00003177 if (cur == elem) {
3178#ifdef DEBUG_TREE
3179 xmlGenericError(xmlGenericErrorContext,
3180 "xmlAddSibling : cur == elem\n");
3181#endif
3182 return(NULL);
3183 }
3184
Owen Taylor3473f882001-02-23 17:55:21 +00003185 /*
3186 * Constant time is we can rely on the ->parent->last to find
3187 * the last sibling.
3188 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00003189 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003190 (cur->parent->children != NULL) &&
3191 (cur->parent->last != NULL) &&
3192 (cur->parent->last->next == NULL)) {
3193 cur = cur->parent->last;
3194 } else {
3195 while (cur->next != NULL) cur = cur->next;
3196 }
3197
3198 xmlUnlinkNode(elem);
3199
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003200 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3201 (cur->name == elem->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003202 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003203 xmlFreeNode(elem);
3204 return(cur);
Rob Richards65815122006-02-25 17:13:33 +00003205 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3206 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003207 }
3208
3209 if (elem->doc != cur->doc) {
3210 xmlSetTreeDoc(elem, cur->doc);
3211 }
3212 parent = cur->parent;
3213 elem->prev = cur;
3214 elem->next = NULL;
3215 elem->parent = parent;
3216 cur->next = elem;
3217 if (parent != NULL)
3218 parent->last = elem;
3219
3220 return(elem);
3221}
3222
3223/**
3224 * xmlAddChildList:
3225 * @parent: the parent node
3226 * @cur: the first node in the list
3227 *
3228 * Add a list of node at the end of the child list of the parent
3229 * merging adjacent TEXT nodes (@cur may be freed)
3230 *
3231 * Returns the last child or NULL in case of error.
3232 */
3233xmlNodePtr
3234xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3235 xmlNodePtr prev;
3236
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003237 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003238#ifdef DEBUG_TREE
3239 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003240 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003241#endif
3242 return(NULL);
3243 }
3244
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003245 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003246#ifdef DEBUG_TREE
3247 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003248 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003249#endif
3250 return(NULL);
3251 }
3252
3253 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3254 (cur->doc != parent->doc)) {
3255#ifdef DEBUG_TREE
3256 xmlGenericError(xmlGenericErrorContext,
3257 "Elements moved to a different document\n");
3258#endif
3259 }
3260
3261 /*
3262 * add the first element at the end of the children list.
3263 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003264
Owen Taylor3473f882001-02-23 17:55:21 +00003265 if (parent->children == NULL) {
3266 parent->children = cur;
3267 } else {
3268 /*
3269 * If cur and parent->last both are TEXT nodes, then merge them.
3270 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00003271 if ((cur->type == XML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003272 (parent->last->type == XML_TEXT_NODE) &&
3273 (cur->name == parent->last->name)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00003274 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003275 /*
3276 * if it's the only child, nothing more to be done.
3277 */
3278 if (cur->next == NULL) {
3279 xmlFreeNode(cur);
3280 return(parent->last);
3281 }
3282 prev = cur;
3283 cur = cur->next;
3284 xmlFreeNode(prev);
3285 }
3286 prev = parent->last;
3287 prev->next = cur;
3288 cur->prev = prev;
3289 }
3290 while (cur->next != NULL) {
3291 cur->parent = parent;
3292 if (cur->doc != parent->doc) {
3293 xmlSetTreeDoc(cur, parent->doc);
3294 }
3295 cur = cur->next;
3296 }
3297 cur->parent = parent;
Rob Richards810a78b2008-12-31 22:13:57 +00003298 /* the parent may not be linked to a doc ! */
3299 if (cur->doc != parent->doc) {
3300 xmlSetTreeDoc(cur, parent->doc);
3301 }
Owen Taylor3473f882001-02-23 17:55:21 +00003302 parent->last = cur;
3303
3304 return(cur);
3305}
3306
3307/**
3308 * xmlAddChild:
3309 * @parent: the parent node
3310 * @cur: the child node
3311 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003312 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003313 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003314 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003315 * If there is an attribute with equal name, it is first destroyed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003316 *
Owen Taylor3473f882001-02-23 17:55:21 +00003317 * Returns the child or NULL in case of error.
3318 */
3319xmlNodePtr
3320xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3321 xmlNodePtr prev;
3322
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003323 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003324#ifdef DEBUG_TREE
3325 xmlGenericError(xmlGenericErrorContext,
3326 "xmlAddChild : parent == NULL\n");
3327#endif
3328 return(NULL);
3329 }
3330
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003331 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003332#ifdef DEBUG_TREE
3333 xmlGenericError(xmlGenericErrorContext,
3334 "xmlAddChild : child == NULL\n");
3335#endif
3336 return(NULL);
3337 }
3338
Rob Richards19dc9612005-10-28 16:15:16 +00003339 if (parent == cur) {
3340#ifdef DEBUG_TREE
3341 xmlGenericError(xmlGenericErrorContext,
3342 "xmlAddChild : parent == cur\n");
3343#endif
3344 return(NULL);
3345 }
Owen Taylor3473f882001-02-23 17:55:21 +00003346 /*
3347 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003348 * cur is then freed.
3349 */
3350 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003351 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003352 (parent->content != NULL) &&
Rob Richards19dc9612005-10-28 16:15:16 +00003353 (parent->name == cur->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003354 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003355 xmlFreeNode(cur);
3356 return(parent);
3357 }
3358 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003359 (parent->last->name == cur->name) &&
3360 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003361 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003362 xmlFreeNode(cur);
3363 return(parent->last);
3364 }
3365 }
3366
3367 /*
3368 * add the new element at the end of the children list.
3369 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003370 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003371 cur->parent = parent;
3372 if (cur->doc != parent->doc) {
3373 xmlSetTreeDoc(cur, parent->doc);
3374 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003375 /* this check prevents a loop on tree-traversions if a developer
3376 * tries to add a node to its parent multiple times
3377 */
3378 if (prev == parent)
3379 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003380
3381 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003382 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003383 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003384 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003385 (parent->content != NULL) &&
3386 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003387 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003388 xmlFreeNode(cur);
3389 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003390 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003391 if (cur->type == XML_ATTRIBUTE_NODE) {
Rob Richards19dc9612005-10-28 16:15:16 +00003392 if (parent->type != XML_ELEMENT_NODE)
3393 return(NULL);
Rob Richards810a78b2008-12-31 22:13:57 +00003394 if (parent->properties != NULL) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003395 /* check if an attribute with the same name exists */
3396 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003397
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003398 if (cur->ns == NULL)
Rob Richardsc342ec62005-10-25 00:10:12 +00003399 lastattr = xmlHasNsProp(parent, cur->name, NULL);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003400 else
3401 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
Rob Richardsc342ec62005-10-25 00:10:12 +00003402 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003403 /* different instance, destroy it (attributes must be unique) */
Rob Richards19dc9612005-10-28 16:15:16 +00003404 xmlUnlinkNode((xmlNodePtr) lastattr);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003405 xmlFreeProp(lastattr);
3406 }
Rob Richards19dc9612005-10-28 16:15:16 +00003407 if (lastattr == (xmlAttrPtr) cur)
3408 return(cur);
Rob Richards810a78b2008-12-31 22:13:57 +00003409
3410 }
3411 if (parent->properties == NULL) {
3412 parent->properties = (xmlAttrPtr) cur;
3413 } else {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003414 /* find the end */
Rob Richards810a78b2008-12-31 22:13:57 +00003415 xmlAttrPtr lastattr = parent->properties;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003416 while (lastattr->next != NULL) {
3417 lastattr = lastattr->next;
3418 }
3419 lastattr->next = (xmlAttrPtr) cur;
3420 ((xmlAttrPtr) cur)->prev = lastattr;
3421 }
3422 } else {
3423 if (parent->children == NULL) {
3424 parent->children = cur;
3425 parent->last = cur;
3426 } else {
3427 prev = parent->last;
3428 prev->next = cur;
3429 cur->prev = prev;
3430 parent->last = cur;
3431 }
3432 }
Owen Taylor3473f882001-02-23 17:55:21 +00003433 return(cur);
3434}
3435
3436/**
3437 * xmlGetLastChild:
3438 * @parent: the parent node
3439 *
3440 * Search the last child of a node.
3441 * Returns the last child or NULL if none.
3442 */
3443xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08003444xmlGetLastChild(const xmlNode *parent) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003445 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003446#ifdef DEBUG_TREE
3447 xmlGenericError(xmlGenericErrorContext,
3448 "xmlGetLastChild : parent == NULL\n");
3449#endif
3450 return(NULL);
3451 }
3452 return(parent->last);
3453}
3454
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003455#ifdef LIBXML_TREE_ENABLED
3456/*
3457 * 5 interfaces from DOM ElementTraversal
3458 */
3459
3460/**
3461 * xmlChildElementCount:
3462 * @parent: the parent node
3463 *
3464 * Finds the current number of child nodes of that element which are
3465 * element nodes.
3466 * Note the handling of entities references is different than in
3467 * the W3C DOM element traversal spec since we don't have back reference
3468 * from entities content to entities references.
3469 *
3470 * Returns the count of element child or 0 if not available
3471 */
3472unsigned long
3473xmlChildElementCount(xmlNodePtr parent) {
3474 unsigned long ret = 0;
3475 xmlNodePtr cur = NULL;
3476
3477 if (parent == NULL)
3478 return(0);
3479 switch (parent->type) {
3480 case XML_ELEMENT_NODE:
3481 case XML_ENTITY_NODE:
3482 case XML_DOCUMENT_NODE:
Kyle VanderBeek1db99692014-07-29 00:32:15 -07003483 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003484 case XML_HTML_DOCUMENT_NODE:
3485 cur = parent->children;
3486 break;
3487 default:
3488 return(0);
3489 }
3490 while (cur != NULL) {
3491 if (cur->type == XML_ELEMENT_NODE)
3492 ret++;
3493 cur = cur->next;
3494 }
3495 return(ret);
3496}
3497
3498/**
3499 * xmlFirstElementChild:
3500 * @parent: the parent node
3501 *
3502 * Finds the first child node of that element which is a Element node
3503 * Note the handling of entities references is different than in
3504 * the W3C DOM element traversal spec since we don't have back reference
3505 * from entities content to entities references.
3506 *
3507 * Returns the first element child or NULL if not available
3508 */
3509xmlNodePtr
3510xmlFirstElementChild(xmlNodePtr parent) {
3511 xmlNodePtr cur = NULL;
3512
3513 if (parent == NULL)
3514 return(NULL);
3515 switch (parent->type) {
3516 case XML_ELEMENT_NODE:
3517 case XML_ENTITY_NODE:
3518 case XML_DOCUMENT_NODE:
Kyle VanderBeek1db99692014-07-29 00:32:15 -07003519 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003520 case XML_HTML_DOCUMENT_NODE:
3521 cur = parent->children;
3522 break;
3523 default:
3524 return(NULL);
3525 }
3526 while (cur != NULL) {
3527 if (cur->type == XML_ELEMENT_NODE)
3528 return(cur);
3529 cur = cur->next;
3530 }
3531 return(NULL);
3532}
3533
3534/**
3535 * xmlLastElementChild:
3536 * @parent: the parent node
3537 *
3538 * Finds the last child node of that element which is a Element node
3539 * Note the handling of entities references is different than in
3540 * the W3C DOM element traversal spec since we don't have back reference
3541 * from entities content to entities references.
3542 *
3543 * Returns the last element child or NULL if not available
3544 */
3545xmlNodePtr
3546xmlLastElementChild(xmlNodePtr parent) {
3547 xmlNodePtr cur = NULL;
3548
3549 if (parent == NULL)
3550 return(NULL);
3551 switch (parent->type) {
3552 case XML_ELEMENT_NODE:
3553 case XML_ENTITY_NODE:
3554 case XML_DOCUMENT_NODE:
Kyle VanderBeek1db99692014-07-29 00:32:15 -07003555 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003556 case XML_HTML_DOCUMENT_NODE:
3557 cur = parent->last;
3558 break;
3559 default:
3560 return(NULL);
3561 }
3562 while (cur != NULL) {
3563 if (cur->type == XML_ELEMENT_NODE)
3564 return(cur);
3565 cur = cur->prev;
3566 }
3567 return(NULL);
3568}
3569
3570/**
3571 * xmlPreviousElementSibling:
3572 * @node: the current node
3573 *
3574 * Finds the first closest previous sibling of the node which is an
3575 * element node.
3576 * Note the handling of entities references is different than in
3577 * the W3C DOM element traversal spec since we don't have back reference
3578 * from entities content to entities references.
3579 *
3580 * Returns the previous element sibling or NULL if not available
3581 */
3582xmlNodePtr
3583xmlPreviousElementSibling(xmlNodePtr node) {
3584 if (node == NULL)
3585 return(NULL);
3586 switch (node->type) {
3587 case XML_ELEMENT_NODE:
3588 case XML_TEXT_NODE:
3589 case XML_CDATA_SECTION_NODE:
3590 case XML_ENTITY_REF_NODE:
3591 case XML_ENTITY_NODE:
3592 case XML_PI_NODE:
3593 case XML_COMMENT_NODE:
3594 case XML_XINCLUDE_START:
3595 case XML_XINCLUDE_END:
3596 node = node->prev;
3597 break;
3598 default:
3599 return(NULL);
3600 }
3601 while (node != NULL) {
3602 if (node->type == XML_ELEMENT_NODE)
3603 return(node);
François Delyon2f700902010-02-03 17:32:37 +01003604 node = node->prev;
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003605 }
3606 return(NULL);
3607}
3608
3609/**
3610 * xmlNextElementSibling:
3611 * @node: the current node
3612 *
3613 * Finds the first closest next sibling of the node which is an
3614 * element node.
3615 * Note the handling of entities references is different than in
3616 * the W3C DOM element traversal spec since we don't have back reference
3617 * from entities content to entities references.
3618 *
3619 * Returns the next element sibling or NULL if not available
3620 */
3621xmlNodePtr
3622xmlNextElementSibling(xmlNodePtr node) {
3623 if (node == NULL)
3624 return(NULL);
3625 switch (node->type) {
3626 case XML_ELEMENT_NODE:
3627 case XML_TEXT_NODE:
3628 case XML_CDATA_SECTION_NODE:
3629 case XML_ENTITY_REF_NODE:
3630 case XML_ENTITY_NODE:
3631 case XML_PI_NODE:
3632 case XML_COMMENT_NODE:
3633 case XML_DTD_NODE:
3634 case XML_XINCLUDE_START:
3635 case XML_XINCLUDE_END:
3636 node = node->next;
3637 break;
3638 default:
3639 return(NULL);
3640 }
3641 while (node != NULL) {
3642 if (node->type == XML_ELEMENT_NODE)
3643 return(node);
3644 node = node->next;
3645 }
3646 return(NULL);
3647}
3648
3649#endif /* LIBXML_TREE_ENABLED */
3650
Owen Taylor3473f882001-02-23 17:55:21 +00003651/**
3652 * xmlFreeNodeList:
3653 * @cur: the first node in the list
3654 *
3655 * Free a node and all its siblings, this is a recursive behaviour, all
3656 * the children are freed too.
3657 */
3658void
3659xmlFreeNodeList(xmlNodePtr cur) {
3660 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003661 xmlDictPtr dict = NULL;
3662
3663 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003664 if (cur->type == XML_NAMESPACE_DECL) {
3665 xmlFreeNsList((xmlNsPtr) cur);
3666 return;
3667 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003668 if ((cur->type == XML_DOCUMENT_NODE) ||
3669#ifdef LIBXML_DOCB_ENABLED
3670 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003671#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003672 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003673 xmlFreeDoc((xmlDocPtr) cur);
3674 return;
3675 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003676 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003677 while (cur != NULL) {
3678 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003679 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003680
Daniel Veillarda880b122003-04-21 21:36:41 +00003681 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003682 xmlDeregisterNodeDefaultValue(cur);
3683
Daniel Veillard02141ea2001-04-30 11:46:40 +00003684 if ((cur->children != NULL) &&
3685 (cur->type != XML_ENTITY_REF_NODE))
3686 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003687 if (((cur->type == XML_ELEMENT_NODE) ||
3688 (cur->type == XML_XINCLUDE_START) ||
3689 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003690 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003691 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003692 if ((cur->type != XML_ELEMENT_NODE) &&
3693 (cur->type != XML_XINCLUDE_START) &&
3694 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003695 (cur->type != XML_ENTITY_REF_NODE) &&
3696 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003697 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003698 }
3699 if (((cur->type == XML_ELEMENT_NODE) ||
3700 (cur->type == XML_XINCLUDE_START) ||
3701 (cur->type == XML_XINCLUDE_END)) &&
3702 (cur->nsDef != NULL))
3703 xmlFreeNsList(cur->nsDef);
3704
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003705 /*
3706 * When a node is a text node or a comment, it uses a global static
3707 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003708 * Otherwise the node name might come from the document's
Jan Pokornýbb654fe2016-04-13 16:56:07 +02003709 * dictionary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003710 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003711 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003712 (cur->type != XML_TEXT_NODE) &&
3713 (cur->type != XML_COMMENT_NODE))
3714 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003715 xmlFree(cur);
3716 }
Owen Taylor3473f882001-02-23 17:55:21 +00003717 cur = next;
3718 }
3719}
3720
3721/**
3722 * xmlFreeNode:
3723 * @cur: the node
3724 *
3725 * Free a node, this is a recursive behaviour, all the children are freed too.
3726 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3727 */
3728void
3729xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003730 xmlDictPtr dict = NULL;
3731
3732 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003733
Daniel Veillard02141ea2001-04-30 11:46:40 +00003734 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003735 if (cur->type == XML_DTD_NODE) {
3736 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003737 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003738 }
3739 if (cur->type == XML_NAMESPACE_DECL) {
3740 xmlFreeNs((xmlNsPtr) cur);
3741 return;
3742 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003743 if (cur->type == XML_ATTRIBUTE_NODE) {
3744 xmlFreeProp((xmlAttrPtr) cur);
3745 return;
3746 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003747
Daniel Veillarda880b122003-04-21 21:36:41 +00003748 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003749 xmlDeregisterNodeDefaultValue(cur);
3750
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003751 if (cur->doc != NULL) dict = cur->doc->dict;
3752
Daniel Veillard2cba4152008-08-27 11:45:41 +00003753 if (cur->type == XML_ENTITY_DECL) {
3754 xmlEntityPtr ent = (xmlEntityPtr) cur;
3755 DICT_FREE(ent->SystemID);
3756 DICT_FREE(ent->ExternalID);
3757 }
Owen Taylor3473f882001-02-23 17:55:21 +00003758 if ((cur->children != NULL) &&
3759 (cur->type != XML_ENTITY_REF_NODE))
3760 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003761 if (((cur->type == XML_ELEMENT_NODE) ||
3762 (cur->type == XML_XINCLUDE_START) ||
3763 (cur->type == XML_XINCLUDE_END)) &&
3764 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003765 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003766 if ((cur->type != XML_ELEMENT_NODE) &&
3767 (cur->content != NULL) &&
3768 (cur->type != XML_ENTITY_REF_NODE) &&
3769 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003770 (cur->type != XML_XINCLUDE_START) &&
3771 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003772 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003773 }
3774
Daniel Veillardacd370f2001-06-09 17:17:51 +00003775 /*
3776 * When a node is a text node or a comment, it uses a global static
3777 * variable for the name of the node.
Jan Pokornýbb654fe2016-04-13 16:56:07 +02003778 * Otherwise the node name might come from the document's dictionary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003779 */
Owen Taylor3473f882001-02-23 17:55:21 +00003780 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003781 (cur->type != XML_TEXT_NODE) &&
3782 (cur->type != XML_COMMENT_NODE))
3783 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003784
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003785 if (((cur->type == XML_ELEMENT_NODE) ||
3786 (cur->type == XML_XINCLUDE_START) ||
3787 (cur->type == XML_XINCLUDE_END)) &&
3788 (cur->nsDef != NULL))
3789 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003790 xmlFree(cur);
3791}
3792
3793/**
3794 * xmlUnlinkNode:
3795 * @cur: the node
3796 *
3797 * Unlink a node from it's current context, the node is not freed
Daniel Veillard39d027c2012-05-11 12:38:23 +08003798 * If one need to free the node, use xmlFreeNode() routine after the
3799 * unlink to discard it.
Daniel Veillard6ca24a32012-08-08 15:31:55 +08003800 * Note that namespace nodes can't be unlinked as they do not have
3801 * pointer to their parent.
Owen Taylor3473f882001-02-23 17:55:21 +00003802 */
3803void
3804xmlUnlinkNode(xmlNodePtr cur) {
3805 if (cur == NULL) {
3806#ifdef DEBUG_TREE
3807 xmlGenericError(xmlGenericErrorContext,
3808 "xmlUnlinkNode : node == NULL\n");
3809#endif
3810 return;
3811 }
Daniel Veillard6ca24a32012-08-08 15:31:55 +08003812 if (cur->type == XML_NAMESPACE_DECL)
3813 return;
Daniel Veillard29e43992001-12-13 22:21:58 +00003814 if (cur->type == XML_DTD_NODE) {
3815 xmlDocPtr doc;
3816 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003817 if (doc != NULL) {
3818 if (doc->intSubset == (xmlDtdPtr) cur)
3819 doc->intSubset = NULL;
3820 if (doc->extSubset == (xmlDtdPtr) cur)
3821 doc->extSubset = NULL;
3822 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003823 }
Daniel Veillard2cba4152008-08-27 11:45:41 +00003824 if (cur->type == XML_ENTITY_DECL) {
3825 xmlDocPtr doc;
3826 doc = cur->doc;
3827 if (doc != NULL) {
3828 if (doc->intSubset != NULL) {
3829 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3830 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3831 NULL);
3832 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3833 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3834 NULL);
3835 }
3836 if (doc->extSubset != NULL) {
3837 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3838 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3839 NULL);
3840 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3841 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3842 NULL);
3843 }
3844 }
3845 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003846 if (cur->parent != NULL) {
3847 xmlNodePtr parent;
3848 parent = cur->parent;
3849 if (cur->type == XML_ATTRIBUTE_NODE) {
3850 if (parent->properties == (xmlAttrPtr) cur)
3851 parent->properties = ((xmlAttrPtr) cur)->next;
3852 } else {
3853 if (parent->children == cur)
3854 parent->children = cur->next;
3855 if (parent->last == cur)
3856 parent->last = cur->prev;
3857 }
3858 cur->parent = NULL;
3859 }
Owen Taylor3473f882001-02-23 17:55:21 +00003860 if (cur->next != NULL)
3861 cur->next->prev = cur->prev;
3862 if (cur->prev != NULL)
3863 cur->prev->next = cur->next;
3864 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003865}
3866
Daniel Veillard2156d432004-03-04 15:59:36 +00003867#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003868/**
3869 * xmlReplaceNode:
3870 * @old: the old node
3871 * @cur: the node
3872 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00003873 * Unlink the old node from its current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003874 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003875 * first unlinked from its existing context.
3876 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003877 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003878 */
3879xmlNodePtr
3880xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003881 if (old == cur) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003882 if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
3883 (old->parent == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003884#ifdef DEBUG_TREE
3885 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda03e3652004-11-02 18:45:30 +00003886 "xmlReplaceNode : old == NULL or without parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003887#endif
3888 return(NULL);
3889 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003890 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003891 xmlUnlinkNode(old);
3892 return(old);
3893 }
3894 if (cur == old) {
3895 return(old);
3896 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003897 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3898#ifdef DEBUG_TREE
3899 xmlGenericError(xmlGenericErrorContext,
3900 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3901#endif
3902 return(old);
3903 }
3904 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3905#ifdef DEBUG_TREE
3906 xmlGenericError(xmlGenericErrorContext,
3907 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3908#endif
3909 return(old);
3910 }
Owen Taylor3473f882001-02-23 17:55:21 +00003911 xmlUnlinkNode(cur);
Daniel Veillard64d7d122005-05-11 18:03:42 +00003912 xmlSetTreeDoc(cur, old->doc);
Owen Taylor3473f882001-02-23 17:55:21 +00003913 cur->parent = old->parent;
3914 cur->next = old->next;
3915 if (cur->next != NULL)
3916 cur->next->prev = cur;
3917 cur->prev = old->prev;
3918 if (cur->prev != NULL)
3919 cur->prev->next = cur;
3920 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003921 if (cur->type == XML_ATTRIBUTE_NODE) {
3922 if (cur->parent->properties == (xmlAttrPtr)old)
3923 cur->parent->properties = ((xmlAttrPtr) cur);
3924 } else {
3925 if (cur->parent->children == old)
3926 cur->parent->children = cur;
3927 if (cur->parent->last == old)
3928 cur->parent->last = cur;
3929 }
Owen Taylor3473f882001-02-23 17:55:21 +00003930 }
3931 old->next = old->prev = NULL;
3932 old->parent = NULL;
3933 return(old);
3934}
Daniel Veillard652327a2003-09-29 18:02:38 +00003935#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003936
3937/************************************************************************
3938 * *
3939 * Copy operations *
3940 * *
3941 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00003942
Owen Taylor3473f882001-02-23 17:55:21 +00003943/**
3944 * xmlCopyNamespace:
3945 * @cur: the namespace
3946 *
3947 * Do a copy of the namespace.
3948 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003949 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003950 */
3951xmlNsPtr
3952xmlCopyNamespace(xmlNsPtr cur) {
3953 xmlNsPtr ret;
3954
3955 if (cur == NULL) return(NULL);
3956 switch (cur->type) {
3957 case XML_LOCAL_NAMESPACE:
3958 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3959 break;
3960 default:
3961#ifdef DEBUG_TREE
3962 xmlGenericError(xmlGenericErrorContext,
3963 "xmlCopyNamespace: invalid type %d\n", cur->type);
3964#endif
3965 return(NULL);
3966 }
3967 return(ret);
3968}
3969
3970/**
3971 * xmlCopyNamespaceList:
3972 * @cur: the first namespace
3973 *
3974 * Do a copy of an namespace list.
3975 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003976 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003977 */
3978xmlNsPtr
3979xmlCopyNamespaceList(xmlNsPtr cur) {
3980 xmlNsPtr ret = NULL;
3981 xmlNsPtr p = NULL,q;
3982
3983 while (cur != NULL) {
3984 q = xmlCopyNamespace(cur);
3985 if (p == NULL) {
3986 ret = p = q;
3987 } else {
3988 p->next = q;
3989 p = q;
3990 }
3991 cur = cur->next;
3992 }
3993 return(ret);
3994}
3995
3996static xmlNodePtr
3997xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
Rob Richards19dc9612005-10-28 16:15:16 +00003998
3999static xmlAttrPtr
4000xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00004001 xmlAttrPtr ret;
4002
4003 if (cur == NULL) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004004 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4005 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004006 if (target != NULL)
4007 ret = xmlNewDocProp(target->doc, cur->name, NULL);
Rob Richards19dc9612005-10-28 16:15:16 +00004008 else if (doc != NULL)
4009 ret = xmlNewDocProp(doc, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004010 else if (cur->parent != NULL)
4011 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
4012 else if (cur->children != NULL)
4013 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
4014 else
4015 ret = xmlNewDocProp(NULL, cur->name, NULL);
4016 if (ret == NULL) return(NULL);
4017 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00004018
Owen Taylor3473f882001-02-23 17:55:21 +00004019 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00004020 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00004021
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00004022 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
4023 if (ns == NULL) {
4024 /*
4025 * Humm, we are copying an element whose namespace is defined
4026 * out of the new tree scope. Search it in the original tree
4027 * and add it at the top of the new tree
4028 */
4029 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
4030 if (ns != NULL) {
4031 xmlNodePtr root = target;
4032 xmlNodePtr pred = NULL;
4033
4034 while (root->parent != NULL) {
4035 pred = root;
4036 root = root->parent;
4037 }
4038 if (root == (xmlNodePtr) target->doc) {
4039 /* correct possibly cycling above the document elt */
4040 root = pred;
4041 }
4042 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4043 }
4044 } else {
4045 /*
4046 * we have to find something appropriate here since
4047 * we cant be sure, that the namespce we found is identified
4048 * by the prefix
4049 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00004050 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00004051 /* this is the nice case */
4052 ret->ns = ns;
4053 } else {
4054 /*
4055 * we are in trouble: we need a new reconcilied namespace.
4056 * This is expensive
4057 */
4058 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
4059 }
4060 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004061
Owen Taylor3473f882001-02-23 17:55:21 +00004062 } else
4063 ret->ns = NULL;
4064
4065 if (cur->children != NULL) {
4066 xmlNodePtr tmp;
4067
4068 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
4069 ret->last = NULL;
4070 tmp = ret->children;
4071 while (tmp != NULL) {
4072 /* tmp->parent = (xmlNodePtr)ret; */
4073 if (tmp->next == NULL)
4074 ret->last = tmp;
4075 tmp = tmp->next;
4076 }
4077 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00004078 /*
4079 * Try to handle IDs
4080 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00004081 if ((target!= NULL) && (cur!= NULL) &&
4082 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00004083 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4084 if (xmlIsID(cur->doc, cur->parent, cur)) {
4085 xmlChar *id;
4086
4087 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4088 if (id != NULL) {
4089 xmlAddID(NULL, target->doc, id, ret);
4090 xmlFree(id);
4091 }
4092 }
4093 }
Owen Taylor3473f882001-02-23 17:55:21 +00004094 return(ret);
4095}
4096
4097/**
Rob Richards19dc9612005-10-28 16:15:16 +00004098 * xmlCopyProp:
4099 * @target: the element where the attribute will be grafted
4100 * @cur: the attribute
4101 *
4102 * Do a copy of the attribute.
4103 *
4104 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4105 */
4106xmlAttrPtr
4107xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4108 return xmlCopyPropInternal(NULL, target, cur);
4109}
4110
4111/**
Owen Taylor3473f882001-02-23 17:55:21 +00004112 * xmlCopyPropList:
4113 * @target: the element where the attributes will be grafted
4114 * @cur: the first attribute
4115 *
4116 * Do a copy of an attribute list.
4117 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004118 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004119 */
4120xmlAttrPtr
4121xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4122 xmlAttrPtr ret = NULL;
4123 xmlAttrPtr p = NULL,q;
4124
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004125 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4126 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004127 while (cur != NULL) {
4128 q = xmlCopyProp(target, cur);
William M. Brack13dfa872004-09-18 04:52:08 +00004129 if (q == NULL)
4130 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004131 if (p == NULL) {
4132 ret = p = q;
4133 } else {
4134 p->next = q;
4135 q->prev = p;
4136 p = q;
4137 }
4138 cur = cur->next;
4139 }
4140 return(ret);
4141}
4142
4143/*
Daniel Veillardd1640922001-12-17 15:30:10 +00004144 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00004145 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004146 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00004147 * tricky reason: namespaces. Doing a direct copy of a node
4148 * say RPM:Copyright without changing the namespace pointer to
4149 * something else can produce stale links. One way to do it is
4150 * to keep a reference counter but this doesn't work as soon
4151 * as one move the element or the subtree out of the scope of
4152 * the existing namespace. The actual solution seems to add
4153 * a copy of the namespace at the top of the copied tree if
4154 * not available in the subtree.
4155 * Hence two functions, the public front-end call the inner ones
William M. Brack57e9e912004-03-09 16:19:02 +00004156 * The argument "recursive" normally indicates a recursive copy
4157 * of the node with values 0 (no) and 1 (yes). For XInclude,
4158 * however, we allow a value of 2 to indicate copy properties and
4159 * namespace info, but don't recurse on children.
Owen Taylor3473f882001-02-23 17:55:21 +00004160 */
4161
4162static xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004163xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
William M. Brack57e9e912004-03-09 16:19:02 +00004164 int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004165 xmlNodePtr ret;
4166
4167 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00004168 switch (node->type) {
4169 case XML_TEXT_NODE:
4170 case XML_CDATA_SECTION_NODE:
4171 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00004172 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004173 case XML_ENTITY_REF_NODE:
4174 case XML_ENTITY_NODE:
4175 case XML_PI_NODE:
4176 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004177 case XML_XINCLUDE_START:
4178 case XML_XINCLUDE_END:
4179 break;
4180 case XML_ATTRIBUTE_NODE:
Rob Richards19dc9612005-10-28 16:15:16 +00004181 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004182 case XML_NAMESPACE_DECL:
4183 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
Daniel Veillardaa6de472008-08-25 14:53:31 +00004184
Daniel Veillard39196eb2001-06-19 18:09:42 +00004185 case XML_DOCUMENT_NODE:
4186 case XML_HTML_DOCUMENT_NODE:
4187#ifdef LIBXML_DOCB_ENABLED
4188 case XML_DOCB_DOCUMENT_NODE:
4189#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00004190#ifdef LIBXML_TREE_ENABLED
William M. Brack57e9e912004-03-09 16:19:02 +00004191 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
Daniel Veillard652327a2003-09-29 18:02:38 +00004192#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00004193 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004194 case XML_NOTATION_NODE:
4195 case XML_DTD_NODE:
4196 case XML_ELEMENT_DECL:
4197 case XML_ATTRIBUTE_DECL:
4198 case XML_ENTITY_DECL:
4199 return(NULL);
4200 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00004201
Owen Taylor3473f882001-02-23 17:55:21 +00004202 /*
4203 * Allocate a new node and fill the fields.
4204 */
4205 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4206 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004207 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00004208 return(NULL);
4209 }
4210 memset(ret, 0, sizeof(xmlNode));
4211 ret->type = node->type;
4212
4213 ret->doc = doc;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004214 ret->parent = parent;
Owen Taylor3473f882001-02-23 17:55:21 +00004215 if (node->name == xmlStringText)
4216 ret->name = xmlStringText;
4217 else if (node->name == xmlStringTextNoenc)
4218 ret->name = xmlStringTextNoenc;
4219 else if (node->name == xmlStringComment)
4220 ret->name = xmlStringComment;
Daniel Veillard03a53c32004-10-26 16:06:51 +00004221 else if (node->name != NULL) {
4222 if ((doc != NULL) && (doc->dict != NULL))
4223 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4224 else
4225 ret->name = xmlStrdup(node->name);
4226 }
Daniel Veillard7db37732001-07-12 01:20:08 +00004227 if ((node->type != XML_ELEMENT_NODE) &&
4228 (node->content != NULL) &&
4229 (node->type != XML_ENTITY_REF_NODE) &&
4230 (node->type != XML_XINCLUDE_END) &&
4231 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004232 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00004233 }else{
4234 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004235 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00004236 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004237 if (parent != NULL) {
4238 xmlNodePtr tmp;
4239
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004240 /*
4241 * this is a tricky part for the node register thing:
4242 * in case ret does get coalesced in xmlAddChild
4243 * the deregister-node callback is called; so we register ret now already
4244 */
Daniel Veillarda880b122003-04-21 21:36:41 +00004245 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004246 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4247
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004248 tmp = xmlAddChild(parent, ret);
4249 /* node could have coalesced */
4250 if (tmp != ret)
4251 return(tmp);
4252 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004253
William M. Brack57e9e912004-03-09 16:19:02 +00004254 if (!extended)
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004255 goto out;
Petr Pajas2afca4a2009-07-30 17:47:32 +02004256 if (((node->type == XML_ELEMENT_NODE) ||
4257 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004258 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4259
4260 if (node->ns != NULL) {
4261 xmlNsPtr ns;
4262
4263 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4264 if (ns == NULL) {
4265 /*
4266 * Humm, we are copying an element whose namespace is defined
4267 * out of the new tree scope. Search it in the original tree
4268 * and add it at the top of the new tree
4269 */
4270 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4271 if (ns != NULL) {
4272 xmlNodePtr root = ret;
4273
4274 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00004275 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Rob Richardsddb01cb2010-01-29 13:32:12 -05004276 } else {
4277 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
Owen Taylor3473f882001-02-23 17:55:21 +00004278 }
4279 } else {
4280 /*
4281 * reference the existing namespace definition in our own tree.
4282 */
4283 ret->ns = ns;
4284 }
4285 }
Petr Pajas2afca4a2009-07-30 17:47:32 +02004286 if (((node->type == XML_ELEMENT_NODE) ||
4287 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004288 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004289 if (node->type == XML_ENTITY_REF_NODE) {
4290 if ((doc == NULL) || (node->doc != doc)) {
4291 /*
4292 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00004293 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00004294 * we cannot keep the reference. Try to find it in the
4295 * target document.
4296 */
4297 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4298 } else {
4299 ret->children = node->children;
4300 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00004301 ret->last = ret->children;
William M. Brack57e9e912004-03-09 16:19:02 +00004302 } else if ((node->children != NULL) && (extended != 2)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004303 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00004304 UPDATE_LAST_CHILD_AND_PARENT(ret)
4305 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004306
4307out:
4308 /* if parent != NULL we already registered the node above */
Daniel Veillardac996a12004-07-30 12:02:58 +00004309 if ((parent == NULL) &&
4310 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004311 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004312 return(ret);
4313}
4314
4315static xmlNodePtr
4316xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4317 xmlNodePtr ret = NULL;
4318 xmlNodePtr p = NULL,q;
4319
4320 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00004321#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004322 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00004323 if (doc == NULL) {
4324 node = node->next;
4325 continue;
4326 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00004327 if (doc->intSubset == NULL) {
4328 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
Gaurav98a4e712013-11-29 23:28:21 +08004329 if (q == NULL) return(NULL);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004330 q->doc = doc;
4331 q->parent = parent;
4332 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004333 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004334 } else {
4335 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004336 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004337 }
4338 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00004339#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00004340 q = xmlStaticCopyNode(node, doc, parent, 1);
Gaurav98a4e712013-11-29 23:28:21 +08004341 if (q == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004342 if (ret == NULL) {
4343 q->prev = NULL;
4344 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004345 } else if (p != q) {
4346 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00004347 p->next = q;
4348 q->prev = p;
4349 p = q;
4350 }
4351 node = node->next;
4352 }
4353 return(ret);
4354}
4355
4356/**
4357 * xmlCopyNode:
4358 * @node: the node
William M. Brack57e9e912004-03-09 16:19:02 +00004359 * @extended: if 1 do a recursive copy (properties, namespaces and children
4360 * when applicable)
4361 * if 2 copy properties and namespaces (when applicable)
Owen Taylor3473f882001-02-23 17:55:21 +00004362 *
4363 * Do a copy of the node.
4364 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004365 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004366 */
4367xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004368xmlCopyNode(xmlNodePtr node, int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004369 xmlNodePtr ret;
4370
William M. Brack57e9e912004-03-09 16:19:02 +00004371 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
Owen Taylor3473f882001-02-23 17:55:21 +00004372 return(ret);
4373}
4374
4375/**
Daniel Veillard82daa812001-04-12 08:55:36 +00004376 * xmlDocCopyNode:
4377 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00004378 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004379 * @extended: if 1 do a recursive copy (properties, namespaces and children
4380 * when applicable)
4381 * if 2 copy properties and namespaces (when applicable)
Daniel Veillard82daa812001-04-12 08:55:36 +00004382 *
4383 * Do a copy of the node to a given document.
4384 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004385 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00004386 */
4387xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004388xmlDocCopyNode(xmlNodePtr node, xmlDocPtr doc, int extended) {
Daniel Veillard82daa812001-04-12 08:55:36 +00004389 xmlNodePtr ret;
4390
William M. Brack57e9e912004-03-09 16:19:02 +00004391 ret = xmlStaticCopyNode(node, doc, NULL, extended);
Daniel Veillard82daa812001-04-12 08:55:36 +00004392 return(ret);
4393}
4394
4395/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00004396 * xmlDocCopyNodeList:
4397 * @doc: the target document
4398 * @node: the first node in the list.
4399 *
4400 * Do a recursive copy of the node list.
4401 *
4402 * Returns: a new #xmlNodePtr, or NULL in case of error.
4403 */
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004404xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, xmlNodePtr node) {
Daniel Veillard03a53c32004-10-26 16:06:51 +00004405 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4406 return(ret);
4407}
4408
4409/**
Owen Taylor3473f882001-02-23 17:55:21 +00004410 * xmlCopyNodeList:
4411 * @node: the first node in the list.
4412 *
4413 * Do a recursive copy of the node list.
Daniel Veillard03a53c32004-10-26 16:06:51 +00004414 * Use xmlDocCopyNodeList() if possible to ensure string interning.
Owen Taylor3473f882001-02-23 17:55:21 +00004415 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004416 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004417 */
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004418xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00004419 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4420 return(ret);
4421}
4422
Daniel Veillard2156d432004-03-04 15:59:36 +00004423#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004424/**
Owen Taylor3473f882001-02-23 17:55:21 +00004425 * xmlCopyDtd:
4426 * @dtd: the dtd
4427 *
4428 * Do a copy of the dtd.
4429 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004430 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004431 */
4432xmlDtdPtr
4433xmlCopyDtd(xmlDtdPtr dtd) {
4434 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004435 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00004436
4437 if (dtd == NULL) return(NULL);
4438 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4439 if (ret == NULL) return(NULL);
4440 if (dtd->entities != NULL)
4441 ret->entities = (void *) xmlCopyEntitiesTable(
4442 (xmlEntitiesTablePtr) dtd->entities);
4443 if (dtd->notations != NULL)
4444 ret->notations = (void *) xmlCopyNotationTable(
4445 (xmlNotationTablePtr) dtd->notations);
4446 if (dtd->elements != NULL)
4447 ret->elements = (void *) xmlCopyElementTable(
4448 (xmlElementTablePtr) dtd->elements);
4449 if (dtd->attributes != NULL)
4450 ret->attributes = (void *) xmlCopyAttributeTable(
4451 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004452 if (dtd->pentities != NULL)
4453 ret->pentities = (void *) xmlCopyEntitiesTable(
4454 (xmlEntitiesTablePtr) dtd->pentities);
Daniel Veillardaa6de472008-08-25 14:53:31 +00004455
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004456 cur = dtd->children;
4457 while (cur != NULL) {
4458 q = NULL;
4459
4460 if (cur->type == XML_ENTITY_DECL) {
4461 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4462 switch (tmp->etype) {
4463 case XML_INTERNAL_GENERAL_ENTITY:
4464 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4465 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4466 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4467 break;
4468 case XML_INTERNAL_PARAMETER_ENTITY:
4469 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardaa6de472008-08-25 14:53:31 +00004470 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004471 xmlGetParameterEntityFromDtd(ret, tmp->name);
4472 break;
4473 case XML_INTERNAL_PREDEFINED_ENTITY:
4474 break;
4475 }
4476 } else if (cur->type == XML_ELEMENT_DECL) {
4477 xmlElementPtr tmp = (xmlElementPtr) cur;
4478 q = (xmlNodePtr)
4479 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4480 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4481 xmlAttributePtr tmp = (xmlAttributePtr) cur;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004482 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004483 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4484 } else if (cur->type == XML_COMMENT_NODE) {
4485 q = xmlCopyNode(cur, 0);
4486 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004487
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004488 if (q == NULL) {
4489 cur = cur->next;
4490 continue;
4491 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004492
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004493 if (p == NULL)
4494 ret->children = q;
4495 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00004496 p->next = q;
4497
4498 q->prev = p;
4499 q->parent = (xmlNodePtr) ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004500 q->next = NULL;
4501 ret->last = q;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004502 p = q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004503 cur = cur->next;
4504 }
4505
Owen Taylor3473f882001-02-23 17:55:21 +00004506 return(ret);
4507}
Daniel Veillard2156d432004-03-04 15:59:36 +00004508#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004509
Daniel Veillard2156d432004-03-04 15:59:36 +00004510#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004511/**
4512 * xmlCopyDoc:
4513 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004514 * @recursive: if not zero do a recursive copy.
Owen Taylor3473f882001-02-23 17:55:21 +00004515 *
4516 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004517 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004518 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004519 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004520 */
4521xmlDocPtr
4522xmlCopyDoc(xmlDocPtr doc, int recursive) {
4523 xmlDocPtr ret;
4524
4525 if (doc == NULL) return(NULL);
4526 ret = xmlNewDoc(doc->version);
4527 if (ret == NULL) return(NULL);
4528 if (doc->name != NULL)
4529 ret->name = xmlMemStrdup(doc->name);
4530 if (doc->encoding != NULL)
4531 ret->encoding = xmlStrdup(doc->encoding);
Daniel Veillardf59507d2005-01-27 17:26:49 +00004532 if (doc->URL != NULL)
4533 ret->URL = xmlStrdup(doc->URL);
Owen Taylor3473f882001-02-23 17:55:21 +00004534 ret->charset = doc->charset;
4535 ret->compression = doc->compression;
4536 ret->standalone = doc->standalone;
4537 if (!recursive) return(ret);
4538
Daniel Veillardb33c2012001-04-25 12:59:04 +00004539 ret->last = NULL;
4540 ret->children = NULL;
Daniel Veillard2156d432004-03-04 15:59:36 +00004541#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb33c2012001-04-25 12:59:04 +00004542 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004543 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard42870f42014-07-26 21:04:54 +08004544 if (ret->intSubset == NULL) {
4545 xmlFreeDoc(ret);
4546 return(NULL);
4547 }
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004548 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004549 ret->intSubset->parent = ret;
4550 }
Daniel Veillard2156d432004-03-04 15:59:36 +00004551#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004552 if (doc->oldNs != NULL)
4553 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4554 if (doc->children != NULL) {
4555 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004556
Daniel Veillardb33c2012001-04-25 12:59:04 +00004557 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4558 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004559 ret->last = NULL;
4560 tmp = ret->children;
4561 while (tmp != NULL) {
4562 if (tmp->next == NULL)
4563 ret->last = tmp;
4564 tmp = tmp->next;
4565 }
4566 }
4567 return(ret);
4568}
Daniel Veillard652327a2003-09-29 18:02:38 +00004569#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004570
4571/************************************************************************
4572 * *
4573 * Content access functions *
4574 * *
4575 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00004576
Owen Taylor3473f882001-02-23 17:55:21 +00004577/**
Daniel Veillard968a03a2012-08-13 12:41:33 +08004578 * xmlGetLineNoInternal:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004579 * @node: valid node
Daniel Veillard968a03a2012-08-13 12:41:33 +08004580 * @depth: used to limit any risk of recursion
Daniel Veillard8faa7832001-11-26 15:58:08 +00004581 *
Daniel Veillard968a03a2012-08-13 12:41:33 +08004582 * Get line number of @node.
4583 * Try to override the limitation of lines being store in 16 bits ints
Daniel Veillard8faa7832001-11-26 15:58:08 +00004584 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004585 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004586 */
Daniel Veillard968a03a2012-08-13 12:41:33 +08004587static long
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004588xmlGetLineNoInternal(const xmlNode *node, int depth)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004589{
4590 long result = -1;
4591
Daniel Veillard968a03a2012-08-13 12:41:33 +08004592 if (depth >= 5)
4593 return(-1);
4594
Daniel Veillard8faa7832001-11-26 15:58:08 +00004595 if (!node)
4596 return result;
Daniel Veillard73da77e2005-08-24 14:05:37 +00004597 if ((node->type == XML_ELEMENT_NODE) ||
4598 (node->type == XML_TEXT_NODE) ||
4599 (node->type == XML_COMMENT_NODE) ||
Daniel Veillard968a03a2012-08-13 12:41:33 +08004600 (node->type == XML_PI_NODE)) {
4601 if (node->line == 65535) {
4602 if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
4603 result = (long) node->psvi;
4604 else if ((node->type == XML_ELEMENT_NODE) &&
4605 (node->children != NULL))
4606 result = xmlGetLineNoInternal(node->children, depth + 1);
4607 else if (node->next != NULL)
4608 result = xmlGetLineNoInternal(node->next, depth + 1);
4609 else if (node->prev != NULL)
4610 result = xmlGetLineNoInternal(node->prev, depth + 1);
4611 }
4612 if ((result == -1) || (result == 65535))
4613 result = (long) node->line;
4614 } else if ((node->prev != NULL) &&
Daniel Veillard8faa7832001-11-26 15:58:08 +00004615 ((node->prev->type == XML_ELEMENT_NODE) ||
Daniel Veillard73da77e2005-08-24 14:05:37 +00004616 (node->prev->type == XML_TEXT_NODE) ||
4617 (node->prev->type == XML_COMMENT_NODE) ||
4618 (node->prev->type == XML_PI_NODE)))
Daniel Veillard968a03a2012-08-13 12:41:33 +08004619 result = xmlGetLineNoInternal(node->prev, depth + 1);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004620 else if ((node->parent != NULL) &&
Daniel Veillard73da77e2005-08-24 14:05:37 +00004621 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard968a03a2012-08-13 12:41:33 +08004622 result = xmlGetLineNoInternal(node->parent, depth + 1);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004623
4624 return result;
4625}
4626
Daniel Veillard968a03a2012-08-13 12:41:33 +08004627/**
4628 * xmlGetLineNo:
4629 * @node: valid node
4630 *
4631 * Get line number of @node.
4632 * Try to override the limitation of lines being store in 16 bits ints
4633 * if XML_PARSE_BIG_LINES parser option was used
4634 *
4635 * Returns the line number if successful, -1 otherwise
4636 */
4637long
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004638xmlGetLineNo(const xmlNode *node)
Daniel Veillard968a03a2012-08-13 12:41:33 +08004639{
4640 return(xmlGetLineNoInternal(node, 0));
4641}
4642
Daniel Veillard2156d432004-03-04 15:59:36 +00004643#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004644/**
4645 * xmlGetNodePath:
4646 * @node: a node
4647 *
4648 * Build a structure based Path for the given node
4649 *
4650 * Returns the new path or NULL in case of error. The caller must free
4651 * the returned string
4652 */
4653xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004654xmlGetNodePath(const xmlNode *node)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004655{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004656 const xmlNode *cur, *tmp, *next;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004657 xmlChar *buffer = NULL, *temp;
4658 size_t buf_len;
4659 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004660 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004661 const char *name;
4662 char nametemp[100];
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004663 int occur = 0, generic;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004664
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004665 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004666 return (NULL);
4667
4668 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004669 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004670 if (buffer == NULL) {
4671 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004672 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004673 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004674 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004675 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004676 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004677 xmlFree(buffer);
4678 return (NULL);
4679 }
4680
4681 buffer[0] = 0;
4682 cur = node;
4683 do {
4684 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004685 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004686 occur = 0;
4687 if ((cur->type == XML_DOCUMENT_NODE) ||
4688 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4689 if (buffer[0] == '/')
4690 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004691 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004692 next = NULL;
4693 } else if (cur->type == XML_ELEMENT_NODE) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004694 generic = 0;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004695 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004696 name = (const char *) cur->name;
4697 if (cur->ns) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004698 if (cur->ns->prefix != NULL) {
William M. Brack13dfa872004-09-18 04:52:08 +00004699 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004700 (char *)cur->ns->prefix, (char *)cur->name);
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004701 nametemp[sizeof(nametemp) - 1] = 0;
4702 name = nametemp;
4703 } else {
4704 /*
4705 * We cannot express named elements in the default
4706 * namespace, so use "*".
4707 */
4708 generic = 1;
4709 name = "*";
Daniel Veillardaa6de472008-08-25 14:53:31 +00004710 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004711 }
4712 next = cur->parent;
4713
4714 /*
4715 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004716 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004717 */
4718 tmp = cur->prev;
4719 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004720 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004721 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004722 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004723 ((tmp->ns == cur->ns) ||
4724 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004725 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004726 occur++;
4727 tmp = tmp->prev;
4728 }
4729 if (occur == 0) {
4730 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004731 while (tmp != NULL && occur == 0) {
4732 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004733 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004734 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004735 ((tmp->ns == cur->ns) ||
4736 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004737 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004738 occur++;
4739 tmp = tmp->next;
4740 }
4741 if (occur != 0)
4742 occur = 1;
4743 } else
4744 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004745 } else if (cur->type == XML_COMMENT_NODE) {
4746 sep = "/";
4747 name = "comment()";
4748 next = cur->parent;
4749
4750 /*
4751 * Thumbler index computation
4752 */
4753 tmp = cur->prev;
4754 while (tmp != NULL) {
4755 if (tmp->type == XML_COMMENT_NODE)
4756 occur++;
4757 tmp = tmp->prev;
4758 }
4759 if (occur == 0) {
4760 tmp = cur->next;
4761 while (tmp != NULL && occur == 0) {
4762 if (tmp->type == XML_COMMENT_NODE)
4763 occur++;
4764 tmp = tmp->next;
4765 }
4766 if (occur != 0)
4767 occur = 1;
4768 } else
4769 occur++;
4770 } else if ((cur->type == XML_TEXT_NODE) ||
4771 (cur->type == XML_CDATA_SECTION_NODE)) {
4772 sep = "/";
4773 name = "text()";
4774 next = cur->parent;
4775
4776 /*
4777 * Thumbler index computation
4778 */
4779 tmp = cur->prev;
4780 while (tmp != NULL) {
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004781 if ((tmp->type == XML_TEXT_NODE) ||
4782 (tmp->type == XML_CDATA_SECTION_NODE))
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004783 occur++;
4784 tmp = tmp->prev;
4785 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004786 /*
4787 * Evaluate if this is the only text- or CDATA-section-node;
4788 * if yes, then we'll get "text()", otherwise "text()[1]".
4789 */
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004790 if (occur == 0) {
4791 tmp = cur->next;
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004792 while (tmp != NULL) {
4793 if ((tmp->type == XML_TEXT_NODE) ||
4794 (tmp->type == XML_CDATA_SECTION_NODE))
4795 {
4796 occur = 1;
4797 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004798 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004799 tmp = tmp->next;
4800 }
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004801 } else
4802 occur++;
4803 } else if (cur->type == XML_PI_NODE) {
4804 sep = "/";
4805 snprintf(nametemp, sizeof(nametemp) - 1,
William M. Brack13dfa872004-09-18 04:52:08 +00004806 "processing-instruction('%s')", (char *)cur->name);
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004807 nametemp[sizeof(nametemp) - 1] = 0;
4808 name = nametemp;
4809
4810 next = cur->parent;
4811
4812 /*
4813 * Thumbler index computation
4814 */
4815 tmp = cur->prev;
4816 while (tmp != NULL) {
4817 if ((tmp->type == XML_PI_NODE) &&
4818 (xmlStrEqual(cur->name, tmp->name)))
4819 occur++;
4820 tmp = tmp->prev;
4821 }
4822 if (occur == 0) {
4823 tmp = cur->next;
4824 while (tmp != NULL && occur == 0) {
4825 if ((tmp->type == XML_PI_NODE) &&
4826 (xmlStrEqual(cur->name, tmp->name)))
4827 occur++;
4828 tmp = tmp->next;
4829 }
4830 if (occur != 0)
4831 occur = 1;
4832 } else
4833 occur++;
4834
Daniel Veillard8faa7832001-11-26 15:58:08 +00004835 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004836 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004837 name = (const char *) (((xmlAttrPtr) cur)->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004838 if (cur->ns) {
4839 if (cur->ns->prefix != NULL)
4840 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004841 (char *)cur->ns->prefix, (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004842 else
4843 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004844 (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004845 nametemp[sizeof(nametemp) - 1] = 0;
4846 name = nametemp;
4847 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004848 next = ((xmlAttrPtr) cur)->parent;
4849 } else {
4850 next = cur->parent;
4851 }
4852
4853 /*
4854 * Make sure there is enough room
4855 */
4856 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4857 buf_len =
4858 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4859 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4860 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004861 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004862 xmlFree(buf);
4863 xmlFree(buffer);
4864 return (NULL);
4865 }
4866 buffer = temp;
4867 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4868 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004869 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004870 xmlFree(buf);
4871 xmlFree(buffer);
4872 return (NULL);
4873 }
4874 buf = temp;
4875 }
4876 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004877 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004878 sep, name, (char *) buffer);
4879 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004880 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004881 sep, name, occur, (char *) buffer);
William M. Brack13dfa872004-09-18 04:52:08 +00004882 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004883 cur = next;
4884 } while (cur != NULL);
4885 xmlFree(buf);
4886 return (buffer);
4887}
Daniel Veillard652327a2003-09-29 18:02:38 +00004888#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004889
4890/**
Owen Taylor3473f882001-02-23 17:55:21 +00004891 * xmlDocGetRootElement:
4892 * @doc: the document
4893 *
4894 * Get the root element of the document (doc->children is a list
4895 * containing possibly comments, PIs, etc ...).
4896 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004897 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004898 */
4899xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004900xmlDocGetRootElement(const xmlDoc *doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00004901 xmlNodePtr ret;
4902
4903 if (doc == NULL) return(NULL);
4904 ret = doc->children;
4905 while (ret != NULL) {
4906 if (ret->type == XML_ELEMENT_NODE)
4907 return(ret);
4908 ret = ret->next;
4909 }
4910 return(ret);
4911}
Daniel Veillardaa6de472008-08-25 14:53:31 +00004912
Daniel Veillard2156d432004-03-04 15:59:36 +00004913#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004914/**
4915 * xmlDocSetRootElement:
4916 * @doc: the document
Daniel Veillard26a45c82006-10-20 12:55:34 +00004917 * @root: the new document root element, if root is NULL no action is taken,
4918 * to remove a node from a document use xmlUnlinkNode(root) instead.
Owen Taylor3473f882001-02-23 17:55:21 +00004919 *
4920 * Set the root element of the document (doc->children is a list
4921 * containing possibly comments, PIs, etc ...).
4922 *
Daniel Veillard26a45c82006-10-20 12:55:34 +00004923 * Returns the old root element if any was found, NULL if root was NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004924 */
4925xmlNodePtr
4926xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4927 xmlNodePtr old = NULL;
4928
4929 if (doc == NULL) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004930 if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
Daniel Veillardc575b992002-02-08 13:28:40 +00004931 return(NULL);
4932 xmlUnlinkNode(root);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00004933 xmlSetTreeDoc(root, doc);
Daniel Veillardc575b992002-02-08 13:28:40 +00004934 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004935 old = doc->children;
4936 while (old != NULL) {
4937 if (old->type == XML_ELEMENT_NODE)
4938 break;
4939 old = old->next;
4940 }
4941 if (old == NULL) {
4942 if (doc->children == NULL) {
4943 doc->children = root;
4944 doc->last = root;
4945 } else {
4946 xmlAddSibling(doc->children, root);
4947 }
4948 } else {
4949 xmlReplaceNode(old, root);
4950 }
4951 return(old);
4952}
Daniel Veillard2156d432004-03-04 15:59:36 +00004953#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00004954
Daniel Veillard2156d432004-03-04 15:59:36 +00004955#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004956/**
4957 * xmlNodeSetLang:
4958 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004959 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004960 *
4961 * Set the language of a node, i.e. the values of the xml:lang
4962 * attribute.
4963 */
4964void
4965xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004966 xmlNsPtr ns;
4967
Owen Taylor3473f882001-02-23 17:55:21 +00004968 if (cur == NULL) return;
4969 switch(cur->type) {
4970 case XML_TEXT_NODE:
4971 case XML_CDATA_SECTION_NODE:
4972 case XML_COMMENT_NODE:
4973 case XML_DOCUMENT_NODE:
4974 case XML_DOCUMENT_TYPE_NODE:
4975 case XML_DOCUMENT_FRAG_NODE:
4976 case XML_NOTATION_NODE:
4977 case XML_HTML_DOCUMENT_NODE:
4978 case XML_DTD_NODE:
4979 case XML_ELEMENT_DECL:
4980 case XML_ATTRIBUTE_DECL:
4981 case XML_ENTITY_DECL:
4982 case XML_PI_NODE:
4983 case XML_ENTITY_REF_NODE:
4984 case XML_ENTITY_NODE:
4985 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004986#ifdef LIBXML_DOCB_ENABLED
4987 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004988#endif
4989 case XML_XINCLUDE_START:
4990 case XML_XINCLUDE_END:
4991 return;
4992 case XML_ELEMENT_NODE:
4993 case XML_ATTRIBUTE_NODE:
4994 break;
4995 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004996 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4997 if (ns == NULL)
4998 return;
4999 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00005000}
Daniel Veillard652327a2003-09-29 18:02:38 +00005001#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardaa6de472008-08-25 14:53:31 +00005002
Owen Taylor3473f882001-02-23 17:55:21 +00005003/**
5004 * xmlNodeGetLang:
5005 * @cur: the node being checked
5006 *
5007 * Searches the language of a node, i.e. the values of the xml:lang
5008 * attribute or the one carried by the nearest ancestor.
5009 *
5010 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005011 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005012 */
5013xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005014xmlNodeGetLang(const xmlNode *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00005015 xmlChar *lang;
5016
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005017 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
5018 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005019 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00005020 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00005021 if (lang != NULL)
5022 return(lang);
5023 cur = cur->parent;
5024 }
5025 return(NULL);
5026}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005027
Owen Taylor3473f882001-02-23 17:55:21 +00005028
Daniel Veillard652327a2003-09-29 18:02:38 +00005029#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005030/**
5031 * xmlNodeSetSpacePreserve:
5032 * @cur: the node being changed
5033 * @val: the xml:space value ("0": default, 1: "preserve")
5034 *
5035 * Set (or reset) the space preserving behaviour of a node, i.e. the
5036 * value of the xml:space attribute.
5037 */
5038void
5039xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005040 xmlNsPtr ns;
5041
Owen Taylor3473f882001-02-23 17:55:21 +00005042 if (cur == NULL) return;
5043 switch(cur->type) {
5044 case XML_TEXT_NODE:
5045 case XML_CDATA_SECTION_NODE:
5046 case XML_COMMENT_NODE:
5047 case XML_DOCUMENT_NODE:
5048 case XML_DOCUMENT_TYPE_NODE:
5049 case XML_DOCUMENT_FRAG_NODE:
5050 case XML_NOTATION_NODE:
5051 case XML_HTML_DOCUMENT_NODE:
5052 case XML_DTD_NODE:
5053 case XML_ELEMENT_DECL:
5054 case XML_ATTRIBUTE_DECL:
5055 case XML_ENTITY_DECL:
5056 case XML_PI_NODE:
5057 case XML_ENTITY_REF_NODE:
5058 case XML_ENTITY_NODE:
5059 case XML_NAMESPACE_DECL:
5060 case XML_XINCLUDE_START:
5061 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005062#ifdef LIBXML_DOCB_ENABLED
5063 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005064#endif
5065 return;
5066 case XML_ELEMENT_NODE:
5067 case XML_ATTRIBUTE_NODE:
5068 break;
5069 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005070 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5071 if (ns == NULL)
5072 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005073 switch (val) {
5074 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005075 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00005076 break;
5077 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005078 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00005079 break;
5080 }
5081}
Daniel Veillard652327a2003-09-29 18:02:38 +00005082#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005083
5084/**
5085 * xmlNodeGetSpacePreserve:
5086 * @cur: the node being checked
5087 *
5088 * Searches the space preserving behaviour of a node, i.e. the values
5089 * of the xml:space attribute or the one carried by the nearest
5090 * ancestor.
5091 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005092 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00005093 */
5094int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005095xmlNodeGetSpacePreserve(const xmlNode *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00005096 xmlChar *space;
5097
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005098 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
5099 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00005100 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005101 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00005102 if (space != NULL) {
5103 if (xmlStrEqual(space, BAD_CAST "preserve")) {
5104 xmlFree(space);
5105 return(1);
5106 }
5107 if (xmlStrEqual(space, BAD_CAST "default")) {
5108 xmlFree(space);
5109 return(0);
5110 }
5111 xmlFree(space);
5112 }
5113 cur = cur->parent;
5114 }
5115 return(-1);
5116}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005117
Daniel Veillard652327a2003-09-29 18:02:38 +00005118#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005119/**
5120 * xmlNodeSetName:
5121 * @cur: the node being changed
5122 * @name: the new tag name
5123 *
5124 * Set (or reset) the name of a node.
5125 */
5126void
5127xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00005128 xmlDocPtr doc;
5129 xmlDictPtr dict;
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005130 const xmlChar *freeme = NULL;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005131
Owen Taylor3473f882001-02-23 17:55:21 +00005132 if (cur == NULL) return;
5133 if (name == NULL) return;
5134 switch(cur->type) {
5135 case XML_TEXT_NODE:
5136 case XML_CDATA_SECTION_NODE:
5137 case XML_COMMENT_NODE:
5138 case XML_DOCUMENT_TYPE_NODE:
5139 case XML_DOCUMENT_FRAG_NODE:
5140 case XML_NOTATION_NODE:
5141 case XML_HTML_DOCUMENT_NODE:
5142 case XML_NAMESPACE_DECL:
5143 case XML_XINCLUDE_START:
5144 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005145#ifdef LIBXML_DOCB_ENABLED
5146 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005147#endif
5148 return;
5149 case XML_ELEMENT_NODE:
5150 case XML_ATTRIBUTE_NODE:
5151 case XML_PI_NODE:
5152 case XML_ENTITY_REF_NODE:
5153 case XML_ENTITY_NODE:
5154 case XML_DTD_NODE:
5155 case XML_DOCUMENT_NODE:
5156 case XML_ELEMENT_DECL:
5157 case XML_ATTRIBUTE_DECL:
5158 case XML_ENTITY_DECL:
5159 break;
5160 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00005161 doc = cur->doc;
5162 if (doc != NULL)
5163 dict = doc->dict;
5164 else
5165 dict = NULL;
5166 if (dict != NULL) {
5167 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005168 freeme = cur->name;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005169 cur->name = xmlDictLookup(dict, name, -1);
5170 } else {
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005171 if (cur->name != NULL)
5172 freeme = cur->name;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005173 cur->name = xmlStrdup(name);
5174 }
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005175
5176 if (freeme)
5177 xmlFree((xmlChar *) freeme);
Owen Taylor3473f882001-02-23 17:55:21 +00005178}
Daniel Veillard2156d432004-03-04 15:59:36 +00005179#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00005180
Daniel Veillard2156d432004-03-04 15:59:36 +00005181#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005182/**
5183 * xmlNodeSetBase:
5184 * @cur: the node being changed
5185 * @uri: the new base URI
5186 *
5187 * Set (or reset) the base URI of a node, i.e. the value of the
5188 * xml:base attribute.
5189 */
5190void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00005191xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005192 xmlNsPtr ns;
Martin Trappelf3703102010-01-22 12:08:00 +01005193 xmlChar* fixed;
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005194
Owen Taylor3473f882001-02-23 17:55:21 +00005195 if (cur == NULL) return;
5196 switch(cur->type) {
5197 case XML_TEXT_NODE:
5198 case XML_CDATA_SECTION_NODE:
5199 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005200 case XML_DOCUMENT_TYPE_NODE:
5201 case XML_DOCUMENT_FRAG_NODE:
5202 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005203 case XML_DTD_NODE:
5204 case XML_ELEMENT_DECL:
5205 case XML_ATTRIBUTE_DECL:
5206 case XML_ENTITY_DECL:
5207 case XML_PI_NODE:
5208 case XML_ENTITY_REF_NODE:
5209 case XML_ENTITY_NODE:
5210 case XML_NAMESPACE_DECL:
5211 case XML_XINCLUDE_START:
5212 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00005213 return;
5214 case XML_ELEMENT_NODE:
5215 case XML_ATTRIBUTE_NODE:
5216 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005217 case XML_DOCUMENT_NODE:
5218#ifdef LIBXML_DOCB_ENABLED
5219 case XML_DOCB_DOCUMENT_NODE:
5220#endif
5221 case XML_HTML_DOCUMENT_NODE: {
5222 xmlDocPtr doc = (xmlDocPtr) cur;
5223
5224 if (doc->URL != NULL)
5225 xmlFree((xmlChar *) doc->URL);
5226 if (uri == NULL)
5227 doc->URL = NULL;
5228 else
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005229 doc->URL = xmlPathToURI(uri);
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005230 return;
5231 }
Owen Taylor3473f882001-02-23 17:55:21 +00005232 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00005233
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005234 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5235 if (ns == NULL)
5236 return;
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005237 fixed = xmlPathToURI(uri);
5238 if (fixed != NULL) {
5239 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
Martin Trappelf3703102010-01-22 12:08:00 +01005240 xmlFree(fixed);
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005241 } else {
5242 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5243 }
Owen Taylor3473f882001-02-23 17:55:21 +00005244}
Daniel Veillard652327a2003-09-29 18:02:38 +00005245#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005246
5247/**
Owen Taylor3473f882001-02-23 17:55:21 +00005248 * xmlNodeGetBase:
5249 * @doc: the document the node pertains to
5250 * @cur: the node being checked
5251 *
5252 * Searches for the BASE URL. The code should work on both XML
5253 * and HTML document even if base mechanisms are completely different.
5254 * It returns the base as defined in RFC 2396 sections
5255 * 5.1.1. Base URI within Document Content
5256 * and
5257 * 5.1.2. Base URI from the Encapsulating Entity
5258 * However it does not return the document base (5.1.3), use
Daniel Veillarde4d18492010-03-09 11:12:30 +01005259 * doc->URL in this case
Owen Taylor3473f882001-02-23 17:55:21 +00005260 *
5261 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005262 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005263 */
5264xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005265xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005266 xmlChar *oldbase = NULL;
5267 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00005268
Daniel Veillardaa6de472008-08-25 14:53:31 +00005269 if ((cur == NULL) && (doc == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005270 return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005271 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
5272 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005273 if (doc == NULL) doc = cur->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00005274 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5275 cur = doc->children;
5276 while ((cur != NULL) && (cur->name != NULL)) {
5277 if (cur->type != XML_ELEMENT_NODE) {
5278 cur = cur->next;
5279 continue;
5280 }
5281 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5282 cur = cur->children;
5283 continue;
5284 }
5285 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5286 cur = cur->children;
5287 continue;
5288 }
5289 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5290 return(xmlGetProp(cur, BAD_CAST "href"));
5291 }
5292 cur = cur->next;
5293 }
5294 return(NULL);
5295 }
5296 while (cur != NULL) {
5297 if (cur->type == XML_ENTITY_DECL) {
5298 xmlEntityPtr ent = (xmlEntityPtr) cur;
5299 return(xmlStrdup(ent->URI));
5300 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00005301 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005302 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00005303 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005304 if (oldbase != NULL) {
5305 newbase = xmlBuildURI(oldbase, base);
5306 if (newbase != NULL) {
5307 xmlFree(oldbase);
5308 xmlFree(base);
5309 oldbase = newbase;
5310 } else {
5311 xmlFree(oldbase);
5312 xmlFree(base);
5313 return(NULL);
5314 }
5315 } else {
5316 oldbase = base;
5317 }
5318 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5319 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5320 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5321 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00005322 }
5323 }
Owen Taylor3473f882001-02-23 17:55:21 +00005324 cur = cur->parent;
5325 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005326 if ((doc != NULL) && (doc->URL != NULL)) {
5327 if (oldbase == NULL)
5328 return(xmlStrdup(doc->URL));
5329 newbase = xmlBuildURI(oldbase, doc->URL);
5330 xmlFree(oldbase);
5331 return(newbase);
5332 }
5333 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00005334}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005335
Owen Taylor3473f882001-02-23 17:55:21 +00005336/**
Daniel Veillard78697292003-10-19 20:44:43 +00005337 * xmlNodeBufGetContent:
5338 * @buffer: a buffer
5339 * @cur: the node being read
5340 *
5341 * Read the value of a node @cur, this can be either the text carried
5342 * directly by this node if it's a TEXT node or the aggregate string
5343 * of the values carried by this node child's (TEXT and ENTITY_REF).
5344 * Entity references are substituted.
5345 * Fills up the buffer @buffer with this value
Daniel Veillardaa6de472008-08-25 14:53:31 +00005346 *
Daniel Veillard78697292003-10-19 20:44:43 +00005347 * Returns 0 in case of success and -1 in case of error.
5348 */
5349int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005350xmlNodeBufGetContent(xmlBufferPtr buffer, const xmlNode *cur)
Daniel Veillard78697292003-10-19 20:44:43 +00005351{
Daniel Veillarddddeede2012-07-16 14:44:26 +08005352 xmlBufPtr buf;
5353 int ret;
5354
Daniel Veillard78697292003-10-19 20:44:43 +00005355 if ((cur == NULL) || (buffer == NULL)) return(-1);
Daniel Veillarddddeede2012-07-16 14:44:26 +08005356 buf = xmlBufFromBuffer(buffer);
5357 ret = xmlBufGetNodeContent(buf, cur);
5358 buffer = xmlBufBackToBuffer(buf);
5359 if ((ret < 0) || (buffer == NULL))
5360 return(-1);
5361 return(0);
5362}
5363
5364/**
5365 * xmlBufGetNodeContent:
Daniel Veillard28cc42d2012-08-10 10:00:18 +08005366 * @buf: a buffer xmlBufPtr
Daniel Veillarddddeede2012-07-16 14:44:26 +08005367 * @cur: the node being read
5368 *
5369 * Read the value of a node @cur, this can be either the text carried
5370 * directly by this node if it's a TEXT node or the aggregate string
5371 * of the values carried by this node child's (TEXT and ENTITY_REF).
5372 * Entity references are substituted.
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005373 * Fills up the buffer @buf with this value
Daniel Veillarddddeede2012-07-16 14:44:26 +08005374 *
5375 * Returns 0 in case of success and -1 in case of error.
5376 */
5377int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005378xmlBufGetNodeContent(xmlBufPtr buf, const xmlNode *cur)
Daniel Veillarddddeede2012-07-16 14:44:26 +08005379{
5380 if ((cur == NULL) || (buf == NULL)) return(-1);
Daniel Veillard78697292003-10-19 20:44:43 +00005381 switch (cur->type) {
5382 case XML_CDATA_SECTION_NODE:
5383 case XML_TEXT_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005384 xmlBufCat(buf, cur->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005385 break;
5386 case XML_DOCUMENT_FRAG_NODE:
5387 case XML_ELEMENT_NODE:{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005388 const xmlNode *tmp = cur;
Daniel Veillard78697292003-10-19 20:44:43 +00005389
5390 while (tmp != NULL) {
5391 switch (tmp->type) {
5392 case XML_CDATA_SECTION_NODE:
5393 case XML_TEXT_NODE:
5394 if (tmp->content != NULL)
Daniel Veillarddddeede2012-07-16 14:44:26 +08005395 xmlBufCat(buf, tmp->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005396 break;
5397 case XML_ENTITY_REF_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005398 xmlBufGetNodeContent(buf, tmp);
Rob Richards77b92ff2005-12-20 15:55:14 +00005399 break;
Daniel Veillard78697292003-10-19 20:44:43 +00005400 default:
5401 break;
5402 }
5403 /*
5404 * Skip to next node
5405 */
5406 if (tmp->children != NULL) {
5407 if (tmp->children->type != XML_ENTITY_DECL) {
5408 tmp = tmp->children;
5409 continue;
5410 }
5411 }
5412 if (tmp == cur)
5413 break;
5414
5415 if (tmp->next != NULL) {
5416 tmp = tmp->next;
5417 continue;
5418 }
5419
5420 do {
5421 tmp = tmp->parent;
5422 if (tmp == NULL)
5423 break;
5424 if (tmp == cur) {
5425 tmp = NULL;
5426 break;
5427 }
5428 if (tmp->next != NULL) {
5429 tmp = tmp->next;
5430 break;
5431 }
5432 } while (tmp != NULL);
5433 }
5434 break;
5435 }
5436 case XML_ATTRIBUTE_NODE:{
5437 xmlAttrPtr attr = (xmlAttrPtr) cur;
5438 xmlNodePtr tmp = attr->children;
5439
5440 while (tmp != NULL) {
5441 if (tmp->type == XML_TEXT_NODE)
Daniel Veillarddddeede2012-07-16 14:44:26 +08005442 xmlBufCat(buf, tmp->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005443 else
Daniel Veillarddddeede2012-07-16 14:44:26 +08005444 xmlBufGetNodeContent(buf, tmp);
Daniel Veillard78697292003-10-19 20:44:43 +00005445 tmp = tmp->next;
5446 }
5447 break;
5448 }
5449 case XML_COMMENT_NODE:
5450 case XML_PI_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005451 xmlBufCat(buf, cur->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005452 break;
5453 case XML_ENTITY_REF_NODE:{
5454 xmlEntityPtr ent;
5455 xmlNodePtr tmp;
5456
5457 /* lookup entity declaration */
5458 ent = xmlGetDocEntity(cur->doc, cur->name);
5459 if (ent == NULL)
5460 return(-1);
5461
5462 /* an entity content can be any "well balanced chunk",
5463 * i.e. the result of the content [43] production:
5464 * http://www.w3.org/TR/REC-xml#NT-content
5465 * -> we iterate through child nodes and recursive call
5466 * xmlNodeGetContent() which handles all possible node types */
5467 tmp = ent->children;
5468 while (tmp) {
Daniel Veillarddddeede2012-07-16 14:44:26 +08005469 xmlBufGetNodeContent(buf, tmp);
Daniel Veillard78697292003-10-19 20:44:43 +00005470 tmp = tmp->next;
5471 }
5472 break;
5473 }
5474 case XML_ENTITY_NODE:
5475 case XML_DOCUMENT_TYPE_NODE:
5476 case XML_NOTATION_NODE:
5477 case XML_DTD_NODE:
5478 case XML_XINCLUDE_START:
5479 case XML_XINCLUDE_END:
5480 break;
5481 case XML_DOCUMENT_NODE:
5482#ifdef LIBXML_DOCB_ENABLED
5483 case XML_DOCB_DOCUMENT_NODE:
5484#endif
5485 case XML_HTML_DOCUMENT_NODE:
5486 cur = cur->children;
5487 while (cur!= NULL) {
5488 if ((cur->type == XML_ELEMENT_NODE) ||
5489 (cur->type == XML_TEXT_NODE) ||
5490 (cur->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillarddddeede2012-07-16 14:44:26 +08005491 xmlBufGetNodeContent(buf, cur);
Daniel Veillard78697292003-10-19 20:44:43 +00005492 }
5493 cur = cur->next;
5494 }
5495 break;
5496 case XML_NAMESPACE_DECL:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005497 xmlBufCat(buf, ((xmlNsPtr) cur)->href);
Daniel Veillard78697292003-10-19 20:44:43 +00005498 break;
5499 case XML_ELEMENT_DECL:
5500 case XML_ATTRIBUTE_DECL:
5501 case XML_ENTITY_DECL:
5502 break;
5503 }
5504 return(0);
5505}
Daniel Veillarddddeede2012-07-16 14:44:26 +08005506
Daniel Veillard78697292003-10-19 20:44:43 +00005507/**
Owen Taylor3473f882001-02-23 17:55:21 +00005508 * xmlNodeGetContent:
5509 * @cur: the node being read
5510 *
5511 * Read the value of a node, this can be either the text carried
5512 * directly by this node if it's a TEXT node or the aggregate string
5513 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00005514 * Entity references are substituted.
5515 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005516 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005517 */
5518xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005519xmlNodeGetContent(const xmlNode *cur)
Daniel Veillard7646b182002-04-20 06:41:40 +00005520{
5521 if (cur == NULL)
5522 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005523 switch (cur->type) {
5524 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005525 case XML_ELEMENT_NODE:{
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005526 xmlBufPtr buf;
Daniel Veillard7646b182002-04-20 06:41:40 +00005527 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00005528
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005529 buf = xmlBufCreateSize(64);
5530 if (buf == NULL)
Daniel Veillard7646b182002-04-20 06:41:40 +00005531 return (NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005532 xmlBufGetNodeContent(buf, cur);
5533 ret = xmlBufDetach(buf);
5534 xmlBufFree(buf);
Daniel Veillard7646b182002-04-20 06:41:40 +00005535 return (ret);
5536 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005537 case XML_ATTRIBUTE_NODE:
5538 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
Owen Taylor3473f882001-02-23 17:55:21 +00005539 case XML_COMMENT_NODE:
5540 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005541 if (cur->content != NULL)
5542 return (xmlStrdup(cur->content));
5543 return (NULL);
5544 case XML_ENTITY_REF_NODE:{
5545 xmlEntityPtr ent;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005546 xmlBufPtr buf;
Daniel Veillard7646b182002-04-20 06:41:40 +00005547 xmlChar *ret;
5548
5549 /* lookup entity declaration */
5550 ent = xmlGetDocEntity(cur->doc, cur->name);
5551 if (ent == NULL)
5552 return (NULL);
5553
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005554 buf = xmlBufCreate();
5555 if (buf == NULL)
Daniel Veillard7646b182002-04-20 06:41:40 +00005556 return (NULL);
5557
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005558 xmlBufGetNodeContent(buf, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005559
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005560 ret = xmlBufDetach(buf);
5561 xmlBufFree(buf);
Daniel Veillard7646b182002-04-20 06:41:40 +00005562 return (ret);
5563 }
Owen Taylor3473f882001-02-23 17:55:21 +00005564 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005565 case XML_DOCUMENT_TYPE_NODE:
5566 case XML_NOTATION_NODE:
5567 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005568 case XML_XINCLUDE_START:
5569 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00005570 return (NULL);
5571 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005572#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00005573 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005574#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00005575 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005576 xmlBufPtr buf;
Daniel Veillardc4696922003-10-19 21:47:14 +00005577 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00005578
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005579 buf = xmlBufCreate();
5580 if (buf == NULL)
Daniel Veillardc4696922003-10-19 21:47:14 +00005581 return (NULL);
5582
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005583 xmlBufGetNodeContent(buf, (xmlNodePtr) cur);
Daniel Veillardc4696922003-10-19 21:47:14 +00005584
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005585 ret = xmlBufDetach(buf);
5586 xmlBufFree(buf);
Daniel Veillardc4696922003-10-19 21:47:14 +00005587 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00005588 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00005589 case XML_NAMESPACE_DECL: {
5590 xmlChar *tmp;
5591
5592 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5593 return (tmp);
5594 }
Owen Taylor3473f882001-02-23 17:55:21 +00005595 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005596 /* TODO !!! */
5597 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005598 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005599 /* TODO !!! */
5600 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005601 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005602 /* TODO !!! */
5603 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005604 case XML_CDATA_SECTION_NODE:
5605 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005606 if (cur->content != NULL)
5607 return (xmlStrdup(cur->content));
5608 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005609 }
Daniel Veillard7646b182002-04-20 06:41:40 +00005610 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005611}
Daniel Veillard652327a2003-09-29 18:02:38 +00005612
Owen Taylor3473f882001-02-23 17:55:21 +00005613/**
5614 * xmlNodeSetContent:
5615 * @cur: the node being modified
5616 * @content: the new value of the content
5617 *
5618 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005619 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5620 * references, but XML special chars need to be escaped first by using
5621 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005622 */
5623void
5624xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5625 if (cur == NULL) {
5626#ifdef DEBUG_TREE
5627 xmlGenericError(xmlGenericErrorContext,
5628 "xmlNodeSetContent : node == NULL\n");
5629#endif
5630 return;
5631 }
5632 switch (cur->type) {
5633 case XML_DOCUMENT_FRAG_NODE:
5634 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005635 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005636 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5637 cur->children = xmlStringGetNodeList(cur->doc, content);
5638 UPDATE_LAST_CHILD_AND_PARENT(cur)
5639 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005640 case XML_TEXT_NODE:
5641 case XML_CDATA_SECTION_NODE:
5642 case XML_ENTITY_REF_NODE:
5643 case XML_ENTITY_NODE:
5644 case XML_PI_NODE:
5645 case XML_COMMENT_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005646 if ((cur->content != NULL) &&
5647 (cur->content != (xmlChar *) &(cur->properties))) {
William M. Brack7762bb12004-01-04 14:49:01 +00005648 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
Daniel Veillardc587bce2005-05-10 15:28:08 +00005649 (xmlDictOwns(cur->doc->dict, cur->content))))
William M. Brack7762bb12004-01-04 14:49:01 +00005650 xmlFree(cur->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005651 }
Owen Taylor3473f882001-02-23 17:55:21 +00005652 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5653 cur->last = cur->children = NULL;
5654 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005655 cur->content = xmlStrdup(content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005656 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005657 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005658 cur->properties = NULL;
5659 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005660 break;
5661 case XML_DOCUMENT_NODE:
5662 case XML_HTML_DOCUMENT_NODE:
5663 case XML_DOCUMENT_TYPE_NODE:
5664 case XML_XINCLUDE_START:
5665 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005666#ifdef LIBXML_DOCB_ENABLED
5667 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005668#endif
5669 break;
5670 case XML_NOTATION_NODE:
5671 break;
5672 case XML_DTD_NODE:
5673 break;
5674 case XML_NAMESPACE_DECL:
5675 break;
5676 case XML_ELEMENT_DECL:
5677 /* TODO !!! */
5678 break;
5679 case XML_ATTRIBUTE_DECL:
5680 /* TODO !!! */
5681 break;
5682 case XML_ENTITY_DECL:
5683 /* TODO !!! */
5684 break;
5685 }
5686}
5687
Daniel Veillard652327a2003-09-29 18:02:38 +00005688#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005689/**
5690 * xmlNodeSetContentLen:
5691 * @cur: the node being modified
5692 * @content: the new value of the content
5693 * @len: the size of @content
5694 *
5695 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005696 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5697 * references, but XML special chars need to be escaped first by using
5698 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005699 */
5700void
5701xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5702 if (cur == NULL) {
5703#ifdef DEBUG_TREE
5704 xmlGenericError(xmlGenericErrorContext,
5705 "xmlNodeSetContentLen : node == NULL\n");
5706#endif
5707 return;
5708 }
5709 switch (cur->type) {
5710 case XML_DOCUMENT_FRAG_NODE:
5711 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005712 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005713 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5714 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5715 UPDATE_LAST_CHILD_AND_PARENT(cur)
5716 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005717 case XML_TEXT_NODE:
5718 case XML_CDATA_SECTION_NODE:
5719 case XML_ENTITY_REF_NODE:
5720 case XML_ENTITY_NODE:
5721 case XML_PI_NODE:
5722 case XML_COMMENT_NODE:
5723 case XML_NOTATION_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005724 if ((cur->content != NULL) &&
5725 (cur->content != (xmlChar *) &(cur->properties))) {
5726 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5727 (xmlDictOwns(cur->doc->dict, cur->content))))
5728 xmlFree(cur->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005729 }
Owen Taylor3473f882001-02-23 17:55:21 +00005730 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5731 cur->children = cur->last = NULL;
5732 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005733 cur->content = xmlStrndup(content, len);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005734 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005735 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005736 cur->properties = NULL;
5737 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005738 break;
5739 case XML_DOCUMENT_NODE:
5740 case XML_DTD_NODE:
5741 case XML_HTML_DOCUMENT_NODE:
5742 case XML_DOCUMENT_TYPE_NODE:
5743 case XML_NAMESPACE_DECL:
5744 case XML_XINCLUDE_START:
5745 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005746#ifdef LIBXML_DOCB_ENABLED
5747 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005748#endif
5749 break;
5750 case XML_ELEMENT_DECL:
5751 /* TODO !!! */
5752 break;
5753 case XML_ATTRIBUTE_DECL:
5754 /* TODO !!! */
5755 break;
5756 case XML_ENTITY_DECL:
5757 /* TODO !!! */
5758 break;
5759 }
5760}
Daniel Veillard652327a2003-09-29 18:02:38 +00005761#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005762
5763/**
5764 * xmlNodeAddContentLen:
5765 * @cur: the node being modified
5766 * @content: extra content
5767 * @len: the size of @content
Daniel Veillardaa6de472008-08-25 14:53:31 +00005768 *
Owen Taylor3473f882001-02-23 17:55:21 +00005769 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005770 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5771 * raw text, so unescaped XML special chars are allowed, entity
5772 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005773 */
5774void
5775xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5776 if (cur == NULL) {
5777#ifdef DEBUG_TREE
5778 xmlGenericError(xmlGenericErrorContext,
5779 "xmlNodeAddContentLen : node == NULL\n");
5780#endif
5781 return;
5782 }
5783 if (len <= 0) return;
5784 switch (cur->type) {
5785 case XML_DOCUMENT_FRAG_NODE:
5786 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005787 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005788
Daniel Veillard7db37732001-07-12 01:20:08 +00005789 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005790 newNode = xmlNewTextLen(content, len);
5791 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005792 tmp = xmlAddChild(cur, newNode);
5793 if (tmp != newNode)
5794 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005795 if ((last != NULL) && (last->next == newNode)) {
5796 xmlTextMerge(last, newNode);
5797 }
5798 }
5799 break;
5800 }
5801 case XML_ATTRIBUTE_NODE:
5802 break;
5803 case XML_TEXT_NODE:
5804 case XML_CDATA_SECTION_NODE:
5805 case XML_ENTITY_REF_NODE:
5806 case XML_ENTITY_NODE:
5807 case XML_PI_NODE:
5808 case XML_COMMENT_NODE:
5809 case XML_NOTATION_NODE:
5810 if (content != NULL) {
Daniel Veillard8874b942005-08-25 13:19:21 +00005811 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5812 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5813 xmlDictOwns(cur->doc->dict, cur->content))) {
5814 cur->content = xmlStrncatNew(cur->content, content, len);
5815 cur->properties = NULL;
5816 cur->nsDef = NULL;
William M. Brack7762bb12004-01-04 14:49:01 +00005817 break;
5818 }
Owen Taylor3473f882001-02-23 17:55:21 +00005819 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005820 }
5821 case XML_DOCUMENT_NODE:
5822 case XML_DTD_NODE:
5823 case XML_HTML_DOCUMENT_NODE:
5824 case XML_DOCUMENT_TYPE_NODE:
5825 case XML_NAMESPACE_DECL:
5826 case XML_XINCLUDE_START:
5827 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005828#ifdef LIBXML_DOCB_ENABLED
5829 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005830#endif
5831 break;
5832 case XML_ELEMENT_DECL:
5833 case XML_ATTRIBUTE_DECL:
5834 case XML_ENTITY_DECL:
5835 break;
5836 }
5837}
5838
5839/**
5840 * xmlNodeAddContent:
5841 * @cur: the node being modified
5842 * @content: extra content
Daniel Veillardaa6de472008-08-25 14:53:31 +00005843 *
Owen Taylor3473f882001-02-23 17:55:21 +00005844 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005845 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5846 * raw text, so unescaped XML special chars are allowed, entity
5847 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005848 */
5849void
5850xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5851 int len;
5852
5853 if (cur == NULL) {
5854#ifdef DEBUG_TREE
5855 xmlGenericError(xmlGenericErrorContext,
5856 "xmlNodeAddContent : node == NULL\n");
5857#endif
5858 return;
5859 }
5860 if (content == NULL) return;
5861 len = xmlStrlen(content);
5862 xmlNodeAddContentLen(cur, content, len);
5863}
5864
5865/**
5866 * xmlTextMerge:
5867 * @first: the first text node
5868 * @second: the second text node being merged
Daniel Veillardaa6de472008-08-25 14:53:31 +00005869 *
Owen Taylor3473f882001-02-23 17:55:21 +00005870 * Merge two text nodes into one
5871 * Returns the first text node augmented
5872 */
5873xmlNodePtr
5874xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5875 if (first == NULL) return(second);
5876 if (second == NULL) return(first);
5877 if (first->type != XML_TEXT_NODE) return(first);
5878 if (second->type != XML_TEXT_NODE) return(first);
5879 if (second->name != first->name)
5880 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005881 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005882 xmlUnlinkNode(second);
5883 xmlFreeNode(second);
5884 return(first);
5885}
5886
Daniel Veillardf1a27c62006-10-13 22:33:03 +00005887#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005888/**
5889 * xmlGetNsList:
5890 * @doc: the document
5891 * @node: the current node
5892 *
5893 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005894 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005895 * that need to be freed by the caller or NULL if no
5896 * namespace if defined
5897 */
5898xmlNsPtr *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005899xmlGetNsList(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node)
Daniel Veillard77044732001-06-29 21:31:07 +00005900{
Owen Taylor3473f882001-02-23 17:55:21 +00005901 xmlNsPtr cur;
5902 xmlNsPtr *ret = NULL;
5903 int nbns = 0;
5904 int maxns = 10;
5905 int i;
5906
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005907 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5908 return(NULL);
5909
Owen Taylor3473f882001-02-23 17:55:21 +00005910 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005911 if (node->type == XML_ELEMENT_NODE) {
5912 cur = node->nsDef;
5913 while (cur != NULL) {
5914 if (ret == NULL) {
5915 ret =
5916 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5917 sizeof(xmlNsPtr));
5918 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005919 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005920 return (NULL);
5921 }
5922 ret[nbns] = NULL;
5923 }
5924 for (i = 0; i < nbns; i++) {
5925 if ((cur->prefix == ret[i]->prefix) ||
5926 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5927 break;
5928 }
5929 if (i >= nbns) {
5930 if (nbns >= maxns) {
5931 maxns *= 2;
5932 ret = (xmlNsPtr *) xmlRealloc(ret,
5933 (maxns +
5934 1) *
5935 sizeof(xmlNsPtr));
5936 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005937 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005938 return (NULL);
5939 }
5940 }
5941 ret[nbns++] = cur;
5942 ret[nbns] = NULL;
5943 }
Owen Taylor3473f882001-02-23 17:55:21 +00005944
Daniel Veillard77044732001-06-29 21:31:07 +00005945 cur = cur->next;
5946 }
5947 }
5948 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005949 }
Daniel Veillard77044732001-06-29 21:31:07 +00005950 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005951}
Daniel Veillard652327a2003-09-29 18:02:38 +00005952#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005953
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005954/*
5955* xmlTreeEnsureXMLDecl:
5956* @doc: the doc
Daniel Veillardaa6de472008-08-25 14:53:31 +00005957*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005958* Ensures that there is an XML namespace declaration on the doc.
Daniel Veillardaa6de472008-08-25 14:53:31 +00005959*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005960* Returns the XML ns-struct or NULL on API and internal errors.
5961*/
5962static xmlNsPtr
5963xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5964{
5965 if (doc == NULL)
5966 return (NULL);
5967 if (doc->oldNs != NULL)
5968 return (doc->oldNs);
5969 {
5970 xmlNsPtr ns;
5971 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5972 if (ns == NULL) {
5973 xmlTreeErrMemory(
5974 "allocating the XML namespace");
5975 return (NULL);
5976 }
5977 memset(ns, 0, sizeof(xmlNs));
5978 ns->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00005979 ns->href = xmlStrdup(XML_XML_NAMESPACE);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005980 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5981 doc->oldNs = ns;
5982 return (ns);
5983 }
5984}
5985
Owen Taylor3473f882001-02-23 17:55:21 +00005986/**
5987 * xmlSearchNs:
5988 * @doc: the document
5989 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005990 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005991 *
5992 * Search a Ns registered under a given name space for a document.
5993 * recurse on the parents until it finds the defined namespace
5994 * or return NULL otherwise.
5995 * @nameSpace can be NULL, this is a search for the default namespace.
5996 * We don't allow to cross entities boundaries. If you don't declare
5997 * the namespace within those you will be in troubles !!! A warning
5998 * is generated to cover this case.
5999 *
6000 * Returns the namespace pointer or NULL.
6001 */
6002xmlNsPtr
6003xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00006004
Owen Taylor3473f882001-02-23 17:55:21 +00006005 xmlNsPtr cur;
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006006 const xmlNode *orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00006007
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006008 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006009 if ((nameSpace != NULL) &&
6010 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00006011 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6012 /*
6013 * The XML-1.0 namespace is normally held on the root
6014 * element. In this case exceptionally create it on the
6015 * node element.
6016 */
6017 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6018 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006019 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00006020 return(NULL);
6021 }
6022 memset(cur, 0, sizeof(xmlNs));
6023 cur->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006024 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6025 cur->prefix = xmlStrdup((const xmlChar *)"xml");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00006026 cur->next = node->nsDef;
6027 node->nsDef = cur;
6028 return(cur);
6029 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00006030 if (doc == NULL) {
6031 doc = node->doc;
6032 if (doc == NULL)
6033 return(NULL);
6034 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006035 /*
6036 * Return the XML namespace declaration held by the doc.
6037 */
6038 if (doc->oldNs == NULL)
6039 return(xmlTreeEnsureXMLDecl(doc));
6040 else
6041 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006042 }
6043 while (node != NULL) {
6044 if ((node->type == XML_ENTITY_REF_NODE) ||
6045 (node->type == XML_ENTITY_NODE) ||
6046 (node->type == XML_ENTITY_DECL))
6047 return(NULL);
6048 if (node->type == XML_ELEMENT_NODE) {
6049 cur = node->nsDef;
6050 while (cur != NULL) {
6051 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6052 (cur->href != NULL))
6053 return(cur);
6054 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6055 (cur->href != NULL) &&
6056 (xmlStrEqual(cur->prefix, nameSpace)))
6057 return(cur);
6058 cur = cur->next;
6059 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006060 if (orig != node) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00006061 cur = node->ns;
6062 if (cur != NULL) {
6063 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6064 (cur->href != NULL))
6065 return(cur);
6066 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6067 (cur->href != NULL) &&
6068 (xmlStrEqual(cur->prefix, nameSpace)))
6069 return(cur);
6070 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006071 }
Owen Taylor3473f882001-02-23 17:55:21 +00006072 }
6073 node = node->parent;
6074 }
6075 return(NULL);
6076}
6077
6078/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006079 * xmlNsInScope:
6080 * @doc: the document
6081 * @node: the current node
6082 * @ancestor: the ancestor carrying the namespace
6083 * @prefix: the namespace prefix
6084 *
6085 * Verify that the given namespace held on @ancestor is still in scope
6086 * on node.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006087 *
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006088 * Returns 1 if true, 0 if false and -1 in case of error.
6089 */
6090static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006091xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
6092 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006093{
6094 xmlNsPtr tst;
6095
6096 while ((node != NULL) && (node != ancestor)) {
6097 if ((node->type == XML_ENTITY_REF_NODE) ||
6098 (node->type == XML_ENTITY_NODE) ||
6099 (node->type == XML_ENTITY_DECL))
6100 return (-1);
6101 if (node->type == XML_ELEMENT_NODE) {
6102 tst = node->nsDef;
6103 while (tst != NULL) {
6104 if ((tst->prefix == NULL)
6105 && (prefix == NULL))
6106 return (0);
6107 if ((tst->prefix != NULL)
6108 && (prefix != NULL)
6109 && (xmlStrEqual(tst->prefix, prefix)))
6110 return (0);
6111 tst = tst->next;
6112 }
6113 }
6114 node = node->parent;
6115 }
6116 if (node != ancestor)
6117 return (-1);
6118 return (1);
6119}
Daniel Veillardaa6de472008-08-25 14:53:31 +00006120
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006121/**
Owen Taylor3473f882001-02-23 17:55:21 +00006122 * xmlSearchNsByHref:
6123 * @doc: the document
6124 * @node: the current node
6125 * @href: the namespace value
6126 *
6127 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
6128 * the defined namespace or return NULL otherwise.
6129 * Returns the namespace pointer or NULL.
6130 */
6131xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006132xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
6133{
Owen Taylor3473f882001-02-23 17:55:21 +00006134 xmlNsPtr cur;
6135 xmlNodePtr orig = node;
Daniel Veillard62040be2004-05-17 03:17:26 +00006136 int is_attr;
Owen Taylor3473f882001-02-23 17:55:21 +00006137
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006138 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006139 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006140 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006141 /*
6142 * Only the document can hold the XML spec namespace.
6143 */
6144 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6145 /*
6146 * The XML-1.0 namespace is normally held on the root
6147 * element. In this case exceptionally create it on the
6148 * node element.
6149 */
6150 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6151 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006152 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006153 return (NULL);
6154 }
6155 memset(cur, 0, sizeof(xmlNs));
6156 cur->type = XML_LOCAL_NAMESPACE;
6157 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6158 cur->prefix = xmlStrdup((const xmlChar *) "xml");
6159 cur->next = node->nsDef;
6160 node->nsDef = cur;
6161 return (cur);
6162 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00006163 if (doc == NULL) {
6164 doc = node->doc;
6165 if (doc == NULL)
6166 return(NULL);
6167 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006168 /*
6169 * Return the XML namespace declaration held by the doc.
6170 */
6171 if (doc->oldNs == NULL)
6172 return(xmlTreeEnsureXMLDecl(doc));
6173 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00006174 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006175 }
Daniel Veillard62040be2004-05-17 03:17:26 +00006176 is_attr = (node->type == XML_ATTRIBUTE_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00006177 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006178 if ((node->type == XML_ENTITY_REF_NODE) ||
6179 (node->type == XML_ENTITY_NODE) ||
6180 (node->type == XML_ENTITY_DECL))
6181 return (NULL);
6182 if (node->type == XML_ELEMENT_NODE) {
6183 cur = node->nsDef;
6184 while (cur != NULL) {
6185 if ((cur->href != NULL) && (href != NULL) &&
6186 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00006187 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006188 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006189 return (cur);
6190 }
6191 cur = cur->next;
6192 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00006193 if (orig != node) {
6194 cur = node->ns;
6195 if (cur != NULL) {
6196 if ((cur->href != NULL) && (href != NULL) &&
6197 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00006198 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006199 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillardf4e56292003-10-28 14:27:41 +00006200 return (cur);
6201 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006202 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006203 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006204 }
6205 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00006206 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006207 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006208}
6209
6210/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006211 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006212 * @doc: the document
6213 * @tree: a node expected to hold the new namespace
6214 * @ns: the original namespace
6215 *
6216 * This function tries to locate a namespace definition in a tree
6217 * ancestors, or create a new namespace definition node similar to
6218 * @ns trying to reuse the same prefix. However if the given prefix is
6219 * null (default namespace) or reused within the subtree defined by
6220 * @tree or on one of its ancestors then a new prefix is generated.
6221 * Returns the (new) namespace definition or NULL in case of error
6222 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02006223static xmlNsPtr
Owen Taylor3473f882001-02-23 17:55:21 +00006224xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6225 xmlNsPtr def;
6226 xmlChar prefix[50];
6227 int counter = 1;
6228
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006229 if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006230#ifdef DEBUG_TREE
6231 xmlGenericError(xmlGenericErrorContext,
6232 "xmlNewReconciliedNs : tree == NULL\n");
6233#endif
6234 return(NULL);
6235 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00006236 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006237#ifdef DEBUG_TREE
6238 xmlGenericError(xmlGenericErrorContext,
6239 "xmlNewReconciliedNs : ns == NULL\n");
6240#endif
6241 return(NULL);
6242 }
6243 /*
6244 * Search an existing namespace definition inherited.
6245 */
6246 def = xmlSearchNsByHref(doc, tree, ns->href);
6247 if (def != NULL)
6248 return(def);
6249
6250 /*
6251 * Find a close prefix which is not already in use.
6252 * Let's strip namespace prefixes longer than 20 chars !
6253 */
Daniel Veillardf742d342002-03-07 00:05:35 +00006254 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006255 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00006256 else
William M. Brack13dfa872004-09-18 04:52:08 +00006257 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00006258
Owen Taylor3473f882001-02-23 17:55:21 +00006259 def = xmlSearchNs(doc, tree, prefix);
6260 while (def != NULL) {
6261 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00006262 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006263 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00006264 else
William M. Brack13dfa872004-09-18 04:52:08 +00006265 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
Daniel Veillardaa6de472008-08-25 14:53:31 +00006266 (char *)ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00006267 def = xmlSearchNs(doc, tree, prefix);
6268 }
6269
6270 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00006271 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00006272 */
6273 def = xmlNewNs(tree, ns->href, prefix);
6274 return(def);
6275}
6276
Daniel Veillard652327a2003-09-29 18:02:38 +00006277#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006278/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006279 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006280 * @doc: the document
6281 * @tree: a node defining the subtree to reconciliate
6282 *
6283 * This function checks that all the namespaces declared within the given
6284 * tree are properly declared. This is needed for example after Copy or Cut
6285 * and then paste operations. The subtree may still hold pointers to
6286 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00006287 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00006288 * the new environment. If not possible the new namespaces are redeclared
6289 * on @tree at the top of the given subtree.
6290 * Returns the number of namespace declarations created or -1 in case of error.
6291 */
6292int
6293xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6294 xmlNsPtr *oldNs = NULL;
6295 xmlNsPtr *newNs = NULL;
6296 int sizeCache = 0;
6297 int nbCache = 0;
6298
6299 xmlNsPtr n;
6300 xmlNodePtr node = tree;
6301 xmlAttrPtr attr;
6302 int ret = 0, i;
6303
Daniel Veillardce244ad2004-11-05 10:03:46 +00006304 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6305 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6306 if (node->doc != doc) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006307 while (node != NULL) {
6308 /*
6309 * Reconciliate the node namespace
6310 */
6311 if (node->ns != NULL) {
6312 /*
6313 * initialize the cache if needed
6314 */
6315 if (sizeCache == 0) {
6316 sizeCache = 10;
6317 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6318 sizeof(xmlNsPtr));
6319 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006320 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006321 return(-1);
6322 }
6323 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6324 sizeof(xmlNsPtr));
6325 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006326 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006327 xmlFree(oldNs);
6328 return(-1);
6329 }
6330 }
6331 for (i = 0;i < nbCache;i++) {
6332 if (oldNs[i] == node->ns) {
6333 node->ns = newNs[i];
6334 break;
6335 }
6336 }
6337 if (i == nbCache) {
6338 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00006339 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00006340 */
6341 n = xmlNewReconciliedNs(doc, tree, node->ns);
6342 if (n != NULL) { /* :-( what if else ??? */
6343 /*
6344 * check if we need to grow the cache buffers.
6345 */
6346 if (sizeCache <= nbCache) {
6347 sizeCache *= 2;
6348 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6349 sizeof(xmlNsPtr));
6350 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006351 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006352 xmlFree(newNs);
6353 return(-1);
6354 }
6355 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6356 sizeof(xmlNsPtr));
6357 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006358 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006359 xmlFree(oldNs);
6360 return(-1);
6361 }
6362 }
6363 newNs[nbCache] = n;
6364 oldNs[nbCache++] = node->ns;
6365 node->ns = n;
6366 }
6367 }
6368 }
6369 /*
6370 * now check for namespace hold by attributes on the node.
6371 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006372 if (node->type == XML_ELEMENT_NODE) {
6373 attr = node->properties;
6374 while (attr != NULL) {
6375 if (attr->ns != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006376 /*
Daniel Veillardb5f11972006-10-14 08:46:40 +00006377 * initialize the cache if needed
Owen Taylor3473f882001-02-23 17:55:21 +00006378 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006379 if (sizeCache == 0) {
6380 sizeCache = 10;
6381 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6382 sizeof(xmlNsPtr));
6383 if (oldNs == NULL) {
6384 xmlTreeErrMemory("fixing namespaces");
6385 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006386 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006387 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6388 sizeof(xmlNsPtr));
6389 if (newNs == NULL) {
6390 xmlTreeErrMemory("fixing namespaces");
6391 xmlFree(oldNs);
6392 return(-1);
6393 }
6394 }
6395 for (i = 0;i < nbCache;i++) {
6396 if (oldNs[i] == attr->ns) {
6397 attr->ns = newNs[i];
6398 break;
6399 }
6400 }
6401 if (i == nbCache) {
6402 /*
6403 * OK we need to recreate a new namespace definition
6404 */
6405 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6406 if (n != NULL) { /* :-( what if else ??? */
6407 /*
6408 * check if we need to grow the cache buffers.
6409 */
6410 if (sizeCache <= nbCache) {
6411 sizeCache *= 2;
6412 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6413 sizeCache * sizeof(xmlNsPtr));
6414 if (oldNs == NULL) {
6415 xmlTreeErrMemory("fixing namespaces");
6416 xmlFree(newNs);
6417 return(-1);
6418 }
6419 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6420 sizeCache * sizeof(xmlNsPtr));
6421 if (newNs == NULL) {
6422 xmlTreeErrMemory("fixing namespaces");
6423 xmlFree(oldNs);
6424 return(-1);
6425 }
6426 }
6427 newNs[nbCache] = n;
6428 oldNs[nbCache++] = attr->ns;
6429 attr->ns = n;
6430 }
Owen Taylor3473f882001-02-23 17:55:21 +00006431 }
6432 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006433 attr = attr->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006434 }
Owen Taylor3473f882001-02-23 17:55:21 +00006435 }
6436
6437 /*
6438 * Browse the full subtree, deep first
6439 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006440 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006441 /* deep first */
6442 node = node->children;
6443 } else if ((node != tree) && (node->next != NULL)) {
6444 /* then siblings */
6445 node = node->next;
6446 } else if (node != tree) {
6447 /* go up to parents->next if needed */
6448 while (node != tree) {
6449 if (node->parent != NULL)
6450 node = node->parent;
6451 if ((node != tree) && (node->next != NULL)) {
6452 node = node->next;
6453 break;
6454 }
6455 if (node->parent == NULL) {
6456 node = NULL;
6457 break;
6458 }
6459 }
6460 /* exit condition */
Daniel Veillardaa6de472008-08-25 14:53:31 +00006461 if (node == tree)
Owen Taylor3473f882001-02-23 17:55:21 +00006462 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00006463 } else
6464 break;
Owen Taylor3473f882001-02-23 17:55:21 +00006465 }
Daniel Veillardf742d342002-03-07 00:05:35 +00006466 if (oldNs != NULL)
6467 xmlFree(oldNs);
6468 if (newNs != NULL)
6469 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006470 return(ret);
6471}
Daniel Veillard652327a2003-09-29 18:02:38 +00006472#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00006473
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006474static xmlAttrPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006475xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006476 const xmlChar *nsName, int useDTD)
6477{
6478 xmlAttrPtr prop;
6479
6480 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6481 return(NULL);
6482
6483 if (node->properties != NULL) {
6484 prop = node->properties;
6485 if (nsName == NULL) {
6486 /*
6487 * We want the attr to be in no namespace.
6488 */
6489 do {
6490 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6491 return(prop);
6492 }
6493 prop = prop->next;
6494 } while (prop != NULL);
6495 } else {
6496 /*
6497 * We want the attr to be in the specified namespace.
6498 */
6499 do {
6500 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6501 ((prop->ns->href == nsName) ||
6502 xmlStrEqual(prop->ns->href, nsName)))
6503 {
6504 return(prop);
6505 }
6506 prop = prop->next;
6507 } while (prop != NULL);
6508 }
6509 }
6510
6511#ifdef LIBXML_TREE_ENABLED
6512 if (! useDTD)
6513 return(NULL);
6514 /*
6515 * Check if there is a default/fixed attribute declaration in
6516 * the internal or external subset.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006517 */
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006518 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6519 xmlDocPtr doc = node->doc;
6520 xmlAttributePtr attrDecl = NULL;
6521 xmlChar *elemQName, *tmpstr = NULL;
6522
6523 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00006524 * We need the QName of the element for the DTD-lookup.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006525 */
6526 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6527 tmpstr = xmlStrdup(node->ns->prefix);
6528 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6529 tmpstr = xmlStrcat(tmpstr, node->name);
6530 if (tmpstr == NULL)
6531 return(NULL);
6532 elemQName = tmpstr;
6533 } else
6534 elemQName = (xmlChar *) node->name;
6535 if (nsName == NULL) {
6536 /*
6537 * The common and nice case: Attr in no namespace.
6538 */
6539 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6540 elemQName, name, NULL);
6541 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6542 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6543 elemQName, name, NULL);
6544 }
6545 } else {
6546 xmlNsPtr *nsList, *cur;
6547
6548 /*
6549 * The ugly case: Search using the prefixes of in-scope
6550 * ns-decls corresponding to @nsName.
6551 */
6552 nsList = xmlGetNsList(node->doc, node);
6553 if (nsList == NULL) {
6554 if (tmpstr != NULL)
6555 xmlFree(tmpstr);
6556 return(NULL);
6557 }
6558 cur = nsList;
6559 while (*cur != NULL) {
6560 if (xmlStrEqual((*cur)->href, nsName)) {
6561 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6562 name, (*cur)->prefix);
6563 if (attrDecl)
6564 break;
6565 if (doc->extSubset != NULL) {
6566 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6567 name, (*cur)->prefix);
6568 if (attrDecl)
6569 break;
6570 }
6571 }
6572 cur++;
6573 }
6574 xmlFree(nsList);
Daniel Veillardaa6de472008-08-25 14:53:31 +00006575 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006576 if (tmpstr != NULL)
6577 xmlFree(tmpstr);
6578 /*
6579 * Only default/fixed attrs are relevant.
6580 */
6581 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6582 return((xmlAttrPtr) attrDecl);
6583 }
6584#endif /* LIBXML_TREE_ENABLED */
6585 return(NULL);
6586}
6587
6588static xmlChar*
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006589xmlGetPropNodeValueInternal(const xmlAttr *prop)
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006590{
6591 if (prop == NULL)
6592 return(NULL);
6593 if (prop->type == XML_ATTRIBUTE_NODE) {
6594 /*
6595 * Note that we return at least the empty string.
6596 * TODO: Do we really always want that?
6597 */
6598 if (prop->children != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006599 if ((prop->children->next == NULL) &&
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006600 ((prop->children->type == XML_TEXT_NODE) ||
6601 (prop->children->type == XML_CDATA_SECTION_NODE)))
6602 {
6603 /*
6604 * Optimization for the common case: only 1 text node.
6605 */
6606 return(xmlStrdup(prop->children->content));
6607 } else {
6608 xmlChar *ret;
6609
6610 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6611 if (ret != NULL)
6612 return(ret);
6613 }
6614 }
6615 return(xmlStrdup((xmlChar *)""));
6616 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6617 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6618 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006619 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006620}
6621
Owen Taylor3473f882001-02-23 17:55:21 +00006622/**
6623 * xmlHasProp:
6624 * @node: the node
6625 * @name: the attribute name
6626 *
6627 * Search an attribute associated to a node
6628 * This function also looks in DTD attribute declaration for #FIXED or
6629 * default declaration values unless DTD use has been turned off.
6630 *
Daniel Veillardaa6de472008-08-25 14:53:31 +00006631 * Returns the attribute or the attribute declaration or NULL if
Owen Taylor3473f882001-02-23 17:55:21 +00006632 * neither was found.
6633 */
6634xmlAttrPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006635xmlHasProp(const xmlNode *node, const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00006636 xmlAttrPtr prop;
6637 xmlDocPtr doc;
6638
Daniel Veillard8874b942005-08-25 13:19:21 +00006639 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6640 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006641 /*
6642 * Check on the properties attached to the node
6643 */
6644 prop = node->properties;
6645 while (prop != NULL) {
6646 if (xmlStrEqual(prop->name, name)) {
6647 return(prop);
6648 }
6649 prop = prop->next;
6650 }
6651 if (!xmlCheckDTD) return(NULL);
6652
6653 /*
6654 * Check if there is a default declaration in the internal
6655 * or external subsets
6656 */
6657 doc = node->doc;
6658 if (doc != NULL) {
6659 xmlAttributePtr attrDecl;
6660 if (doc->intSubset != NULL) {
6661 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6662 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6663 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006664 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6665 /* return attribute declaration only if a default value is given
6666 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00006667 return((xmlAttrPtr) attrDecl);
6668 }
6669 }
6670 return(NULL);
6671}
6672
6673/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00006674 * xmlHasNsProp:
6675 * @node: the node
6676 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006677 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00006678 *
6679 * Search for an attribute associated to a node
6680 * This attribute has to be anchored in the namespace specified.
6681 * This does the entity substitution.
6682 * This function looks in DTD attribute declaration for #FIXED or
6683 * default declaration values unless DTD use has been turned off.
William M. Brack2c228442004-10-03 04:10:00 +00006684 * Note that a namespace of NULL indicates to use the default namespace.
Daniel Veillarde95e2392001-06-06 10:46:28 +00006685 *
6686 * Returns the attribute or the attribute declaration or NULL
6687 * if neither was found.
6688 */
6689xmlAttrPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006690xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00006691
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006692 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
Daniel Veillarde95e2392001-06-06 10:46:28 +00006693}
6694
6695/**
Owen Taylor3473f882001-02-23 17:55:21 +00006696 * xmlGetProp:
6697 * @node: the node
6698 * @name: the attribute name
6699 *
6700 * Search and get the value of an attribute associated to a node
6701 * This does the entity substitution.
6702 * This function looks in DTD attribute declaration for #FIXED or
6703 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00006704 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00006705 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6706 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00006707 *
6708 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006709 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006710 */
6711xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006712xmlGetProp(const xmlNode *node, const xmlChar *name) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00006713 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006714
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006715 prop = xmlHasProp(node, name);
6716 if (prop == NULL)
6717 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00006718 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006719}
6720
6721/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006722 * xmlGetNoNsProp:
6723 * @node: the node
6724 * @name: the attribute name
6725 *
6726 * Search and get the value of an attribute associated to a node
6727 * This does the entity substitution.
6728 * This function looks in DTD attribute declaration for #FIXED or
6729 * default declaration values unless DTD use has been turned off.
6730 * This function is similar to xmlGetProp except it will accept only
6731 * an attribute in no namespace.
6732 *
6733 * Returns the attribute value or NULL if not found.
6734 * It's up to the caller to free the memory with xmlFree().
6735 */
6736xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006737xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) {
Daniel Veillard71531f32003-02-05 13:19:53 +00006738 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006739
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006740 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6741 if (prop == NULL)
Daniel Veillard8874b942005-08-25 13:19:21 +00006742 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006743 return(xmlGetPropNodeValueInternal(prop));
Daniel Veillard71531f32003-02-05 13:19:53 +00006744}
6745
6746/**
Owen Taylor3473f882001-02-23 17:55:21 +00006747 * xmlGetNsProp:
6748 * @node: the node
6749 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006750 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006751 *
6752 * Search and get the value of an attribute associated to a node
6753 * This attribute has to be anchored in the namespace specified.
6754 * This does the entity substitution.
6755 * This function looks in DTD attribute declaration for #FIXED or
6756 * default declaration values unless DTD use has been turned off.
6757 *
6758 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006759 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006760 */
6761xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006762xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006763 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006764
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006765 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6766 if (prop == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006767 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006768 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006769}
6770
Daniel Veillard2156d432004-03-04 15:59:36 +00006771#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6772/**
6773 * xmlUnsetProp:
6774 * @node: the node
6775 * @name: the attribute name
6776 *
6777 * Remove an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006778 * This handles only attributes in no namespace.
Daniel Veillard2156d432004-03-04 15:59:36 +00006779 * Returns 0 if successful, -1 if not found
6780 */
6781int
6782xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006783 xmlAttrPtr prop;
Daniel Veillard2156d432004-03-04 15:59:36 +00006784
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006785 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6786 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006787 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006788 xmlUnlinkNode((xmlNodePtr) prop);
6789 xmlFreeProp(prop);
6790 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006791}
6792
6793/**
6794 * xmlUnsetNsProp:
6795 * @node: the node
6796 * @ns: the namespace definition
6797 * @name: the attribute name
6798 *
6799 * Remove an attribute carried by a node.
6800 * Returns 0 if successful, -1 if not found
6801 */
6802int
6803xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006804 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006805
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006806 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6807 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006808 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006809 xmlUnlinkNode((xmlNodePtr) prop);
6810 xmlFreeProp(prop);
6811 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006812}
6813#endif
6814
6815#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00006816/**
6817 * xmlSetProp:
6818 * @node: the node
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006819 * @name: the attribute name (a QName)
Owen Taylor3473f882001-02-23 17:55:21 +00006820 * @value: the attribute value
6821 *
6822 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006823 * If @name has a prefix, then the corresponding
6824 * namespace-binding will be used, if in scope; it is an
6825 * error it there's no such ns-binding for the prefix in
6826 * scope.
Owen Taylor3473f882001-02-23 17:55:21 +00006827 * Returns the attribute pointer.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006828 *
Owen Taylor3473f882001-02-23 17:55:21 +00006829 */
6830xmlAttrPtr
6831xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006832 int len;
6833 const xmlChar *nqname;
Owen Taylor3473f882001-02-23 17:55:21 +00006834
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006835 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006836 return(NULL);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006837
6838 /*
6839 * handle QNames
6840 */
6841 nqname = xmlSplitQName3(name, &len);
6842 if (nqname != NULL) {
6843 xmlNsPtr ns;
6844 xmlChar *prefix = xmlStrndup(name, len);
6845 ns = xmlSearchNs(node->doc, node, prefix);
6846 if (prefix != NULL)
6847 xmlFree(prefix);
6848 if (ns != NULL)
6849 return(xmlSetNsProp(node, ns, nqname, value));
6850 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006851 return(xmlSetNsProp(node, NULL, name, value));
Owen Taylor3473f882001-02-23 17:55:21 +00006852}
6853
6854/**
6855 * xmlSetNsProp:
6856 * @node: the node
6857 * @ns: the namespace definition
6858 * @name: the attribute name
6859 * @value: the attribute value
6860 *
6861 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006862 * The ns structure must be in scope, this is not checked
Owen Taylor3473f882001-02-23 17:55:21 +00006863 *
6864 * Returns the attribute pointer.
6865 */
6866xmlAttrPtr
6867xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006868 const xmlChar *value)
6869{
Owen Taylor3473f882001-02-23 17:55:21 +00006870 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006871
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006872 if (ns && (ns->href == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006873 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006874 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6875 if (prop != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006876 /*
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006877 * Modify the attribute's value.
6878 */
6879 if (prop->atype == XML_ATTRIBUTE_ID) {
6880 xmlRemoveID(node->doc, prop);
6881 prop->atype = XML_ATTRIBUTE_ID;
6882 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006883 if (prop->children != NULL)
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006884 xmlFreeNodeList(prop->children);
6885 prop->children = NULL;
6886 prop->last = NULL;
6887 prop->ns = ns;
6888 if (value != NULL) {
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006889 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006890
Daniel Veillard6f8611f2008-02-15 08:33:21 +00006891 if(!xmlCheckUTF8(value)) {
6892 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6893 NULL);
6894 if (node->doc != NULL)
6895 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6896 }
6897 prop->children = xmlNewDocText(node->doc, value);
Owen Taylor3473f882001-02-23 17:55:21 +00006898 prop->last = NULL;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006899 tmp = prop->children;
6900 while (tmp != NULL) {
6901 tmp->parent = (xmlNodePtr) prop;
6902 if (tmp->next == NULL)
6903 prop->last = tmp;
6904 tmp = tmp->next;
6905 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006906 }
6907 if (prop->atype == XML_ATTRIBUTE_ID)
6908 xmlAddID(NULL, node->doc, value, prop);
6909 return(prop);
Owen Taylor3473f882001-02-23 17:55:21 +00006910 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006911 /*
6912 * No equal attr found; create a new one.
6913 */
6914 return(xmlNewPropInternal(node, ns, name, value, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00006915}
6916
Daniel Veillard652327a2003-09-29 18:02:38 +00006917#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006918
6919/**
Owen Taylor3473f882001-02-23 17:55:21 +00006920 * xmlNodeIsText:
6921 * @node: the node
Daniel Veillardaa6de472008-08-25 14:53:31 +00006922 *
Owen Taylor3473f882001-02-23 17:55:21 +00006923 * Is this node a Text node ?
6924 * Returns 1 yes, 0 no
6925 */
6926int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006927xmlNodeIsText(const xmlNode *node) {
Owen Taylor3473f882001-02-23 17:55:21 +00006928 if (node == NULL) return(0);
6929
6930 if (node->type == XML_TEXT_NODE) return(1);
6931 return(0);
6932}
6933
6934/**
6935 * xmlIsBlankNode:
6936 * @node: the node
Daniel Veillardaa6de472008-08-25 14:53:31 +00006937 *
Owen Taylor3473f882001-02-23 17:55:21 +00006938 * Checks whether this node is an empty or whitespace only
6939 * (and possibly ignorable) text-node.
6940 *
6941 * Returns 1 yes, 0 no
6942 */
6943int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006944xmlIsBlankNode(const xmlNode *node) {
Owen Taylor3473f882001-02-23 17:55:21 +00006945 const xmlChar *cur;
6946 if (node == NULL) return(0);
6947
Daniel Veillard7db37732001-07-12 01:20:08 +00006948 if ((node->type != XML_TEXT_NODE) &&
6949 (node->type != XML_CDATA_SECTION_NODE))
6950 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006951 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006952 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006953 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006954 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006955 cur++;
6956 }
6957
6958 return(1);
6959}
6960
6961/**
6962 * xmlTextConcat:
6963 * @node: the node
6964 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006965 * @len: @content length
Daniel Veillardaa6de472008-08-25 14:53:31 +00006966 *
Owen Taylor3473f882001-02-23 17:55:21 +00006967 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006968 *
6969 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006970 */
6971
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006972int
Owen Taylor3473f882001-02-23 17:55:21 +00006973xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006974 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006975
6976 if ((node->type != XML_TEXT_NODE) &&
Rob Richardsa02f1992006-09-16 14:04:26 +00006977 (node->type != XML_CDATA_SECTION_NODE) &&
6978 (node->type != XML_COMMENT_NODE) &&
6979 (node->type != XML_PI_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006980#ifdef DEBUG_TREE
6981 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006982 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006983#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006984 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006985 }
William M. Brack7762bb12004-01-04 14:49:01 +00006986 /* need to check if content is currently in the dictionary */
Daniel Veillard8874b942005-08-25 13:19:21 +00006987 if ((node->content == (xmlChar *) &(node->properties)) ||
6988 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6989 xmlDictOwns(node->doc->dict, node->content))) {
William M. Brack7762bb12004-01-04 14:49:01 +00006990 node->content = xmlStrncatNew(node->content, content, len);
6991 } else {
6992 node->content = xmlStrncat(node->content, content, len);
6993 }
Daniel Veillard8874b942005-08-25 13:19:21 +00006994 node->properties = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006995 if (node->content == NULL)
6996 return(-1);
6997 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006998}
6999
7000/************************************************************************
7001 * *
7002 * Output : to a FILE or in memory *
7003 * *
7004 ************************************************************************/
7005
Owen Taylor3473f882001-02-23 17:55:21 +00007006/**
7007 * xmlBufferCreate:
7008 *
7009 * routine to create an XML buffer.
7010 * returns the new structure.
7011 */
7012xmlBufferPtr
7013xmlBufferCreate(void) {
7014 xmlBufferPtr ret;
7015
7016 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7017 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007018 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007019 return(NULL);
7020 }
7021 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00007022 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00007023 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007024 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007025 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007026 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007027 xmlFree(ret);
7028 return(NULL);
7029 }
7030 ret->content[0] = 0;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007031 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00007032 return(ret);
7033}
7034
7035/**
7036 * xmlBufferCreateSize:
7037 * @size: initial size of buffer
7038 *
7039 * routine to create an XML buffer.
7040 * returns the new structure.
7041 */
7042xmlBufferPtr
7043xmlBufferCreateSize(size_t size) {
7044 xmlBufferPtr ret;
7045
7046 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7047 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007048 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007049 return(NULL);
7050 }
7051 ret->use = 0;
7052 ret->alloc = xmlBufferAllocScheme;
7053 ret->size = (size ? size+2 : 0); /* +1 for ending null */
7054 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007055 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007056 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007057 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007058 xmlFree(ret);
7059 return(NULL);
7060 }
7061 ret->content[0] = 0;
7062 } else
7063 ret->content = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007064 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00007065 return(ret);
7066}
7067
7068/**
Daniel Veillard79ee2842012-05-15 10:25:31 +08007069 * xmlBufferDetach:
Conrad Irwin7d553f82012-05-10 20:17:25 -07007070 * @buf: the buffer
7071 *
Daniel Veillard79ee2842012-05-15 10:25:31 +08007072 * Remove the string contained in a buffer and gie it back to the
Daniel Veillard94431ec2012-05-15 10:45:05 +08007073 * caller. The buffer is reset to an empty content.
7074 * This doesn't work with immutable buffers as they can't be reset.
Conrad Irwin7d553f82012-05-10 20:17:25 -07007075 *
Daniel Veillard79ee2842012-05-15 10:25:31 +08007076 * Returns the previous string contained by the buffer.
Conrad Irwin7d553f82012-05-10 20:17:25 -07007077 */
7078xmlChar *
7079xmlBufferDetach(xmlBufferPtr buf) {
7080 xmlChar *ret;
7081
Daniel Veillard94431ec2012-05-15 10:45:05 +08007082 if (buf == NULL)
7083 return(NULL);
7084 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
7085 return(NULL);
Conrad Irwin7d553f82012-05-10 20:17:25 -07007086
7087 ret = buf->content;
7088 buf->content = NULL;
7089 buf->size = 0;
7090 buf->use = 0;
7091
7092 return ret;
7093}
7094
7095
7096/**
Daniel Veillard53350552003-09-18 13:35:51 +00007097 * xmlBufferCreateStatic:
7098 * @mem: the memory area
7099 * @size: the size in byte
7100 *
MST 2003 John Flecka0e7e932003-12-19 03:13:47 +00007101 * routine to create an XML buffer from an immutable memory area.
7102 * The area won't be modified nor copied, and is expected to be
Daniel Veillard53350552003-09-18 13:35:51 +00007103 * present until the end of the buffer lifetime.
7104 *
7105 * returns the new structure.
7106 */
7107xmlBufferPtr
7108xmlBufferCreateStatic(void *mem, size_t size) {
7109 xmlBufferPtr ret;
7110
7111 if ((mem == NULL) || (size == 0))
7112 return(NULL);
7113
7114 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7115 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007116 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00007117 return(NULL);
7118 }
7119 ret->use = size;
7120 ret->size = size;
7121 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
7122 ret->content = (xmlChar *) mem;
7123 return(ret);
7124}
7125
7126/**
Owen Taylor3473f882001-02-23 17:55:21 +00007127 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00007128 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00007129 * @scheme: allocation scheme to use
7130 *
7131 * Sets the allocation scheme for this buffer
7132 */
7133void
Daniel Veillardaa6de472008-08-25 14:53:31 +00007134xmlBufferSetAllocationScheme(xmlBufferPtr buf,
Owen Taylor3473f882001-02-23 17:55:21 +00007135 xmlBufferAllocationScheme scheme) {
7136 if (buf == NULL) {
7137#ifdef DEBUG_BUFFER
7138 xmlGenericError(xmlGenericErrorContext,
7139 "xmlBufferSetAllocationScheme: buf == NULL\n");
7140#endif
7141 return;
7142 }
Daniel Veillardda3fee42008-09-01 13:08:57 +00007143 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7144 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
7145 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
7146 (scheme == XML_BUFFER_ALLOC_EXACT) ||
Conrad Irwin7d0d2a52012-05-14 14:18:58 +08007147 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
Daniel Veillardda3fee42008-09-01 13:08:57 +00007148 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
7149 buf->alloc = scheme;
Owen Taylor3473f882001-02-23 17:55:21 +00007150}
7151
7152/**
7153 * xmlBufferFree:
7154 * @buf: the buffer to free
7155 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00007156 * Frees an XML buffer. It frees both the content and the structure which
7157 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00007158 */
7159void
7160xmlBufferFree(xmlBufferPtr buf) {
7161 if (buf == NULL) {
7162#ifdef DEBUG_BUFFER
7163 xmlGenericError(xmlGenericErrorContext,
7164 "xmlBufferFree: buf == NULL\n");
7165#endif
7166 return;
7167 }
Daniel Veillard53350552003-09-18 13:35:51 +00007168
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007169 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7170 (buf->contentIO != NULL)) {
7171 xmlFree(buf->contentIO);
7172 } else if ((buf->content != NULL) &&
Daniel Veillard53350552003-09-18 13:35:51 +00007173 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007174 xmlFree(buf->content);
7175 }
Owen Taylor3473f882001-02-23 17:55:21 +00007176 xmlFree(buf);
7177}
7178
7179/**
7180 * xmlBufferEmpty:
7181 * @buf: the buffer
7182 *
7183 * empty a buffer.
7184 */
7185void
7186xmlBufferEmpty(xmlBufferPtr buf) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007187 if (buf == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007188 if (buf->content == NULL) return;
7189 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00007190 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00007191 buf->content = BAD_CAST "";
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007192 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7193 (buf->contentIO != NULL)) {
7194 size_t start_buf = buf->content - buf->contentIO;
7195
7196 buf->size += start_buf;
7197 buf->content = buf->contentIO;
7198 buf->content[0] = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00007199 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007200 buf->content[0] = 0;
7201 }
Owen Taylor3473f882001-02-23 17:55:21 +00007202}
7203
7204/**
7205 * xmlBufferShrink:
7206 * @buf: the buffer to dump
7207 * @len: the number of xmlChar to remove
7208 *
7209 * Remove the beginning of an XML buffer.
7210 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007211 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00007212 */
7213int
7214xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00007215 if (buf == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007216 if (len == 0) return(0);
7217 if (len > buf->use) return(-1);
7218
7219 buf->use -= len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007220 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7221 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7222 /*
7223 * we just move the content pointer, but also make sure
7224 * the perceived buffer size has shrinked accordingly
7225 */
Daniel Veillard53350552003-09-18 13:35:51 +00007226 buf->content += len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007227 buf->size -= len;
7228
7229 /*
7230 * sometimes though it maybe be better to really shrink
7231 * on IO buffers
7232 */
7233 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7234 size_t start_buf = buf->content - buf->contentIO;
7235 if (start_buf >= buf->size) {
7236 memmove(buf->contentIO, &buf->content[0], buf->use);
7237 buf->content = buf->contentIO;
7238 buf->content[buf->use] = 0;
7239 buf->size += start_buf;
7240 }
7241 }
Daniel Veillard53350552003-09-18 13:35:51 +00007242 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007243 memmove(buf->content, &buf->content[len], buf->use);
Daniel Veillard53350552003-09-18 13:35:51 +00007244 buf->content[buf->use] = 0;
7245 }
Owen Taylor3473f882001-02-23 17:55:21 +00007246 return(len);
7247}
7248
7249/**
7250 * xmlBufferGrow:
7251 * @buf: the buffer
7252 * @len: the minimum free size to allocate
7253 *
7254 * Grow the available space of an XML buffer.
7255 *
7256 * Returns the new available space or -1 in case of error
7257 */
7258int
7259xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7260 int size;
7261 xmlChar *newbuf;
7262
Daniel Veillard3d97e662004-11-04 10:49:00 +00007263 if (buf == NULL) return(-1);
7264
Daniel Veillard53350552003-09-18 13:35:51 +00007265 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00007266 if (len + buf->use < buf->size) return(0);
7267
Daniel Veillardee20cd72009-08-22 15:18:31 +02007268 /*
7269 * Windows has a BIG problem on realloc timing, so we try to double
7270 * the buffer size (if that's enough) (bug 146697)
7271 * Apparently BSD too, and it's probably best for linux too
7272 * On an embedded system this may be something to change
7273 */
7274#if 1
William M. Brack30fe43f2004-07-26 18:00:58 +00007275 if (buf->size > len)
7276 size = buf->size * 2;
7277 else
7278 size = buf->use + len + 100;
7279#else
Owen Taylor3473f882001-02-23 17:55:21 +00007280 size = buf->use + len + 100;
William M. Brack30fe43f2004-07-26 18:00:58 +00007281#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007282
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007283 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7284 size_t start_buf = buf->content - buf->contentIO;
7285
7286 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7287 if (newbuf == NULL) {
7288 xmlTreeErrMemory("growing buffer");
7289 return(-1);
7290 }
7291 buf->contentIO = newbuf;
7292 buf->content = newbuf + start_buf;
7293 } else {
7294 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7295 if (newbuf == NULL) {
7296 xmlTreeErrMemory("growing buffer");
7297 return(-1);
7298 }
7299 buf->content = newbuf;
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007300 }
Owen Taylor3473f882001-02-23 17:55:21 +00007301 buf->size = size;
7302 return(buf->size - buf->use);
7303}
7304
7305/**
7306 * xmlBufferDump:
7307 * @file: the file output
7308 * @buf: the buffer to dump
7309 *
7310 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00007311 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00007312 */
7313int
7314xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7315 int ret;
7316
7317 if (buf == NULL) {
7318#ifdef DEBUG_BUFFER
7319 xmlGenericError(xmlGenericErrorContext,
7320 "xmlBufferDump: buf == NULL\n");
7321#endif
7322 return(0);
7323 }
7324 if (buf->content == NULL) {
7325#ifdef DEBUG_BUFFER
7326 xmlGenericError(xmlGenericErrorContext,
7327 "xmlBufferDump: buf->content == NULL\n");
7328#endif
7329 return(0);
7330 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00007331 if (file == NULL)
7332 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00007333 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7334 return(ret);
7335}
7336
7337/**
7338 * xmlBufferContent:
7339 * @buf: the buffer
7340 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007341 * Function to extract the content of a buffer
7342 *
Owen Taylor3473f882001-02-23 17:55:21 +00007343 * Returns the internal content
7344 */
7345
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007346const xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08007347xmlBufferContent(const xmlBuffer *buf)
Owen Taylor3473f882001-02-23 17:55:21 +00007348{
7349 if(!buf)
7350 return NULL;
7351
7352 return buf->content;
7353}
7354
7355/**
7356 * xmlBufferLength:
Daniel Veillardaa6de472008-08-25 14:53:31 +00007357 * @buf: the buffer
Owen Taylor3473f882001-02-23 17:55:21 +00007358 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007359 * Function to get the length of a buffer
7360 *
Owen Taylor3473f882001-02-23 17:55:21 +00007361 * Returns the length of data in the internal content
7362 */
7363
7364int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08007365xmlBufferLength(const xmlBuffer *buf)
Owen Taylor3473f882001-02-23 17:55:21 +00007366{
7367 if(!buf)
7368 return 0;
7369
7370 return buf->use;
7371}
7372
7373/**
7374 * xmlBufferResize:
7375 * @buf: the buffer to resize
7376 * @size: the desired size
7377 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007378 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00007379 *
7380 * Returns 0 in case of problems, 1 otherwise
7381 */
7382int
7383xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7384{
7385 unsigned int newSize;
7386 xmlChar* rebuf = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007387 size_t start_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00007388
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007389 if (buf == NULL)
7390 return(0);
7391
Daniel Veillard53350552003-09-18 13:35:51 +00007392 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7393
Owen Taylor3473f882001-02-23 17:55:21 +00007394 /* Don't resize if we don't have to */
7395 if (size < buf->size)
7396 return 1;
7397
7398 /* figure out new size */
7399 switch (buf->alloc){
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007400 case XML_BUFFER_ALLOC_IO:
7401 case XML_BUFFER_ALLOC_DOUBLEIT:
7402 /*take care of empty case*/
7403 newSize = (buf->size ? buf->size*2 : size + 10);
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00007404 while (size > newSize) {
7405 if (newSize > UINT_MAX / 2) {
7406 xmlTreeErrMemory("growing buffer");
7407 return 0;
7408 }
7409 newSize *= 2;
7410 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007411 break;
7412 case XML_BUFFER_ALLOC_EXACT:
7413 newSize = size+10;
7414 break;
Conrad Irwin7d0d2a52012-05-14 14:18:58 +08007415 case XML_BUFFER_ALLOC_HYBRID:
7416 if (buf->use < BASE_BUFFER_SIZE)
7417 newSize = size;
7418 else {
7419 newSize = buf->size * 2;
7420 while (size > newSize) {
7421 if (newSize > UINT_MAX / 2) {
7422 xmlTreeErrMemory("growing buffer");
7423 return 0;
7424 }
7425 newSize *= 2;
7426 }
7427 }
7428 break;
7429
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007430 default:
7431 newSize = size+10;
7432 break;
Owen Taylor3473f882001-02-23 17:55:21 +00007433 }
7434
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007435 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7436 start_buf = buf->content - buf->contentIO;
7437
7438 if (start_buf > newSize) {
7439 /* move data back to start */
7440 memmove(buf->contentIO, buf->content, buf->use);
7441 buf->content = buf->contentIO;
7442 buf->content[buf->use] = 0;
7443 buf->size += start_buf;
7444 } else {
7445 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7446 if (rebuf == NULL) {
7447 xmlTreeErrMemory("growing buffer");
7448 return 0;
7449 }
7450 buf->contentIO = rebuf;
7451 buf->content = rebuf + start_buf;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00007452 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007453 } else {
7454 if (buf->content == NULL) {
7455 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7456 } else if (buf->size - buf->use < 100) {
7457 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7458 } else {
7459 /*
7460 * if we are reallocating a buffer far from being full, it's
7461 * better to make a new allocation and copy only the used range
7462 * and free the old one.
7463 */
7464 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7465 if (rebuf != NULL) {
7466 memcpy(rebuf, buf->content, buf->use);
7467 xmlFree(buf->content);
7468 rebuf[buf->use] = 0;
7469 }
7470 }
7471 if (rebuf == NULL) {
7472 xmlTreeErrMemory("growing buffer");
7473 return 0;
7474 }
7475 buf->content = rebuf;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00007476 }
Owen Taylor3473f882001-02-23 17:55:21 +00007477 buf->size = newSize;
7478
7479 return 1;
7480}
7481
7482/**
7483 * xmlBufferAdd:
7484 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00007485 * @str: the #xmlChar string
7486 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007487 *
Daniel Veillard60087f32001-10-10 09:45:09 +00007488 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00007489 * str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007490 *
7491 * Returns 0 successful, a positive error code number otherwise
7492 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007493 */
William M. Bracka3215c72004-07-31 16:24:01 +00007494int
Owen Taylor3473f882001-02-23 17:55:21 +00007495xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7496 unsigned int needSize;
7497
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007498 if ((str == NULL) || (buf == NULL)) {
William M. Bracka3215c72004-07-31 16:24:01 +00007499 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007500 }
William M. Bracka3215c72004-07-31 16:24:01 +00007501 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007502 if (len < -1) {
7503#ifdef DEBUG_BUFFER
7504 xmlGenericError(xmlGenericErrorContext,
7505 "xmlBufferAdd: len < 0\n");
7506#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007507 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007508 }
William M. Bracka3215c72004-07-31 16:24:01 +00007509 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007510
7511 if (len < 0)
7512 len = xmlStrlen(str);
7513
Daniel Veillardc9923322007-04-24 18:12:06 +00007514 if (len < 0) return -1;
7515 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007516
7517 needSize = buf->use + len + 2;
7518 if (needSize > buf->size){
7519 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007520 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007521 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007522 }
7523 }
7524
7525 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7526 buf->use += len;
7527 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007528 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007529}
7530
7531/**
7532 * xmlBufferAddHead:
7533 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00007534 * @str: the #xmlChar string
7535 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007536 *
7537 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00007538 * if len == -1, the length of @str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007539 *
7540 * Returns 0 successful, a positive error code number otherwise
7541 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007542 */
William M. Bracka3215c72004-07-31 16:24:01 +00007543int
Owen Taylor3473f882001-02-23 17:55:21 +00007544xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7545 unsigned int needSize;
7546
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007547 if (buf == NULL)
7548 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007549 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007550 if (str == NULL) {
7551#ifdef DEBUG_BUFFER
7552 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007553 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007554#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007555 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007556 }
7557 if (len < -1) {
7558#ifdef DEBUG_BUFFER
7559 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007560 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007561#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007562 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007563 }
William M. Bracka3215c72004-07-31 16:24:01 +00007564 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007565
7566 if (len < 0)
7567 len = xmlStrlen(str);
7568
William M. Bracka3215c72004-07-31 16:24:01 +00007569 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007570
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007571 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7572 size_t start_buf = buf->content - buf->contentIO;
7573
7574 if (start_buf > (unsigned int) len) {
7575 /*
7576 * We can add it in the space previously shrinked
7577 */
7578 buf->content -= len;
7579 memmove(&buf->content[0], str, len);
7580 buf->use += len;
7581 buf->size += len;
7582 return(0);
7583 }
7584 }
Owen Taylor3473f882001-02-23 17:55:21 +00007585 needSize = buf->use + len + 2;
7586 if (needSize > buf->size){
7587 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007588 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007589 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007590 }
7591 }
7592
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007593 memmove(&buf->content[len], &buf->content[0], buf->use);
7594 memmove(&buf->content[0], str, len);
Owen Taylor3473f882001-02-23 17:55:21 +00007595 buf->use += len;
7596 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007597 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007598}
7599
7600/**
7601 * xmlBufferCat:
William M. Bracka3215c72004-07-31 16:24:01 +00007602 * @buf: the buffer to add to
Daniel Veillardd1640922001-12-17 15:30:10 +00007603 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00007604 *
7605 * Append a zero terminated string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007606 *
7607 * Returns 0 successful, a positive error code number otherwise
7608 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007609 */
William M. Bracka3215c72004-07-31 16:24:01 +00007610int
Owen Taylor3473f882001-02-23 17:55:21 +00007611xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007612 if (buf == NULL)
7613 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007614 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7615 if (str == NULL) return -1;
7616 return xmlBufferAdd(buf, str, -1);
Owen Taylor3473f882001-02-23 17:55:21 +00007617}
7618
7619/**
7620 * xmlBufferCCat:
7621 * @buf: the buffer to dump
7622 * @str: the C char string
7623 *
7624 * Append a zero terminated C string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007625 *
7626 * Returns 0 successful, a positive error code number otherwise
7627 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007628 */
William M. Bracka3215c72004-07-31 16:24:01 +00007629int
Owen Taylor3473f882001-02-23 17:55:21 +00007630xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7631 const char *cur;
7632
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007633 if (buf == NULL)
7634 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007635 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007636 if (str == NULL) {
7637#ifdef DEBUG_BUFFER
7638 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007639 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007640#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007641 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007642 }
7643 for (cur = str;*cur != 0;cur++) {
7644 if (buf->use + 10 >= buf->size) {
7645 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007646 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007647 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007648 }
7649 }
7650 buf->content[buf->use++] = *cur;
7651 }
7652 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007653 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007654}
7655
7656/**
7657 * xmlBufferWriteCHAR:
7658 * @buf: the XML buffer
7659 * @string: the string to add
7660 *
7661 * routine which manages and grows an output buffer. This one adds
7662 * xmlChars at the end of the buffer.
7663 */
7664void
Daniel Veillard53350552003-09-18 13:35:51 +00007665xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007666 if (buf == NULL)
7667 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007668 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007669 xmlBufferCat(buf, string);
7670}
7671
7672/**
7673 * xmlBufferWriteChar:
7674 * @buf: the XML buffer output
7675 * @string: the string to add
7676 *
7677 * routine which manage and grows an output buffer. This one add
7678 * C chars at the end of the array.
7679 */
7680void
7681xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007682 if (buf == NULL)
7683 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007684 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007685 xmlBufferCCat(buf, string);
7686}
7687
7688
7689/**
7690 * xmlBufferWriteQuotedString:
7691 * @buf: the XML buffer output
7692 * @string: the string to add
7693 *
7694 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00007695 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00007696 * quote or double-quotes internally
7697 */
7698void
7699xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00007700 const xmlChar *cur, *base;
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007701 if (buf == NULL)
7702 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007703 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00007704 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00007705 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007706#ifdef DEBUG_BUFFER
7707 xmlGenericError(xmlGenericErrorContext,
7708 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7709#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00007710 xmlBufferCCat(buf, "\"");
7711 base = cur = string;
7712 while(*cur != 0){
7713 if(*cur == '"'){
7714 if (base != cur)
7715 xmlBufferAdd(buf, base, cur - base);
7716 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7717 cur++;
7718 base = cur;
7719 }
7720 else {
7721 cur++;
7722 }
7723 }
7724 if (base != cur)
7725 xmlBufferAdd(buf, base, cur - base);
7726 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007727 }
Daniel Veillard39057f42003-08-04 01:33:43 +00007728 else{
7729 xmlBufferCCat(buf, "\'");
7730 xmlBufferCat(buf, string);
7731 xmlBufferCCat(buf, "\'");
7732 }
Owen Taylor3473f882001-02-23 17:55:21 +00007733 } else {
7734 xmlBufferCCat(buf, "\"");
7735 xmlBufferCat(buf, string);
7736 xmlBufferCCat(buf, "\"");
7737 }
7738}
7739
7740
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007741/**
7742 * xmlGetDocCompressMode:
7743 * @doc: the document
7744 *
7745 * get the compression ratio for a document, ZLIB based
7746 * Returns 0 (uncompressed) to 9 (max compression)
7747 */
7748int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08007749xmlGetDocCompressMode (const xmlDoc *doc) {
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007750 if (doc == NULL) return(-1);
7751 return(doc->compression);
7752}
7753
7754/**
7755 * xmlSetDocCompressMode:
7756 * @doc: the document
7757 * @mode: the compression ratio
7758 *
7759 * set the compression ratio for a document, ZLIB based
7760 * Correct values: 0 (uncompressed) to 9 (max compression)
7761 */
7762void
7763xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7764 if (doc == NULL) return;
7765 if (mode < 0) doc->compression = 0;
7766 else if (mode > 9) doc->compression = 9;
7767 else doc->compression = mode;
7768}
7769
7770/**
7771 * xmlGetCompressMode:
7772 *
7773 * get the default compression mode used, ZLIB based.
7774 * Returns 0 (uncompressed) to 9 (max compression)
7775 */
7776int
7777xmlGetCompressMode(void)
7778{
7779 return (xmlCompressMode);
7780}
7781
7782/**
7783 * xmlSetCompressMode:
7784 * @mode: the compression ratio
7785 *
7786 * set the default compression mode used, ZLIB based
7787 * Correct values: 0 (uncompressed) to 9 (max compression)
7788 */
7789void
7790xmlSetCompressMode(int mode) {
7791 if (mode < 0) xmlCompressMode = 0;
7792 else if (mode > 9) xmlCompressMode = 9;
7793 else xmlCompressMode = mode;
7794}
7795
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007796#define XML_TREE_NSMAP_PARENT -1
7797#define XML_TREE_NSMAP_XML -2
7798#define XML_TREE_NSMAP_DOC -3
7799#define XML_TREE_NSMAP_CUSTOM -4
7800
7801typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7802struct xmlNsMapItem {
7803 xmlNsMapItemPtr next;
7804 xmlNsMapItemPtr prev;
7805 xmlNsPtr oldNs; /* old ns decl reference */
7806 xmlNsPtr newNs; /* new ns decl reference */
7807 int shadowDepth; /* Shadowed at this depth */
7808 /*
7809 * depth:
7810 * >= 0 == @node's ns-decls
7811 * -1 == @parent's ns-decls
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00007812 * -2 == the doc->oldNs XML ns-decl
7813 * -3 == the doc->oldNs storage ns-decls
7814 * -4 == ns-decls provided via custom ns-handling
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007815 */
7816 int depth;
7817};
7818
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007819typedef struct xmlNsMap *xmlNsMapPtr;
7820struct xmlNsMap {
7821 xmlNsMapItemPtr first;
7822 xmlNsMapItemPtr last;
7823 xmlNsMapItemPtr pool;
7824};
7825
7826#define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7827#define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7828#define XML_NSMAP_POP(m, i) \
7829 i = (m)->last; \
7830 (m)->last = (i)->prev; \
7831 if ((m)->last == NULL) \
7832 (m)->first = NULL; \
7833 else \
7834 (m)->last->next = NULL; \
7835 (i)->next = (m)->pool; \
7836 (m)->pool = i;
7837
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007838/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007839* xmlDOMWrapNsMapFree:
7840* @map: the ns-map
Daniel Veillardaa6de472008-08-25 14:53:31 +00007841*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007842* Frees the ns-map
7843*/
7844static void
7845xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7846{
7847 xmlNsMapItemPtr cur, tmp;
7848
7849 if (nsmap == NULL)
7850 return;
7851 cur = nsmap->pool;
7852 while (cur != NULL) {
7853 tmp = cur;
7854 cur = cur->next;
7855 xmlFree(tmp);
7856 }
7857 cur = nsmap->first;
7858 while (cur != NULL) {
7859 tmp = cur;
7860 cur = cur->next;
7861 xmlFree(tmp);
7862 }
7863 xmlFree(nsmap);
7864}
7865
7866/*
7867* xmlDOMWrapNsMapAddItem:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007868* @map: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007869* @oldNs: the old ns-struct
7870* @newNs: the new ns-struct
7871* @depth: depth and ns-kind information
Daniel Veillardaa6de472008-08-25 14:53:31 +00007872*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007873* Adds an ns-mapping item.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007874*/
7875static xmlNsMapItemPtr
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007876xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007877 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007878{
7879 xmlNsMapItemPtr ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007880 xmlNsMapPtr map;
7881
7882 if (nsmap == NULL)
7883 return(NULL);
7884 if ((position != -1) && (position != 0))
7885 return(NULL);
7886 map = *nsmap;
7887
7888 if (map == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007889 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007890 * Create the ns-map.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007891 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007892 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7893 if (map == NULL) {
7894 xmlTreeErrMemory("allocating namespace map");
7895 return (NULL);
7896 }
7897 memset(map, 0, sizeof(struct xmlNsMap));
7898 *nsmap = map;
7899 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00007900
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007901 if (map->pool != NULL) {
7902 /*
7903 * Reuse an item from the pool.
7904 */
7905 ret = map->pool;
7906 map->pool = ret->next;
7907 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007908 } else {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007909 /*
7910 * Create a new item.
7911 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007912 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7913 if (ret == NULL) {
7914 xmlTreeErrMemory("allocating namespace map item");
7915 return (NULL);
7916 }
7917 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007918 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00007919
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007920 if (map->first == NULL) {
7921 /*
7922 * First ever.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007923 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007924 map->first = ret;
7925 map->last = ret;
7926 } else if (position == -1) {
7927 /*
7928 * Append.
7929 */
7930 ret->prev = map->last;
7931 map->last->next = ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00007932 map->last = ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007933 } else if (position == 0) {
7934 /*
7935 * Set on first position.
7936 */
7937 map->first->prev = ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00007938 ret->next = map->first;
7939 map->first = ret;
Gaurav Gupta6d93e9e2014-10-06 20:20:00 +08007940 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007941
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007942 ret->oldNs = oldNs;
7943 ret->newNs = newNs;
7944 ret->shadowDepth = -1;
7945 ret->depth = depth;
7946 return (ret);
7947}
7948
7949/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007950* xmlDOMWrapStoreNs:
7951* @doc: the doc
7952* @nsName: the namespace name
7953* @prefix: the prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00007954*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007955* Creates or reuses an xmlNs struct on doc->oldNs with
7956* the given prefix and namespace name.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007957*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007958* Returns the aquired ns struct or NULL in case of an API
7959* or internal error.
7960*/
7961static xmlNsPtr
7962xmlDOMWrapStoreNs(xmlDocPtr doc,
7963 const xmlChar *nsName,
7964 const xmlChar *prefix)
7965{
7966 xmlNsPtr ns;
7967
7968 if (doc == NULL)
7969 return (NULL);
7970 ns = xmlTreeEnsureXMLDecl(doc);
7971 if (ns == NULL)
7972 return (NULL);
7973 if (ns->next != NULL) {
7974 /* Reuse. */
7975 ns = ns->next;
7976 while (ns != NULL) {
7977 if (((ns->prefix == prefix) ||
7978 xmlStrEqual(ns->prefix, prefix)) &&
7979 xmlStrEqual(ns->href, nsName)) {
7980 return (ns);
7981 }
7982 if (ns->next == NULL)
7983 break;
7984 ns = ns->next;
7985 }
7986 }
7987 /* Create. */
Daniel Veillard76d36452009-09-07 11:19:33 +02007988 if (ns != NULL) {
7989 ns->next = xmlNewNs(NULL, nsName, prefix);
7990 return (ns->next);
7991 }
7992 return(NULL);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007993}
7994
7995/*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007996* xmlDOMWrapNewCtxt:
7997*
7998* Allocates and initializes a new DOM-wrapper context.
7999*
Jan Pokorný75801652013-12-19 15:09:14 +01008000* Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008001*/
8002xmlDOMWrapCtxtPtr
8003xmlDOMWrapNewCtxt(void)
8004{
8005 xmlDOMWrapCtxtPtr ret;
8006
8007 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
8008 if (ret == NULL) {
8009 xmlTreeErrMemory("allocating DOM-wrapper context");
8010 return (NULL);
8011 }
8012 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
8013 return (ret);
8014}
8015
8016/*
8017* xmlDOMWrapFreeCtxt:
8018* @ctxt: the DOM-wrapper context
8019*
8020* Frees the DOM-wrapper context.
8021*/
8022void
8023xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
8024{
8025 if (ctxt == NULL)
8026 return;
8027 if (ctxt->namespaceMap != NULL)
8028 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
8029 /*
8030 * TODO: Store the namespace map in the context.
8031 */
8032 xmlFree(ctxt);
8033}
8034
8035/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008036* xmlTreeLookupNsListByPrefix:
8037* @nsList: a list of ns-structs
8038* @prefix: the searched prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00008039*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008040* Searches for a ns-decl with the given prefix in @nsList.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008041*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008042* Returns the ns-decl if found, NULL if not found and on
8043* API errors.
8044*/
8045static xmlNsPtr
8046xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
8047{
8048 if (nsList == NULL)
8049 return (NULL);
8050 {
8051 xmlNsPtr ns;
8052 ns = nsList;
8053 do {
8054 if ((prefix == ns->prefix) ||
8055 xmlStrEqual(prefix, ns->prefix)) {
8056 return (ns);
8057 }
8058 ns = ns->next;
8059 } while (ns != NULL);
8060 }
8061 return (NULL);
8062}
8063
8064/*
8065*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008066* xmlDOMWrapNSNormGatherInScopeNs:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008067* @map: the namespace map
8068* @node: the node to start with
Daniel Veillardaa6de472008-08-25 14:53:31 +00008069*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008070* Puts in-scope namespaces into the ns-map.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008071*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008072* Returns 0 on success, -1 on API or internal errors.
8073*/
8074static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008075xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008076 xmlNodePtr node)
8077{
8078 xmlNodePtr cur;
8079 xmlNsPtr ns;
8080 xmlNsMapItemPtr mi;
8081 int shadowed;
8082
8083 if ((map == NULL) || (*map != NULL))
8084 return (-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008085 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8086 return (-1);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008087 /*
8088 * Get in-scope ns-decls of @parent.
8089 */
8090 cur = node;
8091 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
8092 if (cur->type == XML_ELEMENT_NODE) {
8093 if (cur->nsDef != NULL) {
8094 ns = cur->nsDef;
8095 do {
8096 shadowed = 0;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008097 if (XML_NSMAP_NOTEMPTY(*map)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008098 /*
8099 * Skip shadowed prefixes.
8100 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008101 XML_NSMAP_FOREACH(*map, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008102 if ((ns->prefix == mi->newNs->prefix) ||
8103 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
8104 shadowed = 1;
8105 break;
8106 }
8107 }
8108 }
8109 /*
8110 * Insert mapping.
8111 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008112 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008113 ns, XML_TREE_NSMAP_PARENT);
8114 if (mi == NULL)
8115 return (-1);
8116 if (shadowed)
8117 mi->shadowDepth = 0;
8118 ns = ns->next;
8119 } while (ns != NULL);
8120 }
8121 }
8122 cur = cur->parent;
8123 }
8124 return (0);
8125}
8126
8127/*
8128* XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
8129* otherwise copy it, when it was in the source-dict.
8130*/
8131#define XML_TREE_ADOPT_STR(str) \
8132 if (adoptStr && (str != NULL)) { \
8133 if (destDoc->dict) { \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008134 const xmlChar *old = str; \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008135 str = xmlDictLookup(destDoc->dict, str, -1); \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008136 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
8137 (!xmlDictOwns(sourceDoc->dict, old))) \
Daniel Veillard39e5c892005-07-03 22:48:50 +00008138 xmlFree((char *)old); \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008139 } else if ((sourceDoc) && (sourceDoc->dict) && \
8140 xmlDictOwns(sourceDoc->dict, str)) { \
8141 str = BAD_CAST xmlStrdup(str); \
8142 } \
8143 }
8144
8145/*
8146* XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
8147* put it in dest-dict or copy it.
8148*/
8149#define XML_TREE_ADOPT_STR_2(str) \
8150 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
8151 (sourceDoc->dict != NULL) && \
8152 xmlDictOwns(sourceDoc->dict, cur->content)) { \
8153 if (destDoc->dict) \
8154 cur->content = (xmlChar *) \
8155 xmlDictLookup(destDoc->dict, cur->content, -1); \
8156 else \
8157 cur->content = xmlStrdup(BAD_CAST cur->content); \
8158 }
8159
8160/*
8161* xmlDOMWrapNSNormAddNsMapItem2:
8162*
8163* For internal use. Adds a ns-decl mapping.
8164*
Daniel Veillardaa6de472008-08-25 14:53:31 +00008165* Returns 0 on success, -1 on internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008166*/
8167static int
8168xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
8169 xmlNsPtr oldNs, xmlNsPtr newNs)
8170{
8171 if (*list == NULL) {
8172 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
8173 if (*list == NULL) {
8174 xmlTreeErrMemory("alloc ns map item");
8175 return(-1);
8176 }
8177 *size = 3;
8178 *number = 0;
8179 } else if ((*number) >= (*size)) {
8180 *size *= 2;
8181 *list = (xmlNsPtr *) xmlRealloc(*list,
8182 (*size) * 2 * sizeof(xmlNsPtr));
8183 if (*list == NULL) {
8184 xmlTreeErrMemory("realloc ns map item");
8185 return(-1);
8186 }
8187 }
8188 (*list)[2 * (*number)] = oldNs;
8189 (*list)[2 * (*number) +1] = newNs;
8190 (*number)++;
8191 return (0);
8192}
8193
8194/*
8195* xmlDOMWrapRemoveNode:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008196* @ctxt: a DOM wrapper context
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008197* @doc: the doc
8198* @node: the node to be removed.
Daniel Veillard304e78c2005-07-03 16:19:41 +00008199* @options: set of options, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008200*
8201* Unlinks the given node from its owner.
8202* This will substitute ns-references to node->nsDef for
8203* ns-references to doc->oldNs, thus ensuring the removed
8204* branch to be autark wrt ns-references.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008205*
8206* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008207*
8208* Returns 0 on success, 1 if the node is not supported,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008209* -1 on API and internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008210*/
8211int
8212xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
8213 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8214{
8215 xmlNsPtr *list = NULL;
8216 int sizeList, nbList, i, j;
8217 xmlNsPtr ns;
8218
8219 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8220 return (-1);
8221
8222 /* TODO: 0 or -1 ? */
8223 if (node->parent == NULL)
8224 return (0);
8225
Daniel Veillardaa6de472008-08-25 14:53:31 +00008226 switch (node->type) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008227 case XML_TEXT_NODE:
8228 case XML_CDATA_SECTION_NODE:
8229 case XML_ENTITY_REF_NODE:
8230 case XML_PI_NODE:
8231 case XML_COMMENT_NODE:
8232 xmlUnlinkNode(node);
8233 return (0);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008234 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008235 case XML_ATTRIBUTE_NODE:
8236 break;
8237 default:
8238 return (1);
8239 }
8240 xmlUnlinkNode(node);
8241 /*
8242 * Save out-of-scope ns-references in doc->oldNs.
8243 */
8244 do {
8245 switch (node->type) {
8246 case XML_ELEMENT_NODE:
8247 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8248 ns = node->nsDef;
8249 do {
8250 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8251 &nbList, ns, ns) == -1)
8252 goto internal_error;
8253 ns = ns->next;
8254 } while (ns != NULL);
8255 }
8256 /* No break on purpose. */
8257 case XML_ATTRIBUTE_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008258 if (node->ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008259 /*
8260 * Find a mapping.
8261 */
8262 if (list != NULL) {
8263 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8264 if (node->ns == list[j]) {
8265 node->ns = list[++j];
8266 goto next_node;
8267 }
8268 }
8269 }
8270 ns = NULL;
8271 if (ctxt != NULL) {
8272 /*
8273 * User defined.
8274 */
8275 } else {
8276 /*
8277 * Add to doc's oldNs.
8278 */
8279 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8280 node->ns->prefix);
8281 if (ns == NULL)
8282 goto internal_error;
8283 }
8284 if (ns != NULL) {
8285 /*
8286 * Add mapping.
8287 */
8288 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8289 &nbList, node->ns, ns) == -1)
8290 goto internal_error;
8291 }
8292 node->ns = ns;
8293 }
8294 if ((node->type == XML_ELEMENT_NODE) &&
8295 (node->properties != NULL)) {
8296 node = (xmlNodePtr) node->properties;
8297 continue;
8298 }
8299 break;
8300 default:
8301 goto next_sibling;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008302 }
8303next_node:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008304 if ((node->type == XML_ELEMENT_NODE) &&
8305 (node->children != NULL)) {
8306 node = node->children;
8307 continue;
8308 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008309next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008310 if (node == NULL)
8311 break;
8312 if (node->next != NULL)
8313 node = node->next;
8314 else {
8315 node = node->parent;
8316 goto next_sibling;
8317 }
8318 } while (node != NULL);
8319
8320 if (list != NULL)
8321 xmlFree(list);
8322 return (0);
8323
8324internal_error:
8325 if (list != NULL)
8326 xmlFree(list);
8327 return (-1);
8328}
8329
8330/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008331* xmlSearchNsByNamespaceStrict:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008332* @doc: the document
8333* @node: the start node
8334* @nsName: the searched namespace name
8335* @retNs: the resulting ns-decl
8336* @prefixed: if the found ns-decl must have a prefix (for attributes)
8337*
8338* Dynamically searches for a ns-declaration which matches
8339* the given @nsName in the ancestor-or-self axis of @node.
8340*
8341* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8342* and internal errors.
8343*/
8344static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008345xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8346 const xmlChar* nsName,
8347 xmlNsPtr *retNs, int prefixed)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008348{
8349 xmlNodePtr cur, prev = NULL, out = NULL;
8350 xmlNsPtr ns, prevns;
8351
8352 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8353 return (-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008354 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8355 return(-1);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008356
8357 *retNs = NULL;
8358 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8359 *retNs = xmlTreeEnsureXMLDecl(doc);
8360 if (*retNs == NULL)
8361 return (-1);
8362 return (1);
8363 }
8364 cur = node;
8365 do {
8366 if (cur->type == XML_ELEMENT_NODE) {
8367 if (cur->nsDef != NULL) {
8368 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8369 if (prefixed && (ns->prefix == NULL))
8370 continue;
8371 if (prev != NULL) {
8372 /*
8373 * Check the last level of ns-decls for a
8374 * shadowing prefix.
8375 */
8376 prevns = prev->nsDef;
8377 do {
8378 if ((prevns->prefix == ns->prefix) ||
8379 ((prevns->prefix != NULL) &&
8380 (ns->prefix != NULL) &&
8381 xmlStrEqual(prevns->prefix, ns->prefix))) {
8382 /*
8383 * Shadowed.
8384 */
8385 break;
8386 }
8387 prevns = prevns->next;
8388 } while (prevns != NULL);
8389 if (prevns != NULL)
8390 continue;
8391 }
8392 /*
8393 * Ns-name comparison.
8394 */
8395 if ((nsName == ns->href) ||
8396 xmlStrEqual(nsName, ns->href)) {
8397 /*
8398 * At this point the prefix can only be shadowed,
8399 * if we are the the (at least) 3rd level of
8400 * ns-decls.
8401 */
8402 if (out) {
8403 int ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008404
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008405 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8406 if (ret < 0)
8407 return (-1);
8408 /*
8409 * TODO: Should we try to find a matching ns-name
8410 * only once? This here keeps on searching.
8411 * I think we should try further since, there might
8412 * be an other matching ns-decl with an unshadowed
8413 * prefix.
8414 */
8415 if (! ret)
8416 continue;
8417 }
8418 *retNs = ns;
8419 return (1);
8420 }
8421 }
8422 out = prev;
8423 prev = cur;
8424 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008425 } else if ((cur->type == XML_ENTITY_NODE) ||
8426 (cur->type == XML_ENTITY_DECL))
8427 return (0);
8428 cur = cur->parent;
8429 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8430 return (0);
8431}
8432
8433/*
8434* xmlSearchNsByPrefixStrict:
8435* @doc: the document
8436* @node: the start node
8437* @prefix: the searched namespace prefix
8438* @retNs: the resulting ns-decl
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008439*
8440* Dynamically searches for a ns-declaration which matches
8441* the given @nsName in the ancestor-or-self axis of @node.
8442*
8443* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8444* and internal errors.
8445*/
8446static int
8447xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8448 const xmlChar* prefix,
8449 xmlNsPtr *retNs)
8450{
8451 xmlNodePtr cur;
8452 xmlNsPtr ns;
8453
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008454 if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
8455 return(-1);
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008456
8457 if (retNs)
8458 *retNs = NULL;
8459 if (IS_STR_XML(prefix)) {
8460 if (retNs) {
8461 *retNs = xmlTreeEnsureXMLDecl(doc);
8462 if (*retNs == NULL)
8463 return (-1);
8464 }
8465 return (1);
8466 }
8467 cur = node;
8468 do {
8469 if (cur->type == XML_ELEMENT_NODE) {
8470 if (cur->nsDef != NULL) {
8471 ns = cur->nsDef;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008472 do {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008473 if ((prefix == ns->prefix) ||
8474 xmlStrEqual(prefix, ns->prefix))
8475 {
8476 /*
8477 * Disabled namespaces, e.g. xmlns:abc="".
8478 */
8479 if (ns->href == NULL)
8480 return(0);
8481 if (retNs)
8482 *retNs = ns;
8483 return (1);
8484 }
8485 ns = ns->next;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008486 } while (ns != NULL);
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008487 }
8488 } else if ((cur->type == XML_ENTITY_NODE) ||
8489 (cur->type == XML_ENTITY_DECL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008490 return (0);
8491 cur = cur->parent;
8492 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8493 return (0);
8494}
8495
8496/*
8497* xmlDOMWrapNSNormDeclareNsForced:
8498* @doc: the doc
8499* @elem: the element-node to declare on
8500* @nsName: the namespace-name of the ns-decl
8501* @prefix: the preferred prefix of the ns-decl
8502* @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8503*
8504* Declares a new namespace on @elem. It tries to use the
8505* given @prefix; if a ns-decl with the given prefix is already existent
8506* on @elem, it will generate an other prefix.
8507*
8508* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8509* and internal errors.
8510*/
8511static xmlNsPtr
8512xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8513 xmlNodePtr elem,
8514 const xmlChar *nsName,
8515 const xmlChar *prefix,
8516 int checkShadow)
8517{
8518
8519 xmlNsPtr ret;
8520 char buf[50];
8521 const xmlChar *pref;
8522 int counter = 0;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008523
8524 if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
8525 return(NULL);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008526 /*
8527 * Create a ns-decl on @anchor.
8528 */
8529 pref = prefix;
8530 while (1) {
8531 /*
8532 * Lookup whether the prefix is unused in elem's ns-decls.
8533 */
8534 if ((elem->nsDef != NULL) &&
8535 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8536 goto ns_next_prefix;
8537 if (checkShadow && elem->parent &&
8538 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8539 /*
8540 * Does it shadow ancestor ns-decls?
8541 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008542 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008543 goto ns_next_prefix;
8544 }
8545 ret = xmlNewNs(NULL, nsName, pref);
8546 if (ret == NULL)
8547 return (NULL);
8548 if (elem->nsDef == NULL)
8549 elem->nsDef = ret;
8550 else {
8551 xmlNsPtr ns2 = elem->nsDef;
8552 while (ns2->next != NULL)
8553 ns2 = ns2->next;
8554 ns2->next = ret;
8555 }
8556 return (ret);
8557ns_next_prefix:
8558 counter++;
8559 if (counter > 1000)
8560 return (NULL);
8561 if (prefix == NULL) {
8562 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008563 "ns_%d", counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008564 } else
8565 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008566 "%.30s_%d", (char *)prefix, counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008567 pref = BAD_CAST buf;
8568 }
8569}
8570
8571/*
8572* xmlDOMWrapNSNormAquireNormalizedNs:
8573* @doc: the doc
8574* @elem: the element-node to declare namespaces on
8575* @ns: the ns-struct to use for the search
8576* @retNs: the found/created ns-struct
8577* @nsMap: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008578* @depth: the current tree depth
8579* @ancestorsOnly: search in ancestor ns-decls only
8580* @prefixed: if the searched ns-decl must have a prefix (for attributes)
8581*
8582* Searches for a matching ns-name in the ns-decls of @nsMap, if not
8583* found it will either declare it on @elem, or store it in doc->oldNs.
8584* If a new ns-decl needs to be declared on @elem, it tries to use the
8585* @ns->prefix for it, if this prefix is already in use on @elem, it will
8586* change the prefix or the new ns-decl.
8587*
8588* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8589*/
8590static int
8591xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8592 xmlNodePtr elem,
8593 xmlNsPtr ns,
8594 xmlNsPtr *retNs,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008595 xmlNsMapPtr *nsMap,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008596
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008597 int depth,
8598 int ancestorsOnly,
8599 int prefixed)
8600{
Daniel Veillardaa6de472008-08-25 14:53:31 +00008601 xmlNsMapItemPtr mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008602
8603 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008604 (nsMap == NULL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008605 return (-1);
8606
8607 *retNs = NULL;
8608 /*
8609 * Handle XML namespace.
8610 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008611 if (IS_STR_XML(ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008612 /*
8613 * Insert XML namespace mapping.
8614 */
8615 *retNs = xmlTreeEnsureXMLDecl(doc);
8616 if (*retNs == NULL)
8617 return (-1);
8618 return (0);
8619 }
8620 /*
8621 * If the search should be done in ancestors only and no
8622 * @elem (the first ancestor) was specified, then skip the search.
8623 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008624 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8625 (! (ancestorsOnly && (elem == NULL))))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008626 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008627 /*
8628 * Try to find an equal ns-name in in-scope ns-decls.
8629 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008630 XML_NSMAP_FOREACH(*nsMap, mi) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008631 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8632 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008633 * ancestorsOnly: This should be turned on to gain speed,
8634 * if one knows that the branch itself was already
8635 * ns-wellformed and no stale references existed.
8636 * I.e. it searches in the ancestor axis only.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008637 */
8638 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8639 /* Skip shadowed prefixes. */
Daniel Veillardaa6de472008-08-25 14:53:31 +00008640 (mi->shadowDepth == -1) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008641 /* Skip xmlns="" or xmlns:foo="". */
8642 ((mi->newNs->href != NULL) &&
Daniel Veillardaa6de472008-08-25 14:53:31 +00008643 (mi->newNs->href[0] != 0)) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008644 /* Ensure a prefix if wanted. */
8645 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8646 /* Equal ns name */
8647 ((mi->newNs->href == ns->href) ||
8648 xmlStrEqual(mi->newNs->href, ns->href))) {
8649 /* Set the mapping. */
8650 mi->oldNs = ns;
8651 *retNs = mi->newNs;
8652 return (0);
8653 }
8654 }
8655 }
8656 /*
8657 * No luck, the namespace is out of scope or shadowed.
8658 */
8659 if (elem == NULL) {
8660 xmlNsPtr tmpns;
8661
8662 /*
8663 * Store ns-decls in "oldNs" of the document-node.
8664 */
8665 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8666 if (tmpns == NULL)
8667 return (-1);
8668 /*
8669 * Insert mapping.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008670 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008671 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008672 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8673 xmlFreeNs(tmpns);
8674 return (-1);
8675 }
8676 *retNs = tmpns;
8677 } else {
8678 xmlNsPtr tmpns;
8679
8680 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8681 ns->prefix, 0);
8682 if (tmpns == NULL)
8683 return (-1);
8684
8685 if (*nsMap != NULL) {
8686 /*
8687 * Does it shadow ancestor ns-decls?
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008688 */
8689 XML_NSMAP_FOREACH(*nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008690 if ((mi->depth < depth) &&
8691 (mi->shadowDepth == -1) &&
8692 ((ns->prefix == mi->newNs->prefix) ||
8693 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8694 /*
8695 * Shadows.
8696 */
8697 mi->shadowDepth = depth;
8698 break;
8699 }
8700 }
8701 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008702 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008703 xmlFreeNs(tmpns);
8704 return (-1);
8705 }
8706 *retNs = tmpns;
8707 }
8708 return (0);
8709}
8710
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008711typedef enum {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008712 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008713} xmlDOMReconcileNSOptions;
8714
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008715/*
8716* xmlDOMWrapReconcileNamespaces:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008717* @ctxt: DOM wrapper context, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008718* @elem: the element-node
8719* @options: option flags
8720*
8721* Ensures that ns-references point to ns-decls hold on element-nodes.
8722* Ensures that the tree is namespace wellformed by creating additional
8723* ns-decls where needed. Note that, since prefixes of already existent
8724* ns-decls can be shadowed by this process, it could break QNames in
8725* attribute values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008726*
8727* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008728*
8729* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008730*/
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008731
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008732int
8733xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8734 xmlNodePtr elem,
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008735 int options)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008736{
8737 int depth = -1, adoptns = 0, parnsdone = 0;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008738 xmlNsPtr ns, prevns;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008739 xmlDocPtr doc;
8740 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008741 xmlNsMapPtr nsMap = NULL;
8742 xmlNsMapItemPtr /* topmi = NULL, */ mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008743 /* @ancestorsOnly should be set by an option flag. */
8744 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008745 int optRemoveRedundantNS =
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008746 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8747 xmlNsPtr *listRedund = NULL;
8748 int sizeRedund = 0, nbRedund = 0, ret, i, j;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008749
8750 if ((elem == NULL) || (elem->doc == NULL) ||
8751 (elem->type != XML_ELEMENT_NODE))
8752 return (-1);
8753
8754 doc = elem->doc;
8755 cur = elem;
8756 do {
8757 switch (cur->type) {
8758 case XML_ELEMENT_NODE:
8759 adoptns = 1;
8760 curElem = cur;
8761 depth++;
8762 /*
8763 * Namespace declarations.
8764 */
8765 if (cur->nsDef != NULL) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008766 prevns = NULL;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008767 ns = cur->nsDef;
8768 while (ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008769 if (! parnsdone) {
8770 if ((elem->parent) &&
8771 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8772 /*
8773 * Gather ancestor in-scope ns-decls.
8774 */
8775 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8776 elem->parent) == -1)
8777 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008778 }
8779 parnsdone = 1;
8780 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008781
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008782 /*
8783 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8784 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008785 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008786 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008787 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8788 (mi->shadowDepth == -1) &&
8789 ((ns->prefix == mi->newNs->prefix) ||
8790 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8791 ((ns->href == mi->newNs->href) ||
8792 xmlStrEqual(ns->href, mi->newNs->href)))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008793 {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008794 /*
8795 * A redundant ns-decl was found.
8796 * Add it to the list of redundant ns-decls.
8797 */
8798 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8799 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8800 goto internal_error;
8801 /*
8802 * Remove the ns-decl from the element-node.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008803 */
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008804 if (prevns)
8805 prevns->next = ns->next;
8806 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00008807 cur->nsDef = ns->next;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008808 goto next_ns_decl;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008809 }
8810 }
8811 }
8812
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008813 /*
8814 * Skip ns-references handling if the referenced
8815 * ns-decl is declared on the same element.
8816 */
8817 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008818 adoptns = 0;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008819 /*
8820 * Does it shadow any ns-decl?
8821 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008822 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8823 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008824 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8825 (mi->shadowDepth == -1) &&
8826 ((ns->prefix == mi->newNs->prefix) ||
8827 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008828
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008829 mi->shadowDepth = depth;
8830 }
8831 }
8832 }
8833 /*
8834 * Push mapping.
8835 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008836 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008837 depth) == NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +00008838 goto internal_error;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008839
8840 prevns = ns;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008841next_ns_decl:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008842 ns = ns->next;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008843 }
8844 }
8845 if (! adoptns)
8846 goto ns_end;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008847 /* No break on purpose. */
8848 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008849 /* No ns, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008850 if (cur->ns == NULL)
8851 goto ns_end;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008852
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008853 if (! parnsdone) {
8854 if ((elem->parent) &&
8855 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8856 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8857 elem->parent) == -1)
8858 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008859 }
8860 parnsdone = 1;
8861 }
8862 /*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008863 * Adjust the reference if this was a redundant ns-decl.
8864 */
8865 if (listRedund) {
8866 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8867 if (cur->ns == listRedund[j]) {
8868 cur->ns = listRedund[++j];
8869 break;
8870 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008871 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008872 }
8873 /*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008874 * Adopt ns-references.
8875 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008876 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008877 /*
8878 * Search for a mapping.
8879 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008880 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008881 if ((mi->shadowDepth == -1) &&
8882 (cur->ns == mi->oldNs)) {
8883
8884 cur->ns = mi->newNs;
8885 goto ns_end;
8886 }
8887 }
8888 }
8889 /*
8890 * Aquire a normalized ns-decl and add it to the map.
8891 */
8892 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8893 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008894 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008895 ancestorsOnly,
8896 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8897 goto internal_error;
8898 cur->ns = ns;
8899
8900ns_end:
8901 if ((cur->type == XML_ELEMENT_NODE) &&
8902 (cur->properties != NULL)) {
8903 /*
8904 * Process attributes.
8905 */
8906 cur = (xmlNodePtr) cur->properties;
8907 continue;
8908 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008909 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008910 default:
8911 goto next_sibling;
8912 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008913into_content:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008914 if ((cur->type == XML_ELEMENT_NODE) &&
8915 (cur->children != NULL)) {
8916 /*
8917 * Process content of element-nodes only.
8918 */
8919 cur = cur->children;
8920 continue;
8921 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008922next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008923 if (cur == elem)
8924 break;
8925 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008926 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008927 /*
8928 * Pop mappings.
8929 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008930 while ((nsMap->last != NULL) &&
8931 (nsMap->last->depth >= depth))
8932 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008933 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008934 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008935 /*
8936 * Unshadow.
8937 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008938 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008939 if (mi->shadowDepth >= depth)
8940 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008941 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008942 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008943 depth--;
8944 }
8945 if (cur->next != NULL)
8946 cur = cur->next;
8947 else {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008948 if (cur->type == XML_ATTRIBUTE_NODE) {
8949 cur = cur->parent;
8950 goto into_content;
8951 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008952 cur = cur->parent;
8953 goto next_sibling;
8954 }
8955 } while (cur != NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008956
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008957 ret = 0;
8958 goto exit;
8959internal_error:
8960 ret = -1;
8961exit:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008962 if (listRedund) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008963 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8964 xmlFreeNs(listRedund[j]);
8965 }
8966 xmlFree(listRedund);
8967 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008968 if (nsMap != NULL)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008969 xmlDOMWrapNsMapFree(nsMap);
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008970 return (ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008971}
8972
8973/*
8974* xmlDOMWrapAdoptBranch:
8975* @ctxt: the optional context for custom processing
8976* @sourceDoc: the optional sourceDoc
8977* @node: the element-node to start with
8978* @destDoc: the destination doc for adoption
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008979* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008980* @options: option flags
8981*
8982* Ensures that ns-references point to @destDoc: either to
8983* elements->nsDef entries if @destParent is given, or to
8984* @destDoc->oldNs otherwise.
8985* If @destParent is given, it ensures that the tree is namespace
8986* wellformed by creating additional ns-decls where needed.
8987* Note that, since prefixes of already existent ns-decls can be
8988* shadowed by this process, it could break QNames in attribute
8989* values or element content.
8990*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008991* NOTE: This function was not intensively tested.
8992*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008993* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8994*/
8995static int
8996xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8997 xmlDocPtr sourceDoc,
8998 xmlNodePtr node,
8999 xmlDocPtr destDoc,
9000 xmlNodePtr destParent,
9001 int options ATTRIBUTE_UNUSED)
9002{
9003 int ret = 0;
9004 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009005 xmlNsMapPtr nsMap = NULL;
9006 xmlNsMapItemPtr mi;
Daniel Veillard11ce4002006-03-10 00:36:23 +00009007 xmlNsPtr ns = NULL;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009008 int depth = -1, adoptStr = 1;
9009 /* gather @parent's ns-decls. */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009010 int parnsdone;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009011 /* @ancestorsOnly should be set per option. */
9012 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009013
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009014 /*
9015 * Optimize string adoption for equal or none dicts.
9016 */
9017 if ((sourceDoc != NULL) &&
9018 (sourceDoc->dict == destDoc->dict))
9019 adoptStr = 0;
9020 else
9021 adoptStr = 1;
9022
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009023 /*
9024 * Get the ns-map from the context if available.
9025 */
9026 if (ctxt)
9027 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9028 /*
9029 * Disable search for ns-decls in the parent-axis of the
9030 * desination element, if:
9031 * 1) there's no destination parent
9032 * 2) custom ns-reference handling is used
9033 */
9034 if ((destParent == NULL) ||
9035 (ctxt && ctxt->getNsForNodeFunc))
9036 {
9037 parnsdone = 1;
9038 } else
9039 parnsdone = 0;
9040
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009041 cur = node;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08009042 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9043 goto internal_error;
9044
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009045 while (cur != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009046 /*
9047 * Paranoid source-doc sanity check.
9048 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009049 if (cur->doc != sourceDoc) {
9050 /*
9051 * We'll assume XIncluded nodes if the doc differs.
9052 * TODO: Do we need to reconciliate XIncluded nodes?
9053 * This here skips XIncluded nodes and tries to handle
9054 * broken sequences.
9055 */
9056 if (cur->next == NULL)
9057 goto leave_node;
9058 do {
9059 cur = cur->next;
9060 if ((cur->type == XML_XINCLUDE_END) ||
9061 (cur->doc == node->doc))
9062 break;
9063 } while (cur->next != NULL);
9064
9065 if (cur->doc != node->doc)
9066 goto leave_node;
9067 }
9068 cur->doc = destDoc;
9069 switch (cur->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009070 case XML_XINCLUDE_START:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009071 case XML_XINCLUDE_END:
9072 /*
9073 * TODO
9074 */
9075 return (-1);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009076 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009077 curElem = cur;
9078 depth++;
9079 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00009080 * Namespace declarations.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009081 * - ns->href and ns->prefix are never in the dict, so
9082 * we need not move the values over to the destination dict.
9083 * - Note that for custom handling of ns-references,
9084 * the ns-decls need not be stored in the ns-map,
9085 * since they won't be referenced by node->ns.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009086 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009087 if ((cur->nsDef) &&
9088 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
9089 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009090 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009091 /*
9092 * Gather @parent's in-scope ns-decls.
9093 */
9094 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9095 destParent) == -1)
9096 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009097 parnsdone = 1;
9098 }
9099 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9100 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009101 * NOTE: ns->prefix and ns->href are never in the dict.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009102 * XML_TREE_ADOPT_STR(ns->prefix)
9103 * XML_TREE_ADOPT_STR(ns->href)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009104 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009105 /*
9106 * Does it shadow any ns-decl?
Daniel Veillardaa6de472008-08-25 14:53:31 +00009107 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009108 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009109 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009110 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9111 (mi->shadowDepth == -1) &&
9112 ((ns->prefix == mi->newNs->prefix) ||
9113 xmlStrEqual(ns->prefix,
9114 mi->newNs->prefix))) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009115
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009116 mi->shadowDepth = depth;
9117 }
9118 }
9119 }
9120 /*
9121 * Push mapping.
9122 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009123 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009124 ns, ns, depth) == NULL)
9125 goto internal_error;
9126 }
9127 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009128 /* No break on purpose. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009129 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009130 /* No namespace, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009131 if (cur->ns == NULL)
9132 goto ns_end;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009133
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009134 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009135 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9136 destParent) == -1)
9137 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009138 parnsdone = 1;
9139 }
9140 /*
9141 * Adopt ns-references.
9142 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009143 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009144 /*
9145 * Search for a mapping.
9146 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009147 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009148 if ((mi->shadowDepth == -1) &&
9149 (cur->ns == mi->oldNs)) {
9150
9151 cur->ns = mi->newNs;
9152 goto ns_end;
9153 }
9154 }
9155 }
9156 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009157 * No matching namespace in scope. We need a new one.
9158 */
9159 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009160 /*
9161 * User-defined behaviour.
9162 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009163 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9164 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009165 /*
9166 * Insert mapping if ns is available; it's the users fault
9167 * if not.
9168 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009169 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009170 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009171 goto internal_error;
9172 cur->ns = ns;
9173 } else {
9174 /*
9175 * Aquire a normalized ns-decl and add it to the map.
9176 */
9177 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009178 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009179 destParent ? curElem : NULL,
9180 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009181 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009182 ancestorsOnly,
9183 /* ns-decls must be prefixed for attributes. */
9184 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9185 goto internal_error;
9186 cur->ns = ns;
9187 }
9188ns_end:
9189 /*
9190 * Further node properties.
9191 * TODO: Is this all?
9192 */
9193 XML_TREE_ADOPT_STR(cur->name)
9194 if (cur->type == XML_ELEMENT_NODE) {
9195 cur->psvi = NULL;
9196 cur->line = 0;
9197 cur->extra = 0;
9198 /*
9199 * Walk attributes.
9200 */
9201 if (cur->properties != NULL) {
9202 /*
9203 * Process first attribute node.
9204 */
9205 cur = (xmlNodePtr) cur->properties;
9206 continue;
9207 }
9208 } else {
9209 /*
9210 * Attributes.
9211 */
9212 if ((sourceDoc != NULL) &&
9213 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
Daniel Veillardaa6de472008-08-25 14:53:31 +00009214 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009215 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009216 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009217 ((xmlAttrPtr) cur)->atype = 0;
9218 ((xmlAttrPtr) cur)->psvi = NULL;
9219 }
9220 break;
9221 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009222 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009223 /*
9224 * This puts the content in the dest dict, only if
9225 * it was previously in the source dict.
9226 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009227 XML_TREE_ADOPT_STR_2(cur->content)
9228 goto leave_node;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009229 case XML_ENTITY_REF_NODE:
9230 /*
9231 * Remove reference to the entitity-node.
9232 */
9233 cur->content = NULL;
9234 cur->children = NULL;
9235 cur->last = NULL;
9236 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9237 xmlEntityPtr ent;
9238 /*
9239 * Assign new entity-node if available.
9240 */
9241 ent = xmlGetDocEntity(destDoc, cur->name);
9242 if (ent != NULL) {
9243 cur->content = ent->content;
9244 cur->children = (xmlNodePtr) ent;
9245 cur->last = (xmlNodePtr) ent;
9246 }
9247 }
9248 goto leave_node;
9249 case XML_PI_NODE:
9250 XML_TREE_ADOPT_STR(cur->name)
9251 XML_TREE_ADOPT_STR_2(cur->content)
9252 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009253 case XML_COMMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009254 break;
9255 default:
9256 goto internal_error;
9257 }
9258 /*
9259 * Walk the tree.
9260 */
9261 if (cur->children != NULL) {
9262 cur = cur->children;
9263 continue;
9264 }
9265
9266leave_node:
9267 if (cur == node)
9268 break;
9269 if ((cur->type == XML_ELEMENT_NODE) ||
9270 (cur->type == XML_XINCLUDE_START) ||
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009271 (cur->type == XML_XINCLUDE_END))
9272 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009273 /*
9274 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9275 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009276 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009277 /*
9278 * Pop mappings.
9279 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009280 while ((nsMap->last != NULL) &&
9281 (nsMap->last->depth >= depth))
9282 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009283 XML_NSMAP_POP(nsMap, mi)
9284 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009285 /*
9286 * Unshadow.
9287 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009288 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009289 if (mi->shadowDepth >= depth)
9290 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009291 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009292 }
9293 depth--;
9294 }
9295 if (cur->next != NULL)
9296 cur = cur->next;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009297 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9298 (cur->parent->children != NULL))
9299 {
9300 cur = cur->parent->children;
9301 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009302 cur = cur->parent;
9303 goto leave_node;
9304 }
9305 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009306
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009307 goto exit;
9308
Daniel Veillardaa6de472008-08-25 14:53:31 +00009309internal_error:
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009310 ret = -1;
9311
9312exit:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009313 /*
9314 * Cleanup.
9315 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009316 if (nsMap != NULL) {
9317 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9318 /*
9319 * Just cleanup the map but don't free.
9320 */
9321 if (nsMap->first) {
9322 if (nsMap->pool)
9323 nsMap->last->next = nsMap->pool;
9324 nsMap->pool = nsMap->first;
9325 nsMap->first = NULL;
9326 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009327 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009328 xmlDOMWrapNsMapFree(nsMap);
9329 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009330 return(ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009331}
9332
9333/*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009334* xmlDOMWrapCloneNode:
9335* @ctxt: the optional context for custom processing
9336* @sourceDoc: the optional sourceDoc
9337* @node: the node to start with
9338* @resNode: the clone of the given @node
9339* @destDoc: the destination doc
9340* @destParent: the optional new parent of @node in @destDoc
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00009341* @deep: descend into child if set
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009342* @options: option flags
9343*
9344* References of out-of scope ns-decls are remapped to point to @destDoc:
9345* 1) If @destParent is given, then nsDef entries on element-nodes are used
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009346* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9347* This is the case when you don't know already where the cloned branch
9348* will be added to.
Daniel Veillardaa6de472008-08-25 14:53:31 +00009349*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009350* If @destParent is given, it ensures that the tree is namespace
9351* wellformed by creating additional ns-decls where needed.
9352* Note that, since prefixes of already existent ns-decls can be
9353* shadowed by this process, it could break QNames in attribute
9354* values or element content.
9355* TODO:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009356* 1) What to do with XInclude? Currently this returns an error for XInclude.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009357*
9358* Returns 0 if the operation succeeded,
9359* 1 if a node of unsupported (or not yet supported) type was given,
9360* -1 on API/internal errors.
9361*/
9362
9363int
9364xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9365 xmlDocPtr sourceDoc,
9366 xmlNodePtr node,
9367 xmlNodePtr *resNode,
9368 xmlDocPtr destDoc,
9369 xmlNodePtr destParent,
9370 int deep,
9371 int options ATTRIBUTE_UNUSED)
9372{
9373 int ret = 0;
9374 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009375 xmlNsMapPtr nsMap = NULL;
9376 xmlNsMapItemPtr mi;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009377 xmlNsPtr ns;
9378 int depth = -1;
9379 /* int adoptStr = 1; */
9380 /* gather @parent's ns-decls. */
9381 int parnsdone = 0;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009382 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00009383 * @ancestorsOnly:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009384 * TODO: @ancestorsOnly should be set per option.
9385 *
9386 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009387 int ancestorsOnly = 0;
9388 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009389 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9390 xmlDictPtr dict; /* The destination dict */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009391
Daniel Veillard11ce4002006-03-10 00:36:23 +00009392 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009393 return(-1);
9394 /*
9395 * TODO: Initially we support only element-nodes.
9396 */
9397 if (node->type != XML_ELEMENT_NODE)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009398 return(1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009399 /*
9400 * Check node->doc sanity.
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009401 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009402 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9403 (node->doc != sourceDoc)) {
9404 /*
9405 * Might be an XIncluded node.
9406 */
9407 return (-1);
9408 }
9409 if (sourceDoc == NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009410 sourceDoc = node->doc;
Daniel Veillard11ce4002006-03-10 00:36:23 +00009411 if (sourceDoc == NULL)
9412 return (-1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009413
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009414 dict = destDoc->dict;
9415 /*
9416 * Reuse the namespace map of the context.
9417 */
9418 if (ctxt)
9419 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9420
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009421 *resNode = NULL;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009422
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009423 cur = node;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08009424 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9425 return(-1);
9426
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009427 while (cur != NULL) {
9428 if (cur->doc != sourceDoc) {
9429 /*
9430 * We'll assume XIncluded nodes if the doc differs.
9431 * TODO: Do we need to reconciliate XIncluded nodes?
9432 * TODO: This here returns -1 in this case.
9433 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009434 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009435 }
9436 /*
9437 * Create a new node.
9438 */
9439 switch (cur->type) {
9440 case XML_XINCLUDE_START:
9441 case XML_XINCLUDE_END:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009442 /*
9443 * TODO: What to do with XInclude?
9444 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009445 goto internal_error;
9446 break;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009447 case XML_ELEMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009448 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009449 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009450 case XML_COMMENT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009451 case XML_PI_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009452 case XML_DOCUMENT_FRAG_NODE:
9453 case XML_ENTITY_REF_NODE:
9454 case XML_ENTITY_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009455 /*
9456 * Nodes of xmlNode structure.
9457 */
9458 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9459 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009460 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009461 goto internal_error;
9462 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009463 memset(clone, 0, sizeof(xmlNode));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009464 /*
9465 * Set hierachical links.
9466 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009467 if (resultClone != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009468 clone->parent = parentClone;
9469 if (prevClone) {
9470 prevClone->next = clone;
9471 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009472 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009473 parentClone->children = clone;
9474 } else
9475 resultClone = clone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009476
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009477 break;
9478 case XML_ATTRIBUTE_NODE:
9479 /*
9480 * Attributes (xmlAttr).
9481 */
9482 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9483 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009484 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009485 goto internal_error;
9486 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009487 memset(clone, 0, sizeof(xmlAttr));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009488 /*
9489 * Set hierachical links.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009490 * TODO: Change this to add to the end of attributes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009491 */
9492 if (resultClone != NULL) {
9493 clone->parent = parentClone;
9494 if (prevClone) {
9495 prevClone->next = clone;
9496 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009497 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009498 parentClone->properties = (xmlAttrPtr) clone;
9499 } else
9500 resultClone = clone;
9501 break;
9502 default:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009503 /*
9504 * TODO QUESTION: Any other nodes expected?
9505 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009506 goto internal_error;
9507 }
9508
9509 clone->type = cur->type;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009510 clone->doc = destDoc;
9511
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009512 /*
9513 * Clone the name of the node if any.
9514 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009515 if (cur->name == xmlStringText)
9516 clone->name = xmlStringText;
9517 else if (cur->name == xmlStringTextNoenc)
9518 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009519 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9520 * in tree.c, it might be set in Libxslt via
Daniel Veillardaa6de472008-08-25 14:53:31 +00009521 * "xsl:disable-output-escaping".
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009522 */
9523 clone->name = xmlStringTextNoenc;
9524 else if (cur->name == xmlStringComment)
9525 clone->name = xmlStringComment;
9526 else if (cur->name != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009527 DICT_CONST_COPY(cur->name, clone->name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009528 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009529
9530 switch (cur->type) {
9531 case XML_XINCLUDE_START:
9532 case XML_XINCLUDE_END:
9533 /*
9534 * TODO
9535 */
9536 return (-1);
9537 case XML_ELEMENT_NODE:
9538 curElem = cur;
9539 depth++;
9540 /*
9541 * Namespace declarations.
9542 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009543 if (cur->nsDef != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009544 if (! parnsdone) {
9545 if (destParent && (ctxt == NULL)) {
9546 /*
9547 * Gather @parent's in-scope ns-decls.
9548 */
9549 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9550 destParent) == -1)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009551 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009552 }
9553 parnsdone = 1;
9554 }
9555 /*
9556 * Clone namespace declarations.
9557 */
9558 cloneNsDefSlot = &(clone->nsDef);
9559 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9560 /*
9561 * Create a new xmlNs.
9562 */
9563 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9564 if (cloneNs == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009565 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009566 "allocating namespace");
9567 return(-1);
9568 }
9569 memset(cloneNs, 0, sizeof(xmlNs));
9570 cloneNs->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009571
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009572 if (ns->href != NULL)
9573 cloneNs->href = xmlStrdup(ns->href);
9574 if (ns->prefix != NULL)
9575 cloneNs->prefix = xmlStrdup(ns->prefix);
9576
9577 *cloneNsDefSlot = cloneNs;
9578 cloneNsDefSlot = &(cloneNs->next);
9579
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009580 /*
9581 * Note that for custom handling of ns-references,
9582 * the ns-decls need not be stored in the ns-map,
9583 * since they won't be referenced by node->ns.
9584 */
9585 if ((ctxt == NULL) ||
9586 (ctxt->getNsForNodeFunc == NULL))
9587 {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009588 /*
9589 * Does it shadow any ns-decl?
9590 */
9591 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009592 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009593 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9594 (mi->shadowDepth == -1) &&
9595 ((ns->prefix == mi->newNs->prefix) ||
9596 xmlStrEqual(ns->prefix,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009597 mi->newNs->prefix))) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009598 /*
9599 * Mark as shadowed at the current
9600 * depth.
9601 */
9602 mi->shadowDepth = depth;
9603 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009604 }
9605 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009606 /*
9607 * Push mapping.
9608 */
9609 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9610 ns, cloneNs, depth) == NULL)
9611 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009612 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009613 }
9614 }
9615 /* cur->ns will be processed further down. */
9616 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009617 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009618 /* IDs will be processed further down. */
9619 /* cur->ns will be processed further down. */
9620 break;
9621 case XML_TEXT_NODE:
9622 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009623 /*
9624 * Note that this will also cover the values of attributes.
9625 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009626 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009627 goto leave_node;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009628 case XML_ENTITY_NODE:
9629 /* TODO: What to do here? */
9630 goto leave_node;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009631 case XML_ENTITY_REF_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009632 if (sourceDoc != destDoc) {
9633 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9634 xmlEntityPtr ent;
9635 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009636 * Different doc: Assign new entity-node if available.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009637 */
9638 ent = xmlGetDocEntity(destDoc, cur->name);
9639 if (ent != NULL) {
9640 clone->content = ent->content;
9641 clone->children = (xmlNodePtr) ent;
9642 clone->last = (xmlNodePtr) ent;
9643 }
9644 }
9645 } else {
9646 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009647 * Same doc: Use the current node's entity declaration
9648 * and value.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009649 */
9650 clone->content = cur->content;
9651 clone->children = cur->children;
9652 clone->last = cur->last;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009653 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009654 goto leave_node;
9655 case XML_PI_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009656 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009657 goto leave_node;
9658 case XML_COMMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009659 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009660 goto leave_node;
9661 default:
9662 goto internal_error;
9663 }
9664
9665 if (cur->ns == NULL)
9666 goto end_ns_reference;
9667
9668/* handle_ns_reference: */
9669 /*
9670 ** The following will take care of references to ns-decls ********
Daniel Veillardaa6de472008-08-25 14:53:31 +00009671 ** and is intended only for element- and attribute-nodes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009672 **
9673 */
9674 if (! parnsdone) {
9675 if (destParent && (ctxt == NULL)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009676 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009677 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009678 }
9679 parnsdone = 1;
9680 }
9681 /*
9682 * Adopt ns-references.
9683 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009684 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009685 /*
9686 * Search for a mapping.
9687 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009688 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009689 if ((mi->shadowDepth == -1) &&
9690 (cur->ns == mi->oldNs)) {
9691 /*
9692 * This is the nice case: a mapping was found.
9693 */
9694 clone->ns = mi->newNs;
9695 goto end_ns_reference;
9696 }
9697 }
9698 }
9699 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009700 * No matching namespace in scope. We need a new one.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009701 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009702 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009703 /*
9704 * User-defined behaviour.
9705 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009706 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9707 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009708 /*
9709 * Add user's mapping.
9710 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009711 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009712 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9713 goto internal_error;
9714 clone->ns = ns;
9715 } else {
9716 /*
9717 * Aquire a normalized ns-decl and add it to the map.
9718 */
9719 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009720 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009721 destParent ? curElem : NULL,
9722 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009723 &nsMap, depth,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009724 /* if we need to search only in the ancestor-axis */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009725 ancestorsOnly,
9726 /* ns-decls must be prefixed for attributes. */
9727 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9728 goto internal_error;
9729 clone->ns = ns;
9730 }
9731
9732end_ns_reference:
9733
9734 /*
9735 * Some post-processing.
9736 *
9737 * Handle ID attributes.
9738 */
9739 if ((clone->type == XML_ATTRIBUTE_NODE) &&
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009740 (clone->parent != NULL))
9741 {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009742 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009743
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009744 xmlChar *idVal;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009745
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009746 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9747 if (idVal != NULL) {
9748 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9749 /* TODO: error message. */
9750 xmlFree(idVal);
9751 goto internal_error;
9752 }
9753 xmlFree(idVal);
9754 }
9755 }
9756 }
9757 /*
9758 **
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009759 ** The following will traverse the tree **************************
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009760 **
Daniel Veillardaa6de472008-08-25 14:53:31 +00009761 *
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009762 * Walk the element's attributes before descending into child-nodes.
9763 */
9764 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9765 prevClone = NULL;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009766 parentClone = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009767 cur = (xmlNodePtr) cur->properties;
9768 continue;
9769 }
9770into_content:
9771 /*
9772 * Descend into child-nodes.
9773 */
9774 if (cur->children != NULL) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009775 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9776 prevClone = NULL;
9777 parentClone = clone;
9778 cur = cur->children;
9779 continue;
9780 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009781 }
9782
9783leave_node:
9784 /*
9785 * At this point we are done with the node, its content
9786 * and an element-nodes's attribute-nodes.
9787 */
9788 if (cur == node)
9789 break;
9790 if ((cur->type == XML_ELEMENT_NODE) ||
9791 (cur->type == XML_XINCLUDE_START) ||
9792 (cur->type == XML_XINCLUDE_END)) {
9793 /*
9794 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9795 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009796 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009797 /*
9798 * Pop mappings.
9799 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009800 while ((nsMap->last != NULL) &&
9801 (nsMap->last->depth >= depth))
9802 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009803 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009804 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009805 /*
9806 * Unshadow.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009807 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009808 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009809 if (mi->shadowDepth >= depth)
9810 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009811 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009812 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009813 depth--;
9814 }
9815 if (cur->next != NULL) {
9816 prevClone = clone;
9817 cur = cur->next;
9818 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9819 /*
9820 * Set clone->last.
9821 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00009822 if (clone->parent != NULL)
9823 clone->parent->last = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009824 clone = clone->parent;
Daniel Veillard75d13092013-09-11 15:11:27 +08009825 if (clone != NULL)
9826 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009827 /*
9828 * Process parent --> next;
9829 */
9830 cur = cur->parent;
9831 goto leave_node;
9832 } else {
9833 /* This is for attributes only. */
9834 clone = clone->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009835 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009836 /*
9837 * Process parent-element --> children.
9838 */
9839 cur = cur->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009840 goto into_content;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009841 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009842 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009843 goto exit;
9844
9845internal_error:
9846 ret = -1;
9847
9848exit:
9849 /*
9850 * Cleanup.
9851 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009852 if (nsMap != NULL) {
9853 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9854 /*
9855 * Just cleanup the map but don't free.
9856 */
9857 if (nsMap->first) {
9858 if (nsMap->pool)
9859 nsMap->last->next = nsMap->pool;
9860 nsMap->pool = nsMap->first;
9861 nsMap->first = NULL;
9862 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009863 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009864 xmlDOMWrapNsMapFree(nsMap);
9865 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009866 /*
9867 * TODO: Should we try a cleanup of the cloned node in case of a
9868 * fatal error?
9869 */
9870 *resNode = resultClone;
9871 return (ret);
9872}
9873
9874/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009875* xmlDOMWrapAdoptAttr:
9876* @ctxt: the optional context for custom processing
9877* @sourceDoc: the optional source document of attr
9878* @attr: the attribute-node to be adopted
9879* @destDoc: the destination doc for adoption
9880* @destParent: the optional new parent of @attr in @destDoc
9881* @options: option flags
9882*
9883* @attr is adopted by @destDoc.
9884* Ensures that ns-references point to @destDoc: either to
9885* elements->nsDef entries if @destParent is given, or to
9886* @destDoc->oldNs otherwise.
9887*
9888* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9889*/
9890static int
9891xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9892 xmlDocPtr sourceDoc,
9893 xmlAttrPtr attr,
9894 xmlDocPtr destDoc,
9895 xmlNodePtr destParent,
9896 int options ATTRIBUTE_UNUSED)
9897{
9898 xmlNodePtr cur;
9899 int adoptStr = 1;
9900
9901 if ((attr == NULL) || (destDoc == NULL))
9902 return (-1);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009903
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009904 attr->doc = destDoc;
9905 if (attr->ns != NULL) {
9906 xmlNsPtr ns = NULL;
9907
9908 if (ctxt != NULL) {
9909 /* TODO: User defined. */
9910 }
9911 /* XML Namespace. */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009912 if (IS_STR_XML(attr->ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009913 ns = xmlTreeEnsureXMLDecl(destDoc);
9914 } else if (destParent == NULL) {
9915 /*
9916 * Store in @destDoc->oldNs.
9917 */
9918 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9919 } else {
9920 /*
9921 * Declare on @destParent.
9922 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009923 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009924 &ns, 1) == -1)
9925 goto internal_error;
9926 if (ns == NULL) {
9927 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9928 attr->ns->href, attr->ns->prefix, 1);
9929 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009930 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009931 if (ns == NULL)
9932 goto internal_error;
9933 attr->ns = ns;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009934 }
9935
9936 XML_TREE_ADOPT_STR(attr->name);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009937 attr->atype = 0;
9938 attr->psvi = NULL;
9939 /*
9940 * Walk content.
9941 */
9942 if (attr->children == NULL)
9943 return (0);
9944 cur = attr->children;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08009945 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9946 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009947 while (cur != NULL) {
9948 cur->doc = destDoc;
9949 switch (cur->type) {
9950 case XML_TEXT_NODE:
9951 case XML_CDATA_SECTION_NODE:
9952 XML_TREE_ADOPT_STR_2(cur->content)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009953 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009954 case XML_ENTITY_REF_NODE:
9955 /*
9956 * Remove reference to the entitity-node.
9957 */
9958 cur->content = NULL;
9959 cur->children = NULL;
9960 cur->last = NULL;
9961 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9962 xmlEntityPtr ent;
9963 /*
9964 * Assign new entity-node if available.
9965 */
9966 ent = xmlGetDocEntity(destDoc, cur->name);
9967 if (ent != NULL) {
9968 cur->content = ent->content;
9969 cur->children = (xmlNodePtr) ent;
9970 cur->last = (xmlNodePtr) ent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009971 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009972 }
9973 break;
9974 default:
9975 break;
9976 }
9977 if (cur->children != NULL) {
9978 cur = cur->children;
9979 continue;
9980 }
9981next_sibling:
9982 if (cur == (xmlNodePtr) attr)
9983 break;
9984 if (cur->next != NULL)
9985 cur = cur->next;
9986 else {
9987 cur = cur->parent;
9988 goto next_sibling;
9989 }
9990 }
9991 return (0);
9992internal_error:
9993 return (-1);
9994}
9995
9996/*
9997* xmlDOMWrapAdoptNode:
9998* @ctxt: the optional context for custom processing
9999* @sourceDoc: the optional sourceDoc
10000* @node: the node to start with
10001* @destDoc: the destination doc
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +000010002* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010003* @options: option flags
10004*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010005* References of out-of scope ns-decls are remapped to point to @destDoc:
10006* 1) If @destParent is given, then nsDef entries on element-nodes are used
10007* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
Daniel Veillard7e35abe2014-03-28 22:55:31 +080010008* This is the case when you have an unlinked node and just want to move it
Daniel Veillardaa6de472008-08-25 14:53:31 +000010009* to the context of
10010*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010011* If @destParent is given, it ensures that the tree is namespace
10012* wellformed by creating additional ns-decls where needed.
10013* Note that, since prefixes of already existent ns-decls can be
10014* shadowed by this process, it could break QNames in attribute
10015* values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000010016* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010017*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010018* Returns 0 if the operation succeeded,
10019* 1 if a node of unsupported type was given,
10020* 2 if a node of not yet supported type was given and
10021* -1 on API/internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010022*/
10023int
10024xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
10025 xmlDocPtr sourceDoc,
10026 xmlNodePtr node,
Daniel Veillardaa6de472008-08-25 14:53:31 +000010027 xmlDocPtr destDoc,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010028 xmlNodePtr destParent,
10029 int options)
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010030{
Daniel Veillard3e62adb2012-08-09 14:24:02 +080010031 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
10032 (destDoc == NULL) ||
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010033 ((destParent != NULL) && (destParent->doc != destDoc)))
10034 return(-1);
10035 /*
10036 * Check node->doc sanity.
Daniel Veillardaa6de472008-08-25 14:53:31 +000010037 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010038 if ((node->doc != NULL) && (sourceDoc != NULL) &&
10039 (node->doc != sourceDoc)) {
10040 /*
10041 * Might be an XIncluded node.
10042 */
10043 return (-1);
10044 }
10045 if (sourceDoc == NULL)
10046 sourceDoc = node->doc;
10047 if (sourceDoc == destDoc)
10048 return (-1);
10049 switch (node->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +000010050 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010051 case XML_ATTRIBUTE_NODE:
10052 case XML_TEXT_NODE:
10053 case XML_CDATA_SECTION_NODE:
10054 case XML_ENTITY_REF_NODE:
10055 case XML_PI_NODE:
10056 case XML_COMMENT_NODE:
10057 break;
10058 case XML_DOCUMENT_FRAG_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010059 /* TODO: Support document-fragment-nodes. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010060 return (2);
10061 default:
10062 return (1);
10063 }
10064 /*
10065 * Unlink only if @node was not already added to @destParent.
10066 */
10067 if ((node->parent != NULL) && (destParent != node->parent))
10068 xmlUnlinkNode(node);
10069
10070 if (node->type == XML_ELEMENT_NODE) {
10071 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
10072 destDoc, destParent, options));
10073 } else if (node->type == XML_ATTRIBUTE_NODE) {
10074 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
10075 (xmlAttrPtr) node, destDoc, destParent, options));
Daniel Veillardaa6de472008-08-25 14:53:31 +000010076 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010077 xmlNodePtr cur = node;
10078 int adoptStr = 1;
10079
10080 cur->doc = destDoc;
10081 /*
10082 * Optimize string adoption.
10083 */
10084 if ((sourceDoc != NULL) &&
10085 (sourceDoc->dict == destDoc->dict))
10086 adoptStr = 0;
10087 switch (node->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +000010088 case XML_TEXT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010089 case XML_CDATA_SECTION_NODE:
10090 XML_TREE_ADOPT_STR_2(node->content)
10091 break;
10092 case XML_ENTITY_REF_NODE:
10093 /*
10094 * Remove reference to the entitity-node.
10095 */
10096 node->content = NULL;
10097 node->children = NULL;
10098 node->last = NULL;
10099 if ((destDoc->intSubset) || (destDoc->extSubset)) {
10100 xmlEntityPtr ent;
10101 /*
10102 * Assign new entity-node if available.
10103 */
10104 ent = xmlGetDocEntity(destDoc, node->name);
10105 if (ent != NULL) {
10106 node->content = ent->content;
10107 node->children = (xmlNodePtr) ent;
10108 node->last = (xmlNodePtr) ent;
10109 }
10110 }
10111 XML_TREE_ADOPT_STR(node->name)
10112 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +000010113 case XML_PI_NODE: {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010114 XML_TREE_ADOPT_STR(node->name)
10115 XML_TREE_ADOPT_STR_2(node->content)
10116 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +000010117 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010118 default:
10119 break;
10120 }
Daniel Veillardaa6de472008-08-25 14:53:31 +000010121 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010122 return (0);
10123}
10124
Daniel Veillard5d4644e2005-04-01 13:11:58 +000010125#define bottom_tree
10126#include "elfgcchack.h"